[AArch64] Avoid GET_MODE_NUNITS in v8.4 support
[official-gcc.git] / gcc / cp / parser.c
blob80d65a84af09292639260ae1e4df041934fde5be
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static bool cp_parser_extension_opt
2397 (cp_parser *, int *);
2398 static void cp_parser_label_declaration
2399 (cp_parser *);
2401 /* Concept Extensions */
2403 static tree cp_parser_requires_clause
2404 (cp_parser *);
2405 static tree cp_parser_requires_clause_opt
2406 (cp_parser *);
2407 static tree cp_parser_requires_expression
2408 (cp_parser *);
2409 static tree cp_parser_requirement_parameter_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement_body
2412 (cp_parser *);
2413 static tree cp_parser_requirement_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement
2416 (cp_parser *);
2417 static tree cp_parser_simple_requirement
2418 (cp_parser *);
2419 static tree cp_parser_compound_requirement
2420 (cp_parser *);
2421 static tree cp_parser_type_requirement
2422 (cp_parser *);
2423 static tree cp_parser_nested_requirement
2424 (cp_parser *);
2426 /* Transactional Memory Extensions */
2428 static tree cp_parser_transaction
2429 (cp_parser *, cp_token *);
2430 static tree cp_parser_transaction_expression
2431 (cp_parser *, enum rid);
2432 static void cp_parser_function_transaction
2433 (cp_parser *, enum rid);
2434 static tree cp_parser_transaction_cancel
2435 (cp_parser *);
2437 enum pragma_context {
2438 pragma_external,
2439 pragma_member,
2440 pragma_objc_icode,
2441 pragma_stmt,
2442 pragma_compound
2444 static bool cp_parser_pragma
2445 (cp_parser *, enum pragma_context, bool *);
2447 /* Objective-C++ Productions */
2449 static tree cp_parser_objc_message_receiver
2450 (cp_parser *);
2451 static tree cp_parser_objc_message_args
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_expression
2454 (cp_parser *);
2455 static cp_expr cp_parser_objc_encode_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_defs_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_protocol_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_selector_expression
2462 (cp_parser *);
2463 static cp_expr cp_parser_objc_expression
2464 (cp_parser *);
2465 static bool cp_parser_objc_selector_p
2466 (enum cpp_ttype);
2467 static tree cp_parser_objc_selector
2468 (cp_parser *);
2469 static tree cp_parser_objc_protocol_refs_opt
2470 (cp_parser *);
2471 static void cp_parser_objc_declaration
2472 (cp_parser *, tree);
2473 static tree cp_parser_objc_statement
2474 (cp_parser *);
2475 static bool cp_parser_objc_valid_prefix_attributes
2476 (cp_parser *, tree *);
2477 static void cp_parser_objc_at_property_declaration
2478 (cp_parser *) ;
2479 static void cp_parser_objc_at_synthesize_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_dynamic_declaration
2482 (cp_parser *) ;
2483 static tree cp_parser_objc_struct_declaration
2484 (cp_parser *) ;
2486 /* Utility Routines */
2488 static cp_expr cp_parser_lookup_name
2489 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2490 static tree cp_parser_lookup_name_simple
2491 (cp_parser *, tree, location_t);
2492 static tree cp_parser_maybe_treat_template_as_class
2493 (tree, bool);
2494 static bool cp_parser_check_declarator_template_parameters
2495 (cp_parser *, cp_declarator *, location_t);
2496 static bool cp_parser_check_template_parameters
2497 (cp_parser *, unsigned, location_t, cp_declarator *);
2498 static cp_expr cp_parser_simple_cast_expression
2499 (cp_parser *);
2500 static tree cp_parser_global_scope_opt
2501 (cp_parser *, bool);
2502 static bool cp_parser_constructor_declarator_p
2503 (cp_parser *, bool);
2504 static tree cp_parser_function_definition_from_specifiers_and_declarator
2505 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2506 static tree cp_parser_function_definition_after_declarator
2507 (cp_parser *, bool);
2508 static bool cp_parser_template_declaration_after_export
2509 (cp_parser *, bool);
2510 static void cp_parser_perform_template_parameter_access_checks
2511 (vec<deferred_access_check, va_gc> *);
2512 static tree cp_parser_single_declaration
2513 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2514 static cp_expr cp_parser_functional_cast
2515 (cp_parser *, tree);
2516 static tree cp_parser_save_member_function_body
2517 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2518 static tree cp_parser_save_nsdmi
2519 (cp_parser *);
2520 static tree cp_parser_enclosed_template_argument_list
2521 (cp_parser *);
2522 static void cp_parser_save_default_args
2523 (cp_parser *, tree);
2524 static void cp_parser_late_parsing_for_member
2525 (cp_parser *, tree);
2526 static tree cp_parser_late_parse_one_default_arg
2527 (cp_parser *, tree, tree, tree);
2528 static void cp_parser_late_parsing_nsdmi
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_default_args
2531 (cp_parser *, tree);
2532 static tree cp_parser_sizeof_operand
2533 (cp_parser *, enum rid);
2534 static cp_expr cp_parser_trait_expr
2535 (cp_parser *, enum rid);
2536 static bool cp_parser_declares_only_class_p
2537 (cp_parser *);
2538 static void cp_parser_set_storage_class
2539 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2540 static void cp_parser_set_decl_spec_type
2541 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2542 static void set_and_check_decl_spec_loc
2543 (cp_decl_specifier_seq *decl_specs,
2544 cp_decl_spec ds, cp_token *);
2545 static bool cp_parser_friend_p
2546 (const cp_decl_specifier_seq *);
2547 static void cp_parser_required_error
2548 (cp_parser *, required_token, bool, location_t);
2549 static cp_token *cp_parser_require
2550 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2551 static cp_token *cp_parser_require_keyword
2552 (cp_parser *, enum rid, required_token);
2553 static bool cp_parser_token_starts_function_definition_p
2554 (cp_token *);
2555 static bool cp_parser_next_token_starts_class_definition_p
2556 (cp_parser *);
2557 static bool cp_parser_next_token_ends_template_argument_p
2558 (cp_parser *);
2559 static bool cp_parser_nth_token_starts_template_argument_list_p
2560 (cp_parser *, size_t);
2561 static enum tag_types cp_parser_token_is_class_key
2562 (cp_token *);
2563 static enum tag_types cp_parser_token_is_type_parameter_key
2564 (cp_token *);
2565 static void cp_parser_check_class_key
2566 (enum tag_types, tree type);
2567 static void cp_parser_check_access_in_redeclaration
2568 (tree type, location_t location);
2569 static bool cp_parser_optional_template_keyword
2570 (cp_parser *);
2571 static void cp_parser_pre_parsed_nested_name_specifier
2572 (cp_parser *);
2573 static bool cp_parser_cache_group
2574 (cp_parser *, enum cpp_ttype, unsigned);
2575 static tree cp_parser_cache_defarg
2576 (cp_parser *parser, bool nsdmi);
2577 static void cp_parser_parse_tentatively
2578 (cp_parser *);
2579 static void cp_parser_commit_to_tentative_parse
2580 (cp_parser *);
2581 static void cp_parser_commit_to_topmost_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_abort_tentative_parse
2584 (cp_parser *);
2585 static bool cp_parser_parse_definitely
2586 (cp_parser *);
2587 static inline bool cp_parser_parsing_tentatively
2588 (cp_parser *);
2589 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 (cp_parser *);
2591 static void cp_parser_error
2592 (cp_parser *, const char *);
2593 static void cp_parser_name_lookup_error
2594 (cp_parser *, tree, tree, name_lookup_error, location_t);
2595 static bool cp_parser_simulate_error
2596 (cp_parser *);
2597 static bool cp_parser_check_type_definition
2598 (cp_parser *);
2599 static void cp_parser_check_for_definition_in_return_type
2600 (cp_declarator *, tree, location_t type_location);
2601 static void cp_parser_check_for_invalid_template_id
2602 (cp_parser *, tree, enum tag_types, location_t location);
2603 static bool cp_parser_non_integral_constant_expression
2604 (cp_parser *, non_integral_constant);
2605 static void cp_parser_diagnose_invalid_type_name
2606 (cp_parser *, tree, location_t);
2607 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 (cp_parser *);
2609 static int cp_parser_skip_to_closing_parenthesis
2610 (cp_parser *, bool, bool, bool);
2611 static void cp_parser_skip_to_end_of_statement
2612 (cp_parser *);
2613 static void cp_parser_consume_semicolon_at_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_block_or_statement
2616 (cp_parser *);
2617 static bool cp_parser_skip_to_closing_brace
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_template_parameter_list
2620 (cp_parser *);
2621 static void cp_parser_skip_to_pragma_eol
2622 (cp_parser*, cp_token *);
2623 static bool cp_parser_error_occurred
2624 (cp_parser *);
2625 static bool cp_parser_allow_gnu_extensions_p
2626 (cp_parser *);
2627 static bool cp_parser_is_pure_string_literal
2628 (cp_token *);
2629 static bool cp_parser_is_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_keyword
2632 (cp_token *, enum rid);
2633 static tree cp_parser_make_typename_type
2634 (cp_parser *, tree, location_t location);
2635 static cp_declarator * cp_parser_make_indirect_declarator
2636 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2637 static bool cp_parser_compound_literal_p
2638 (cp_parser *);
2639 static bool cp_parser_array_designator_p
2640 (cp_parser *);
2641 static bool cp_parser_init_statement_p
2642 (cp_parser *);
2643 static bool cp_parser_skip_to_closing_square_bracket
2644 (cp_parser *);
2646 /* Concept-related syntactic transformations */
2648 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2649 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2651 // -------------------------------------------------------------------------- //
2652 // Unevaluated Operand Guard
2654 // Implementation of an RAII helper for unevaluated operand parsing.
2655 cp_unevaluated::cp_unevaluated ()
2657 ++cp_unevaluated_operand;
2658 ++c_inhibit_evaluation_warnings;
2661 cp_unevaluated::~cp_unevaluated ()
2663 --c_inhibit_evaluation_warnings;
2664 --cp_unevaluated_operand;
2667 // -------------------------------------------------------------------------- //
2668 // Tentative Parsing
2670 /* Returns nonzero if we are parsing tentatively. */
2672 static inline bool
2673 cp_parser_parsing_tentatively (cp_parser* parser)
2675 return parser->context->next != NULL;
2678 /* Returns nonzero if TOKEN is a string literal. */
2680 static bool
2681 cp_parser_is_pure_string_literal (cp_token* token)
2683 return (token->type == CPP_STRING ||
2684 token->type == CPP_STRING16 ||
2685 token->type == CPP_STRING32 ||
2686 token->type == CPP_WSTRING ||
2687 token->type == CPP_UTF8STRING);
2690 /* Returns nonzero if TOKEN is a string literal
2691 of a user-defined string literal. */
2693 static bool
2694 cp_parser_is_string_literal (cp_token* token)
2696 return (cp_parser_is_pure_string_literal (token) ||
2697 token->type == CPP_STRING_USERDEF ||
2698 token->type == CPP_STRING16_USERDEF ||
2699 token->type == CPP_STRING32_USERDEF ||
2700 token->type == CPP_WSTRING_USERDEF ||
2701 token->type == CPP_UTF8STRING_USERDEF);
2704 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 static bool
2707 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2709 return token->keyword == keyword;
2712 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2713 PRAGMA_NONE. */
2715 static enum pragma_kind
2716 cp_parser_pragma_kind (cp_token *token)
2718 if (token->type != CPP_PRAGMA)
2719 return PRAGMA_NONE;
2720 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2721 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2724 /* Helper function for cp_parser_error.
2725 Having peeked a token of kind TOK1_KIND that might signify
2726 a conflict marker, peek successor tokens to determine
2727 if we actually do have a conflict marker.
2728 Specifically, we consider a run of 7 '<', '=' or '>' characters
2729 at the start of a line as a conflict marker.
2730 These come through the lexer as three pairs and a single,
2731 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2732 If it returns true, *OUT_LOC is written to with the location/range
2733 of the marker. */
2735 static bool
2736 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2737 location_t *out_loc)
2739 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2740 if (token2->type != tok1_kind)
2741 return false;
2742 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2743 if (token3->type != tok1_kind)
2744 return false;
2745 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2746 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2747 return false;
2749 /* It must be at the start of the line. */
2750 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2751 if (LOCATION_COLUMN (start_loc) != 1)
2752 return false;
2754 /* We have a conflict marker. Construct a location of the form:
2755 <<<<<<<
2756 ^~~~~~~
2757 with start == caret, finishing at the end of the marker. */
2758 location_t finish_loc = get_finish (token4->location);
2759 *out_loc = make_location (start_loc, start_loc, finish_loc);
2761 return true;
2764 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2765 RT_CLOSE_PAREN. */
2767 static const char *
2768 get_matching_symbol (required_token token_desc)
2770 switch (token_desc)
2772 default:
2773 gcc_unreachable ();
2774 return "";
2775 case RT_CLOSE_BRACE:
2776 return "{";
2777 case RT_CLOSE_PAREN:
2778 return "(";
2782 /* Attempt to convert TOKEN_DESC from a required_token to an
2783 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785 static enum cpp_ttype
2786 get_required_cpp_ttype (required_token token_desc)
2788 switch (token_desc)
2790 case RT_SEMICOLON:
2791 return CPP_SEMICOLON;
2792 case RT_OPEN_PAREN:
2793 return CPP_OPEN_PAREN;
2794 case RT_CLOSE_BRACE:
2795 return CPP_CLOSE_BRACE;
2796 case RT_OPEN_BRACE:
2797 return CPP_OPEN_BRACE;
2798 case RT_CLOSE_SQUARE:
2799 return CPP_CLOSE_SQUARE;
2800 case RT_OPEN_SQUARE:
2801 return CPP_OPEN_SQUARE;
2802 case RT_COMMA:
2803 return CPP_COMMA;
2804 case RT_COLON:
2805 return CPP_COLON;
2806 case RT_CLOSE_PAREN:
2807 return CPP_CLOSE_PAREN;
2809 default:
2810 /* Use CPP_EOF as a "no completions possible" code. */
2811 return CPP_EOF;
2816 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818 Issue a diagnostic of the form
2819 FILE:LINE: MESSAGE before TOKEN
2820 where TOKEN is the next token in the input stream. MESSAGE
2821 (specified by the caller) is usually of the form "expected
2822 OTHER-TOKEN".
2824 This bypasses the check for tentative passing, and potentially
2825 adds material needed by cp_parser_required_error.
2827 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2828 suggesting insertion of the missing token.
2830 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2831 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2832 location. */
2834 static void
2835 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2836 required_token missing_token_desc,
2837 location_t matching_location)
2839 cp_token *token = cp_lexer_peek_token (parser->lexer);
2840 /* This diagnostic makes more sense if it is tagged to the line
2841 of the token we just peeked at. */
2842 cp_lexer_set_source_position_from_token (token);
2844 if (token->type == CPP_PRAGMA)
2846 error_at (token->location,
2847 "%<#pragma%> is not allowed here");
2848 cp_parser_skip_to_pragma_eol (parser, token);
2849 return;
2852 /* If this is actually a conflict marker, report it as such. */
2853 if (token->type == CPP_LSHIFT
2854 || token->type == CPP_RSHIFT
2855 || token->type == CPP_EQ_EQ)
2857 location_t loc;
2858 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2860 error_at (loc, "version control conflict marker in file");
2861 return;
2865 gcc_rich_location richloc (input_location);
2867 bool added_matching_location = false;
2869 if (missing_token_desc != RT_NONE)
2871 /* Potentially supply a fix-it hint, suggesting to add the
2872 missing token immediately after the *previous* token.
2873 This may move the primary location within richloc. */
2874 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2875 location_t prev_token_loc
2876 = cp_lexer_previous_token (parser->lexer)->location;
2877 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2879 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2880 Attempt to consolidate diagnostics by printing it as a
2881 secondary range within the main diagnostic. */
2882 if (matching_location != UNKNOWN_LOCATION)
2883 added_matching_location
2884 = richloc.add_location_if_nearby (matching_location);
2887 /* Actually emit the error. */
2888 c_parse_error (gmsgid,
2889 /* Because c_parser_error does not understand
2890 CPP_KEYWORD, keywords are treated like
2891 identifiers. */
2892 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2893 token->u.value, token->flags, &richloc);
2895 if (missing_token_desc != RT_NONE)
2897 /* If we weren't able to consolidate matching_location, then
2898 print it as a secondary diagnostic. */
2899 if (matching_location != UNKNOWN_LOCATION
2900 && !added_matching_location)
2901 inform (matching_location, "to match this %qs",
2902 get_matching_symbol (missing_token_desc));
2906 /* If not parsing tentatively, issue a diagnostic of the form
2907 FILE:LINE: MESSAGE before TOKEN
2908 where TOKEN is the next token in the input stream. MESSAGE
2909 (specified by the caller) is usually of the form "expected
2910 OTHER-TOKEN". */
2912 static void
2913 cp_parser_error (cp_parser* parser, const char* gmsgid)
2915 if (!cp_parser_simulate_error (parser))
2916 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2919 /* Issue an error about name-lookup failing. NAME is the
2920 IDENTIFIER_NODE DECL is the result of
2921 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2922 the thing that we hoped to find. */
2924 static void
2925 cp_parser_name_lookup_error (cp_parser* parser,
2926 tree name,
2927 tree decl,
2928 name_lookup_error desired,
2929 location_t location)
2931 /* If name lookup completely failed, tell the user that NAME was not
2932 declared. */
2933 if (decl == error_mark_node)
2935 if (parser->scope && parser->scope != global_namespace)
2936 error_at (location, "%<%E::%E%> has not been declared",
2937 parser->scope, name);
2938 else if (parser->scope == global_namespace)
2939 error_at (location, "%<::%E%> has not been declared", name);
2940 else if (parser->object_scope
2941 && !CLASS_TYPE_P (parser->object_scope))
2942 error_at (location, "request for member %qE in non-class type %qT",
2943 name, parser->object_scope);
2944 else if (parser->object_scope)
2945 error_at (location, "%<%T::%E%> has not been declared",
2946 parser->object_scope, name);
2947 else
2948 error_at (location, "%qE has not been declared", name);
2950 else if (parser->scope && parser->scope != global_namespace)
2952 switch (desired)
2954 case NLE_TYPE:
2955 error_at (location, "%<%E::%E%> is not a type",
2956 parser->scope, name);
2957 break;
2958 case NLE_CXX98:
2959 error_at (location, "%<%E::%E%> is not a class or namespace",
2960 parser->scope, name);
2961 break;
2962 case NLE_NOT_CXX98:
2963 error_at (location,
2964 "%<%E::%E%> is not a class, namespace, or enumeration",
2965 parser->scope, name);
2966 break;
2967 default:
2968 gcc_unreachable ();
2972 else if (parser->scope == global_namespace)
2974 switch (desired)
2976 case NLE_TYPE:
2977 error_at (location, "%<::%E%> is not a type", name);
2978 break;
2979 case NLE_CXX98:
2980 error_at (location, "%<::%E%> is not a class or namespace", name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<::%E%> is not a class, namespace, or enumeration",
2985 name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2991 else
2993 switch (desired)
2995 case NLE_TYPE:
2996 error_at (location, "%qE is not a type", name);
2997 break;
2998 case NLE_CXX98:
2999 error_at (location, "%qE is not a class or namespace", name);
3000 break;
3001 case NLE_NOT_CXX98:
3002 error_at (location,
3003 "%qE is not a class, namespace, or enumeration", name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3011 /* If we are parsing tentatively, remember that an error has occurred
3012 during this tentative parse. Returns true if the error was
3013 simulated; false if a message should be issued by the caller. */
3015 static bool
3016 cp_parser_simulate_error (cp_parser* parser)
3018 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3020 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3021 return true;
3023 return false;
3026 /* This function is called when a type is defined. If type
3027 definitions are forbidden at this point, an error message is
3028 issued. */
3030 static bool
3031 cp_parser_check_type_definition (cp_parser* parser)
3033 /* If types are forbidden here, issue a message. */
3034 if (parser->type_definition_forbidden_message)
3036 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3037 in the message need to be interpreted. */
3038 error (parser->type_definition_forbidden_message);
3039 return false;
3041 return true;
3044 /* This function is called when the DECLARATOR is processed. The TYPE
3045 was a type defined in the decl-specifiers. If it is invalid to
3046 define a type in the decl-specifiers for DECLARATOR, an error is
3047 issued. TYPE_LOCATION is the location of TYPE and is used
3048 for error reporting. */
3050 static void
3051 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3052 tree type, location_t type_location)
3054 /* [dcl.fct] forbids type definitions in return types.
3055 Unfortunately, it's not easy to know whether or not we are
3056 processing a return type until after the fact. */
3057 while (declarator
3058 && (declarator->kind == cdk_pointer
3059 || declarator->kind == cdk_reference
3060 || declarator->kind == cdk_ptrmem))
3061 declarator = declarator->declarator;
3062 if (declarator
3063 && declarator->kind == cdk_function)
3065 error_at (type_location,
3066 "new types may not be defined in a return type");
3067 inform (type_location,
3068 "(perhaps a semicolon is missing after the definition of %qT)",
3069 type);
3073 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3074 "<" in any valid C++ program. If the next token is indeed "<",
3075 issue a message warning the user about what appears to be an
3076 invalid attempt to form a template-id. LOCATION is the location
3077 of the type-specifier (TYPE) */
3079 static void
3080 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3081 tree type,
3082 enum tag_types tag_type,
3083 location_t location)
3085 cp_token_position start = 0;
3087 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3089 if (TREE_CODE (type) == TYPE_DECL)
3090 type = TREE_TYPE (type);
3091 if (TYPE_P (type) && !template_placeholder_p (type))
3092 error_at (location, "%qT is not a template", type);
3093 else if (identifier_p (type))
3095 if (tag_type != none_type)
3096 error_at (location, "%qE is not a class template", type);
3097 else
3098 error_at (location, "%qE is not a template", type);
3100 else
3101 error_at (location, "invalid template-id");
3102 /* Remember the location of the invalid "<". */
3103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3104 start = cp_lexer_token_position (parser->lexer, true);
3105 /* Consume the "<". */
3106 cp_lexer_consume_token (parser->lexer);
3107 /* Parse the template arguments. */
3108 cp_parser_enclosed_template_argument_list (parser);
3109 /* Permanently remove the invalid template arguments so that
3110 this error message is not issued again. */
3111 if (start)
3112 cp_lexer_purge_tokens_after (parser->lexer, start);
3116 /* If parsing an integral constant-expression, issue an error message
3117 about the fact that THING appeared and return true. Otherwise,
3118 return false. In either case, set
3119 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3121 static bool
3122 cp_parser_non_integral_constant_expression (cp_parser *parser,
3123 non_integral_constant thing)
3125 parser->non_integral_constant_expression_p = true;
3126 if (parser->integral_constant_expression_p)
3128 if (!parser->allow_non_integral_constant_expression_p)
3130 const char *msg = NULL;
3131 switch (thing)
3133 case NIC_FLOAT:
3134 pedwarn (input_location, OPT_Wpedantic,
3135 "ISO C++ forbids using a floating-point literal "
3136 "in a constant-expression");
3137 return true;
3138 case NIC_CAST:
3139 error ("a cast to a type other than an integral or "
3140 "enumeration type cannot appear in a "
3141 "constant-expression");
3142 return true;
3143 case NIC_TYPEID:
3144 error ("%<typeid%> operator "
3145 "cannot appear in a constant-expression");
3146 return true;
3147 case NIC_NCC:
3148 error ("non-constant compound literals "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_FUNC_CALL:
3152 error ("a function call "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_INC:
3156 error ("an increment "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_DEC:
3160 error ("an decrement "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_ARRAY_REF:
3164 error ("an array reference "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ADDR_LABEL:
3168 error ("the address of a label "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_OVERLOADED:
3172 error ("calls to overloaded operators "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_ASSIGNMENT:
3176 error ("an assignment cannot appear in a constant-expression");
3177 return true;
3178 case NIC_COMMA:
3179 error ("a comma operator "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_CONSTRUCTOR:
3183 error ("a call to a constructor "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_TRANSACTION:
3187 error ("a transaction expression "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_THIS:
3191 msg = "this";
3192 break;
3193 case NIC_FUNC_NAME:
3194 msg = "__FUNCTION__";
3195 break;
3196 case NIC_PRETTY_FUNC:
3197 msg = "__PRETTY_FUNCTION__";
3198 break;
3199 case NIC_C99_FUNC:
3200 msg = "__func__";
3201 break;
3202 case NIC_VA_ARG:
3203 msg = "va_arg";
3204 break;
3205 case NIC_ARROW:
3206 msg = "->";
3207 break;
3208 case NIC_POINT:
3209 msg = ".";
3210 break;
3211 case NIC_STAR:
3212 msg = "*";
3213 break;
3214 case NIC_ADDR:
3215 msg = "&";
3216 break;
3217 case NIC_PREINCREMENT:
3218 msg = "++";
3219 break;
3220 case NIC_PREDECREMENT:
3221 msg = "--";
3222 break;
3223 case NIC_NEW:
3224 msg = "new";
3225 break;
3226 case NIC_DEL:
3227 msg = "delete";
3228 break;
3229 default:
3230 gcc_unreachable ();
3232 if (msg)
3233 error ("%qs cannot appear in a constant-expression", msg);
3234 return true;
3237 return false;
3240 /* Emit a diagnostic for an invalid type name. This function commits
3241 to the current active tentative parse, if any. (Otherwise, the
3242 problematic construct might be encountered again later, resulting
3243 in duplicate error messages.) LOCATION is the location of ID. */
3245 static void
3246 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3247 location_t location)
3249 tree decl, ambiguous_decls;
3250 cp_parser_commit_to_tentative_parse (parser);
3251 /* Try to lookup the identifier. */
3252 decl = cp_parser_lookup_name (parser, id, none_type,
3253 /*is_template=*/false,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3256 &ambiguous_decls, location);
3257 if (ambiguous_decls)
3258 /* If the lookup was ambiguous, an error will already have
3259 been issued. */
3260 return;
3261 /* If the lookup found a template-name, it means that the user forgot
3262 to specify an argument list. Emit a useful error message. */
3263 if (DECL_TYPE_TEMPLATE_P (decl))
3265 error_at (location,
3266 "invalid use of template-name %qE without an argument list",
3267 decl);
3268 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3269 inform (location, "class template argument deduction is only available "
3270 "with -std=c++17 or -std=gnu++17");
3271 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3273 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3274 error_at (location, "invalid use of destructor %qD as a type", id);
3275 else if (TREE_CODE (decl) == TYPE_DECL)
3276 /* Something like 'unsigned A a;' */
3277 error_at (location, "invalid combination of multiple type-specifiers");
3278 else if (!parser->scope)
3280 /* Issue an error message. */
3281 name_hint hint;
3282 if (TREE_CODE (id) == IDENTIFIER_NODE)
3283 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3284 if (hint)
3286 gcc_rich_location richloc (location);
3287 richloc.add_fixit_replace (hint.suggestion ());
3288 error_at (&richloc,
3289 "%qE does not name a type; did you mean %qs?",
3290 id, hint.suggestion ());
3292 else
3293 error_at (location, "%qE does not name a type", id);
3294 /* If we're in a template class, it's possible that the user was
3295 referring to a type from a base class. For example:
3297 template <typename T> struct A { typedef T X; };
3298 template <typename T> struct B : public A<T> { X x; };
3300 The user should have said "typename A<T>::X". */
3301 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3302 inform (location, "C++11 %<constexpr%> only available with "
3303 "-std=c++11 or -std=gnu++11");
3304 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3305 inform (location, "C++11 %<noexcept%> only available with "
3306 "-std=c++11 or -std=gnu++11");
3307 else if (cxx_dialect < cxx11
3308 && TREE_CODE (id) == IDENTIFIER_NODE
3309 && id_equal (id, "thread_local"))
3310 inform (location, "C++11 %<thread_local%> only available with "
3311 "-std=c++11 or -std=gnu++11");
3312 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3313 inform (location, "%<concept%> only available with -fconcepts");
3314 else if (processing_template_decl && current_class_type
3315 && TYPE_BINFO (current_class_type))
3317 tree b;
3319 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3321 b = TREE_CHAIN (b))
3323 tree base_type = BINFO_TYPE (b);
3324 if (CLASS_TYPE_P (base_type)
3325 && dependent_type_p (base_type))
3327 tree field;
3328 /* Go from a particular instantiation of the
3329 template (which will have an empty TYPE_FIELDs),
3330 to the main version. */
3331 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3332 for (field = TYPE_FIELDS (base_type);
3333 field;
3334 field = DECL_CHAIN (field))
3335 if (TREE_CODE (field) == TYPE_DECL
3336 && DECL_NAME (field) == id)
3338 inform (location,
3339 "(perhaps %<typename %T::%E%> was intended)",
3340 BINFO_TYPE (b), id);
3341 break;
3343 if (field)
3344 break;
3349 /* Here we diagnose qualified-ids where the scope is actually correct,
3350 but the identifier does not resolve to a valid type name. */
3351 else if (parser->scope != error_mark_node)
3353 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3355 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3356 error_at (location_of (id),
3357 "%qE in namespace %qE does not name a template type",
3358 id, parser->scope);
3359 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 TREE_OPERAND (id, 0), parser->scope);
3363 else
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a type",
3366 id, parser->scope);
3367 if (DECL_P (decl))
3368 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3369 else if (decl == error_mark_node)
3370 suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3373 else if (CLASS_TYPE_P (parser->scope)
3374 && constructor_name_p (id, parser->scope))
3376 /* A<T>::A<T>() */
3377 error_at (location, "%<%T::%E%> names the constructor, not"
3378 " the type", parser->scope, id);
3379 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3380 error_at (location, "and %qT has no template constructors",
3381 parser->scope);
3383 else if (TYPE_P (parser->scope)
3384 && dependent_scope_p (parser->scope))
3386 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3387 error_at (location,
3388 "need %<typename%> before %<%T::%D::%E%> because "
3389 "%<%T::%D%> is a dependent scope",
3390 TYPE_CONTEXT (parser->scope),
3391 TYPENAME_TYPE_FULLNAME (parser->scope),
3393 TYPE_CONTEXT (parser->scope),
3394 TYPENAME_TYPE_FULLNAME (parser->scope));
3395 else
3396 error_at (location, "need %<typename%> before %<%T::%E%> because "
3397 "%qT is a dependent scope",
3398 parser->scope, id, parser->scope);
3400 else if (TYPE_P (parser->scope))
3402 if (!COMPLETE_TYPE_P (parser->scope))
3403 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3404 parser->scope);
3405 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3406 error_at (location_of (id),
3407 "%qE in %q#T does not name a template type",
3408 id, parser->scope);
3409 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 TREE_OPERAND (id, 0), parser->scope);
3413 else
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a type",
3416 id, parser->scope);
3417 if (DECL_P (decl))
3418 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3420 else
3421 gcc_unreachable ();
3425 /* Check for a common situation where a type-name should be present,
3426 but is not, and issue a sensible error message. Returns true if an
3427 invalid type-name was detected.
3429 The situation handled by this function are variable declarations of the
3430 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3431 Usually, `ID' should name a type, but if we got here it means that it
3432 does not. We try to emit the best possible error message depending on
3433 how exactly the id-expression looks like. */
3435 static bool
3436 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3438 tree id;
3439 cp_token *token = cp_lexer_peek_token (parser->lexer);
3441 /* Avoid duplicate error about ambiguous lookup. */
3442 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3444 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3445 if (next->type == CPP_NAME && next->error_reported)
3446 goto out;
3449 cp_parser_parse_tentatively (parser);
3450 id = cp_parser_id_expression (parser,
3451 /*template_keyword_p=*/false,
3452 /*check_dependency_p=*/true,
3453 /*template_p=*/NULL,
3454 /*declarator_p=*/true,
3455 /*optional_p=*/false);
3456 /* If the next token is a (, this is a function with no explicit return
3457 type, i.e. constructor, destructor or conversion op. */
3458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3459 || TREE_CODE (id) == TYPE_DECL)
3461 cp_parser_abort_tentative_parse (parser);
3462 return false;
3464 if (!cp_parser_parse_definitely (parser))
3465 return false;
3467 /* Emit a diagnostic for the invalid type. */
3468 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3469 out:
3470 /* If we aren't in the middle of a declarator (i.e. in a
3471 parameter-declaration-clause), skip to the end of the declaration;
3472 there's no point in trying to process it. */
3473 if (!parser->in_declarator_p)
3474 cp_parser_skip_to_end_of_block_or_statement (parser);
3475 return true;
3478 /* Consume tokens up to, and including, the next non-nested closing `)'.
3479 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3480 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3481 found an unnested token of that type. */
3483 static int
3484 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3485 bool recovering,
3486 cpp_ttype or_ttype,
3487 bool consume_paren)
3489 unsigned paren_depth = 0;
3490 unsigned brace_depth = 0;
3491 unsigned square_depth = 0;
3493 if (recovering && or_ttype == CPP_EOF
3494 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3495 return 0;
3497 while (true)
3499 cp_token * token = cp_lexer_peek_token (parser->lexer);
3501 /* Have we found what we're looking for before the closing paren? */
3502 if (token->type == or_ttype && or_ttype != CPP_EOF
3503 && !brace_depth && !paren_depth && !square_depth)
3504 return -1;
3506 switch (token->type)
3508 case CPP_EOF:
3509 case CPP_PRAGMA_EOL:
3510 /* If we've run out of tokens, then there is no closing `)'. */
3511 return 0;
3513 /* This is good for lambda expression capture-lists. */
3514 case CPP_OPEN_SQUARE:
3515 ++square_depth;
3516 break;
3517 case CPP_CLOSE_SQUARE:
3518 if (!square_depth--)
3519 return 0;
3520 break;
3522 case CPP_SEMICOLON:
3523 /* This matches the processing in skip_to_end_of_statement. */
3524 if (!brace_depth)
3525 return 0;
3526 break;
3528 case CPP_OPEN_BRACE:
3529 ++brace_depth;
3530 break;
3531 case CPP_CLOSE_BRACE:
3532 if (!brace_depth--)
3533 return 0;
3534 break;
3536 case CPP_OPEN_PAREN:
3537 if (!brace_depth)
3538 ++paren_depth;
3539 break;
3541 case CPP_CLOSE_PAREN:
3542 if (!brace_depth && !paren_depth--)
3544 if (consume_paren)
3545 cp_lexer_consume_token (parser->lexer);
3546 return 1;
3548 break;
3550 default:
3551 break;
3554 /* Consume the token. */
3555 cp_lexer_consume_token (parser->lexer);
3559 /* Consume tokens up to, and including, the next non-nested closing `)'.
3560 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3561 are doing error recovery. Returns -1 if OR_COMMA is true and we
3562 found an unnested token of that type. */
3564 static int
3565 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3566 bool recovering,
3567 bool or_comma,
3568 bool consume_paren)
3570 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3571 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3572 ttype, consume_paren);
3575 /* Consume tokens until we reach the end of the current statement.
3576 Normally, that will be just before consuming a `;'. However, if a
3577 non-nested `}' comes first, then we stop before consuming that. */
3579 static void
3580 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3582 unsigned nesting_depth = 0;
3584 /* Unwind generic function template scope if necessary. */
3585 if (parser->fully_implicit_function_template_p)
3586 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3588 while (true)
3590 cp_token *token = cp_lexer_peek_token (parser->lexer);
3592 switch (token->type)
3594 case CPP_EOF:
3595 case CPP_PRAGMA_EOL:
3596 /* If we've run out of tokens, stop. */
3597 return;
3599 case CPP_SEMICOLON:
3600 /* If the next token is a `;', we have reached the end of the
3601 statement. */
3602 if (!nesting_depth)
3603 return;
3604 break;
3606 case CPP_CLOSE_BRACE:
3607 /* If this is a non-nested '}', stop before consuming it.
3608 That way, when confronted with something like:
3610 { 3 + }
3612 we stop before consuming the closing '}', even though we
3613 have not yet reached a `;'. */
3614 if (nesting_depth == 0)
3615 return;
3617 /* If it is the closing '}' for a block that we have
3618 scanned, stop -- but only after consuming the token.
3619 That way given:
3621 void f g () { ... }
3622 typedef int I;
3624 we will stop after the body of the erroneously declared
3625 function, but before consuming the following `typedef'
3626 declaration. */
3627 if (--nesting_depth == 0)
3629 cp_lexer_consume_token (parser->lexer);
3630 return;
3632 break;
3634 case CPP_OPEN_BRACE:
3635 ++nesting_depth;
3636 break;
3638 default:
3639 break;
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser->lexer);
3647 /* This function is called at the end of a statement or declaration.
3648 If the next token is a semicolon, it is consumed; otherwise, error
3649 recovery is attempted. */
3651 static void
3652 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3654 /* Look for the trailing `;'. */
3655 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3657 /* If there is additional (erroneous) input, skip to the end of
3658 the statement. */
3659 cp_parser_skip_to_end_of_statement (parser);
3660 /* If the next token is now a `;', consume it. */
3661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3662 cp_lexer_consume_token (parser->lexer);
3666 /* Skip tokens until we have consumed an entire block, or until we
3667 have consumed a non-nested `;'. */
3669 static void
3670 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3672 int nesting_depth = 0;
3674 /* Unwind generic function template scope if necessary. */
3675 if (parser->fully_implicit_function_template_p)
3676 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3678 while (nesting_depth >= 0)
3680 cp_token *token = cp_lexer_peek_token (parser->lexer);
3682 switch (token->type)
3684 case CPP_EOF:
3685 case CPP_PRAGMA_EOL:
3686 /* If we've run out of tokens, stop. */
3687 return;
3689 case CPP_SEMICOLON:
3690 /* Stop if this is an unnested ';'. */
3691 if (!nesting_depth)
3692 nesting_depth = -1;
3693 break;
3695 case CPP_CLOSE_BRACE:
3696 /* Stop if this is an unnested '}', or closes the outermost
3697 nesting level. */
3698 nesting_depth--;
3699 if (nesting_depth < 0)
3700 return;
3701 if (!nesting_depth)
3702 nesting_depth = -1;
3703 break;
3705 case CPP_OPEN_BRACE:
3706 /* Nest. */
3707 nesting_depth++;
3708 break;
3710 default:
3711 break;
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser->lexer);
3719 /* Skip tokens until a non-nested closing curly brace is the next
3720 token, or there are no more tokens. Return true in the first case,
3721 false otherwise. */
3723 static bool
3724 cp_parser_skip_to_closing_brace (cp_parser *parser)
3726 unsigned nesting_depth = 0;
3728 while (true)
3730 cp_token *token = cp_lexer_peek_token (parser->lexer);
3732 switch (token->type)
3734 case CPP_EOF:
3735 case CPP_PRAGMA_EOL:
3736 /* If we've run out of tokens, stop. */
3737 return false;
3739 case CPP_CLOSE_BRACE:
3740 /* If the next token is a non-nested `}', then we have reached
3741 the end of the current block. */
3742 if (nesting_depth-- == 0)
3743 return true;
3744 break;
3746 case CPP_OPEN_BRACE:
3747 /* If it the next token is a `{', then we are entering a new
3748 block. Consume the entire block. */
3749 ++nesting_depth;
3750 break;
3752 default:
3753 break;
3756 /* Consume the token. */
3757 cp_lexer_consume_token (parser->lexer);
3761 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3762 parameter is the PRAGMA token, allowing us to purge the entire pragma
3763 sequence. */
3765 static void
3766 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3768 cp_token *token;
3770 parser->lexer->in_pragma = false;
3773 token = cp_lexer_consume_token (parser->lexer);
3774 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3776 /* Ensure that the pragma is not parsed again. */
3777 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3780 /* Require pragma end of line, resyncing with it as necessary. The
3781 arguments are as for cp_parser_skip_to_pragma_eol. */
3783 static void
3784 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3786 parser->lexer->in_pragma = false;
3787 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3791 /* This is a simple wrapper around make_typename_type. When the id is
3792 an unresolved identifier node, we can provide a superior diagnostic
3793 using cp_parser_diagnose_invalid_type_name. */
3795 static tree
3796 cp_parser_make_typename_type (cp_parser *parser, tree id,
3797 location_t id_location)
3799 tree result;
3800 if (identifier_p (id))
3802 result = make_typename_type (parser->scope, id, typename_type,
3803 /*complain=*/tf_none);
3804 if (result == error_mark_node)
3805 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3806 return result;
3808 return make_typename_type (parser->scope, id, typename_type, tf_error);
3811 /* This is a wrapper around the
3812 make_{pointer,ptrmem,reference}_declarator functions that decides
3813 which one to call based on the CODE and CLASS_TYPE arguments. The
3814 CODE argument should be one of the values returned by
3815 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3816 appertain to the pointer or reference. */
3818 static cp_declarator *
3819 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3820 cp_cv_quals cv_qualifiers,
3821 cp_declarator *target,
3822 tree attributes)
3824 if (code == ERROR_MARK)
3825 return cp_error_declarator;
3827 if (code == INDIRECT_REF)
3828 if (class_type == NULL_TREE)
3829 return make_pointer_declarator (cv_qualifiers, target, attributes);
3830 else
3831 return make_ptrmem_declarator (cv_qualifiers, class_type,
3832 target, attributes);
3833 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3834 return make_reference_declarator (cv_qualifiers, target,
3835 false, attributes);
3836 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3837 return make_reference_declarator (cv_qualifiers, target,
3838 true, attributes);
3839 gcc_unreachable ();
3842 /* Create a new C++ parser. */
3844 static cp_parser *
3845 cp_parser_new (void)
3847 cp_parser *parser;
3848 cp_lexer *lexer;
3849 unsigned i;
3851 /* cp_lexer_new_main is called before doing GC allocation because
3852 cp_lexer_new_main might load a PCH file. */
3853 lexer = cp_lexer_new_main ();
3855 /* Initialize the binops_by_token so that we can get the tree
3856 directly from the token. */
3857 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3858 binops_by_token[binops[i].token_type] = binops[i];
3860 parser = ggc_cleared_alloc<cp_parser> ();
3861 parser->lexer = lexer;
3862 parser->context = cp_parser_context_new (NULL);
3864 /* For now, we always accept GNU extensions. */
3865 parser->allow_gnu_extensions_p = 1;
3867 /* The `>' token is a greater-than operator, not the end of a
3868 template-id. */
3869 parser->greater_than_is_operator_p = true;
3871 parser->default_arg_ok_p = true;
3873 /* We are not parsing a constant-expression. */
3874 parser->integral_constant_expression_p = false;
3875 parser->allow_non_integral_constant_expression_p = false;
3876 parser->non_integral_constant_expression_p = false;
3878 /* Local variable names are not forbidden. */
3879 parser->local_variables_forbidden_p = false;
3881 /* We are not processing an `extern "C"' declaration. */
3882 parser->in_unbraced_linkage_specification_p = false;
3884 /* We are not processing a declarator. */
3885 parser->in_declarator_p = false;
3887 /* We are not processing a template-argument-list. */
3888 parser->in_template_argument_list_p = false;
3890 /* We are not in an iteration statement. */
3891 parser->in_statement = 0;
3893 /* We are not in a switch statement. */
3894 parser->in_switch_statement_p = false;
3896 /* We are not parsing a type-id inside an expression. */
3897 parser->in_type_id_in_expr_p = false;
3899 /* Declarations aren't implicitly extern "C". */
3900 parser->implicit_extern_c = false;
3902 /* String literals should be translated to the execution character set. */
3903 parser->translate_strings_p = true;
3905 /* We are not parsing a function body. */
3906 parser->in_function_body = false;
3908 /* We can correct until told otherwise. */
3909 parser->colon_corrects_to_scope_p = true;
3911 /* The unparsed function queue is empty. */
3912 push_unparsed_function_queues (parser);
3914 /* There are no classes being defined. */
3915 parser->num_classes_being_defined = 0;
3917 /* No template parameters apply. */
3918 parser->num_template_parameter_lists = 0;
3920 /* Special parsing data structures. */
3921 parser->omp_declare_simd = NULL;
3922 parser->oacc_routine = NULL;
3924 /* Not declaring an implicit function template. */
3925 parser->auto_is_implicit_function_template_parm_p = false;
3926 parser->fully_implicit_function_template_p = false;
3927 parser->implicit_template_parms = 0;
3928 parser->implicit_template_scope = 0;
3930 /* Allow constrained-type-specifiers. */
3931 parser->prevent_constrained_type_specifiers = 0;
3933 /* We haven't yet seen an 'extern "C"'. */
3934 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3936 return parser;
3939 /* Create a cp_lexer structure which will emit the tokens in CACHE
3940 and push it onto the parser's lexer stack. This is used for delayed
3941 parsing of in-class method bodies and default arguments, and should
3942 not be confused with tentative parsing. */
3943 static void
3944 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3946 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3947 lexer->next = parser->lexer;
3948 parser->lexer = lexer;
3950 /* Move the current source position to that of the first token in the
3951 new lexer. */
3952 cp_lexer_set_source_position_from_token (lexer->next_token);
3955 /* Pop the top lexer off the parser stack. This is never used for the
3956 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3957 static void
3958 cp_parser_pop_lexer (cp_parser *parser)
3960 cp_lexer *lexer = parser->lexer;
3961 parser->lexer = lexer->next;
3962 cp_lexer_destroy (lexer);
3964 /* Put the current source position back where it was before this
3965 lexer was pushed. */
3966 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3969 /* Lexical conventions [gram.lex] */
3971 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3972 identifier. */
3974 static cp_expr
3975 cp_parser_identifier (cp_parser* parser)
3977 cp_token *token;
3979 /* Look for the identifier. */
3980 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3981 /* Return the value. */
3982 if (token)
3983 return cp_expr (token->u.value, token->location);
3984 else
3985 return error_mark_node;
3988 /* Parse a sequence of adjacent string constants. Returns a
3989 TREE_STRING representing the combined, nul-terminated string
3990 constant. If TRANSLATE is true, translate the string to the
3991 execution character set. If WIDE_OK is true, a wide string is
3992 invalid here.
3994 C++98 [lex.string] says that if a narrow string literal token is
3995 adjacent to a wide string literal token, the behavior is undefined.
3996 However, C99 6.4.5p4 says that this results in a wide string literal.
3997 We follow C99 here, for consistency with the C front end.
3999 This code is largely lifted from lex_string() in c-lex.c.
4001 FUTURE: ObjC++ will need to handle @-strings here. */
4002 static cp_expr
4003 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4004 bool lookup_udlit = true)
4006 tree value;
4007 size_t count;
4008 struct obstack str_ob;
4009 cpp_string str, istr, *strs;
4010 cp_token *tok;
4011 enum cpp_ttype type, curr_type;
4012 int have_suffix_p = 0;
4013 tree string_tree;
4014 tree suffix_id = NULL_TREE;
4015 bool curr_tok_is_userdef_p = false;
4017 tok = cp_lexer_peek_token (parser->lexer);
4018 if (!cp_parser_is_string_literal (tok))
4020 cp_parser_error (parser, "expected string-literal");
4021 return error_mark_node;
4024 location_t loc = tok->location;
4026 if (cpp_userdef_string_p (tok->type))
4028 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4029 curr_type = cpp_userdef_string_remove_type (tok->type);
4030 curr_tok_is_userdef_p = true;
4032 else
4034 string_tree = tok->u.value;
4035 curr_type = tok->type;
4037 type = curr_type;
4039 /* Try to avoid the overhead of creating and destroying an obstack
4040 for the common case of just one string. */
4041 if (!cp_parser_is_string_literal
4042 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4044 cp_lexer_consume_token (parser->lexer);
4046 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4047 str.len = TREE_STRING_LENGTH (string_tree);
4048 count = 1;
4050 if (curr_tok_is_userdef_p)
4052 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4053 have_suffix_p = 1;
4054 curr_type = cpp_userdef_string_remove_type (tok->type);
4056 else
4057 curr_type = tok->type;
4059 strs = &str;
4061 else
4063 location_t last_tok_loc = tok->location;
4064 gcc_obstack_init (&str_ob);
4065 count = 0;
4069 cp_lexer_consume_token (parser->lexer);
4070 count++;
4071 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4072 str.len = TREE_STRING_LENGTH (string_tree);
4074 if (curr_tok_is_userdef_p)
4076 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4077 if (have_suffix_p == 0)
4079 suffix_id = curr_suffix_id;
4080 have_suffix_p = 1;
4082 else if (have_suffix_p == 1
4083 && curr_suffix_id != suffix_id)
4085 error ("inconsistent user-defined literal suffixes"
4086 " %qD and %qD in string literal",
4087 suffix_id, curr_suffix_id);
4088 have_suffix_p = -1;
4090 curr_type = cpp_userdef_string_remove_type (tok->type);
4092 else
4093 curr_type = tok->type;
4095 if (type != curr_type)
4097 if (type == CPP_STRING)
4098 type = curr_type;
4099 else if (curr_type != CPP_STRING)
4101 rich_location rich_loc (line_table, tok->location);
4102 rich_loc.add_range (last_tok_loc, false);
4103 error_at (&rich_loc,
4104 "unsupported non-standard concatenation "
4105 "of string literals");
4109 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4111 last_tok_loc = tok->location;
4113 tok = cp_lexer_peek_token (parser->lexer);
4114 if (cpp_userdef_string_p (tok->type))
4116 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4117 curr_type = cpp_userdef_string_remove_type (tok->type);
4118 curr_tok_is_userdef_p = true;
4120 else
4122 string_tree = tok->u.value;
4123 curr_type = tok->type;
4124 curr_tok_is_userdef_p = false;
4127 while (cp_parser_is_string_literal (tok));
4129 /* A string literal built by concatenation has its caret=start at
4130 the start of the initial string, and its finish at the finish of
4131 the final string literal. */
4132 loc = make_location (loc, loc, get_finish (last_tok_loc));
4134 strs = (cpp_string *) obstack_finish (&str_ob);
4137 if (type != CPP_STRING && !wide_ok)
4139 cp_parser_error (parser, "a wide string is invalid in this context");
4140 type = CPP_STRING;
4143 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4144 (parse_in, strs, count, &istr, type))
4146 value = build_string (istr.len, (const char *)istr.text);
4147 free (CONST_CAST (unsigned char *, istr.text));
4149 switch (type)
4151 default:
4152 case CPP_STRING:
4153 case CPP_UTF8STRING:
4154 TREE_TYPE (value) = char_array_type_node;
4155 break;
4156 case CPP_STRING16:
4157 TREE_TYPE (value) = char16_array_type_node;
4158 break;
4159 case CPP_STRING32:
4160 TREE_TYPE (value) = char32_array_type_node;
4161 break;
4162 case CPP_WSTRING:
4163 TREE_TYPE (value) = wchar_array_type_node;
4164 break;
4167 value = fix_string_type (value);
4169 if (have_suffix_p)
4171 tree literal = build_userdef_literal (suffix_id, value,
4172 OT_NONE, NULL_TREE);
4173 if (lookup_udlit)
4174 value = cp_parser_userdef_string_literal (literal);
4175 else
4176 value = literal;
4179 else
4180 /* cpp_interpret_string has issued an error. */
4181 value = error_mark_node;
4183 if (count > 1)
4184 obstack_free (&str_ob, 0);
4186 return cp_expr (value, loc);
4189 /* Look up a literal operator with the name and the exact arguments. */
4191 static tree
4192 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4194 tree decl;
4195 decl = lookup_name (name);
4196 if (!decl || !is_overloaded_fn (decl))
4197 return error_mark_node;
4199 for (lkp_iterator iter (decl); iter; ++iter)
4201 unsigned int ix;
4202 bool found = true;
4203 tree fn = *iter;
4204 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4205 if (parmtypes != NULL_TREE)
4207 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4208 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4210 tree tparm = TREE_VALUE (parmtypes);
4211 tree targ = TREE_TYPE ((*args)[ix]);
4212 bool ptr = TYPE_PTR_P (tparm);
4213 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4214 if ((ptr || arr || !same_type_p (tparm, targ))
4215 && (!ptr || !arr
4216 || !same_type_p (TREE_TYPE (tparm),
4217 TREE_TYPE (targ))))
4218 found = false;
4220 if (found
4221 && ix == vec_safe_length (args)
4222 /* May be this should be sufficient_parms_p instead,
4223 depending on how exactly should user-defined literals
4224 work in presence of default arguments on the literal
4225 operator parameters. */
4226 && parmtypes == void_list_node)
4227 return decl;
4231 return error_mark_node;
4234 /* Parse a user-defined char constant. Returns a call to a user-defined
4235 literal operator taking the character as an argument. */
4237 static cp_expr
4238 cp_parser_userdef_char_literal (cp_parser *parser)
4240 cp_token *token = cp_lexer_consume_token (parser->lexer);
4241 tree literal = token->u.value;
4242 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4243 tree value = USERDEF_LITERAL_VALUE (literal);
4244 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4245 tree decl, result;
4247 /* Build up a call to the user-defined operator */
4248 /* Lookup the name we got back from the id-expression. */
4249 vec<tree, va_gc> *args = make_tree_vector ();
4250 vec_safe_push (args, value);
4251 decl = lookup_literal_operator (name, args);
4252 if (!decl || decl == error_mark_node)
4254 error ("unable to find character literal operator %qD with %qT argument",
4255 name, TREE_TYPE (value));
4256 release_tree_vector (args);
4257 return error_mark_node;
4259 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4260 release_tree_vector (args);
4261 return result;
4264 /* A subroutine of cp_parser_userdef_numeric_literal to
4265 create a char... template parameter pack from a string node. */
4267 static tree
4268 make_char_string_pack (tree value)
4270 tree charvec;
4271 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4272 const char *str = TREE_STRING_POINTER (value);
4273 int i, len = TREE_STRING_LENGTH (value) - 1;
4274 tree argvec = make_tree_vec (1);
4276 /* Fill in CHARVEC with all of the parameters. */
4277 charvec = make_tree_vec (len);
4278 for (i = 0; i < len; ++i)
4279 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4281 /* Build the argument packs. */
4282 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4284 TREE_VEC_ELT (argvec, 0) = argpack;
4286 return argvec;
4289 /* A subroutine of cp_parser_userdef_numeric_literal to
4290 create a char... template parameter pack from a string node. */
4292 static tree
4293 make_string_pack (tree value)
4295 tree charvec;
4296 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4297 const unsigned char *str
4298 = (const unsigned char *) TREE_STRING_POINTER (value);
4299 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4300 int len = TREE_STRING_LENGTH (value) / sz - 1;
4301 tree argvec = make_tree_vec (2);
4303 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4304 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4306 /* First template parm is character type. */
4307 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4309 /* Fill in CHARVEC with all of the parameters. */
4310 charvec = make_tree_vec (len);
4311 for (int i = 0; i < len; ++i)
4312 TREE_VEC_ELT (charvec, i)
4313 = double_int_to_tree (str_char_type_node,
4314 double_int::from_buffer (str + i * sz, sz));
4316 /* Build the argument packs. */
4317 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4319 TREE_VEC_ELT (argvec, 1) = argpack;
4321 return argvec;
4324 /* Parse a user-defined numeric constant. returns a call to a user-defined
4325 literal operator. */
4327 static cp_expr
4328 cp_parser_userdef_numeric_literal (cp_parser *parser)
4330 cp_token *token = cp_lexer_consume_token (parser->lexer);
4331 tree literal = token->u.value;
4332 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4333 tree value = USERDEF_LITERAL_VALUE (literal);
4334 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4335 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4336 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4337 tree decl, result;
4338 vec<tree, va_gc> *args;
4340 /* Look for a literal operator taking the exact type of numeric argument
4341 as the literal value. */
4342 args = make_tree_vector ();
4343 vec_safe_push (args, value);
4344 decl = lookup_literal_operator (name, args);
4345 if (decl && decl != error_mark_node)
4347 result = finish_call_expr (decl, &args, false, true,
4348 tf_warning_or_error);
4350 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4352 warning_at (token->location, OPT_Woverflow,
4353 "integer literal exceeds range of %qT type",
4354 long_long_unsigned_type_node);
4356 else
4358 if (overflow > 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "floating literal exceeds range of %qT type",
4361 long_double_type_node);
4362 else if (overflow < 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal truncated to zero");
4367 release_tree_vector (args);
4368 return result;
4370 release_tree_vector (args);
4372 /* If the numeric argument didn't work, look for a raw literal
4373 operator taking a const char* argument consisting of the number
4374 in string format. */
4375 args = make_tree_vector ();
4376 vec_safe_push (args, num_string);
4377 decl = lookup_literal_operator (name, args);
4378 if (decl && decl != error_mark_node)
4380 result = finish_call_expr (decl, &args, false, true,
4381 tf_warning_or_error);
4382 release_tree_vector (args);
4383 return result;
4385 release_tree_vector (args);
4387 /* If the raw literal didn't work, look for a non-type template
4388 function with parameter pack char.... Call the function with
4389 template parameter characters representing the number. */
4390 args = make_tree_vector ();
4391 decl = lookup_literal_operator (name, args);
4392 if (decl && decl != error_mark_node)
4394 tree tmpl_args = make_char_string_pack (num_string);
4395 decl = lookup_template_function (decl, tmpl_args);
4396 result = finish_call_expr (decl, &args, false, true,
4397 tf_warning_or_error);
4398 release_tree_vector (args);
4399 return result;
4402 release_tree_vector (args);
4404 /* In C++14 the standard library defines complex number suffixes that
4405 conflict with GNU extensions. Prefer them if <complex> is #included. */
4406 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4407 bool i14 = (cxx_dialect > cxx11
4408 && (id_equal (suffix_id, "i")
4409 || id_equal (suffix_id, "if")
4410 || id_equal (suffix_id, "il")));
4411 diagnostic_t kind = DK_ERROR;
4412 int opt = 0;
4414 if (i14 && ext)
4416 tree cxlit = lookup_qualified_name (std_node,
4417 get_identifier ("complex_literals"),
4418 0, false, false);
4419 if (cxlit == error_mark_node)
4421 /* No <complex>, so pedwarn and use GNU semantics. */
4422 kind = DK_PEDWARN;
4423 opt = OPT_Wpedantic;
4427 bool complained
4428 = emit_diagnostic (kind, input_location, opt,
4429 "unable to find numeric literal operator %qD", name);
4431 if (!complained)
4432 /* Don't inform either. */;
4433 else if (i14)
4435 inform (token->location, "add %<using namespace std::complex_literals%> "
4436 "(from <complex>) to enable the C++14 user-defined literal "
4437 "suffixes");
4438 if (ext)
4439 inform (token->location, "or use %<j%> instead of %<i%> for the "
4440 "GNU built-in suffix");
4442 else if (!ext)
4443 inform (token->location, "use -fext-numeric-literals "
4444 "to enable more built-in suffixes");
4446 if (kind == DK_ERROR)
4447 value = error_mark_node;
4448 else
4450 /* Use the built-in semantics. */
4451 tree type;
4452 if (id_equal (suffix_id, "i"))
4454 if (TREE_CODE (value) == INTEGER_CST)
4455 type = integer_type_node;
4456 else
4457 type = double_type_node;
4459 else if (id_equal (suffix_id, "if"))
4460 type = float_type_node;
4461 else /* if (id_equal (suffix_id, "il")) */
4462 type = long_double_type_node;
4464 value = build_complex (build_complex_type (type),
4465 fold_convert (type, integer_zero_node),
4466 fold_convert (type, value));
4469 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4470 /* Avoid repeated diagnostics. */
4471 token->u.value = value;
4472 return value;
4475 /* Parse a user-defined string constant. Returns a call to a user-defined
4476 literal operator taking a character pointer and the length of the string
4477 as arguments. */
4479 static tree
4480 cp_parser_userdef_string_literal (tree literal)
4482 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4483 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4484 tree value = USERDEF_LITERAL_VALUE (literal);
4485 int len = TREE_STRING_LENGTH (value)
4486 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4487 tree decl, result;
4488 vec<tree, va_gc> *args;
4490 /* Build up a call to the user-defined operator. */
4491 /* Lookup the name we got back from the id-expression. */
4492 args = make_tree_vector ();
4493 vec_safe_push (args, value);
4494 vec_safe_push (args, build_int_cst (size_type_node, len));
4495 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4499 result = finish_call_expr (decl, &args, false, true,
4500 tf_warning_or_error);
4501 release_tree_vector (args);
4502 return result;
4504 release_tree_vector (args);
4506 /* Look for a template function with typename parameter CharT
4507 and parameter pack CharT... Call the function with
4508 template parameter characters representing the string. */
4509 args = make_tree_vector ();
4510 decl = lookup_literal_operator (name, args);
4511 if (decl && decl != error_mark_node)
4513 tree tmpl_args = make_string_pack (value);
4514 decl = lookup_template_function (decl, tmpl_args);
4515 result = finish_call_expr (decl, &args, false, true,
4516 tf_warning_or_error);
4517 release_tree_vector (args);
4518 return result;
4520 release_tree_vector (args);
4522 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4523 name, TREE_TYPE (value), size_type_node);
4524 return error_mark_node;
4528 /* Basic concepts [gram.basic] */
4530 /* Parse a translation-unit.
4532 translation-unit:
4533 declaration-seq [opt]
4535 Returns TRUE if all went well. */
4537 static bool
4538 cp_parser_translation_unit (cp_parser* parser)
4540 /* The address of the first non-permanent object on the declarator
4541 obstack. */
4542 static void *declarator_obstack_base;
4544 bool success;
4546 /* Create the declarator obstack, if necessary. */
4547 if (!cp_error_declarator)
4549 gcc_obstack_init (&declarator_obstack);
4550 /* Create the error declarator. */
4551 cp_error_declarator = make_declarator (cdk_error);
4552 /* Create the empty parameter list. */
4553 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4554 UNKNOWN_LOCATION);
4555 /* Remember where the base of the declarator obstack lies. */
4556 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4559 cp_parser_declaration_seq_opt (parser);
4561 /* If there are no tokens left then all went well. */
4562 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4564 /* Get rid of the token array; we don't need it any more. */
4565 cp_lexer_destroy (parser->lexer);
4566 parser->lexer = NULL;
4568 /* This file might have been a context that's implicitly extern
4569 "C". If so, pop the lang context. (Only relevant for PCH.) */
4570 if (parser->implicit_extern_c)
4572 pop_lang_context ();
4573 parser->implicit_extern_c = false;
4576 /* Finish up. */
4577 finish_translation_unit ();
4579 success = true;
4581 else
4583 cp_parser_error (parser, "expected declaration");
4584 success = false;
4587 /* Make sure the declarator obstack was fully cleaned up. */
4588 gcc_assert (obstack_next_free (&declarator_obstack)
4589 == declarator_obstack_base);
4591 /* All went well. */
4592 return success;
4595 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4596 decltype context. */
4598 static inline tsubst_flags_t
4599 complain_flags (bool decltype_p)
4601 tsubst_flags_t complain = tf_warning_or_error;
4602 if (decltype_p)
4603 complain |= tf_decltype;
4604 return complain;
4607 /* We're about to parse a collection of statements. If we're currently
4608 parsing tentatively, set up a firewall so that any nested
4609 cp_parser_commit_to_tentative_parse won't affect the current context. */
4611 static cp_token_position
4612 cp_parser_start_tentative_firewall (cp_parser *parser)
4614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4615 return 0;
4617 cp_parser_parse_tentatively (parser);
4618 cp_parser_commit_to_topmost_tentative_parse (parser);
4619 return cp_lexer_token_position (parser->lexer, false);
4622 /* We've finished parsing the collection of statements. Wrap up the
4623 firewall and replace the relevant tokens with the parsed form. */
4625 static void
4626 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4627 tree expr)
4629 if (!start)
4630 return;
4632 /* Finish the firewall level. */
4633 cp_parser_parse_definitely (parser);
4634 /* And remember the result of the parse for when we try again. */
4635 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4636 token->type = CPP_PREPARSED_EXPR;
4637 token->u.value = expr;
4638 token->keyword = RID_MAX;
4639 cp_lexer_purge_tokens_after (parser->lexer, start);
4642 /* Like the above functions, but let the user modify the tokens. Used by
4643 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4644 later parses, so it makes sense to localize the effects of
4645 cp_parser_commit_to_tentative_parse. */
4647 struct tentative_firewall
4649 cp_parser *parser;
4650 bool set;
4652 tentative_firewall (cp_parser *p): parser(p)
4654 /* If we're currently parsing tentatively, start a committed level as a
4655 firewall and then an inner tentative parse. */
4656 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4658 cp_parser_parse_tentatively (parser);
4659 cp_parser_commit_to_topmost_tentative_parse (parser);
4660 cp_parser_parse_tentatively (parser);
4664 ~tentative_firewall()
4666 if (set)
4668 /* Finish the inner tentative parse and the firewall, propagating any
4669 uncommitted error state to the outer tentative parse. */
4670 bool err = cp_parser_error_occurred (parser);
4671 cp_parser_parse_definitely (parser);
4672 cp_parser_parse_definitely (parser);
4673 if (err)
4674 cp_parser_simulate_error (parser);
4679 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4680 This class is for tracking such a matching pair of symbols.
4681 In particular, it tracks the location of the first token,
4682 so that if the second token is missing, we can highlight the
4683 location of the first token when notifying the user about the
4684 problem. */
4686 template <typename traits_t>
4687 class token_pair
4689 public:
4690 /* token_pair's ctor. */
4691 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4693 /* If the next token is the opening symbol for this pair, consume it and
4694 return true.
4695 Otherwise, issue an error and return false.
4696 In either case, record the location of the opening token. */
4698 bool require_open (cp_parser *parser)
4700 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4701 return cp_parser_require (parser, traits_t::open_token_type,
4702 traits_t::required_token_open);
4705 /* Consume the next token from PARSER, recording its location as
4706 that of the opening token within the pair. */
4708 cp_token * consume_open (cp_parser *parser)
4710 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4711 gcc_assert (tok->type == traits_t::open_token_type);
4712 m_open_loc = tok->location;
4713 return tok;
4716 /* If the next token is the closing symbol for this pair, consume it
4717 and return it.
4718 Otherwise, issue an error, highlighting the location of the
4719 corresponding opening token, and return NULL. */
4721 cp_token *require_close (cp_parser *parser) const
4723 return cp_parser_require (parser, traits_t::close_token_type,
4724 traits_t::required_token_close,
4725 m_open_loc);
4728 private:
4729 location_t m_open_loc;
4732 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4734 struct matching_paren_traits
4736 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4737 static const enum required_token required_token_open = RT_OPEN_PAREN;
4738 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4739 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4742 /* "matching_parens" is a token_pair<T> class for tracking matching
4743 pairs of parentheses. */
4745 typedef token_pair<matching_paren_traits> matching_parens;
4747 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4749 struct matching_brace_traits
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4752 static const enum required_token required_token_open = RT_OPEN_BRACE;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4754 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4757 /* "matching_braces" is a token_pair<T> class for tracking matching
4758 pairs of braces. */
4760 typedef token_pair<matching_brace_traits> matching_braces;
4763 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4764 enclosing parentheses. */
4766 static cp_expr
4767 cp_parser_statement_expr (cp_parser *parser)
4769 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4771 /* Consume the '('. */
4772 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4773 matching_parens parens;
4774 parens.consume_open (parser);
4775 /* Start the statement-expression. */
4776 tree expr = begin_stmt_expr ();
4777 /* Parse the compound-statement. */
4778 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4779 /* Finish up. */
4780 expr = finish_stmt_expr (expr, false);
4781 /* Consume the ')'. */
4782 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4783 if (!parens.require_close (parser))
4784 cp_parser_skip_to_end_of_statement (parser);
4786 cp_parser_end_tentative_firewall (parser, start, expr);
4787 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4788 return cp_expr (expr, combined_loc);
4791 /* Expressions [gram.expr] */
4793 /* Parse a fold-operator.
4795 fold-operator:
4796 - * / % ^ & | = < > << >>
4797 = -= *= /= %= ^= &= |= <<= >>=
4798 == != <= >= && || , .* ->*
4800 This returns the tree code corresponding to the matched operator
4801 as an int. When the current token matches a compound assignment
4802 opertor, the resulting tree code is the negative value of the
4803 non-assignment operator. */
4805 static int
4806 cp_parser_fold_operator (cp_token *token)
4808 switch (token->type)
4810 case CPP_PLUS: return PLUS_EXPR;
4811 case CPP_MINUS: return MINUS_EXPR;
4812 case CPP_MULT: return MULT_EXPR;
4813 case CPP_DIV: return TRUNC_DIV_EXPR;
4814 case CPP_MOD: return TRUNC_MOD_EXPR;
4815 case CPP_XOR: return BIT_XOR_EXPR;
4816 case CPP_AND: return BIT_AND_EXPR;
4817 case CPP_OR: return BIT_IOR_EXPR;
4818 case CPP_LSHIFT: return LSHIFT_EXPR;
4819 case CPP_RSHIFT: return RSHIFT_EXPR;
4821 case CPP_EQ: return -NOP_EXPR;
4822 case CPP_PLUS_EQ: return -PLUS_EXPR;
4823 case CPP_MINUS_EQ: return -MINUS_EXPR;
4824 case CPP_MULT_EQ: return -MULT_EXPR;
4825 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4826 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4827 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4828 case CPP_AND_EQ: return -BIT_AND_EXPR;
4829 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4830 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4831 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4833 case CPP_EQ_EQ: return EQ_EXPR;
4834 case CPP_NOT_EQ: return NE_EXPR;
4835 case CPP_LESS: return LT_EXPR;
4836 case CPP_GREATER: return GT_EXPR;
4837 case CPP_LESS_EQ: return LE_EXPR;
4838 case CPP_GREATER_EQ: return GE_EXPR;
4840 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4841 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4843 case CPP_COMMA: return COMPOUND_EXPR;
4845 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4846 case CPP_DEREF_STAR: return MEMBER_REF;
4848 default: return ERROR_MARK;
4852 /* Returns true if CODE indicates a binary expression, which is not allowed in
4853 the LHS of a fold-expression. More codes will need to be added to use this
4854 function in other contexts. */
4856 static bool
4857 is_binary_op (tree_code code)
4859 switch (code)
4861 case PLUS_EXPR:
4862 case POINTER_PLUS_EXPR:
4863 case MINUS_EXPR:
4864 case MULT_EXPR:
4865 case TRUNC_DIV_EXPR:
4866 case TRUNC_MOD_EXPR:
4867 case BIT_XOR_EXPR:
4868 case BIT_AND_EXPR:
4869 case BIT_IOR_EXPR:
4870 case LSHIFT_EXPR:
4871 case RSHIFT_EXPR:
4873 case MODOP_EXPR:
4875 case EQ_EXPR:
4876 case NE_EXPR:
4877 case LE_EXPR:
4878 case GE_EXPR:
4879 case LT_EXPR:
4880 case GT_EXPR:
4882 case TRUTH_ANDIF_EXPR:
4883 case TRUTH_ORIF_EXPR:
4885 case COMPOUND_EXPR:
4887 case DOTSTAR_EXPR:
4888 case MEMBER_REF:
4889 return true;
4891 default:
4892 return false;
4896 /* If the next token is a suitable fold operator, consume it and return as
4897 the function above. */
4899 static int
4900 cp_parser_fold_operator (cp_parser *parser)
4902 cp_token* token = cp_lexer_peek_token (parser->lexer);
4903 int code = cp_parser_fold_operator (token);
4904 if (code != ERROR_MARK)
4905 cp_lexer_consume_token (parser->lexer);
4906 return code;
4909 /* Parse a fold-expression.
4911 fold-expression:
4912 ( ... folding-operator cast-expression)
4913 ( cast-expression folding-operator ... )
4914 ( cast-expression folding operator ... folding-operator cast-expression)
4916 Note that the '(' and ')' are matched in primary expression. */
4918 static cp_expr
4919 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4921 cp_id_kind pidk;
4923 // Left fold.
4924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4926 cp_lexer_consume_token (parser->lexer);
4927 int op = cp_parser_fold_operator (parser);
4928 if (op == ERROR_MARK)
4930 cp_parser_error (parser, "expected binary operator");
4931 return error_mark_node;
4934 tree expr = cp_parser_cast_expression (parser, false, false,
4935 false, &pidk);
4936 if (expr == error_mark_node)
4937 return error_mark_node;
4938 return finish_left_unary_fold_expr (expr, op);
4941 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4951 cp_parser_error (parser, "expected ...");
4952 return error_mark_node;
4954 cp_lexer_consume_token (parser->lexer);
4956 /* The operands of a fold-expression are cast-expressions, so binary or
4957 conditional expressions are not allowed. We check this here to avoid
4958 tentative parsing. */
4959 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4960 /* OK, the expression was parenthesized. */;
4961 else if (is_binary_op (TREE_CODE (expr1)))
4962 error_at (location_of (expr1),
4963 "binary expression in operand of fold-expression");
4964 else if (TREE_CODE (expr1) == COND_EXPR)
4965 error_at (location_of (expr1),
4966 "conditional expression in operand of fold-expression");
4968 // Right fold.
4969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4970 return finish_right_unary_fold_expr (expr1, op);
4972 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4974 cp_parser_error (parser, "mismatched operator in fold-expression");
4975 return error_mark_node;
4977 cp_lexer_consume_token (parser->lexer);
4979 // Binary left or right fold.
4980 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4981 if (expr2 == error_mark_node)
4982 return error_mark_node;
4983 return finish_binary_fold_expr (expr1, expr2, op);
4986 /* Parse a primary-expression.
4988 primary-expression:
4989 literal
4990 this
4991 ( expression )
4992 id-expression
4993 lambda-expression (C++11)
4995 GNU Extensions:
4997 primary-expression:
4998 ( compound-statement )
4999 __builtin_va_arg ( assignment-expression , type-id )
5000 __builtin_offsetof ( type-id , offsetof-expression )
5002 C++ Extensions:
5003 __has_nothrow_assign ( type-id )
5004 __has_nothrow_constructor ( type-id )
5005 __has_nothrow_copy ( type-id )
5006 __has_trivial_assign ( type-id )
5007 __has_trivial_constructor ( type-id )
5008 __has_trivial_copy ( type-id )
5009 __has_trivial_destructor ( type-id )
5010 __has_virtual_destructor ( type-id )
5011 __is_abstract ( type-id )
5012 __is_base_of ( type-id , type-id )
5013 __is_class ( type-id )
5014 __is_empty ( type-id )
5015 __is_enum ( type-id )
5016 __is_final ( type-id )
5017 __is_literal_type ( type-id )
5018 __is_pod ( type-id )
5019 __is_polymorphic ( type-id )
5020 __is_std_layout ( type-id )
5021 __is_trivial ( type-id )
5022 __is_union ( type-id )
5024 Objective-C++ Extension:
5026 primary-expression:
5027 objc-expression
5029 literal:
5030 __null
5032 ADDRESS_P is true iff this expression was immediately preceded by
5033 "&" and therefore might denote a pointer-to-member. CAST_P is true
5034 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5035 true iff this expression is a template argument.
5037 Returns a representation of the expression. Upon return, *IDK
5038 indicates what kind of id-expression (if any) was present. */
5040 static cp_expr
5041 cp_parser_primary_expression (cp_parser *parser,
5042 bool address_p,
5043 bool cast_p,
5044 bool template_arg_p,
5045 bool decltype_p,
5046 cp_id_kind *idk)
5048 cp_token *token = NULL;
5050 /* Assume the primary expression is not an id-expression. */
5051 *idk = CP_ID_KIND_NONE;
5053 /* Peek at the next token. */
5054 token = cp_lexer_peek_token (parser->lexer);
5055 switch ((int) token->type)
5057 /* literal:
5058 integer-literal
5059 character-literal
5060 floating-literal
5061 string-literal
5062 boolean-literal
5063 pointer-literal
5064 user-defined-literal */
5065 case CPP_CHAR:
5066 case CPP_CHAR16:
5067 case CPP_CHAR32:
5068 case CPP_WCHAR:
5069 case CPP_UTF8CHAR:
5070 case CPP_NUMBER:
5071 case CPP_PREPARSED_EXPR:
5072 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5073 return cp_parser_userdef_numeric_literal (parser);
5074 token = cp_lexer_consume_token (parser->lexer);
5075 if (TREE_CODE (token->u.value) == FIXED_CST)
5077 error_at (token->location,
5078 "fixed-point types not supported in C++");
5079 return error_mark_node;
5081 /* Floating-point literals are only allowed in an integral
5082 constant expression if they are cast to an integral or
5083 enumeration type. */
5084 if (TREE_CODE (token->u.value) == REAL_CST
5085 && parser->integral_constant_expression_p
5086 && pedantic)
5088 /* CAST_P will be set even in invalid code like "int(2.7 +
5089 ...)". Therefore, we have to check that the next token
5090 is sure to end the cast. */
5091 if (cast_p)
5093 cp_token *next_token;
5095 next_token = cp_lexer_peek_token (parser->lexer);
5096 if (/* The comma at the end of an
5097 enumerator-definition. */
5098 next_token->type != CPP_COMMA
5099 /* The curly brace at the end of an enum-specifier. */
5100 && next_token->type != CPP_CLOSE_BRACE
5101 /* The end of a statement. */
5102 && next_token->type != CPP_SEMICOLON
5103 /* The end of the cast-expression. */
5104 && next_token->type != CPP_CLOSE_PAREN
5105 /* The end of an array bound. */
5106 && next_token->type != CPP_CLOSE_SQUARE
5107 /* The closing ">" in a template-argument-list. */
5108 && (next_token->type != CPP_GREATER
5109 || parser->greater_than_is_operator_p)
5110 /* C++0x only: A ">>" treated like two ">" tokens,
5111 in a template-argument-list. */
5112 && (next_token->type != CPP_RSHIFT
5113 || (cxx_dialect == cxx98)
5114 || parser->greater_than_is_operator_p))
5115 cast_p = false;
5118 /* If we are within a cast, then the constraint that the
5119 cast is to an integral or enumeration type will be
5120 checked at that point. If we are not within a cast, then
5121 this code is invalid. */
5122 if (!cast_p)
5123 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5125 return cp_expr (token->u.value, token->location);
5127 case CPP_CHAR_USERDEF:
5128 case CPP_CHAR16_USERDEF:
5129 case CPP_CHAR32_USERDEF:
5130 case CPP_WCHAR_USERDEF:
5131 case CPP_UTF8CHAR_USERDEF:
5132 return cp_parser_userdef_char_literal (parser);
5134 case CPP_STRING:
5135 case CPP_STRING16:
5136 case CPP_STRING32:
5137 case CPP_WSTRING:
5138 case CPP_UTF8STRING:
5139 case CPP_STRING_USERDEF:
5140 case CPP_STRING16_USERDEF:
5141 case CPP_STRING32_USERDEF:
5142 case CPP_WSTRING_USERDEF:
5143 case CPP_UTF8STRING_USERDEF:
5144 /* ??? Should wide strings be allowed when parser->translate_strings_p
5145 is false (i.e. in attributes)? If not, we can kill the third
5146 argument to cp_parser_string_literal. */
5147 return cp_parser_string_literal (parser,
5148 parser->translate_strings_p,
5149 true);
5151 case CPP_OPEN_PAREN:
5152 /* If we see `( { ' then we are looking at the beginning of
5153 a GNU statement-expression. */
5154 if (cp_parser_allow_gnu_extensions_p (parser)
5155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5157 /* Statement-expressions are not allowed by the standard. */
5158 pedwarn (token->location, OPT_Wpedantic,
5159 "ISO C++ forbids braced-groups within expressions");
5161 /* And they're not allowed outside of a function-body; you
5162 cannot, for example, write:
5164 int i = ({ int j = 3; j + 1; });
5166 at class or namespace scope. */
5167 if (!parser->in_function_body
5168 || parser->in_template_argument_list_p)
5170 error_at (token->location,
5171 "statement-expressions are not allowed outside "
5172 "functions nor in template-argument lists");
5173 cp_parser_skip_to_end_of_block_or_statement (parser);
5174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5175 cp_lexer_consume_token (parser->lexer);
5176 return error_mark_node;
5178 else
5179 return cp_parser_statement_expr (parser);
5181 /* Otherwise it's a normal parenthesized expression. */
5183 cp_expr expr;
5184 bool saved_greater_than_is_operator_p;
5186 location_t open_paren_loc = token->location;
5188 /* Consume the `('. */
5189 matching_parens parens;
5190 parens.consume_open (parser);
5191 /* Within a parenthesized expression, a `>' token is always
5192 the greater-than operator. */
5193 saved_greater_than_is_operator_p
5194 = parser->greater_than_is_operator_p;
5195 parser->greater_than_is_operator_p = true;
5197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5198 /* Left fold expression. */
5199 expr = NULL_TREE;
5200 else
5201 /* Parse the parenthesized expression. */
5202 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5204 token = cp_lexer_peek_token (parser->lexer);
5205 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5207 expr = cp_parser_fold_expression (parser, expr);
5208 if (expr != error_mark_node
5209 && cxx_dialect < cxx17
5210 && !in_system_header_at (input_location))
5211 pedwarn (input_location, 0, "fold-expressions only available "
5212 "with -std=c++17 or -std=gnu++17");
5214 else
5215 /* Let the front end know that this expression was
5216 enclosed in parentheses. This matters in case, for
5217 example, the expression is of the form `A::B', since
5218 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5219 not. */
5220 expr = finish_parenthesized_expr (expr);
5222 /* DR 705: Wrapping an unqualified name in parentheses
5223 suppresses arg-dependent lookup. We want to pass back
5224 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5225 (c++/37862), but none of the others. */
5226 if (*idk != CP_ID_KIND_QUALIFIED)
5227 *idk = CP_ID_KIND_NONE;
5229 /* The `>' token might be the end of a template-id or
5230 template-parameter-list now. */
5231 parser->greater_than_is_operator_p
5232 = saved_greater_than_is_operator_p;
5234 /* Consume the `)'. */
5235 token = cp_lexer_peek_token (parser->lexer);
5236 location_t close_paren_loc = token->location;
5237 expr.set_range (open_paren_loc, close_paren_loc);
5238 if (!parens.require_close (parser)
5239 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5240 cp_parser_skip_to_end_of_statement (parser);
5242 return expr;
5245 case CPP_OPEN_SQUARE:
5247 if (c_dialect_objc ())
5249 /* We might have an Objective-C++ message. */
5250 cp_parser_parse_tentatively (parser);
5251 tree msg = cp_parser_objc_message_expression (parser);
5252 /* If that works out, we're done ... */
5253 if (cp_parser_parse_definitely (parser))
5254 return msg;
5255 /* ... else, fall though to see if it's a lambda. */
5257 cp_expr lam = cp_parser_lambda_expression (parser);
5258 /* Don't warn about a failed tentative parse. */
5259 if (cp_parser_error_occurred (parser))
5260 return error_mark_node;
5261 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5262 return lam;
5265 case CPP_OBJC_STRING:
5266 if (c_dialect_objc ())
5267 /* We have an Objective-C++ string literal. */
5268 return cp_parser_objc_expression (parser);
5269 cp_parser_error (parser, "expected primary-expression");
5270 return error_mark_node;
5272 case CPP_KEYWORD:
5273 switch (token->keyword)
5275 /* These two are the boolean literals. */
5276 case RID_TRUE:
5277 cp_lexer_consume_token (parser->lexer);
5278 return cp_expr (boolean_true_node, token->location);
5279 case RID_FALSE:
5280 cp_lexer_consume_token (parser->lexer);
5281 return cp_expr (boolean_false_node, token->location);
5283 /* The `__null' literal. */
5284 case RID_NULL:
5285 cp_lexer_consume_token (parser->lexer);
5286 return cp_expr (null_node, token->location);
5288 /* The `nullptr' literal. */
5289 case RID_NULLPTR:
5290 cp_lexer_consume_token (parser->lexer);
5291 return cp_expr (nullptr_node, token->location);
5293 /* Recognize the `this' keyword. */
5294 case RID_THIS:
5295 cp_lexer_consume_token (parser->lexer);
5296 if (parser->local_variables_forbidden_p)
5298 error_at (token->location,
5299 "%<this%> may not be used in this context");
5300 return error_mark_node;
5302 /* Pointers cannot appear in constant-expressions. */
5303 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5304 return error_mark_node;
5305 return cp_expr (finish_this_expr (), token->location);
5307 /* The `operator' keyword can be the beginning of an
5308 id-expression. */
5309 case RID_OPERATOR:
5310 goto id_expression;
5312 case RID_FUNCTION_NAME:
5313 case RID_PRETTY_FUNCTION_NAME:
5314 case RID_C99_FUNCTION_NAME:
5316 non_integral_constant name;
5318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5319 __func__ are the names of variables -- but they are
5320 treated specially. Therefore, they are handled here,
5321 rather than relying on the generic id-expression logic
5322 below. Grammatically, these names are id-expressions.
5324 Consume the token. */
5325 token = cp_lexer_consume_token (parser->lexer);
5327 switch (token->keyword)
5329 case RID_FUNCTION_NAME:
5330 name = NIC_FUNC_NAME;
5331 break;
5332 case RID_PRETTY_FUNCTION_NAME:
5333 name = NIC_PRETTY_FUNC;
5334 break;
5335 case RID_C99_FUNCTION_NAME:
5336 name = NIC_C99_FUNC;
5337 break;
5338 default:
5339 gcc_unreachable ();
5342 if (cp_parser_non_integral_constant_expression (parser, name))
5343 return error_mark_node;
5345 /* Look up the name. */
5346 return finish_fname (token->u.value);
5349 case RID_VA_ARG:
5351 tree expression;
5352 tree type;
5353 source_location type_location;
5354 location_t start_loc
5355 = cp_lexer_peek_token (parser->lexer)->location;
5356 /* The `__builtin_va_arg' construct is used to handle
5357 `va_arg'. Consume the `__builtin_va_arg' token. */
5358 cp_lexer_consume_token (parser->lexer);
5359 /* Look for the opening `('. */
5360 matching_parens parens;
5361 parens.require_open (parser);
5362 /* Now, parse the assignment-expression. */
5363 expression = cp_parser_assignment_expression (parser);
5364 /* Look for the `,'. */
5365 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5366 type_location = cp_lexer_peek_token (parser->lexer)->location;
5367 /* Parse the type-id. */
5369 type_id_in_expr_sentinel s (parser);
5370 type = cp_parser_type_id (parser);
5372 /* Look for the closing `)'. */
5373 location_t finish_loc
5374 = cp_lexer_peek_token (parser->lexer)->location;
5375 parens.require_close (parser);
5376 /* Using `va_arg' in a constant-expression is not
5377 allowed. */
5378 if (cp_parser_non_integral_constant_expression (parser,
5379 NIC_VA_ARG))
5380 return error_mark_node;
5381 /* Construct a location of the form:
5382 __builtin_va_arg (v, int)
5383 ~~~~~~~~~~~~~~~~~~~~~^~~~
5384 with the caret at the type, ranging from the start of the
5385 "__builtin_va_arg" token to the close paren. */
5386 location_t combined_loc
5387 = make_location (type_location, start_loc, finish_loc);
5388 return build_x_va_arg (combined_loc, expression, type);
5391 case RID_OFFSETOF:
5392 return cp_parser_builtin_offsetof (parser);
5394 case RID_HAS_NOTHROW_ASSIGN:
5395 case RID_HAS_NOTHROW_CONSTRUCTOR:
5396 case RID_HAS_NOTHROW_COPY:
5397 case RID_HAS_TRIVIAL_ASSIGN:
5398 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5399 case RID_HAS_TRIVIAL_COPY:
5400 case RID_HAS_TRIVIAL_DESTRUCTOR:
5401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5402 case RID_HAS_VIRTUAL_DESTRUCTOR:
5403 case RID_IS_ABSTRACT:
5404 case RID_IS_AGGREGATE:
5405 case RID_IS_BASE_OF:
5406 case RID_IS_CLASS:
5407 case RID_IS_EMPTY:
5408 case RID_IS_ENUM:
5409 case RID_IS_FINAL:
5410 case RID_IS_LITERAL_TYPE:
5411 case RID_IS_POD:
5412 case RID_IS_POLYMORPHIC:
5413 case RID_IS_SAME_AS:
5414 case RID_IS_STD_LAYOUT:
5415 case RID_IS_TRIVIAL:
5416 case RID_IS_TRIVIALLY_ASSIGNABLE:
5417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5418 case RID_IS_TRIVIALLY_COPYABLE:
5419 case RID_IS_UNION:
5420 case RID_IS_ASSIGNABLE:
5421 case RID_IS_CONSTRUCTIBLE:
5422 return cp_parser_trait_expr (parser, token->keyword);
5424 // C++ concepts
5425 case RID_REQUIRES:
5426 return cp_parser_requires_expression (parser);
5428 /* Objective-C++ expressions. */
5429 case RID_AT_ENCODE:
5430 case RID_AT_PROTOCOL:
5431 case RID_AT_SELECTOR:
5432 return cp_parser_objc_expression (parser);
5434 case RID_TEMPLATE:
5435 if (parser->in_function_body
5436 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5437 == CPP_LESS))
5439 error_at (token->location,
5440 "a template declaration cannot appear at block scope");
5441 cp_parser_skip_to_end_of_block_or_statement (parser);
5442 return error_mark_node;
5444 /* FALLTHRU */
5445 default:
5446 cp_parser_error (parser, "expected primary-expression");
5447 return error_mark_node;
5450 /* An id-expression can start with either an identifier, a
5451 `::' as the beginning of a qualified-id, or the "operator"
5452 keyword. */
5453 case CPP_NAME:
5454 case CPP_SCOPE:
5455 case CPP_TEMPLATE_ID:
5456 case CPP_NESTED_NAME_SPECIFIER:
5458 id_expression:
5459 cp_expr id_expression;
5460 cp_expr decl;
5461 const char *error_msg;
5462 bool template_p;
5463 bool done;
5464 cp_token *id_expr_token;
5466 /* Parse the id-expression. */
5467 id_expression
5468 = cp_parser_id_expression (parser,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5471 &template_p,
5472 /*declarator_p=*/false,
5473 /*optional_p=*/false);
5474 if (id_expression == error_mark_node)
5475 return error_mark_node;
5476 id_expr_token = token;
5477 token = cp_lexer_peek_token (parser->lexer);
5478 done = (token->type != CPP_OPEN_SQUARE
5479 && token->type != CPP_OPEN_PAREN
5480 && token->type != CPP_DOT
5481 && token->type != CPP_DEREF
5482 && token->type != CPP_PLUS_PLUS
5483 && token->type != CPP_MINUS_MINUS);
5484 /* If we have a template-id, then no further lookup is
5485 required. If the template-id was for a template-class, we
5486 will sometimes have a TYPE_DECL at this point. */
5487 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5488 || TREE_CODE (id_expression) == TYPE_DECL)
5489 decl = id_expression;
5490 /* Look up the name. */
5491 else
5493 tree ambiguous_decls;
5495 /* If we already know that this lookup is ambiguous, then
5496 we've already issued an error message; there's no reason
5497 to check again. */
5498 if (id_expr_token->type == CPP_NAME
5499 && id_expr_token->error_reported)
5501 cp_parser_simulate_error (parser);
5502 return error_mark_node;
5505 decl = cp_parser_lookup_name (parser, id_expression,
5506 none_type,
5507 template_p,
5508 /*is_namespace=*/false,
5509 /*check_dependency=*/true,
5510 &ambiguous_decls,
5511 id_expr_token->location);
5512 /* If the lookup was ambiguous, an error will already have
5513 been issued. */
5514 if (ambiguous_decls)
5515 return error_mark_node;
5517 /* In Objective-C++, we may have an Objective-C 2.0
5518 dot-syntax for classes here. */
5519 if (c_dialect_objc ()
5520 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5521 && TREE_CODE (decl) == TYPE_DECL
5522 && objc_is_class_name (decl))
5524 tree component;
5525 cp_lexer_consume_token (parser->lexer);
5526 component = cp_parser_identifier (parser);
5527 if (component == error_mark_node)
5528 return error_mark_node;
5530 tree result = objc_build_class_component_ref (id_expression,
5531 component);
5532 /* Build a location of the form:
5533 expr.component
5534 ~~~~~^~~~~~~~~
5535 with caret at the start of the component name (at
5536 input_location), ranging from the start of the id_expression
5537 to the end of the component name. */
5538 location_t combined_loc
5539 = make_location (input_location, id_expression.get_start (),
5540 get_finish (input_location));
5541 protected_set_expr_location (result, combined_loc);
5542 return result;
5545 /* In Objective-C++, an instance variable (ivar) may be preferred
5546 to whatever cp_parser_lookup_name() found.
5547 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5548 rest of c-family, we have to do a little extra work to preserve
5549 any location information in cp_expr "decl". Given that
5550 objc_lookup_ivar is implemented in "c-family" and "objc", we
5551 have a trip through the pure "tree" type, rather than cp_expr.
5552 Naively copying it back to "decl" would implicitly give the
5553 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5554 store an EXPR_LOCATION. Hence we only update "decl" (and
5555 hence its location_t) if we get back a different tree node. */
5556 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5557 id_expression);
5558 if (decl_tree != decl.get_value ())
5559 decl = cp_expr (decl_tree);
5561 /* If name lookup gives us a SCOPE_REF, then the
5562 qualifying scope was dependent. */
5563 if (TREE_CODE (decl) == SCOPE_REF)
5565 /* At this point, we do not know if DECL is a valid
5566 integral constant expression. We assume that it is
5567 in fact such an expression, so that code like:
5569 template <int N> struct A {
5570 int a[B<N>::i];
5573 is accepted. At template-instantiation time, we
5574 will check that B<N>::i is actually a constant. */
5575 return decl;
5577 /* Check to see if DECL is a local variable in a context
5578 where that is forbidden. */
5579 if (parser->local_variables_forbidden_p
5580 && local_variable_p (decl))
5582 /* It might be that we only found DECL because we are
5583 trying to be generous with pre-ISO scoping rules.
5584 For example, consider:
5586 int i;
5587 void g() {
5588 for (int i = 0; i < 10; ++i) {}
5589 extern void f(int j = i);
5592 Here, name look up will originally find the out
5593 of scope `i'. We need to issue a warning message,
5594 but then use the global `i'. */
5595 decl = check_for_out_of_scope_variable (decl);
5596 if (local_variable_p (decl))
5598 error_at (id_expr_token->location,
5599 "local variable %qD may not appear in this context",
5600 decl.get_value ());
5601 return error_mark_node;
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5640 /* Parse an id-expression.
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5735 return unqualified_id;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5741 cp_token *token;
5742 tree id;
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5770 switch (token->type)
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5792 /* Parse an unqualified-id.
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5821 cp_token *token;
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5826 switch ((int) token->type)
5828 case CPP_NAME:
5830 tree id;
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5853 case CPP_COMPL:
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5884 struct S {
5885 int S;
5886 ~S();
5889 S::~S() {}
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5920 gcc_assert (!scope || TYPE_P (scope));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6005 /* Look in the surrounding context. */
6006 if (!done)
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6059 /* [class.dtor]
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6077 cp_expr id;
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6097 else if (UDLIT_OPER_P (id))
6099 /* 17.6.3.3.5 */
6100 const char *name = UDLIT_OP_SUFFIX (id);
6101 if (name[0] != '_' && !in_system_header_at (input_location)
6102 && declarator_p)
6103 warning (OPT_Wliteral_suffix,
6104 "literal operator suffixes not preceded by %<_%>"
6105 " are reserved for future standardization");
6108 return id;
6110 /* Fall through. */
6112 default:
6113 if (optional_p)
6114 return NULL_TREE;
6115 cp_parser_error (parser, "expected unqualified-id");
6116 return error_mark_node;
6120 /* Parse an (optional) nested-name-specifier.
6122 nested-name-specifier: [C++98]
6123 class-or-namespace-name :: nested-name-specifier [opt]
6124 class-or-namespace-name :: template nested-name-specifier [opt]
6126 nested-name-specifier: [C++0x]
6127 type-name ::
6128 namespace-name ::
6129 nested-name-specifier identifier ::
6130 nested-name-specifier template [opt] simple-template-id ::
6132 PARSER->SCOPE should be set appropriately before this function is
6133 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6135 in name lookups.
6137 Sets PARSER->SCOPE to the class (TYPE) or namespace
6138 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139 it unchanged if there is no nested-name-specifier. Returns the new
6140 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6142 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143 part of a declaration and/or decl-specifier. */
6145 static tree
6146 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6147 bool typename_keyword_p,
6148 bool check_dependency_p,
6149 bool type_p,
6150 bool is_declaration,
6151 bool template_keyword_p /* = false */)
6153 bool success = false;
6154 cp_token_position start = 0;
6155 cp_token *token;
6157 /* Remember where the nested-name-specifier starts. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6160 start = cp_lexer_token_position (parser->lexer, false);
6161 push_deferring_access_checks (dk_deferred);
6164 while (true)
6166 tree new_scope;
6167 tree old_scope;
6168 tree saved_qualifying_scope;
6170 /* Spot cases that cannot be the beginning of a
6171 nested-name-specifier. */
6172 token = cp_lexer_peek_token (parser->lexer);
6174 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 the already parsed nested-name-specifier. */
6176 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6178 /* Grab the nested-name-specifier and continue the loop. */
6179 cp_parser_pre_parsed_nested_name_specifier (parser);
6180 /* If we originally encountered this nested-name-specifier
6181 with IS_DECLARATION set to false, we will not have
6182 resolved TYPENAME_TYPEs, so we must do so here. */
6183 if (is_declaration
6184 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6186 new_scope = resolve_typename_type (parser->scope,
6187 /*only_current_p=*/false);
6188 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6189 parser->scope = new_scope;
6191 success = true;
6192 continue;
6195 /* Spot cases that cannot be the beginning of a
6196 nested-name-specifier. On the second and subsequent times
6197 through the loop, we look for the `template' keyword. */
6198 if (success && token->keyword == RID_TEMPLATE)
6200 /* A template-id can start a nested-name-specifier. */
6201 else if (token->type == CPP_TEMPLATE_ID)
6203 /* DR 743: decltype can be used in a nested-name-specifier. */
6204 else if (token_is_decltype (token))
6206 else
6208 /* If the next token is not an identifier, then it is
6209 definitely not a type-name or namespace-name. */
6210 if (token->type != CPP_NAME)
6211 break;
6212 /* If the following token is neither a `<' (to begin a
6213 template-id), nor a `::', then we are not looking at a
6214 nested-name-specifier. */
6215 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6217 if (token->type == CPP_COLON
6218 && parser->colon_corrects_to_scope_p
6219 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6221 gcc_rich_location richloc (token->location);
6222 richloc.add_fixit_replace ("::");
6223 error_at (&richloc,
6224 "found %<:%> in nested-name-specifier, "
6225 "expected %<::%>");
6226 token->type = CPP_SCOPE;
6229 if (token->type != CPP_SCOPE
6230 && !cp_parser_nth_token_starts_template_argument_list_p
6231 (parser, 2))
6232 break;
6235 /* The nested-name-specifier is optional, so we parse
6236 tentatively. */
6237 cp_parser_parse_tentatively (parser);
6239 /* Look for the optional `template' keyword, if this isn't the
6240 first time through the loop. */
6241 if (success)
6242 template_keyword_p = cp_parser_optional_template_keyword (parser);
6244 /* Save the old scope since the name lookup we are about to do
6245 might destroy it. */
6246 old_scope = parser->scope;
6247 saved_qualifying_scope = parser->qualifying_scope;
6248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 look up names in "X<T>::I" in order to determine that "Y" is
6250 a template. So, if we have a typename at this point, we make
6251 an effort to look through it. */
6252 if (is_declaration
6253 && !typename_keyword_p
6254 && parser->scope
6255 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6256 parser->scope = resolve_typename_type (parser->scope,
6257 /*only_current_p=*/false);
6258 /* Parse the qualifying entity. */
6259 new_scope
6260 = cp_parser_qualifying_entity (parser,
6261 typename_keyword_p,
6262 template_keyword_p,
6263 check_dependency_p,
6264 type_p,
6265 is_declaration);
6266 /* Look for the `::' token. */
6267 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6269 /* If we found what we wanted, we keep going; otherwise, we're
6270 done. */
6271 if (!cp_parser_parse_definitely (parser))
6273 bool error_p = false;
6275 /* Restore the OLD_SCOPE since it was valid before the
6276 failed attempt at finding the last
6277 class-or-namespace-name. */
6278 parser->scope = old_scope;
6279 parser->qualifying_scope = saved_qualifying_scope;
6281 /* If the next token is a decltype, and the one after that is a
6282 `::', then the decltype has failed to resolve to a class or
6283 enumeration type. Give this error even when parsing
6284 tentatively since it can't possibly be valid--and we're going
6285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 won't get another chance.*/
6287 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6288 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6289 == CPP_SCOPE))
6291 token = cp_lexer_consume_token (parser->lexer);
6292 error_at (token->location, "decltype evaluates to %qT, "
6293 "which is not a class or enumeration type",
6294 token->u.tree_check_value->value);
6295 parser->scope = error_mark_node;
6296 error_p = true;
6297 /* As below. */
6298 success = true;
6299 cp_lexer_consume_token (parser->lexer);
6302 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6303 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6305 /* If we have a non-type template-id followed by ::, it can't
6306 possibly be valid. */
6307 token = cp_lexer_peek_token (parser->lexer);
6308 tree tid = token->u.tree_check_value->value;
6309 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6310 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6312 tree tmpl = NULL_TREE;
6313 if (is_overloaded_fn (tid))
6315 tree fns = get_fns (tid);
6316 if (OVL_SINGLE_P (fns))
6317 tmpl = OVL_FIRST (fns);
6318 error_at (token->location, "function template-id %qD "
6319 "in nested-name-specifier", tid);
6321 else
6323 /* Variable template. */
6324 tmpl = TREE_OPERAND (tid, 0);
6325 gcc_assert (variable_template_p (tmpl));
6326 error_at (token->location, "variable template-id %qD "
6327 "in nested-name-specifier", tid);
6329 if (tmpl)
6330 inform (DECL_SOURCE_LOCATION (tmpl),
6331 "%qD declared here", tmpl);
6333 parser->scope = error_mark_node;
6334 error_p = true;
6335 /* As below. */
6336 success = true;
6337 cp_lexer_consume_token (parser->lexer);
6338 cp_lexer_consume_token (parser->lexer);
6342 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6343 break;
6344 /* If the next token is an identifier, and the one after
6345 that is a `::', then any valid interpretation would have
6346 found a class-or-namespace-name. */
6347 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6348 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6349 == CPP_SCOPE)
6350 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6351 != CPP_COMPL))
6353 token = cp_lexer_consume_token (parser->lexer);
6354 if (!error_p)
6356 if (!token->error_reported)
6358 tree decl;
6359 tree ambiguous_decls;
6361 decl = cp_parser_lookup_name (parser, token->u.value,
6362 none_type,
6363 /*is_template=*/false,
6364 /*is_namespace=*/false,
6365 /*check_dependency=*/true,
6366 &ambiguous_decls,
6367 token->location);
6368 if (TREE_CODE (decl) == TEMPLATE_DECL)
6369 error_at (token->location,
6370 "%qD used without template parameters",
6371 decl);
6372 else if (ambiguous_decls)
6374 // cp_parser_lookup_name has the same diagnostic,
6375 // thus make sure to emit it at most once.
6376 if (cp_parser_uncommitted_to_tentative_parse_p
6377 (parser))
6379 error_at (token->location,
6380 "reference to %qD is ambiguous",
6381 token->u.value);
6382 print_candidates (ambiguous_decls);
6384 decl = error_mark_node;
6386 else
6388 if (cxx_dialect != cxx98)
6389 cp_parser_name_lookup_error
6390 (parser, token->u.value, decl, NLE_NOT_CXX98,
6391 token->location);
6392 else
6393 cp_parser_name_lookup_error
6394 (parser, token->u.value, decl, NLE_CXX98,
6395 token->location);
6398 parser->scope = error_mark_node;
6399 error_p = true;
6400 /* Treat this as a successful nested-name-specifier
6401 due to:
6403 [basic.lookup.qual]
6405 If the name found is not a class-name (clause
6406 _class_) or namespace-name (_namespace.def_), the
6407 program is ill-formed. */
6408 success = true;
6410 cp_lexer_consume_token (parser->lexer);
6412 break;
6414 /* We've found one valid nested-name-specifier. */
6415 success = true;
6416 /* Name lookup always gives us a DECL. */
6417 if (TREE_CODE (new_scope) == TYPE_DECL)
6418 new_scope = TREE_TYPE (new_scope);
6419 /* Uses of "template" must be followed by actual templates. */
6420 if (template_keyword_p
6421 && !(CLASS_TYPE_P (new_scope)
6422 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6424 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6425 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6426 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6427 == TEMPLATE_ID_EXPR)))
6428 permerror (input_location, TYPE_P (new_scope)
6429 ? G_("%qT is not a template")
6430 : G_("%qD is not a template"),
6431 new_scope);
6432 /* If it is a class scope, try to complete it; we are about to
6433 be looking up names inside the class. */
6434 if (TYPE_P (new_scope)
6435 /* Since checking types for dependency can be expensive,
6436 avoid doing it if the type is already complete. */
6437 && !COMPLETE_TYPE_P (new_scope)
6438 /* Do not try to complete dependent types. */
6439 && !dependent_type_p (new_scope))
6441 new_scope = complete_type (new_scope);
6442 /* If it is a typedef to current class, use the current
6443 class instead, as the typedef won't have any names inside
6444 it yet. */
6445 if (!COMPLETE_TYPE_P (new_scope)
6446 && currently_open_class (new_scope))
6447 new_scope = TYPE_MAIN_VARIANT (new_scope);
6449 /* Make sure we look in the right scope the next time through
6450 the loop. */
6451 parser->scope = new_scope;
6454 /* If parsing tentatively, replace the sequence of tokens that makes
6455 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456 token. That way, should we re-parse the token stream, we will
6457 not have to repeat the effort required to do the parse, nor will
6458 we issue duplicate error messages. */
6459 if (success && start)
6461 cp_token *token;
6463 token = cp_lexer_token_at (parser->lexer, start);
6464 /* Reset the contents of the START token. */
6465 token->type = CPP_NESTED_NAME_SPECIFIER;
6466 /* Retrieve any deferred checks. Do not pop this access checks yet
6467 so the memory will not be reclaimed during token replacing below. */
6468 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6469 token->u.tree_check_value->value = parser->scope;
6470 token->u.tree_check_value->checks = get_deferred_access_checks ();
6471 token->u.tree_check_value->qualifying_scope =
6472 parser->qualifying_scope;
6473 token->keyword = RID_MAX;
6475 /* Purge all subsequent tokens. */
6476 cp_lexer_purge_tokens_after (parser->lexer, start);
6479 if (start)
6480 pop_to_parent_deferring_access_checks ();
6482 return success ? parser->scope : NULL_TREE;
6485 /* Parse a nested-name-specifier. See
6486 cp_parser_nested_name_specifier_opt for details. This function
6487 behaves identically, except that it will an issue an error if no
6488 nested-name-specifier is present. */
6490 static tree
6491 cp_parser_nested_name_specifier (cp_parser *parser,
6492 bool typename_keyword_p,
6493 bool check_dependency_p,
6494 bool type_p,
6495 bool is_declaration)
6497 tree scope;
6499 /* Look for the nested-name-specifier. */
6500 scope = cp_parser_nested_name_specifier_opt (parser,
6501 typename_keyword_p,
6502 check_dependency_p,
6503 type_p,
6504 is_declaration);
6505 /* If it was not present, issue an error message. */
6506 if (!scope)
6508 cp_parser_error (parser, "expected nested-name-specifier");
6509 parser->scope = NULL_TREE;
6512 return scope;
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516 this is either a class-name or a namespace-name (which corresponds
6517 to the class-or-namespace-name production in the grammar). For
6518 C++0x, it can also be a type-name that refers to an enumeration
6519 type or a simple-template-id.
6521 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524 TYPE_P is TRUE iff the next name should be taken as a class-name,
6525 even the same name is declared to be another entity in the same
6526 scope.
6528 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529 specified by the class-or-namespace-name. If neither is found the
6530 ERROR_MARK_NODE is returned. */
6532 static tree
6533 cp_parser_qualifying_entity (cp_parser *parser,
6534 bool typename_keyword_p,
6535 bool template_keyword_p,
6536 bool check_dependency_p,
6537 bool type_p,
6538 bool is_declaration)
6540 tree saved_scope;
6541 tree saved_qualifying_scope;
6542 tree saved_object_scope;
6543 tree scope;
6544 bool only_class_p;
6545 bool successful_parse_p;
6547 /* DR 743: decltype can appear in a nested-name-specifier. */
6548 if (cp_lexer_next_token_is_decltype (parser->lexer))
6550 scope = cp_parser_decltype (parser);
6551 if (TREE_CODE (scope) != ENUMERAL_TYPE
6552 && !MAYBE_CLASS_TYPE_P (scope))
6554 cp_parser_simulate_error (parser);
6555 return error_mark_node;
6557 if (TYPE_NAME (scope))
6558 scope = TYPE_NAME (scope);
6559 return scope;
6562 /* Before we try to parse the class-name, we must save away the
6563 current PARSER->SCOPE since cp_parser_class_name will destroy
6564 it. */
6565 saved_scope = parser->scope;
6566 saved_qualifying_scope = parser->qualifying_scope;
6567 saved_object_scope = parser->object_scope;
6568 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6569 there is no need to look for a namespace-name. */
6570 only_class_p = template_keyword_p
6571 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6572 if (!only_class_p)
6573 cp_parser_parse_tentatively (parser);
6574 scope = cp_parser_class_name (parser,
6575 typename_keyword_p,
6576 template_keyword_p,
6577 type_p ? class_type : none_type,
6578 check_dependency_p,
6579 /*class_head_p=*/false,
6580 is_declaration,
6581 /*enum_ok=*/cxx_dialect > cxx98);
6582 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6583 /* If that didn't work, try for a namespace-name. */
6584 if (!only_class_p && !successful_parse_p)
6586 /* Restore the saved scope. */
6587 parser->scope = saved_scope;
6588 parser->qualifying_scope = saved_qualifying_scope;
6589 parser->object_scope = saved_object_scope;
6590 /* If we are not looking at an identifier followed by the scope
6591 resolution operator, then this is not part of a
6592 nested-name-specifier. (Note that this function is only used
6593 to parse the components of a nested-name-specifier.) */
6594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6595 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6596 return error_mark_node;
6597 scope = cp_parser_namespace_name (parser);
6600 return scope;
6603 /* Return true if we are looking at a compound-literal, false otherwise. */
6605 static bool
6606 cp_parser_compound_literal_p (cp_parser *parser)
6608 cp_lexer_save_tokens (parser->lexer);
6610 /* Skip tokens until the next token is a closing parenthesis.
6611 If we find the closing `)', and the next token is a `{', then
6612 we are looking at a compound-literal. */
6613 bool compound_literal_p
6614 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6615 /*consume_paren=*/true)
6616 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6618 /* Roll back the tokens we skipped. */
6619 cp_lexer_rollback_tokens (parser->lexer);
6621 return compound_literal_p;
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625 of zero, without any folding, but ignoring location wrappers. */
6627 static bool
6628 literal_integer_zerop (const_tree expr)
6630 STRIP_ANY_LOCATION_WRAPPER (expr);
6631 return integer_zerop (expr);
6634 /* Parse a postfix-expression.
6636 postfix-expression:
6637 primary-expression
6638 postfix-expression [ expression ]
6639 postfix-expression ( expression-list [opt] )
6640 simple-type-specifier ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier identifier
6642 ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier template [opt] template-id
6644 ( expression-list [opt] )
6645 postfix-expression . template [opt] id-expression
6646 postfix-expression -> template [opt] id-expression
6647 postfix-expression . pseudo-destructor-name
6648 postfix-expression -> pseudo-destructor-name
6649 postfix-expression ++
6650 postfix-expression --
6651 dynamic_cast < type-id > ( expression )
6652 static_cast < type-id > ( expression )
6653 reinterpret_cast < type-id > ( expression )
6654 const_cast < type-id > ( expression )
6655 typeid ( expression )
6656 typeid ( type-id )
6658 GNU Extension:
6660 postfix-expression:
6661 ( type-id ) { initializer-list , [opt] }
6663 This extension is a GNU version of the C99 compound-literal
6664 construct. (The C99 grammar uses `type-name' instead of `type-id',
6665 but they are essentially the same concept.)
6667 If ADDRESS_P is true, the postfix expression is the operand of the
6668 `&' operator. CAST_P is true if this expression is the target of a
6669 cast.
6671 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672 class member access expressions [expr.ref].
6674 Returns a representation of the expression. */
6676 static cp_expr
6677 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6678 bool member_access_only_p, bool decltype_p,
6679 cp_id_kind * pidk_return)
6681 cp_token *token;
6682 location_t loc;
6683 enum rid keyword;
6684 cp_id_kind idk = CP_ID_KIND_NONE;
6685 cp_expr postfix_expression = NULL_TREE;
6686 bool is_member_access = false;
6688 /* Peek at the next token. */
6689 token = cp_lexer_peek_token (parser->lexer);
6690 loc = token->location;
6691 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6693 /* Some of the productions are determined by keywords. */
6694 keyword = token->keyword;
6695 switch (keyword)
6697 case RID_DYNCAST:
6698 case RID_STATCAST:
6699 case RID_REINTCAST:
6700 case RID_CONSTCAST:
6702 tree type;
6703 cp_expr expression;
6704 const char *saved_message;
6705 bool saved_in_type_id_in_expr_p;
6707 /* All of these can be handled in the same way from the point
6708 of view of parsing. Begin by consuming the token
6709 identifying the cast. */
6710 cp_lexer_consume_token (parser->lexer);
6712 /* New types cannot be defined in the cast. */
6713 saved_message = parser->type_definition_forbidden_message;
6714 parser->type_definition_forbidden_message
6715 = G_("types may not be defined in casts");
6717 /* Look for the opening `<'. */
6718 cp_parser_require (parser, CPP_LESS, RT_LESS);
6719 /* Parse the type to which we are casting. */
6720 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6721 parser->in_type_id_in_expr_p = true;
6722 type = cp_parser_type_id (parser);
6723 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6724 /* Look for the closing `>'. */
6725 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6726 /* Restore the old message. */
6727 parser->type_definition_forbidden_message = saved_message;
6729 bool saved_greater_than_is_operator_p
6730 = parser->greater_than_is_operator_p;
6731 parser->greater_than_is_operator_p = true;
6733 /* And the expression which is being cast. */
6734 matching_parens parens;
6735 parens.require_open (parser);
6736 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6737 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6738 RT_CLOSE_PAREN);
6739 location_t end_loc = close_paren ?
6740 close_paren->location : UNKNOWN_LOCATION;
6742 parser->greater_than_is_operator_p
6743 = saved_greater_than_is_operator_p;
6745 /* Only type conversions to integral or enumeration types
6746 can be used in constant-expressions. */
6747 if (!cast_valid_in_integral_constant_expression_p (type)
6748 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6750 postfix_expression = error_mark_node;
6751 break;
6754 switch (keyword)
6756 case RID_DYNCAST:
6757 postfix_expression
6758 = build_dynamic_cast (type, expression, tf_warning_or_error);
6759 break;
6760 case RID_STATCAST:
6761 postfix_expression
6762 = build_static_cast (type, expression, tf_warning_or_error);
6763 break;
6764 case RID_REINTCAST:
6765 postfix_expression
6766 = build_reinterpret_cast (type, expression,
6767 tf_warning_or_error);
6768 break;
6769 case RID_CONSTCAST:
6770 postfix_expression
6771 = build_const_cast (type, expression, tf_warning_or_error);
6772 break;
6773 default:
6774 gcc_unreachable ();
6777 /* Construct a location e.g. :
6778 reinterpret_cast <int *> (expr)
6779 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 ranging from the start of the "*_cast" token to the final closing
6781 paren, with the caret at the start. */
6782 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6783 postfix_expression.set_location (cp_cast_loc);
6785 break;
6787 case RID_TYPEID:
6789 tree type;
6790 const char *saved_message;
6791 bool saved_in_type_id_in_expr_p;
6793 /* Consume the `typeid' token. */
6794 cp_lexer_consume_token (parser->lexer);
6795 /* Look for the `(' token. */
6796 matching_parens parens;
6797 parens.require_open (parser);
6798 /* Types cannot be defined in a `typeid' expression. */
6799 saved_message = parser->type_definition_forbidden_message;
6800 parser->type_definition_forbidden_message
6801 = G_("types may not be defined in a %<typeid%> expression");
6802 /* We can't be sure yet whether we're looking at a type-id or an
6803 expression. */
6804 cp_parser_parse_tentatively (parser);
6805 /* Try a type-id first. */
6806 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6807 parser->in_type_id_in_expr_p = true;
6808 type = cp_parser_type_id (parser);
6809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6810 /* Look for the `)' token. Otherwise, we can't be sure that
6811 we're not looking at an expression: consider `typeid (int
6812 (3))', for example. */
6813 cp_token *close_paren = parens.require_close (parser);
6814 /* If all went well, simply lookup the type-id. */
6815 if (cp_parser_parse_definitely (parser))
6816 postfix_expression = get_typeid (type, tf_warning_or_error);
6817 /* Otherwise, fall back to the expression variant. */
6818 else
6820 tree expression;
6822 /* Look for an expression. */
6823 expression = cp_parser_expression (parser, & idk);
6824 /* Compute its typeid. */
6825 postfix_expression = build_typeid (expression, tf_warning_or_error);
6826 /* Look for the `)' token. */
6827 close_paren = parens.require_close (parser);
6829 /* Restore the saved message. */
6830 parser->type_definition_forbidden_message = saved_message;
6831 /* `typeid' may not appear in an integral constant expression. */
6832 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6833 postfix_expression = error_mark_node;
6835 /* Construct a location e.g. :
6836 typeid (expr)
6837 ^~~~~~~~~~~~~
6838 ranging from the start of the "typeid" token to the final closing
6839 paren, with the caret at the start. */
6840 if (close_paren)
6842 location_t typeid_loc
6843 = make_location (start_loc, start_loc, close_paren->location);
6844 postfix_expression.set_location (typeid_loc);
6845 postfix_expression.maybe_add_location_wrapper ();
6848 break;
6850 case RID_TYPENAME:
6852 tree type;
6853 /* The syntax permitted here is the same permitted for an
6854 elaborated-type-specifier. */
6855 ++parser->prevent_constrained_type_specifiers;
6856 type = cp_parser_elaborated_type_specifier (parser,
6857 /*is_friend=*/false,
6858 /*is_declaration=*/false);
6859 --parser->prevent_constrained_type_specifiers;
6860 postfix_expression = cp_parser_functional_cast (parser, type);
6862 break;
6864 case RID_ADDRESSOF:
6865 case RID_BUILTIN_SHUFFLE:
6866 case RID_BUILTIN_LAUNDER:
6868 vec<tree, va_gc> *vec;
6869 unsigned int i;
6870 tree p;
6872 cp_lexer_consume_token (parser->lexer);
6873 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6874 /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 /*non_constant_p=*/NULL);
6876 if (vec == NULL)
6878 postfix_expression = error_mark_node;
6879 break;
6882 FOR_EACH_VEC_ELT (*vec, i, p)
6883 mark_exp_read (p);
6885 switch (keyword)
6887 case RID_ADDRESSOF:
6888 if (vec->length () == 1)
6889 postfix_expression
6890 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6891 else
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_addressof%>");
6895 postfix_expression = error_mark_node;
6897 break;
6899 case RID_BUILTIN_LAUNDER:
6900 if (vec->length () == 1)
6901 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6902 tf_warning_or_error);
6903 else
6905 error_at (loc, "wrong number of arguments to "
6906 "%<__builtin_launder%>");
6907 postfix_expression = error_mark_node;
6909 break;
6911 case RID_BUILTIN_SHUFFLE:
6912 if (vec->length () == 2)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6915 (*vec)[1], tf_warning_or_error);
6916 else if (vec->length () == 3)
6917 postfix_expression
6918 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6919 (*vec)[2], tf_warning_or_error);
6920 else
6922 error_at (loc, "wrong number of arguments to "
6923 "%<__builtin_shuffle%>");
6924 postfix_expression = error_mark_node;
6926 break;
6928 default:
6929 gcc_unreachable ();
6931 break;
6934 default:
6936 tree type;
6938 /* If the next thing is a simple-type-specifier, we may be
6939 looking at a functional cast. We could also be looking at
6940 an id-expression. So, we try the functional cast, and if
6941 that doesn't work we fall back to the primary-expression. */
6942 cp_parser_parse_tentatively (parser);
6943 /* Look for the simple-type-specifier. */
6944 ++parser->prevent_constrained_type_specifiers;
6945 type = cp_parser_simple_type_specifier (parser,
6946 /*decl_specs=*/NULL,
6947 CP_PARSER_FLAGS_NONE);
6948 --parser->prevent_constrained_type_specifiers;
6949 /* Parse the cast itself. */
6950 if (!cp_parser_error_occurred (parser))
6951 postfix_expression
6952 = cp_parser_functional_cast (parser, type);
6953 /* If that worked, we're done. */
6954 if (cp_parser_parse_definitely (parser))
6955 break;
6957 /* If the functional-cast didn't work out, try a
6958 compound-literal. */
6959 if (cp_parser_allow_gnu_extensions_p (parser)
6960 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6962 cp_expr initializer = NULL_TREE;
6964 cp_parser_parse_tentatively (parser);
6966 matching_parens parens;
6967 parens.consume_open (parser);
6969 /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 in cp_parser_cast_expression about c++/29234. */
6971 if (!cp_parser_compound_literal_p (parser))
6972 cp_parser_simulate_error (parser);
6973 else
6975 /* Parse the type. */
6976 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6977 parser->in_type_id_in_expr_p = true;
6978 type = cp_parser_type_id (parser);
6979 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6980 parens.require_close (parser);
6983 /* If things aren't going well, there's no need to
6984 keep going. */
6985 if (!cp_parser_error_occurred (parser))
6987 bool non_constant_p;
6988 /* Parse the brace-enclosed initializer list. */
6989 initializer = cp_parser_braced_list (parser,
6990 &non_constant_p);
6992 /* If that worked, we're definitely looking at a
6993 compound-literal expression. */
6994 if (cp_parser_parse_definitely (parser))
6996 /* Warn the user that a compound literal is not
6997 allowed in standard C++. */
6998 pedwarn (input_location, OPT_Wpedantic,
6999 "ISO C++ forbids compound-literals");
7000 /* For simplicity, we disallow compound literals in
7001 constant-expressions. We could
7002 allow compound literals of integer type, whose
7003 initializer was a constant, in constant
7004 expressions. Permitting that usage, as a further
7005 extension, would not change the meaning of any
7006 currently accepted programs. (Of course, as
7007 compound literals are not part of ISO C++, the
7008 standard has nothing to say.) */
7009 if (cp_parser_non_integral_constant_expression (parser,
7010 NIC_NCC))
7012 postfix_expression = error_mark_node;
7013 break;
7015 /* Form the representation of the compound-literal. */
7016 postfix_expression
7017 = finish_compound_literal (type, initializer,
7018 tf_warning_or_error, fcl_c99);
7019 postfix_expression.set_location (initializer.get_location ());
7020 break;
7024 /* It must be a primary-expression. */
7025 postfix_expression
7026 = cp_parser_primary_expression (parser, address_p, cast_p,
7027 /*template_arg_p=*/false,
7028 decltype_p,
7029 &idk);
7031 break;
7034 /* Note that we don't need to worry about calling build_cplus_new on a
7035 class-valued CALL_EXPR in decltype when it isn't the end of the
7036 postfix-expression; unary_complex_lvalue will take care of that for
7037 all these cases. */
7039 /* Keep looping until the postfix-expression is complete. */
7040 while (true)
7042 if (idk == CP_ID_KIND_UNQUALIFIED
7043 && identifier_p (postfix_expression)
7044 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7045 /* It is not a Koenig lookup function call. */
7046 postfix_expression
7047 = unqualified_name_lookup_error (postfix_expression);
7049 /* Peek at the next token. */
7050 token = cp_lexer_peek_token (parser->lexer);
7052 switch (token->type)
7054 case CPP_OPEN_SQUARE:
7055 if (cp_next_tokens_can_be_std_attribute_p (parser))
7057 cp_parser_error (parser,
7058 "two consecutive %<[%> shall "
7059 "only introduce an attribute");
7060 return error_mark_node;
7062 postfix_expression
7063 = cp_parser_postfix_open_square_expression (parser,
7064 postfix_expression,
7065 false,
7066 decltype_p);
7067 postfix_expression.set_range (start_loc,
7068 postfix_expression.get_location ());
7070 idk = CP_ID_KIND_NONE;
7071 is_member_access = false;
7072 break;
7074 case CPP_OPEN_PAREN:
7075 /* postfix-expression ( expression-list [opt] ) */
7077 bool koenig_p;
7078 bool is_builtin_constant_p;
7079 bool saved_integral_constant_expression_p = false;
7080 bool saved_non_integral_constant_expression_p = false;
7081 tsubst_flags_t complain = complain_flags (decltype_p);
7082 vec<tree, va_gc> *args;
7083 location_t close_paren_loc = UNKNOWN_LOCATION;
7085 is_member_access = false;
7087 is_builtin_constant_p
7088 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7089 if (is_builtin_constant_p)
7091 /* The whole point of __builtin_constant_p is to allow
7092 non-constant expressions to appear as arguments. */
7093 saved_integral_constant_expression_p
7094 = parser->integral_constant_expression_p;
7095 saved_non_integral_constant_expression_p
7096 = parser->non_integral_constant_expression_p;
7097 parser->integral_constant_expression_p = false;
7099 args = (cp_parser_parenthesized_expression_list
7100 (parser, non_attr,
7101 /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 /*non_constant_p=*/NULL,
7103 /*close_paren_loc=*/&close_paren_loc,
7104 /*wrap_locations_p=*/true));
7105 if (is_builtin_constant_p)
7107 parser->integral_constant_expression_p
7108 = saved_integral_constant_expression_p;
7109 parser->non_integral_constant_expression_p
7110 = saved_non_integral_constant_expression_p;
7113 if (args == NULL)
7115 postfix_expression = error_mark_node;
7116 break;
7119 /* Function calls are not permitted in
7120 constant-expressions. */
7121 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7122 && cp_parser_non_integral_constant_expression (parser,
7123 NIC_FUNC_CALL))
7125 postfix_expression = error_mark_node;
7126 release_tree_vector (args);
7127 break;
7130 koenig_p = false;
7131 if (idk == CP_ID_KIND_UNQUALIFIED
7132 || idk == CP_ID_KIND_TEMPLATE_ID)
7134 if (identifier_p (postfix_expression))
7136 if (!args->is_empty ())
7138 koenig_p = true;
7139 if (!any_type_dependent_arguments_p (args))
7140 postfix_expression
7141 = perform_koenig_lookup (postfix_expression, args,
7142 complain);
7144 else
7145 postfix_expression
7146 = unqualified_fn_lookup_error (postfix_expression);
7148 /* We do not perform argument-dependent lookup if
7149 normal lookup finds a non-function, in accordance
7150 with the expected resolution of DR 218. */
7151 else if (!args->is_empty ()
7152 && is_overloaded_fn (postfix_expression))
7154 tree fn = get_first_fn (postfix_expression);
7155 fn = STRIP_TEMPLATE (fn);
7157 /* Do not do argument dependent lookup if regular
7158 lookup finds a member function or a block-scope
7159 function declaration. [basic.lookup.argdep]/3 */
7160 if (!DECL_FUNCTION_MEMBER_P (fn)
7161 && !DECL_LOCAL_FUNCTION_P (fn))
7163 koenig_p = true;
7164 if (!any_type_dependent_arguments_p (args))
7165 postfix_expression
7166 = perform_koenig_lookup (postfix_expression, args,
7167 complain);
7172 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7173 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7175 && vec_safe_length (args) == 3)
7177 tree arg0 = (*args)[0];
7178 tree arg1 = (*args)[1];
7179 tree arg2 = (*args)[2];
7180 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7181 | (literal_integer_zerop (arg2) << 2));
7182 warn_for_memset (input_location, arg0, arg2, literal_mask);
7185 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7187 tree instance = TREE_OPERAND (postfix_expression, 0);
7188 tree fn = TREE_OPERAND (postfix_expression, 1);
7190 if (processing_template_decl
7191 && (type_dependent_object_expression_p (instance)
7192 || (!BASELINK_P (fn)
7193 && TREE_CODE (fn) != FIELD_DECL)
7194 || type_dependent_expression_p (fn)
7195 || any_type_dependent_arguments_p (args)))
7197 maybe_generic_this_capture (instance, fn);
7198 postfix_expression
7199 = build_min_nt_call_vec (postfix_expression, args);
7200 release_tree_vector (args);
7201 break;
7204 if (BASELINK_P (fn))
7206 postfix_expression
7207 = (build_new_method_call
7208 (instance, fn, &args, NULL_TREE,
7209 (idk == CP_ID_KIND_QUALIFIED
7210 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7211 : LOOKUP_NORMAL),
7212 /*fn_p=*/NULL,
7213 complain));
7215 else
7216 postfix_expression
7217 = finish_call_expr (postfix_expression, &args,
7218 /*disallow_virtual=*/false,
7219 /*koenig_p=*/false,
7220 complain);
7222 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7223 || TREE_CODE (postfix_expression) == MEMBER_REF
7224 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7225 postfix_expression = (build_offset_ref_call_from_tree
7226 (postfix_expression, &args,
7227 complain));
7228 else if (idk == CP_ID_KIND_QUALIFIED)
7229 /* A call to a static class member, or a namespace-scope
7230 function. */
7231 postfix_expression
7232 = finish_call_expr (postfix_expression, &args,
7233 /*disallow_virtual=*/true,
7234 koenig_p,
7235 complain);
7236 else
7237 /* All other function calls. */
7238 postfix_expression
7239 = finish_call_expr (postfix_expression, &args,
7240 /*disallow_virtual=*/false,
7241 koenig_p,
7242 complain);
7244 if (close_paren_loc != UNKNOWN_LOCATION)
7246 location_t combined_loc = make_location (token->location,
7247 start_loc,
7248 close_paren_loc);
7249 postfix_expression.set_location (combined_loc);
7252 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7253 idk = CP_ID_KIND_NONE;
7255 release_tree_vector (args);
7257 break;
7259 case CPP_DOT:
7260 case CPP_DEREF:
7261 /* postfix-expression . template [opt] id-expression
7262 postfix-expression . pseudo-destructor-name
7263 postfix-expression -> template [opt] id-expression
7264 postfix-expression -> pseudo-destructor-name */
7266 /* Consume the `.' or `->' operator. */
7267 cp_lexer_consume_token (parser->lexer);
7269 postfix_expression
7270 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7271 postfix_expression,
7272 false, &idk, loc);
7274 is_member_access = true;
7275 break;
7277 case CPP_PLUS_PLUS:
7278 /* postfix-expression ++ */
7279 /* Consume the `++' token. */
7280 cp_lexer_consume_token (parser->lexer);
7281 /* Generate a representation for the complete expression. */
7282 postfix_expression
7283 = finish_increment_expr (postfix_expression,
7284 POSTINCREMENT_EXPR);
7285 /* Increments may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7287 postfix_expression = error_mark_node;
7288 idk = CP_ID_KIND_NONE;
7289 is_member_access = false;
7290 break;
7292 case CPP_MINUS_MINUS:
7293 /* postfix-expression -- */
7294 /* Consume the `--' token. */
7295 cp_lexer_consume_token (parser->lexer);
7296 /* Generate a representation for the complete expression. */
7297 postfix_expression
7298 = finish_increment_expr (postfix_expression,
7299 POSTDECREMENT_EXPR);
7300 /* Decrements may not appear in constant-expressions. */
7301 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7302 postfix_expression = error_mark_node;
7303 idk = CP_ID_KIND_NONE;
7304 is_member_access = false;
7305 break;
7307 default:
7308 if (pidk_return != NULL)
7309 * pidk_return = idk;
7310 if (member_access_only_p)
7311 return is_member_access
7312 ? postfix_expression
7313 : cp_expr (error_mark_node);
7314 else
7315 return postfix_expression;
7319 /* We should never get here. */
7320 gcc_unreachable ();
7321 return error_mark_node;
7324 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7325 by cp_parser_builtin_offsetof. We're looking for
7327 postfix-expression [ expression ]
7328 postfix-expression [ braced-init-list ] (C++11)
7330 FOR_OFFSETOF is set if we're being called in that context, which
7331 changes how we deal with integer constant expressions. */
7333 static tree
7334 cp_parser_postfix_open_square_expression (cp_parser *parser,
7335 tree postfix_expression,
7336 bool for_offsetof,
7337 bool decltype_p)
7339 tree index = NULL_TREE;
7340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7341 bool saved_greater_than_is_operator_p;
7343 /* Consume the `[' token. */
7344 cp_lexer_consume_token (parser->lexer);
7346 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7347 parser->greater_than_is_operator_p = true;
7349 /* Parse the index expression. */
7350 /* ??? For offsetof, there is a question of what to allow here. If
7351 offsetof is not being used in an integral constant expression context,
7352 then we *could* get the right answer by computing the value at runtime.
7353 If we are in an integral constant expression context, then we might
7354 could accept any constant expression; hard to say without analysis.
7355 Rather than open the barn door too wide right away, allow only integer
7356 constant expressions here. */
7357 if (for_offsetof)
7358 index = cp_parser_constant_expression (parser);
7359 else
7361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7363 bool expr_nonconst_p;
7364 cp_lexer_set_source_position (parser->lexer);
7365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7366 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7368 else
7369 index = cp_parser_expression (parser);
7372 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7374 /* Look for the closing `]'. */
7375 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7377 /* Build the ARRAY_REF. */
7378 postfix_expression = grok_array_decl (loc, postfix_expression,
7379 index, decltype_p);
7381 /* When not doing offsetof, array references are not permitted in
7382 constant-expressions. */
7383 if (!for_offsetof
7384 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7385 postfix_expression = error_mark_node;
7387 return postfix_expression;
7390 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7391 by cp_parser_builtin_offsetof. We're looking for
7393 postfix-expression . template [opt] id-expression
7394 postfix-expression . pseudo-destructor-name
7395 postfix-expression -> template [opt] id-expression
7396 postfix-expression -> pseudo-destructor-name
7398 FOR_OFFSETOF is set if we're being called in that context. That sorta
7399 limits what of the above we'll actually accept, but nevermind.
7400 TOKEN_TYPE is the "." or "->" token, which will already have been
7401 removed from the stream. */
7403 static tree
7404 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7405 enum cpp_ttype token_type,
7406 cp_expr postfix_expression,
7407 bool for_offsetof, cp_id_kind *idk,
7408 location_t location)
7410 tree name;
7411 bool dependent_p;
7412 bool pseudo_destructor_p;
7413 tree scope = NULL_TREE;
7414 location_t start_loc = postfix_expression.get_start ();
7416 /* If this is a `->' operator, dereference the pointer. */
7417 if (token_type == CPP_DEREF)
7418 postfix_expression = build_x_arrow (location, postfix_expression,
7419 tf_warning_or_error);
7420 /* Check to see whether or not the expression is type-dependent and
7421 not the current instantiation. */
7422 dependent_p = type_dependent_object_expression_p (postfix_expression);
7423 /* The identifier following the `->' or `.' is not qualified. */
7424 parser->scope = NULL_TREE;
7425 parser->qualifying_scope = NULL_TREE;
7426 parser->object_scope = NULL_TREE;
7427 *idk = CP_ID_KIND_NONE;
7429 /* Enter the scope corresponding to the type of the object
7430 given by the POSTFIX_EXPRESSION. */
7431 if (!dependent_p)
7433 scope = TREE_TYPE (postfix_expression);
7434 /* According to the standard, no expression should ever have
7435 reference type. Unfortunately, we do not currently match
7436 the standard in this respect in that our internal representation
7437 of an expression may have reference type even when the standard
7438 says it does not. Therefore, we have to manually obtain the
7439 underlying type here. */
7440 scope = non_reference (scope);
7441 /* The type of the POSTFIX_EXPRESSION must be complete. */
7442 /* Unlike the object expression in other contexts, *this is not
7443 required to be of complete type for purposes of class member
7444 access (5.2.5) outside the member function body. */
7445 if (postfix_expression != current_class_ref
7446 && scope != error_mark_node
7447 && !(processing_template_decl
7448 && current_class_type
7449 && (same_type_ignoring_top_level_qualifiers_p
7450 (scope, current_class_type))))
7452 scope = complete_type (scope);
7453 if (!COMPLETE_TYPE_P (scope)
7454 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7455 && EXPR_P (postfix_expression))
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind = (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (scope)
7461 ? DK_PEDWARN
7462 : DK_ERROR);
7463 cxx_incomplete_type_diagnostic
7464 (location_of (postfix_expression),
7465 postfix_expression, scope, kind);
7466 if (!MAYBE_CLASS_TYPE_P (scope))
7467 return error_mark_node;
7468 if (processing_template_decl)
7470 dependent_p = true;
7471 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7476 if (!dependent_p)
7478 /* Let the name lookup machinery know that we are processing a
7479 class member access expression. */
7480 parser->context->object_type = scope;
7481 /* If something went wrong, we want to be able to discern that case,
7482 as opposed to the case where there was no SCOPE due to the type
7483 of expression being dependent. */
7484 if (!scope)
7485 scope = error_mark_node;
7486 /* If the SCOPE was erroneous, make the various semantic analysis
7487 functions exit quickly -- and without issuing additional error
7488 messages. */
7489 if (scope == error_mark_node)
7490 postfix_expression = error_mark_node;
7494 if (dependent_p)
7495 /* Tell cp_parser_lookup_name that there was an object, even though it's
7496 type-dependent. */
7497 parser->context->object_type = unknown_type_node;
7499 /* Assume this expression is not a pseudo-destructor access. */
7500 pseudo_destructor_p = false;
7502 /* If the SCOPE is a scalar type, then, if this is a valid program,
7503 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7504 is type dependent, it can be pseudo-destructor-name or something else.
7505 Try to parse it as pseudo-destructor-name first. */
7506 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7508 tree s;
7509 tree type;
7511 cp_parser_parse_tentatively (parser);
7512 /* Parse the pseudo-destructor-name. */
7513 s = NULL_TREE;
7514 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7515 &s, &type);
7516 if (dependent_p
7517 && (cp_parser_error_occurred (parser)
7518 || !SCALAR_TYPE_P (type)))
7519 cp_parser_abort_tentative_parse (parser);
7520 else if (cp_parser_parse_definitely (parser))
7522 pseudo_destructor_p = true;
7523 postfix_expression
7524 = finish_pseudo_destructor_expr (postfix_expression,
7525 s, type, location);
7529 if (!pseudo_destructor_p)
7531 /* If the SCOPE is not a scalar type, we are looking at an
7532 ordinary class member access expression, rather than a
7533 pseudo-destructor-name. */
7534 bool template_p;
7535 cp_token *token = cp_lexer_peek_token (parser->lexer);
7536 /* Parse the id-expression. */
7537 name = (cp_parser_id_expression
7538 (parser,
7539 cp_parser_optional_template_keyword (parser),
7540 /*check_dependency_p=*/true,
7541 &template_p,
7542 /*declarator_p=*/false,
7543 /*optional_p=*/false));
7544 /* In general, build a SCOPE_REF if the member name is qualified.
7545 However, if the name was not dependent and has already been
7546 resolved; there is no need to build the SCOPE_REF. For example;
7548 struct X { void f(); };
7549 template <typename T> void f(T* t) { t->X::f(); }
7551 Even though "t" is dependent, "X::f" is not and has been resolved
7552 to a BASELINK; there is no need to include scope information. */
7554 /* But we do need to remember that there was an explicit scope for
7555 virtual function calls. */
7556 if (parser->scope)
7557 *idk = CP_ID_KIND_QUALIFIED;
7559 /* If the name is a template-id that names a type, we will get a
7560 TYPE_DECL here. That is invalid code. */
7561 if (TREE_CODE (name) == TYPE_DECL)
7563 error_at (token->location, "invalid use of %qD", name);
7564 postfix_expression = error_mark_node;
7566 else
7568 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7570 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7572 error_at (token->location, "%<%D::%D%> is not a class member",
7573 parser->scope, name);
7574 postfix_expression = error_mark_node;
7576 else
7577 name = build_qualified_name (/*type=*/NULL_TREE,
7578 parser->scope,
7579 name,
7580 template_p);
7581 parser->scope = NULL_TREE;
7582 parser->qualifying_scope = NULL_TREE;
7583 parser->object_scope = NULL_TREE;
7585 if (parser->scope && name && BASELINK_P (name))
7586 adjust_result_of_qualified_name_lookup
7587 (name, parser->scope, scope);
7588 postfix_expression
7589 = finish_class_member_access_expr (postfix_expression, name,
7590 template_p,
7591 tf_warning_or_error);
7592 /* Build a location e.g.:
7593 ptr->access_expr
7594 ~~~^~~~~~~~~~~~~
7595 where the caret is at the deref token, ranging from
7596 the start of postfix_expression to the end of the access expr. */
7597 location_t end_loc
7598 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7599 location_t combined_loc
7600 = make_location (input_location, start_loc, end_loc);
7601 protected_set_expr_location (postfix_expression, combined_loc);
7605 /* We no longer need to look up names in the scope of the object on
7606 the left-hand side of the `.' or `->' operator. */
7607 parser->context->object_type = NULL_TREE;
7609 /* Outside of offsetof, these operators may not appear in
7610 constant-expressions. */
7611 if (!for_offsetof
7612 && (cp_parser_non_integral_constant_expression
7613 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7614 postfix_expression = error_mark_node;
7616 return postfix_expression;
7619 /* Parse a parenthesized expression-list.
7621 expression-list:
7622 assignment-expression
7623 expression-list, assignment-expression
7625 attribute-list:
7626 expression-list
7627 identifier
7628 identifier, expression-list
7630 CAST_P is true if this expression is the target of a cast.
7632 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7633 argument pack.
7635 WRAP_LOCATIONS_P is true if expressions within this list for which
7636 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7637 their source locations.
7639 Returns a vector of trees. Each element is a representation of an
7640 assignment-expression. NULL is returned if the ( and or ) are
7641 missing. An empty, but allocated, vector is returned on no
7642 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7643 if we are parsing an attribute list for an attribute that wants a
7644 plain identifier argument, normal_attr for an attribute that wants
7645 an expression, or non_attr if we aren't parsing an attribute list. If
7646 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7647 not all of the expressions in the list were constant.
7648 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7649 will be written to with the location of the closing parenthesis. If
7650 an error occurs, it may or may not be written to. */
7652 static vec<tree, va_gc> *
7653 cp_parser_parenthesized_expression_list (cp_parser* parser,
7654 int is_attribute_list,
7655 bool cast_p,
7656 bool allow_expansion_p,
7657 bool *non_constant_p,
7658 location_t *close_paren_loc,
7659 bool wrap_locations_p)
7661 vec<tree, va_gc> *expression_list;
7662 bool fold_expr_p = is_attribute_list != non_attr;
7663 tree identifier = NULL_TREE;
7664 bool saved_greater_than_is_operator_p;
7666 /* Assume all the expressions will be constant. */
7667 if (non_constant_p)
7668 *non_constant_p = false;
7670 matching_parens parens;
7671 if (!parens.require_open (parser))
7672 return NULL;
7674 expression_list = make_tree_vector ();
7676 /* Within a parenthesized expression, a `>' token is always
7677 the greater-than operator. */
7678 saved_greater_than_is_operator_p
7679 = parser->greater_than_is_operator_p;
7680 parser->greater_than_is_operator_p = true;
7682 cp_expr expr (NULL_TREE);
7684 /* Consume expressions until there are no more. */
7685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7686 while (true)
7688 /* At the beginning of attribute lists, check to see if the
7689 next token is an identifier. */
7690 if (is_attribute_list == id_attr
7691 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7693 cp_token *token;
7695 /* Consume the identifier. */
7696 token = cp_lexer_consume_token (parser->lexer);
7697 /* Save the identifier. */
7698 identifier = token->u.value;
7700 else
7702 bool expr_non_constant_p;
7704 /* Parse the next assignment-expression. */
7705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7707 /* A braced-init-list. */
7708 cp_lexer_set_source_position (parser->lexer);
7709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7710 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7711 if (non_constant_p && expr_non_constant_p)
7712 *non_constant_p = true;
7714 else if (non_constant_p)
7716 expr = (cp_parser_constant_expression
7717 (parser, /*allow_non_constant_p=*/true,
7718 &expr_non_constant_p));
7719 if (expr_non_constant_p)
7720 *non_constant_p = true;
7722 else
7723 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7724 cast_p);
7726 if (fold_expr_p)
7727 expr = instantiate_non_dependent_expr (expr);
7729 /* If we have an ellipsis, then this is an expression
7730 expansion. */
7731 if (allow_expansion_p
7732 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7734 /* Consume the `...'. */
7735 cp_lexer_consume_token (parser->lexer);
7737 /* Build the argument pack. */
7738 expr = make_pack_expansion (expr);
7741 if (wrap_locations_p)
7742 expr.maybe_add_location_wrapper ();
7744 /* Add it to the list. We add error_mark_node
7745 expressions to the list, so that we can still tell if
7746 the correct form for a parenthesized expression-list
7747 is found. That gives better errors. */
7748 vec_safe_push (expression_list, expr.get_value ());
7750 if (expr == error_mark_node)
7751 goto skip_comma;
7754 /* After the first item, attribute lists look the same as
7755 expression lists. */
7756 is_attribute_list = non_attr;
7758 get_comma:;
7759 /* If the next token isn't a `,', then we are done. */
7760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7761 break;
7763 /* Otherwise, consume the `,' and keep going. */
7764 cp_lexer_consume_token (parser->lexer);
7767 if (close_paren_loc)
7768 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7770 if (!parens.require_close (parser))
7772 int ending;
7774 skip_comma:;
7775 /* We try and resync to an unnested comma, as that will give the
7776 user better diagnostics. */
7777 ending = cp_parser_skip_to_closing_parenthesis (parser,
7778 /*recovering=*/true,
7779 /*or_comma=*/true,
7780 /*consume_paren=*/true);
7781 if (ending < 0)
7782 goto get_comma;
7783 if (!ending)
7785 parser->greater_than_is_operator_p
7786 = saved_greater_than_is_operator_p;
7787 return NULL;
7791 parser->greater_than_is_operator_p
7792 = saved_greater_than_is_operator_p;
7794 if (identifier)
7795 vec_safe_insert (expression_list, 0, identifier);
7797 return expression_list;
7800 /* Parse a pseudo-destructor-name.
7802 pseudo-destructor-name:
7803 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7804 :: [opt] nested-name-specifier template template-id :: ~ type-name
7805 :: [opt] nested-name-specifier [opt] ~ type-name
7807 If either of the first two productions is used, sets *SCOPE to the
7808 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7809 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7810 or ERROR_MARK_NODE if the parse fails. */
7812 static void
7813 cp_parser_pseudo_destructor_name (cp_parser* parser,
7814 tree object,
7815 tree* scope,
7816 tree* type)
7818 bool nested_name_specifier_p;
7820 /* Handle ~auto. */
7821 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7822 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7823 && !type_dependent_expression_p (object))
7825 if (cxx_dialect < cxx14)
7826 pedwarn (input_location, 0,
7827 "%<~auto%> only available with "
7828 "-std=c++14 or -std=gnu++14");
7829 cp_lexer_consume_token (parser->lexer);
7830 cp_lexer_consume_token (parser->lexer);
7831 *scope = NULL_TREE;
7832 *type = TREE_TYPE (object);
7833 return;
7836 /* Assume that things will not work out. */
7837 *type = error_mark_node;
7839 /* Look for the optional `::' operator. */
7840 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7841 /* Look for the optional nested-name-specifier. */
7842 nested_name_specifier_p
7843 = (cp_parser_nested_name_specifier_opt (parser,
7844 /*typename_keyword_p=*/false,
7845 /*check_dependency_p=*/true,
7846 /*type_p=*/false,
7847 /*is_declaration=*/false)
7848 != NULL_TREE);
7849 /* Now, if we saw a nested-name-specifier, we might be doing the
7850 second production. */
7851 if (nested_name_specifier_p
7852 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7854 /* Consume the `template' keyword. */
7855 cp_lexer_consume_token (parser->lexer);
7856 /* Parse the template-id. */
7857 cp_parser_template_id (parser,
7858 /*template_keyword_p=*/true,
7859 /*check_dependency_p=*/false,
7860 class_type,
7861 /*is_declaration=*/true);
7862 /* Look for the `::' token. */
7863 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7865 /* If the next token is not a `~', then there might be some
7866 additional qualification. */
7867 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7869 /* At this point, we're looking for "type-name :: ~". The type-name
7870 must not be a class-name, since this is a pseudo-destructor. So,
7871 it must be either an enum-name, or a typedef-name -- both of which
7872 are just identifiers. So, we peek ahead to check that the "::"
7873 and "~" tokens are present; if they are not, then we can avoid
7874 calling type_name. */
7875 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7876 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7877 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7879 cp_parser_error (parser, "non-scalar type");
7880 return;
7883 /* Look for the type-name. */
7884 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7885 if (*scope == error_mark_node)
7886 return;
7888 /* Look for the `::' token. */
7889 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7891 else
7892 *scope = NULL_TREE;
7894 /* Look for the `~'. */
7895 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7897 /* Once we see the ~, this has to be a pseudo-destructor. */
7898 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7899 cp_parser_commit_to_topmost_tentative_parse (parser);
7901 /* Look for the type-name again. We are not responsible for
7902 checking that it matches the first type-name. */
7903 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7906 /* Parse a unary-expression.
7908 unary-expression:
7909 postfix-expression
7910 ++ cast-expression
7911 -- cast-expression
7912 unary-operator cast-expression
7913 sizeof unary-expression
7914 sizeof ( type-id )
7915 alignof ( type-id ) [C++0x]
7916 new-expression
7917 delete-expression
7919 GNU Extensions:
7921 unary-expression:
7922 __extension__ cast-expression
7923 __alignof__ unary-expression
7924 __alignof__ ( type-id )
7925 alignof unary-expression [C++0x]
7926 __real__ cast-expression
7927 __imag__ cast-expression
7928 && identifier
7929 sizeof ( type-id ) { initializer-list , [opt] }
7930 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7931 __alignof__ ( type-id ) { initializer-list , [opt] }
7933 ADDRESS_P is true iff the unary-expression is appearing as the
7934 operand of the `&' operator. CAST_P is true if this expression is
7935 the target of a cast.
7937 Returns a representation of the expression. */
7939 static cp_expr
7940 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7941 bool address_p, bool cast_p, bool decltype_p)
7943 cp_token *token;
7944 enum tree_code unary_operator;
7946 /* Peek at the next token. */
7947 token = cp_lexer_peek_token (parser->lexer);
7948 /* Some keywords give away the kind of expression. */
7949 if (token->type == CPP_KEYWORD)
7951 enum rid keyword = token->keyword;
7953 switch (keyword)
7955 case RID_ALIGNOF:
7956 case RID_SIZEOF:
7958 tree operand, ret;
7959 enum tree_code op;
7960 location_t start_loc = token->location;
7962 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7963 /* Consume the token. */
7964 cp_lexer_consume_token (parser->lexer);
7965 /* Parse the operand. */
7966 operand = cp_parser_sizeof_operand (parser, keyword);
7968 if (TYPE_P (operand))
7969 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7970 else
7972 /* ISO C++ defines alignof only with types, not with
7973 expressions. So pedwarn if alignof is used with a non-
7974 type expression. However, __alignof__ is ok. */
7975 if (id_equal (token->u.value, "alignof"))
7976 pedwarn (token->location, OPT_Wpedantic,
7977 "ISO C++ does not allow %<alignof%> "
7978 "with a non-type");
7980 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7982 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7983 SIZEOF_EXPR with the original operand. */
7984 if (op == SIZEOF_EXPR && ret != error_mark_node)
7986 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7988 if (!processing_template_decl && TYPE_P (operand))
7990 ret = build_min (SIZEOF_EXPR, size_type_node,
7991 build1 (NOP_EXPR, operand,
7992 error_mark_node));
7993 SIZEOF_EXPR_TYPE_P (ret) = 1;
7995 else
7996 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7997 TREE_SIDE_EFFECTS (ret) = 0;
7998 TREE_READONLY (ret) = 1;
8002 /* Construct a location e.g. :
8003 alignof (expr)
8004 ^~~~~~~~~~~~~~
8005 with start == caret at the start of the "alignof"/"sizeof"
8006 token, with the endpoint at the final closing paren. */
8007 location_t finish_loc
8008 = cp_lexer_previous_token (parser->lexer)->location;
8009 location_t compound_loc
8010 = make_location (start_loc, start_loc, finish_loc);
8012 cp_expr ret_expr (ret);
8013 ret_expr.set_location (compound_loc);
8014 ret_expr = ret_expr.maybe_add_location_wrapper ();
8015 return ret_expr;
8018 case RID_NEW:
8019 return cp_parser_new_expression (parser);
8021 case RID_DELETE:
8022 return cp_parser_delete_expression (parser);
8024 case RID_EXTENSION:
8026 /* The saved value of the PEDANTIC flag. */
8027 int saved_pedantic;
8028 tree expr;
8030 /* Save away the PEDANTIC flag. */
8031 cp_parser_extension_opt (parser, &saved_pedantic);
8032 /* Parse the cast-expression. */
8033 expr = cp_parser_simple_cast_expression (parser);
8034 /* Restore the PEDANTIC flag. */
8035 pedantic = saved_pedantic;
8037 return expr;
8040 case RID_REALPART:
8041 case RID_IMAGPART:
8043 tree expression;
8045 /* Consume the `__real__' or `__imag__' token. */
8046 cp_lexer_consume_token (parser->lexer);
8047 /* Parse the cast-expression. */
8048 expression = cp_parser_simple_cast_expression (parser);
8049 /* Create the complete representation. */
8050 return build_x_unary_op (token->location,
8051 (keyword == RID_REALPART
8052 ? REALPART_EXPR : IMAGPART_EXPR),
8053 expression,
8054 tf_warning_or_error);
8056 break;
8058 case RID_TRANSACTION_ATOMIC:
8059 case RID_TRANSACTION_RELAXED:
8060 return cp_parser_transaction_expression (parser, keyword);
8062 case RID_NOEXCEPT:
8064 tree expr;
8065 const char *saved_message;
8066 bool saved_integral_constant_expression_p;
8067 bool saved_non_integral_constant_expression_p;
8068 bool saved_greater_than_is_operator_p;
8070 location_t start_loc = token->location;
8072 cp_lexer_consume_token (parser->lexer);
8073 matching_parens parens;
8074 parens.require_open (parser);
8076 saved_message = parser->type_definition_forbidden_message;
8077 parser->type_definition_forbidden_message
8078 = G_("types may not be defined in %<noexcept%> expressions");
8080 saved_integral_constant_expression_p
8081 = parser->integral_constant_expression_p;
8082 saved_non_integral_constant_expression_p
8083 = parser->non_integral_constant_expression_p;
8084 parser->integral_constant_expression_p = false;
8086 saved_greater_than_is_operator_p
8087 = parser->greater_than_is_operator_p;
8088 parser->greater_than_is_operator_p = true;
8090 ++cp_unevaluated_operand;
8091 ++c_inhibit_evaluation_warnings;
8092 ++cp_noexcept_operand;
8093 expr = cp_parser_expression (parser);
8094 --cp_noexcept_operand;
8095 --c_inhibit_evaluation_warnings;
8096 --cp_unevaluated_operand;
8098 parser->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p;
8101 parser->integral_constant_expression_p
8102 = saved_integral_constant_expression_p;
8103 parser->non_integral_constant_expression_p
8104 = saved_non_integral_constant_expression_p;
8106 parser->type_definition_forbidden_message = saved_message;
8108 location_t finish_loc
8109 = cp_lexer_peek_token (parser->lexer)->location;
8110 parens.require_close (parser);
8112 /* Construct a location of the form:
8113 noexcept (expr)
8114 ^~~~~~~~~~~~~~~
8115 with start == caret, finishing at the close-paren. */
8116 location_t noexcept_loc
8117 = make_location (start_loc, start_loc, finish_loc);
8119 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8120 noexcept_loc);
8123 default:
8124 break;
8128 /* Look for the `:: new' and `:: delete', which also signal the
8129 beginning of a new-expression, or delete-expression,
8130 respectively. If the next token is `::', then it might be one of
8131 these. */
8132 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8134 enum rid keyword;
8136 /* See if the token after the `::' is one of the keywords in
8137 which we're interested. */
8138 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8139 /* If it's `new', we have a new-expression. */
8140 if (keyword == RID_NEW)
8141 return cp_parser_new_expression (parser);
8142 /* Similarly, for `delete'. */
8143 else if (keyword == RID_DELETE)
8144 return cp_parser_delete_expression (parser);
8147 /* Look for a unary operator. */
8148 unary_operator = cp_parser_unary_operator (token);
8149 /* The `++' and `--' operators can be handled similarly, even though
8150 they are not technically unary-operators in the grammar. */
8151 if (unary_operator == ERROR_MARK)
8153 if (token->type == CPP_PLUS_PLUS)
8154 unary_operator = PREINCREMENT_EXPR;
8155 else if (token->type == CPP_MINUS_MINUS)
8156 unary_operator = PREDECREMENT_EXPR;
8157 /* Handle the GNU address-of-label extension. */
8158 else if (cp_parser_allow_gnu_extensions_p (parser)
8159 && token->type == CPP_AND_AND)
8161 tree identifier;
8162 tree expression;
8163 location_t start_loc = token->location;
8165 /* Consume the '&&' token. */
8166 cp_lexer_consume_token (parser->lexer);
8167 /* Look for the identifier. */
8168 location_t finish_loc
8169 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8170 identifier = cp_parser_identifier (parser);
8171 /* Construct a location of the form:
8172 &&label
8173 ^~~~~~~
8174 with caret==start at the "&&", finish at the end of the label. */
8175 location_t combined_loc
8176 = make_location (start_loc, start_loc, finish_loc);
8177 /* Create an expression representing the address. */
8178 expression = finish_label_address_expr (identifier, combined_loc);
8179 if (cp_parser_non_integral_constant_expression (parser,
8180 NIC_ADDR_LABEL))
8181 expression = error_mark_node;
8182 return expression;
8185 if (unary_operator != ERROR_MARK)
8187 cp_expr cast_expression;
8188 cp_expr expression = error_mark_node;
8189 non_integral_constant non_constant_p = NIC_NONE;
8190 location_t loc = token->location;
8191 tsubst_flags_t complain = complain_flags (decltype_p);
8193 /* Consume the operator token. */
8194 token = cp_lexer_consume_token (parser->lexer);
8195 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8197 /* Parse the cast-expression. */
8198 cast_expression
8199 = cp_parser_cast_expression (parser,
8200 unary_operator == ADDR_EXPR,
8201 /*cast_p=*/false,
8202 /*decltype*/false,
8203 pidk);
8205 /* Make a location:
8206 OP_TOKEN CAST_EXPRESSION
8207 ^~~~~~~~~~~~~~~~~~~~~~~~~
8208 with start==caret at the operator token, and
8209 extending to the end of the cast_expression. */
8210 loc = make_location (loc, loc, cast_expression.get_finish ());
8212 /* Now, build an appropriate representation. */
8213 switch (unary_operator)
8215 case INDIRECT_REF:
8216 non_constant_p = NIC_STAR;
8217 expression = build_x_indirect_ref (loc, cast_expression,
8218 RO_UNARY_STAR,
8219 complain);
8220 /* TODO: build_x_indirect_ref does not always honor the
8221 location, so ensure it is set. */
8222 expression.set_location (loc);
8223 break;
8225 case ADDR_EXPR:
8226 non_constant_p = NIC_ADDR;
8227 /* Fall through. */
8228 case BIT_NOT_EXPR:
8229 expression = build_x_unary_op (loc, unary_operator,
8230 cast_expression,
8231 complain);
8232 /* TODO: build_x_unary_op does not always honor the location,
8233 so ensure it is set. */
8234 expression.set_location (loc);
8235 break;
8237 case PREINCREMENT_EXPR:
8238 case PREDECREMENT_EXPR:
8239 non_constant_p = unary_operator == PREINCREMENT_EXPR
8240 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8241 /* Fall through. */
8242 case NEGATE_EXPR:
8243 /* Immediately fold negation of a constant, unless the constant is 0
8244 (since -0 == 0) or it would overflow. */
8245 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8246 && CONSTANT_CLASS_P (cast_expression)
8247 && !integer_zerop (cast_expression)
8248 && !TREE_OVERFLOW (cast_expression))
8250 tree folded = fold_build1 (unary_operator,
8251 TREE_TYPE (cast_expression),
8252 cast_expression);
8253 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8255 expression = cp_expr (folded, loc);
8256 break;
8259 /* Fall through. */
8260 case UNARY_PLUS_EXPR:
8261 case TRUTH_NOT_EXPR:
8262 expression = finish_unary_op_expr (loc, unary_operator,
8263 cast_expression, complain);
8264 break;
8266 default:
8267 gcc_unreachable ();
8270 if (non_constant_p != NIC_NONE
8271 && cp_parser_non_integral_constant_expression (parser,
8272 non_constant_p))
8273 expression = error_mark_node;
8275 return expression;
8278 return cp_parser_postfix_expression (parser, address_p, cast_p,
8279 /*member_access_only_p=*/false,
8280 decltype_p,
8281 pidk);
8284 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8285 unary-operator, the corresponding tree code is returned. */
8287 static enum tree_code
8288 cp_parser_unary_operator (cp_token* token)
8290 switch (token->type)
8292 case CPP_MULT:
8293 return INDIRECT_REF;
8295 case CPP_AND:
8296 return ADDR_EXPR;
8298 case CPP_PLUS:
8299 return UNARY_PLUS_EXPR;
8301 case CPP_MINUS:
8302 return NEGATE_EXPR;
8304 case CPP_NOT:
8305 return TRUTH_NOT_EXPR;
8307 case CPP_COMPL:
8308 return BIT_NOT_EXPR;
8310 default:
8311 return ERROR_MARK;
8315 /* Parse a new-expression.
8317 new-expression:
8318 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8319 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8321 Returns a representation of the expression. */
8323 static tree
8324 cp_parser_new_expression (cp_parser* parser)
8326 bool global_scope_p;
8327 vec<tree, va_gc> *placement;
8328 tree type;
8329 vec<tree, va_gc> *initializer;
8330 tree nelts = NULL_TREE;
8331 tree ret;
8333 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8335 /* Look for the optional `::' operator. */
8336 global_scope_p
8337 = (cp_parser_global_scope_opt (parser,
8338 /*current_scope_valid_p=*/false)
8339 != NULL_TREE);
8340 /* Look for the `new' operator. */
8341 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8342 /* There's no easy way to tell a new-placement from the
8343 `( type-id )' construct. */
8344 cp_parser_parse_tentatively (parser);
8345 /* Look for a new-placement. */
8346 placement = cp_parser_new_placement (parser);
8347 /* If that didn't work out, there's no new-placement. */
8348 if (!cp_parser_parse_definitely (parser))
8350 if (placement != NULL)
8351 release_tree_vector (placement);
8352 placement = NULL;
8355 /* If the next token is a `(', then we have a parenthesized
8356 type-id. */
8357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8359 cp_token *token;
8360 const char *saved_message = parser->type_definition_forbidden_message;
8362 /* Consume the `('. */
8363 matching_parens parens;
8364 parens.consume_open (parser);
8366 /* Parse the type-id. */
8367 parser->type_definition_forbidden_message
8368 = G_("types may not be defined in a new-expression");
8370 type_id_in_expr_sentinel s (parser);
8371 type = cp_parser_type_id (parser);
8373 parser->type_definition_forbidden_message = saved_message;
8375 /* Look for the closing `)'. */
8376 parens.require_close (parser);
8377 token = cp_lexer_peek_token (parser->lexer);
8378 /* There should not be a direct-new-declarator in this production,
8379 but GCC used to allowed this, so we check and emit a sensible error
8380 message for this case. */
8381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8383 error_at (token->location,
8384 "array bound forbidden after parenthesized type-id");
8385 inform (token->location,
8386 "try removing the parentheses around the type-id");
8387 cp_parser_direct_new_declarator (parser);
8390 /* Otherwise, there must be a new-type-id. */
8391 else
8392 type = cp_parser_new_type_id (parser, &nelts);
8394 /* If the next token is a `(' or '{', then we have a new-initializer. */
8395 cp_token *token = cp_lexer_peek_token (parser->lexer);
8396 if (token->type == CPP_OPEN_PAREN
8397 || token->type == CPP_OPEN_BRACE)
8398 initializer = cp_parser_new_initializer (parser);
8399 else
8400 initializer = NULL;
8402 /* A new-expression may not appear in an integral constant
8403 expression. */
8404 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8405 ret = error_mark_node;
8406 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8407 of a new-type-id or type-id of a new-expression, the new-expression shall
8408 contain a new-initializer of the form ( assignment-expression )".
8409 Additionally, consistently with the spirit of DR 1467, we want to accept
8410 'new auto { 2 }' too. */
8411 else if ((ret = type_uses_auto (type))
8412 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8413 && (vec_safe_length (initializer) != 1
8414 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8415 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8417 error_at (token->location,
8418 "initialization of new-expression for type %<auto%> "
8419 "requires exactly one element");
8420 ret = error_mark_node;
8422 else
8424 /* Construct a location e.g.:
8425 ptr = new int[100]
8426 ^~~~~~~~~~~~
8427 with caret == start at the start of the "new" token, and the end
8428 at the end of the final token we consumed. */
8429 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8430 location_t end_loc = get_finish (end_tok->location);
8431 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8433 /* Create a representation of the new-expression. */
8434 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8435 tf_warning_or_error);
8436 protected_set_expr_location (ret, combined_loc);
8439 if (placement != NULL)
8440 release_tree_vector (placement);
8441 if (initializer != NULL)
8442 release_tree_vector (initializer);
8444 return ret;
8447 /* Parse a new-placement.
8449 new-placement:
8450 ( expression-list )
8452 Returns the same representation as for an expression-list. */
8454 static vec<tree, va_gc> *
8455 cp_parser_new_placement (cp_parser* parser)
8457 vec<tree, va_gc> *expression_list;
8459 /* Parse the expression-list. */
8460 expression_list = (cp_parser_parenthesized_expression_list
8461 (parser, non_attr, /*cast_p=*/false,
8462 /*allow_expansion_p=*/true,
8463 /*non_constant_p=*/NULL));
8465 if (expression_list && expression_list->is_empty ())
8466 error ("expected expression-list or type-id");
8468 return expression_list;
8471 /* Parse a new-type-id.
8473 new-type-id:
8474 type-specifier-seq new-declarator [opt]
8476 Returns the TYPE allocated. If the new-type-id indicates an array
8477 type, *NELTS is set to the number of elements in the last array
8478 bound; the TYPE will not include the last array bound. */
8480 static tree
8481 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8483 cp_decl_specifier_seq type_specifier_seq;
8484 cp_declarator *new_declarator;
8485 cp_declarator *declarator;
8486 cp_declarator *outer_declarator;
8487 const char *saved_message;
8489 /* The type-specifier sequence must not contain type definitions.
8490 (It cannot contain declarations of new types either, but if they
8491 are not definitions we will catch that because they are not
8492 complete.) */
8493 saved_message = parser->type_definition_forbidden_message;
8494 parser->type_definition_forbidden_message
8495 = G_("types may not be defined in a new-type-id");
8496 /* Parse the type-specifier-seq. */
8497 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8498 /*is_trailing_return=*/false,
8499 &type_specifier_seq);
8500 /* Restore the old message. */
8501 parser->type_definition_forbidden_message = saved_message;
8503 if (type_specifier_seq.type == error_mark_node)
8504 return error_mark_node;
8506 /* Parse the new-declarator. */
8507 new_declarator = cp_parser_new_declarator_opt (parser);
8509 /* Determine the number of elements in the last array dimension, if
8510 any. */
8511 *nelts = NULL_TREE;
8512 /* Skip down to the last array dimension. */
8513 declarator = new_declarator;
8514 outer_declarator = NULL;
8515 while (declarator && (declarator->kind == cdk_pointer
8516 || declarator->kind == cdk_ptrmem))
8518 outer_declarator = declarator;
8519 declarator = declarator->declarator;
8521 while (declarator
8522 && declarator->kind == cdk_array
8523 && declarator->declarator
8524 && declarator->declarator->kind == cdk_array)
8526 outer_declarator = declarator;
8527 declarator = declarator->declarator;
8530 if (declarator && declarator->kind == cdk_array)
8532 *nelts = declarator->u.array.bounds;
8533 if (*nelts == error_mark_node)
8534 *nelts = integer_one_node;
8536 if (outer_declarator)
8537 outer_declarator->declarator = declarator->declarator;
8538 else
8539 new_declarator = NULL;
8542 return groktypename (&type_specifier_seq, new_declarator, false);
8545 /* Parse an (optional) new-declarator.
8547 new-declarator:
8548 ptr-operator new-declarator [opt]
8549 direct-new-declarator
8551 Returns the declarator. */
8553 static cp_declarator *
8554 cp_parser_new_declarator_opt (cp_parser* parser)
8556 enum tree_code code;
8557 tree type, std_attributes = NULL_TREE;
8558 cp_cv_quals cv_quals;
8560 /* We don't know if there's a ptr-operator next, or not. */
8561 cp_parser_parse_tentatively (parser);
8562 /* Look for a ptr-operator. */
8563 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8564 /* If that worked, look for more new-declarators. */
8565 if (cp_parser_parse_definitely (parser))
8567 cp_declarator *declarator;
8569 /* Parse another optional declarator. */
8570 declarator = cp_parser_new_declarator_opt (parser);
8572 declarator = cp_parser_make_indirect_declarator
8573 (code, type, cv_quals, declarator, std_attributes);
8575 return declarator;
8578 /* If the next token is a `[', there is a direct-new-declarator. */
8579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8580 return cp_parser_direct_new_declarator (parser);
8582 return NULL;
8585 /* Parse a direct-new-declarator.
8587 direct-new-declarator:
8588 [ expression ]
8589 direct-new-declarator [constant-expression]
8593 static cp_declarator *
8594 cp_parser_direct_new_declarator (cp_parser* parser)
8596 cp_declarator *declarator = NULL;
8598 while (true)
8600 tree expression;
8601 cp_token *token;
8603 /* Look for the opening `['. */
8604 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8606 token = cp_lexer_peek_token (parser->lexer);
8607 expression = cp_parser_expression (parser);
8608 /* The standard requires that the expression have integral
8609 type. DR 74 adds enumeration types. We believe that the
8610 real intent is that these expressions be handled like the
8611 expression in a `switch' condition, which also allows
8612 classes with a single conversion to integral or
8613 enumeration type. */
8614 if (!processing_template_decl)
8616 expression
8617 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8618 expression,
8619 /*complain=*/true);
8620 if (!expression)
8622 error_at (token->location,
8623 "expression in new-declarator must have integral "
8624 "or enumeration type");
8625 expression = error_mark_node;
8629 /* Look for the closing `]'. */
8630 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8632 /* Add this bound to the declarator. */
8633 declarator = make_array_declarator (declarator, expression);
8635 /* If the next token is not a `[', then there are no more
8636 bounds. */
8637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8638 break;
8641 return declarator;
8644 /* Parse a new-initializer.
8646 new-initializer:
8647 ( expression-list [opt] )
8648 braced-init-list
8650 Returns a representation of the expression-list. */
8652 static vec<tree, va_gc> *
8653 cp_parser_new_initializer (cp_parser* parser)
8655 vec<tree, va_gc> *expression_list;
8657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8659 tree t;
8660 bool expr_non_constant_p;
8661 cp_lexer_set_source_position (parser->lexer);
8662 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8663 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8664 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8665 expression_list = make_tree_vector_single (t);
8667 else
8668 expression_list = (cp_parser_parenthesized_expression_list
8669 (parser, non_attr, /*cast_p=*/false,
8670 /*allow_expansion_p=*/true,
8671 /*non_constant_p=*/NULL));
8673 return expression_list;
8676 /* Parse a delete-expression.
8678 delete-expression:
8679 :: [opt] delete cast-expression
8680 :: [opt] delete [ ] cast-expression
8682 Returns a representation of the expression. */
8684 static tree
8685 cp_parser_delete_expression (cp_parser* parser)
8687 bool global_scope_p;
8688 bool array_p;
8689 tree expression;
8691 /* Look for the optional `::' operator. */
8692 global_scope_p
8693 = (cp_parser_global_scope_opt (parser,
8694 /*current_scope_valid_p=*/false)
8695 != NULL_TREE);
8696 /* Look for the `delete' keyword. */
8697 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8698 /* See if the array syntax is in use. */
8699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8701 /* Consume the `[' token. */
8702 cp_lexer_consume_token (parser->lexer);
8703 /* Look for the `]' token. */
8704 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8705 /* Remember that this is the `[]' construct. */
8706 array_p = true;
8708 else
8709 array_p = false;
8711 /* Parse the cast-expression. */
8712 expression = cp_parser_simple_cast_expression (parser);
8714 /* A delete-expression may not appear in an integral constant
8715 expression. */
8716 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8717 return error_mark_node;
8719 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8720 tf_warning_or_error);
8723 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8724 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8725 0 otherwise. */
8727 static int
8728 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8730 cp_token *token = cp_lexer_peek_token (parser->lexer);
8731 switch (token->type)
8733 case CPP_COMMA:
8734 case CPP_SEMICOLON:
8735 case CPP_QUERY:
8736 case CPP_COLON:
8737 case CPP_CLOSE_SQUARE:
8738 case CPP_CLOSE_PAREN:
8739 case CPP_CLOSE_BRACE:
8740 case CPP_OPEN_BRACE:
8741 case CPP_DOT:
8742 case CPP_DOT_STAR:
8743 case CPP_DEREF:
8744 case CPP_DEREF_STAR:
8745 case CPP_DIV:
8746 case CPP_MOD:
8747 case CPP_LSHIFT:
8748 case CPP_RSHIFT:
8749 case CPP_LESS:
8750 case CPP_GREATER:
8751 case CPP_LESS_EQ:
8752 case CPP_GREATER_EQ:
8753 case CPP_EQ_EQ:
8754 case CPP_NOT_EQ:
8755 case CPP_EQ:
8756 case CPP_MULT_EQ:
8757 case CPP_DIV_EQ:
8758 case CPP_MOD_EQ:
8759 case CPP_PLUS_EQ:
8760 case CPP_MINUS_EQ:
8761 case CPP_RSHIFT_EQ:
8762 case CPP_LSHIFT_EQ:
8763 case CPP_AND_EQ:
8764 case CPP_XOR_EQ:
8765 case CPP_OR_EQ:
8766 case CPP_XOR:
8767 case CPP_OR:
8768 case CPP_OR_OR:
8769 case CPP_EOF:
8770 case CPP_ELLIPSIS:
8771 return 0;
8773 case CPP_OPEN_PAREN:
8774 /* In ((type ()) () the last () isn't a valid cast-expression,
8775 so the whole must be parsed as postfix-expression. */
8776 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8777 != CPP_CLOSE_PAREN;
8779 case CPP_OPEN_SQUARE:
8780 /* '[' may start a primary-expression in obj-c++ and in C++11,
8781 as a lambda-expression, eg, '(void)[]{}'. */
8782 if (cxx_dialect >= cxx11)
8783 return -1;
8784 return c_dialect_objc ();
8786 case CPP_PLUS_PLUS:
8787 case CPP_MINUS_MINUS:
8788 /* '++' and '--' may or may not start a cast-expression:
8790 struct T { void operator++(int); };
8791 void f() { (T())++; }
8795 int a;
8796 (int)++a; */
8797 return -1;
8799 default:
8800 return 1;
8804 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8805 in the order: const_cast, static_cast, reinterpret_cast.
8807 Don't suggest dynamic_cast.
8809 Return the first legal cast kind found, or NULL otherwise. */
8811 static const char *
8812 get_cast_suggestion (tree dst_type, tree orig_expr)
8814 tree trial;
8816 /* Reuse the parser logic by attempting to build the various kinds of
8817 cast, with "complain" disabled.
8818 Identify the first such cast that is valid. */
8820 /* Don't attempt to run such logic within template processing. */
8821 if (processing_template_decl)
8822 return NULL;
8824 /* First try const_cast. */
8825 trial = build_const_cast (dst_type, orig_expr, tf_none);
8826 if (trial != error_mark_node)
8827 return "const_cast";
8829 /* If that fails, try static_cast. */
8830 trial = build_static_cast (dst_type, orig_expr, tf_none);
8831 if (trial != error_mark_node)
8832 return "static_cast";
8834 /* Finally, try reinterpret_cast. */
8835 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8836 if (trial != error_mark_node)
8837 return "reinterpret_cast";
8839 /* No such cast possible. */
8840 return NULL;
8843 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8844 suggesting how to convert a C-style cast of the form:
8846 (DST_TYPE)ORIG_EXPR
8848 to a C++-style cast.
8850 The primary range of RICHLOC is asssumed to be that of the original
8851 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8852 of the parens in the C-style cast. */
8854 static void
8855 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8856 location_t close_paren_loc, tree orig_expr,
8857 tree dst_type)
8859 /* This function is non-trivial, so bail out now if the warning isn't
8860 going to be emitted. */
8861 if (!warn_old_style_cast)
8862 return;
8864 /* Try to find a legal C++ cast, trying them in order:
8865 const_cast, static_cast, reinterpret_cast. */
8866 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8867 if (!cast_suggestion)
8868 return;
8870 /* Replace the open paren with "CAST_SUGGESTION<". */
8871 pretty_printer pp;
8872 pp_printf (&pp, "%s<", cast_suggestion);
8873 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8875 /* Replace the close paren with "> (". */
8876 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8878 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8879 rich_loc->add_fixit_insert_after (")");
8883 /* Parse a cast-expression.
8885 cast-expression:
8886 unary-expression
8887 ( type-id ) cast-expression
8889 ADDRESS_P is true iff the unary-expression is appearing as the
8890 operand of the `&' operator. CAST_P is true if this expression is
8891 the target of a cast.
8893 Returns a representation of the expression. */
8895 static cp_expr
8896 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8897 bool decltype_p, cp_id_kind * pidk)
8899 /* If it's a `(', then we might be looking at a cast. */
8900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8902 tree type = NULL_TREE;
8903 cp_expr expr (NULL_TREE);
8904 int cast_expression = 0;
8905 const char *saved_message;
8907 /* There's no way to know yet whether or not this is a cast.
8908 For example, `(int (3))' is a unary-expression, while `(int)
8909 3' is a cast. So, we resort to parsing tentatively. */
8910 cp_parser_parse_tentatively (parser);
8911 /* Types may not be defined in a cast. */
8912 saved_message = parser->type_definition_forbidden_message;
8913 parser->type_definition_forbidden_message
8914 = G_("types may not be defined in casts");
8915 /* Consume the `('. */
8916 matching_parens parens;
8917 cp_token *open_paren = parens.consume_open (parser);
8918 location_t open_paren_loc = open_paren->location;
8919 location_t close_paren_loc = UNKNOWN_LOCATION;
8921 /* A very tricky bit is that `(struct S) { 3 }' is a
8922 compound-literal (which we permit in C++ as an extension).
8923 But, that construct is not a cast-expression -- it is a
8924 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8925 is legal; if the compound-literal were a cast-expression,
8926 you'd need an extra set of parentheses.) But, if we parse
8927 the type-id, and it happens to be a class-specifier, then we
8928 will commit to the parse at that point, because we cannot
8929 undo the action that is done when creating a new class. So,
8930 then we cannot back up and do a postfix-expression.
8932 Another tricky case is the following (c++/29234):
8934 struct S { void operator () (); };
8936 void foo ()
8938 ( S()() );
8941 As a type-id we parse the parenthesized S()() as a function
8942 returning a function, groktypename complains and we cannot
8943 back up in this case either.
8945 Therefore, we scan ahead to the closing `)', and check to see
8946 if the tokens after the `)' can start a cast-expression. Otherwise
8947 we are dealing with an unary-expression, a postfix-expression
8948 or something else.
8950 Yet another tricky case, in C++11, is the following (c++/54891):
8952 (void)[]{};
8954 The issue is that usually, besides the case of lambda-expressions,
8955 the parenthesized type-id cannot be followed by '[', and, eg, we
8956 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8957 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8958 we don't commit, we try a cast-expression, then an unary-expression.
8960 Save tokens so that we can put them back. */
8961 cp_lexer_save_tokens (parser->lexer);
8963 /* We may be looking at a cast-expression. */
8964 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8965 /*consume_paren=*/true))
8966 cast_expression
8967 = cp_parser_tokens_start_cast_expression (parser);
8969 /* Roll back the tokens we skipped. */
8970 cp_lexer_rollback_tokens (parser->lexer);
8971 /* If we aren't looking at a cast-expression, simulate an error so
8972 that the call to cp_parser_error_occurred below returns true. */
8973 if (!cast_expression)
8974 cp_parser_simulate_error (parser);
8975 else
8977 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8978 parser->in_type_id_in_expr_p = true;
8979 /* Look for the type-id. */
8980 type = cp_parser_type_id (parser);
8981 /* Look for the closing `)'. */
8982 cp_token *close_paren = parens.require_close (parser);
8983 if (close_paren)
8984 close_paren_loc = close_paren->location;
8985 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8988 /* Restore the saved message. */
8989 parser->type_definition_forbidden_message = saved_message;
8991 /* At this point this can only be either a cast or a
8992 parenthesized ctor such as `(T ())' that looks like a cast to
8993 function returning T. */
8994 if (!cp_parser_error_occurred (parser))
8996 /* Only commit if the cast-expression doesn't start with
8997 '++', '--', or '[' in C++11. */
8998 if (cast_expression > 0)
8999 cp_parser_commit_to_topmost_tentative_parse (parser);
9001 expr = cp_parser_cast_expression (parser,
9002 /*address_p=*/false,
9003 /*cast_p=*/true,
9004 /*decltype_p=*/false,
9005 pidk);
9007 if (cp_parser_parse_definitely (parser))
9009 /* Warn about old-style casts, if so requested. */
9010 if (warn_old_style_cast
9011 && !in_system_header_at (input_location)
9012 && !VOID_TYPE_P (type)
9013 && current_lang_name != lang_name_c)
9015 gcc_rich_location rich_loc (input_location);
9016 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9017 expr, type);
9018 warning_at (&rich_loc, OPT_Wold_style_cast,
9019 "use of old-style cast to %q#T", type);
9022 /* Only type conversions to integral or enumeration types
9023 can be used in constant-expressions. */
9024 if (!cast_valid_in_integral_constant_expression_p (type)
9025 && cp_parser_non_integral_constant_expression (parser,
9026 NIC_CAST))
9027 return error_mark_node;
9029 /* Perform the cast. */
9030 /* Make a location:
9031 (TYPE) EXPR
9032 ^~~~~~~~~~~
9033 with start==caret at the open paren, extending to the
9034 end of "expr". */
9035 location_t cast_loc = make_location (open_paren_loc,
9036 open_paren_loc,
9037 expr.get_finish ());
9038 expr = build_c_cast (cast_loc, type, expr);
9039 return expr;
9042 else
9043 cp_parser_abort_tentative_parse (parser);
9046 /* If we get here, then it's not a cast, so it must be a
9047 unary-expression. */
9048 return cp_parser_unary_expression (parser, pidk, address_p,
9049 cast_p, decltype_p);
9052 /* Parse a binary expression of the general form:
9054 pm-expression:
9055 cast-expression
9056 pm-expression .* cast-expression
9057 pm-expression ->* cast-expression
9059 multiplicative-expression:
9060 pm-expression
9061 multiplicative-expression * pm-expression
9062 multiplicative-expression / pm-expression
9063 multiplicative-expression % pm-expression
9065 additive-expression:
9066 multiplicative-expression
9067 additive-expression + multiplicative-expression
9068 additive-expression - multiplicative-expression
9070 shift-expression:
9071 additive-expression
9072 shift-expression << additive-expression
9073 shift-expression >> additive-expression
9075 relational-expression:
9076 shift-expression
9077 relational-expression < shift-expression
9078 relational-expression > shift-expression
9079 relational-expression <= shift-expression
9080 relational-expression >= shift-expression
9082 GNU Extension:
9084 relational-expression:
9085 relational-expression <? shift-expression
9086 relational-expression >? shift-expression
9088 equality-expression:
9089 relational-expression
9090 equality-expression == relational-expression
9091 equality-expression != relational-expression
9093 and-expression:
9094 equality-expression
9095 and-expression & equality-expression
9097 exclusive-or-expression:
9098 and-expression
9099 exclusive-or-expression ^ and-expression
9101 inclusive-or-expression:
9102 exclusive-or-expression
9103 inclusive-or-expression | exclusive-or-expression
9105 logical-and-expression:
9106 inclusive-or-expression
9107 logical-and-expression && inclusive-or-expression
9109 logical-or-expression:
9110 logical-and-expression
9111 logical-or-expression || logical-and-expression
9113 All these are implemented with a single function like:
9115 binary-expression:
9116 simple-cast-expression
9117 binary-expression <token> binary-expression
9119 CAST_P is true if this expression is the target of a cast.
9121 The binops_by_token map is used to get the tree codes for each <token> type.
9122 binary-expressions are associated according to a precedence table. */
9124 #define TOKEN_PRECEDENCE(token) \
9125 (((token->type == CPP_GREATER \
9126 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9127 && !parser->greater_than_is_operator_p) \
9128 ? PREC_NOT_OPERATOR \
9129 : binops_by_token[token->type].prec)
9131 static cp_expr
9132 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9133 bool no_toplevel_fold_p,
9134 bool decltype_p,
9135 enum cp_parser_prec prec,
9136 cp_id_kind * pidk)
9138 cp_parser_expression_stack stack;
9139 cp_parser_expression_stack_entry *sp = &stack[0];
9140 cp_parser_expression_stack_entry current;
9141 cp_expr rhs;
9142 cp_token *token;
9143 enum tree_code rhs_type;
9144 enum cp_parser_prec new_prec, lookahead_prec;
9145 tree overload;
9147 /* Parse the first expression. */
9148 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9149 ? TRUTH_NOT_EXPR : ERROR_MARK);
9150 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9151 cast_p, decltype_p, pidk);
9152 current.prec = prec;
9154 if (cp_parser_error_occurred (parser))
9155 return error_mark_node;
9157 for (;;)
9159 /* Get an operator token. */
9160 token = cp_lexer_peek_token (parser->lexer);
9162 if (warn_cxx11_compat
9163 && token->type == CPP_RSHIFT
9164 && !parser->greater_than_is_operator_p)
9166 if (warning_at (token->location, OPT_Wc__11_compat,
9167 "%<>>%> operator is treated"
9168 " as two right angle brackets in C++11"))
9169 inform (token->location,
9170 "suggest parentheses around %<>>%> expression");
9173 new_prec = TOKEN_PRECEDENCE (token);
9174 if (new_prec != PREC_NOT_OPERATOR
9175 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9176 /* This is a fold-expression; handle it later. */
9177 new_prec = PREC_NOT_OPERATOR;
9179 /* Popping an entry off the stack means we completed a subexpression:
9180 - either we found a token which is not an operator (`>' where it is not
9181 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9182 will happen repeatedly;
9183 - or, we found an operator which has lower priority. This is the case
9184 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9185 parsing `3 * 4'. */
9186 if (new_prec <= current.prec)
9188 if (sp == stack)
9189 break;
9190 else
9191 goto pop;
9194 get_rhs:
9195 current.tree_type = binops_by_token[token->type].tree_type;
9196 current.loc = token->location;
9198 /* We used the operator token. */
9199 cp_lexer_consume_token (parser->lexer);
9201 /* For "false && x" or "true || x", x will never be executed;
9202 disable warnings while evaluating it. */
9203 if (current.tree_type == TRUTH_ANDIF_EXPR)
9204 c_inhibit_evaluation_warnings +=
9205 cp_fully_fold (current.lhs) == truthvalue_false_node;
9206 else if (current.tree_type == TRUTH_ORIF_EXPR)
9207 c_inhibit_evaluation_warnings +=
9208 cp_fully_fold (current.lhs) == truthvalue_true_node;
9210 /* Extract another operand. It may be the RHS of this expression
9211 or the LHS of a new, higher priority expression. */
9212 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9213 ? TRUTH_NOT_EXPR : ERROR_MARK);
9214 rhs = cp_parser_simple_cast_expression (parser);
9216 /* Get another operator token. Look up its precedence to avoid
9217 building a useless (immediately popped) stack entry for common
9218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9219 token = cp_lexer_peek_token (parser->lexer);
9220 lookahead_prec = TOKEN_PRECEDENCE (token);
9221 if (lookahead_prec != PREC_NOT_OPERATOR
9222 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9223 lookahead_prec = PREC_NOT_OPERATOR;
9224 if (lookahead_prec > new_prec)
9226 /* ... and prepare to parse the RHS of the new, higher priority
9227 expression. Since precedence levels on the stack are
9228 monotonically increasing, we do not have to care about
9229 stack overflows. */
9230 *sp = current;
9231 ++sp;
9232 current.lhs = rhs;
9233 current.lhs_type = rhs_type;
9234 current.prec = new_prec;
9235 new_prec = lookahead_prec;
9236 goto get_rhs;
9238 pop:
9239 lookahead_prec = new_prec;
9240 /* If the stack is not empty, we have parsed into LHS the right side
9241 (`4' in the example above) of an expression we had suspended.
9242 We can use the information on the stack to recover the LHS (`3')
9243 from the stack together with the tree code (`MULT_EXPR'), and
9244 the precedence of the higher level subexpression
9245 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9246 which will be used to actually build the additive expression. */
9247 rhs = current.lhs;
9248 rhs_type = current.lhs_type;
9249 --sp;
9250 current = *sp;
9253 /* Undo the disabling of warnings done above. */
9254 if (current.tree_type == TRUTH_ANDIF_EXPR)
9255 c_inhibit_evaluation_warnings -=
9256 cp_fully_fold (current.lhs) == truthvalue_false_node;
9257 else if (current.tree_type == TRUTH_ORIF_EXPR)
9258 c_inhibit_evaluation_warnings -=
9259 cp_fully_fold (current.lhs) == truthvalue_true_node;
9261 if (warn_logical_not_paren
9262 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9263 && current.lhs_type == TRUTH_NOT_EXPR
9264 /* Avoid warning for !!x == y. */
9265 && (TREE_CODE (current.lhs) != NE_EXPR
9266 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9267 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9268 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9269 /* Avoid warning for !b == y where b is boolean. */
9270 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9271 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9272 != BOOLEAN_TYPE))))
9273 /* Avoid warning for !!b == y where b is boolean. */
9274 && (!DECL_P (current.lhs)
9275 || TREE_TYPE (current.lhs) == NULL_TREE
9276 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9277 warn_logical_not_parentheses (current.loc, current.tree_type,
9278 current.lhs, maybe_constant_value (rhs));
9280 overload = NULL;
9282 location_t combined_loc = make_location (current.loc,
9283 current.lhs.get_start (),
9284 rhs.get_finish ());
9286 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9287 ERROR_MARK for everything that is not a binary expression.
9288 This makes warn_about_parentheses miss some warnings that
9289 involve unary operators. For unary expressions we should
9290 pass the correct tree_code unless the unary expression was
9291 surrounded by parentheses.
9293 if (no_toplevel_fold_p
9294 && lookahead_prec <= current.prec
9295 && sp == stack)
9296 current.lhs = build2_loc (combined_loc,
9297 current.tree_type,
9298 TREE_CODE_CLASS (current.tree_type)
9299 == tcc_comparison
9300 ? boolean_type_node : TREE_TYPE (current.lhs),
9301 current.lhs, rhs);
9302 else
9304 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9305 current.lhs, current.lhs_type,
9306 rhs, rhs_type, &overload,
9307 complain_flags (decltype_p));
9308 /* TODO: build_x_binary_op doesn't always honor the location. */
9309 current.lhs.set_location (combined_loc);
9311 current.lhs_type = current.tree_type;
9313 /* If the binary operator required the use of an overloaded operator,
9314 then this expression cannot be an integral constant-expression.
9315 An overloaded operator can be used even if both operands are
9316 otherwise permissible in an integral constant-expression if at
9317 least one of the operands is of enumeration type. */
9319 if (overload
9320 && cp_parser_non_integral_constant_expression (parser,
9321 NIC_OVERLOADED))
9322 return error_mark_node;
9325 return current.lhs;
9328 static cp_expr
9329 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9330 bool no_toplevel_fold_p,
9331 enum cp_parser_prec prec,
9332 cp_id_kind * pidk)
9334 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9335 /*decltype*/false, prec, pidk);
9338 /* Parse the `? expression : assignment-expression' part of a
9339 conditional-expression. The LOGICAL_OR_EXPR is the
9340 logical-or-expression that started the conditional-expression.
9341 Returns a representation of the entire conditional-expression.
9343 This routine is used by cp_parser_assignment_expression.
9345 ? expression : assignment-expression
9347 GNU Extensions:
9349 ? : assignment-expression */
9351 static tree
9352 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9354 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9355 cp_expr assignment_expr;
9356 struct cp_token *token;
9357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9359 /* Consume the `?' token. */
9360 cp_lexer_consume_token (parser->lexer);
9361 token = cp_lexer_peek_token (parser->lexer);
9362 if (cp_parser_allow_gnu_extensions_p (parser)
9363 && token->type == CPP_COLON)
9365 pedwarn (token->location, OPT_Wpedantic,
9366 "ISO C++ does not allow ?: with omitted middle operand");
9367 /* Implicit true clause. */
9368 expr = NULL_TREE;
9369 c_inhibit_evaluation_warnings +=
9370 folded_logical_or_expr == truthvalue_true_node;
9371 warn_for_omitted_condop (token->location, logical_or_expr);
9373 else
9375 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9376 parser->colon_corrects_to_scope_p = false;
9377 /* Parse the expression. */
9378 c_inhibit_evaluation_warnings +=
9379 folded_logical_or_expr == truthvalue_false_node;
9380 expr = cp_parser_expression (parser);
9381 c_inhibit_evaluation_warnings +=
9382 ((folded_logical_or_expr == truthvalue_true_node)
9383 - (folded_logical_or_expr == truthvalue_false_node));
9384 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9387 /* The next token should be a `:'. */
9388 cp_parser_require (parser, CPP_COLON, RT_COLON);
9389 /* Parse the assignment-expression. */
9390 assignment_expr = cp_parser_assignment_expression (parser);
9391 c_inhibit_evaluation_warnings -=
9392 folded_logical_or_expr == truthvalue_true_node;
9394 /* Make a location:
9395 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9396 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9397 with the caret at the "?", ranging from the start of
9398 the logical_or_expr to the end of the assignment_expr. */
9399 loc = make_location (loc,
9400 logical_or_expr.get_start (),
9401 assignment_expr.get_finish ());
9403 /* Build the conditional-expression. */
9404 return build_x_conditional_expr (loc, logical_or_expr,
9405 expr,
9406 assignment_expr,
9407 tf_warning_or_error);
9410 /* Parse an assignment-expression.
9412 assignment-expression:
9413 conditional-expression
9414 logical-or-expression assignment-operator assignment_expression
9415 throw-expression
9417 CAST_P is true if this expression is the target of a cast.
9418 DECLTYPE_P is true if this expression is the operand of decltype.
9420 Returns a representation for the expression. */
9422 static cp_expr
9423 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9424 bool cast_p, bool decltype_p)
9426 cp_expr expr;
9428 /* If the next token is the `throw' keyword, then we're looking at
9429 a throw-expression. */
9430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9431 expr = cp_parser_throw_expression (parser);
9432 /* Otherwise, it must be that we are looking at a
9433 logical-or-expression. */
9434 else
9436 /* Parse the binary expressions (logical-or-expression). */
9437 expr = cp_parser_binary_expression (parser, cast_p, false,
9438 decltype_p,
9439 PREC_NOT_OPERATOR, pidk);
9440 /* If the next token is a `?' then we're actually looking at a
9441 conditional-expression. */
9442 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9443 return cp_parser_question_colon_clause (parser, expr);
9444 else
9446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9448 /* If it's an assignment-operator, we're using the second
9449 production. */
9450 enum tree_code assignment_operator
9451 = cp_parser_assignment_operator_opt (parser);
9452 if (assignment_operator != ERROR_MARK)
9454 bool non_constant_p;
9456 /* Parse the right-hand side of the assignment. */
9457 cp_expr rhs = cp_parser_initializer_clause (parser,
9458 &non_constant_p);
9460 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9463 /* An assignment may not appear in a
9464 constant-expression. */
9465 if (cp_parser_non_integral_constant_expression (parser,
9466 NIC_ASSIGNMENT))
9467 return error_mark_node;
9468 /* Build the assignment expression. Its default
9469 location:
9470 LHS = RHS
9471 ~~~~^~~~~
9472 is the location of the '=' token as the
9473 caret, ranging from the start of the lhs to the
9474 end of the rhs. */
9475 loc = make_location (loc,
9476 expr.get_start (),
9477 rhs.get_finish ());
9478 expr = build_x_modify_expr (loc, expr,
9479 assignment_operator,
9480 rhs,
9481 complain_flags (decltype_p));
9482 /* TODO: build_x_modify_expr doesn't honor the location,
9483 so we must set it here. */
9484 expr.set_location (loc);
9489 return expr;
9492 /* Parse an (optional) assignment-operator.
9494 assignment-operator: one of
9495 = *= /= %= += -= >>= <<= &= ^= |=
9497 GNU Extension:
9499 assignment-operator: one of
9500 <?= >?=
9502 If the next token is an assignment operator, the corresponding tree
9503 code is returned, and the token is consumed. For example, for
9504 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9505 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9506 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9507 operator, ERROR_MARK is returned. */
9509 static enum tree_code
9510 cp_parser_assignment_operator_opt (cp_parser* parser)
9512 enum tree_code op;
9513 cp_token *token;
9515 /* Peek at the next token. */
9516 token = cp_lexer_peek_token (parser->lexer);
9518 switch (token->type)
9520 case CPP_EQ:
9521 op = NOP_EXPR;
9522 break;
9524 case CPP_MULT_EQ:
9525 op = MULT_EXPR;
9526 break;
9528 case CPP_DIV_EQ:
9529 op = TRUNC_DIV_EXPR;
9530 break;
9532 case CPP_MOD_EQ:
9533 op = TRUNC_MOD_EXPR;
9534 break;
9536 case CPP_PLUS_EQ:
9537 op = PLUS_EXPR;
9538 break;
9540 case CPP_MINUS_EQ:
9541 op = MINUS_EXPR;
9542 break;
9544 case CPP_RSHIFT_EQ:
9545 op = RSHIFT_EXPR;
9546 break;
9548 case CPP_LSHIFT_EQ:
9549 op = LSHIFT_EXPR;
9550 break;
9552 case CPP_AND_EQ:
9553 op = BIT_AND_EXPR;
9554 break;
9556 case CPP_XOR_EQ:
9557 op = BIT_XOR_EXPR;
9558 break;
9560 case CPP_OR_EQ:
9561 op = BIT_IOR_EXPR;
9562 break;
9564 default:
9565 /* Nothing else is an assignment operator. */
9566 op = ERROR_MARK;
9569 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9570 if (op != ERROR_MARK
9571 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9572 op = ERROR_MARK;
9574 /* If it was an assignment operator, consume it. */
9575 if (op != ERROR_MARK)
9576 cp_lexer_consume_token (parser->lexer);
9578 return op;
9581 /* Parse an expression.
9583 expression:
9584 assignment-expression
9585 expression , assignment-expression
9587 CAST_P is true if this expression is the target of a cast.
9588 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9589 except possibly parenthesized or on the RHS of a comma (N3276).
9591 Returns a representation of the expression. */
9593 static cp_expr
9594 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9595 bool cast_p, bool decltype_p)
9597 cp_expr expression = NULL_TREE;
9598 location_t loc = UNKNOWN_LOCATION;
9600 while (true)
9602 cp_expr assignment_expression;
9604 /* Parse the next assignment-expression. */
9605 assignment_expression
9606 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9608 /* We don't create a temporary for a call that is the immediate operand
9609 of decltype or on the RHS of a comma. But when we see a comma, we
9610 need to create a temporary for a call on the LHS. */
9611 if (decltype_p && !processing_template_decl
9612 && TREE_CODE (assignment_expression) == CALL_EXPR
9613 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9614 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9615 assignment_expression
9616 = build_cplus_new (TREE_TYPE (assignment_expression),
9617 assignment_expression, tf_warning_or_error);
9619 /* If this is the first assignment-expression, we can just
9620 save it away. */
9621 if (!expression)
9622 expression = assignment_expression;
9623 else
9625 /* Create a location with caret at the comma, ranging
9626 from the start of the LHS to the end of the RHS. */
9627 loc = make_location (loc,
9628 expression.get_start (),
9629 assignment_expression.get_finish ());
9630 expression = build_x_compound_expr (loc, expression,
9631 assignment_expression,
9632 complain_flags (decltype_p));
9633 expression.set_location (loc);
9635 /* If the next token is not a comma, or we're in a fold-expression, then
9636 we are done with the expression. */
9637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9638 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9639 break;
9640 /* Consume the `,'. */
9641 loc = cp_lexer_peek_token (parser->lexer)->location;
9642 cp_lexer_consume_token (parser->lexer);
9643 /* A comma operator cannot appear in a constant-expression. */
9644 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9645 expression = error_mark_node;
9648 return expression;
9651 /* Parse a constant-expression.
9653 constant-expression:
9654 conditional-expression
9656 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9657 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9658 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9659 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9660 only parse a conditional-expression, otherwise parse an
9661 assignment-expression. See below for rationale. */
9663 static cp_expr
9664 cp_parser_constant_expression (cp_parser* parser,
9665 bool allow_non_constant_p,
9666 bool *non_constant_p,
9667 bool strict_p)
9669 bool saved_integral_constant_expression_p;
9670 bool saved_allow_non_integral_constant_expression_p;
9671 bool saved_non_integral_constant_expression_p;
9672 cp_expr expression;
9674 /* It might seem that we could simply parse the
9675 conditional-expression, and then check to see if it were
9676 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9677 one that the compiler can figure out is constant, possibly after
9678 doing some simplifications or optimizations. The standard has a
9679 precise definition of constant-expression, and we must honor
9680 that, even though it is somewhat more restrictive.
9682 For example:
9684 int i[(2, 3)];
9686 is not a legal declaration, because `(2, 3)' is not a
9687 constant-expression. The `,' operator is forbidden in a
9688 constant-expression. However, GCC's constant-folding machinery
9689 will fold this operation to an INTEGER_CST for `3'. */
9691 /* Save the old settings. */
9692 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9693 saved_allow_non_integral_constant_expression_p
9694 = parser->allow_non_integral_constant_expression_p;
9695 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9696 /* We are now parsing a constant-expression. */
9697 parser->integral_constant_expression_p = true;
9698 parser->allow_non_integral_constant_expression_p
9699 = (allow_non_constant_p || cxx_dialect >= cxx11);
9700 parser->non_integral_constant_expression_p = false;
9701 /* Although the grammar says "conditional-expression", when not STRICT_P,
9702 we parse an "assignment-expression", which also permits
9703 "throw-expression" and the use of assignment operators. In the case
9704 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9705 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9706 actually essential that we look for an assignment-expression.
9707 For example, cp_parser_initializer_clauses uses this function to
9708 determine whether a particular assignment-expression is in fact
9709 constant. */
9710 if (strict_p)
9712 /* Parse the binary expressions (logical-or-expression). */
9713 expression = cp_parser_binary_expression (parser, false, false, false,
9714 PREC_NOT_OPERATOR, NULL);
9715 /* If the next token is a `?' then we're actually looking at
9716 a conditional-expression; otherwise we're done. */
9717 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9718 expression = cp_parser_question_colon_clause (parser, expression);
9720 else
9721 expression = cp_parser_assignment_expression (parser);
9722 /* Restore the old settings. */
9723 parser->integral_constant_expression_p
9724 = saved_integral_constant_expression_p;
9725 parser->allow_non_integral_constant_expression_p
9726 = saved_allow_non_integral_constant_expression_p;
9727 if (cxx_dialect >= cxx11)
9729 /* Require an rvalue constant expression here; that's what our
9730 callers expect. Reference constant expressions are handled
9731 separately in e.g. cp_parser_template_argument. */
9732 tree decay = expression;
9733 if (TREE_TYPE (expression)
9734 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9735 decay = build_address (expression);
9736 bool is_const = potential_rvalue_constant_expression (decay);
9737 parser->non_integral_constant_expression_p = !is_const;
9738 if (!is_const && !allow_non_constant_p)
9739 require_potential_rvalue_constant_expression (decay);
9741 if (allow_non_constant_p)
9742 *non_constant_p = parser->non_integral_constant_expression_p;
9743 parser->non_integral_constant_expression_p
9744 = saved_non_integral_constant_expression_p;
9746 return expression;
9749 /* Parse __builtin_offsetof.
9751 offsetof-expression:
9752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9754 offsetof-member-designator:
9755 id-expression
9756 | offsetof-member-designator "." id-expression
9757 | offsetof-member-designator "[" expression "]"
9758 | offsetof-member-designator "->" id-expression */
9760 static cp_expr
9761 cp_parser_builtin_offsetof (cp_parser *parser)
9763 int save_ice_p, save_non_ice_p;
9764 tree type;
9765 cp_expr expr;
9766 cp_id_kind dummy;
9767 cp_token *token;
9768 location_t finish_loc;
9770 /* We're about to accept non-integral-constant things, but will
9771 definitely yield an integral constant expression. Save and
9772 restore these values around our local parsing. */
9773 save_ice_p = parser->integral_constant_expression_p;
9774 save_non_ice_p = parser->non_integral_constant_expression_p;
9776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9778 /* Consume the "__builtin_offsetof" token. */
9779 cp_lexer_consume_token (parser->lexer);
9780 /* Consume the opening `('. */
9781 matching_parens parens;
9782 parens.require_open (parser);
9783 /* Parse the type-id. */
9784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9785 type = cp_parser_type_id (parser);
9786 /* Look for the `,'. */
9787 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9788 token = cp_lexer_peek_token (parser->lexer);
9790 /* Build the (type *)null that begins the traditional offsetof macro. */
9791 tree object_ptr
9792 = build_static_cast (build_pointer_type (type), null_pointer_node,
9793 tf_warning_or_error);
9795 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9796 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9797 true, &dummy, token->location);
9798 while (true)
9800 token = cp_lexer_peek_token (parser->lexer);
9801 switch (token->type)
9803 case CPP_OPEN_SQUARE:
9804 /* offsetof-member-designator "[" expression "]" */
9805 expr = cp_parser_postfix_open_square_expression (parser, expr,
9806 true, false);
9807 break;
9809 case CPP_DEREF:
9810 /* offsetof-member-designator "->" identifier */
9811 expr = grok_array_decl (token->location, expr,
9812 integer_zero_node, false);
9813 /* FALLTHRU */
9815 case CPP_DOT:
9816 /* offsetof-member-designator "." identifier */
9817 cp_lexer_consume_token (parser->lexer);
9818 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9819 expr, true, &dummy,
9820 token->location);
9821 break;
9823 case CPP_CLOSE_PAREN:
9824 /* Consume the ")" token. */
9825 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9826 cp_lexer_consume_token (parser->lexer);
9827 goto success;
9829 default:
9830 /* Error. We know the following require will fail, but
9831 that gives the proper error message. */
9832 parens.require_close (parser);
9833 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9834 expr = error_mark_node;
9835 goto failure;
9839 success:
9840 /* Make a location of the form:
9841 __builtin_offsetof (struct s, f)
9842 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9843 with caret at the type-id, ranging from the start of the
9844 "_builtin_offsetof" token to the close paren. */
9845 loc = make_location (loc, start_loc, finish_loc);
9846 /* The result will be an INTEGER_CST, so we need to explicitly
9847 preserve the location. */
9848 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9850 failure:
9851 parser->integral_constant_expression_p = save_ice_p;
9852 parser->non_integral_constant_expression_p = save_non_ice_p;
9854 expr = expr.maybe_add_location_wrapper ();
9855 return expr;
9858 /* Parse a trait expression.
9860 Returns a representation of the expression, the underlying type
9861 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9863 static cp_expr
9864 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9866 cp_trait_kind kind;
9867 tree type1, type2 = NULL_TREE;
9868 bool binary = false;
9869 bool variadic = false;
9871 switch (keyword)
9873 case RID_HAS_NOTHROW_ASSIGN:
9874 kind = CPTK_HAS_NOTHROW_ASSIGN;
9875 break;
9876 case RID_HAS_NOTHROW_CONSTRUCTOR:
9877 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9878 break;
9879 case RID_HAS_NOTHROW_COPY:
9880 kind = CPTK_HAS_NOTHROW_COPY;
9881 break;
9882 case RID_HAS_TRIVIAL_ASSIGN:
9883 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9884 break;
9885 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9886 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9887 break;
9888 case RID_HAS_TRIVIAL_COPY:
9889 kind = CPTK_HAS_TRIVIAL_COPY;
9890 break;
9891 case RID_HAS_TRIVIAL_DESTRUCTOR:
9892 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9893 break;
9894 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9895 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9896 break;
9897 case RID_HAS_VIRTUAL_DESTRUCTOR:
9898 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9899 break;
9900 case RID_IS_ABSTRACT:
9901 kind = CPTK_IS_ABSTRACT;
9902 break;
9903 case RID_IS_AGGREGATE:
9904 kind = CPTK_IS_AGGREGATE;
9905 break;
9906 case RID_IS_BASE_OF:
9907 kind = CPTK_IS_BASE_OF;
9908 binary = true;
9909 break;
9910 case RID_IS_CLASS:
9911 kind = CPTK_IS_CLASS;
9912 break;
9913 case RID_IS_EMPTY:
9914 kind = CPTK_IS_EMPTY;
9915 break;
9916 case RID_IS_ENUM:
9917 kind = CPTK_IS_ENUM;
9918 break;
9919 case RID_IS_FINAL:
9920 kind = CPTK_IS_FINAL;
9921 break;
9922 case RID_IS_LITERAL_TYPE:
9923 kind = CPTK_IS_LITERAL_TYPE;
9924 break;
9925 case RID_IS_POD:
9926 kind = CPTK_IS_POD;
9927 break;
9928 case RID_IS_POLYMORPHIC:
9929 kind = CPTK_IS_POLYMORPHIC;
9930 break;
9931 case RID_IS_SAME_AS:
9932 kind = CPTK_IS_SAME_AS;
9933 binary = true;
9934 break;
9935 case RID_IS_STD_LAYOUT:
9936 kind = CPTK_IS_STD_LAYOUT;
9937 break;
9938 case RID_IS_TRIVIAL:
9939 kind = CPTK_IS_TRIVIAL;
9940 break;
9941 case RID_IS_TRIVIALLY_ASSIGNABLE:
9942 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9943 binary = true;
9944 break;
9945 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9946 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9947 variadic = true;
9948 break;
9949 case RID_IS_TRIVIALLY_COPYABLE:
9950 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9951 break;
9952 case RID_IS_UNION:
9953 kind = CPTK_IS_UNION;
9954 break;
9955 case RID_UNDERLYING_TYPE:
9956 kind = CPTK_UNDERLYING_TYPE;
9957 break;
9958 case RID_BASES:
9959 kind = CPTK_BASES;
9960 break;
9961 case RID_DIRECT_BASES:
9962 kind = CPTK_DIRECT_BASES;
9963 break;
9964 case RID_IS_ASSIGNABLE:
9965 kind = CPTK_IS_ASSIGNABLE;
9966 binary = true;
9967 break;
9968 case RID_IS_CONSTRUCTIBLE:
9969 kind = CPTK_IS_CONSTRUCTIBLE;
9970 variadic = true;
9971 break;
9972 default:
9973 gcc_unreachable ();
9976 /* Get location of initial token. */
9977 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9979 /* Consume the token. */
9980 cp_lexer_consume_token (parser->lexer);
9982 matching_parens parens;
9983 parens.require_open (parser);
9986 type_id_in_expr_sentinel s (parser);
9987 type1 = cp_parser_type_id (parser);
9990 if (type1 == error_mark_node)
9991 return error_mark_node;
9993 if (binary)
9995 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9998 type_id_in_expr_sentinel s (parser);
9999 type2 = cp_parser_type_id (parser);
10002 if (type2 == error_mark_node)
10003 return error_mark_node;
10005 else if (variadic)
10007 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10009 cp_lexer_consume_token (parser->lexer);
10010 tree elt = cp_parser_type_id (parser);
10011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10013 cp_lexer_consume_token (parser->lexer);
10014 elt = make_pack_expansion (elt);
10016 if (elt == error_mark_node)
10017 return error_mark_node;
10018 type2 = tree_cons (NULL_TREE, elt, type2);
10022 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10023 parens.require_close (parser);
10025 /* Construct a location of the form:
10026 __is_trivially_copyable(_Tp)
10027 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10028 with start == caret, finishing at the close-paren. */
10029 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10031 /* Complete the trait expression, which may mean either processing
10032 the trait expr now or saving it for template instantiation. */
10033 switch (kind)
10035 case CPTK_UNDERLYING_TYPE:
10036 return cp_expr (finish_underlying_type (type1), trait_loc);
10037 case CPTK_BASES:
10038 return cp_expr (finish_bases (type1, false), trait_loc);
10039 case CPTK_DIRECT_BASES:
10040 return cp_expr (finish_bases (type1, true), trait_loc);
10041 default:
10042 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10046 /* Parse a lambda expression.
10048 lambda-expression:
10049 lambda-introducer lambda-declarator [opt] compound-statement
10051 Returns a representation of the expression. */
10053 static cp_expr
10054 cp_parser_lambda_expression (cp_parser* parser)
10056 tree lambda_expr = build_lambda_expr ();
10057 tree type;
10058 bool ok = true;
10059 cp_token *token = cp_lexer_peek_token (parser->lexer);
10060 cp_token_position start = 0;
10062 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10064 if (cp_unevaluated_operand)
10066 if (!token->error_reported)
10068 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10069 "lambda-expression in unevaluated context");
10070 token->error_reported = true;
10072 ok = false;
10074 else if (parser->in_template_argument_list_p)
10076 if (!token->error_reported)
10078 error_at (token->location, "lambda-expression in template-argument");
10079 token->error_reported = true;
10081 ok = false;
10084 /* We may be in the middle of deferred access check. Disable
10085 it now. */
10086 push_deferring_access_checks (dk_no_deferred);
10088 cp_parser_lambda_introducer (parser, lambda_expr);
10090 type = begin_lambda_type (lambda_expr);
10091 if (type == error_mark_node)
10092 return error_mark_node;
10094 record_lambda_scope (lambda_expr);
10096 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10097 determine_visibility (TYPE_NAME (type));
10099 /* Now that we've started the type, add the capture fields for any
10100 explicit captures. */
10101 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10104 /* Inside the class, surrounding template-parameter-lists do not apply. */
10105 unsigned int saved_num_template_parameter_lists
10106 = parser->num_template_parameter_lists;
10107 unsigned char in_statement = parser->in_statement;
10108 bool in_switch_statement_p = parser->in_switch_statement_p;
10109 bool fully_implicit_function_template_p
10110 = parser->fully_implicit_function_template_p;
10111 tree implicit_template_parms = parser->implicit_template_parms;
10112 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10113 bool auto_is_implicit_function_template_parm_p
10114 = parser->auto_is_implicit_function_template_parm_p;
10116 parser->num_template_parameter_lists = 0;
10117 parser->in_statement = 0;
10118 parser->in_switch_statement_p = false;
10119 parser->fully_implicit_function_template_p = false;
10120 parser->implicit_template_parms = 0;
10121 parser->implicit_template_scope = 0;
10122 parser->auto_is_implicit_function_template_parm_p = false;
10124 /* By virtue of defining a local class, a lambda expression has access to
10125 the private variables of enclosing classes. */
10127 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10129 if (ok && cp_parser_error_occurred (parser))
10130 ok = false;
10132 if (ok)
10134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10135 && cp_parser_start_tentative_firewall (parser))
10136 start = token;
10137 cp_parser_lambda_body (parser, lambda_expr);
10139 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10141 if (cp_parser_skip_to_closing_brace (parser))
10142 cp_lexer_consume_token (parser->lexer);
10145 /* The capture list was built up in reverse order; fix that now. */
10146 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10147 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10149 if (ok)
10150 maybe_add_lambda_conv_op (type);
10152 type = finish_struct (type, /*attributes=*/NULL_TREE);
10154 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10155 parser->in_statement = in_statement;
10156 parser->in_switch_statement_p = in_switch_statement_p;
10157 parser->fully_implicit_function_template_p
10158 = fully_implicit_function_template_p;
10159 parser->implicit_template_parms = implicit_template_parms;
10160 parser->implicit_template_scope = implicit_template_scope;
10161 parser->auto_is_implicit_function_template_parm_p
10162 = auto_is_implicit_function_template_parm_p;
10165 /* This field is only used during parsing of the lambda. */
10166 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10168 /* This lambda shouldn't have any proxies left at this point. */
10169 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10170 /* And now that we're done, push proxies for an enclosing lambda. */
10171 insert_pending_capture_proxies ();
10173 if (ok)
10174 lambda_expr = build_lambda_object (lambda_expr);
10175 else
10176 lambda_expr = error_mark_node;
10178 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10180 pop_deferring_access_checks ();
10182 return lambda_expr;
10185 /* Parse the beginning of a lambda expression.
10187 lambda-introducer:
10188 [ lambda-capture [opt] ]
10190 LAMBDA_EXPR is the current representation of the lambda expression. */
10192 static void
10193 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10195 /* Need commas after the first capture. */
10196 bool first = true;
10198 /* Eat the leading `['. */
10199 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10201 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10202 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10203 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10205 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10206 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10208 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10210 cp_lexer_consume_token (parser->lexer);
10211 first = false;
10214 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10216 cp_token* capture_token;
10217 tree capture_id;
10218 tree capture_init_expr;
10219 cp_id_kind idk = CP_ID_KIND_NONE;
10220 bool explicit_init_p = false;
10222 enum capture_kind_type
10224 BY_COPY,
10225 BY_REFERENCE
10227 enum capture_kind_type capture_kind = BY_COPY;
10229 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10231 error ("expected end of capture-list");
10232 return;
10235 if (first)
10236 first = false;
10237 else
10238 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10240 /* Possibly capture `this'. */
10241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10243 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10244 if (cxx_dialect < cxx2a
10245 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10246 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10247 "with by-copy capture default");
10248 cp_lexer_consume_token (parser->lexer);
10249 add_capture (lambda_expr,
10250 /*id=*/this_identifier,
10251 /*initializer=*/finish_this_expr (),
10252 /*by_reference_p=*/true,
10253 explicit_init_p);
10254 continue;
10257 /* Possibly capture `*this'. */
10258 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10259 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10261 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10262 if (cxx_dialect < cxx17)
10263 pedwarn (loc, 0, "%<*this%> capture only available with "
10264 "-std=c++17 or -std=gnu++17");
10265 cp_lexer_consume_token (parser->lexer);
10266 cp_lexer_consume_token (parser->lexer);
10267 add_capture (lambda_expr,
10268 /*id=*/this_identifier,
10269 /*initializer=*/finish_this_expr (),
10270 /*by_reference_p=*/false,
10271 explicit_init_p);
10272 continue;
10275 /* Remember whether we want to capture as a reference or not. */
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10278 capture_kind = BY_REFERENCE;
10279 cp_lexer_consume_token (parser->lexer);
10282 /* Get the identifier. */
10283 capture_token = cp_lexer_peek_token (parser->lexer);
10284 capture_id = cp_parser_identifier (parser);
10286 if (capture_id == error_mark_node)
10287 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10288 delimiters, but I modified this to stop on unnested ']' as well. It
10289 was already changed to stop on unnested '}', so the
10290 "closing_parenthesis" name is no more misleading with my change. */
10292 cp_parser_skip_to_closing_parenthesis (parser,
10293 /*recovering=*/true,
10294 /*or_comma=*/true,
10295 /*consume_paren=*/true);
10296 break;
10299 /* Find the initializer for this capture. */
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10301 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10302 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10304 bool direct, non_constant;
10305 /* An explicit initializer exists. */
10306 if (cxx_dialect < cxx14)
10307 pedwarn (input_location, 0,
10308 "lambda capture initializers "
10309 "only available with -std=c++14 or -std=gnu++14");
10310 capture_init_expr = cp_parser_initializer (parser, &direct,
10311 &non_constant);
10312 explicit_init_p = true;
10313 if (capture_init_expr == NULL_TREE)
10315 error ("empty initializer for lambda init-capture");
10316 capture_init_expr = error_mark_node;
10319 else
10321 const char* error_msg;
10323 /* Turn the identifier into an id-expression. */
10324 capture_init_expr
10325 = cp_parser_lookup_name_simple (parser, capture_id,
10326 capture_token->location);
10328 if (capture_init_expr == error_mark_node)
10330 unqualified_name_lookup_error (capture_id);
10331 continue;
10333 else if (DECL_P (capture_init_expr)
10334 && (!VAR_P (capture_init_expr)
10335 && TREE_CODE (capture_init_expr) != PARM_DECL))
10337 error_at (capture_token->location,
10338 "capture of non-variable %qD ",
10339 capture_init_expr);
10340 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10341 "%q#D declared here", capture_init_expr);
10342 continue;
10344 if (VAR_P (capture_init_expr)
10345 && decl_storage_duration (capture_init_expr) != dk_auto)
10347 if (pedwarn (capture_token->location, 0, "capture of variable "
10348 "%qD with non-automatic storage duration",
10349 capture_init_expr))
10350 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10351 "%q#D declared here", capture_init_expr);
10352 continue;
10355 capture_init_expr
10356 = finish_id_expression
10357 (capture_id,
10358 capture_init_expr,
10359 parser->scope,
10360 &idk,
10361 /*integral_constant_expression_p=*/false,
10362 /*allow_non_integral_constant_expression_p=*/false,
10363 /*non_integral_constant_expression_p=*/NULL,
10364 /*template_p=*/false,
10365 /*done=*/true,
10366 /*address_p=*/false,
10367 /*template_arg_p=*/false,
10368 &error_msg,
10369 capture_token->location);
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10373 cp_lexer_consume_token (parser->lexer);
10374 capture_init_expr = make_pack_expansion (capture_init_expr);
10376 else
10377 check_for_bare_parameter_packs (capture_init_expr);
10380 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10381 && !explicit_init_p)
10383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10384 && capture_kind == BY_COPY)
10385 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10386 "of %qD redundant with by-copy capture default",
10387 capture_id);
10388 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10389 && capture_kind == BY_REFERENCE)
10390 pedwarn (capture_token->location, 0, "explicit by-reference "
10391 "capture of %qD redundant with by-reference capture "
10392 "default", capture_id);
10395 add_capture (lambda_expr,
10396 capture_id,
10397 capture_init_expr,
10398 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10399 explicit_init_p);
10402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10405 /* Parse the (optional) middle of a lambda expression.
10407 lambda-declarator:
10408 < template-parameter-list [opt] >
10409 ( parameter-declaration-clause [opt] )
10410 attribute-specifier [opt]
10411 decl-specifier-seq [opt]
10412 exception-specification [opt]
10413 lambda-return-type-clause [opt]
10415 LAMBDA_EXPR is the current representation of the lambda expression. */
10417 static bool
10418 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10420 /* 5.1.1.4 of the standard says:
10421 If a lambda-expression does not include a lambda-declarator, it is as if
10422 the lambda-declarator were ().
10423 This means an empty parameter list, no attributes, and no exception
10424 specification. */
10425 tree param_list = void_list_node;
10426 tree attributes = NULL_TREE;
10427 tree exception_spec = NULL_TREE;
10428 tree template_param_list = NULL_TREE;
10429 tree tx_qual = NULL_TREE;
10430 tree return_type = NULL_TREE;
10431 cp_decl_specifier_seq lambda_specs;
10432 clear_decl_specs (&lambda_specs);
10434 /* The template-parameter-list is optional, but must begin with
10435 an opening angle if present. */
10436 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10438 if (cxx_dialect < cxx14)
10439 pedwarn (parser->lexer->next_token->location, 0,
10440 "lambda templates are only available with "
10441 "-std=c++14 or -std=gnu++14");
10442 else if (cxx_dialect < cxx2a)
10443 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10444 "lambda templates are only available with "
10445 "-std=c++2a or -std=gnu++2a");
10447 cp_lexer_consume_token (parser->lexer);
10449 template_param_list = cp_parser_template_parameter_list (parser);
10451 cp_parser_skip_to_end_of_template_parameter_list (parser);
10453 /* We just processed one more parameter list. */
10454 ++parser->num_template_parameter_lists;
10457 /* The parameter-declaration-clause is optional (unless
10458 template-parameter-list was given), but must begin with an
10459 opening parenthesis if present. */
10460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10462 matching_parens parens;
10463 parens.consume_open (parser);
10465 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10467 /* Parse parameters. */
10468 param_list = cp_parser_parameter_declaration_clause (parser);
10470 /* Default arguments shall not be specified in the
10471 parameter-declaration-clause of a lambda-declarator. */
10472 if (cxx_dialect < cxx14)
10473 for (tree t = param_list; t; t = TREE_CHAIN (t))
10474 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10475 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10476 "default argument specified for lambda parameter");
10478 parens.require_close (parser);
10480 attributes = cp_parser_attributes_opt (parser);
10482 /* In the decl-specifier-seq of the lambda-declarator, each
10483 decl-specifier shall either be mutable or constexpr. */
10484 int declares_class_or_enum;
10485 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10486 cp_parser_decl_specifier_seq (parser,
10487 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10488 &lambda_specs, &declares_class_or_enum);
10489 if (lambda_specs.storage_class == sc_mutable)
10491 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10492 if (lambda_specs.conflicting_specifiers_p)
10493 error_at (lambda_specs.locations[ds_storage_class],
10494 "duplicate %<mutable%>");
10497 tx_qual = cp_parser_tx_qualifier_opt (parser);
10499 /* Parse optional exception specification. */
10500 exception_spec = cp_parser_exception_specification_opt (parser);
10502 /* Parse optional trailing return type. */
10503 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10505 cp_lexer_consume_token (parser->lexer);
10506 return_type = cp_parser_trailing_type_id (parser);
10509 /* The function parameters must be in scope all the way until after the
10510 trailing-return-type in case of decltype. */
10511 pop_bindings_and_leave_scope ();
10513 else if (template_param_list != NULL_TREE) // generate diagnostic
10514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10516 /* Create the function call operator.
10518 Messing with declarators like this is no uglier than building up the
10519 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10520 other code. */
10522 cp_decl_specifier_seq return_type_specs;
10523 cp_declarator* declarator;
10524 tree fco;
10525 int quals;
10526 void *p;
10528 clear_decl_specs (&return_type_specs);
10529 if (return_type)
10530 return_type_specs.type = return_type;
10531 else
10532 /* Maybe we will deduce the return type later. */
10533 return_type_specs.type = make_auto ();
10535 if (lambda_specs.locations[ds_constexpr])
10537 if (cxx_dialect >= cxx17)
10538 return_type_specs.locations[ds_constexpr]
10539 = lambda_specs.locations[ds_constexpr];
10540 else
10541 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10542 "lambda only available with -std=c++17 or -std=gnu++17");
10545 p = obstack_alloc (&declarator_obstack, 0);
10547 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10549 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10550 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10551 declarator = make_call_declarator (declarator, param_list, quals,
10552 VIRT_SPEC_UNSPECIFIED,
10553 REF_QUAL_NONE,
10554 tx_qual,
10555 exception_spec,
10556 /*late_return_type=*/NULL_TREE,
10557 /*requires_clause*/NULL_TREE);
10558 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10560 fco = grokmethod (&return_type_specs,
10561 declarator,
10562 attributes);
10563 if (fco != error_mark_node)
10565 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10566 DECL_ARTIFICIAL (fco) = 1;
10567 /* Give the object parameter a different name. */
10568 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10569 if (return_type)
10570 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10572 if (template_param_list)
10574 fco = finish_member_template_decl (fco);
10575 finish_template_decl (template_param_list);
10576 --parser->num_template_parameter_lists;
10578 else if (parser->fully_implicit_function_template_p)
10579 fco = finish_fully_implicit_template (parser, fco);
10581 finish_member_declaration (fco);
10583 obstack_free (&declarator_obstack, p);
10585 return (fco != error_mark_node);
10589 /* Parse the body of a lambda expression, which is simply
10591 compound-statement
10593 but which requires special handling.
10594 LAMBDA_EXPR is the current representation of the lambda expression. */
10596 static void
10597 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10599 bool nested = (current_function_decl != NULL_TREE);
10600 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10601 bool in_function_body = parser->in_function_body;
10603 if (nested)
10604 push_function_context ();
10605 else
10606 /* Still increment function_depth so that we don't GC in the
10607 middle of an expression. */
10608 ++function_depth;
10610 vec<tree> omp_privatization_save;
10611 save_omp_privatization_clauses (omp_privatization_save);
10612 /* Clear this in case we're in the middle of a default argument. */
10613 parser->local_variables_forbidden_p = false;
10614 parser->in_function_body = true;
10617 local_specialization_stack s (lss_copy);
10618 tree fco = lambda_function (lambda_expr);
10619 tree body = start_lambda_function (fco, lambda_expr);
10620 matching_braces braces;
10622 if (braces.require_open (parser))
10624 tree compound_stmt = begin_compound_stmt (0);
10626 /* Originally C++11 required us to peek for 'return expr'; and
10627 process it specially here to deduce the return type. N3638
10628 removed the need for that. */
10630 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10631 cp_parser_label_declaration (parser);
10632 cp_parser_statement_seq_opt (parser, NULL_TREE);
10633 braces.require_close (parser);
10635 finish_compound_stmt (compound_stmt);
10638 finish_lambda_function (body);
10641 restore_omp_privatization_clauses (omp_privatization_save);
10642 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10643 parser->in_function_body = in_function_body;
10644 if (nested)
10645 pop_function_context();
10646 else
10647 --function_depth;
10650 /* Statements [gram.stmt.stmt] */
10652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10654 static void
10655 add_debug_begin_stmt (location_t loc)
10657 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10658 return;
10660 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10661 SET_EXPR_LOCATION (stmt, loc);
10662 add_stmt (stmt);
10665 /* Parse a statement.
10667 statement:
10668 labeled-statement
10669 expression-statement
10670 compound-statement
10671 selection-statement
10672 iteration-statement
10673 jump-statement
10674 declaration-statement
10675 try-block
10677 C++11:
10679 statement:
10680 labeled-statement
10681 attribute-specifier-seq (opt) expression-statement
10682 attribute-specifier-seq (opt) compound-statement
10683 attribute-specifier-seq (opt) selection-statement
10684 attribute-specifier-seq (opt) iteration-statement
10685 attribute-specifier-seq (opt) jump-statement
10686 declaration-statement
10687 attribute-specifier-seq (opt) try-block
10689 init-statement:
10690 expression-statement
10691 simple-declaration
10693 TM Extension:
10695 statement:
10696 atomic-statement
10698 IN_COMPOUND is true when the statement is nested inside a
10699 cp_parser_compound_statement; this matters for certain pragmas.
10701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10702 is a (possibly labeled) if statement which is not enclosed in braces
10703 and has an else clause. This is used to implement -Wparentheses.
10705 CHAIN is a vector of if-else-if conditions. */
10707 static void
10708 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10709 bool in_compound, bool *if_p, vec<tree> *chain,
10710 location_t *loc_after_labels)
10712 tree statement, std_attrs = NULL_TREE;
10713 cp_token *token;
10714 location_t statement_location, attrs_location;
10716 restart:
10717 if (if_p != NULL)
10718 *if_p = false;
10719 /* There is no statement yet. */
10720 statement = NULL_TREE;
10722 saved_token_sentinel saved_tokens (parser->lexer);
10723 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10724 if (c_dialect_objc ())
10725 /* In obj-c++, seeing '[[' might be the either the beginning of
10726 c++11 attributes, or a nested objc-message-expression. So
10727 let's parse the c++11 attributes tentatively. */
10728 cp_parser_parse_tentatively (parser);
10729 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10730 if (c_dialect_objc ())
10732 if (!cp_parser_parse_definitely (parser))
10733 std_attrs = NULL_TREE;
10736 /* Peek at the next token. */
10737 token = cp_lexer_peek_token (parser->lexer);
10738 /* Remember the location of the first token in the statement. */
10739 statement_location = token->location;
10740 add_debug_begin_stmt (statement_location);
10741 /* If this is a keyword, then that will often determine what kind of
10742 statement we have. */
10743 if (token->type == CPP_KEYWORD)
10745 enum rid keyword = token->keyword;
10747 switch (keyword)
10749 case RID_CASE:
10750 case RID_DEFAULT:
10751 /* Looks like a labeled-statement with a case label.
10752 Parse the label, and then use tail recursion to parse
10753 the statement. */
10754 cp_parser_label_for_labeled_statement (parser, std_attrs);
10755 in_compound = false;
10756 goto restart;
10758 case RID_IF:
10759 case RID_SWITCH:
10760 statement = cp_parser_selection_statement (parser, if_p, chain);
10761 break;
10763 case RID_WHILE:
10764 case RID_DO:
10765 case RID_FOR:
10766 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10767 break;
10769 case RID_BREAK:
10770 case RID_CONTINUE:
10771 case RID_RETURN:
10772 case RID_GOTO:
10773 statement = cp_parser_jump_statement (parser);
10774 break;
10776 /* Objective-C++ exception-handling constructs. */
10777 case RID_AT_TRY:
10778 case RID_AT_CATCH:
10779 case RID_AT_FINALLY:
10780 case RID_AT_SYNCHRONIZED:
10781 case RID_AT_THROW:
10782 statement = cp_parser_objc_statement (parser);
10783 break;
10785 case RID_TRY:
10786 statement = cp_parser_try_block (parser);
10787 break;
10789 case RID_NAMESPACE:
10790 /* This must be a namespace alias definition. */
10791 cp_parser_declaration_statement (parser);
10792 return;
10794 case RID_TRANSACTION_ATOMIC:
10795 case RID_TRANSACTION_RELAXED:
10796 case RID_SYNCHRONIZED:
10797 case RID_ATOMIC_NOEXCEPT:
10798 case RID_ATOMIC_CANCEL:
10799 statement = cp_parser_transaction (parser, token);
10800 break;
10801 case RID_TRANSACTION_CANCEL:
10802 statement = cp_parser_transaction_cancel (parser);
10803 break;
10805 default:
10806 /* It might be a keyword like `int' that can start a
10807 declaration-statement. */
10808 break;
10811 else if (token->type == CPP_NAME)
10813 /* If the next token is a `:', then we are looking at a
10814 labeled-statement. */
10815 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10816 if (token->type == CPP_COLON)
10818 /* Looks like a labeled-statement with an ordinary label.
10819 Parse the label, and then use tail recursion to parse
10820 the statement. */
10822 cp_parser_label_for_labeled_statement (parser, std_attrs);
10823 in_compound = false;
10824 goto restart;
10827 /* Anything that starts with a `{' must be a compound-statement. */
10828 else if (token->type == CPP_OPEN_BRACE)
10829 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10830 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10831 a statement all its own. */
10832 else if (token->type == CPP_PRAGMA)
10834 /* Only certain OpenMP pragmas are attached to statements, and thus
10835 are considered statements themselves. All others are not. In
10836 the context of a compound, accept the pragma as a "statement" and
10837 return so that we can check for a close brace. Otherwise we
10838 require a real statement and must go back and read one. */
10839 if (in_compound)
10840 cp_parser_pragma (parser, pragma_compound, if_p);
10841 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10842 goto restart;
10843 return;
10845 else if (token->type == CPP_EOF)
10847 cp_parser_error (parser, "expected statement");
10848 return;
10851 /* Everything else must be a declaration-statement or an
10852 expression-statement. Try for the declaration-statement
10853 first, unless we are looking at a `;', in which case we know that
10854 we have an expression-statement. */
10855 if (!statement)
10857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10859 if (std_attrs != NULL_TREE)
10861 /* Attributes should be parsed as part of the the
10862 declaration, so let's un-parse them. */
10863 saved_tokens.rollback();
10864 std_attrs = NULL_TREE;
10867 cp_parser_parse_tentatively (parser);
10868 /* Try to parse the declaration-statement. */
10869 cp_parser_declaration_statement (parser);
10870 /* If that worked, we're done. */
10871 if (cp_parser_parse_definitely (parser))
10872 return;
10874 /* All preceding labels have been parsed at this point. */
10875 if (loc_after_labels != NULL)
10876 *loc_after_labels = statement_location;
10878 /* Look for an expression-statement instead. */
10879 statement = cp_parser_expression_statement (parser, in_statement_expr);
10881 /* Handle [[fallthrough]];. */
10882 if (attribute_fallthrough_p (std_attrs))
10884 /* The next token after the fallthrough attribute is ';'. */
10885 if (statement == NULL_TREE)
10887 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10888 statement = build_call_expr_internal_loc (statement_location,
10889 IFN_FALLTHROUGH,
10890 void_type_node, 0);
10891 finish_expr_stmt (statement);
10893 else
10894 warning_at (statement_location, OPT_Wattributes,
10895 "%<fallthrough%> attribute not followed by %<;%>");
10896 std_attrs = NULL_TREE;
10900 /* Set the line number for the statement. */
10901 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10902 SET_EXPR_LOCATION (statement, statement_location);
10904 /* Allow "[[fallthrough]];", but warn otherwise. */
10905 if (std_attrs != NULL_TREE)
10906 warning_at (attrs_location,
10907 OPT_Wattributes,
10908 "attributes at the beginning of statement are ignored");
10911 /* Parse the label for a labeled-statement, i.e.
10913 identifier :
10914 case constant-expression :
10915 default :
10917 GNU Extension:
10918 case constant-expression ... constant-expression : statement
10920 When a label is parsed without errors, the label is added to the
10921 parse tree by the finish_* functions, so this function doesn't
10922 have to return the label. */
10924 static void
10925 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10927 cp_token *token;
10928 tree label = NULL_TREE;
10929 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10931 /* The next token should be an identifier. */
10932 token = cp_lexer_peek_token (parser->lexer);
10933 if (token->type != CPP_NAME
10934 && token->type != CPP_KEYWORD)
10936 cp_parser_error (parser, "expected labeled-statement");
10937 return;
10940 /* Remember whether this case or a user-defined label is allowed to fall
10941 through to. */
10942 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10944 parser->colon_corrects_to_scope_p = false;
10945 switch (token->keyword)
10947 case RID_CASE:
10949 tree expr, expr_hi;
10950 cp_token *ellipsis;
10952 /* Consume the `case' token. */
10953 cp_lexer_consume_token (parser->lexer);
10954 /* Parse the constant-expression. */
10955 expr = cp_parser_constant_expression (parser);
10956 if (check_for_bare_parameter_packs (expr))
10957 expr = error_mark_node;
10959 ellipsis = cp_lexer_peek_token (parser->lexer);
10960 if (ellipsis->type == CPP_ELLIPSIS)
10962 /* Consume the `...' token. */
10963 cp_lexer_consume_token (parser->lexer);
10964 expr_hi = cp_parser_constant_expression (parser);
10965 if (check_for_bare_parameter_packs (expr_hi))
10966 expr_hi = error_mark_node;
10968 /* We don't need to emit warnings here, as the common code
10969 will do this for us. */
10971 else
10972 expr_hi = NULL_TREE;
10974 if (parser->in_switch_statement_p)
10976 tree l = finish_case_label (token->location, expr, expr_hi);
10977 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10978 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10980 else
10981 error_at (token->location,
10982 "case label %qE not within a switch statement",
10983 expr);
10985 break;
10987 case RID_DEFAULT:
10988 /* Consume the `default' token. */
10989 cp_lexer_consume_token (parser->lexer);
10991 if (parser->in_switch_statement_p)
10993 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10994 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10995 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10997 else
10998 error_at (token->location, "case label not within a switch statement");
10999 break;
11001 default:
11002 /* Anything else must be an ordinary label. */
11003 label = finish_label_stmt (cp_parser_identifier (parser));
11004 if (label && TREE_CODE (label) == LABEL_DECL)
11005 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11006 break;
11009 /* Require the `:' token. */
11010 cp_parser_require (parser, CPP_COLON, RT_COLON);
11012 /* An ordinary label may optionally be followed by attributes.
11013 However, this is only permitted if the attributes are then
11014 followed by a semicolon. This is because, for backward
11015 compatibility, when parsing
11016 lab: __attribute__ ((unused)) int i;
11017 we want the attribute to attach to "i", not "lab". */
11018 if (label != NULL_TREE
11019 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11021 tree attrs;
11022 cp_parser_parse_tentatively (parser);
11023 attrs = cp_parser_gnu_attributes_opt (parser);
11024 if (attrs == NULL_TREE
11025 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11026 cp_parser_abort_tentative_parse (parser);
11027 else if (!cp_parser_parse_definitely (parser))
11029 else
11030 attributes = chainon (attributes, attrs);
11033 if (attributes != NULL_TREE)
11034 cplus_decl_attributes (&label, attributes, 0);
11036 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11039 /* Parse an expression-statement.
11041 expression-statement:
11042 expression [opt] ;
11044 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11045 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11046 indicates whether this expression-statement is part of an
11047 expression statement. */
11049 static tree
11050 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11052 tree statement = NULL_TREE;
11053 cp_token *token = cp_lexer_peek_token (parser->lexer);
11054 location_t loc = token->location;
11056 /* There might be attribute fallthrough. */
11057 tree attr = cp_parser_gnu_attributes_opt (parser);
11059 /* If the next token is a ';', then there is no expression
11060 statement. */
11061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11063 statement = cp_parser_expression (parser);
11064 if (statement == error_mark_node
11065 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11067 cp_parser_skip_to_end_of_block_or_statement (parser);
11068 return error_mark_node;
11072 /* Handle [[fallthrough]];. */
11073 if (attribute_fallthrough_p (attr))
11075 /* The next token after the fallthrough attribute is ';'. */
11076 if (statement == NULL_TREE)
11077 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11078 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11079 void_type_node, 0);
11080 else
11081 warning_at (loc, OPT_Wattributes,
11082 "%<fallthrough%> attribute not followed by %<;%>");
11083 attr = NULL_TREE;
11086 /* Allow "[[fallthrough]];", but warn otherwise. */
11087 if (attr != NULL_TREE)
11088 warning_at (loc, OPT_Wattributes,
11089 "attributes at the beginning of statement are ignored");
11091 /* Give a helpful message for "A<T>::type t;" and the like. */
11092 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11093 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11095 if (TREE_CODE (statement) == SCOPE_REF)
11096 error_at (token->location, "need %<typename%> before %qE because "
11097 "%qT is a dependent scope",
11098 statement, TREE_OPERAND (statement, 0));
11099 else if (is_overloaded_fn (statement)
11100 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11102 /* A::A a; */
11103 tree fn = get_first_fn (statement);
11104 error_at (token->location,
11105 "%<%T::%D%> names the constructor, not the type",
11106 DECL_CONTEXT (fn), DECL_NAME (fn));
11110 /* Consume the final `;'. */
11111 cp_parser_consume_semicolon_at_end_of_statement (parser);
11113 if (in_statement_expr
11114 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11115 /* This is the final expression statement of a statement
11116 expression. */
11117 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11118 else if (statement)
11119 statement = finish_expr_stmt (statement);
11121 return statement;
11124 /* Parse a compound-statement.
11126 compound-statement:
11127 { statement-seq [opt] }
11129 GNU extension:
11131 compound-statement:
11132 { label-declaration-seq [opt] statement-seq [opt] }
11134 label-declaration-seq:
11135 label-declaration
11136 label-declaration-seq label-declaration
11138 Returns a tree representing the statement. */
11140 static tree
11141 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11142 int bcs_flags, bool function_body)
11144 tree compound_stmt;
11145 matching_braces braces;
11147 /* Consume the `{'. */
11148 if (!braces.require_open (parser))
11149 return error_mark_node;
11150 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11151 && !function_body && cxx_dialect < cxx14)
11152 pedwarn (input_location, OPT_Wpedantic,
11153 "compound-statement in %<constexpr%> function");
11154 /* Begin the compound-statement. */
11155 compound_stmt = begin_compound_stmt (bcs_flags);
11156 /* If the next keyword is `__label__' we have a label declaration. */
11157 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11158 cp_parser_label_declaration (parser);
11159 /* Parse an (optional) statement-seq. */
11160 cp_parser_statement_seq_opt (parser, in_statement_expr);
11161 /* Finish the compound-statement. */
11162 finish_compound_stmt (compound_stmt);
11163 /* Consume the `}'. */
11164 braces.require_close (parser);
11166 return compound_stmt;
11169 /* Parse an (optional) statement-seq.
11171 statement-seq:
11172 statement
11173 statement-seq [opt] statement */
11175 static void
11176 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11178 /* Scan statements until there aren't any more. */
11179 while (true)
11181 cp_token *token = cp_lexer_peek_token (parser->lexer);
11183 /* If we are looking at a `}', then we have run out of
11184 statements; the same is true if we have reached the end
11185 of file, or have stumbled upon a stray '@end'. */
11186 if (token->type == CPP_CLOSE_BRACE
11187 || token->type == CPP_EOF
11188 || token->type == CPP_PRAGMA_EOL
11189 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11190 break;
11192 /* If we are in a compound statement and find 'else' then
11193 something went wrong. */
11194 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11196 if (parser->in_statement & IN_IF_STMT)
11197 break;
11198 else
11200 token = cp_lexer_consume_token (parser->lexer);
11201 error_at (token->location, "%<else%> without a previous %<if%>");
11205 /* Parse the statement. */
11206 cp_parser_statement (parser, in_statement_expr, true, NULL);
11210 /* Return true if we're looking at (init; cond), false otherwise. */
11212 static bool
11213 cp_parser_init_statement_p (cp_parser *parser)
11215 /* Save tokens so that we can put them back. */
11216 cp_lexer_save_tokens (parser->lexer);
11218 /* Look for ';' that is not nested in () or {}. */
11219 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11220 /*recovering=*/false,
11221 CPP_SEMICOLON,
11222 /*consume_paren=*/false);
11224 /* Roll back the tokens we skipped. */
11225 cp_lexer_rollback_tokens (parser->lexer);
11227 return ret == -1;
11230 /* Parse a selection-statement.
11232 selection-statement:
11233 if ( init-statement [opt] condition ) statement
11234 if ( init-statement [opt] condition ) statement else statement
11235 switch ( init-statement [opt] condition ) statement
11237 Returns the new IF_STMT or SWITCH_STMT.
11239 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11240 is a (possibly labeled) if statement which is not enclosed in
11241 braces and has an else clause. This is used to implement
11242 -Wparentheses.
11244 CHAIN is a vector of if-else-if conditions. This is used to implement
11245 -Wduplicated-cond. */
11247 static tree
11248 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11249 vec<tree> *chain)
11251 cp_token *token;
11252 enum rid keyword;
11253 token_indent_info guard_tinfo;
11255 if (if_p != NULL)
11256 *if_p = false;
11258 /* Peek at the next token. */
11259 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11260 guard_tinfo = get_token_indent_info (token);
11262 /* See what kind of keyword it is. */
11263 keyword = token->keyword;
11264 switch (keyword)
11266 case RID_IF:
11267 case RID_SWITCH:
11269 tree statement;
11270 tree condition;
11272 bool cx = false;
11273 if (keyword == RID_IF
11274 && cp_lexer_next_token_is_keyword (parser->lexer,
11275 RID_CONSTEXPR))
11277 cx = true;
11278 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11279 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11280 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11281 "with -std=c++17 or -std=gnu++17");
11284 /* Look for the `('. */
11285 matching_parens parens;
11286 if (!parens.require_open (parser))
11288 cp_parser_skip_to_end_of_statement (parser);
11289 return error_mark_node;
11292 /* Begin the selection-statement. */
11293 if (keyword == RID_IF)
11295 statement = begin_if_stmt ();
11296 IF_STMT_CONSTEXPR_P (statement) = cx;
11298 else
11299 statement = begin_switch_stmt ();
11301 /* Parse the optional init-statement. */
11302 if (cp_parser_init_statement_p (parser))
11304 tree decl;
11305 if (cxx_dialect < cxx17)
11306 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11307 "init-statement in selection statements only available "
11308 "with -std=c++17 or -std=gnu++17");
11309 cp_parser_init_statement (parser, &decl);
11312 /* Parse the condition. */
11313 condition = cp_parser_condition (parser);
11314 /* Look for the `)'. */
11315 if (!parens.require_close (parser))
11316 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11317 /*consume_paren=*/true);
11319 if (keyword == RID_IF)
11321 bool nested_if;
11322 unsigned char in_statement;
11324 /* Add the condition. */
11325 condition = finish_if_stmt_cond (condition, statement);
11327 if (warn_duplicated_cond)
11328 warn_duplicated_cond_add_or_warn (token->location, condition,
11329 &chain);
11331 /* Parse the then-clause. */
11332 in_statement = parser->in_statement;
11333 parser->in_statement |= IN_IF_STMT;
11335 /* Outside a template, the non-selected branch of a constexpr
11336 if is a 'discarded statement', i.e. unevaluated. */
11337 bool was_discarded = in_discarded_stmt;
11338 bool discard_then = (cx && !processing_template_decl
11339 && integer_zerop (condition));
11340 if (discard_then)
11342 in_discarded_stmt = true;
11343 ++c_inhibit_evaluation_warnings;
11346 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11347 guard_tinfo);
11349 parser->in_statement = in_statement;
11351 finish_then_clause (statement);
11353 if (discard_then)
11355 THEN_CLAUSE (statement) = NULL_TREE;
11356 in_discarded_stmt = was_discarded;
11357 --c_inhibit_evaluation_warnings;
11360 /* If the next token is `else', parse the else-clause. */
11361 if (cp_lexer_next_token_is_keyword (parser->lexer,
11362 RID_ELSE))
11364 bool discard_else = (cx && !processing_template_decl
11365 && integer_nonzerop (condition));
11366 if (discard_else)
11368 in_discarded_stmt = true;
11369 ++c_inhibit_evaluation_warnings;
11372 guard_tinfo
11373 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11374 /* Consume the `else' keyword. */
11375 cp_lexer_consume_token (parser->lexer);
11376 if (warn_duplicated_cond)
11378 if (cp_lexer_next_token_is_keyword (parser->lexer,
11379 RID_IF)
11380 && chain == NULL)
11382 /* We've got "if (COND) else if (COND2)". Start
11383 the condition chain and add COND as the first
11384 element. */
11385 chain = new vec<tree> ();
11386 if (!CONSTANT_CLASS_P (condition)
11387 && !TREE_SIDE_EFFECTS (condition))
11389 /* Wrap it in a NOP_EXPR so that we can set the
11390 location of the condition. */
11391 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11392 condition);
11393 SET_EXPR_LOCATION (e, token->location);
11394 chain->safe_push (e);
11397 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11398 RID_IF))
11400 /* This is if-else without subsequent if. Zap the
11401 condition chain; we would have already warned at
11402 this point. */
11403 delete chain;
11404 chain = NULL;
11407 begin_else_clause (statement);
11408 /* Parse the else-clause. */
11409 cp_parser_implicitly_scoped_statement (parser, NULL,
11410 guard_tinfo, chain);
11412 finish_else_clause (statement);
11414 /* If we are currently parsing a then-clause, then
11415 IF_P will not be NULL. We set it to true to
11416 indicate that this if statement has an else clause.
11417 This may trigger the Wparentheses warning below
11418 when we get back up to the parent if statement. */
11419 if (if_p != NULL)
11420 *if_p = true;
11422 if (discard_else)
11424 ELSE_CLAUSE (statement) = NULL_TREE;
11425 in_discarded_stmt = was_discarded;
11426 --c_inhibit_evaluation_warnings;
11429 else
11431 /* This if statement does not have an else clause. If
11432 NESTED_IF is true, then the then-clause has an if
11433 statement which does have an else clause. We warn
11434 about the potential ambiguity. */
11435 if (nested_if)
11436 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11437 "suggest explicit braces to avoid ambiguous"
11438 " %<else%>");
11439 if (warn_duplicated_cond)
11441 /* We don't need the condition chain anymore. */
11442 delete chain;
11443 chain = NULL;
11447 /* Now we're all done with the if-statement. */
11448 finish_if_stmt (statement);
11450 else
11452 bool in_switch_statement_p;
11453 unsigned char in_statement;
11455 /* Add the condition. */
11456 finish_switch_cond (condition, statement);
11458 /* Parse the body of the switch-statement. */
11459 in_switch_statement_p = parser->in_switch_statement_p;
11460 in_statement = parser->in_statement;
11461 parser->in_switch_statement_p = true;
11462 parser->in_statement |= IN_SWITCH_STMT;
11463 cp_parser_implicitly_scoped_statement (parser, if_p,
11464 guard_tinfo);
11465 parser->in_switch_statement_p = in_switch_statement_p;
11466 parser->in_statement = in_statement;
11468 /* Now we're all done with the switch-statement. */
11469 finish_switch_stmt (statement);
11472 return statement;
11474 break;
11476 default:
11477 cp_parser_error (parser, "expected selection-statement");
11478 return error_mark_node;
11482 /* Parse a condition.
11484 condition:
11485 expression
11486 type-specifier-seq declarator = initializer-clause
11487 type-specifier-seq declarator braced-init-list
11489 GNU Extension:
11491 condition:
11492 type-specifier-seq declarator asm-specification [opt]
11493 attributes [opt] = assignment-expression
11495 Returns the expression that should be tested. */
11497 static tree
11498 cp_parser_condition (cp_parser* parser)
11500 cp_decl_specifier_seq type_specifiers;
11501 const char *saved_message;
11502 int declares_class_or_enum;
11504 /* Try the declaration first. */
11505 cp_parser_parse_tentatively (parser);
11506 /* New types are not allowed in the type-specifier-seq for a
11507 condition. */
11508 saved_message = parser->type_definition_forbidden_message;
11509 parser->type_definition_forbidden_message
11510 = G_("types may not be defined in conditions");
11511 /* Parse the type-specifier-seq. */
11512 cp_parser_decl_specifier_seq (parser,
11513 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11514 &type_specifiers,
11515 &declares_class_or_enum);
11516 /* Restore the saved message. */
11517 parser->type_definition_forbidden_message = saved_message;
11518 /* If all is well, we might be looking at a declaration. */
11519 if (!cp_parser_error_occurred (parser))
11521 tree decl;
11522 tree asm_specification;
11523 tree attributes;
11524 cp_declarator *declarator;
11525 tree initializer = NULL_TREE;
11527 /* Parse the declarator. */
11528 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11529 /*ctor_dtor_or_conv_p=*/NULL,
11530 /*parenthesized_p=*/NULL,
11531 /*member_p=*/false,
11532 /*friend_p=*/false);
11533 /* Parse the attributes. */
11534 attributes = cp_parser_attributes_opt (parser);
11535 /* Parse the asm-specification. */
11536 asm_specification = cp_parser_asm_specification_opt (parser);
11537 /* If the next token is not an `=' or '{', then we might still be
11538 looking at an expression. For example:
11540 if (A(a).x)
11542 looks like a decl-specifier-seq and a declarator -- but then
11543 there is no `=', so this is an expression. */
11544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11545 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11546 cp_parser_simulate_error (parser);
11548 /* If we did see an `=' or '{', then we are looking at a declaration
11549 for sure. */
11550 if (cp_parser_parse_definitely (parser))
11552 tree pushed_scope;
11553 bool non_constant_p;
11554 int flags = LOOKUP_ONLYCONVERTING;
11556 /* Create the declaration. */
11557 decl = start_decl (declarator, &type_specifiers,
11558 /*initialized_p=*/true,
11559 attributes, /*prefix_attributes=*/NULL_TREE,
11560 &pushed_scope);
11562 /* Parse the initializer. */
11563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11565 initializer = cp_parser_braced_list (parser, &non_constant_p);
11566 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11567 flags = 0;
11569 else
11571 /* Consume the `='. */
11572 cp_parser_require (parser, CPP_EQ, RT_EQ);
11573 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11575 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11576 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11578 /* Process the initializer. */
11579 cp_finish_decl (decl,
11580 initializer, !non_constant_p,
11581 asm_specification,
11582 flags);
11584 if (pushed_scope)
11585 pop_scope (pushed_scope);
11587 return convert_from_reference (decl);
11590 /* If we didn't even get past the declarator successfully, we are
11591 definitely not looking at a declaration. */
11592 else
11593 cp_parser_abort_tentative_parse (parser);
11595 /* Otherwise, we are looking at an expression. */
11596 return cp_parser_expression (parser);
11599 /* Parses a for-statement or range-for-statement until the closing ')',
11600 not included. */
11602 static tree
11603 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11605 tree init, scope, decl;
11606 bool is_range_for;
11608 /* Begin the for-statement. */
11609 scope = begin_for_scope (&init);
11611 /* Parse the initialization. */
11612 is_range_for = cp_parser_init_statement (parser, &decl);
11614 if (is_range_for)
11615 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11616 else
11617 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11620 static tree
11621 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11622 unsigned short unroll)
11624 /* Normal for loop */
11625 tree condition = NULL_TREE;
11626 tree expression = NULL_TREE;
11627 tree stmt;
11629 stmt = begin_for_stmt (scope, init);
11630 /* The init-statement has already been parsed in
11631 cp_parser_init_statement, so no work is needed here. */
11632 finish_init_stmt (stmt);
11634 /* If there's a condition, process it. */
11635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11636 condition = cp_parser_condition (parser);
11637 else if (ivdep)
11639 cp_parser_error (parser, "missing loop condition in loop with "
11640 "%<GCC ivdep%> pragma");
11641 condition = error_mark_node;
11643 else if (unroll)
11645 cp_parser_error (parser, "missing loop condition in loop with "
11646 "%<GCC unroll%> pragma");
11647 condition = error_mark_node;
11649 finish_for_cond (condition, stmt, ivdep, unroll);
11650 /* Look for the `;'. */
11651 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11653 /* If there's an expression, process it. */
11654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11655 expression = cp_parser_expression (parser);
11656 finish_for_expr (expression, stmt);
11658 return stmt;
11661 /* Tries to parse a range-based for-statement:
11663 range-based-for:
11664 decl-specifier-seq declarator : expression
11666 The decl-specifier-seq declarator and the `:' are already parsed by
11667 cp_parser_init_statement. If processing_template_decl it returns a
11668 newly created RANGE_FOR_STMT; if not, it is converted to a
11669 regular FOR_STMT. */
11671 static tree
11672 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11673 bool ivdep, unsigned short unroll)
11675 tree stmt, range_expr;
11676 auto_vec <cxx_binding *, 16> bindings;
11677 auto_vec <tree, 16> names;
11678 tree decomp_first_name = NULL_TREE;
11679 unsigned int decomp_cnt = 0;
11681 /* Get the range declaration momentarily out of the way so that
11682 the range expression doesn't clash with it. */
11683 if (range_decl != error_mark_node)
11685 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11687 tree v = DECL_VALUE_EXPR (range_decl);
11688 /* For decomposition declaration get all of the corresponding
11689 declarations out of the way. */
11690 if (TREE_CODE (v) == ARRAY_REF
11691 && VAR_P (TREE_OPERAND (v, 0))
11692 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11694 tree d = range_decl;
11695 range_decl = TREE_OPERAND (v, 0);
11696 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11697 decomp_first_name = d;
11698 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11700 tree name = DECL_NAME (d);
11701 names.safe_push (name);
11702 bindings.safe_push (IDENTIFIER_BINDING (name));
11703 IDENTIFIER_BINDING (name)
11704 = IDENTIFIER_BINDING (name)->previous;
11708 if (names.is_empty ())
11710 tree name = DECL_NAME (range_decl);
11711 names.safe_push (name);
11712 bindings.safe_push (IDENTIFIER_BINDING (name));
11713 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11719 bool expr_non_constant_p;
11720 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11722 else
11723 range_expr = cp_parser_expression (parser);
11725 /* Put the range declaration(s) back into scope. */
11726 for (unsigned int i = 0; i < names.length (); i++)
11728 cxx_binding *binding = bindings[i];
11729 binding->previous = IDENTIFIER_BINDING (names[i]);
11730 IDENTIFIER_BINDING (names[i]) = binding;
11733 /* If in template, STMT is converted to a normal for-statement
11734 at instantiation. If not, it is done just ahead. */
11735 if (processing_template_decl)
11737 if (check_for_bare_parameter_packs (range_expr))
11738 range_expr = error_mark_node;
11739 stmt = begin_range_for_stmt (scope, init);
11740 if (ivdep)
11741 RANGE_FOR_IVDEP (stmt) = 1;
11742 if (unroll)
11743 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11744 finish_range_for_decl (stmt, range_decl, range_expr);
11745 if (!type_dependent_expression_p (range_expr)
11746 /* do_auto_deduction doesn't mess with template init-lists. */
11747 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11748 do_range_for_auto_deduction (range_decl, range_expr);
11750 else
11752 stmt = begin_for_stmt (scope, init);
11753 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11754 decomp_first_name, decomp_cnt, ivdep,
11755 unroll);
11757 return stmt;
11760 /* Subroutine of cp_convert_range_for: given the initializer expression,
11761 builds up the range temporary. */
11763 static tree
11764 build_range_temp (tree range_expr)
11766 tree range_type, range_temp;
11768 /* Find out the type deduced by the declaration
11769 `auto &&__range = range_expr'. */
11770 range_type = cp_build_reference_type (make_auto (), true);
11771 range_type = do_auto_deduction (range_type, range_expr,
11772 type_uses_auto (range_type));
11774 /* Create the __range variable. */
11775 range_temp = build_decl (input_location, VAR_DECL,
11776 get_identifier ("__for_range"), range_type);
11777 TREE_USED (range_temp) = 1;
11778 DECL_ARTIFICIAL (range_temp) = 1;
11780 return range_temp;
11783 /* Used by cp_parser_range_for in template context: we aren't going to
11784 do a full conversion yet, but we still need to resolve auto in the
11785 type of the for-range-declaration if present. This is basically
11786 a shortcut version of cp_convert_range_for. */
11788 static void
11789 do_range_for_auto_deduction (tree decl, tree range_expr)
11791 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11792 if (auto_node)
11794 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11795 range_temp = convert_from_reference (build_range_temp (range_expr));
11796 iter_type = (cp_parser_perform_range_for_lookup
11797 (range_temp, &begin_dummy, &end_dummy));
11798 if (iter_type)
11800 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11801 iter_type);
11802 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11803 RO_UNARY_STAR,
11804 tf_warning_or_error);
11805 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11806 iter_decl, auto_node);
11811 /* Converts a range-based for-statement into a normal
11812 for-statement, as per the definition.
11814 for (RANGE_DECL : RANGE_EXPR)
11815 BLOCK
11817 should be equivalent to:
11820 auto &&__range = RANGE_EXPR;
11821 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11822 __begin != __end;
11823 ++__begin)
11825 RANGE_DECL = *__begin;
11826 BLOCK
11830 If RANGE_EXPR is an array:
11831 BEGIN_EXPR = __range
11832 END_EXPR = __range + ARRAY_SIZE(__range)
11833 Else if RANGE_EXPR has a member 'begin' or 'end':
11834 BEGIN_EXPR = __range.begin()
11835 END_EXPR = __range.end()
11836 Else:
11837 BEGIN_EXPR = begin(__range)
11838 END_EXPR = end(__range);
11840 If __range has a member 'begin' but not 'end', or vice versa, we must
11841 still use the second alternative (it will surely fail, however).
11842 When calling begin()/end() in the third alternative we must use
11843 argument dependent lookup, but always considering 'std' as an associated
11844 namespace. */
11846 tree
11847 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11848 tree decomp_first_name, unsigned int decomp_cnt,
11849 bool ivdep, unsigned short unroll)
11851 tree begin, end;
11852 tree iter_type, begin_expr, end_expr;
11853 tree condition, expression;
11855 range_expr = mark_lvalue_use (range_expr);
11857 if (range_decl == error_mark_node || range_expr == error_mark_node)
11858 /* If an error happened previously do nothing or else a lot of
11859 unhelpful errors would be issued. */
11860 begin_expr = end_expr = iter_type = error_mark_node;
11861 else
11863 tree range_temp;
11865 if (VAR_P (range_expr)
11866 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11867 /* Can't bind a reference to an array of runtime bound. */
11868 range_temp = range_expr;
11869 else
11871 range_temp = build_range_temp (range_expr);
11872 pushdecl (range_temp);
11873 cp_finish_decl (range_temp, range_expr,
11874 /*is_constant_init*/false, NULL_TREE,
11875 LOOKUP_ONLYCONVERTING);
11876 range_temp = convert_from_reference (range_temp);
11878 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11879 &begin_expr, &end_expr);
11882 /* The new for initialization statement. */
11883 begin = build_decl (input_location, VAR_DECL,
11884 get_identifier ("__for_begin"), iter_type);
11885 TREE_USED (begin) = 1;
11886 DECL_ARTIFICIAL (begin) = 1;
11887 pushdecl (begin);
11888 cp_finish_decl (begin, begin_expr,
11889 /*is_constant_init*/false, NULL_TREE,
11890 LOOKUP_ONLYCONVERTING);
11892 if (cxx_dialect >= cxx17)
11893 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11894 end = build_decl (input_location, VAR_DECL,
11895 get_identifier ("__for_end"), iter_type);
11896 TREE_USED (end) = 1;
11897 DECL_ARTIFICIAL (end) = 1;
11898 pushdecl (end);
11899 cp_finish_decl (end, end_expr,
11900 /*is_constant_init*/false, NULL_TREE,
11901 LOOKUP_ONLYCONVERTING);
11903 finish_init_stmt (statement);
11905 /* The new for condition. */
11906 condition = build_x_binary_op (input_location, NE_EXPR,
11907 begin, ERROR_MARK,
11908 end, ERROR_MARK,
11909 NULL, tf_warning_or_error);
11910 finish_for_cond (condition, statement, ivdep, unroll);
11912 /* The new increment expression. */
11913 expression = finish_unary_op_expr (input_location,
11914 PREINCREMENT_EXPR, begin,
11915 tf_warning_or_error);
11916 finish_for_expr (expression, statement);
11918 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11919 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11921 /* The declaration is initialized with *__begin inside the loop body. */
11922 cp_finish_decl (range_decl,
11923 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11924 tf_warning_or_error),
11925 /*is_constant_init*/false, NULL_TREE,
11926 LOOKUP_ONLYCONVERTING);
11927 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11928 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11930 return statement;
11933 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11934 We need to solve both at the same time because the method used
11935 depends on the existence of members begin or end.
11936 Returns the type deduced for the iterator expression. */
11938 static tree
11939 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11941 if (error_operand_p (range))
11943 *begin = *end = error_mark_node;
11944 return error_mark_node;
11947 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11949 error ("range-based %<for%> expression of type %qT "
11950 "has incomplete type", TREE_TYPE (range));
11951 *begin = *end = error_mark_node;
11952 return error_mark_node;
11954 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11956 /* If RANGE is an array, we will use pointer arithmetic. */
11957 *begin = decay_conversion (range, tf_warning_or_error);
11958 *end = build_binary_op (input_location, PLUS_EXPR,
11959 range,
11960 array_type_nelts_top (TREE_TYPE (range)),
11961 false);
11962 return TREE_TYPE (*begin);
11964 else
11966 /* If it is not an array, we must do a bit of magic. */
11967 tree id_begin, id_end;
11968 tree member_begin, member_end;
11970 *begin = *end = error_mark_node;
11972 id_begin = get_identifier ("begin");
11973 id_end = get_identifier ("end");
11974 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11975 /*protect=*/2, /*want_type=*/false,
11976 tf_warning_or_error);
11977 member_end = lookup_member (TREE_TYPE (range), id_end,
11978 /*protect=*/2, /*want_type=*/false,
11979 tf_warning_or_error);
11981 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11983 /* Use the member functions. */
11984 if (member_begin != NULL_TREE)
11985 *begin = cp_parser_range_for_member_function (range, id_begin);
11986 else
11987 error ("range-based %<for%> expression of type %qT has an "
11988 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11990 if (member_end != NULL_TREE)
11991 *end = cp_parser_range_for_member_function (range, id_end);
11992 else
11993 error ("range-based %<for%> expression of type %qT has a "
11994 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11996 else
11998 /* Use global functions with ADL. */
11999 vec<tree, va_gc> *vec;
12000 vec = make_tree_vector ();
12002 vec_safe_push (vec, range);
12004 member_begin = perform_koenig_lookup (id_begin, vec,
12005 tf_warning_or_error);
12006 *begin = finish_call_expr (member_begin, &vec, false, true,
12007 tf_warning_or_error);
12008 member_end = perform_koenig_lookup (id_end, vec,
12009 tf_warning_or_error);
12010 *end = finish_call_expr (member_end, &vec, false, true,
12011 tf_warning_or_error);
12013 release_tree_vector (vec);
12016 /* Last common checks. */
12017 if (*begin == error_mark_node || *end == error_mark_node)
12019 /* If one of the expressions is an error do no more checks. */
12020 *begin = *end = error_mark_node;
12021 return error_mark_node;
12023 else if (type_dependent_expression_p (*begin)
12024 || type_dependent_expression_p (*end))
12025 /* Can happen, when, eg, in a template context, Koenig lookup
12026 can't resolve begin/end (c++/58503). */
12027 return NULL_TREE;
12028 else
12030 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12031 /* The unqualified type of the __begin and __end temporaries should
12032 be the same, as required by the multiple auto declaration. */
12033 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12035 if (cxx_dialect >= cxx17
12036 && (build_x_binary_op (input_location, NE_EXPR,
12037 *begin, ERROR_MARK,
12038 *end, ERROR_MARK,
12039 NULL, tf_none)
12040 != error_mark_node))
12041 /* P0184R0 allows __begin and __end to have different types,
12042 but make sure they are comparable so we can give a better
12043 diagnostic. */;
12044 else
12045 error ("inconsistent begin/end types in range-based %<for%> "
12046 "statement: %qT and %qT",
12047 TREE_TYPE (*begin), TREE_TYPE (*end));
12049 return iter_type;
12054 /* Helper function for cp_parser_perform_range_for_lookup.
12055 Builds a tree for RANGE.IDENTIFIER(). */
12057 static tree
12058 cp_parser_range_for_member_function (tree range, tree identifier)
12060 tree member, res;
12061 vec<tree, va_gc> *vec;
12063 member = finish_class_member_access_expr (range, identifier,
12064 false, tf_warning_or_error);
12065 if (member == error_mark_node)
12066 return error_mark_node;
12068 vec = make_tree_vector ();
12069 res = finish_call_expr (member, &vec,
12070 /*disallow_virtual=*/false,
12071 /*koenig_p=*/false,
12072 tf_warning_or_error);
12073 release_tree_vector (vec);
12074 return res;
12077 /* Parse an iteration-statement.
12079 iteration-statement:
12080 while ( condition ) statement
12081 do statement while ( expression ) ;
12082 for ( init-statement condition [opt] ; expression [opt] )
12083 statement
12085 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12087 static tree
12088 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12089 unsigned short unroll)
12091 cp_token *token;
12092 enum rid keyword;
12093 tree statement;
12094 unsigned char in_statement;
12095 token_indent_info guard_tinfo;
12097 /* Peek at the next token. */
12098 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12099 if (!token)
12100 return error_mark_node;
12102 guard_tinfo = get_token_indent_info (token);
12104 /* Remember whether or not we are already within an iteration
12105 statement. */
12106 in_statement = parser->in_statement;
12108 /* See what kind of keyword it is. */
12109 keyword = token->keyword;
12110 switch (keyword)
12112 case RID_WHILE:
12114 tree condition;
12116 /* Begin the while-statement. */
12117 statement = begin_while_stmt ();
12118 /* Look for the `('. */
12119 matching_parens parens;
12120 parens.require_open (parser);
12121 /* Parse the condition. */
12122 condition = cp_parser_condition (parser);
12123 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12124 /* Look for the `)'. */
12125 parens.require_close (parser);
12126 /* Parse the dependent statement. */
12127 parser->in_statement = IN_ITERATION_STMT;
12128 bool prev = note_iteration_stmt_body_start ();
12129 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12130 note_iteration_stmt_body_end (prev);
12131 parser->in_statement = in_statement;
12132 /* We're done with the while-statement. */
12133 finish_while_stmt (statement);
12135 break;
12137 case RID_DO:
12139 tree expression;
12141 /* Begin the do-statement. */
12142 statement = begin_do_stmt ();
12143 /* Parse the body of the do-statement. */
12144 parser->in_statement = IN_ITERATION_STMT;
12145 bool prev = note_iteration_stmt_body_start ();
12146 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12147 note_iteration_stmt_body_end (prev);
12148 parser->in_statement = in_statement;
12149 finish_do_body (statement);
12150 /* Look for the `while' keyword. */
12151 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12152 /* Look for the `('. */
12153 matching_parens parens;
12154 parens.require_open (parser);
12155 /* Parse the expression. */
12156 expression = cp_parser_expression (parser);
12157 /* We're done with the do-statement. */
12158 finish_do_stmt (expression, statement, ivdep, unroll);
12159 /* Look for the `)'. */
12160 parens.require_close (parser);
12161 /* Look for the `;'. */
12162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12164 break;
12166 case RID_FOR:
12168 /* Look for the `('. */
12169 matching_parens parens;
12170 parens.require_open (parser);
12172 statement = cp_parser_for (parser, ivdep, unroll);
12174 /* Look for the `)'. */
12175 parens.require_close (parser);
12177 /* Parse the body of the for-statement. */
12178 parser->in_statement = IN_ITERATION_STMT;
12179 bool prev = note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12181 note_iteration_stmt_body_end (prev);
12182 parser->in_statement = in_statement;
12184 /* We're done with the for-statement. */
12185 finish_for_stmt (statement);
12187 break;
12189 default:
12190 cp_parser_error (parser, "expected iteration-statement");
12191 statement = error_mark_node;
12192 break;
12195 return statement;
12198 /* Parse a init-statement or the declarator of a range-based-for.
12199 Returns true if a range-based-for declaration is seen.
12201 init-statement:
12202 expression-statement
12203 simple-declaration */
12205 static bool
12206 cp_parser_init_statement (cp_parser* parser, tree *decl)
12208 /* If the next token is a `;', then we have an empty
12209 expression-statement. Grammatically, this is also a
12210 simple-declaration, but an invalid one, because it does not
12211 declare anything. Therefore, if we did not handle this case
12212 specially, we would issue an error message about an invalid
12213 declaration. */
12214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12216 bool is_range_for = false;
12217 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12219 /* A colon is used in range-based for. */
12220 parser->colon_corrects_to_scope_p = false;
12222 /* We're going to speculatively look for a declaration, falling back
12223 to an expression, if necessary. */
12224 cp_parser_parse_tentatively (parser);
12225 /* Parse the declaration. */
12226 cp_parser_simple_declaration (parser,
12227 /*function_definition_allowed_p=*/false,
12228 decl);
12229 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12230 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12232 /* It is a range-for, consume the ':' */
12233 cp_lexer_consume_token (parser->lexer);
12234 is_range_for = true;
12235 if (cxx_dialect < cxx11)
12237 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12238 "range-based %<for%> loops only available with "
12239 "-std=c++11 or -std=gnu++11");
12240 *decl = error_mark_node;
12243 else
12244 /* The ';' is not consumed yet because we told
12245 cp_parser_simple_declaration not to. */
12246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12248 if (cp_parser_parse_definitely (parser))
12249 return is_range_for;
12250 /* If the tentative parse failed, then we shall need to look for an
12251 expression-statement. */
12253 /* If we are here, it is an expression-statement. */
12254 cp_parser_expression_statement (parser, NULL_TREE);
12255 return false;
12258 /* Parse a jump-statement.
12260 jump-statement:
12261 break ;
12262 continue ;
12263 return expression [opt] ;
12264 return braced-init-list ;
12265 goto identifier ;
12267 GNU extension:
12269 jump-statement:
12270 goto * expression ;
12272 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12274 static tree
12275 cp_parser_jump_statement (cp_parser* parser)
12277 tree statement = error_mark_node;
12278 cp_token *token;
12279 enum rid keyword;
12280 unsigned char in_statement;
12282 /* Peek at the next token. */
12283 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12284 if (!token)
12285 return error_mark_node;
12287 /* See what kind of keyword it is. */
12288 keyword = token->keyword;
12289 switch (keyword)
12291 case RID_BREAK:
12292 in_statement = parser->in_statement & ~IN_IF_STMT;
12293 switch (in_statement)
12295 case 0:
12296 error_at (token->location, "break statement not within loop or switch");
12297 break;
12298 default:
12299 gcc_assert ((in_statement & IN_SWITCH_STMT)
12300 || in_statement == IN_ITERATION_STMT);
12301 statement = finish_break_stmt ();
12302 if (in_statement == IN_ITERATION_STMT)
12303 break_maybe_infinite_loop ();
12304 break;
12305 case IN_OMP_BLOCK:
12306 error_at (token->location, "invalid exit from OpenMP structured block");
12307 break;
12308 case IN_OMP_FOR:
12309 error_at (token->location, "break statement used with OpenMP for loop");
12310 break;
12312 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12313 break;
12315 case RID_CONTINUE:
12316 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12318 case 0:
12319 error_at (token->location, "continue statement not within a loop");
12320 break;
12321 /* Fall through. */
12322 case IN_ITERATION_STMT:
12323 case IN_OMP_FOR:
12324 statement = finish_continue_stmt ();
12325 break;
12326 case IN_OMP_BLOCK:
12327 error_at (token->location, "invalid exit from OpenMP structured block");
12328 break;
12329 default:
12330 gcc_unreachable ();
12332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12333 break;
12335 case RID_RETURN:
12337 tree expr;
12338 bool expr_non_constant_p;
12340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12342 cp_lexer_set_source_position (parser->lexer);
12343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12344 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12346 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12347 expr = cp_parser_expression (parser);
12348 else
12349 /* If the next token is a `;', then there is no
12350 expression. */
12351 expr = NULL_TREE;
12352 /* Build the return-statement. */
12353 if (current_function_auto_return_pattern && in_discarded_stmt)
12354 /* Don't deduce from a discarded return statement. */;
12355 else
12356 statement = finish_return_stmt (expr);
12357 /* Look for the final `;'. */
12358 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12360 break;
12362 case RID_GOTO:
12363 if (parser->in_function_body
12364 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12366 error ("%<goto%> in %<constexpr%> function");
12367 cp_function_chain->invalid_constexpr = true;
12370 /* Create the goto-statement. */
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12373 /* Issue a warning about this use of a GNU extension. */
12374 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12375 /* Consume the '*' token. */
12376 cp_lexer_consume_token (parser->lexer);
12377 /* Parse the dependent expression. */
12378 finish_goto_stmt (cp_parser_expression (parser));
12380 else
12381 finish_goto_stmt (cp_parser_identifier (parser));
12382 /* Look for the final `;'. */
12383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12384 break;
12386 default:
12387 cp_parser_error (parser, "expected jump-statement");
12388 break;
12391 return statement;
12394 /* Parse a declaration-statement.
12396 declaration-statement:
12397 block-declaration */
12399 static void
12400 cp_parser_declaration_statement (cp_parser* parser)
12402 void *p;
12404 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12405 p = obstack_alloc (&declarator_obstack, 0);
12407 /* Parse the block-declaration. */
12408 cp_parser_block_declaration (parser, /*statement_p=*/true);
12410 /* Free any declarators allocated. */
12411 obstack_free (&declarator_obstack, p);
12414 /* Some dependent statements (like `if (cond) statement'), are
12415 implicitly in their own scope. In other words, if the statement is
12416 a single statement (as opposed to a compound-statement), it is
12417 none-the-less treated as if it were enclosed in braces. Any
12418 declarations appearing in the dependent statement are out of scope
12419 after control passes that point. This function parses a statement,
12420 but ensures that is in its own scope, even if it is not a
12421 compound-statement.
12423 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12424 is a (possibly labeled) if statement which is not enclosed in
12425 braces and has an else clause. This is used to implement
12426 -Wparentheses.
12428 CHAIN is a vector of if-else-if conditions. This is used to implement
12429 -Wduplicated-cond.
12431 Returns the new statement. */
12433 static tree
12434 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12435 const token_indent_info &guard_tinfo,
12436 vec<tree> *chain)
12438 tree statement;
12439 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12440 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12441 token_indent_info body_tinfo
12442 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12444 if (if_p != NULL)
12445 *if_p = false;
12447 /* Mark if () ; with a special NOP_EXPR. */
12448 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12450 cp_lexer_consume_token (parser->lexer);
12451 statement = add_stmt (build_empty_stmt (body_loc));
12453 if (guard_tinfo.keyword == RID_IF
12454 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12455 warning_at (body_loc, OPT_Wempty_body,
12456 "suggest braces around empty body in an %<if%> statement");
12457 else if (guard_tinfo.keyword == RID_ELSE)
12458 warning_at (body_loc, OPT_Wempty_body,
12459 "suggest braces around empty body in an %<else%> statement");
12461 /* if a compound is opened, we simply parse the statement directly. */
12462 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12463 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12464 /* If the token is not a `{', then we must take special action. */
12465 else
12467 /* Create a compound-statement. */
12468 statement = begin_compound_stmt (0);
12469 /* Parse the dependent-statement. */
12470 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12471 &body_loc_after_labels);
12472 /* Finish the dummy compound-statement. */
12473 finish_compound_stmt (statement);
12476 token_indent_info next_tinfo
12477 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12478 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12480 if (body_loc_after_labels != UNKNOWN_LOCATION
12481 && next_tinfo.type != CPP_SEMICOLON)
12482 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12483 guard_tinfo.location, guard_tinfo.keyword);
12485 /* Return the statement. */
12486 return statement;
12489 /* For some dependent statements (like `while (cond) statement'), we
12490 have already created a scope. Therefore, even if the dependent
12491 statement is a compound-statement, we do not want to create another
12492 scope. */
12494 static void
12495 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12496 const token_indent_info &guard_tinfo)
12498 /* If the token is a `{', then we must take special action. */
12499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12501 token_indent_info body_tinfo
12502 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12503 location_t loc_after_labels = UNKNOWN_LOCATION;
12505 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12506 &loc_after_labels);
12507 token_indent_info next_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12509 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12511 if (loc_after_labels != UNKNOWN_LOCATION
12512 && next_tinfo.type != CPP_SEMICOLON)
12513 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12514 guard_tinfo.location,
12515 guard_tinfo.keyword);
12517 else
12519 /* Avoid calling cp_parser_compound_statement, so that we
12520 don't create a new scope. Do everything else by hand. */
12521 matching_braces braces;
12522 braces.require_open (parser);
12523 /* If the next keyword is `__label__' we have a label declaration. */
12524 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12525 cp_parser_label_declaration (parser);
12526 /* Parse an (optional) statement-seq. */
12527 cp_parser_statement_seq_opt (parser, NULL_TREE);
12528 braces.require_close (parser);
12532 /* Declarations [gram.dcl.dcl] */
12534 /* Parse an optional declaration-sequence.
12536 declaration-seq:
12537 declaration
12538 declaration-seq declaration */
12540 static void
12541 cp_parser_declaration_seq_opt (cp_parser* parser)
12543 while (true)
12545 cp_token *token;
12547 token = cp_lexer_peek_token (parser->lexer);
12549 if (token->type == CPP_CLOSE_BRACE
12550 || token->type == CPP_EOF
12551 || token->type == CPP_PRAGMA_EOL)
12552 break;
12554 if (token->type == CPP_SEMICOLON)
12556 /* A declaration consisting of a single semicolon is
12557 invalid. Allow it unless we're being pedantic. */
12558 cp_lexer_consume_token (parser->lexer);
12559 if (!in_system_header_at (input_location))
12560 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12561 continue;
12564 /* If we're entering or exiting a region that's implicitly
12565 extern "C", modify the lang context appropriately. */
12566 if (!parser->implicit_extern_c && token->implicit_extern_c)
12568 push_lang_context (lang_name_c);
12569 parser->implicit_extern_c = true;
12571 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12573 pop_lang_context ();
12574 parser->implicit_extern_c = false;
12577 if (token->type == CPP_PRAGMA)
12579 /* A top-level declaration can consist solely of a #pragma.
12580 A nested declaration cannot, so this is done here and not
12581 in cp_parser_declaration. (A #pragma at block scope is
12582 handled in cp_parser_statement.) */
12583 cp_parser_pragma (parser, pragma_external, NULL);
12584 continue;
12587 /* Parse the declaration itself. */
12588 cp_parser_declaration (parser);
12592 /* Parse a declaration.
12594 declaration:
12595 block-declaration
12596 function-definition
12597 template-declaration
12598 explicit-instantiation
12599 explicit-specialization
12600 linkage-specification
12601 namespace-definition
12603 C++17:
12604 deduction-guide
12606 GNU extension:
12608 declaration:
12609 __extension__ declaration */
12611 static void
12612 cp_parser_declaration (cp_parser* parser)
12614 cp_token token1;
12615 cp_token token2;
12616 int saved_pedantic;
12617 void *p;
12618 tree attributes = NULL_TREE;
12620 /* Check for the `__extension__' keyword. */
12621 if (cp_parser_extension_opt (parser, &saved_pedantic))
12623 /* Parse the qualified declaration. */
12624 cp_parser_declaration (parser);
12625 /* Restore the PEDANTIC flag. */
12626 pedantic = saved_pedantic;
12628 return;
12631 /* Try to figure out what kind of declaration is present. */
12632 token1 = *cp_lexer_peek_token (parser->lexer);
12634 if (token1.type != CPP_EOF)
12635 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12636 else
12638 token2.type = CPP_EOF;
12639 token2.keyword = RID_MAX;
12642 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12643 p = obstack_alloc (&declarator_obstack, 0);
12645 /* If the next token is `extern' and the following token is a string
12646 literal, then we have a linkage specification. */
12647 if (token1.keyword == RID_EXTERN
12648 && cp_parser_is_pure_string_literal (&token2))
12649 cp_parser_linkage_specification (parser);
12650 /* If the next token is `template', then we have either a template
12651 declaration, an explicit instantiation, or an explicit
12652 specialization. */
12653 else if (token1.keyword == RID_TEMPLATE)
12655 /* `template <>' indicates a template specialization. */
12656 if (token2.type == CPP_LESS
12657 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12658 cp_parser_explicit_specialization (parser);
12659 /* `template <' indicates a template declaration. */
12660 else if (token2.type == CPP_LESS)
12661 cp_parser_template_declaration (parser, /*member_p=*/false);
12662 /* Anything else must be an explicit instantiation. */
12663 else
12664 cp_parser_explicit_instantiation (parser);
12666 /* If the next token is `export', then we have a template
12667 declaration. */
12668 else if (token1.keyword == RID_EXPORT)
12669 cp_parser_template_declaration (parser, /*member_p=*/false);
12670 /* If the next token is `extern', 'static' or 'inline' and the one
12671 after that is `template', we have a GNU extended explicit
12672 instantiation directive. */
12673 else if (cp_parser_allow_gnu_extensions_p (parser)
12674 && (token1.keyword == RID_EXTERN
12675 || token1.keyword == RID_STATIC
12676 || token1.keyword == RID_INLINE)
12677 && token2.keyword == RID_TEMPLATE)
12678 cp_parser_explicit_instantiation (parser);
12679 /* If the next token is `namespace', check for a named or unnamed
12680 namespace definition. */
12681 else if (token1.keyword == RID_NAMESPACE
12682 && (/* A named namespace definition. */
12683 (token2.type == CPP_NAME
12684 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12685 != CPP_EQ))
12686 || (token2.type == CPP_OPEN_SQUARE
12687 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12688 == CPP_OPEN_SQUARE)
12689 /* An unnamed namespace definition. */
12690 || token2.type == CPP_OPEN_BRACE
12691 || token2.keyword == RID_ATTRIBUTE))
12692 cp_parser_namespace_definition (parser);
12693 /* An inline (associated) namespace definition. */
12694 else if (token1.keyword == RID_INLINE
12695 && token2.keyword == RID_NAMESPACE)
12696 cp_parser_namespace_definition (parser);
12697 /* Objective-C++ declaration/definition. */
12698 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12699 cp_parser_objc_declaration (parser, NULL_TREE);
12700 else if (c_dialect_objc ()
12701 && token1.keyword == RID_ATTRIBUTE
12702 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12703 cp_parser_objc_declaration (parser, attributes);
12704 /* At this point we may have a template declared by a concept
12705 introduction. */
12706 else if (flag_concepts
12707 && cp_parser_template_declaration_after_export (parser,
12708 /*member_p=*/false))
12709 /* We did. */;
12710 else
12711 /* Try to parse a block-declaration, or a function-definition. */
12712 cp_parser_block_declaration (parser, /*statement_p=*/false);
12714 /* Free any declarators allocated. */
12715 obstack_free (&declarator_obstack, p);
12718 /* Parse a block-declaration.
12720 block-declaration:
12721 simple-declaration
12722 asm-definition
12723 namespace-alias-definition
12724 using-declaration
12725 using-directive
12727 GNU Extension:
12729 block-declaration:
12730 __extension__ block-declaration
12732 C++0x Extension:
12734 block-declaration:
12735 static_assert-declaration
12737 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12738 part of a declaration-statement. */
12740 static void
12741 cp_parser_block_declaration (cp_parser *parser,
12742 bool statement_p)
12744 cp_token *token1;
12745 int saved_pedantic;
12747 /* Check for the `__extension__' keyword. */
12748 if (cp_parser_extension_opt (parser, &saved_pedantic))
12750 /* Parse the qualified declaration. */
12751 cp_parser_block_declaration (parser, statement_p);
12752 /* Restore the PEDANTIC flag. */
12753 pedantic = saved_pedantic;
12755 return;
12758 /* Peek at the next token to figure out which kind of declaration is
12759 present. */
12760 token1 = cp_lexer_peek_token (parser->lexer);
12762 /* If the next keyword is `asm', we have an asm-definition. */
12763 if (token1->keyword == RID_ASM)
12765 if (statement_p)
12766 cp_parser_commit_to_tentative_parse (parser);
12767 cp_parser_asm_definition (parser);
12769 /* If the next keyword is `namespace', we have a
12770 namespace-alias-definition. */
12771 else if (token1->keyword == RID_NAMESPACE)
12772 cp_parser_namespace_alias_definition (parser);
12773 /* If the next keyword is `using', we have a
12774 using-declaration, a using-directive, or an alias-declaration. */
12775 else if (token1->keyword == RID_USING)
12777 cp_token *token2;
12779 if (statement_p)
12780 cp_parser_commit_to_tentative_parse (parser);
12781 /* If the token after `using' is `namespace', then we have a
12782 using-directive. */
12783 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12784 if (token2->keyword == RID_NAMESPACE)
12785 cp_parser_using_directive (parser);
12786 /* If the second token after 'using' is '=', then we have an
12787 alias-declaration. */
12788 else if (cxx_dialect >= cxx11
12789 && token2->type == CPP_NAME
12790 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12791 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12792 cp_parser_alias_declaration (parser);
12793 /* Otherwise, it's a using-declaration. */
12794 else
12795 cp_parser_using_declaration (parser,
12796 /*access_declaration_p=*/false);
12798 /* If the next keyword is `__label__' we have a misplaced label
12799 declaration. */
12800 else if (token1->keyword == RID_LABEL)
12802 cp_lexer_consume_token (parser->lexer);
12803 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12804 cp_parser_skip_to_end_of_statement (parser);
12805 /* If the next token is now a `;', consume it. */
12806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12807 cp_lexer_consume_token (parser->lexer);
12809 /* If the next token is `static_assert' we have a static assertion. */
12810 else if (token1->keyword == RID_STATIC_ASSERT)
12811 cp_parser_static_assert (parser, /*member_p=*/false);
12812 /* Anything else must be a simple-declaration. */
12813 else
12814 cp_parser_simple_declaration (parser, !statement_p,
12815 /*maybe_range_for_decl*/NULL);
12818 /* Parse a simple-declaration.
12820 simple-declaration:
12821 decl-specifier-seq [opt] init-declarator-list [opt] ;
12822 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12823 brace-or-equal-initializer ;
12825 init-declarator-list:
12826 init-declarator
12827 init-declarator-list , init-declarator
12829 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12830 function-definition as a simple-declaration.
12832 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12833 parsed declaration if it is an uninitialized single declarator not followed
12834 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12835 if present, will not be consumed. */
12837 static void
12838 cp_parser_simple_declaration (cp_parser* parser,
12839 bool function_definition_allowed_p,
12840 tree *maybe_range_for_decl)
12842 cp_decl_specifier_seq decl_specifiers;
12843 int declares_class_or_enum;
12844 bool saw_declarator;
12845 location_t comma_loc = UNKNOWN_LOCATION;
12846 location_t init_loc = UNKNOWN_LOCATION;
12848 if (maybe_range_for_decl)
12849 *maybe_range_for_decl = NULL_TREE;
12851 /* Defer access checks until we know what is being declared; the
12852 checks for names appearing in the decl-specifier-seq should be
12853 done as if we were in the scope of the thing being declared. */
12854 push_deferring_access_checks (dk_deferred);
12856 /* Parse the decl-specifier-seq. We have to keep track of whether
12857 or not the decl-specifier-seq declares a named class or
12858 enumeration type, since that is the only case in which the
12859 init-declarator-list is allowed to be empty.
12861 [dcl.dcl]
12863 In a simple-declaration, the optional init-declarator-list can be
12864 omitted only when declaring a class or enumeration, that is when
12865 the decl-specifier-seq contains either a class-specifier, an
12866 elaborated-type-specifier, or an enum-specifier. */
12867 cp_parser_decl_specifier_seq (parser,
12868 CP_PARSER_FLAGS_OPTIONAL,
12869 &decl_specifiers,
12870 &declares_class_or_enum);
12871 /* We no longer need to defer access checks. */
12872 stop_deferring_access_checks ();
12874 /* In a block scope, a valid declaration must always have a
12875 decl-specifier-seq. By not trying to parse declarators, we can
12876 resolve the declaration/expression ambiguity more quickly. */
12877 if (!function_definition_allowed_p
12878 && !decl_specifiers.any_specifiers_p)
12880 cp_parser_error (parser, "expected declaration");
12881 goto done;
12884 /* If the next two tokens are both identifiers, the code is
12885 erroneous. The usual cause of this situation is code like:
12887 T t;
12889 where "T" should name a type -- but does not. */
12890 if (!decl_specifiers.any_type_specifiers_p
12891 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12893 /* If parsing tentatively, we should commit; we really are
12894 looking at a declaration. */
12895 cp_parser_commit_to_tentative_parse (parser);
12896 /* Give up. */
12897 goto done;
12900 /* If we have seen at least one decl-specifier, and the next token
12901 is not a parenthesis, then we must be looking at a declaration.
12902 (After "int (" we might be looking at a functional cast.) */
12903 if (decl_specifiers.any_specifiers_p
12904 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12905 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12906 && !cp_parser_error_occurred (parser))
12907 cp_parser_commit_to_tentative_parse (parser);
12909 /* Look for C++17 decomposition declaration. */
12910 for (size_t n = 1; ; n++)
12911 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12912 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12913 continue;
12914 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12915 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12916 && decl_specifiers.any_specifiers_p)
12918 tree decl
12919 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12920 maybe_range_for_decl,
12921 &init_loc);
12923 /* The next token should be either a `,' or a `;'. */
12924 cp_token *token = cp_lexer_peek_token (parser->lexer);
12925 /* If it's a `;', we are done. */
12926 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12927 goto finish;
12928 /* Anything else is an error. */
12929 else
12931 /* If we have already issued an error message we don't need
12932 to issue another one. */
12933 if ((decl != error_mark_node
12934 && DECL_INITIAL (decl) != error_mark_node)
12935 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12936 cp_parser_error (parser, "expected %<,%> or %<;%>");
12937 /* Skip tokens until we reach the end of the statement. */
12938 cp_parser_skip_to_end_of_statement (parser);
12939 /* If the next token is now a `;', consume it. */
12940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12941 cp_lexer_consume_token (parser->lexer);
12942 goto done;
12945 else
12946 break;
12948 tree last_type;
12949 bool auto_specifier_p;
12950 /* NULL_TREE if both variable and function declaration are allowed,
12951 error_mark_node if function declaration are not allowed and
12952 a FUNCTION_DECL that should be diagnosed if it is followed by
12953 variable declarations. */
12954 tree auto_function_declaration;
12956 last_type = NULL_TREE;
12957 auto_specifier_p
12958 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12959 auto_function_declaration = NULL_TREE;
12961 /* Keep going until we hit the `;' at the end of the simple
12962 declaration. */
12963 saw_declarator = false;
12964 while (cp_lexer_next_token_is_not (parser->lexer,
12965 CPP_SEMICOLON))
12967 cp_token *token;
12968 bool function_definition_p;
12969 tree decl;
12970 tree auto_result = NULL_TREE;
12972 if (saw_declarator)
12974 /* If we are processing next declarator, comma is expected */
12975 token = cp_lexer_peek_token (parser->lexer);
12976 gcc_assert (token->type == CPP_COMMA);
12977 cp_lexer_consume_token (parser->lexer);
12978 if (maybe_range_for_decl)
12980 *maybe_range_for_decl = error_mark_node;
12981 if (comma_loc == UNKNOWN_LOCATION)
12982 comma_loc = token->location;
12985 else
12986 saw_declarator = true;
12988 /* Parse the init-declarator. */
12989 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12990 /*checks=*/NULL,
12991 function_definition_allowed_p,
12992 /*member_p=*/false,
12993 declares_class_or_enum,
12994 &function_definition_p,
12995 maybe_range_for_decl,
12996 &init_loc,
12997 &auto_result);
12998 /* If an error occurred while parsing tentatively, exit quickly.
12999 (That usually happens when in the body of a function; each
13000 statement is treated as a declaration-statement until proven
13001 otherwise.) */
13002 if (cp_parser_error_occurred (parser))
13003 goto done;
13005 if (auto_specifier_p && cxx_dialect >= cxx14)
13007 /* If the init-declarator-list contains more than one
13008 init-declarator, they shall all form declarations of
13009 variables. */
13010 if (auto_function_declaration == NULL_TREE)
13011 auto_function_declaration
13012 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13013 else if (TREE_CODE (decl) == FUNCTION_DECL
13014 || auto_function_declaration != error_mark_node)
13016 error_at (decl_specifiers.locations[ds_type_spec],
13017 "non-variable %qD in declaration with more than one "
13018 "declarator with placeholder type",
13019 TREE_CODE (decl) == FUNCTION_DECL
13020 ? decl : auto_function_declaration);
13021 auto_function_declaration = error_mark_node;
13025 if (auto_result
13026 && (!processing_template_decl || !type_uses_auto (auto_result)))
13028 if (last_type
13029 && last_type != error_mark_node
13030 && !same_type_p (auto_result, last_type))
13032 /* If the list of declarators contains more than one declarator,
13033 the type of each declared variable is determined as described
13034 above. If the type deduced for the template parameter U is not
13035 the same in each deduction, the program is ill-formed. */
13036 error_at (decl_specifiers.locations[ds_type_spec],
13037 "inconsistent deduction for %qT: %qT and then %qT",
13038 decl_specifiers.type, last_type, auto_result);
13039 last_type = error_mark_node;
13041 else
13042 last_type = auto_result;
13045 /* Handle function definitions specially. */
13046 if (function_definition_p)
13048 /* If the next token is a `,', then we are probably
13049 processing something like:
13051 void f() {}, *p;
13053 which is erroneous. */
13054 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13056 cp_token *token = cp_lexer_peek_token (parser->lexer);
13057 error_at (token->location,
13058 "mixing"
13059 " declarations and function-definitions is forbidden");
13061 /* Otherwise, we're done with the list of declarators. */
13062 else
13064 pop_deferring_access_checks ();
13065 return;
13068 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13069 *maybe_range_for_decl = decl;
13070 /* The next token should be either a `,' or a `;'. */
13071 token = cp_lexer_peek_token (parser->lexer);
13072 /* If it's a `,', there are more declarators to come. */
13073 if (token->type == CPP_COMMA)
13074 /* will be consumed next time around */;
13075 /* If it's a `;', we are done. */
13076 else if (token->type == CPP_SEMICOLON)
13077 break;
13078 else if (maybe_range_for_decl)
13080 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13081 permerror (decl_specifiers.locations[ds_type_spec],
13082 "types may not be defined in a for-range-declaration");
13083 break;
13085 /* Anything else is an error. */
13086 else
13088 /* If we have already issued an error message we don't need
13089 to issue another one. */
13090 if ((decl != error_mark_node
13091 && DECL_INITIAL (decl) != error_mark_node)
13092 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13093 cp_parser_error (parser, "expected %<,%> or %<;%>");
13094 /* Skip tokens until we reach the end of the statement. */
13095 cp_parser_skip_to_end_of_statement (parser);
13096 /* If the next token is now a `;', consume it. */
13097 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13098 cp_lexer_consume_token (parser->lexer);
13099 goto done;
13101 /* After the first time around, a function-definition is not
13102 allowed -- even if it was OK at first. For example:
13104 int i, f() {}
13106 is not valid. */
13107 function_definition_allowed_p = false;
13110 /* Issue an error message if no declarators are present, and the
13111 decl-specifier-seq does not itself declare a class or
13112 enumeration: [dcl.dcl]/3. */
13113 if (!saw_declarator)
13115 if (cp_parser_declares_only_class_p (parser))
13117 if (!declares_class_or_enum
13118 && decl_specifiers.type
13119 && OVERLOAD_TYPE_P (decl_specifiers.type))
13120 /* Ensure an error is issued anyway when finish_decltype_type,
13121 called via cp_parser_decl_specifier_seq, returns a class or
13122 an enumeration (c++/51786). */
13123 decl_specifiers.type = NULL_TREE;
13124 shadow_tag (&decl_specifiers);
13126 /* Perform any deferred access checks. */
13127 perform_deferred_access_checks (tf_warning_or_error);
13130 /* Consume the `;'. */
13131 finish:
13132 if (!maybe_range_for_decl)
13133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13134 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13136 if (init_loc != UNKNOWN_LOCATION)
13137 error_at (init_loc, "initializer in range-based %<for%> loop");
13138 if (comma_loc != UNKNOWN_LOCATION)
13139 error_at (comma_loc,
13140 "multiple declarations in range-based %<for%> loop");
13143 done:
13144 pop_deferring_access_checks ();
13147 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13148 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13149 initializer ; */
13151 static tree
13152 cp_parser_decomposition_declaration (cp_parser *parser,
13153 cp_decl_specifier_seq *decl_specifiers,
13154 tree *maybe_range_for_decl,
13155 location_t *init_loc)
13157 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13159 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13161 /* Parse the identifier-list. */
13162 auto_vec<cp_expr, 10> v;
13163 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13164 while (true)
13166 cp_expr e = cp_parser_identifier (parser);
13167 if (e.get_value () == error_mark_node)
13168 break;
13169 v.safe_push (e);
13170 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13171 break;
13172 cp_lexer_consume_token (parser->lexer);
13175 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13176 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13178 end_loc = UNKNOWN_LOCATION;
13179 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13180 false);
13181 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13182 cp_lexer_consume_token (parser->lexer);
13183 else
13185 cp_parser_skip_to_end_of_statement (parser);
13186 return error_mark_node;
13190 if (cxx_dialect < cxx17)
13191 pedwarn (loc, 0, "structured bindings only available with "
13192 "-std=c++17 or -std=gnu++17");
13194 tree pushed_scope;
13195 cp_declarator *declarator = make_declarator (cdk_decomp);
13196 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13197 declarator->id_loc = loc;
13198 if (ref_qual != REF_QUAL_NONE)
13199 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13200 ref_qual == REF_QUAL_RVALUE,
13201 NULL_TREE);
13202 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13203 NULL_TREE, decl_specifiers->attributes,
13204 &pushed_scope);
13205 tree orig_decl = decl;
13207 unsigned int i;
13208 cp_expr e;
13209 cp_decl_specifier_seq decl_specs;
13210 clear_decl_specs (&decl_specs);
13211 decl_specs.type = make_auto ();
13212 tree prev = decl;
13213 FOR_EACH_VEC_ELT (v, i, e)
13215 if (i == 0)
13216 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13217 else
13218 declarator->u.id.unqualified_name = e.get_value ();
13219 declarator->id_loc = e.get_location ();
13220 tree elt_pushed_scope;
13221 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13222 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13223 if (decl2 == error_mark_node)
13224 decl = error_mark_node;
13225 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13227 /* Ensure we've diagnosed redeclaration if we aren't creating
13228 a new VAR_DECL. */
13229 gcc_assert (errorcount);
13230 decl = error_mark_node;
13232 else
13233 prev = decl2;
13234 if (elt_pushed_scope)
13235 pop_scope (elt_pushed_scope);
13238 if (v.is_empty ())
13240 error_at (loc, "empty structured binding declaration");
13241 decl = error_mark_node;
13244 if (maybe_range_for_decl == NULL
13245 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13247 bool non_constant_p = false, is_direct_init = false;
13248 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13249 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13250 &non_constant_p);
13251 if (initializer == NULL_TREE
13252 || (TREE_CODE (initializer) == TREE_LIST
13253 && TREE_CHAIN (initializer))
13254 || (is_direct_init
13255 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13256 && CONSTRUCTOR_NELTS (initializer) != 1))
13258 error_at (loc, "invalid initializer for structured binding "
13259 "declaration");
13260 initializer = error_mark_node;
13263 if (decl != error_mark_node)
13265 cp_maybe_mangle_decomp (decl, prev, v.length ());
13266 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13267 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13268 cp_finish_decomp (decl, prev, v.length ());
13271 else if (decl != error_mark_node)
13273 *maybe_range_for_decl = prev;
13274 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13275 the underlying DECL. */
13276 cp_finish_decomp (decl, prev, v.length ());
13279 if (pushed_scope)
13280 pop_scope (pushed_scope);
13282 if (decl == error_mark_node && DECL_P (orig_decl))
13284 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13285 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13288 return decl;
13291 /* Parse a decl-specifier-seq.
13293 decl-specifier-seq:
13294 decl-specifier-seq [opt] decl-specifier
13295 decl-specifier attribute-specifier-seq [opt] (C++11)
13297 decl-specifier:
13298 storage-class-specifier
13299 type-specifier
13300 function-specifier
13301 friend
13302 typedef
13304 GNU Extension:
13306 decl-specifier:
13307 attributes
13309 Concepts Extension:
13311 decl-specifier:
13312 concept
13314 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13316 The parser flags FLAGS is used to control type-specifier parsing.
13318 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13319 flags:
13321 1: one of the decl-specifiers is an elaborated-type-specifier
13322 (i.e., a type declaration)
13323 2: one of the decl-specifiers is an enum-specifier or a
13324 class-specifier (i.e., a type definition)
13328 static void
13329 cp_parser_decl_specifier_seq (cp_parser* parser,
13330 cp_parser_flags flags,
13331 cp_decl_specifier_seq *decl_specs,
13332 int* declares_class_or_enum)
13334 bool constructor_possible_p = !parser->in_declarator_p;
13335 bool found_decl_spec = false;
13336 cp_token *start_token = NULL;
13337 cp_decl_spec ds;
13339 /* Clear DECL_SPECS. */
13340 clear_decl_specs (decl_specs);
13342 /* Assume no class or enumeration type is declared. */
13343 *declares_class_or_enum = 0;
13345 /* Keep reading specifiers until there are no more to read. */
13346 while (true)
13348 bool constructor_p;
13349 cp_token *token;
13350 ds = ds_last;
13352 /* Peek at the next token. */
13353 token = cp_lexer_peek_token (parser->lexer);
13355 /* Save the first token of the decl spec list for error
13356 reporting. */
13357 if (!start_token)
13358 start_token = token;
13359 /* Handle attributes. */
13360 if (cp_next_tokens_can_be_attribute_p (parser))
13362 /* Parse the attributes. */
13363 tree attrs = cp_parser_attributes_opt (parser);
13365 /* In a sequence of declaration specifiers, c++11 attributes
13366 appertain to the type that precede them. In that case
13367 [dcl.spec]/1 says:
13369 The attribute-specifier-seq affects the type only for
13370 the declaration it appears in, not other declarations
13371 involving the same type.
13373 But for now let's force the user to position the
13374 attribute either at the beginning of the declaration or
13375 after the declarator-id, which would clearly mean that it
13376 applies to the declarator. */
13377 if (cxx11_attribute_p (attrs))
13379 if (!found_decl_spec)
13380 /* The c++11 attribute is at the beginning of the
13381 declaration. It appertains to the entity being
13382 declared. */;
13383 else
13385 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13387 /* This is an attribute following a
13388 class-specifier. */
13389 if (decl_specs->type_definition_p)
13390 warn_misplaced_attr_for_class_type (token->location,
13391 decl_specs->type);
13392 attrs = NULL_TREE;
13394 else
13396 decl_specs->std_attributes
13397 = chainon (decl_specs->std_attributes,
13398 attrs);
13399 if (decl_specs->locations[ds_std_attribute] == 0)
13400 decl_specs->locations[ds_std_attribute] = token->location;
13402 continue;
13406 decl_specs->attributes
13407 = chainon (decl_specs->attributes,
13408 attrs);
13409 if (decl_specs->locations[ds_attribute] == 0)
13410 decl_specs->locations[ds_attribute] = token->location;
13411 continue;
13413 /* Assume we will find a decl-specifier keyword. */
13414 found_decl_spec = true;
13415 /* If the next token is an appropriate keyword, we can simply
13416 add it to the list. */
13417 switch (token->keyword)
13419 /* decl-specifier:
13420 friend
13421 constexpr */
13422 case RID_FRIEND:
13423 if (!at_class_scope_p ())
13425 gcc_rich_location richloc (token->location);
13426 richloc.add_fixit_remove ();
13427 error_at (&richloc, "%<friend%> used outside of class");
13428 cp_lexer_purge_token (parser->lexer);
13430 else
13432 ds = ds_friend;
13433 /* Consume the token. */
13434 cp_lexer_consume_token (parser->lexer);
13436 break;
13438 case RID_CONSTEXPR:
13439 ds = ds_constexpr;
13440 cp_lexer_consume_token (parser->lexer);
13441 break;
13443 case RID_CONCEPT:
13444 ds = ds_concept;
13445 cp_lexer_consume_token (parser->lexer);
13446 break;
13448 /* function-specifier:
13449 inline
13450 virtual
13451 explicit */
13452 case RID_INLINE:
13453 case RID_VIRTUAL:
13454 case RID_EXPLICIT:
13455 cp_parser_function_specifier_opt (parser, decl_specs);
13456 break;
13458 /* decl-specifier:
13459 typedef */
13460 case RID_TYPEDEF:
13461 ds = ds_typedef;
13462 /* Consume the token. */
13463 cp_lexer_consume_token (parser->lexer);
13464 /* A constructor declarator cannot appear in a typedef. */
13465 constructor_possible_p = false;
13466 /* The "typedef" keyword can only occur in a declaration; we
13467 may as well commit at this point. */
13468 cp_parser_commit_to_tentative_parse (parser);
13470 if (decl_specs->storage_class != sc_none)
13471 decl_specs->conflicting_specifiers_p = true;
13472 break;
13474 /* storage-class-specifier:
13475 auto
13476 register
13477 static
13478 extern
13479 mutable
13481 GNU Extension:
13482 thread */
13483 case RID_AUTO:
13484 if (cxx_dialect == cxx98)
13486 /* Consume the token. */
13487 cp_lexer_consume_token (parser->lexer);
13489 /* Complain about `auto' as a storage specifier, if
13490 we're complaining about C++0x compatibility. */
13491 gcc_rich_location richloc (token->location);
13492 richloc.add_fixit_remove ();
13493 warning_at (&richloc, OPT_Wc__11_compat,
13494 "%<auto%> changes meaning in C++11; "
13495 "please remove it");
13497 /* Set the storage class anyway. */
13498 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13499 token);
13501 else
13502 /* C++0x auto type-specifier. */
13503 found_decl_spec = false;
13504 break;
13506 case RID_REGISTER:
13507 case RID_STATIC:
13508 case RID_EXTERN:
13509 case RID_MUTABLE:
13510 /* Consume the token. */
13511 cp_lexer_consume_token (parser->lexer);
13512 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13513 token);
13514 break;
13515 case RID_THREAD:
13516 /* Consume the token. */
13517 ds = ds_thread;
13518 cp_lexer_consume_token (parser->lexer);
13519 break;
13521 default:
13522 /* We did not yet find a decl-specifier yet. */
13523 found_decl_spec = false;
13524 break;
13527 if (found_decl_spec
13528 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13529 && token->keyword != RID_CONSTEXPR)
13530 error ("decl-specifier invalid in condition");
13532 if (found_decl_spec
13533 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13534 && token->keyword != RID_MUTABLE
13535 && token->keyword != RID_CONSTEXPR)
13536 error_at (token->location, "%qD invalid in lambda",
13537 ridpointers[token->keyword]);
13539 if (ds != ds_last)
13540 set_and_check_decl_spec_loc (decl_specs, ds, token);
13542 /* Constructors are a special case. The `S' in `S()' is not a
13543 decl-specifier; it is the beginning of the declarator. */
13544 constructor_p
13545 = (!found_decl_spec
13546 && constructor_possible_p
13547 && (cp_parser_constructor_declarator_p
13548 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13550 /* If we don't have a DECL_SPEC yet, then we must be looking at
13551 a type-specifier. */
13552 if (!found_decl_spec && !constructor_p)
13554 int decl_spec_declares_class_or_enum;
13555 bool is_cv_qualifier;
13556 tree type_spec;
13558 type_spec
13559 = cp_parser_type_specifier (parser, flags,
13560 decl_specs,
13561 /*is_declaration=*/true,
13562 &decl_spec_declares_class_or_enum,
13563 &is_cv_qualifier);
13564 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13566 /* If this type-specifier referenced a user-defined type
13567 (a typedef, class-name, etc.), then we can't allow any
13568 more such type-specifiers henceforth.
13570 [dcl.spec]
13572 The longest sequence of decl-specifiers that could
13573 possibly be a type name is taken as the
13574 decl-specifier-seq of a declaration. The sequence shall
13575 be self-consistent as described below.
13577 [dcl.type]
13579 As a general rule, at most one type-specifier is allowed
13580 in the complete decl-specifier-seq of a declaration. The
13581 only exceptions are the following:
13583 -- const or volatile can be combined with any other
13584 type-specifier.
13586 -- signed or unsigned can be combined with char, long,
13587 short, or int.
13589 -- ..
13591 Example:
13593 typedef char* Pc;
13594 void g (const int Pc);
13596 Here, Pc is *not* part of the decl-specifier seq; it's
13597 the declarator. Therefore, once we see a type-specifier
13598 (other than a cv-qualifier), we forbid any additional
13599 user-defined types. We *do* still allow things like `int
13600 int' to be considered a decl-specifier-seq, and issue the
13601 error message later. */
13602 if (type_spec && !is_cv_qualifier)
13603 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13604 /* A constructor declarator cannot follow a type-specifier. */
13605 if (type_spec)
13607 constructor_possible_p = false;
13608 found_decl_spec = true;
13609 if (!is_cv_qualifier)
13610 decl_specs->any_type_specifiers_p = true;
13614 /* If we still do not have a DECL_SPEC, then there are no more
13615 decl-specifiers. */
13616 if (!found_decl_spec)
13617 break;
13619 decl_specs->any_specifiers_p = true;
13620 /* After we see one decl-specifier, further decl-specifiers are
13621 always optional. */
13622 flags |= CP_PARSER_FLAGS_OPTIONAL;
13625 /* Don't allow a friend specifier with a class definition. */
13626 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13627 && (*declares_class_or_enum & 2))
13628 error_at (decl_specs->locations[ds_friend],
13629 "class definition may not be declared a friend");
13632 /* Parse an (optional) storage-class-specifier.
13634 storage-class-specifier:
13635 auto
13636 register
13637 static
13638 extern
13639 mutable
13641 GNU Extension:
13643 storage-class-specifier:
13644 thread
13646 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13648 static tree
13649 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13651 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13653 case RID_AUTO:
13654 if (cxx_dialect != cxx98)
13655 return NULL_TREE;
13656 /* Fall through for C++98. */
13657 gcc_fallthrough ();
13659 case RID_REGISTER:
13660 case RID_STATIC:
13661 case RID_EXTERN:
13662 case RID_MUTABLE:
13663 case RID_THREAD:
13664 /* Consume the token. */
13665 return cp_lexer_consume_token (parser->lexer)->u.value;
13667 default:
13668 return NULL_TREE;
13672 /* Parse an (optional) function-specifier.
13674 function-specifier:
13675 inline
13676 virtual
13677 explicit
13679 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13680 Updates DECL_SPECS, if it is non-NULL. */
13682 static tree
13683 cp_parser_function_specifier_opt (cp_parser* parser,
13684 cp_decl_specifier_seq *decl_specs)
13686 cp_token *token = cp_lexer_peek_token (parser->lexer);
13687 switch (token->keyword)
13689 case RID_INLINE:
13690 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13691 break;
13693 case RID_VIRTUAL:
13694 /* 14.5.2.3 [temp.mem]
13696 A member function template shall not be virtual. */
13697 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13698 && current_class_type)
13699 error_at (token->location, "templates may not be %<virtual%>");
13700 else
13701 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13702 break;
13704 case RID_EXPLICIT:
13705 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13706 break;
13708 default:
13709 return NULL_TREE;
13712 /* Consume the token. */
13713 return cp_lexer_consume_token (parser->lexer)->u.value;
13716 /* Parse a linkage-specification.
13718 linkage-specification:
13719 extern string-literal { declaration-seq [opt] }
13720 extern string-literal declaration */
13722 static void
13723 cp_parser_linkage_specification (cp_parser* parser)
13725 tree linkage;
13727 /* Look for the `extern' keyword. */
13728 cp_token *extern_token
13729 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13731 /* Look for the string-literal. */
13732 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13733 linkage = cp_parser_string_literal (parser, false, false);
13735 /* Transform the literal into an identifier. If the literal is a
13736 wide-character string, or contains embedded NULs, then we can't
13737 handle it as the user wants. */
13738 if (strlen (TREE_STRING_POINTER (linkage))
13739 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13741 cp_parser_error (parser, "invalid linkage-specification");
13742 /* Assume C++ linkage. */
13743 linkage = lang_name_cplusplus;
13745 else
13746 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13748 /* We're now using the new linkage. */
13749 push_lang_context (linkage);
13751 /* Preserve the location of the the innermost linkage specification,
13752 tracking the locations of nested specifications via a local. */
13753 location_t saved_location
13754 = parser->innermost_linkage_specification_location;
13755 /* Construct a location ranging from the start of the "extern" to
13756 the end of the string-literal, with the caret at the start, e.g.:
13757 extern "C" {
13758 ^~~~~~~~~~
13760 parser->innermost_linkage_specification_location
13761 = make_location (extern_token->location,
13762 extern_token->location,
13763 get_finish (string_token->location));
13765 /* If the next token is a `{', then we're using the first
13766 production. */
13767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13769 cp_ensure_no_omp_declare_simd (parser);
13770 cp_ensure_no_oacc_routine (parser);
13772 /* Consume the `{' token. */
13773 matching_braces braces;
13774 braces.consume_open (parser)->location;
13775 /* Parse the declarations. */
13776 cp_parser_declaration_seq_opt (parser);
13777 /* Look for the closing `}'. */
13778 braces.require_close (parser);
13780 /* Otherwise, there's just one declaration. */
13781 else
13783 bool saved_in_unbraced_linkage_specification_p;
13785 saved_in_unbraced_linkage_specification_p
13786 = parser->in_unbraced_linkage_specification_p;
13787 parser->in_unbraced_linkage_specification_p = true;
13788 cp_parser_declaration (parser);
13789 parser->in_unbraced_linkage_specification_p
13790 = saved_in_unbraced_linkage_specification_p;
13793 /* We're done with the linkage-specification. */
13794 pop_lang_context ();
13796 /* Restore location of parent linkage specification, if any. */
13797 parser->innermost_linkage_specification_location = saved_location;
13800 /* Parse a static_assert-declaration.
13802 static_assert-declaration:
13803 static_assert ( constant-expression , string-literal ) ;
13804 static_assert ( constant-expression ) ; (C++17)
13806 If MEMBER_P, this static_assert is a class member. */
13808 static void
13809 cp_parser_static_assert(cp_parser *parser, bool member_p)
13811 cp_expr condition;
13812 location_t token_loc;
13813 tree message;
13814 bool dummy;
13816 /* Peek at the `static_assert' token so we can keep track of exactly
13817 where the static assertion started. */
13818 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13820 /* Look for the `static_assert' keyword. */
13821 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13822 RT_STATIC_ASSERT))
13823 return;
13825 /* We know we are in a static assertion; commit to any tentative
13826 parse. */
13827 if (cp_parser_parsing_tentatively (parser))
13828 cp_parser_commit_to_tentative_parse (parser);
13830 /* Parse the `(' starting the static assertion condition. */
13831 matching_parens parens;
13832 parens.require_open (parser);
13834 /* Parse the constant-expression. Allow a non-constant expression
13835 here in order to give better diagnostics in finish_static_assert. */
13836 condition =
13837 cp_parser_constant_expression (parser,
13838 /*allow_non_constant_p=*/true,
13839 /*non_constant_p=*/&dummy);
13841 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13843 if (cxx_dialect < cxx17)
13844 pedwarn (input_location, OPT_Wpedantic,
13845 "static_assert without a message "
13846 "only available with -std=c++17 or -std=gnu++17");
13847 /* Eat the ')' */
13848 cp_lexer_consume_token (parser->lexer);
13849 message = build_string (1, "");
13850 TREE_TYPE (message) = char_array_type_node;
13851 fix_string_type (message);
13853 else
13855 /* Parse the separating `,'. */
13856 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13858 /* Parse the string-literal message. */
13859 message = cp_parser_string_literal (parser,
13860 /*translate=*/false,
13861 /*wide_ok=*/true);
13863 /* A `)' completes the static assertion. */
13864 if (!parens.require_close (parser))
13865 cp_parser_skip_to_closing_parenthesis (parser,
13866 /*recovering=*/true,
13867 /*or_comma=*/false,
13868 /*consume_paren=*/true);
13871 /* A semicolon terminates the declaration. */
13872 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13874 /* Get the location for the static assertion. Use that of the
13875 condition if available, otherwise, use that of the "static_assert"
13876 token. */
13877 location_t assert_loc = condition.get_location ();
13878 if (assert_loc == UNKNOWN_LOCATION)
13879 assert_loc = token_loc;
13881 /* Complete the static assertion, which may mean either processing
13882 the static assert now or saving it for template instantiation. */
13883 finish_static_assert (condition, message, assert_loc, member_p);
13886 /* Parse the expression in decltype ( expression ). */
13888 static tree
13889 cp_parser_decltype_expr (cp_parser *parser,
13890 bool &id_expression_or_member_access_p)
13892 cp_token *id_expr_start_token;
13893 tree expr;
13895 /* Since we're going to preserve any side-effects from this parse, set up a
13896 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13897 in the expression. */
13898 tentative_firewall firewall (parser);
13900 /* First, try parsing an id-expression. */
13901 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13902 cp_parser_parse_tentatively (parser);
13903 expr = cp_parser_id_expression (parser,
13904 /*template_keyword_p=*/false,
13905 /*check_dependency_p=*/true,
13906 /*template_p=*/NULL,
13907 /*declarator_p=*/false,
13908 /*optional_p=*/false);
13910 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13912 bool non_integral_constant_expression_p = false;
13913 tree id_expression = expr;
13914 cp_id_kind idk;
13915 const char *error_msg;
13917 if (identifier_p (expr))
13918 /* Lookup the name we got back from the id-expression. */
13919 expr = cp_parser_lookup_name_simple (parser, expr,
13920 id_expr_start_token->location);
13922 if (expr
13923 && expr != error_mark_node
13924 && TREE_CODE (expr) != TYPE_DECL
13925 && (TREE_CODE (expr) != BIT_NOT_EXPR
13926 || !TYPE_P (TREE_OPERAND (expr, 0)))
13927 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13929 /* Complete lookup of the id-expression. */
13930 expr = (finish_id_expression
13931 (id_expression, expr, parser->scope, &idk,
13932 /*integral_constant_expression_p=*/false,
13933 /*allow_non_integral_constant_expression_p=*/true,
13934 &non_integral_constant_expression_p,
13935 /*template_p=*/false,
13936 /*done=*/true,
13937 /*address_p=*/false,
13938 /*template_arg_p=*/false,
13939 &error_msg,
13940 id_expr_start_token->location));
13942 if (expr == error_mark_node)
13943 /* We found an id-expression, but it was something that we
13944 should not have found. This is an error, not something
13945 we can recover from, so note that we found an
13946 id-expression and we'll recover as gracefully as
13947 possible. */
13948 id_expression_or_member_access_p = true;
13951 if (expr
13952 && expr != error_mark_node
13953 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13954 /* We have an id-expression. */
13955 id_expression_or_member_access_p = true;
13958 if (!id_expression_or_member_access_p)
13960 /* Abort the id-expression parse. */
13961 cp_parser_abort_tentative_parse (parser);
13963 /* Parsing tentatively, again. */
13964 cp_parser_parse_tentatively (parser);
13966 /* Parse a class member access. */
13967 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13968 /*cast_p=*/false, /*decltype*/true,
13969 /*member_access_only_p=*/true, NULL);
13971 if (expr
13972 && expr != error_mark_node
13973 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13974 /* We have an id-expression. */
13975 id_expression_or_member_access_p = true;
13978 if (id_expression_or_member_access_p)
13979 /* We have parsed the complete id-expression or member access. */
13980 cp_parser_parse_definitely (parser);
13981 else
13983 /* Abort our attempt to parse an id-expression or member access
13984 expression. */
13985 cp_parser_abort_tentative_parse (parser);
13987 /* Parse a full expression. */
13988 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13989 /*decltype_p=*/true);
13992 return expr;
13995 /* Parse a `decltype' type. Returns the type.
13997 simple-type-specifier:
13998 decltype ( expression )
13999 C++14 proposal:
14000 decltype ( auto ) */
14002 static tree
14003 cp_parser_decltype (cp_parser *parser)
14005 tree expr;
14006 bool id_expression_or_member_access_p = false;
14007 const char *saved_message;
14008 bool saved_integral_constant_expression_p;
14009 bool saved_non_integral_constant_expression_p;
14010 bool saved_greater_than_is_operator_p;
14011 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14013 if (start_token->type == CPP_DECLTYPE)
14015 /* Already parsed. */
14016 cp_lexer_consume_token (parser->lexer);
14017 return saved_checks_value (start_token->u.tree_check_value);
14020 /* Look for the `decltype' token. */
14021 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14022 return error_mark_node;
14024 /* Parse the opening `('. */
14025 matching_parens parens;
14026 if (!parens.require_open (parser))
14027 return error_mark_node;
14029 /* decltype (auto) */
14030 if (cxx_dialect >= cxx14
14031 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14033 cp_lexer_consume_token (parser->lexer);
14034 if (!parens.require_close (parser))
14035 return error_mark_node;
14036 expr = make_decltype_auto ();
14037 AUTO_IS_DECLTYPE (expr) = true;
14038 goto rewrite;
14041 /* Types cannot be defined in a `decltype' expression. Save away the
14042 old message. */
14043 saved_message = parser->type_definition_forbidden_message;
14045 /* And create the new one. */
14046 parser->type_definition_forbidden_message
14047 = G_("types may not be defined in %<decltype%> expressions");
14049 /* The restrictions on constant-expressions do not apply inside
14050 decltype expressions. */
14051 saved_integral_constant_expression_p
14052 = parser->integral_constant_expression_p;
14053 saved_non_integral_constant_expression_p
14054 = parser->non_integral_constant_expression_p;
14055 parser->integral_constant_expression_p = false;
14057 /* Within a parenthesized expression, a `>' token is always
14058 the greater-than operator. */
14059 saved_greater_than_is_operator_p
14060 = parser->greater_than_is_operator_p;
14061 parser->greater_than_is_operator_p = true;
14063 /* Do not actually evaluate the expression. */
14064 ++cp_unevaluated_operand;
14066 /* Do not warn about problems with the expression. */
14067 ++c_inhibit_evaluation_warnings;
14069 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14071 /* Go back to evaluating expressions. */
14072 --cp_unevaluated_operand;
14073 --c_inhibit_evaluation_warnings;
14075 /* The `>' token might be the end of a template-id or
14076 template-parameter-list now. */
14077 parser->greater_than_is_operator_p
14078 = saved_greater_than_is_operator_p;
14080 /* Restore the old message and the integral constant expression
14081 flags. */
14082 parser->type_definition_forbidden_message = saved_message;
14083 parser->integral_constant_expression_p
14084 = saved_integral_constant_expression_p;
14085 parser->non_integral_constant_expression_p
14086 = saved_non_integral_constant_expression_p;
14088 /* Parse to the closing `)'. */
14089 if (!parens.require_close (parser))
14091 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14092 /*consume_paren=*/true);
14093 return error_mark_node;
14096 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14097 tf_warning_or_error);
14099 rewrite:
14100 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14101 it again. */
14102 start_token->type = CPP_DECLTYPE;
14103 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14104 start_token->u.tree_check_value->value = expr;
14105 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14106 start_token->keyword = RID_MAX;
14107 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14109 return expr;
14112 /* Special member functions [gram.special] */
14114 /* Parse a conversion-function-id.
14116 conversion-function-id:
14117 operator conversion-type-id
14119 Returns an IDENTIFIER_NODE representing the operator. */
14121 static tree
14122 cp_parser_conversion_function_id (cp_parser* parser)
14124 tree type;
14125 tree saved_scope;
14126 tree saved_qualifying_scope;
14127 tree saved_object_scope;
14128 tree pushed_scope = NULL_TREE;
14130 /* Look for the `operator' token. */
14131 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14132 return error_mark_node;
14133 /* When we parse the conversion-type-id, the current scope will be
14134 reset. However, we need that information in able to look up the
14135 conversion function later, so we save it here. */
14136 saved_scope = parser->scope;
14137 saved_qualifying_scope = parser->qualifying_scope;
14138 saved_object_scope = parser->object_scope;
14139 /* We must enter the scope of the class so that the names of
14140 entities declared within the class are available in the
14141 conversion-type-id. For example, consider:
14143 struct S {
14144 typedef int I;
14145 operator I();
14148 S::operator I() { ... }
14150 In order to see that `I' is a type-name in the definition, we
14151 must be in the scope of `S'. */
14152 if (saved_scope)
14153 pushed_scope = push_scope (saved_scope);
14154 /* Parse the conversion-type-id. */
14155 type = cp_parser_conversion_type_id (parser);
14156 /* Leave the scope of the class, if any. */
14157 if (pushed_scope)
14158 pop_scope (pushed_scope);
14159 /* Restore the saved scope. */
14160 parser->scope = saved_scope;
14161 parser->qualifying_scope = saved_qualifying_scope;
14162 parser->object_scope = saved_object_scope;
14163 /* If the TYPE is invalid, indicate failure. */
14164 if (type == error_mark_node)
14165 return error_mark_node;
14166 return make_conv_op_name (type);
14169 /* Parse a conversion-type-id:
14171 conversion-type-id:
14172 type-specifier-seq conversion-declarator [opt]
14174 Returns the TYPE specified. */
14176 static tree
14177 cp_parser_conversion_type_id (cp_parser* parser)
14179 tree attributes;
14180 cp_decl_specifier_seq type_specifiers;
14181 cp_declarator *declarator;
14182 tree type_specified;
14183 const char *saved_message;
14185 /* Parse the attributes. */
14186 attributes = cp_parser_attributes_opt (parser);
14188 saved_message = parser->type_definition_forbidden_message;
14189 parser->type_definition_forbidden_message
14190 = G_("types may not be defined in a conversion-type-id");
14192 /* Parse the type-specifiers. */
14193 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14194 /*is_trailing_return=*/false,
14195 &type_specifiers);
14197 parser->type_definition_forbidden_message = saved_message;
14199 /* If that didn't work, stop. */
14200 if (type_specifiers.type == error_mark_node)
14201 return error_mark_node;
14202 /* Parse the conversion-declarator. */
14203 declarator = cp_parser_conversion_declarator_opt (parser);
14205 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14206 /*initialized=*/0, &attributes);
14207 if (attributes)
14208 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14210 /* Don't give this error when parsing tentatively. This happens to
14211 work because we always parse this definitively once. */
14212 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14213 && type_uses_auto (type_specified))
14215 if (cxx_dialect < cxx14)
14217 error ("invalid use of %<auto%> in conversion operator");
14218 return error_mark_node;
14220 else if (template_parm_scope_p ())
14221 warning (0, "use of %<auto%> in member template "
14222 "conversion operator can never be deduced");
14225 return type_specified;
14228 /* Parse an (optional) conversion-declarator.
14230 conversion-declarator:
14231 ptr-operator conversion-declarator [opt]
14235 static cp_declarator *
14236 cp_parser_conversion_declarator_opt (cp_parser* parser)
14238 enum tree_code code;
14239 tree class_type, std_attributes = NULL_TREE;
14240 cp_cv_quals cv_quals;
14242 /* We don't know if there's a ptr-operator next, or not. */
14243 cp_parser_parse_tentatively (parser);
14244 /* Try the ptr-operator. */
14245 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14246 &std_attributes);
14247 /* If it worked, look for more conversion-declarators. */
14248 if (cp_parser_parse_definitely (parser))
14250 cp_declarator *declarator;
14252 /* Parse another optional declarator. */
14253 declarator = cp_parser_conversion_declarator_opt (parser);
14255 declarator = cp_parser_make_indirect_declarator
14256 (code, class_type, cv_quals, declarator, std_attributes);
14258 return declarator;
14261 return NULL;
14264 /* Parse an (optional) ctor-initializer.
14266 ctor-initializer:
14267 : mem-initializer-list */
14269 static void
14270 cp_parser_ctor_initializer_opt (cp_parser* parser)
14272 /* If the next token is not a `:', then there is no
14273 ctor-initializer. */
14274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14276 /* Do default initialization of any bases and members. */
14277 if (DECL_CONSTRUCTOR_P (current_function_decl))
14278 finish_mem_initializers (NULL_TREE);
14279 return;
14282 /* Consume the `:' token. */
14283 cp_lexer_consume_token (parser->lexer);
14284 /* And the mem-initializer-list. */
14285 cp_parser_mem_initializer_list (parser);
14288 /* Parse a mem-initializer-list.
14290 mem-initializer-list:
14291 mem-initializer ... [opt]
14292 mem-initializer ... [opt] , mem-initializer-list */
14294 static void
14295 cp_parser_mem_initializer_list (cp_parser* parser)
14297 tree mem_initializer_list = NULL_TREE;
14298 tree target_ctor = error_mark_node;
14299 cp_token *token = cp_lexer_peek_token (parser->lexer);
14301 /* Let the semantic analysis code know that we are starting the
14302 mem-initializer-list. */
14303 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14304 error_at (token->location,
14305 "only constructors take member initializers");
14307 /* Loop through the list. */
14308 while (true)
14310 tree mem_initializer;
14312 token = cp_lexer_peek_token (parser->lexer);
14313 /* Parse the mem-initializer. */
14314 mem_initializer = cp_parser_mem_initializer (parser);
14315 /* If the next token is a `...', we're expanding member initializers. */
14316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14318 /* Consume the `...'. */
14319 cp_lexer_consume_token (parser->lexer);
14321 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14322 can be expanded but members cannot. */
14323 if (mem_initializer != error_mark_node
14324 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14326 error_at (token->location,
14327 "cannot expand initializer for member %qD",
14328 TREE_PURPOSE (mem_initializer));
14329 mem_initializer = error_mark_node;
14332 /* Construct the pack expansion type. */
14333 if (mem_initializer != error_mark_node)
14334 mem_initializer = make_pack_expansion (mem_initializer);
14336 if (target_ctor != error_mark_node
14337 && mem_initializer != error_mark_node)
14339 error ("mem-initializer for %qD follows constructor delegation",
14340 TREE_PURPOSE (mem_initializer));
14341 mem_initializer = error_mark_node;
14343 /* Look for a target constructor. */
14344 if (mem_initializer != error_mark_node
14345 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14346 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14348 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14349 if (mem_initializer_list)
14351 error ("constructor delegation follows mem-initializer for %qD",
14352 TREE_PURPOSE (mem_initializer_list));
14353 mem_initializer = error_mark_node;
14355 target_ctor = mem_initializer;
14357 /* Add it to the list, unless it was erroneous. */
14358 if (mem_initializer != error_mark_node)
14360 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14361 mem_initializer_list = mem_initializer;
14363 /* If the next token is not a `,', we're done. */
14364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14365 break;
14366 /* Consume the `,' token. */
14367 cp_lexer_consume_token (parser->lexer);
14370 /* Perform semantic analysis. */
14371 if (DECL_CONSTRUCTOR_P (current_function_decl))
14372 finish_mem_initializers (mem_initializer_list);
14375 /* Parse a mem-initializer.
14377 mem-initializer:
14378 mem-initializer-id ( expression-list [opt] )
14379 mem-initializer-id braced-init-list
14381 GNU extension:
14383 mem-initializer:
14384 ( expression-list [opt] )
14386 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14387 class) or FIELD_DECL (for a non-static data member) to initialize;
14388 the TREE_VALUE is the expression-list. An empty initialization
14389 list is represented by void_list_node. */
14391 static tree
14392 cp_parser_mem_initializer (cp_parser* parser)
14394 tree mem_initializer_id;
14395 tree expression_list;
14396 tree member;
14397 cp_token *token = cp_lexer_peek_token (parser->lexer);
14399 /* Find out what is being initialized. */
14400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14402 permerror (token->location,
14403 "anachronistic old-style base class initializer");
14404 mem_initializer_id = NULL_TREE;
14406 else
14408 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14409 if (mem_initializer_id == error_mark_node)
14410 return mem_initializer_id;
14412 member = expand_member_init (mem_initializer_id);
14413 if (member && !DECL_P (member))
14414 in_base_initializer = 1;
14416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14418 bool expr_non_constant_p;
14419 cp_lexer_set_source_position (parser->lexer);
14420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14421 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14422 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14423 expression_list = build_tree_list (NULL_TREE, expression_list);
14425 else
14427 vec<tree, va_gc> *vec;
14428 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14429 /*cast_p=*/false,
14430 /*allow_expansion_p=*/true,
14431 /*non_constant_p=*/NULL);
14432 if (vec == NULL)
14433 return error_mark_node;
14434 expression_list = build_tree_list_vec (vec);
14435 release_tree_vector (vec);
14438 if (expression_list == error_mark_node)
14439 return error_mark_node;
14440 if (!expression_list)
14441 expression_list = void_type_node;
14443 in_base_initializer = 0;
14445 return member ? build_tree_list (member, expression_list) : error_mark_node;
14448 /* Parse a mem-initializer-id.
14450 mem-initializer-id:
14451 :: [opt] nested-name-specifier [opt] class-name
14452 decltype-specifier (C++11)
14453 identifier
14455 Returns a TYPE indicating the class to be initialized for the first
14456 production (and the second in C++11). Returns an IDENTIFIER_NODE
14457 indicating the data member to be initialized for the last production. */
14459 static tree
14460 cp_parser_mem_initializer_id (cp_parser* parser)
14462 bool global_scope_p;
14463 bool nested_name_specifier_p;
14464 bool template_p = false;
14465 tree id;
14467 cp_token *token = cp_lexer_peek_token (parser->lexer);
14469 /* `typename' is not allowed in this context ([temp.res]). */
14470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14472 error_at (token->location,
14473 "keyword %<typename%> not allowed in this context (a qualified "
14474 "member initializer is implicitly a type)");
14475 cp_lexer_consume_token (parser->lexer);
14477 /* Look for the optional `::' operator. */
14478 global_scope_p
14479 = (cp_parser_global_scope_opt (parser,
14480 /*current_scope_valid_p=*/false)
14481 != NULL_TREE);
14482 /* Look for the optional nested-name-specifier. The simplest way to
14483 implement:
14485 [temp.res]
14487 The keyword `typename' is not permitted in a base-specifier or
14488 mem-initializer; in these contexts a qualified name that
14489 depends on a template-parameter is implicitly assumed to be a
14490 type name.
14492 is to assume that we have seen the `typename' keyword at this
14493 point. */
14494 nested_name_specifier_p
14495 = (cp_parser_nested_name_specifier_opt (parser,
14496 /*typename_keyword_p=*/true,
14497 /*check_dependency_p=*/true,
14498 /*type_p=*/true,
14499 /*is_declaration=*/true)
14500 != NULL_TREE);
14501 if (nested_name_specifier_p)
14502 template_p = cp_parser_optional_template_keyword (parser);
14503 /* If there is a `::' operator or a nested-name-specifier, then we
14504 are definitely looking for a class-name. */
14505 if (global_scope_p || nested_name_specifier_p)
14506 return cp_parser_class_name (parser,
14507 /*typename_keyword_p=*/true,
14508 /*template_keyword_p=*/template_p,
14509 typename_type,
14510 /*check_dependency_p=*/true,
14511 /*class_head_p=*/false,
14512 /*is_declaration=*/true);
14513 /* Otherwise, we could also be looking for an ordinary identifier. */
14514 cp_parser_parse_tentatively (parser);
14515 if (cp_lexer_next_token_is_decltype (parser->lexer))
14516 /* Try a decltype-specifier. */
14517 id = cp_parser_decltype (parser);
14518 else
14519 /* Otherwise, try a class-name. */
14520 id = cp_parser_class_name (parser,
14521 /*typename_keyword_p=*/true,
14522 /*template_keyword_p=*/false,
14523 none_type,
14524 /*check_dependency_p=*/true,
14525 /*class_head_p=*/false,
14526 /*is_declaration=*/true);
14527 /* If we found one, we're done. */
14528 if (cp_parser_parse_definitely (parser))
14529 return id;
14530 /* Otherwise, look for an ordinary identifier. */
14531 return cp_parser_identifier (parser);
14534 /* Overloading [gram.over] */
14536 /* Parse an operator-function-id.
14538 operator-function-id:
14539 operator operator
14541 Returns an IDENTIFIER_NODE for the operator which is a
14542 human-readable spelling of the identifier, e.g., `operator +'. */
14544 static cp_expr
14545 cp_parser_operator_function_id (cp_parser* parser)
14547 /* Look for the `operator' keyword. */
14548 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14549 return error_mark_node;
14550 /* And then the name of the operator itself. */
14551 return cp_parser_operator (parser);
14554 /* Return an identifier node for a user-defined literal operator.
14555 The suffix identifier is chained to the operator name identifier. */
14557 tree
14558 cp_literal_operator_id (const char* name)
14560 tree identifier;
14561 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14562 + strlen (name) + 10);
14563 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14564 identifier = get_identifier (buffer);
14566 return identifier;
14569 /* Parse an operator.
14571 operator:
14572 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14573 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14574 || ++ -- , ->* -> () []
14576 GNU Extensions:
14578 operator:
14579 <? >? <?= >?=
14581 Returns an IDENTIFIER_NODE for the operator which is a
14582 human-readable spelling of the identifier, e.g., `operator +'. */
14584 static cp_expr
14585 cp_parser_operator (cp_parser* parser)
14587 tree id = NULL_TREE;
14588 cp_token *token;
14589 bool utf8 = false;
14591 /* Peek at the next token. */
14592 token = cp_lexer_peek_token (parser->lexer);
14594 location_t start_loc = token->location;
14596 /* Figure out which operator we have. */
14597 enum tree_code op = ERROR_MARK;
14598 bool assop = false;
14599 bool consumed = false;
14600 switch (token->type)
14602 case CPP_KEYWORD:
14604 /* The keyword should be either `new' or `delete'. */
14605 if (token->keyword == RID_NEW)
14606 op = NEW_EXPR;
14607 else if (token->keyword == RID_DELETE)
14608 op = DELETE_EXPR;
14609 else
14610 break;
14612 /* Consume the `new' or `delete' token. */
14613 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14615 /* Peek at the next token. */
14616 token = cp_lexer_peek_token (parser->lexer);
14617 /* If it's a `[' token then this is the array variant of the
14618 operator. */
14619 if (token->type == CPP_OPEN_SQUARE)
14621 /* Consume the `[' token. */
14622 cp_lexer_consume_token (parser->lexer);
14623 /* Look for the `]' token. */
14624 if (cp_token *close_token
14625 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14626 end_loc = close_token->location;
14627 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14629 start_loc = make_location (start_loc, start_loc, end_loc);
14630 consumed = true;
14631 break;
14634 case CPP_PLUS:
14635 op = PLUS_EXPR;
14636 break;
14638 case CPP_MINUS:
14639 op = MINUS_EXPR;
14640 break;
14642 case CPP_MULT:
14643 op = MULT_EXPR;
14644 break;
14646 case CPP_DIV:
14647 op = TRUNC_DIV_EXPR;
14648 break;
14650 case CPP_MOD:
14651 op = TRUNC_MOD_EXPR;
14652 break;
14654 case CPP_XOR:
14655 op = BIT_XOR_EXPR;
14656 break;
14658 case CPP_AND:
14659 op = BIT_AND_EXPR;
14660 break;
14662 case CPP_OR:
14663 op = BIT_IOR_EXPR;
14664 break;
14666 case CPP_COMPL:
14667 op = BIT_NOT_EXPR;
14668 break;
14670 case CPP_NOT:
14671 op = TRUTH_NOT_EXPR;
14672 break;
14674 case CPP_EQ:
14675 assop = true;
14676 op = NOP_EXPR;
14677 break;
14679 case CPP_LESS:
14680 op = LT_EXPR;
14681 break;
14683 case CPP_GREATER:
14684 op = GT_EXPR;
14685 break;
14687 case CPP_PLUS_EQ:
14688 assop = true;
14689 op = PLUS_EXPR;
14690 break;
14692 case CPP_MINUS_EQ:
14693 assop = true;
14694 op = MINUS_EXPR;
14695 break;
14697 case CPP_MULT_EQ:
14698 assop = true;
14699 op = MULT_EXPR;
14700 break;
14702 case CPP_DIV_EQ:
14703 assop = true;
14704 op = TRUNC_DIV_EXPR;
14705 break;
14707 case CPP_MOD_EQ:
14708 assop = true;
14709 op = TRUNC_MOD_EXPR;
14710 break;
14712 case CPP_XOR_EQ:
14713 assop = true;
14714 op = BIT_XOR_EXPR;
14715 break;
14717 case CPP_AND_EQ:
14718 assop = true;
14719 op = BIT_AND_EXPR;
14720 break;
14722 case CPP_OR_EQ:
14723 assop = true;
14724 op = BIT_IOR_EXPR;
14725 break;
14727 case CPP_LSHIFT:
14728 op = LSHIFT_EXPR;
14729 break;
14731 case CPP_RSHIFT:
14732 op = RSHIFT_EXPR;
14733 break;
14735 case CPP_LSHIFT_EQ:
14736 assop = true;
14737 op = LSHIFT_EXPR;
14738 break;
14740 case CPP_RSHIFT_EQ:
14741 assop = true;
14742 op = RSHIFT_EXPR;
14743 break;
14745 case CPP_EQ_EQ:
14746 op = EQ_EXPR;
14747 break;
14749 case CPP_NOT_EQ:
14750 op = NE_EXPR;
14751 break;
14753 case CPP_LESS_EQ:
14754 op = LE_EXPR;
14755 break;
14757 case CPP_GREATER_EQ:
14758 op = GE_EXPR;
14759 break;
14761 case CPP_AND_AND:
14762 op = TRUTH_ANDIF_EXPR;
14763 break;
14765 case CPP_OR_OR:
14766 op = TRUTH_ORIF_EXPR;
14767 break;
14769 case CPP_PLUS_PLUS:
14770 op = POSTINCREMENT_EXPR;
14771 break;
14773 case CPP_MINUS_MINUS:
14774 op = PREDECREMENT_EXPR;
14775 break;
14777 case CPP_COMMA:
14778 op = COMPOUND_EXPR;
14779 break;
14781 case CPP_DEREF_STAR:
14782 op = MEMBER_REF;
14783 break;
14785 case CPP_DEREF:
14786 op = COMPONENT_REF;
14787 break;
14789 case CPP_OPEN_PAREN:
14791 /* Consume the `('. */
14792 matching_parens parens;
14793 parens.consume_open (parser);
14794 /* Look for the matching `)'. */
14795 parens.require_close (parser);
14796 op = CALL_EXPR;
14797 consumed = true;
14798 break;
14801 case CPP_OPEN_SQUARE:
14802 /* Consume the `['. */
14803 cp_lexer_consume_token (parser->lexer);
14804 /* Look for the matching `]'. */
14805 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14806 op = ARRAY_REF;
14807 consumed = true;
14808 break;
14810 case CPP_UTF8STRING:
14811 case CPP_UTF8STRING_USERDEF:
14812 utf8 = true;
14813 /* FALLTHRU */
14814 case CPP_STRING:
14815 case CPP_WSTRING:
14816 case CPP_STRING16:
14817 case CPP_STRING32:
14818 case CPP_STRING_USERDEF:
14819 case CPP_WSTRING_USERDEF:
14820 case CPP_STRING16_USERDEF:
14821 case CPP_STRING32_USERDEF:
14823 tree str, string_tree;
14824 int sz, len;
14826 if (cxx_dialect == cxx98)
14827 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14829 /* Consume the string. */
14830 str = cp_parser_string_literal (parser, /*translate=*/true,
14831 /*wide_ok=*/true, /*lookup_udlit=*/false);
14832 if (str == error_mark_node)
14833 return error_mark_node;
14834 else if (TREE_CODE (str) == USERDEF_LITERAL)
14836 string_tree = USERDEF_LITERAL_VALUE (str);
14837 id = USERDEF_LITERAL_SUFFIX_ID (str);
14839 else
14841 string_tree = str;
14842 /* Look for the suffix identifier. */
14843 token = cp_lexer_peek_token (parser->lexer);
14844 if (token->type == CPP_NAME)
14845 id = cp_parser_identifier (parser);
14846 else if (token->type == CPP_KEYWORD)
14848 error ("unexpected keyword;"
14849 " remove space between quotes and suffix identifier");
14850 return error_mark_node;
14852 else
14854 error ("expected suffix identifier");
14855 return error_mark_node;
14858 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14859 (TREE_TYPE (TREE_TYPE (string_tree))));
14860 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14861 if (len != 0)
14863 error ("expected empty string after %<operator%> keyword");
14864 return error_mark_node;
14866 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14867 != char_type_node)
14869 error ("invalid encoding prefix in literal operator");
14870 return error_mark_node;
14872 if (id != error_mark_node)
14874 const char *name = IDENTIFIER_POINTER (id);
14875 id = cp_literal_operator_id (name);
14877 return id;
14880 default:
14881 /* Anything else is an error. */
14882 break;
14885 /* If we have selected an identifier, we need to consume the
14886 operator token. */
14887 if (op != ERROR_MARK)
14889 id = ovl_op_identifier (assop, op);
14890 if (!consumed)
14891 cp_lexer_consume_token (parser->lexer);
14893 /* Otherwise, no valid operator name was present. */
14894 else
14896 cp_parser_error (parser, "expected operator");
14897 id = error_mark_node;
14900 return cp_expr (id, start_loc);
14903 /* Parse a template-declaration.
14905 template-declaration:
14906 export [opt] template < template-parameter-list > declaration
14908 If MEMBER_P is TRUE, this template-declaration occurs within a
14909 class-specifier.
14911 The grammar rule given by the standard isn't correct. What
14912 is really meant is:
14914 template-declaration:
14915 export [opt] template-parameter-list-seq
14916 decl-specifier-seq [opt] init-declarator [opt] ;
14917 export [opt] template-parameter-list-seq
14918 function-definition
14920 template-parameter-list-seq:
14921 template-parameter-list-seq [opt]
14922 template < template-parameter-list >
14924 Concept Extensions:
14926 template-parameter-list-seq:
14927 template < template-parameter-list > requires-clause [opt]
14929 requires-clause:
14930 requires logical-or-expression */
14932 static void
14933 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14935 /* Check for `export'. */
14936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14938 /* Consume the `export' token. */
14939 cp_lexer_consume_token (parser->lexer);
14940 /* Warn that we do not support `export'. */
14941 warning (0, "keyword %<export%> not implemented, and will be ignored");
14944 cp_parser_template_declaration_after_export (parser, member_p);
14947 /* Parse a template-parameter-list.
14949 template-parameter-list:
14950 template-parameter
14951 template-parameter-list , template-parameter
14953 Returns a TREE_LIST. Each node represents a template parameter.
14954 The nodes are connected via their TREE_CHAINs. */
14956 static tree
14957 cp_parser_template_parameter_list (cp_parser* parser)
14959 tree parameter_list = NULL_TREE;
14961 begin_template_parm_list ();
14963 /* The loop below parses the template parms. We first need to know
14964 the total number of template parms to be able to compute proper
14965 canonical types of each dependent type. So after the loop, when
14966 we know the total number of template parms,
14967 end_template_parm_list computes the proper canonical types and
14968 fixes up the dependent types accordingly. */
14969 while (true)
14971 tree parameter;
14972 bool is_non_type;
14973 bool is_parameter_pack;
14974 location_t parm_loc;
14976 /* Parse the template-parameter. */
14977 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14978 parameter = cp_parser_template_parameter (parser,
14979 &is_non_type,
14980 &is_parameter_pack);
14981 /* Add it to the list. */
14982 if (parameter != error_mark_node)
14983 parameter_list = process_template_parm (parameter_list,
14984 parm_loc,
14985 parameter,
14986 is_non_type,
14987 is_parameter_pack);
14988 else
14990 tree err_parm = build_tree_list (parameter, parameter);
14991 parameter_list = chainon (parameter_list, err_parm);
14994 /* If the next token is not a `,', we're done. */
14995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14996 break;
14997 /* Otherwise, consume the `,' token. */
14998 cp_lexer_consume_token (parser->lexer);
15001 return end_template_parm_list (parameter_list);
15004 /* Parse a introduction-list.
15006 introduction-list:
15007 introduced-parameter
15008 introduction-list , introduced-parameter
15010 introduced-parameter:
15011 ...[opt] identifier
15013 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15014 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15015 WILDCARD_DECL will also have DECL_NAME set and token location in
15016 DECL_SOURCE_LOCATION. */
15018 static tree
15019 cp_parser_introduction_list (cp_parser *parser)
15021 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15023 while (true)
15025 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15026 if (is_pack)
15027 cp_lexer_consume_token (parser->lexer);
15029 /* Build placeholder. */
15030 tree parm = build_nt (WILDCARD_DECL);
15031 DECL_SOURCE_LOCATION (parm)
15032 = cp_lexer_peek_token (parser->lexer)->location;
15033 DECL_NAME (parm) = cp_parser_identifier (parser);
15034 WILDCARD_PACK_P (parm) = is_pack;
15035 vec_safe_push (introduction_vec, parm);
15037 /* If the next token is not a `,', we're done. */
15038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15039 break;
15040 /* Otherwise, consume the `,' token. */
15041 cp_lexer_consume_token (parser->lexer);
15044 /* Convert the vec into a TREE_VEC. */
15045 tree introduction_list = make_tree_vec (introduction_vec->length ());
15046 unsigned int n;
15047 tree parm;
15048 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15049 TREE_VEC_ELT (introduction_list, n) = parm;
15051 release_tree_vector (introduction_vec);
15052 return introduction_list;
15055 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15056 is an abstract declarator. */
15058 static inline cp_declarator*
15059 get_id_declarator (cp_declarator *declarator)
15061 cp_declarator *d = declarator;
15062 while (d && d->kind != cdk_id)
15063 d = d->declarator;
15064 return d;
15067 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15068 is an abstract declarator. */
15070 static inline tree
15071 get_unqualified_id (cp_declarator *declarator)
15073 declarator = get_id_declarator (declarator);
15074 if (declarator)
15075 return declarator->u.id.unqualified_name;
15076 else
15077 return NULL_TREE;
15080 /* Returns true if DECL represents a constrained-parameter. */
15082 static inline bool
15083 is_constrained_parameter (tree decl)
15085 return (decl
15086 && TREE_CODE (decl) == TYPE_DECL
15087 && CONSTRAINED_PARM_CONCEPT (decl)
15088 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15091 /* Returns true if PARM declares a constrained-parameter. */
15093 static inline bool
15094 is_constrained_parameter (cp_parameter_declarator *parm)
15096 return is_constrained_parameter (parm->decl_specifiers.type);
15099 /* Check that the type parameter is only a declarator-id, and that its
15100 type is not cv-qualified. */
15102 bool
15103 cp_parser_check_constrained_type_parm (cp_parser *parser,
15104 cp_parameter_declarator *parm)
15106 if (!parm->declarator)
15107 return true;
15109 if (parm->declarator->kind != cdk_id)
15111 cp_parser_error (parser, "invalid constrained type parameter");
15112 return false;
15115 /* Don't allow cv-qualified type parameters. */
15116 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15117 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15119 cp_parser_error (parser, "cv-qualified type parameter");
15120 return false;
15123 return true;
15126 /* Finish parsing/processing a template type parameter and checking
15127 various restrictions. */
15129 static inline tree
15130 cp_parser_constrained_type_template_parm (cp_parser *parser,
15131 tree id,
15132 cp_parameter_declarator* parmdecl)
15134 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15135 return finish_template_type_parm (class_type_node, id);
15136 else
15137 return error_mark_node;
15140 static tree
15141 finish_constrained_template_template_parm (tree proto, tree id)
15143 /* FIXME: This should probably be copied, and we may need to adjust
15144 the template parameter depths. */
15145 tree saved_parms = current_template_parms;
15146 begin_template_parm_list ();
15147 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15148 end_template_parm_list ();
15150 tree parm = finish_template_template_parm (class_type_node, id);
15151 current_template_parms = saved_parms;
15153 return parm;
15156 /* Finish parsing/processing a template template parameter by borrowing
15157 the template parameter list from the prototype parameter. */
15159 static tree
15160 cp_parser_constrained_template_template_parm (cp_parser *parser,
15161 tree proto,
15162 tree id,
15163 cp_parameter_declarator *parmdecl)
15165 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15166 return error_mark_node;
15167 return finish_constrained_template_template_parm (proto, id);
15170 /* Create a new non-type template parameter from the given PARM
15171 declarator. */
15173 static tree
15174 constrained_non_type_template_parm (bool *is_non_type,
15175 cp_parameter_declarator *parm)
15177 *is_non_type = true;
15178 cp_declarator *decl = parm->declarator;
15179 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15180 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15181 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15184 /* Build a constrained template parameter based on the PARMDECL
15185 declarator. The type of PARMDECL is the constrained type, which
15186 refers to the prototype template parameter that ultimately
15187 specifies the type of the declared parameter. */
15189 static tree
15190 finish_constrained_parameter (cp_parser *parser,
15191 cp_parameter_declarator *parmdecl,
15192 bool *is_non_type,
15193 bool *is_parameter_pack)
15195 tree decl = parmdecl->decl_specifiers.type;
15196 tree id = get_unqualified_id (parmdecl->declarator);
15197 tree def = parmdecl->default_argument;
15198 tree proto = DECL_INITIAL (decl);
15200 /* A template parameter constrained by a variadic concept shall also
15201 be declared as a template parameter pack. */
15202 bool is_variadic = template_parameter_pack_p (proto);
15203 if (is_variadic && !*is_parameter_pack)
15204 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15206 /* Build the parameter. Return an error if the declarator was invalid. */
15207 tree parm;
15208 if (TREE_CODE (proto) == TYPE_DECL)
15209 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15210 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15211 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15212 parmdecl);
15213 else
15214 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15215 if (parm == error_mark_node)
15216 return error_mark_node;
15218 /* Finish the parameter decl and create a node attaching the
15219 default argument and constraint. */
15220 parm = build_tree_list (def, parm);
15221 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15223 return parm;
15226 /* Returns true if the parsed type actually represents the declaration
15227 of a type template-parameter. */
15229 static inline bool
15230 declares_constrained_type_template_parameter (tree type)
15232 return (is_constrained_parameter (type)
15233 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15237 /* Returns true if the parsed type actually represents the declaration of
15238 a template template-parameter. */
15240 static bool
15241 declares_constrained_template_template_parameter (tree type)
15243 return (is_constrained_parameter (type)
15244 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15247 /* Parse a default argument for a type template-parameter.
15248 Note that diagnostics are handled in cp_parser_template_parameter. */
15250 static tree
15251 cp_parser_default_type_template_argument (cp_parser *parser)
15253 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15255 /* Consume the `=' token. */
15256 cp_lexer_consume_token (parser->lexer);
15258 cp_token *token = cp_lexer_peek_token (parser->lexer);
15260 /* Parse the default-argument. */
15261 push_deferring_access_checks (dk_no_deferred);
15262 tree default_argument = cp_parser_type_id (parser);
15263 pop_deferring_access_checks ();
15265 if (flag_concepts && type_uses_auto (default_argument))
15267 error_at (token->location,
15268 "invalid use of %<auto%> in default template argument");
15269 return error_mark_node;
15272 return default_argument;
15275 /* Parse a default argument for a template template-parameter. */
15277 static tree
15278 cp_parser_default_template_template_argument (cp_parser *parser)
15280 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15282 bool is_template;
15284 /* Consume the `='. */
15285 cp_lexer_consume_token (parser->lexer);
15286 /* Parse the id-expression. */
15287 push_deferring_access_checks (dk_no_deferred);
15288 /* save token before parsing the id-expression, for error
15289 reporting */
15290 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15291 tree default_argument
15292 = cp_parser_id_expression (parser,
15293 /*template_keyword_p=*/false,
15294 /*check_dependency_p=*/true,
15295 /*template_p=*/&is_template,
15296 /*declarator_p=*/false,
15297 /*optional_p=*/false);
15298 if (TREE_CODE (default_argument) == TYPE_DECL)
15299 /* If the id-expression was a template-id that refers to
15300 a template-class, we already have the declaration here,
15301 so no further lookup is needed. */
15303 else
15304 /* Look up the name. */
15305 default_argument
15306 = cp_parser_lookup_name (parser, default_argument,
15307 none_type,
15308 /*is_template=*/is_template,
15309 /*is_namespace=*/false,
15310 /*check_dependency=*/true,
15311 /*ambiguous_decls=*/NULL,
15312 token->location);
15313 /* See if the default argument is valid. */
15314 default_argument = check_template_template_default_arg (default_argument);
15315 pop_deferring_access_checks ();
15316 return default_argument;
15319 /* Parse a template-parameter.
15321 template-parameter:
15322 type-parameter
15323 parameter-declaration
15325 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15326 the parameter. The TREE_PURPOSE is the default value, if any.
15327 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15328 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15329 set to true iff this parameter is a parameter pack. */
15331 static tree
15332 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15333 bool *is_parameter_pack)
15335 cp_token *token;
15336 cp_parameter_declarator *parameter_declarator;
15337 tree parm;
15339 /* Assume it is a type parameter or a template parameter. */
15340 *is_non_type = false;
15341 /* Assume it not a parameter pack. */
15342 *is_parameter_pack = false;
15343 /* Peek at the next token. */
15344 token = cp_lexer_peek_token (parser->lexer);
15345 /* If it is `template', we have a type-parameter. */
15346 if (token->keyword == RID_TEMPLATE)
15347 return cp_parser_type_parameter (parser, is_parameter_pack);
15348 /* If it is `class' or `typename' we do not know yet whether it is a
15349 type parameter or a non-type parameter. Consider:
15351 template <typename T, typename T::X X> ...
15355 template <class C, class D*> ...
15357 Here, the first parameter is a type parameter, and the second is
15358 a non-type parameter. We can tell by looking at the token after
15359 the identifier -- if it is a `,', `=', or `>' then we have a type
15360 parameter. */
15361 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15363 /* Peek at the token after `class' or `typename'. */
15364 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15365 /* If it's an ellipsis, we have a template type parameter
15366 pack. */
15367 if (token->type == CPP_ELLIPSIS)
15368 return cp_parser_type_parameter (parser, is_parameter_pack);
15369 /* If it's an identifier, skip it. */
15370 if (token->type == CPP_NAME)
15371 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15372 /* Now, see if the token looks like the end of a template
15373 parameter. */
15374 if (token->type == CPP_COMMA
15375 || token->type == CPP_EQ
15376 || token->type == CPP_GREATER)
15377 return cp_parser_type_parameter (parser, is_parameter_pack);
15380 /* Otherwise, it is a non-type parameter or a constrained parameter.
15382 [temp.param]
15384 When parsing a default template-argument for a non-type
15385 template-parameter, the first non-nested `>' is taken as the end
15386 of the template parameter-list rather than a greater-than
15387 operator. */
15388 parameter_declarator
15389 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15390 /*parenthesized_p=*/NULL);
15392 if (!parameter_declarator)
15393 return error_mark_node;
15395 /* If the parameter declaration is marked as a parameter pack, set
15396 *IS_PARAMETER_PACK to notify the caller. */
15397 if (parameter_declarator->template_parameter_pack_p)
15398 *is_parameter_pack = true;
15400 if (parameter_declarator->default_argument)
15402 /* Can happen in some cases of erroneous input (c++/34892). */
15403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15404 /* Consume the `...' for better error recovery. */
15405 cp_lexer_consume_token (parser->lexer);
15408 // The parameter may have been constrained.
15409 if (is_constrained_parameter (parameter_declarator))
15410 return finish_constrained_parameter (parser,
15411 parameter_declarator,
15412 is_non_type,
15413 is_parameter_pack);
15415 // Now we're sure that the parameter is a non-type parameter.
15416 *is_non_type = true;
15418 parm = grokdeclarator (parameter_declarator->declarator,
15419 &parameter_declarator->decl_specifiers,
15420 TPARM, /*initialized=*/0,
15421 /*attrlist=*/NULL);
15422 if (parm == error_mark_node)
15423 return error_mark_node;
15425 return build_tree_list (parameter_declarator->default_argument, parm);
15428 /* Parse a type-parameter.
15430 type-parameter:
15431 class identifier [opt]
15432 class identifier [opt] = type-id
15433 typename identifier [opt]
15434 typename identifier [opt] = type-id
15435 template < template-parameter-list > class identifier [opt]
15436 template < template-parameter-list > class identifier [opt]
15437 = id-expression
15439 GNU Extension (variadic templates):
15441 type-parameter:
15442 class ... identifier [opt]
15443 typename ... identifier [opt]
15445 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15446 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15447 the declaration of the parameter.
15449 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15451 static tree
15452 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15454 cp_token *token;
15455 tree parameter;
15457 /* Look for a keyword to tell us what kind of parameter this is. */
15458 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15459 if (!token)
15460 return error_mark_node;
15462 switch (token->keyword)
15464 case RID_CLASS:
15465 case RID_TYPENAME:
15467 tree identifier;
15468 tree default_argument;
15470 /* If the next token is an ellipsis, we have a template
15471 argument pack. */
15472 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15474 /* Consume the `...' token. */
15475 cp_lexer_consume_token (parser->lexer);
15476 maybe_warn_variadic_templates ();
15478 *is_parameter_pack = true;
15481 /* If the next token is an identifier, then it names the
15482 parameter. */
15483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15484 identifier = cp_parser_identifier (parser);
15485 else
15486 identifier = NULL_TREE;
15488 /* Create the parameter. */
15489 parameter = finish_template_type_parm (class_type_node, identifier);
15491 /* If the next token is an `=', we have a default argument. */
15492 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15494 default_argument
15495 = cp_parser_default_type_template_argument (parser);
15497 /* Template parameter packs cannot have default
15498 arguments. */
15499 if (*is_parameter_pack)
15501 if (identifier)
15502 error_at (token->location,
15503 "template parameter pack %qD cannot have a "
15504 "default argument", identifier);
15505 else
15506 error_at (token->location,
15507 "template parameter packs cannot have "
15508 "default arguments");
15509 default_argument = NULL_TREE;
15511 else if (check_for_bare_parameter_packs (default_argument))
15512 default_argument = error_mark_node;
15514 else
15515 default_argument = NULL_TREE;
15517 /* Create the combined representation of the parameter and the
15518 default argument. */
15519 parameter = build_tree_list (default_argument, parameter);
15521 break;
15523 case RID_TEMPLATE:
15525 tree identifier;
15526 tree default_argument;
15528 /* Look for the `<'. */
15529 cp_parser_require (parser, CPP_LESS, RT_LESS);
15530 /* Parse the template-parameter-list. */
15531 cp_parser_template_parameter_list (parser);
15532 /* Look for the `>'. */
15533 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15535 // If template requirements are present, parse them.
15536 if (flag_concepts)
15538 tree reqs = get_shorthand_constraints (current_template_parms);
15539 if (tree r = cp_parser_requires_clause_opt (parser))
15540 reqs = conjoin_constraints (reqs, normalize_expression (r));
15541 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15544 /* Look for the `class' or 'typename' keywords. */
15545 cp_parser_type_parameter_key (parser);
15546 /* If the next token is an ellipsis, we have a template
15547 argument pack. */
15548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15550 /* Consume the `...' token. */
15551 cp_lexer_consume_token (parser->lexer);
15552 maybe_warn_variadic_templates ();
15554 *is_parameter_pack = true;
15556 /* If the next token is an `=', then there is a
15557 default-argument. If the next token is a `>', we are at
15558 the end of the parameter-list. If the next token is a `,',
15559 then we are at the end of this parameter. */
15560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15561 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15562 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15564 identifier = cp_parser_identifier (parser);
15565 /* Treat invalid names as if the parameter were nameless. */
15566 if (identifier == error_mark_node)
15567 identifier = NULL_TREE;
15569 else
15570 identifier = NULL_TREE;
15572 /* Create the template parameter. */
15573 parameter = finish_template_template_parm (class_type_node,
15574 identifier);
15576 /* If the next token is an `=', then there is a
15577 default-argument. */
15578 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15580 default_argument
15581 = cp_parser_default_template_template_argument (parser);
15583 /* Template parameter packs cannot have default
15584 arguments. */
15585 if (*is_parameter_pack)
15587 if (identifier)
15588 error_at (token->location,
15589 "template parameter pack %qD cannot "
15590 "have a default argument",
15591 identifier);
15592 else
15593 error_at (token->location, "template parameter packs cannot "
15594 "have default arguments");
15595 default_argument = NULL_TREE;
15598 else
15599 default_argument = NULL_TREE;
15601 /* Create the combined representation of the parameter and the
15602 default argument. */
15603 parameter = build_tree_list (default_argument, parameter);
15605 break;
15607 default:
15608 gcc_unreachable ();
15609 break;
15612 return parameter;
15615 /* Parse a template-id.
15617 template-id:
15618 template-name < template-argument-list [opt] >
15620 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15621 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15622 returned. Otherwise, if the template-name names a function, or set
15623 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15624 names a class, returns a TYPE_DECL for the specialization.
15626 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15627 uninstantiated templates. */
15629 static tree
15630 cp_parser_template_id (cp_parser *parser,
15631 bool template_keyword_p,
15632 bool check_dependency_p,
15633 enum tag_types tag_type,
15634 bool is_declaration)
15636 tree templ;
15637 tree arguments;
15638 tree template_id;
15639 cp_token_position start_of_id = 0;
15640 cp_token *next_token = NULL, *next_token_2 = NULL;
15641 bool is_identifier;
15643 /* If the next token corresponds to a template-id, there is no need
15644 to reparse it. */
15645 cp_token *token = cp_lexer_peek_token (parser->lexer);
15646 if (token->type == CPP_TEMPLATE_ID)
15648 cp_lexer_consume_token (parser->lexer);
15649 return saved_checks_value (token->u.tree_check_value);
15652 /* Avoid performing name lookup if there is no possibility of
15653 finding a template-id. */
15654 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15655 || (token->type == CPP_NAME
15656 && !cp_parser_nth_token_starts_template_argument_list_p
15657 (parser, 2)))
15659 cp_parser_error (parser, "expected template-id");
15660 return error_mark_node;
15663 /* Remember where the template-id starts. */
15664 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15665 start_of_id = cp_lexer_token_position (parser->lexer, false);
15667 push_deferring_access_checks (dk_deferred);
15669 /* Parse the template-name. */
15670 is_identifier = false;
15671 templ = cp_parser_template_name (parser, template_keyword_p,
15672 check_dependency_p,
15673 is_declaration,
15674 tag_type,
15675 &is_identifier);
15676 if (templ == error_mark_node || is_identifier)
15678 pop_deferring_access_checks ();
15679 return templ;
15682 /* Since we're going to preserve any side-effects from this parse, set up a
15683 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15684 in the template arguments. */
15685 tentative_firewall firewall (parser);
15687 /* If we find the sequence `[:' after a template-name, it's probably
15688 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15689 parse correctly the argument list. */
15690 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15691 == CPP_OPEN_SQUARE)
15692 && next_token->flags & DIGRAPH
15693 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15694 == CPP_COLON)
15695 && !(next_token_2->flags & PREV_WHITE))
15697 cp_parser_parse_tentatively (parser);
15698 /* Change `:' into `::'. */
15699 next_token_2->type = CPP_SCOPE;
15700 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15701 CPP_LESS. */
15702 cp_lexer_consume_token (parser->lexer);
15704 /* Parse the arguments. */
15705 arguments = cp_parser_enclosed_template_argument_list (parser);
15706 if (!cp_parser_parse_definitely (parser))
15708 /* If we couldn't parse an argument list, then we revert our changes
15709 and return simply an error. Maybe this is not a template-id
15710 after all. */
15711 next_token_2->type = CPP_COLON;
15712 cp_parser_error (parser, "expected %<<%>");
15713 pop_deferring_access_checks ();
15714 return error_mark_node;
15716 /* Otherwise, emit an error about the invalid digraph, but continue
15717 parsing because we got our argument list. */
15718 if (permerror (next_token->location,
15719 "%<<::%> cannot begin a template-argument list"))
15721 static bool hint = false;
15722 inform (next_token->location,
15723 "%<<:%> is an alternate spelling for %<[%>."
15724 " Insert whitespace between %<<%> and %<::%>");
15725 if (!hint && !flag_permissive)
15727 inform (next_token->location, "(if you use %<-fpermissive%> "
15728 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15729 "accept your code)");
15730 hint = true;
15734 else
15736 /* Look for the `<' that starts the template-argument-list. */
15737 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15739 pop_deferring_access_checks ();
15740 return error_mark_node;
15742 /* Parse the arguments. */
15743 arguments = cp_parser_enclosed_template_argument_list (parser);
15746 /* Set the location to be of the form:
15747 template-name < template-argument-list [opt] >
15748 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15749 with caret == start at the start of the template-name,
15750 ranging until the closing '>'. */
15751 location_t finish_loc
15752 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15753 location_t combined_loc
15754 = make_location (token->location, token->location, finish_loc);
15756 /* Build a representation of the specialization. */
15757 if (identifier_p (templ))
15758 template_id = build_min_nt_loc (combined_loc,
15759 TEMPLATE_ID_EXPR,
15760 templ, arguments);
15761 else if (DECL_TYPE_TEMPLATE_P (templ)
15762 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15764 bool entering_scope;
15765 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15766 template (rather than some instantiation thereof) only if
15767 is not nested within some other construct. For example, in
15768 "template <typename T> void f(T) { A<T>::", A<T> is just an
15769 instantiation of A. */
15770 entering_scope = (template_parm_scope_p ()
15771 && cp_lexer_next_token_is (parser->lexer,
15772 CPP_SCOPE));
15773 template_id
15774 = finish_template_type (templ, arguments, entering_scope);
15776 /* A template-like identifier may be a partial concept id. */
15777 else if (flag_concepts
15778 && (template_id = (cp_parser_maybe_partial_concept_id
15779 (parser, templ, arguments))))
15780 return template_id;
15781 else if (variable_template_p (templ))
15783 template_id = lookup_template_variable (templ, arguments);
15784 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15785 SET_EXPR_LOCATION (template_id, combined_loc);
15787 else
15789 /* If it's not a class-template or a template-template, it should be
15790 a function-template. */
15791 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15792 || TREE_CODE (templ) == OVERLOAD
15793 || BASELINK_P (templ)));
15795 template_id = lookup_template_function (templ, arguments);
15796 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15797 SET_EXPR_LOCATION (template_id, combined_loc);
15800 /* If parsing tentatively, replace the sequence of tokens that makes
15801 up the template-id with a CPP_TEMPLATE_ID token. That way,
15802 should we re-parse the token stream, we will not have to repeat
15803 the effort required to do the parse, nor will we issue duplicate
15804 error messages about problems during instantiation of the
15805 template. */
15806 if (start_of_id
15807 /* Don't do this if we had a parse error in a declarator; re-parsing
15808 might succeed if a name changes meaning (60361). */
15809 && !(cp_parser_error_occurred (parser)
15810 && cp_parser_parsing_tentatively (parser)
15811 && parser->in_declarator_p))
15813 /* Reset the contents of the START_OF_ID token. */
15814 token->type = CPP_TEMPLATE_ID;
15815 token->location = combined_loc;
15817 /* We must mark the lookup as kept, so we don't throw it away on
15818 the first parse. */
15819 if (is_overloaded_fn (template_id))
15820 lookup_keep (get_fns (template_id), true);
15822 /* Retrieve any deferred checks. Do not pop this access checks yet
15823 so the memory will not be reclaimed during token replacing below. */
15824 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15825 token->u.tree_check_value->value = template_id;
15826 token->u.tree_check_value->checks = get_deferred_access_checks ();
15827 token->keyword = RID_MAX;
15829 /* Purge all subsequent tokens. */
15830 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15832 /* ??? Can we actually assume that, if template_id ==
15833 error_mark_node, we will have issued a diagnostic to the
15834 user, as opposed to simply marking the tentative parse as
15835 failed? */
15836 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15837 error_at (token->location, "parse error in template argument list");
15840 pop_to_parent_deferring_access_checks ();
15841 return template_id;
15844 /* Parse a template-name.
15846 template-name:
15847 identifier
15849 The standard should actually say:
15851 template-name:
15852 identifier
15853 operator-function-id
15855 A defect report has been filed about this issue.
15857 A conversion-function-id cannot be a template name because they cannot
15858 be part of a template-id. In fact, looking at this code:
15860 a.operator K<int>()
15862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15863 It is impossible to call a templated conversion-function-id with an
15864 explicit argument list, since the only allowed template parameter is
15865 the type to which it is converting.
15867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15868 `template' keyword, in a construction like:
15870 T::template f<3>()
15872 In that case `f' is taken to be a template-name, even though there
15873 is no way of knowing for sure.
15875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15876 name refers to a set of overloaded functions, at least one of which
15877 is a template, or an IDENTIFIER_NODE with the name of the template,
15878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15879 names are looked up inside uninstantiated templates. */
15881 static tree
15882 cp_parser_template_name (cp_parser* parser,
15883 bool template_keyword_p,
15884 bool check_dependency_p,
15885 bool is_declaration,
15886 enum tag_types tag_type,
15887 bool *is_identifier)
15889 tree identifier;
15890 tree decl;
15891 cp_token *token = cp_lexer_peek_token (parser->lexer);
15893 /* If the next token is `operator', then we have either an
15894 operator-function-id or a conversion-function-id. */
15895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15897 /* We don't know whether we're looking at an
15898 operator-function-id or a conversion-function-id. */
15899 cp_parser_parse_tentatively (parser);
15900 /* Try an operator-function-id. */
15901 identifier = cp_parser_operator_function_id (parser);
15902 /* If that didn't work, try a conversion-function-id. */
15903 if (!cp_parser_parse_definitely (parser))
15905 cp_parser_error (parser, "expected template-name");
15906 return error_mark_node;
15909 /* Look for the identifier. */
15910 else
15911 identifier = cp_parser_identifier (parser);
15913 /* If we didn't find an identifier, we don't have a template-id. */
15914 if (identifier == error_mark_node)
15915 return error_mark_node;
15917 /* If the name immediately followed the `template' keyword, then it
15918 is a template-name. However, if the next token is not `<', then
15919 we do not treat it as a template-name, since it is not being used
15920 as part of a template-id. This enables us to handle constructs
15921 like:
15923 template <typename T> struct S { S(); };
15924 template <typename T> S<T>::S();
15926 correctly. We would treat `S' as a template -- if it were `S<T>'
15927 -- but we do not if there is no `<'. */
15929 if (processing_template_decl
15930 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15932 /* In a declaration, in a dependent context, we pretend that the
15933 "template" keyword was present in order to improve error
15934 recovery. For example, given:
15936 template <typename T> void f(T::X<int>);
15938 we want to treat "X<int>" as a template-id. */
15939 if (is_declaration
15940 && !template_keyword_p
15941 && parser->scope && TYPE_P (parser->scope)
15942 && check_dependency_p
15943 && dependent_scope_p (parser->scope)
15944 /* Do not do this for dtors (or ctors), since they never
15945 need the template keyword before their name. */
15946 && !constructor_name_p (identifier, parser->scope))
15948 cp_token_position start = 0;
15950 /* Explain what went wrong. */
15951 error_at (token->location, "non-template %qD used as template",
15952 identifier);
15953 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15954 parser->scope, identifier);
15955 /* If parsing tentatively, find the location of the "<" token. */
15956 if (cp_parser_simulate_error (parser))
15957 start = cp_lexer_token_position (parser->lexer, true);
15958 /* Parse the template arguments so that we can issue error
15959 messages about them. */
15960 cp_lexer_consume_token (parser->lexer);
15961 cp_parser_enclosed_template_argument_list (parser);
15962 /* Skip tokens until we find a good place from which to
15963 continue parsing. */
15964 cp_parser_skip_to_closing_parenthesis (parser,
15965 /*recovering=*/true,
15966 /*or_comma=*/true,
15967 /*consume_paren=*/false);
15968 /* If parsing tentatively, permanently remove the
15969 template argument list. That will prevent duplicate
15970 error messages from being issued about the missing
15971 "template" keyword. */
15972 if (start)
15973 cp_lexer_purge_tokens_after (parser->lexer, start);
15974 if (is_identifier)
15975 *is_identifier = true;
15976 parser->context->object_type = NULL_TREE;
15977 return identifier;
15980 /* If the "template" keyword is present, then there is generally
15981 no point in doing name-lookup, so we just return IDENTIFIER.
15982 But, if the qualifying scope is non-dependent then we can
15983 (and must) do name-lookup normally. */
15984 if (template_keyword_p)
15986 tree scope = (parser->scope ? parser->scope
15987 : parser->context->object_type);
15988 if (scope && TYPE_P (scope)
15989 && (!CLASS_TYPE_P (scope)
15990 || (check_dependency_p && dependent_type_p (scope))))
15992 /* We're optimizing away the call to cp_parser_lookup_name, but
15993 we still need to do this. */
15994 parser->context->object_type = NULL_TREE;
15995 return identifier;
16000 /* Look up the name. */
16001 decl = cp_parser_lookup_name (parser, identifier,
16002 tag_type,
16003 /*is_template=*/true,
16004 /*is_namespace=*/false,
16005 check_dependency_p,
16006 /*ambiguous_decls=*/NULL,
16007 token->location);
16009 decl = strip_using_decl (decl);
16011 /* If DECL is a template, then the name was a template-name. */
16012 if (TREE_CODE (decl) == TEMPLATE_DECL)
16014 if (TREE_DEPRECATED (decl)
16015 && deprecated_state != DEPRECATED_SUPPRESS)
16016 warn_deprecated_use (decl, NULL_TREE);
16018 else
16020 /* The standard does not explicitly indicate whether a name that
16021 names a set of overloaded declarations, some of which are
16022 templates, is a template-name. However, such a name should
16023 be a template-name; otherwise, there is no way to form a
16024 template-id for the overloaded templates. */
16025 bool found = false;
16027 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16028 !found && iter; ++iter)
16029 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16030 found = true;
16032 if (!found)
16034 /* The name does not name a template. */
16035 cp_parser_error (parser, "expected template-name");
16036 return error_mark_node;
16040 /* If DECL is dependent, and refers to a function, then just return
16041 its name; we will look it up again during template instantiation. */
16042 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16044 tree scope = ovl_scope (decl);
16045 if (TYPE_P (scope) && dependent_type_p (scope))
16046 return identifier;
16049 return decl;
16052 /* Parse a template-argument-list.
16054 template-argument-list:
16055 template-argument ... [opt]
16056 template-argument-list , template-argument ... [opt]
16058 Returns a TREE_VEC containing the arguments. */
16060 static tree
16061 cp_parser_template_argument_list (cp_parser* parser)
16063 tree fixed_args[10];
16064 unsigned n_args = 0;
16065 unsigned alloced = 10;
16066 tree *arg_ary = fixed_args;
16067 tree vec;
16068 bool saved_in_template_argument_list_p;
16069 bool saved_ice_p;
16070 bool saved_non_ice_p;
16072 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16073 parser->in_template_argument_list_p = true;
16074 /* Even if the template-id appears in an integral
16075 constant-expression, the contents of the argument list do
16076 not. */
16077 saved_ice_p = parser->integral_constant_expression_p;
16078 parser->integral_constant_expression_p = false;
16079 saved_non_ice_p = parser->non_integral_constant_expression_p;
16080 parser->non_integral_constant_expression_p = false;
16082 /* Parse the arguments. */
16085 tree argument;
16087 if (n_args)
16088 /* Consume the comma. */
16089 cp_lexer_consume_token (parser->lexer);
16091 /* Parse the template-argument. */
16092 argument = cp_parser_template_argument (parser);
16094 /* If the next token is an ellipsis, we're expanding a template
16095 argument pack. */
16096 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16098 if (argument == error_mark_node)
16100 cp_token *token = cp_lexer_peek_token (parser->lexer);
16101 error_at (token->location,
16102 "expected parameter pack before %<...%>");
16104 /* Consume the `...' token. */
16105 cp_lexer_consume_token (parser->lexer);
16107 /* Make the argument into a TYPE_PACK_EXPANSION or
16108 EXPR_PACK_EXPANSION. */
16109 argument = make_pack_expansion (argument);
16112 if (n_args == alloced)
16114 alloced *= 2;
16116 if (arg_ary == fixed_args)
16118 arg_ary = XNEWVEC (tree, alloced);
16119 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16121 else
16122 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16124 arg_ary[n_args++] = argument;
16126 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16128 vec = make_tree_vec (n_args);
16130 while (n_args--)
16131 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16133 if (arg_ary != fixed_args)
16134 free (arg_ary);
16135 parser->non_integral_constant_expression_p = saved_non_ice_p;
16136 parser->integral_constant_expression_p = saved_ice_p;
16137 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16138 if (CHECKING_P)
16139 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16140 return vec;
16143 /* Parse a template-argument.
16145 template-argument:
16146 assignment-expression
16147 type-id
16148 id-expression
16150 The representation is that of an assignment-expression, type-id, or
16151 id-expression -- except that the qualified id-expression is
16152 evaluated, so that the value returned is either a DECL or an
16153 OVERLOAD.
16155 Although the standard says "assignment-expression", it forbids
16156 throw-expressions or assignments in the template argument.
16157 Therefore, we use "conditional-expression" instead. */
16159 static tree
16160 cp_parser_template_argument (cp_parser* parser)
16162 tree argument;
16163 bool template_p;
16164 bool address_p;
16165 bool maybe_type_id = false;
16166 cp_token *token = NULL, *argument_start_token = NULL;
16167 location_t loc = 0;
16168 cp_id_kind idk;
16170 /* There's really no way to know what we're looking at, so we just
16171 try each alternative in order.
16173 [temp.arg]
16175 In a template-argument, an ambiguity between a type-id and an
16176 expression is resolved to a type-id, regardless of the form of
16177 the corresponding template-parameter.
16179 Therefore, we try a type-id first. */
16180 cp_parser_parse_tentatively (parser);
16181 argument = cp_parser_template_type_arg (parser);
16182 /* If there was no error parsing the type-id but the next token is a
16183 '>>', our behavior depends on which dialect of C++ we're
16184 parsing. In C++98, we probably found a typo for '> >'. But there
16185 are type-id which are also valid expressions. For instance:
16187 struct X { int operator >> (int); };
16188 template <int V> struct Foo {};
16189 Foo<X () >> 5> r;
16191 Here 'X()' is a valid type-id of a function type, but the user just
16192 wanted to write the expression "X() >> 5". Thus, we remember that we
16193 found a valid type-id, but we still try to parse the argument as an
16194 expression to see what happens.
16196 In C++0x, the '>>' will be considered two separate '>'
16197 tokens. */
16198 if (!cp_parser_error_occurred (parser)
16199 && cxx_dialect == cxx98
16200 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16202 maybe_type_id = true;
16203 cp_parser_abort_tentative_parse (parser);
16205 else
16207 /* If the next token isn't a `,' or a `>', then this argument wasn't
16208 really finished. This means that the argument is not a valid
16209 type-id. */
16210 if (!cp_parser_next_token_ends_template_argument_p (parser))
16211 cp_parser_error (parser, "expected template-argument");
16212 /* If that worked, we're done. */
16213 if (cp_parser_parse_definitely (parser))
16214 return argument;
16216 /* We're still not sure what the argument will be. */
16217 cp_parser_parse_tentatively (parser);
16218 /* Try a template. */
16219 argument_start_token = cp_lexer_peek_token (parser->lexer);
16220 argument = cp_parser_id_expression (parser,
16221 /*template_keyword_p=*/false,
16222 /*check_dependency_p=*/true,
16223 &template_p,
16224 /*declarator_p=*/false,
16225 /*optional_p=*/false);
16226 /* If the next token isn't a `,' or a `>', then this argument wasn't
16227 really finished. */
16228 if (!cp_parser_next_token_ends_template_argument_p (parser))
16229 cp_parser_error (parser, "expected template-argument");
16230 if (!cp_parser_error_occurred (parser))
16232 /* Figure out what is being referred to. If the id-expression
16233 was for a class template specialization, then we will have a
16234 TYPE_DECL at this point. There is no need to do name lookup
16235 at this point in that case. */
16236 if (TREE_CODE (argument) != TYPE_DECL)
16237 argument = cp_parser_lookup_name (parser, argument,
16238 none_type,
16239 /*is_template=*/template_p,
16240 /*is_namespace=*/false,
16241 /*check_dependency=*/true,
16242 /*ambiguous_decls=*/NULL,
16243 argument_start_token->location);
16244 /* Handle a constrained-type-specifier for a non-type template
16245 parameter. */
16246 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16247 argument = decl;
16248 else if (TREE_CODE (argument) != TEMPLATE_DECL
16249 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16250 cp_parser_error (parser, "expected template-name");
16252 if (cp_parser_parse_definitely (parser))
16254 if (TREE_DEPRECATED (argument))
16255 warn_deprecated_use (argument, NULL_TREE);
16256 return argument;
16258 /* It must be a non-type argument. In C++17 any constant-expression is
16259 allowed. */
16260 if (cxx_dialect > cxx14)
16261 goto general_expr;
16263 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16265 -- an integral constant-expression of integral or enumeration
16266 type; or
16268 -- the name of a non-type template-parameter; or
16270 -- the name of an object or function with external linkage...
16272 -- the address of an object or function with external linkage...
16274 -- a pointer to member... */
16275 /* Look for a non-type template parameter. */
16276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16278 cp_parser_parse_tentatively (parser);
16279 argument = cp_parser_primary_expression (parser,
16280 /*address_p=*/false,
16281 /*cast_p=*/false,
16282 /*template_arg_p=*/true,
16283 &idk);
16284 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16285 || !cp_parser_next_token_ends_template_argument_p (parser))
16286 cp_parser_simulate_error (parser);
16287 if (cp_parser_parse_definitely (parser))
16288 return argument;
16291 /* If the next token is "&", the argument must be the address of an
16292 object or function with external linkage. */
16293 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16294 if (address_p)
16296 loc = cp_lexer_peek_token (parser->lexer)->location;
16297 cp_lexer_consume_token (parser->lexer);
16299 /* See if we might have an id-expression. */
16300 token = cp_lexer_peek_token (parser->lexer);
16301 if (token->type == CPP_NAME
16302 || token->keyword == RID_OPERATOR
16303 || token->type == CPP_SCOPE
16304 || token->type == CPP_TEMPLATE_ID
16305 || token->type == CPP_NESTED_NAME_SPECIFIER)
16307 cp_parser_parse_tentatively (parser);
16308 argument = cp_parser_primary_expression (parser,
16309 address_p,
16310 /*cast_p=*/false,
16311 /*template_arg_p=*/true,
16312 &idk);
16313 if (cp_parser_error_occurred (parser)
16314 || !cp_parser_next_token_ends_template_argument_p (parser))
16315 cp_parser_abort_tentative_parse (parser);
16316 else
16318 tree probe;
16320 if (INDIRECT_REF_P (argument))
16322 /* Strip the dereference temporarily. */
16323 gcc_assert (REFERENCE_REF_P (argument));
16324 argument = TREE_OPERAND (argument, 0);
16327 /* If we're in a template, we represent a qualified-id referring
16328 to a static data member as a SCOPE_REF even if the scope isn't
16329 dependent so that we can check access control later. */
16330 probe = argument;
16331 if (TREE_CODE (probe) == SCOPE_REF)
16332 probe = TREE_OPERAND (probe, 1);
16333 if (VAR_P (probe))
16335 /* A variable without external linkage might still be a
16336 valid constant-expression, so no error is issued here
16337 if the external-linkage check fails. */
16338 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16339 cp_parser_simulate_error (parser);
16341 else if (is_overloaded_fn (argument))
16342 /* All overloaded functions are allowed; if the external
16343 linkage test does not pass, an error will be issued
16344 later. */
16346 else if (address_p
16347 && (TREE_CODE (argument) == OFFSET_REF
16348 || TREE_CODE (argument) == SCOPE_REF))
16349 /* A pointer-to-member. */
16351 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16353 else
16354 cp_parser_simulate_error (parser);
16356 if (cp_parser_parse_definitely (parser))
16358 if (address_p)
16359 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16360 tf_warning_or_error);
16361 else
16362 argument = convert_from_reference (argument);
16363 return argument;
16367 /* If the argument started with "&", there are no other valid
16368 alternatives at this point. */
16369 if (address_p)
16371 cp_parser_error (parser, "invalid non-type template argument");
16372 return error_mark_node;
16375 general_expr:
16376 /* If the argument wasn't successfully parsed as a type-id followed
16377 by '>>', the argument can only be a constant expression now.
16378 Otherwise, we try parsing the constant-expression tentatively,
16379 because the argument could really be a type-id. */
16380 if (maybe_type_id)
16381 cp_parser_parse_tentatively (parser);
16383 if (cxx_dialect <= cxx14)
16384 argument = cp_parser_constant_expression (parser);
16385 else
16387 /* With C++17 generalized non-type template arguments we need to handle
16388 lvalue constant expressions, too. */
16389 argument = cp_parser_assignment_expression (parser);
16390 require_potential_constant_expression (argument);
16393 if (!maybe_type_id)
16394 return argument;
16395 if (!cp_parser_next_token_ends_template_argument_p (parser))
16396 cp_parser_error (parser, "expected template-argument");
16397 if (cp_parser_parse_definitely (parser))
16398 return argument;
16399 /* We did our best to parse the argument as a non type-id, but that
16400 was the only alternative that matched (albeit with a '>' after
16401 it). We can assume it's just a typo from the user, and a
16402 diagnostic will then be issued. */
16403 return cp_parser_template_type_arg (parser);
16406 /* Parse an explicit-instantiation.
16408 explicit-instantiation:
16409 template declaration
16411 Although the standard says `declaration', what it really means is:
16413 explicit-instantiation:
16414 template decl-specifier-seq [opt] declarator [opt] ;
16416 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16417 supposed to be allowed. A defect report has been filed about this
16418 issue.
16420 GNU Extension:
16422 explicit-instantiation:
16423 storage-class-specifier template
16424 decl-specifier-seq [opt] declarator [opt] ;
16425 function-specifier template
16426 decl-specifier-seq [opt] declarator [opt] ; */
16428 static void
16429 cp_parser_explicit_instantiation (cp_parser* parser)
16431 int declares_class_or_enum;
16432 cp_decl_specifier_seq decl_specifiers;
16433 tree extension_specifier = NULL_TREE;
16435 timevar_push (TV_TEMPLATE_INST);
16437 /* Look for an (optional) storage-class-specifier or
16438 function-specifier. */
16439 if (cp_parser_allow_gnu_extensions_p (parser))
16441 extension_specifier
16442 = cp_parser_storage_class_specifier_opt (parser);
16443 if (!extension_specifier)
16444 extension_specifier
16445 = cp_parser_function_specifier_opt (parser,
16446 /*decl_specs=*/NULL);
16449 /* Look for the `template' keyword. */
16450 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16451 /* Let the front end know that we are processing an explicit
16452 instantiation. */
16453 begin_explicit_instantiation ();
16454 /* [temp.explicit] says that we are supposed to ignore access
16455 control while processing explicit instantiation directives. */
16456 push_deferring_access_checks (dk_no_check);
16457 /* Parse a decl-specifier-seq. */
16458 cp_parser_decl_specifier_seq (parser,
16459 CP_PARSER_FLAGS_OPTIONAL,
16460 &decl_specifiers,
16461 &declares_class_or_enum);
16462 /* If there was exactly one decl-specifier, and it declared a class,
16463 and there's no declarator, then we have an explicit type
16464 instantiation. */
16465 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16467 tree type;
16469 type = check_tag_decl (&decl_specifiers,
16470 /*explicit_type_instantiation_p=*/true);
16471 /* Turn access control back on for names used during
16472 template instantiation. */
16473 pop_deferring_access_checks ();
16474 if (type)
16475 do_type_instantiation (type, extension_specifier,
16476 /*complain=*/tf_error);
16478 else
16480 cp_declarator *declarator;
16481 tree decl;
16483 /* Parse the declarator. */
16484 declarator
16485 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16486 /*ctor_dtor_or_conv_p=*/NULL,
16487 /*parenthesized_p=*/NULL,
16488 /*member_p=*/false,
16489 /*friend_p=*/false);
16490 if (declares_class_or_enum & 2)
16491 cp_parser_check_for_definition_in_return_type (declarator,
16492 decl_specifiers.type,
16493 decl_specifiers.locations[ds_type_spec]);
16494 if (declarator != cp_error_declarator)
16496 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16497 permerror (decl_specifiers.locations[ds_inline],
16498 "explicit instantiation shall not use"
16499 " %<inline%> specifier");
16500 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16501 permerror (decl_specifiers.locations[ds_constexpr],
16502 "explicit instantiation shall not use"
16503 " %<constexpr%> specifier");
16505 decl = grokdeclarator (declarator, &decl_specifiers,
16506 NORMAL, 0, &decl_specifiers.attributes);
16507 /* Turn access control back on for names used during
16508 template instantiation. */
16509 pop_deferring_access_checks ();
16510 /* Do the explicit instantiation. */
16511 do_decl_instantiation (decl, extension_specifier);
16513 else
16515 pop_deferring_access_checks ();
16516 /* Skip the body of the explicit instantiation. */
16517 cp_parser_skip_to_end_of_statement (parser);
16520 /* We're done with the instantiation. */
16521 end_explicit_instantiation ();
16523 cp_parser_consume_semicolon_at_end_of_statement (parser);
16525 timevar_pop (TV_TEMPLATE_INST);
16528 /* Parse an explicit-specialization.
16530 explicit-specialization:
16531 template < > declaration
16533 Although the standard says `declaration', what it really means is:
16535 explicit-specialization:
16536 template <> decl-specifier [opt] init-declarator [opt] ;
16537 template <> function-definition
16538 template <> explicit-specialization
16539 template <> template-declaration */
16541 static void
16542 cp_parser_explicit_specialization (cp_parser* parser)
16544 bool need_lang_pop;
16545 cp_token *token = cp_lexer_peek_token (parser->lexer);
16547 /* Look for the `template' keyword. */
16548 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16549 /* Look for the `<'. */
16550 cp_parser_require (parser, CPP_LESS, RT_LESS);
16551 /* Look for the `>'. */
16552 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16553 /* We have processed another parameter list. */
16554 ++parser->num_template_parameter_lists;
16555 /* [temp]
16557 A template ... explicit specialization ... shall not have C
16558 linkage. */
16559 if (current_lang_name == lang_name_c)
16561 error_at (token->location, "template specialization with C linkage");
16562 maybe_show_extern_c_location ();
16563 /* Give it C++ linkage to avoid confusing other parts of the
16564 front end. */
16565 push_lang_context (lang_name_cplusplus);
16566 need_lang_pop = true;
16568 else
16569 need_lang_pop = false;
16570 /* Let the front end know that we are beginning a specialization. */
16571 if (!begin_specialization ())
16573 end_specialization ();
16574 return;
16577 /* If the next keyword is `template', we need to figure out whether
16578 or not we're looking a template-declaration. */
16579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16581 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16582 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16583 cp_parser_template_declaration_after_export (parser,
16584 /*member_p=*/false);
16585 else
16586 cp_parser_explicit_specialization (parser);
16588 else
16589 /* Parse the dependent declaration. */
16590 cp_parser_single_declaration (parser,
16591 /*checks=*/NULL,
16592 /*member_p=*/false,
16593 /*explicit_specialization_p=*/true,
16594 /*friend_p=*/NULL);
16595 /* We're done with the specialization. */
16596 end_specialization ();
16597 /* For the erroneous case of a template with C linkage, we pushed an
16598 implicit C++ linkage scope; exit that scope now. */
16599 if (need_lang_pop)
16600 pop_lang_context ();
16601 /* We're done with this parameter list. */
16602 --parser->num_template_parameter_lists;
16605 /* Parse a type-specifier.
16607 type-specifier:
16608 simple-type-specifier
16609 class-specifier
16610 enum-specifier
16611 elaborated-type-specifier
16612 cv-qualifier
16614 GNU Extension:
16616 type-specifier:
16617 __complex__
16619 Returns a representation of the type-specifier. For a
16620 class-specifier, enum-specifier, or elaborated-type-specifier, a
16621 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16623 The parser flags FLAGS is used to control type-specifier parsing.
16625 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16626 in a decl-specifier-seq.
16628 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16629 class-specifier, enum-specifier, or elaborated-type-specifier, then
16630 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16631 if a type is declared; 2 if it is defined. Otherwise, it is set to
16632 zero.
16634 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16635 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16636 is set to FALSE. */
16638 static tree
16639 cp_parser_type_specifier (cp_parser* parser,
16640 cp_parser_flags flags,
16641 cp_decl_specifier_seq *decl_specs,
16642 bool is_declaration,
16643 int* declares_class_or_enum,
16644 bool* is_cv_qualifier)
16646 tree type_spec = NULL_TREE;
16647 cp_token *token;
16648 enum rid keyword;
16649 cp_decl_spec ds = ds_last;
16651 /* Assume this type-specifier does not declare a new type. */
16652 if (declares_class_or_enum)
16653 *declares_class_or_enum = 0;
16654 /* And that it does not specify a cv-qualifier. */
16655 if (is_cv_qualifier)
16656 *is_cv_qualifier = false;
16657 /* Peek at the next token. */
16658 token = cp_lexer_peek_token (parser->lexer);
16660 /* If we're looking at a keyword, we can use that to guide the
16661 production we choose. */
16662 keyword = token->keyword;
16663 switch (keyword)
16665 case RID_ENUM:
16666 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16667 goto elaborated_type_specifier;
16669 /* Look for the enum-specifier. */
16670 type_spec = cp_parser_enum_specifier (parser);
16671 /* If that worked, we're done. */
16672 if (type_spec)
16674 if (declares_class_or_enum)
16675 *declares_class_or_enum = 2;
16676 if (decl_specs)
16677 cp_parser_set_decl_spec_type (decl_specs,
16678 type_spec,
16679 token,
16680 /*type_definition_p=*/true);
16681 return type_spec;
16683 else
16684 goto elaborated_type_specifier;
16686 /* Any of these indicate either a class-specifier, or an
16687 elaborated-type-specifier. */
16688 case RID_CLASS:
16689 case RID_STRUCT:
16690 case RID_UNION:
16691 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16692 goto elaborated_type_specifier;
16694 /* Parse tentatively so that we can back up if we don't find a
16695 class-specifier. */
16696 cp_parser_parse_tentatively (parser);
16697 /* Look for the class-specifier. */
16698 type_spec = cp_parser_class_specifier (parser);
16699 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16700 /* If that worked, we're done. */
16701 if (cp_parser_parse_definitely (parser))
16703 if (declares_class_or_enum)
16704 *declares_class_or_enum = 2;
16705 if (decl_specs)
16706 cp_parser_set_decl_spec_type (decl_specs,
16707 type_spec,
16708 token,
16709 /*type_definition_p=*/true);
16710 return type_spec;
16713 /* Fall through. */
16714 elaborated_type_specifier:
16715 /* We're declaring (not defining) a class or enum. */
16716 if (declares_class_or_enum)
16717 *declares_class_or_enum = 1;
16719 /* Fall through. */
16720 case RID_TYPENAME:
16721 /* Look for an elaborated-type-specifier. */
16722 type_spec
16723 = (cp_parser_elaborated_type_specifier
16724 (parser,
16725 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16726 is_declaration));
16727 if (decl_specs)
16728 cp_parser_set_decl_spec_type (decl_specs,
16729 type_spec,
16730 token,
16731 /*type_definition_p=*/false);
16732 return type_spec;
16734 case RID_CONST:
16735 ds = ds_const;
16736 if (is_cv_qualifier)
16737 *is_cv_qualifier = true;
16738 break;
16740 case RID_VOLATILE:
16741 ds = ds_volatile;
16742 if (is_cv_qualifier)
16743 *is_cv_qualifier = true;
16744 break;
16746 case RID_RESTRICT:
16747 ds = ds_restrict;
16748 if (is_cv_qualifier)
16749 *is_cv_qualifier = true;
16750 break;
16752 case RID_COMPLEX:
16753 /* The `__complex__' keyword is a GNU extension. */
16754 ds = ds_complex;
16755 break;
16757 default:
16758 break;
16761 /* Handle simple keywords. */
16762 if (ds != ds_last)
16764 if (decl_specs)
16766 set_and_check_decl_spec_loc (decl_specs, ds, token);
16767 decl_specs->any_specifiers_p = true;
16769 return cp_lexer_consume_token (parser->lexer)->u.value;
16772 /* If we do not already have a type-specifier, assume we are looking
16773 at a simple-type-specifier. */
16774 type_spec = cp_parser_simple_type_specifier (parser,
16775 decl_specs,
16776 flags);
16778 /* If we didn't find a type-specifier, and a type-specifier was not
16779 optional in this context, issue an error message. */
16780 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16782 cp_parser_error (parser, "expected type specifier");
16783 return error_mark_node;
16786 return type_spec;
16789 /* Parse a simple-type-specifier.
16791 simple-type-specifier:
16792 :: [opt] nested-name-specifier [opt] type-name
16793 :: [opt] nested-name-specifier template template-id
16794 char
16795 wchar_t
16796 bool
16797 short
16799 long
16800 signed
16801 unsigned
16802 float
16803 double
16804 void
16806 C++11 Extension:
16808 simple-type-specifier:
16809 auto
16810 decltype ( expression )
16811 char16_t
16812 char32_t
16813 __underlying_type ( type-id )
16815 C++17 extension:
16817 nested-name-specifier(opt) template-name
16819 GNU Extension:
16821 simple-type-specifier:
16822 __int128
16823 __typeof__ unary-expression
16824 __typeof__ ( type-id )
16825 __typeof__ ( type-id ) { initializer-list , [opt] }
16827 Concepts Extension:
16829 simple-type-specifier:
16830 constrained-type-specifier
16832 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16833 appropriately updated. */
16835 static tree
16836 cp_parser_simple_type_specifier (cp_parser* parser,
16837 cp_decl_specifier_seq *decl_specs,
16838 cp_parser_flags flags)
16840 tree type = NULL_TREE;
16841 cp_token *token;
16842 int idx;
16844 /* Peek at the next token. */
16845 token = cp_lexer_peek_token (parser->lexer);
16847 /* If we're looking at a keyword, things are easy. */
16848 switch (token->keyword)
16850 case RID_CHAR:
16851 if (decl_specs)
16852 decl_specs->explicit_char_p = true;
16853 type = char_type_node;
16854 break;
16855 case RID_CHAR16:
16856 type = char16_type_node;
16857 break;
16858 case RID_CHAR32:
16859 type = char32_type_node;
16860 break;
16861 case RID_WCHAR:
16862 type = wchar_type_node;
16863 break;
16864 case RID_BOOL:
16865 type = boolean_type_node;
16866 break;
16867 case RID_SHORT:
16868 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16869 type = short_integer_type_node;
16870 break;
16871 case RID_INT:
16872 if (decl_specs)
16873 decl_specs->explicit_int_p = true;
16874 type = integer_type_node;
16875 break;
16876 case RID_INT_N_0:
16877 case RID_INT_N_1:
16878 case RID_INT_N_2:
16879 case RID_INT_N_3:
16880 idx = token->keyword - RID_INT_N_0;
16881 if (! int_n_enabled_p [idx])
16882 break;
16883 if (decl_specs)
16885 decl_specs->explicit_intN_p = true;
16886 decl_specs->int_n_idx = idx;
16888 type = int_n_trees [idx].signed_type;
16889 break;
16890 case RID_LONG:
16891 if (decl_specs)
16892 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16893 type = long_integer_type_node;
16894 break;
16895 case RID_SIGNED:
16896 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16897 type = integer_type_node;
16898 break;
16899 case RID_UNSIGNED:
16900 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16901 type = unsigned_type_node;
16902 break;
16903 case RID_FLOAT:
16904 type = float_type_node;
16905 break;
16906 case RID_DOUBLE:
16907 type = double_type_node;
16908 break;
16909 case RID_VOID:
16910 type = void_type_node;
16911 break;
16913 case RID_AUTO:
16914 maybe_warn_cpp0x (CPP0X_AUTO);
16915 if (parser->auto_is_implicit_function_template_parm_p)
16917 /* The 'auto' might be the placeholder return type for a function decl
16918 with trailing return type. */
16919 bool have_trailing_return_fn_decl = false;
16921 cp_parser_parse_tentatively (parser);
16922 cp_lexer_consume_token (parser->lexer);
16923 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16924 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16925 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16926 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16930 cp_lexer_consume_token (parser->lexer);
16931 cp_parser_skip_to_closing_parenthesis (parser,
16932 /*recovering*/false,
16933 /*or_comma*/false,
16934 /*consume_paren*/true);
16935 continue;
16938 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16940 have_trailing_return_fn_decl = true;
16941 break;
16944 cp_lexer_consume_token (parser->lexer);
16946 cp_parser_abort_tentative_parse (parser);
16948 if (have_trailing_return_fn_decl)
16950 type = make_auto ();
16951 break;
16954 if (cxx_dialect >= cxx14)
16956 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16957 type = TREE_TYPE (type);
16959 else
16960 type = error_mark_node;
16962 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16964 if (cxx_dialect < cxx14)
16965 error_at (token->location,
16966 "use of %<auto%> in lambda parameter declaration "
16967 "only available with "
16968 "-std=c++14 or -std=gnu++14");
16970 else if (cxx_dialect < cxx14)
16971 error_at (token->location,
16972 "use of %<auto%> in parameter declaration "
16973 "only available with "
16974 "-std=c++14 or -std=gnu++14");
16975 else if (!flag_concepts)
16976 pedwarn (token->location, OPT_Wpedantic,
16977 "ISO C++ forbids use of %<auto%> in parameter "
16978 "declaration");
16980 else
16981 type = make_auto ();
16982 break;
16984 case RID_DECLTYPE:
16985 /* Since DR 743, decltype can either be a simple-type-specifier by
16986 itself or begin a nested-name-specifier. Parsing it will replace
16987 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16988 handling below decide what to do. */
16989 cp_parser_decltype (parser);
16990 cp_lexer_set_token_position (parser->lexer, token);
16991 break;
16993 case RID_TYPEOF:
16994 /* Consume the `typeof' token. */
16995 cp_lexer_consume_token (parser->lexer);
16996 /* Parse the operand to `typeof'. */
16997 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16998 /* If it is not already a TYPE, take its type. */
16999 if (!TYPE_P (type))
17000 type = finish_typeof (type);
17002 if (decl_specs)
17003 cp_parser_set_decl_spec_type (decl_specs, type,
17004 token,
17005 /*type_definition_p=*/false);
17007 return type;
17009 case RID_UNDERLYING_TYPE:
17010 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17011 if (decl_specs)
17012 cp_parser_set_decl_spec_type (decl_specs, type,
17013 token,
17014 /*type_definition_p=*/false);
17016 return type;
17018 case RID_BASES:
17019 case RID_DIRECT_BASES:
17020 type = cp_parser_trait_expr (parser, token->keyword);
17021 if (decl_specs)
17022 cp_parser_set_decl_spec_type (decl_specs, type,
17023 token,
17024 /*type_definition_p=*/false);
17025 return type;
17026 default:
17027 break;
17030 /* If token is an already-parsed decltype not followed by ::,
17031 it's a simple-type-specifier. */
17032 if (token->type == CPP_DECLTYPE
17033 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17035 type = saved_checks_value (token->u.tree_check_value);
17036 if (decl_specs)
17038 cp_parser_set_decl_spec_type (decl_specs, type,
17039 token,
17040 /*type_definition_p=*/false);
17041 /* Remember that we are handling a decltype in order to
17042 implement the resolution of DR 1510 when the argument
17043 isn't instantiation dependent. */
17044 decl_specs->decltype_p = true;
17046 cp_lexer_consume_token (parser->lexer);
17047 return type;
17050 /* If the type-specifier was for a built-in type, we're done. */
17051 if (type)
17053 /* Record the type. */
17054 if (decl_specs
17055 && (token->keyword != RID_SIGNED
17056 && token->keyword != RID_UNSIGNED
17057 && token->keyword != RID_SHORT
17058 && token->keyword != RID_LONG))
17059 cp_parser_set_decl_spec_type (decl_specs,
17060 type,
17061 token,
17062 /*type_definition_p=*/false);
17063 if (decl_specs)
17064 decl_specs->any_specifiers_p = true;
17066 /* Consume the token. */
17067 cp_lexer_consume_token (parser->lexer);
17069 if (type == error_mark_node)
17070 return error_mark_node;
17072 /* There is no valid C++ program where a non-template type is
17073 followed by a "<". That usually indicates that the user thought
17074 that the type was a template. */
17075 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17076 token->location);
17078 return TYPE_NAME (type);
17081 /* The type-specifier must be a user-defined type. */
17082 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17084 bool qualified_p;
17085 bool global_p;
17087 /* Don't gobble tokens or issue error messages if this is an
17088 optional type-specifier. */
17089 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17090 cp_parser_parse_tentatively (parser);
17092 token = cp_lexer_peek_token (parser->lexer);
17094 /* Look for the optional `::' operator. */
17095 global_p
17096 = (cp_parser_global_scope_opt (parser,
17097 /*current_scope_valid_p=*/false)
17098 != NULL_TREE);
17099 /* Look for the nested-name specifier. */
17100 qualified_p
17101 = (cp_parser_nested_name_specifier_opt (parser,
17102 /*typename_keyword_p=*/false,
17103 /*check_dependency_p=*/true,
17104 /*type_p=*/false,
17105 /*is_declaration=*/false)
17106 != NULL_TREE);
17107 /* If we have seen a nested-name-specifier, and the next token
17108 is `template', then we are using the template-id production. */
17109 if (parser->scope
17110 && cp_parser_optional_template_keyword (parser))
17112 /* Look for the template-id. */
17113 type = cp_parser_template_id (parser,
17114 /*template_keyword_p=*/true,
17115 /*check_dependency_p=*/true,
17116 none_type,
17117 /*is_declaration=*/false);
17118 /* If the template-id did not name a type, we are out of
17119 luck. */
17120 if (TREE_CODE (type) != TYPE_DECL)
17122 cp_parser_error (parser, "expected template-id for type");
17123 type = NULL_TREE;
17126 /* Otherwise, look for a type-name. */
17127 else
17128 type = cp_parser_type_name (parser);
17129 /* Keep track of all name-lookups performed in class scopes. */
17130 if (type
17131 && !global_p
17132 && !qualified_p
17133 && TREE_CODE (type) == TYPE_DECL
17134 && identifier_p (DECL_NAME (type)))
17135 maybe_note_name_used_in_class (DECL_NAME (type), type);
17136 /* If it didn't work out, we don't have a TYPE. */
17137 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17138 && !cp_parser_parse_definitely (parser))
17139 type = NULL_TREE;
17140 if (!type && cxx_dialect >= cxx17)
17142 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17143 cp_parser_parse_tentatively (parser);
17145 cp_parser_global_scope_opt (parser,
17146 /*current_scope_valid_p=*/false);
17147 cp_parser_nested_name_specifier_opt (parser,
17148 /*typename_keyword_p=*/false,
17149 /*check_dependency_p=*/true,
17150 /*type_p=*/false,
17151 /*is_declaration=*/false);
17152 tree name = cp_parser_identifier (parser);
17153 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17154 && parser->scope != error_mark_node)
17156 tree tmpl = cp_parser_lookup_name (parser, name,
17157 none_type,
17158 /*is_template=*/false,
17159 /*is_namespace=*/false,
17160 /*check_dependency=*/true,
17161 /*ambiguous_decls=*/NULL,
17162 token->location);
17163 if (tmpl && tmpl != error_mark_node
17164 && (DECL_CLASS_TEMPLATE_P (tmpl)
17165 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17166 type = make_template_placeholder (tmpl);
17167 else
17169 type = error_mark_node;
17170 if (!cp_parser_simulate_error (parser))
17171 cp_parser_name_lookup_error (parser, name, tmpl,
17172 NLE_TYPE, token->location);
17175 else
17176 type = error_mark_node;
17178 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17179 && !cp_parser_parse_definitely (parser))
17180 type = NULL_TREE;
17182 if (type && decl_specs)
17183 cp_parser_set_decl_spec_type (decl_specs, type,
17184 token,
17185 /*type_definition_p=*/false);
17188 /* If we didn't get a type-name, issue an error message. */
17189 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17191 cp_parser_error (parser, "expected type-name");
17192 return error_mark_node;
17195 if (type && type != error_mark_node)
17197 /* See if TYPE is an Objective-C type, and if so, parse and
17198 accept any protocol references following it. Do this before
17199 the cp_parser_check_for_invalid_template_id() call, because
17200 Objective-C types can be followed by '<...>' which would
17201 enclose protocol names rather than template arguments, and so
17202 everything is fine. */
17203 if (c_dialect_objc () && !parser->scope
17204 && (objc_is_id (type) || objc_is_class_name (type)))
17206 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17207 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17209 /* Clobber the "unqualified" type previously entered into
17210 DECL_SPECS with the new, improved protocol-qualified version. */
17211 if (decl_specs)
17212 decl_specs->type = qual_type;
17214 return qual_type;
17217 /* There is no valid C++ program where a non-template type is
17218 followed by a "<". That usually indicates that the user
17219 thought that the type was a template. */
17220 cp_parser_check_for_invalid_template_id (parser, type,
17221 none_type,
17222 token->location);
17225 return type;
17228 /* Parse a type-name.
17230 type-name:
17231 class-name
17232 enum-name
17233 typedef-name
17234 simple-template-id [in c++0x]
17236 enum-name:
17237 identifier
17239 typedef-name:
17240 identifier
17242 Concepts:
17244 type-name:
17245 concept-name
17246 partial-concept-id
17248 concept-name:
17249 identifier
17251 Returns a TYPE_DECL for the type. */
17253 static tree
17254 cp_parser_type_name (cp_parser* parser)
17256 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17259 /* See above. */
17260 static tree
17261 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17263 tree type_decl;
17265 /* We can't know yet whether it is a class-name or not. */
17266 cp_parser_parse_tentatively (parser);
17267 /* Try a class-name. */
17268 type_decl = cp_parser_class_name (parser,
17269 typename_keyword_p,
17270 /*template_keyword_p=*/false,
17271 none_type,
17272 /*check_dependency_p=*/true,
17273 /*class_head_p=*/false,
17274 /*is_declaration=*/false);
17275 /* If it's not a class-name, keep looking. */
17276 if (!cp_parser_parse_definitely (parser))
17278 if (cxx_dialect < cxx11)
17279 /* It must be a typedef-name or an enum-name. */
17280 return cp_parser_nonclass_name (parser);
17282 cp_parser_parse_tentatively (parser);
17283 /* It is either a simple-template-id representing an
17284 instantiation of an alias template... */
17285 type_decl = cp_parser_template_id (parser,
17286 /*template_keyword_p=*/false,
17287 /*check_dependency_p=*/true,
17288 none_type,
17289 /*is_declaration=*/false);
17290 /* Note that this must be an instantiation of an alias template
17291 because [temp.names]/6 says:
17293 A template-id that names an alias template specialization
17294 is a type-name.
17296 Whereas [temp.names]/7 says:
17298 A simple-template-id that names a class template
17299 specialization is a class-name.
17301 With concepts, this could also be a partial-concept-id that
17302 declares a non-type template parameter. */
17303 if (type_decl != NULL_TREE
17304 && TREE_CODE (type_decl) == TYPE_DECL
17305 && TYPE_DECL_ALIAS_P (type_decl))
17306 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17307 else if (is_constrained_parameter (type_decl))
17308 /* Don't do anything. */ ;
17309 else
17310 cp_parser_simulate_error (parser);
17312 if (!cp_parser_parse_definitely (parser))
17313 /* ... Or a typedef-name or an enum-name. */
17314 return cp_parser_nonclass_name (parser);
17317 return type_decl;
17320 /* Check if DECL and ARGS can form a constrained-type-specifier.
17321 If ARGS is non-null, we try to form a concept check of the
17322 form DECL<?, ARGS> where ? is a wildcard that matches any
17323 kind of template argument. If ARGS is NULL, then we try to
17324 form a concept check of the form DECL<?>. */
17326 static tree
17327 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17328 tree decl, tree args)
17330 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17332 /* If we a constrained-type-specifier cannot be deduced. */
17333 if (parser->prevent_constrained_type_specifiers)
17334 return NULL_TREE;
17336 /* A constrained type specifier can only be found in an
17337 overload set or as a reference to a template declaration.
17339 FIXME: This might be masking a bug. It's possible that
17340 that the deduction below is causing template specializations
17341 to be formed with the wildcard as an argument. */
17342 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17343 return NULL_TREE;
17345 /* Try to build a call expression that evaluates the
17346 concept. This can fail if the overload set refers
17347 only to non-templates. */
17348 tree placeholder = build_nt (WILDCARD_DECL);
17349 tree check = build_concept_check (decl, placeholder, args);
17350 if (check == error_mark_node)
17351 return NULL_TREE;
17353 /* Deduce the checked constraint and the prototype parameter.
17355 FIXME: In certain cases, failure to deduce should be a
17356 diagnosable error. */
17357 tree conc;
17358 tree proto;
17359 if (!deduce_constrained_parameter (check, conc, proto))
17360 return NULL_TREE;
17362 /* In template parameter scope, this results in a constrained
17363 parameter. Return a descriptor of that parm. */
17364 if (processing_template_parmlist)
17365 return build_constrained_parameter (conc, proto, args);
17367 /* In a parameter-declaration-clause, constrained-type
17368 specifiers result in invented template parameters. */
17369 if (parser->auto_is_implicit_function_template_parm_p)
17371 tree x = build_constrained_parameter (conc, proto, args);
17372 return synthesize_implicit_template_parm (parser, x);
17374 else
17376 /* Otherwise, we're in a context where the constrained
17377 type name is deduced and the constraint applies
17378 after deduction. */
17379 return make_constrained_auto (conc, args);
17382 return NULL_TREE;
17385 /* If DECL refers to a concept, return a TYPE_DECL representing
17386 the result of using the constrained type specifier in the
17387 current context. DECL refers to a concept if
17389 - it is an overload set containing a function concept taking a single
17390 type argument, or
17392 - it is a variable concept taking a single type argument. */
17394 static tree
17395 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17397 if (flag_concepts
17398 && (TREE_CODE (decl) == OVERLOAD
17399 || BASELINK_P (decl)
17400 || variable_concept_p (decl)))
17401 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17402 else
17403 return NULL_TREE;
17406 /* Check if DECL and ARGS form a partial-concept-id. If so,
17407 assign ID to the resulting constrained placeholder.
17409 Returns true if the partial-concept-id designates a placeholder
17410 and false otherwise. Note that *id is set to NULL_TREE in
17411 this case. */
17413 static tree
17414 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17416 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17419 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17420 or a concept-name.
17422 enum-name:
17423 identifier
17425 typedef-name:
17426 identifier
17428 concept-name:
17429 identifier
17431 Returns a TYPE_DECL for the type. */
17433 static tree
17434 cp_parser_nonclass_name (cp_parser* parser)
17436 tree type_decl;
17437 tree identifier;
17439 cp_token *token = cp_lexer_peek_token (parser->lexer);
17440 identifier = cp_parser_identifier (parser);
17441 if (identifier == error_mark_node)
17442 return error_mark_node;
17444 /* Look up the type-name. */
17445 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17447 type_decl = strip_using_decl (type_decl);
17449 /* If we found an overload set, then it may refer to a concept-name. */
17450 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17451 type_decl = decl;
17453 if (TREE_CODE (type_decl) != TYPE_DECL
17454 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17456 /* See if this is an Objective-C type. */
17457 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17458 tree type = objc_get_protocol_qualified_type (identifier, protos);
17459 if (type)
17460 type_decl = TYPE_NAME (type);
17463 /* Issue an error if we did not find a type-name. */
17464 if (TREE_CODE (type_decl) != TYPE_DECL
17465 /* In Objective-C, we have the complication that class names are
17466 normally type names and start declarations (eg, the
17467 "NSObject" in "NSObject *object;"), but can be used in an
17468 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17469 is an expression. So, a classname followed by a dot is not a
17470 valid type-name. */
17471 || (objc_is_class_name (TREE_TYPE (type_decl))
17472 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17474 if (!cp_parser_simulate_error (parser))
17475 cp_parser_name_lookup_error (parser, identifier, type_decl,
17476 NLE_TYPE, token->location);
17477 return error_mark_node;
17479 /* Remember that the name was used in the definition of the
17480 current class so that we can check later to see if the
17481 meaning would have been different after the class was
17482 entirely defined. */
17483 else if (type_decl != error_mark_node
17484 && !parser->scope)
17485 maybe_note_name_used_in_class (identifier, type_decl);
17487 return type_decl;
17490 /* Parse an elaborated-type-specifier. Note that the grammar given
17491 here incorporates the resolution to DR68.
17493 elaborated-type-specifier:
17494 class-key :: [opt] nested-name-specifier [opt] identifier
17495 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17496 enum-key :: [opt] nested-name-specifier [opt] identifier
17497 typename :: [opt] nested-name-specifier identifier
17498 typename :: [opt] nested-name-specifier template [opt]
17499 template-id
17501 GNU extension:
17503 elaborated-type-specifier:
17504 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17505 class-key attributes :: [opt] nested-name-specifier [opt]
17506 template [opt] template-id
17507 enum attributes :: [opt] nested-name-specifier [opt] identifier
17509 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17510 declared `friend'. If IS_DECLARATION is TRUE, then this
17511 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17512 something is being declared.
17514 Returns the TYPE specified. */
17516 static tree
17517 cp_parser_elaborated_type_specifier (cp_parser* parser,
17518 bool is_friend,
17519 bool is_declaration)
17521 enum tag_types tag_type;
17522 tree identifier;
17523 tree type = NULL_TREE;
17524 tree attributes = NULL_TREE;
17525 tree globalscope;
17526 cp_token *token = NULL;
17528 /* See if we're looking at the `enum' keyword. */
17529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17531 /* Consume the `enum' token. */
17532 cp_lexer_consume_token (parser->lexer);
17533 /* Remember that it's an enumeration type. */
17534 tag_type = enum_type;
17535 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17536 enums) is used here. */
17537 cp_token *token = cp_lexer_peek_token (parser->lexer);
17538 if (cp_parser_is_keyword (token, RID_CLASS)
17539 || cp_parser_is_keyword (token, RID_STRUCT))
17541 gcc_rich_location richloc (token->location);
17542 richloc.add_range (input_location, false);
17543 richloc.add_fixit_remove ();
17544 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17545 "a scoped enum must not use the %qD keyword",
17546 token->u.value);
17547 /* Consume the `struct' or `class' and parse it anyway. */
17548 cp_lexer_consume_token (parser->lexer);
17550 /* Parse the attributes. */
17551 attributes = cp_parser_attributes_opt (parser);
17553 /* Or, it might be `typename'. */
17554 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17555 RID_TYPENAME))
17557 /* Consume the `typename' token. */
17558 cp_lexer_consume_token (parser->lexer);
17559 /* Remember that it's a `typename' type. */
17560 tag_type = typename_type;
17562 /* Otherwise it must be a class-key. */
17563 else
17565 tag_type = cp_parser_class_key (parser);
17566 if (tag_type == none_type)
17567 return error_mark_node;
17568 /* Parse the attributes. */
17569 attributes = cp_parser_attributes_opt (parser);
17572 /* Look for the `::' operator. */
17573 globalscope = cp_parser_global_scope_opt (parser,
17574 /*current_scope_valid_p=*/false);
17575 /* Look for the nested-name-specifier. */
17576 tree nested_name_specifier;
17577 if (tag_type == typename_type && !globalscope)
17579 nested_name_specifier
17580 = cp_parser_nested_name_specifier (parser,
17581 /*typename_keyword_p=*/true,
17582 /*check_dependency_p=*/true,
17583 /*type_p=*/true,
17584 is_declaration);
17585 if (!nested_name_specifier)
17586 return error_mark_node;
17588 else
17589 /* Even though `typename' is not present, the proposed resolution
17590 to Core Issue 180 says that in `class A<T>::B', `B' should be
17591 considered a type-name, even if `A<T>' is dependent. */
17592 nested_name_specifier
17593 = cp_parser_nested_name_specifier_opt (parser,
17594 /*typename_keyword_p=*/true,
17595 /*check_dependency_p=*/true,
17596 /*type_p=*/true,
17597 is_declaration);
17598 /* For everything but enumeration types, consider a template-id.
17599 For an enumeration type, consider only a plain identifier. */
17600 if (tag_type != enum_type)
17602 bool template_p = false;
17603 tree decl;
17605 /* Allow the `template' keyword. */
17606 template_p = cp_parser_optional_template_keyword (parser);
17607 /* If we didn't see `template', we don't know if there's a
17608 template-id or not. */
17609 if (!template_p)
17610 cp_parser_parse_tentatively (parser);
17611 /* Parse the template-id. */
17612 token = cp_lexer_peek_token (parser->lexer);
17613 decl = cp_parser_template_id (parser, template_p,
17614 /*check_dependency_p=*/true,
17615 tag_type,
17616 is_declaration);
17617 /* If we didn't find a template-id, look for an ordinary
17618 identifier. */
17619 if (!template_p && !cp_parser_parse_definitely (parser))
17621 /* We can get here when cp_parser_template_id, called by
17622 cp_parser_class_name with tag_type == none_type, succeeds
17623 and caches a BASELINK. Then, when called again here,
17624 instead of failing and returning an error_mark_node
17625 returns it (see template/typename17.C in C++11).
17626 ??? Could we diagnose this earlier? */
17627 else if (tag_type == typename_type && BASELINK_P (decl))
17629 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17630 type = error_mark_node;
17632 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17633 in effect, then we must assume that, upon instantiation, the
17634 template will correspond to a class. */
17635 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17636 && tag_type == typename_type)
17637 type = make_typename_type (parser->scope, decl,
17638 typename_type,
17639 /*complain=*/tf_error);
17640 /* If the `typename' keyword is in effect and DECL is not a type
17641 decl, then type is non existent. */
17642 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17644 else if (TREE_CODE (decl) == TYPE_DECL)
17646 type = check_elaborated_type_specifier (tag_type, decl,
17647 /*allow_template_p=*/true);
17649 /* If the next token is a semicolon, this must be a specialization,
17650 instantiation, or friend declaration. Check the scope while we
17651 still know whether or not we had a nested-name-specifier. */
17652 if (type != error_mark_node
17653 && !nested_name_specifier && !is_friend
17654 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17655 check_unqualified_spec_or_inst (type, token->location);
17657 else if (decl == error_mark_node)
17658 type = error_mark_node;
17661 if (!type)
17663 token = cp_lexer_peek_token (parser->lexer);
17664 identifier = cp_parser_identifier (parser);
17666 if (identifier == error_mark_node)
17668 parser->scope = NULL_TREE;
17669 return error_mark_node;
17672 /* For a `typename', we needn't call xref_tag. */
17673 if (tag_type == typename_type
17674 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17675 return cp_parser_make_typename_type (parser, identifier,
17676 token->location);
17678 /* Template parameter lists apply only if we are not within a
17679 function parameter list. */
17680 bool template_parm_lists_apply
17681 = parser->num_template_parameter_lists;
17682 if (template_parm_lists_apply)
17683 for (cp_binding_level *s = current_binding_level;
17684 s && s->kind != sk_template_parms;
17685 s = s->level_chain)
17686 if (s->kind == sk_function_parms)
17687 template_parm_lists_apply = false;
17689 /* Look up a qualified name in the usual way. */
17690 if (parser->scope)
17692 tree decl;
17693 tree ambiguous_decls;
17695 decl = cp_parser_lookup_name (parser, identifier,
17696 tag_type,
17697 /*is_template=*/false,
17698 /*is_namespace=*/false,
17699 /*check_dependency=*/true,
17700 &ambiguous_decls,
17701 token->location);
17703 /* If the lookup was ambiguous, an error will already have been
17704 issued. */
17705 if (ambiguous_decls)
17706 return error_mark_node;
17708 /* If we are parsing friend declaration, DECL may be a
17709 TEMPLATE_DECL tree node here. However, we need to check
17710 whether this TEMPLATE_DECL results in valid code. Consider
17711 the following example:
17713 namespace N {
17714 template <class T> class C {};
17716 class X {
17717 template <class T> friend class N::C; // #1, valid code
17719 template <class T> class Y {
17720 friend class N::C; // #2, invalid code
17723 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17724 name lookup of `N::C'. We see that friend declaration must
17725 be template for the code to be valid. Note that
17726 processing_template_decl does not work here since it is
17727 always 1 for the above two cases. */
17729 decl = (cp_parser_maybe_treat_template_as_class
17730 (decl, /*tag_name_p=*/is_friend
17731 && template_parm_lists_apply));
17733 if (TREE_CODE (decl) != TYPE_DECL)
17735 cp_parser_diagnose_invalid_type_name (parser,
17736 identifier,
17737 token->location);
17738 return error_mark_node;
17741 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17743 bool allow_template = (template_parm_lists_apply
17744 || DECL_SELF_REFERENCE_P (decl));
17745 type = check_elaborated_type_specifier (tag_type, decl,
17746 allow_template);
17748 if (type == error_mark_node)
17749 return error_mark_node;
17752 /* Forward declarations of nested types, such as
17754 class C1::C2;
17755 class C1::C2::C3;
17757 are invalid unless all components preceding the final '::'
17758 are complete. If all enclosing types are complete, these
17759 declarations become merely pointless.
17761 Invalid forward declarations of nested types are errors
17762 caught elsewhere in parsing. Those that are pointless arrive
17763 here. */
17765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17766 && !is_friend && !processing_explicit_instantiation)
17767 warning (0, "declaration %qD does not declare anything", decl);
17769 type = TREE_TYPE (decl);
17771 else
17773 /* An elaborated-type-specifier sometimes introduces a new type and
17774 sometimes names an existing type. Normally, the rule is that it
17775 introduces a new type only if there is not an existing type of
17776 the same name already in scope. For example, given:
17778 struct S {};
17779 void f() { struct S s; }
17781 the `struct S' in the body of `f' is the same `struct S' as in
17782 the global scope; the existing definition is used. However, if
17783 there were no global declaration, this would introduce a new
17784 local class named `S'.
17786 An exception to this rule applies to the following code:
17788 namespace N { struct S; }
17790 Here, the elaborated-type-specifier names a new type
17791 unconditionally; even if there is already an `S' in the
17792 containing scope this declaration names a new type.
17793 This exception only applies if the elaborated-type-specifier
17794 forms the complete declaration:
17796 [class.name]
17798 A declaration consisting solely of `class-key identifier ;' is
17799 either a redeclaration of the name in the current scope or a
17800 forward declaration of the identifier as a class name. It
17801 introduces the name into the current scope.
17803 We are in this situation precisely when the next token is a `;'.
17805 An exception to the exception is that a `friend' declaration does
17806 *not* name a new type; i.e., given:
17808 struct S { friend struct T; };
17810 `T' is not a new type in the scope of `S'.
17812 Also, `new struct S' or `sizeof (struct S)' never results in the
17813 definition of a new type; a new type can only be declared in a
17814 declaration context. */
17816 tag_scope ts;
17817 bool template_p;
17819 if (is_friend)
17820 /* Friends have special name lookup rules. */
17821 ts = ts_within_enclosing_non_class;
17822 else if (is_declaration
17823 && cp_lexer_next_token_is (parser->lexer,
17824 CPP_SEMICOLON))
17825 /* This is a `class-key identifier ;' */
17826 ts = ts_current;
17827 else
17828 ts = ts_global;
17830 template_p =
17831 (template_parm_lists_apply
17832 && (cp_parser_next_token_starts_class_definition_p (parser)
17833 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17834 /* An unqualified name was used to reference this type, so
17835 there were no qualifying templates. */
17836 if (template_parm_lists_apply
17837 && !cp_parser_check_template_parameters (parser,
17838 /*num_templates=*/0,
17839 token->location,
17840 /*declarator=*/NULL))
17841 return error_mark_node;
17842 type = xref_tag (tag_type, identifier, ts, template_p);
17846 if (type == error_mark_node)
17847 return error_mark_node;
17849 /* Allow attributes on forward declarations of classes. */
17850 if (attributes)
17852 if (TREE_CODE (type) == TYPENAME_TYPE)
17853 warning (OPT_Wattributes,
17854 "attributes ignored on uninstantiated type");
17855 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17856 && ! processing_explicit_instantiation)
17857 warning (OPT_Wattributes,
17858 "attributes ignored on template instantiation");
17859 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17860 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17861 else
17862 warning (OPT_Wattributes,
17863 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17866 if (tag_type != enum_type)
17868 /* Indicate whether this class was declared as a `class' or as a
17869 `struct'. */
17870 if (CLASS_TYPE_P (type))
17871 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17872 cp_parser_check_class_key (tag_type, type);
17875 /* A "<" cannot follow an elaborated type specifier. If that
17876 happens, the user was probably trying to form a template-id. */
17877 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17878 token->location);
17880 return type;
17883 /* Parse an enum-specifier.
17885 enum-specifier:
17886 enum-head { enumerator-list [opt] }
17887 enum-head { enumerator-list , } [C++0x]
17889 enum-head:
17890 enum-key identifier [opt] enum-base [opt]
17891 enum-key nested-name-specifier identifier enum-base [opt]
17893 enum-key:
17894 enum
17895 enum class [C++0x]
17896 enum struct [C++0x]
17898 enum-base: [C++0x]
17899 : type-specifier-seq
17901 opaque-enum-specifier:
17902 enum-key identifier enum-base [opt] ;
17904 GNU Extensions:
17905 enum-key attributes[opt] identifier [opt] enum-base [opt]
17906 { enumerator-list [opt] }attributes[opt]
17907 enum-key attributes[opt] identifier [opt] enum-base [opt]
17908 { enumerator-list, }attributes[opt] [C++0x]
17910 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17911 if the token stream isn't an enum-specifier after all. */
17913 static tree
17914 cp_parser_enum_specifier (cp_parser* parser)
17916 tree identifier;
17917 tree type = NULL_TREE;
17918 tree prev_scope;
17919 tree nested_name_specifier = NULL_TREE;
17920 tree attributes;
17921 bool scoped_enum_p = false;
17922 bool has_underlying_type = false;
17923 bool nested_being_defined = false;
17924 bool new_value_list = false;
17925 bool is_new_type = false;
17926 bool is_unnamed = false;
17927 tree underlying_type = NULL_TREE;
17928 cp_token *type_start_token = NULL;
17929 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17931 parser->colon_corrects_to_scope_p = false;
17933 /* Parse tentatively so that we can back up if we don't find a
17934 enum-specifier. */
17935 cp_parser_parse_tentatively (parser);
17937 /* Caller guarantees that the current token is 'enum', an identifier
17938 possibly follows, and the token after that is an opening brace.
17939 If we don't have an identifier, fabricate an anonymous name for
17940 the enumeration being defined. */
17941 cp_lexer_consume_token (parser->lexer);
17943 /* Parse the "class" or "struct", which indicates a scoped
17944 enumeration type in C++0x. */
17945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17946 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17948 if (cxx_dialect < cxx11)
17949 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17951 /* Consume the `struct' or `class' token. */
17952 cp_lexer_consume_token (parser->lexer);
17954 scoped_enum_p = true;
17957 attributes = cp_parser_attributes_opt (parser);
17959 /* Clear the qualification. */
17960 parser->scope = NULL_TREE;
17961 parser->qualifying_scope = NULL_TREE;
17962 parser->object_scope = NULL_TREE;
17964 /* Figure out in what scope the declaration is being placed. */
17965 prev_scope = current_scope ();
17967 type_start_token = cp_lexer_peek_token (parser->lexer);
17969 push_deferring_access_checks (dk_no_check);
17970 nested_name_specifier
17971 = cp_parser_nested_name_specifier_opt (parser,
17972 /*typename_keyword_p=*/true,
17973 /*check_dependency_p=*/false,
17974 /*type_p=*/false,
17975 /*is_declaration=*/false);
17977 if (nested_name_specifier)
17979 tree name;
17981 identifier = cp_parser_identifier (parser);
17982 name = cp_parser_lookup_name (parser, identifier,
17983 enum_type,
17984 /*is_template=*/false,
17985 /*is_namespace=*/false,
17986 /*check_dependency=*/true,
17987 /*ambiguous_decls=*/NULL,
17988 input_location);
17989 if (name && name != error_mark_node)
17991 type = TREE_TYPE (name);
17992 if (TREE_CODE (type) == TYPENAME_TYPE)
17994 /* Are template enums allowed in ISO? */
17995 if (template_parm_scope_p ())
17996 pedwarn (type_start_token->location, OPT_Wpedantic,
17997 "%qD is an enumeration template", name);
17998 /* ignore a typename reference, for it will be solved by name
17999 in start_enum. */
18000 type = NULL_TREE;
18003 else if (nested_name_specifier == error_mark_node)
18004 /* We already issued an error. */;
18005 else
18007 error_at (type_start_token->location,
18008 "%qD does not name an enumeration in %qT",
18009 identifier, nested_name_specifier);
18010 nested_name_specifier = error_mark_node;
18013 else
18015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18016 identifier = cp_parser_identifier (parser);
18017 else
18019 identifier = make_anon_name ();
18020 is_unnamed = true;
18021 if (scoped_enum_p)
18022 error_at (type_start_token->location,
18023 "unnamed scoped enum is not allowed");
18026 pop_deferring_access_checks ();
18028 /* Check for the `:' that denotes a specified underlying type in C++0x.
18029 Note that a ':' could also indicate a bitfield width, however. */
18030 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18032 cp_decl_specifier_seq type_specifiers;
18034 /* Consume the `:'. */
18035 cp_lexer_consume_token (parser->lexer);
18037 /* Parse the type-specifier-seq. */
18038 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18039 /*is_trailing_return=*/false,
18040 &type_specifiers);
18042 /* At this point this is surely not elaborated type specifier. */
18043 if (!cp_parser_parse_definitely (parser))
18044 return NULL_TREE;
18046 if (cxx_dialect < cxx11)
18047 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18049 has_underlying_type = true;
18051 /* If that didn't work, stop. */
18052 if (type_specifiers.type != error_mark_node)
18054 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18055 /*initialized=*/0, NULL);
18056 if (underlying_type == error_mark_node
18057 || check_for_bare_parameter_packs (underlying_type))
18058 underlying_type = NULL_TREE;
18062 /* Look for the `{' but don't consume it yet. */
18063 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18065 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18067 cp_parser_error (parser, "expected %<{%>");
18068 if (has_underlying_type)
18070 type = NULL_TREE;
18071 goto out;
18074 /* An opaque-enum-specifier must have a ';' here. */
18075 if ((scoped_enum_p || underlying_type)
18076 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18078 cp_parser_error (parser, "expected %<;%> or %<{%>");
18079 if (has_underlying_type)
18081 type = NULL_TREE;
18082 goto out;
18087 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18088 return NULL_TREE;
18090 if (nested_name_specifier)
18092 if (CLASS_TYPE_P (nested_name_specifier))
18094 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18095 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18096 push_scope (nested_name_specifier);
18098 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18100 push_nested_namespace (nested_name_specifier);
18104 /* Issue an error message if type-definitions are forbidden here. */
18105 if (!cp_parser_check_type_definition (parser))
18106 type = error_mark_node;
18107 else
18108 /* Create the new type. We do this before consuming the opening
18109 brace so the enum will be recorded as being on the line of its
18110 tag (or the 'enum' keyword, if there is no tag). */
18111 type = start_enum (identifier, type, underlying_type,
18112 attributes, scoped_enum_p, &is_new_type);
18114 /* If the next token is not '{' it is an opaque-enum-specifier or an
18115 elaborated-type-specifier. */
18116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18118 timevar_push (TV_PARSE_ENUM);
18119 if (nested_name_specifier
18120 && nested_name_specifier != error_mark_node)
18122 /* The following catches invalid code such as:
18123 enum class S<int>::E { A, B, C }; */
18124 if (!processing_specialization
18125 && CLASS_TYPE_P (nested_name_specifier)
18126 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18127 error_at (type_start_token->location, "cannot add an enumerator "
18128 "list to a template instantiation");
18130 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18132 error_at (type_start_token->location,
18133 "%<%T::%E%> has not been declared",
18134 TYPE_CONTEXT (nested_name_specifier),
18135 nested_name_specifier);
18136 type = error_mark_node;
18138 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18139 && !CLASS_TYPE_P (nested_name_specifier))
18141 error_at (type_start_token->location, "nested name specifier "
18142 "%qT for enum declaration does not name a class "
18143 "or namespace", nested_name_specifier);
18144 type = error_mark_node;
18146 /* If that scope does not contain the scope in which the
18147 class was originally declared, the program is invalid. */
18148 else if (prev_scope && !is_ancestor (prev_scope,
18149 nested_name_specifier))
18151 if (at_namespace_scope_p ())
18152 error_at (type_start_token->location,
18153 "declaration of %qD in namespace %qD which does not "
18154 "enclose %qD",
18155 type, prev_scope, nested_name_specifier);
18156 else
18157 error_at (type_start_token->location,
18158 "declaration of %qD in %qD which does not "
18159 "enclose %qD",
18160 type, prev_scope, nested_name_specifier);
18161 type = error_mark_node;
18163 /* If that scope is the scope where the declaration is being placed
18164 the program is invalid. */
18165 else if (CLASS_TYPE_P (nested_name_specifier)
18166 && CLASS_TYPE_P (prev_scope)
18167 && same_type_p (nested_name_specifier, prev_scope))
18169 permerror (type_start_token->location,
18170 "extra qualification not allowed");
18171 nested_name_specifier = NULL_TREE;
18175 if (scoped_enum_p)
18176 begin_scope (sk_scoped_enum, type);
18178 /* Consume the opening brace. */
18179 matching_braces braces;
18180 braces.consume_open (parser);
18182 if (type == error_mark_node)
18183 ; /* Nothing to add */
18184 else if (OPAQUE_ENUM_P (type)
18185 || (cxx_dialect > cxx98 && processing_specialization))
18187 new_value_list = true;
18188 SET_OPAQUE_ENUM_P (type, false);
18189 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18191 else
18193 error_at (type_start_token->location,
18194 "multiple definition of %q#T", type);
18195 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18196 "previous definition here");
18197 type = error_mark_node;
18200 if (type == error_mark_node)
18201 cp_parser_skip_to_end_of_block_or_statement (parser);
18202 /* If the next token is not '}', then there are some enumerators. */
18203 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18205 if (is_unnamed && !scoped_enum_p)
18206 pedwarn (type_start_token->location, OPT_Wpedantic,
18207 "ISO C++ forbids empty unnamed enum");
18209 else
18210 cp_parser_enumerator_list (parser, type);
18212 /* Consume the final '}'. */
18213 braces.require_close (parser);
18215 if (scoped_enum_p)
18216 finish_scope ();
18217 timevar_pop (TV_PARSE_ENUM);
18219 else
18221 /* If a ';' follows, then it is an opaque-enum-specifier
18222 and additional restrictions apply. */
18223 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18225 if (is_unnamed)
18226 error_at (type_start_token->location,
18227 "opaque-enum-specifier without name");
18228 else if (nested_name_specifier)
18229 error_at (type_start_token->location,
18230 "opaque-enum-specifier must use a simple identifier");
18234 /* Look for trailing attributes to apply to this enumeration, and
18235 apply them if appropriate. */
18236 if (cp_parser_allow_gnu_extensions_p (parser))
18238 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18239 cplus_decl_attributes (&type,
18240 trailing_attr,
18241 (int) ATTR_FLAG_TYPE_IN_PLACE);
18244 /* Finish up the enumeration. */
18245 if (type != error_mark_node)
18247 if (new_value_list)
18248 finish_enum_value_list (type);
18249 if (is_new_type)
18250 finish_enum (type);
18253 if (nested_name_specifier)
18255 if (CLASS_TYPE_P (nested_name_specifier))
18257 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18258 pop_scope (nested_name_specifier);
18260 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18262 pop_nested_namespace (nested_name_specifier);
18265 out:
18266 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18267 return type;
18270 /* Parse an enumerator-list. The enumerators all have the indicated
18271 TYPE.
18273 enumerator-list:
18274 enumerator-definition
18275 enumerator-list , enumerator-definition */
18277 static void
18278 cp_parser_enumerator_list (cp_parser* parser, tree type)
18280 while (true)
18282 /* Parse an enumerator-definition. */
18283 cp_parser_enumerator_definition (parser, type);
18285 /* If the next token is not a ',', we've reached the end of
18286 the list. */
18287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18288 break;
18289 /* Otherwise, consume the `,' and keep going. */
18290 cp_lexer_consume_token (parser->lexer);
18291 /* If the next token is a `}', there is a trailing comma. */
18292 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18294 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18295 pedwarn (input_location, OPT_Wpedantic,
18296 "comma at end of enumerator list");
18297 break;
18302 /* Parse an enumerator-definition. The enumerator has the indicated
18303 TYPE.
18305 enumerator-definition:
18306 enumerator
18307 enumerator = constant-expression
18309 enumerator:
18310 identifier
18312 GNU Extensions:
18314 enumerator-definition:
18315 enumerator attributes [opt]
18316 enumerator attributes [opt] = constant-expression */
18318 static void
18319 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18321 tree identifier;
18322 tree value;
18323 location_t loc;
18325 /* Save the input location because we are interested in the location
18326 of the identifier and not the location of the explicit value. */
18327 loc = cp_lexer_peek_token (parser->lexer)->location;
18329 /* Look for the identifier. */
18330 identifier = cp_parser_identifier (parser);
18331 if (identifier == error_mark_node)
18332 return;
18334 /* Parse any specified attributes. */
18335 tree attrs = cp_parser_attributes_opt (parser);
18337 /* If the next token is an '=', then there is an explicit value. */
18338 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18340 /* Consume the `=' token. */
18341 cp_lexer_consume_token (parser->lexer);
18342 /* Parse the value. */
18343 value = cp_parser_constant_expression (parser);
18345 else
18346 value = NULL_TREE;
18348 /* If we are processing a template, make sure the initializer of the
18349 enumerator doesn't contain any bare template parameter pack. */
18350 if (check_for_bare_parameter_packs (value))
18351 value = error_mark_node;
18353 /* Create the enumerator. */
18354 build_enumerator (identifier, value, type, attrs, loc);
18357 /* Parse a namespace-name.
18359 namespace-name:
18360 original-namespace-name
18361 namespace-alias
18363 Returns the NAMESPACE_DECL for the namespace. */
18365 static tree
18366 cp_parser_namespace_name (cp_parser* parser)
18368 tree identifier;
18369 tree namespace_decl;
18371 cp_token *token = cp_lexer_peek_token (parser->lexer);
18373 /* Get the name of the namespace. */
18374 identifier = cp_parser_identifier (parser);
18375 if (identifier == error_mark_node)
18376 return error_mark_node;
18378 /* Look up the identifier in the currently active scope. Look only
18379 for namespaces, due to:
18381 [basic.lookup.udir]
18383 When looking up a namespace-name in a using-directive or alias
18384 definition, only namespace names are considered.
18386 And:
18388 [basic.lookup.qual]
18390 During the lookup of a name preceding the :: scope resolution
18391 operator, object, function, and enumerator names are ignored.
18393 (Note that cp_parser_qualifying_entity only calls this
18394 function if the token after the name is the scope resolution
18395 operator.) */
18396 namespace_decl = cp_parser_lookup_name (parser, identifier,
18397 none_type,
18398 /*is_template=*/false,
18399 /*is_namespace=*/true,
18400 /*check_dependency=*/true,
18401 /*ambiguous_decls=*/NULL,
18402 token->location);
18403 /* If it's not a namespace, issue an error. */
18404 if (namespace_decl == error_mark_node
18405 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18407 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18409 error_at (token->location, "%qD is not a namespace-name", identifier);
18410 if (namespace_decl == error_mark_node
18411 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18412 suggest_alternative_in_explicit_scope (token->location, identifier,
18413 parser->scope);
18415 cp_parser_error (parser, "expected namespace-name");
18416 namespace_decl = error_mark_node;
18419 return namespace_decl;
18422 /* Parse a namespace-definition.
18424 namespace-definition:
18425 named-namespace-definition
18426 unnamed-namespace-definition
18428 named-namespace-definition:
18429 original-namespace-definition
18430 extension-namespace-definition
18432 original-namespace-definition:
18433 namespace identifier { namespace-body }
18435 extension-namespace-definition:
18436 namespace original-namespace-name { namespace-body }
18438 unnamed-namespace-definition:
18439 namespace { namespace-body } */
18441 static void
18442 cp_parser_namespace_definition (cp_parser* parser)
18444 tree identifier;
18445 int nested_definition_count = 0;
18447 cp_ensure_no_omp_declare_simd (parser);
18448 cp_ensure_no_oacc_routine (parser);
18450 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18452 if (is_inline)
18454 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18455 cp_lexer_consume_token (parser->lexer);
18458 /* Look for the `namespace' keyword. */
18459 cp_token* token
18460 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18462 /* Parse any specified attributes before the identifier. */
18463 tree attribs = cp_parser_attributes_opt (parser);
18465 for (;;)
18467 identifier = NULL_TREE;
18469 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18471 identifier = cp_parser_identifier (parser);
18473 /* Parse any attributes specified after the identifier. */
18474 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18478 break;
18480 if (!nested_definition_count && cxx_dialect < cxx17)
18481 pedwarn (input_location, OPT_Wpedantic,
18482 "nested namespace definitions only available with "
18483 "-std=c++17 or -std=gnu++17");
18485 /* Nested namespace names can create new namespaces (unlike
18486 other qualified-ids). */
18487 if (int count = identifier ? push_namespace (identifier) : 0)
18488 nested_definition_count += count;
18489 else
18490 cp_parser_error (parser, "nested namespace name required");
18491 cp_lexer_consume_token (parser->lexer);
18494 if (nested_definition_count && !identifier)
18495 cp_parser_error (parser, "namespace name required");
18497 if (nested_definition_count && attribs)
18498 error_at (token->location,
18499 "a nested namespace definition cannot have attributes");
18500 if (nested_definition_count && is_inline)
18501 error_at (token->location,
18502 "a nested namespace definition cannot be inline");
18504 /* Start the namespace. */
18505 nested_definition_count += push_namespace (identifier, is_inline);
18507 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18509 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18511 /* Look for the `{' to validate starting the namespace. */
18512 matching_braces braces;
18513 if (braces.require_open (parser))
18515 /* Parse the body of the namespace. */
18516 cp_parser_namespace_body (parser);
18518 /* Look for the final `}'. */
18519 braces.require_close (parser);
18522 if (has_visibility)
18523 pop_visibility (1);
18525 /* Pop the nested namespace definitions. */
18526 while (nested_definition_count--)
18527 pop_namespace ();
18530 /* Parse a namespace-body.
18532 namespace-body:
18533 declaration-seq [opt] */
18535 static void
18536 cp_parser_namespace_body (cp_parser* parser)
18538 cp_parser_declaration_seq_opt (parser);
18541 /* Parse a namespace-alias-definition.
18543 namespace-alias-definition:
18544 namespace identifier = qualified-namespace-specifier ; */
18546 static void
18547 cp_parser_namespace_alias_definition (cp_parser* parser)
18549 tree identifier;
18550 tree namespace_specifier;
18552 cp_token *token = cp_lexer_peek_token (parser->lexer);
18554 /* Look for the `namespace' keyword. */
18555 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18556 /* Look for the identifier. */
18557 identifier = cp_parser_identifier (parser);
18558 if (identifier == error_mark_node)
18559 return;
18560 /* Look for the `=' token. */
18561 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18562 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18564 error_at (token->location, "%<namespace%> definition is not allowed here");
18565 /* Skip the definition. */
18566 cp_lexer_consume_token (parser->lexer);
18567 if (cp_parser_skip_to_closing_brace (parser))
18568 cp_lexer_consume_token (parser->lexer);
18569 return;
18571 cp_parser_require (parser, CPP_EQ, RT_EQ);
18572 /* Look for the qualified-namespace-specifier. */
18573 namespace_specifier
18574 = cp_parser_qualified_namespace_specifier (parser);
18575 /* Look for the `;' token. */
18576 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18578 /* Register the alias in the symbol table. */
18579 do_namespace_alias (identifier, namespace_specifier);
18582 /* Parse a qualified-namespace-specifier.
18584 qualified-namespace-specifier:
18585 :: [opt] nested-name-specifier [opt] namespace-name
18587 Returns a NAMESPACE_DECL corresponding to the specified
18588 namespace. */
18590 static tree
18591 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18593 /* Look for the optional `::'. */
18594 cp_parser_global_scope_opt (parser,
18595 /*current_scope_valid_p=*/false);
18597 /* Look for the optional nested-name-specifier. */
18598 cp_parser_nested_name_specifier_opt (parser,
18599 /*typename_keyword_p=*/false,
18600 /*check_dependency_p=*/true,
18601 /*type_p=*/false,
18602 /*is_declaration=*/true);
18604 return cp_parser_namespace_name (parser);
18607 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18608 access declaration.
18610 using-declaration:
18611 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18612 using :: unqualified-id ;
18614 access-declaration:
18615 qualified-id ;
18619 static bool
18620 cp_parser_using_declaration (cp_parser* parser,
18621 bool access_declaration_p)
18623 cp_token *token;
18624 bool typename_p = false;
18625 bool global_scope_p;
18626 tree decl;
18627 tree identifier;
18628 tree qscope;
18629 int oldcount = errorcount;
18630 cp_token *diag_token = NULL;
18632 if (access_declaration_p)
18634 diag_token = cp_lexer_peek_token (parser->lexer);
18635 cp_parser_parse_tentatively (parser);
18637 else
18639 /* Look for the `using' keyword. */
18640 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18642 again:
18643 /* Peek at the next token. */
18644 token = cp_lexer_peek_token (parser->lexer);
18645 /* See if it's `typename'. */
18646 if (token->keyword == RID_TYPENAME)
18648 /* Remember that we've seen it. */
18649 typename_p = true;
18650 /* Consume the `typename' token. */
18651 cp_lexer_consume_token (parser->lexer);
18655 /* Look for the optional global scope qualification. */
18656 global_scope_p
18657 = (cp_parser_global_scope_opt (parser,
18658 /*current_scope_valid_p=*/false)
18659 != NULL_TREE);
18661 /* If we saw `typename', or didn't see `::', then there must be a
18662 nested-name-specifier present. */
18663 if (typename_p || !global_scope_p)
18665 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18666 /*check_dependency_p=*/true,
18667 /*type_p=*/false,
18668 /*is_declaration=*/true);
18669 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18671 cp_parser_skip_to_end_of_block_or_statement (parser);
18672 return false;
18675 /* Otherwise, we could be in either of the two productions. In that
18676 case, treat the nested-name-specifier as optional. */
18677 else
18678 qscope = cp_parser_nested_name_specifier_opt (parser,
18679 /*typename_keyword_p=*/false,
18680 /*check_dependency_p=*/true,
18681 /*type_p=*/false,
18682 /*is_declaration=*/true);
18683 if (!qscope)
18684 qscope = global_namespace;
18685 else if (UNSCOPED_ENUM_P (qscope))
18686 qscope = CP_TYPE_CONTEXT (qscope);
18688 if (access_declaration_p && cp_parser_error_occurred (parser))
18689 /* Something has already gone wrong; there's no need to parse
18690 further. Since an error has occurred, the return value of
18691 cp_parser_parse_definitely will be false, as required. */
18692 return cp_parser_parse_definitely (parser);
18694 token = cp_lexer_peek_token (parser->lexer);
18695 /* Parse the unqualified-id. */
18696 identifier = cp_parser_unqualified_id (parser,
18697 /*template_keyword_p=*/false,
18698 /*check_dependency_p=*/true,
18699 /*declarator_p=*/true,
18700 /*optional_p=*/false);
18702 if (access_declaration_p)
18704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18705 cp_parser_simulate_error (parser);
18706 if (!cp_parser_parse_definitely (parser))
18707 return false;
18709 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18711 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18712 if (cxx_dialect < cxx17
18713 && !in_system_header_at (ell->location))
18714 pedwarn (ell->location, 0,
18715 "pack expansion in using-declaration only available "
18716 "with -std=c++17 or -std=gnu++17");
18717 qscope = make_pack_expansion (qscope);
18720 /* The function we call to handle a using-declaration is different
18721 depending on what scope we are in. */
18722 if (qscope == error_mark_node || identifier == error_mark_node)
18724 else if (!identifier_p (identifier)
18725 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18726 /* [namespace.udecl]
18728 A using declaration shall not name a template-id. */
18729 error_at (token->location,
18730 "a template-id may not appear in a using-declaration");
18731 else
18733 if (at_class_scope_p ())
18735 /* Create the USING_DECL. */
18736 decl = do_class_using_decl (qscope, identifier);
18738 if (decl && typename_p)
18739 USING_DECL_TYPENAME_P (decl) = 1;
18741 if (check_for_bare_parameter_packs (decl))
18743 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18744 return false;
18746 else
18747 /* Add it to the list of members in this class. */
18748 finish_member_declaration (decl);
18750 else
18752 decl = cp_parser_lookup_name_simple (parser,
18753 identifier,
18754 token->location);
18755 if (decl == error_mark_node)
18756 cp_parser_name_lookup_error (parser, identifier,
18757 decl, NLE_NULL,
18758 token->location);
18759 else if (check_for_bare_parameter_packs (decl))
18761 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18762 return false;
18764 else if (!at_namespace_scope_p ())
18765 finish_local_using_decl (decl, qscope, identifier);
18766 else
18767 finish_namespace_using_decl (decl, qscope, identifier);
18771 if (!access_declaration_p
18772 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18774 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18775 if (cxx_dialect < cxx17)
18776 pedwarn (comma->location, 0,
18777 "comma-separated list in using-declaration only available "
18778 "with -std=c++17 or -std=gnu++17");
18779 goto again;
18782 /* Look for the final `;'. */
18783 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18785 if (access_declaration_p && errorcount == oldcount)
18786 warning_at (diag_token->location, OPT_Wdeprecated,
18787 "access declarations are deprecated "
18788 "in favour of using-declarations; "
18789 "suggestion: add the %<using%> keyword");
18791 return true;
18794 /* Parse an alias-declaration.
18796 alias-declaration:
18797 using identifier attribute-specifier-seq [opt] = type-id */
18799 static tree
18800 cp_parser_alias_declaration (cp_parser* parser)
18802 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18803 location_t id_location;
18804 cp_declarator *declarator;
18805 cp_decl_specifier_seq decl_specs;
18806 bool member_p;
18807 const char *saved_message = NULL;
18809 /* Look for the `using' keyword. */
18810 cp_token *using_token
18811 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18812 if (using_token == NULL)
18813 return error_mark_node;
18815 id_location = cp_lexer_peek_token (parser->lexer)->location;
18816 id = cp_parser_identifier (parser);
18817 if (id == error_mark_node)
18818 return error_mark_node;
18820 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18821 attributes = cp_parser_attributes_opt (parser);
18822 if (attributes == error_mark_node)
18823 return error_mark_node;
18825 cp_parser_require (parser, CPP_EQ, RT_EQ);
18827 if (cp_parser_error_occurred (parser))
18828 return error_mark_node;
18830 cp_parser_commit_to_tentative_parse (parser);
18832 /* Now we are going to parse the type-id of the declaration. */
18835 [dcl.type]/3 says:
18837 "A type-specifier-seq shall not define a class or enumeration
18838 unless it appears in the type-id of an alias-declaration (7.1.3) that
18839 is not the declaration of a template-declaration."
18841 In other words, if we currently are in an alias template, the
18842 type-id should not define a type.
18844 So let's set parser->type_definition_forbidden_message in that
18845 case; cp_parser_check_type_definition (called by
18846 cp_parser_class_specifier) will then emit an error if a type is
18847 defined in the type-id. */
18848 if (parser->num_template_parameter_lists)
18850 saved_message = parser->type_definition_forbidden_message;
18851 parser->type_definition_forbidden_message =
18852 G_("types may not be defined in alias template declarations");
18855 type = cp_parser_type_id (parser);
18857 /* Restore the error message if need be. */
18858 if (parser->num_template_parameter_lists)
18859 parser->type_definition_forbidden_message = saved_message;
18861 if (type == error_mark_node
18862 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18864 cp_parser_skip_to_end_of_block_or_statement (parser);
18865 return error_mark_node;
18868 /* A typedef-name can also be introduced by an alias-declaration. The
18869 identifier following the using keyword becomes a typedef-name. It has
18870 the same semantics as if it were introduced by the typedef
18871 specifier. In particular, it does not define a new type and it shall
18872 not appear in the type-id. */
18874 clear_decl_specs (&decl_specs);
18875 decl_specs.type = type;
18876 if (attributes != NULL_TREE)
18878 decl_specs.attributes = attributes;
18879 set_and_check_decl_spec_loc (&decl_specs,
18880 ds_attribute,
18881 attrs_token);
18883 set_and_check_decl_spec_loc (&decl_specs,
18884 ds_typedef,
18885 using_token);
18886 set_and_check_decl_spec_loc (&decl_specs,
18887 ds_alias,
18888 using_token);
18890 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18891 declarator->id_loc = id_location;
18893 member_p = at_class_scope_p ();
18894 if (member_p)
18895 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18896 NULL_TREE, attributes);
18897 else
18898 decl = start_decl (declarator, &decl_specs, 0,
18899 attributes, NULL_TREE, &pushed_scope);
18900 if (decl == error_mark_node)
18901 return decl;
18903 // Attach constraints to the alias declaration.
18904 if (flag_concepts && current_template_parms)
18906 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18907 tree constr = build_constraints (reqs, NULL_TREE);
18908 set_constraints (decl, constr);
18911 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18913 if (pushed_scope)
18914 pop_scope (pushed_scope);
18916 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18917 added into the symbol table; otherwise, return the TYPE_DECL. */
18918 if (DECL_LANG_SPECIFIC (decl)
18919 && DECL_TEMPLATE_INFO (decl)
18920 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18922 decl = DECL_TI_TEMPLATE (decl);
18923 if (member_p)
18924 check_member_template (decl);
18927 return decl;
18930 /* Parse a using-directive.
18932 using-directive:
18933 using namespace :: [opt] nested-name-specifier [opt]
18934 namespace-name ; */
18936 static void
18937 cp_parser_using_directive (cp_parser* parser)
18939 tree namespace_decl;
18940 tree attribs;
18942 /* Look for the `using' keyword. */
18943 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18944 /* And the `namespace' keyword. */
18945 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18946 /* Look for the optional `::' operator. */
18947 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18948 /* And the optional nested-name-specifier. */
18949 cp_parser_nested_name_specifier_opt (parser,
18950 /*typename_keyword_p=*/false,
18951 /*check_dependency_p=*/true,
18952 /*type_p=*/false,
18953 /*is_declaration=*/true);
18954 /* Get the namespace being used. */
18955 namespace_decl = cp_parser_namespace_name (parser);
18956 /* And any specified attributes. */
18957 attribs = cp_parser_attributes_opt (parser);
18959 /* Update the symbol table. */
18960 if (namespace_bindings_p ())
18961 finish_namespace_using_directive (namespace_decl, attribs);
18962 else
18963 finish_local_using_directive (namespace_decl, attribs);
18965 /* Look for the final `;'. */
18966 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18969 /* Parse an asm-definition.
18971 asm-definition:
18972 asm ( string-literal ) ;
18974 GNU Extension:
18976 asm-definition:
18977 asm volatile [opt] ( string-literal ) ;
18978 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18979 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18980 : asm-operand-list [opt] ) ;
18981 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18982 : asm-operand-list [opt]
18983 : asm-clobber-list [opt] ) ;
18984 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18985 : asm-clobber-list [opt]
18986 : asm-goto-list ) ; */
18988 static void
18989 cp_parser_asm_definition (cp_parser* parser)
18991 tree string;
18992 tree outputs = NULL_TREE;
18993 tree inputs = NULL_TREE;
18994 tree clobbers = NULL_TREE;
18995 tree labels = NULL_TREE;
18996 tree asm_stmt;
18997 bool volatile_p = false;
18998 bool extended_p = false;
18999 bool invalid_inputs_p = false;
19000 bool invalid_outputs_p = false;
19001 bool goto_p = false;
19002 required_token missing = RT_NONE;
19004 /* Look for the `asm' keyword. */
19005 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19007 if (parser->in_function_body
19008 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19010 error ("%<asm%> in %<constexpr%> function");
19011 cp_function_chain->invalid_constexpr = true;
19014 /* See if the next token is `volatile'. */
19015 if (cp_parser_allow_gnu_extensions_p (parser)
19016 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19018 /* Remember that we saw the `volatile' keyword. */
19019 volatile_p = true;
19020 /* Consume the token. */
19021 cp_lexer_consume_token (parser->lexer);
19023 if (cp_parser_allow_gnu_extensions_p (parser)
19024 && parser->in_function_body
19025 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19027 /* Remember that we saw the `goto' keyword. */
19028 goto_p = true;
19029 /* Consume the token. */
19030 cp_lexer_consume_token (parser->lexer);
19032 /* Look for the opening `('. */
19033 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19034 return;
19035 /* Look for the string. */
19036 string = cp_parser_string_literal (parser, false, false);
19037 if (string == error_mark_node)
19039 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19040 /*consume_paren=*/true);
19041 return;
19044 /* If we're allowing GNU extensions, check for the extended assembly
19045 syntax. Unfortunately, the `:' tokens need not be separated by
19046 a space in C, and so, for compatibility, we tolerate that here
19047 too. Doing that means that we have to treat the `::' operator as
19048 two `:' tokens. */
19049 if (cp_parser_allow_gnu_extensions_p (parser)
19050 && parser->in_function_body
19051 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19052 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19054 bool inputs_p = false;
19055 bool clobbers_p = false;
19056 bool labels_p = false;
19058 /* The extended syntax was used. */
19059 extended_p = true;
19061 /* Look for outputs. */
19062 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19064 /* Consume the `:'. */
19065 cp_lexer_consume_token (parser->lexer);
19066 /* Parse the output-operands. */
19067 if (cp_lexer_next_token_is_not (parser->lexer,
19068 CPP_COLON)
19069 && cp_lexer_next_token_is_not (parser->lexer,
19070 CPP_SCOPE)
19071 && cp_lexer_next_token_is_not (parser->lexer,
19072 CPP_CLOSE_PAREN)
19073 && !goto_p)
19075 outputs = cp_parser_asm_operand_list (parser);
19076 if (outputs == error_mark_node)
19077 invalid_outputs_p = true;
19080 /* If the next token is `::', there are no outputs, and the
19081 next token is the beginning of the inputs. */
19082 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19083 /* The inputs are coming next. */
19084 inputs_p = true;
19086 /* Look for inputs. */
19087 if (inputs_p
19088 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19090 /* Consume the `:' or `::'. */
19091 cp_lexer_consume_token (parser->lexer);
19092 /* Parse the output-operands. */
19093 if (cp_lexer_next_token_is_not (parser->lexer,
19094 CPP_COLON)
19095 && cp_lexer_next_token_is_not (parser->lexer,
19096 CPP_SCOPE)
19097 && cp_lexer_next_token_is_not (parser->lexer,
19098 CPP_CLOSE_PAREN))
19100 inputs = cp_parser_asm_operand_list (parser);
19101 if (inputs == error_mark_node)
19102 invalid_inputs_p = true;
19105 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19106 /* The clobbers are coming next. */
19107 clobbers_p = true;
19109 /* Look for clobbers. */
19110 if (clobbers_p
19111 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19113 clobbers_p = true;
19114 /* Consume the `:' or `::'. */
19115 cp_lexer_consume_token (parser->lexer);
19116 /* Parse the clobbers. */
19117 if (cp_lexer_next_token_is_not (parser->lexer,
19118 CPP_COLON)
19119 && cp_lexer_next_token_is_not (parser->lexer,
19120 CPP_CLOSE_PAREN))
19121 clobbers = cp_parser_asm_clobber_list (parser);
19123 else if (goto_p
19124 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19125 /* The labels are coming next. */
19126 labels_p = true;
19128 /* Look for labels. */
19129 if (labels_p
19130 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19132 labels_p = true;
19133 /* Consume the `:' or `::'. */
19134 cp_lexer_consume_token (parser->lexer);
19135 /* Parse the labels. */
19136 labels = cp_parser_asm_label_list (parser);
19139 if (goto_p && !labels_p)
19140 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19142 else if (goto_p)
19143 missing = RT_COLON_SCOPE;
19145 /* Look for the closing `)'. */
19146 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19147 missing ? missing : RT_CLOSE_PAREN))
19148 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19149 /*consume_paren=*/true);
19150 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19152 if (!invalid_inputs_p && !invalid_outputs_p)
19154 /* Create the ASM_EXPR. */
19155 if (parser->in_function_body)
19157 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19158 inputs, clobbers, labels);
19159 /* If the extended syntax was not used, mark the ASM_EXPR. */
19160 if (!extended_p)
19162 tree temp = asm_stmt;
19163 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19164 temp = TREE_OPERAND (temp, 0);
19166 ASM_INPUT_P (temp) = 1;
19169 else
19170 symtab->finalize_toplevel_asm (string);
19174 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19175 type that comes from the decl-specifier-seq. */
19177 static tree
19178 strip_declarator_types (tree type, cp_declarator *declarator)
19180 for (cp_declarator *d = declarator; d;)
19181 switch (d->kind)
19183 case cdk_id:
19184 case cdk_decomp:
19185 case cdk_error:
19186 d = NULL;
19187 break;
19189 default:
19190 if (TYPE_PTRMEMFUNC_P (type))
19191 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19192 type = TREE_TYPE (type);
19193 d = d->declarator;
19194 break;
19197 return type;
19200 /* Declarators [gram.dcl.decl] */
19202 /* Parse an init-declarator.
19204 init-declarator:
19205 declarator initializer [opt]
19207 GNU Extension:
19209 init-declarator:
19210 declarator asm-specification [opt] attributes [opt] initializer [opt]
19212 function-definition:
19213 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19214 function-body
19215 decl-specifier-seq [opt] declarator function-try-block
19217 GNU Extension:
19219 function-definition:
19220 __extension__ function-definition
19222 TM Extension:
19224 function-definition:
19225 decl-specifier-seq [opt] declarator function-transaction-block
19227 The DECL_SPECIFIERS apply to this declarator. Returns a
19228 representation of the entity declared. If MEMBER_P is TRUE, then
19229 this declarator appears in a class scope. The new DECL created by
19230 this declarator is returned.
19232 The CHECKS are access checks that should be performed once we know
19233 what entity is being declared (and, therefore, what classes have
19234 befriended it).
19236 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19237 for a function-definition here as well. If the declarator is a
19238 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19239 be TRUE upon return. By that point, the function-definition will
19240 have been completely parsed.
19242 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19243 is FALSE.
19245 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19246 parsed declaration if it is an uninitialized single declarator not followed
19247 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19248 if present, will not be consumed. If returned, this declarator will be
19249 created with SD_INITIALIZED but will not call cp_finish_decl.
19251 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19252 and there is an initializer, the pointed location_t is set to the
19253 location of the '=' or `(', or '{' in C++11 token introducing the
19254 initializer. */
19256 static tree
19257 cp_parser_init_declarator (cp_parser* parser,
19258 cp_decl_specifier_seq *decl_specifiers,
19259 vec<deferred_access_check, va_gc> *checks,
19260 bool function_definition_allowed_p,
19261 bool member_p,
19262 int declares_class_or_enum,
19263 bool* function_definition_p,
19264 tree* maybe_range_for_decl,
19265 location_t* init_loc,
19266 tree* auto_result)
19268 cp_token *token = NULL, *asm_spec_start_token = NULL,
19269 *attributes_start_token = NULL;
19270 cp_declarator *declarator;
19271 tree prefix_attributes;
19272 tree attributes = NULL;
19273 tree asm_specification;
19274 tree initializer;
19275 tree decl = NULL_TREE;
19276 tree scope;
19277 int is_initialized;
19278 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19279 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19280 "(...)". */
19281 enum cpp_ttype initialization_kind;
19282 bool is_direct_init = false;
19283 bool is_non_constant_init;
19284 int ctor_dtor_or_conv_p;
19285 bool friend_p = cp_parser_friend_p (decl_specifiers);
19286 tree pushed_scope = NULL_TREE;
19287 bool range_for_decl_p = false;
19288 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19289 location_t tmp_init_loc = UNKNOWN_LOCATION;
19291 /* Gather the attributes that were provided with the
19292 decl-specifiers. */
19293 prefix_attributes = decl_specifiers->attributes;
19295 /* Assume that this is not the declarator for a function
19296 definition. */
19297 if (function_definition_p)
19298 *function_definition_p = false;
19300 /* Default arguments are only permitted for function parameters. */
19301 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19302 parser->default_arg_ok_p = false;
19304 /* Defer access checks while parsing the declarator; we cannot know
19305 what names are accessible until we know what is being
19306 declared. */
19307 resume_deferring_access_checks ();
19309 token = cp_lexer_peek_token (parser->lexer);
19311 /* Parse the declarator. */
19312 declarator
19313 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19314 &ctor_dtor_or_conv_p,
19315 /*parenthesized_p=*/NULL,
19316 member_p, friend_p);
19317 /* Gather up the deferred checks. */
19318 stop_deferring_access_checks ();
19320 parser->default_arg_ok_p = saved_default_arg_ok_p;
19322 /* If the DECLARATOR was erroneous, there's no need to go
19323 further. */
19324 if (declarator == cp_error_declarator)
19325 return error_mark_node;
19327 /* Check that the number of template-parameter-lists is OK. */
19328 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19329 token->location))
19330 return error_mark_node;
19332 if (declares_class_or_enum & 2)
19333 cp_parser_check_for_definition_in_return_type (declarator,
19334 decl_specifiers->type,
19335 decl_specifiers->locations[ds_type_spec]);
19337 /* Figure out what scope the entity declared by the DECLARATOR is
19338 located in. `grokdeclarator' sometimes changes the scope, so
19339 we compute it now. */
19340 scope = get_scope_of_declarator (declarator);
19342 /* Perform any lookups in the declared type which were thought to be
19343 dependent, but are not in the scope of the declarator. */
19344 decl_specifiers->type
19345 = maybe_update_decl_type (decl_specifiers->type, scope);
19347 /* If we're allowing GNU extensions, look for an
19348 asm-specification. */
19349 if (cp_parser_allow_gnu_extensions_p (parser))
19351 /* Look for an asm-specification. */
19352 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19353 asm_specification = cp_parser_asm_specification_opt (parser);
19355 else
19356 asm_specification = NULL_TREE;
19358 /* Look for attributes. */
19359 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19360 attributes = cp_parser_attributes_opt (parser);
19362 /* Peek at the next token. */
19363 token = cp_lexer_peek_token (parser->lexer);
19365 bool bogus_implicit_tmpl = false;
19367 if (function_declarator_p (declarator))
19369 /* Handle C++17 deduction guides. */
19370 if (!decl_specifiers->type
19371 && ctor_dtor_or_conv_p <= 0
19372 && cxx_dialect >= cxx17)
19374 cp_declarator *id = get_id_declarator (declarator);
19375 tree name = id->u.id.unqualified_name;
19376 parser->scope = id->u.id.qualifying_scope;
19377 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19378 if (tmpl
19379 && (DECL_CLASS_TEMPLATE_P (tmpl)
19380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19382 id->u.id.unqualified_name = dguide_name (tmpl);
19383 id->u.id.sfk = sfk_deduction_guide;
19384 ctor_dtor_or_conv_p = 1;
19388 /* Check to see if the token indicates the start of a
19389 function-definition. */
19390 if (cp_parser_token_starts_function_definition_p (token))
19392 if (!function_definition_allowed_p)
19394 /* If a function-definition should not appear here, issue an
19395 error message. */
19396 cp_parser_error (parser,
19397 "a function-definition is not allowed here");
19398 return error_mark_node;
19401 location_t func_brace_location
19402 = cp_lexer_peek_token (parser->lexer)->location;
19404 /* Neither attributes nor an asm-specification are allowed
19405 on a function-definition. */
19406 if (asm_specification)
19407 error_at (asm_spec_start_token->location,
19408 "an asm-specification is not allowed "
19409 "on a function-definition");
19410 if (attributes)
19411 error_at (attributes_start_token->location,
19412 "attributes are not allowed "
19413 "on a function-definition");
19414 /* This is a function-definition. */
19415 *function_definition_p = true;
19417 /* Parse the function definition. */
19418 if (member_p)
19419 decl = cp_parser_save_member_function_body (parser,
19420 decl_specifiers,
19421 declarator,
19422 prefix_attributes);
19423 else
19424 decl =
19425 (cp_parser_function_definition_from_specifiers_and_declarator
19426 (parser, decl_specifiers, prefix_attributes, declarator));
19428 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19430 /* This is where the prologue starts... */
19431 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19432 = func_brace_location;
19435 return decl;
19438 else if (parser->fully_implicit_function_template_p)
19440 /* A non-template declaration involving a function parameter list
19441 containing an implicit template parameter will be made into a
19442 template. If the resulting declaration is not going to be an
19443 actual function then finish the template scope here to prevent it.
19444 An error message will be issued once we have a decl to talk about.
19446 FIXME probably we should do type deduction rather than create an
19447 implicit template, but the standard currently doesn't allow it. */
19448 bogus_implicit_tmpl = true;
19449 finish_fully_implicit_template (parser, NULL_TREE);
19452 /* [dcl.dcl]
19454 Only in function declarations for constructors, destructors, type
19455 conversions, and deduction guides can the decl-specifier-seq be omitted.
19457 We explicitly postpone this check past the point where we handle
19458 function-definitions because we tolerate function-definitions
19459 that are missing their return types in some modes. */
19460 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19462 cp_parser_error (parser,
19463 "expected constructor, destructor, or type conversion");
19464 return error_mark_node;
19467 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19468 if (token->type == CPP_EQ
19469 || token->type == CPP_OPEN_PAREN
19470 || token->type == CPP_OPEN_BRACE)
19472 is_initialized = SD_INITIALIZED;
19473 initialization_kind = token->type;
19474 if (maybe_range_for_decl)
19475 *maybe_range_for_decl = error_mark_node;
19476 tmp_init_loc = token->location;
19477 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19478 *init_loc = tmp_init_loc;
19480 if (token->type == CPP_EQ
19481 && function_declarator_p (declarator))
19483 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19484 if (t2->keyword == RID_DEFAULT)
19485 is_initialized = SD_DEFAULTED;
19486 else if (t2->keyword == RID_DELETE)
19487 is_initialized = SD_DELETED;
19490 else
19492 /* If the init-declarator isn't initialized and isn't followed by a
19493 `,' or `;', it's not a valid init-declarator. */
19494 if (token->type != CPP_COMMA
19495 && token->type != CPP_SEMICOLON)
19497 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19498 range_for_decl_p = true;
19499 else
19501 if (!maybe_range_for_decl)
19502 cp_parser_error (parser, "expected initializer");
19503 return error_mark_node;
19506 is_initialized = SD_UNINITIALIZED;
19507 initialization_kind = CPP_EOF;
19510 /* Because start_decl has side-effects, we should only call it if we
19511 know we're going ahead. By this point, we know that we cannot
19512 possibly be looking at any other construct. */
19513 cp_parser_commit_to_tentative_parse (parser);
19515 /* Enter the newly declared entry in the symbol table. If we're
19516 processing a declaration in a class-specifier, we wait until
19517 after processing the initializer. */
19518 if (!member_p)
19520 if (parser->in_unbraced_linkage_specification_p)
19521 decl_specifiers->storage_class = sc_extern;
19522 decl = start_decl (declarator, decl_specifiers,
19523 range_for_decl_p? SD_INITIALIZED : is_initialized,
19524 attributes, prefix_attributes, &pushed_scope);
19525 cp_finalize_omp_declare_simd (parser, decl);
19526 cp_finalize_oacc_routine (parser, decl, false);
19527 /* Adjust location of decl if declarator->id_loc is more appropriate:
19528 set, and decl wasn't merged with another decl, in which case its
19529 location would be different from input_location, and more accurate. */
19530 if (DECL_P (decl)
19531 && declarator->id_loc != UNKNOWN_LOCATION
19532 && DECL_SOURCE_LOCATION (decl) == input_location)
19533 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19535 else if (scope)
19536 /* Enter the SCOPE. That way unqualified names appearing in the
19537 initializer will be looked up in SCOPE. */
19538 pushed_scope = push_scope (scope);
19540 /* Perform deferred access control checks, now that we know in which
19541 SCOPE the declared entity resides. */
19542 if (!member_p && decl)
19544 tree saved_current_function_decl = NULL_TREE;
19546 /* If the entity being declared is a function, pretend that we
19547 are in its scope. If it is a `friend', it may have access to
19548 things that would not otherwise be accessible. */
19549 if (TREE_CODE (decl) == FUNCTION_DECL)
19551 saved_current_function_decl = current_function_decl;
19552 current_function_decl = decl;
19555 /* Perform access checks for template parameters. */
19556 cp_parser_perform_template_parameter_access_checks (checks);
19558 /* Perform the access control checks for the declarator and the
19559 decl-specifiers. */
19560 perform_deferred_access_checks (tf_warning_or_error);
19562 /* Restore the saved value. */
19563 if (TREE_CODE (decl) == FUNCTION_DECL)
19564 current_function_decl = saved_current_function_decl;
19567 /* Parse the initializer. */
19568 initializer = NULL_TREE;
19569 is_direct_init = false;
19570 is_non_constant_init = true;
19571 if (is_initialized)
19573 if (function_declarator_p (declarator))
19575 if (initialization_kind == CPP_EQ)
19576 initializer = cp_parser_pure_specifier (parser);
19577 else
19579 /* If the declaration was erroneous, we don't really
19580 know what the user intended, so just silently
19581 consume the initializer. */
19582 if (decl != error_mark_node)
19583 error_at (tmp_init_loc, "initializer provided for function");
19584 cp_parser_skip_to_closing_parenthesis (parser,
19585 /*recovering=*/true,
19586 /*or_comma=*/false,
19587 /*consume_paren=*/true);
19590 else
19592 /* We want to record the extra mangling scope for in-class
19593 initializers of class members and initializers of static data
19594 member templates. The former involves deferring
19595 parsing of the initializer until end of class as with default
19596 arguments. So right here we only handle the latter. */
19597 if (!member_p && processing_template_decl)
19598 start_lambda_scope (decl);
19599 initializer = cp_parser_initializer (parser,
19600 &is_direct_init,
19601 &is_non_constant_init);
19602 if (!member_p && processing_template_decl)
19603 finish_lambda_scope ();
19604 if (initializer == error_mark_node)
19605 cp_parser_skip_to_end_of_statement (parser);
19609 /* The old parser allows attributes to appear after a parenthesized
19610 initializer. Mark Mitchell proposed removing this functionality
19611 on the GCC mailing lists on 2002-08-13. This parser accepts the
19612 attributes -- but ignores them. */
19613 if (cp_parser_allow_gnu_extensions_p (parser)
19614 && initialization_kind == CPP_OPEN_PAREN)
19615 if (cp_parser_attributes_opt (parser))
19616 warning (OPT_Wattributes,
19617 "attributes after parenthesized initializer ignored");
19619 /* And now complain about a non-function implicit template. */
19620 if (bogus_implicit_tmpl && decl != error_mark_node)
19621 error_at (DECL_SOURCE_LOCATION (decl),
19622 "non-function %qD declared as implicit template", decl);
19624 /* For an in-class declaration, use `grokfield' to create the
19625 declaration. */
19626 if (member_p)
19628 if (pushed_scope)
19630 pop_scope (pushed_scope);
19631 pushed_scope = NULL_TREE;
19633 decl = grokfield (declarator, decl_specifiers,
19634 initializer, !is_non_constant_init,
19635 /*asmspec=*/NULL_TREE,
19636 chainon (attributes, prefix_attributes));
19637 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19638 cp_parser_save_default_args (parser, decl);
19639 cp_finalize_omp_declare_simd (parser, decl);
19640 cp_finalize_oacc_routine (parser, decl, false);
19643 /* Finish processing the declaration. But, skip member
19644 declarations. */
19645 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19647 cp_finish_decl (decl,
19648 initializer, !is_non_constant_init,
19649 asm_specification,
19650 /* If the initializer is in parentheses, then this is
19651 a direct-initialization, which means that an
19652 `explicit' constructor is OK. Otherwise, an
19653 `explicit' constructor cannot be used. */
19654 ((is_direct_init || !is_initialized)
19655 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19657 else if ((cxx_dialect != cxx98) && friend_p
19658 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19659 /* Core issue #226 (C++0x only): A default template-argument
19660 shall not be specified in a friend class template
19661 declaration. */
19662 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19663 /*is_partial=*/false, /*is_friend_decl=*/1);
19665 if (!friend_p && pushed_scope)
19666 pop_scope (pushed_scope);
19668 if (function_declarator_p (declarator)
19669 && parser->fully_implicit_function_template_p)
19671 if (member_p)
19672 decl = finish_fully_implicit_template (parser, decl);
19673 else
19674 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19677 if (auto_result && is_initialized && decl_specifiers->type
19678 && type_uses_auto (decl_specifiers->type))
19679 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19681 return decl;
19684 /* Parse a declarator.
19686 declarator:
19687 direct-declarator
19688 ptr-operator declarator
19690 abstract-declarator:
19691 ptr-operator abstract-declarator [opt]
19692 direct-abstract-declarator
19694 GNU Extensions:
19696 declarator:
19697 attributes [opt] direct-declarator
19698 attributes [opt] ptr-operator declarator
19700 abstract-declarator:
19701 attributes [opt] ptr-operator abstract-declarator [opt]
19702 attributes [opt] direct-abstract-declarator
19704 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19705 detect constructors, destructors, deduction guides, or conversion operators.
19706 It is set to -1 if the declarator is a name, and +1 if it is a
19707 function. Otherwise it is set to zero. Usually you just want to
19708 test for >0, but internally the negative value is used.
19710 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19711 a decl-specifier-seq unless it declares a constructor, destructor,
19712 or conversion. It might seem that we could check this condition in
19713 semantic analysis, rather than parsing, but that makes it difficult
19714 to handle something like `f()'. We want to notice that there are
19715 no decl-specifiers, and therefore realize that this is an
19716 expression, not a declaration.)
19718 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19719 the declarator is a direct-declarator of the form "(...)".
19721 MEMBER_P is true iff this declarator is a member-declarator.
19723 FRIEND_P is true iff this declarator is a friend. */
19725 static cp_declarator *
19726 cp_parser_declarator (cp_parser* parser,
19727 cp_parser_declarator_kind dcl_kind,
19728 int* ctor_dtor_or_conv_p,
19729 bool* parenthesized_p,
19730 bool member_p, bool friend_p)
19732 cp_declarator *declarator;
19733 enum tree_code code;
19734 cp_cv_quals cv_quals;
19735 tree class_type;
19736 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19738 /* Assume this is not a constructor, destructor, or type-conversion
19739 operator. */
19740 if (ctor_dtor_or_conv_p)
19741 *ctor_dtor_or_conv_p = 0;
19743 if (cp_parser_allow_gnu_extensions_p (parser))
19744 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19746 /* Check for the ptr-operator production. */
19747 cp_parser_parse_tentatively (parser);
19748 /* Parse the ptr-operator. */
19749 code = cp_parser_ptr_operator (parser,
19750 &class_type,
19751 &cv_quals,
19752 &std_attributes);
19754 /* If that worked, then we have a ptr-operator. */
19755 if (cp_parser_parse_definitely (parser))
19757 /* If a ptr-operator was found, then this declarator was not
19758 parenthesized. */
19759 if (parenthesized_p)
19760 *parenthesized_p = true;
19761 /* The dependent declarator is optional if we are parsing an
19762 abstract-declarator. */
19763 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19764 cp_parser_parse_tentatively (parser);
19766 /* Parse the dependent declarator. */
19767 declarator = cp_parser_declarator (parser, dcl_kind,
19768 /*ctor_dtor_or_conv_p=*/NULL,
19769 /*parenthesized_p=*/NULL,
19770 /*member_p=*/false,
19771 friend_p);
19773 /* If we are parsing an abstract-declarator, we must handle the
19774 case where the dependent declarator is absent. */
19775 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19776 && !cp_parser_parse_definitely (parser))
19777 declarator = NULL;
19779 declarator = cp_parser_make_indirect_declarator
19780 (code, class_type, cv_quals, declarator, std_attributes);
19782 /* Everything else is a direct-declarator. */
19783 else
19785 if (parenthesized_p)
19786 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19787 CPP_OPEN_PAREN);
19788 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19789 ctor_dtor_or_conv_p,
19790 member_p, friend_p);
19793 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19794 declarator->attributes = gnu_attributes;
19795 return declarator;
19798 /* Parse a direct-declarator or direct-abstract-declarator.
19800 direct-declarator:
19801 declarator-id
19802 direct-declarator ( parameter-declaration-clause )
19803 cv-qualifier-seq [opt]
19804 ref-qualifier [opt]
19805 exception-specification [opt]
19806 direct-declarator [ constant-expression [opt] ]
19807 ( declarator )
19809 direct-abstract-declarator:
19810 direct-abstract-declarator [opt]
19811 ( parameter-declaration-clause )
19812 cv-qualifier-seq [opt]
19813 ref-qualifier [opt]
19814 exception-specification [opt]
19815 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19816 ( abstract-declarator )
19818 Returns a representation of the declarator. DCL_KIND is
19819 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19820 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19821 we are parsing a direct-declarator. It is
19822 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19823 of ambiguity we prefer an abstract declarator, as per
19824 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19825 as for cp_parser_declarator. */
19827 static cp_declarator *
19828 cp_parser_direct_declarator (cp_parser* parser,
19829 cp_parser_declarator_kind dcl_kind,
19830 int* ctor_dtor_or_conv_p,
19831 bool member_p, bool friend_p)
19833 cp_token *token;
19834 cp_declarator *declarator = NULL;
19835 tree scope = NULL_TREE;
19836 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19837 bool saved_in_declarator_p = parser->in_declarator_p;
19838 bool first = true;
19839 tree pushed_scope = NULL_TREE;
19840 cp_token *open_paren = NULL, *close_paren = NULL;
19842 while (true)
19844 /* Peek at the next token. */
19845 token = cp_lexer_peek_token (parser->lexer);
19846 if (token->type == CPP_OPEN_PAREN)
19848 /* This is either a parameter-declaration-clause, or a
19849 parenthesized declarator. When we know we are parsing a
19850 named declarator, it must be a parenthesized declarator
19851 if FIRST is true. For instance, `(int)' is a
19852 parameter-declaration-clause, with an omitted
19853 direct-abstract-declarator. But `((*))', is a
19854 parenthesized abstract declarator. Finally, when T is a
19855 template parameter `(T)' is a
19856 parameter-declaration-clause, and not a parenthesized
19857 named declarator.
19859 We first try and parse a parameter-declaration-clause,
19860 and then try a nested declarator (if FIRST is true).
19862 It is not an error for it not to be a
19863 parameter-declaration-clause, even when FIRST is
19864 false. Consider,
19866 int i (int);
19867 int i (3);
19869 The first is the declaration of a function while the
19870 second is the definition of a variable, including its
19871 initializer.
19873 Having seen only the parenthesis, we cannot know which of
19874 these two alternatives should be selected. Even more
19875 complex are examples like:
19877 int i (int (a));
19878 int i (int (3));
19880 The former is a function-declaration; the latter is a
19881 variable initialization.
19883 Thus again, we try a parameter-declaration-clause, and if
19884 that fails, we back out and return. */
19886 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19888 tree params;
19889 bool is_declarator = false;
19891 open_paren = NULL;
19893 /* In a member-declarator, the only valid interpretation
19894 of a parenthesis is the start of a
19895 parameter-declaration-clause. (It is invalid to
19896 initialize a static data member with a parenthesized
19897 initializer; only the "=" form of initialization is
19898 permitted.) */
19899 if (!member_p)
19900 cp_parser_parse_tentatively (parser);
19902 /* Consume the `('. */
19903 matching_parens parens;
19904 parens.consume_open (parser);
19905 if (first)
19907 /* If this is going to be an abstract declarator, we're
19908 in a declarator and we can't have default args. */
19909 parser->default_arg_ok_p = false;
19910 parser->in_declarator_p = true;
19913 begin_scope (sk_function_parms, NULL_TREE);
19915 /* Parse the parameter-declaration-clause. */
19916 params = cp_parser_parameter_declaration_clause (parser);
19918 /* Consume the `)'. */
19919 parens.require_close (parser);
19921 /* If all went well, parse the cv-qualifier-seq,
19922 ref-qualifier and the exception-specification. */
19923 if (member_p || cp_parser_parse_definitely (parser))
19925 cp_cv_quals cv_quals;
19926 cp_virt_specifiers virt_specifiers;
19927 cp_ref_qualifier ref_qual;
19928 tree exception_specification;
19929 tree late_return;
19930 tree attrs;
19931 bool memfn = (member_p || (pushed_scope
19932 && CLASS_TYPE_P (pushed_scope)));
19934 is_declarator = true;
19936 if (ctor_dtor_or_conv_p)
19937 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19938 first = false;
19940 /* Parse the cv-qualifier-seq. */
19941 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19942 /* Parse the ref-qualifier. */
19943 ref_qual = cp_parser_ref_qualifier_opt (parser);
19944 /* Parse the tx-qualifier. */
19945 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19946 /* And the exception-specification. */
19947 exception_specification
19948 = cp_parser_exception_specification_opt (parser);
19950 attrs = cp_parser_std_attribute_spec_seq (parser);
19952 /* In here, we handle cases where attribute is used after
19953 the function declaration. For example:
19954 void func (int x) __attribute__((vector(..))); */
19955 tree gnu_attrs = NULL_TREE;
19956 tree requires_clause = NULL_TREE;
19957 late_return = (cp_parser_late_return_type_opt
19958 (parser, declarator, requires_clause,
19959 memfn ? cv_quals : -1));
19961 /* Parse the virt-specifier-seq. */
19962 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19964 /* Create the function-declarator. */
19965 declarator = make_call_declarator (declarator,
19966 params,
19967 cv_quals,
19968 virt_specifiers,
19969 ref_qual,
19970 tx_qual,
19971 exception_specification,
19972 late_return,
19973 requires_clause);
19974 declarator->std_attributes = attrs;
19975 declarator->attributes = gnu_attrs;
19976 /* Any subsequent parameter lists are to do with
19977 return type, so are not those of the declared
19978 function. */
19979 parser->default_arg_ok_p = false;
19982 /* Remove the function parms from scope. */
19983 pop_bindings_and_leave_scope ();
19985 if (is_declarator)
19986 /* Repeat the main loop. */
19987 continue;
19990 /* If this is the first, we can try a parenthesized
19991 declarator. */
19992 if (first)
19994 bool saved_in_type_id_in_expr_p;
19996 parser->default_arg_ok_p = saved_default_arg_ok_p;
19997 parser->in_declarator_p = saved_in_declarator_p;
19999 open_paren = token;
20000 /* Consume the `('. */
20001 matching_parens parens;
20002 parens.consume_open (parser);
20003 /* Parse the nested declarator. */
20004 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20005 parser->in_type_id_in_expr_p = true;
20006 declarator
20007 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20008 /*parenthesized_p=*/NULL,
20009 member_p, friend_p);
20010 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20011 first = false;
20012 /* Expect a `)'. */
20013 close_paren = cp_lexer_peek_token (parser->lexer);
20014 if (!parens.require_close (parser))
20015 declarator = cp_error_declarator;
20016 if (declarator == cp_error_declarator)
20017 break;
20019 goto handle_declarator;
20021 /* Otherwise, we must be done. */
20022 else
20023 break;
20025 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20026 && token->type == CPP_OPEN_SQUARE
20027 && !cp_next_tokens_can_be_attribute_p (parser))
20029 /* Parse an array-declarator. */
20030 tree bounds, attrs;
20032 if (ctor_dtor_or_conv_p)
20033 *ctor_dtor_or_conv_p = 0;
20035 open_paren = NULL;
20036 first = false;
20037 parser->default_arg_ok_p = false;
20038 parser->in_declarator_p = true;
20039 /* Consume the `['. */
20040 cp_lexer_consume_token (parser->lexer);
20041 /* Peek at the next token. */
20042 token = cp_lexer_peek_token (parser->lexer);
20043 /* If the next token is `]', then there is no
20044 constant-expression. */
20045 if (token->type != CPP_CLOSE_SQUARE)
20047 bool non_constant_p;
20048 bounds
20049 = cp_parser_constant_expression (parser,
20050 /*allow_non_constant=*/true,
20051 &non_constant_p);
20052 if (!non_constant_p)
20053 /* OK */;
20054 else if (error_operand_p (bounds))
20055 /* Already gave an error. */;
20056 else if (!parser->in_function_body
20057 || current_binding_level->kind == sk_function_parms)
20059 /* Normally, the array bound must be an integral constant
20060 expression. However, as an extension, we allow VLAs
20061 in function scopes as long as they aren't part of a
20062 parameter declaration. */
20063 cp_parser_error (parser,
20064 "array bound is not an integer constant");
20065 bounds = error_mark_node;
20067 else if (processing_template_decl
20068 && !type_dependent_expression_p (bounds))
20070 /* Remember this wasn't a constant-expression. */
20071 bounds = build_nop (TREE_TYPE (bounds), bounds);
20072 TREE_SIDE_EFFECTS (bounds) = 1;
20075 else
20076 bounds = NULL_TREE;
20077 /* Look for the closing `]'. */
20078 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20080 declarator = cp_error_declarator;
20081 break;
20084 attrs = cp_parser_std_attribute_spec_seq (parser);
20085 declarator = make_array_declarator (declarator, bounds);
20086 declarator->std_attributes = attrs;
20088 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20091 tree qualifying_scope;
20092 tree unqualified_name;
20093 tree attrs;
20094 special_function_kind sfk;
20095 bool abstract_ok;
20096 bool pack_expansion_p = false;
20097 cp_token *declarator_id_start_token;
20099 /* Parse a declarator-id */
20100 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20101 if (abstract_ok)
20103 cp_parser_parse_tentatively (parser);
20105 /* If we see an ellipsis, we should be looking at a
20106 parameter pack. */
20107 if (token->type == CPP_ELLIPSIS)
20109 /* Consume the `...' */
20110 cp_lexer_consume_token (parser->lexer);
20112 pack_expansion_p = true;
20116 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20117 unqualified_name
20118 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20119 qualifying_scope = parser->scope;
20120 if (abstract_ok)
20122 bool okay = false;
20124 if (!unqualified_name && pack_expansion_p)
20126 /* Check whether an error occurred. */
20127 okay = !cp_parser_error_occurred (parser);
20129 /* We already consumed the ellipsis to mark a
20130 parameter pack, but we have no way to report it,
20131 so abort the tentative parse. We will be exiting
20132 immediately anyway. */
20133 cp_parser_abort_tentative_parse (parser);
20135 else
20136 okay = cp_parser_parse_definitely (parser);
20138 if (!okay)
20139 unqualified_name = error_mark_node;
20140 else if (unqualified_name
20141 && (qualifying_scope
20142 || (!identifier_p (unqualified_name))))
20144 cp_parser_error (parser, "expected unqualified-id");
20145 unqualified_name = error_mark_node;
20149 if (!unqualified_name)
20150 return NULL;
20151 if (unqualified_name == error_mark_node)
20153 declarator = cp_error_declarator;
20154 pack_expansion_p = false;
20155 declarator->parameter_pack_p = false;
20156 break;
20159 attrs = cp_parser_std_attribute_spec_seq (parser);
20161 if (qualifying_scope && at_namespace_scope_p ()
20162 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20164 /* In the declaration of a member of a template class
20165 outside of the class itself, the SCOPE will sometimes
20166 be a TYPENAME_TYPE. For example, given:
20168 template <typename T>
20169 int S<T>::R::i = 3;
20171 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20172 this context, we must resolve S<T>::R to an ordinary
20173 type, rather than a typename type.
20175 The reason we normally avoid resolving TYPENAME_TYPEs
20176 is that a specialization of `S' might render
20177 `S<T>::R' not a type. However, if `S' is
20178 specialized, then this `i' will not be used, so there
20179 is no harm in resolving the types here. */
20180 tree type;
20182 /* Resolve the TYPENAME_TYPE. */
20183 type = resolve_typename_type (qualifying_scope,
20184 /*only_current_p=*/false);
20185 /* If that failed, the declarator is invalid. */
20186 if (TREE_CODE (type) == TYPENAME_TYPE)
20188 if (typedef_variant_p (type))
20189 error_at (declarator_id_start_token->location,
20190 "cannot define member of dependent typedef "
20191 "%qT", type);
20192 else
20193 error_at (declarator_id_start_token->location,
20194 "%<%T::%E%> is not a type",
20195 TYPE_CONTEXT (qualifying_scope),
20196 TYPE_IDENTIFIER (qualifying_scope));
20198 qualifying_scope = type;
20201 sfk = sfk_none;
20203 if (unqualified_name)
20205 tree class_type;
20207 if (qualifying_scope
20208 && CLASS_TYPE_P (qualifying_scope))
20209 class_type = qualifying_scope;
20210 else
20211 class_type = current_class_type;
20213 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20215 tree name_type = TREE_TYPE (unqualified_name);
20217 if (!class_type || !same_type_p (name_type, class_type))
20219 /* We do not attempt to print the declarator
20220 here because we do not have enough
20221 information about its original syntactic
20222 form. */
20223 cp_parser_error (parser, "invalid declarator");
20224 declarator = cp_error_declarator;
20225 break;
20227 else if (qualifying_scope
20228 && CLASSTYPE_USE_TEMPLATE (name_type))
20230 error_at (declarator_id_start_token->location,
20231 "invalid use of constructor as a template");
20232 inform (declarator_id_start_token->location,
20233 "use %<%T::%D%> instead of %<%T::%D%> to "
20234 "name the constructor in a qualified name",
20235 class_type,
20236 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20237 class_type, name_type);
20238 declarator = cp_error_declarator;
20239 break;
20241 unqualified_name = constructor_name (class_type);
20244 if (class_type)
20246 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20247 sfk = sfk_destructor;
20248 else if (identifier_p (unqualified_name)
20249 && IDENTIFIER_CONV_OP_P (unqualified_name))
20250 sfk = sfk_conversion;
20251 else if (/* There's no way to declare a constructor
20252 for an unnamed type, even if the type
20253 got a name for linkage purposes. */
20254 !TYPE_WAS_UNNAMED (class_type)
20255 /* Handle correctly (c++/19200):
20257 struct S {
20258 struct T{};
20259 friend void S(T);
20262 and also:
20264 namespace N {
20265 void S();
20268 struct S {
20269 friend void N::S();
20270 }; */
20271 && (!friend_p || class_type == qualifying_scope)
20272 && constructor_name_p (unqualified_name,
20273 class_type))
20274 sfk = sfk_constructor;
20275 else if (is_overloaded_fn (unqualified_name)
20276 && DECL_CONSTRUCTOR_P (get_first_fn
20277 (unqualified_name)))
20278 sfk = sfk_constructor;
20280 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20281 *ctor_dtor_or_conv_p = -1;
20284 declarator = make_id_declarator (qualifying_scope,
20285 unqualified_name,
20286 sfk);
20287 declarator->std_attributes = attrs;
20288 declarator->id_loc = token->location;
20289 declarator->parameter_pack_p = pack_expansion_p;
20291 if (pack_expansion_p)
20292 maybe_warn_variadic_templates ();
20295 handle_declarator:;
20296 scope = get_scope_of_declarator (declarator);
20297 if (scope)
20299 /* Any names that appear after the declarator-id for a
20300 member are looked up in the containing scope. */
20301 if (at_function_scope_p ())
20303 /* But declarations with qualified-ids can't appear in a
20304 function. */
20305 cp_parser_error (parser, "qualified-id in declaration");
20306 declarator = cp_error_declarator;
20307 break;
20309 pushed_scope = push_scope (scope);
20311 parser->in_declarator_p = true;
20312 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20313 || (declarator && declarator->kind == cdk_id))
20314 /* Default args are only allowed on function
20315 declarations. */
20316 parser->default_arg_ok_p = saved_default_arg_ok_p;
20317 else
20318 parser->default_arg_ok_p = false;
20320 first = false;
20322 /* We're done. */
20323 else
20324 break;
20327 /* For an abstract declarator, we might wind up with nothing at this
20328 point. That's an error; the declarator is not optional. */
20329 if (!declarator)
20330 cp_parser_error (parser, "expected declarator");
20331 else if (open_paren)
20333 /* Record overly parenthesized declarator so we can give a
20334 diagnostic about confusing decl/expr disambiguation. */
20335 if (declarator->kind == cdk_array)
20337 /* If the open and close parens are on different lines, this
20338 is probably a formatting thing, so ignore. */
20339 expanded_location open = expand_location (open_paren->location);
20340 expanded_location close = expand_location (close_paren->location);
20341 if (open.line != close.line || open.file != close.file)
20342 open_paren = NULL;
20344 if (open_paren)
20345 declarator->parenthesized = open_paren->location;
20348 /* If we entered a scope, we must exit it now. */
20349 if (pushed_scope)
20350 pop_scope (pushed_scope);
20352 parser->default_arg_ok_p = saved_default_arg_ok_p;
20353 parser->in_declarator_p = saved_in_declarator_p;
20355 return declarator;
20358 /* Parse a ptr-operator.
20360 ptr-operator:
20361 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20362 * cv-qualifier-seq [opt]
20364 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20365 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20367 GNU Extension:
20369 ptr-operator:
20370 & cv-qualifier-seq [opt]
20372 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20373 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20374 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20375 filled in with the TYPE containing the member. *CV_QUALS is
20376 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20377 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20378 Note that the tree codes returned by this function have nothing
20379 to do with the types of trees that will be eventually be created
20380 to represent the pointer or reference type being parsed. They are
20381 just constants with suggestive names. */
20382 static enum tree_code
20383 cp_parser_ptr_operator (cp_parser* parser,
20384 tree* type,
20385 cp_cv_quals *cv_quals,
20386 tree *attributes)
20388 enum tree_code code = ERROR_MARK;
20389 cp_token *token;
20390 tree attrs = NULL_TREE;
20392 /* Assume that it's not a pointer-to-member. */
20393 *type = NULL_TREE;
20394 /* And that there are no cv-qualifiers. */
20395 *cv_quals = TYPE_UNQUALIFIED;
20397 /* Peek at the next token. */
20398 token = cp_lexer_peek_token (parser->lexer);
20400 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20401 if (token->type == CPP_MULT)
20402 code = INDIRECT_REF;
20403 else if (token->type == CPP_AND)
20404 code = ADDR_EXPR;
20405 else if ((cxx_dialect != cxx98) &&
20406 token->type == CPP_AND_AND) /* C++0x only */
20407 code = NON_LVALUE_EXPR;
20409 if (code != ERROR_MARK)
20411 /* Consume the `*', `&' or `&&'. */
20412 cp_lexer_consume_token (parser->lexer);
20414 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20415 `&', if we are allowing GNU extensions. (The only qualifier
20416 that can legally appear after `&' is `restrict', but that is
20417 enforced during semantic analysis. */
20418 if (code == INDIRECT_REF
20419 || cp_parser_allow_gnu_extensions_p (parser))
20420 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20422 attrs = cp_parser_std_attribute_spec_seq (parser);
20423 if (attributes != NULL)
20424 *attributes = attrs;
20426 else
20428 /* Try the pointer-to-member case. */
20429 cp_parser_parse_tentatively (parser);
20430 /* Look for the optional `::' operator. */
20431 cp_parser_global_scope_opt (parser,
20432 /*current_scope_valid_p=*/false);
20433 /* Look for the nested-name specifier. */
20434 token = cp_lexer_peek_token (parser->lexer);
20435 cp_parser_nested_name_specifier (parser,
20436 /*typename_keyword_p=*/false,
20437 /*check_dependency_p=*/true,
20438 /*type_p=*/false,
20439 /*is_declaration=*/false);
20440 /* If we found it, and the next token is a `*', then we are
20441 indeed looking at a pointer-to-member operator. */
20442 if (!cp_parser_error_occurred (parser)
20443 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20445 /* Indicate that the `*' operator was used. */
20446 code = INDIRECT_REF;
20448 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20449 error_at (token->location, "%qD is a namespace", parser->scope);
20450 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20451 error_at (token->location, "cannot form pointer to member of "
20452 "non-class %q#T", parser->scope);
20453 else
20455 /* The type of which the member is a member is given by the
20456 current SCOPE. */
20457 *type = parser->scope;
20458 /* The next name will not be qualified. */
20459 parser->scope = NULL_TREE;
20460 parser->qualifying_scope = NULL_TREE;
20461 parser->object_scope = NULL_TREE;
20462 /* Look for optional c++11 attributes. */
20463 attrs = cp_parser_std_attribute_spec_seq (parser);
20464 if (attributes != NULL)
20465 *attributes = attrs;
20466 /* Look for the optional cv-qualifier-seq. */
20467 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20470 /* If that didn't work we don't have a ptr-operator. */
20471 if (!cp_parser_parse_definitely (parser))
20472 cp_parser_error (parser, "expected ptr-operator");
20475 return code;
20478 /* Parse an (optional) cv-qualifier-seq.
20480 cv-qualifier-seq:
20481 cv-qualifier cv-qualifier-seq [opt]
20483 cv-qualifier:
20484 const
20485 volatile
20487 GNU Extension:
20489 cv-qualifier:
20490 __restrict__
20492 Returns a bitmask representing the cv-qualifiers. */
20494 static cp_cv_quals
20495 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20497 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20499 while (true)
20501 cp_token *token;
20502 cp_cv_quals cv_qualifier;
20504 /* Peek at the next token. */
20505 token = cp_lexer_peek_token (parser->lexer);
20506 /* See if it's a cv-qualifier. */
20507 switch (token->keyword)
20509 case RID_CONST:
20510 cv_qualifier = TYPE_QUAL_CONST;
20511 break;
20513 case RID_VOLATILE:
20514 cv_qualifier = TYPE_QUAL_VOLATILE;
20515 break;
20517 case RID_RESTRICT:
20518 cv_qualifier = TYPE_QUAL_RESTRICT;
20519 break;
20521 default:
20522 cv_qualifier = TYPE_UNQUALIFIED;
20523 break;
20526 if (!cv_qualifier)
20527 break;
20529 if (cv_quals & cv_qualifier)
20531 gcc_rich_location richloc (token->location);
20532 richloc.add_fixit_remove ();
20533 error_at (&richloc, "duplicate cv-qualifier");
20534 cp_lexer_purge_token (parser->lexer);
20536 else
20538 cp_lexer_consume_token (parser->lexer);
20539 cv_quals |= cv_qualifier;
20543 return cv_quals;
20546 /* Parse an (optional) ref-qualifier
20548 ref-qualifier:
20552 Returns cp_ref_qualifier representing ref-qualifier. */
20554 static cp_ref_qualifier
20555 cp_parser_ref_qualifier_opt (cp_parser* parser)
20557 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20559 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20560 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20561 return ref_qual;
20563 while (true)
20565 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20566 cp_token *token = cp_lexer_peek_token (parser->lexer);
20568 switch (token->type)
20570 case CPP_AND:
20571 curr_ref_qual = REF_QUAL_LVALUE;
20572 break;
20574 case CPP_AND_AND:
20575 curr_ref_qual = REF_QUAL_RVALUE;
20576 break;
20578 default:
20579 curr_ref_qual = REF_QUAL_NONE;
20580 break;
20583 if (!curr_ref_qual)
20584 break;
20585 else if (ref_qual)
20587 error_at (token->location, "multiple ref-qualifiers");
20588 cp_lexer_purge_token (parser->lexer);
20590 else
20592 ref_qual = curr_ref_qual;
20593 cp_lexer_consume_token (parser->lexer);
20597 return ref_qual;
20600 /* Parse an optional tx-qualifier.
20602 tx-qualifier:
20603 transaction_safe
20604 transaction_safe_dynamic */
20606 static tree
20607 cp_parser_tx_qualifier_opt (cp_parser *parser)
20609 cp_token *token = cp_lexer_peek_token (parser->lexer);
20610 if (token->type == CPP_NAME)
20612 tree name = token->u.value;
20613 const char *p = IDENTIFIER_POINTER (name);
20614 const int len = strlen ("transaction_safe");
20615 if (!strncmp (p, "transaction_safe", len))
20617 p += len;
20618 if (*p == '\0'
20619 || !strcmp (p, "_dynamic"))
20621 cp_lexer_consume_token (parser->lexer);
20622 if (!flag_tm)
20624 error ("%qE requires %<-fgnu-tm%>", name);
20625 return NULL_TREE;
20627 else
20628 return name;
20632 return NULL_TREE;
20635 /* Parse an (optional) virt-specifier-seq.
20637 virt-specifier-seq:
20638 virt-specifier virt-specifier-seq [opt]
20640 virt-specifier:
20641 override
20642 final
20644 Returns a bitmask representing the virt-specifiers. */
20646 static cp_virt_specifiers
20647 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20649 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20651 while (true)
20653 cp_token *token;
20654 cp_virt_specifiers virt_specifier;
20656 /* Peek at the next token. */
20657 token = cp_lexer_peek_token (parser->lexer);
20658 /* See if it's a virt-specifier-qualifier. */
20659 if (token->type != CPP_NAME)
20660 break;
20661 if (id_equal (token->u.value, "override"))
20663 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20664 virt_specifier = VIRT_SPEC_OVERRIDE;
20666 else if (id_equal (token->u.value, "final"))
20668 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20669 virt_specifier = VIRT_SPEC_FINAL;
20671 else if (id_equal (token->u.value, "__final"))
20673 virt_specifier = VIRT_SPEC_FINAL;
20675 else
20676 break;
20678 if (virt_specifiers & virt_specifier)
20680 gcc_rich_location richloc (token->location);
20681 richloc.add_fixit_remove ();
20682 error_at (&richloc, "duplicate virt-specifier");
20683 cp_lexer_purge_token (parser->lexer);
20685 else
20687 cp_lexer_consume_token (parser->lexer);
20688 virt_specifiers |= virt_specifier;
20691 return virt_specifiers;
20694 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20695 is in scope even though it isn't real. */
20697 void
20698 inject_this_parameter (tree ctype, cp_cv_quals quals)
20700 tree this_parm;
20702 if (current_class_ptr)
20704 /* We don't clear this between NSDMIs. Is it already what we want? */
20705 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20706 if (DECL_P (current_class_ptr)
20707 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20708 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20709 && cp_type_quals (type) == quals)
20710 return;
20713 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20715 current_class_ptr = NULL_TREE;
20716 current_class_ref
20717 = cp_build_fold_indirect_ref (this_parm);
20718 current_class_ptr = this_parm;
20721 /* Return true iff our current scope is a non-static data member
20722 initializer. */
20724 bool
20725 parsing_nsdmi (void)
20727 /* We recognize NSDMI context by the context-less 'this' pointer set up
20728 by the function above. */
20729 if (current_class_ptr
20730 && TREE_CODE (current_class_ptr) == PARM_DECL
20731 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20732 return true;
20733 return false;
20736 /* Parse a late-specified return type, if any. This is not a separate
20737 non-terminal, but part of a function declarator, which looks like
20739 -> trailing-type-specifier-seq abstract-declarator(opt)
20741 Returns the type indicated by the type-id.
20743 In addition to this, parse any queued up #pragma omp declare simd
20744 clauses, and #pragma acc routine clauses.
20746 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20747 function. */
20749 static tree
20750 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20751 tree& requires_clause, cp_cv_quals quals)
20753 cp_token *token;
20754 tree type = NULL_TREE;
20755 bool declare_simd_p = (parser->omp_declare_simd
20756 && declarator
20757 && declarator->kind == cdk_id);
20759 bool oacc_routine_p = (parser->oacc_routine
20760 && declarator
20761 && declarator->kind == cdk_id);
20763 /* Peek at the next token. */
20764 token = cp_lexer_peek_token (parser->lexer);
20765 /* A late-specified return type is indicated by an initial '->'. */
20766 if (token->type != CPP_DEREF
20767 && token->keyword != RID_REQUIRES
20768 && !(token->type == CPP_NAME
20769 && token->u.value == ridpointers[RID_REQUIRES])
20770 && !(declare_simd_p || oacc_routine_p))
20771 return NULL_TREE;
20773 tree save_ccp = current_class_ptr;
20774 tree save_ccr = current_class_ref;
20775 if (quals >= 0)
20777 /* DR 1207: 'this' is in scope in the trailing return type. */
20778 inject_this_parameter (current_class_type, quals);
20781 if (token->type == CPP_DEREF)
20783 /* Consume the ->. */
20784 cp_lexer_consume_token (parser->lexer);
20786 type = cp_parser_trailing_type_id (parser);
20789 /* Function declarations may be followed by a trailing
20790 requires-clause. */
20791 requires_clause = cp_parser_requires_clause_opt (parser);
20793 if (declare_simd_p)
20794 declarator->attributes
20795 = cp_parser_late_parsing_omp_declare_simd (parser,
20796 declarator->attributes);
20797 if (oacc_routine_p)
20798 declarator->attributes
20799 = cp_parser_late_parsing_oacc_routine (parser,
20800 declarator->attributes);
20802 if (quals >= 0)
20804 current_class_ptr = save_ccp;
20805 current_class_ref = save_ccr;
20808 return type;
20811 /* Parse a declarator-id.
20813 declarator-id:
20814 id-expression
20815 :: [opt] nested-name-specifier [opt] type-name
20817 In the `id-expression' case, the value returned is as for
20818 cp_parser_id_expression if the id-expression was an unqualified-id.
20819 If the id-expression was a qualified-id, then a SCOPE_REF is
20820 returned. The first operand is the scope (either a NAMESPACE_DECL
20821 or TREE_TYPE), but the second is still just a representation of an
20822 unqualified-id. */
20824 static tree
20825 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20827 tree id;
20828 /* The expression must be an id-expression. Assume that qualified
20829 names are the names of types so that:
20831 template <class T>
20832 int S<T>::R::i = 3;
20834 will work; we must treat `S<T>::R' as the name of a type.
20835 Similarly, assume that qualified names are templates, where
20836 required, so that:
20838 template <class T>
20839 int S<T>::R<T>::i = 3;
20841 will work, too. */
20842 id = cp_parser_id_expression (parser,
20843 /*template_keyword_p=*/false,
20844 /*check_dependency_p=*/false,
20845 /*template_p=*/NULL,
20846 /*declarator_p=*/true,
20847 optional_p);
20848 if (id && BASELINK_P (id))
20849 id = BASELINK_FUNCTIONS (id);
20850 return id;
20853 /* Parse a type-id.
20855 type-id:
20856 type-specifier-seq abstract-declarator [opt]
20858 Returns the TYPE specified. */
20860 static tree
20861 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20862 bool is_trailing_return)
20864 cp_decl_specifier_seq type_specifier_seq;
20865 cp_declarator *abstract_declarator;
20867 /* Parse the type-specifier-seq. */
20868 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20869 is_trailing_return,
20870 &type_specifier_seq);
20871 if (is_template_arg && type_specifier_seq.type
20872 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20873 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20874 /* A bare template name as a template argument is a template template
20875 argument, not a placeholder, so fail parsing it as a type argument. */
20877 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20878 cp_parser_simulate_error (parser);
20879 return error_mark_node;
20881 if (type_specifier_seq.type == error_mark_node)
20882 return error_mark_node;
20884 /* There might or might not be an abstract declarator. */
20885 cp_parser_parse_tentatively (parser);
20886 /* Look for the declarator. */
20887 abstract_declarator
20888 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20889 /*parenthesized_p=*/NULL,
20890 /*member_p=*/false,
20891 /*friend_p=*/false);
20892 /* Check to see if there really was a declarator. */
20893 if (!cp_parser_parse_definitely (parser))
20894 abstract_declarator = NULL;
20896 if (type_specifier_seq.type
20897 /* The concepts TS allows 'auto' as a type-id. */
20898 && (!flag_concepts || parser->in_type_id_in_expr_p)
20899 /* None of the valid uses of 'auto' in C++14 involve the type-id
20900 nonterminal, but it is valid in a trailing-return-type. */
20901 && !(cxx_dialect >= cxx14 && is_trailing_return))
20902 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20904 /* A type-id with type 'auto' is only ok if the abstract declarator
20905 is a function declarator with a late-specified return type.
20907 A type-id with 'auto' is also valid in a trailing-return-type
20908 in a compound-requirement. */
20909 if (abstract_declarator
20910 && abstract_declarator->kind == cdk_function
20911 && abstract_declarator->u.function.late_return_type)
20912 /* OK */;
20913 else if (parser->in_result_type_constraint_p)
20914 /* OK */;
20915 else
20917 location_t loc = type_specifier_seq.locations[ds_type_spec];
20918 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20920 error_at (loc, "missing template arguments after %qT",
20921 auto_node);
20922 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20923 tmpl);
20925 else
20926 error_at (loc, "invalid use of %qT", auto_node);
20927 return error_mark_node;
20931 return groktypename (&type_specifier_seq, abstract_declarator,
20932 is_template_arg);
20935 static tree
20936 cp_parser_type_id (cp_parser *parser)
20938 return cp_parser_type_id_1 (parser, false, false);
20941 static tree
20942 cp_parser_template_type_arg (cp_parser *parser)
20944 tree r;
20945 const char *saved_message = parser->type_definition_forbidden_message;
20946 parser->type_definition_forbidden_message
20947 = G_("types may not be defined in template arguments");
20948 r = cp_parser_type_id_1 (parser, true, false);
20949 parser->type_definition_forbidden_message = saved_message;
20950 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20952 error ("invalid use of %<auto%> in template argument");
20953 r = error_mark_node;
20955 return r;
20958 static tree
20959 cp_parser_trailing_type_id (cp_parser *parser)
20961 return cp_parser_type_id_1 (parser, false, true);
20964 /* Parse a type-specifier-seq.
20966 type-specifier-seq:
20967 type-specifier type-specifier-seq [opt]
20969 GNU extension:
20971 type-specifier-seq:
20972 attributes type-specifier-seq [opt]
20974 If IS_DECLARATION is true, we are at the start of a "condition" or
20975 exception-declaration, so we might be followed by a declarator-id.
20977 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20978 i.e. we've just seen "->".
20980 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20982 static void
20983 cp_parser_type_specifier_seq (cp_parser* parser,
20984 bool is_declaration,
20985 bool is_trailing_return,
20986 cp_decl_specifier_seq *type_specifier_seq)
20988 bool seen_type_specifier = false;
20989 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20990 cp_token *start_token = NULL;
20992 /* Clear the TYPE_SPECIFIER_SEQ. */
20993 clear_decl_specs (type_specifier_seq);
20995 /* In the context of a trailing return type, enum E { } is an
20996 elaborated-type-specifier followed by a function-body, not an
20997 enum-specifier. */
20998 if (is_trailing_return)
20999 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21001 /* Parse the type-specifiers and attributes. */
21002 while (true)
21004 tree type_specifier;
21005 bool is_cv_qualifier;
21007 /* Check for attributes first. */
21008 if (cp_next_tokens_can_be_attribute_p (parser))
21010 type_specifier_seq->attributes =
21011 chainon (type_specifier_seq->attributes,
21012 cp_parser_attributes_opt (parser));
21013 continue;
21016 /* record the token of the beginning of the type specifier seq,
21017 for error reporting purposes*/
21018 if (!start_token)
21019 start_token = cp_lexer_peek_token (parser->lexer);
21021 /* Look for the type-specifier. */
21022 type_specifier = cp_parser_type_specifier (parser,
21023 flags,
21024 type_specifier_seq,
21025 /*is_declaration=*/false,
21026 NULL,
21027 &is_cv_qualifier);
21028 if (!type_specifier)
21030 /* If the first type-specifier could not be found, this is not a
21031 type-specifier-seq at all. */
21032 if (!seen_type_specifier)
21034 /* Set in_declarator_p to avoid skipping to the semicolon. */
21035 int in_decl = parser->in_declarator_p;
21036 parser->in_declarator_p = true;
21038 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21039 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21040 cp_parser_error (parser, "expected type-specifier");
21042 parser->in_declarator_p = in_decl;
21044 type_specifier_seq->type = error_mark_node;
21045 return;
21047 /* If subsequent type-specifiers could not be found, the
21048 type-specifier-seq is complete. */
21049 break;
21052 seen_type_specifier = true;
21053 /* The standard says that a condition can be:
21055 type-specifier-seq declarator = assignment-expression
21057 However, given:
21059 struct S {};
21060 if (int S = ...)
21062 we should treat the "S" as a declarator, not as a
21063 type-specifier. The standard doesn't say that explicitly for
21064 type-specifier-seq, but it does say that for
21065 decl-specifier-seq in an ordinary declaration. Perhaps it
21066 would be clearer just to allow a decl-specifier-seq here, and
21067 then add a semantic restriction that if any decl-specifiers
21068 that are not type-specifiers appear, the program is invalid. */
21069 if (is_declaration && !is_cv_qualifier)
21070 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21074 /* Return whether the function currently being declared has an associated
21075 template parameter list. */
21077 static bool
21078 function_being_declared_is_template_p (cp_parser* parser)
21080 if (!current_template_parms || processing_template_parmlist)
21081 return false;
21083 if (parser->implicit_template_scope)
21084 return true;
21086 if (at_class_scope_p ()
21087 && TYPE_BEING_DEFINED (current_class_type))
21088 return parser->num_template_parameter_lists != 0;
21090 return ((int) parser->num_template_parameter_lists > template_class_depth
21091 (current_class_type));
21094 /* Parse a parameter-declaration-clause.
21096 parameter-declaration-clause:
21097 parameter-declaration-list [opt] ... [opt]
21098 parameter-declaration-list , ...
21100 Returns a representation for the parameter declarations. A return
21101 value of NULL indicates a parameter-declaration-clause consisting
21102 only of an ellipsis. */
21104 static tree
21105 cp_parser_parameter_declaration_clause (cp_parser* parser)
21107 tree parameters;
21108 cp_token *token;
21109 bool ellipsis_p;
21110 bool is_error;
21112 struct cleanup {
21113 cp_parser* parser;
21114 int auto_is_implicit_function_template_parm_p;
21115 ~cleanup() {
21116 parser->auto_is_implicit_function_template_parm_p
21117 = auto_is_implicit_function_template_parm_p;
21119 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21121 (void) cleanup;
21123 if (!processing_specialization
21124 && !processing_template_parmlist
21125 && !processing_explicit_instantiation)
21126 if (!current_function_decl
21127 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21128 parser->auto_is_implicit_function_template_parm_p = true;
21130 /* Peek at the next token. */
21131 token = cp_lexer_peek_token (parser->lexer);
21132 /* Check for trivial parameter-declaration-clauses. */
21133 if (token->type == CPP_ELLIPSIS)
21135 /* Consume the `...' token. */
21136 cp_lexer_consume_token (parser->lexer);
21137 return NULL_TREE;
21139 else if (token->type == CPP_CLOSE_PAREN)
21140 /* There are no parameters. */
21142 #ifndef NO_IMPLICIT_EXTERN_C
21143 if (in_system_header_at (input_location)
21144 && current_class_type == NULL
21145 && current_lang_name == lang_name_c)
21146 return NULL_TREE;
21147 else
21148 #endif
21149 return void_list_node;
21151 /* Check for `(void)', too, which is a special case. */
21152 else if (token->keyword == RID_VOID
21153 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21154 == CPP_CLOSE_PAREN))
21156 /* Consume the `void' token. */
21157 cp_lexer_consume_token (parser->lexer);
21158 /* There are no parameters. */
21159 return void_list_node;
21162 /* Parse the parameter-declaration-list. */
21163 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21164 /* If a parse error occurred while parsing the
21165 parameter-declaration-list, then the entire
21166 parameter-declaration-clause is erroneous. */
21167 if (is_error)
21168 return NULL;
21170 /* Peek at the next token. */
21171 token = cp_lexer_peek_token (parser->lexer);
21172 /* If it's a `,', the clause should terminate with an ellipsis. */
21173 if (token->type == CPP_COMMA)
21175 /* Consume the `,'. */
21176 cp_lexer_consume_token (parser->lexer);
21177 /* Expect an ellipsis. */
21178 ellipsis_p
21179 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21181 /* It might also be `...' if the optional trailing `,' was
21182 omitted. */
21183 else if (token->type == CPP_ELLIPSIS)
21185 /* Consume the `...' token. */
21186 cp_lexer_consume_token (parser->lexer);
21187 /* And remember that we saw it. */
21188 ellipsis_p = true;
21190 else
21191 ellipsis_p = false;
21193 /* Finish the parameter list. */
21194 if (!ellipsis_p)
21195 parameters = chainon (parameters, void_list_node);
21197 return parameters;
21200 /* Parse a parameter-declaration-list.
21202 parameter-declaration-list:
21203 parameter-declaration
21204 parameter-declaration-list , parameter-declaration
21206 Returns a representation of the parameter-declaration-list, as for
21207 cp_parser_parameter_declaration_clause. However, the
21208 `void_list_node' is never appended to the list. Upon return,
21209 *IS_ERROR will be true iff an error occurred. */
21211 static tree
21212 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21214 tree parameters = NULL_TREE;
21215 tree *tail = &parameters;
21216 bool saved_in_unbraced_linkage_specification_p;
21217 int index = 0;
21219 /* Assume all will go well. */
21220 *is_error = false;
21221 /* The special considerations that apply to a function within an
21222 unbraced linkage specifications do not apply to the parameters
21223 to the function. */
21224 saved_in_unbraced_linkage_specification_p
21225 = parser->in_unbraced_linkage_specification_p;
21226 parser->in_unbraced_linkage_specification_p = false;
21228 /* Look for more parameters. */
21229 while (true)
21231 cp_parameter_declarator *parameter;
21232 tree decl = error_mark_node;
21233 bool parenthesized_p = false;
21234 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21235 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21236 (current_template_parms)) : 0);
21238 /* Parse the parameter. */
21239 parameter
21240 = cp_parser_parameter_declaration (parser,
21241 /*template_parm_p=*/false,
21242 &parenthesized_p);
21244 /* We don't know yet if the enclosing context is deprecated, so wait
21245 and warn in grokparms if appropriate. */
21246 deprecated_state = DEPRECATED_SUPPRESS;
21248 if (parameter)
21250 /* If a function parameter pack was specified and an implicit template
21251 parameter was introduced during cp_parser_parameter_declaration,
21252 change any implicit parameters introduced into packs. */
21253 if (parser->implicit_template_parms
21254 && parameter->declarator
21255 && parameter->declarator->parameter_pack_p)
21257 int latest_template_parm_idx = TREE_VEC_LENGTH
21258 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21260 if (latest_template_parm_idx != template_parm_idx)
21261 parameter->decl_specifiers.type = convert_generic_types_to_packs
21262 (parameter->decl_specifiers.type,
21263 template_parm_idx, latest_template_parm_idx);
21266 decl = grokdeclarator (parameter->declarator,
21267 &parameter->decl_specifiers,
21268 PARM,
21269 parameter->default_argument != NULL_TREE,
21270 &parameter->decl_specifiers.attributes);
21271 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21272 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21275 deprecated_state = DEPRECATED_NORMAL;
21277 /* If a parse error occurred parsing the parameter declaration,
21278 then the entire parameter-declaration-list is erroneous. */
21279 if (decl == error_mark_node)
21281 *is_error = true;
21282 parameters = error_mark_node;
21283 break;
21286 if (parameter->decl_specifiers.attributes)
21287 cplus_decl_attributes (&decl,
21288 parameter->decl_specifiers.attributes,
21290 if (DECL_NAME (decl))
21291 decl = pushdecl (decl);
21293 if (decl != error_mark_node)
21295 retrofit_lang_decl (decl);
21296 DECL_PARM_INDEX (decl) = ++index;
21297 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21300 /* Add the new parameter to the list. */
21301 *tail = build_tree_list (parameter->default_argument, decl);
21302 tail = &TREE_CHAIN (*tail);
21304 /* Peek at the next token. */
21305 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21306 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21307 /* These are for Objective-C++ */
21308 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21309 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21310 /* The parameter-declaration-list is complete. */
21311 break;
21312 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21314 cp_token *token;
21316 /* Peek at the next token. */
21317 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21318 /* If it's an ellipsis, then the list is complete. */
21319 if (token->type == CPP_ELLIPSIS)
21320 break;
21321 /* Otherwise, there must be more parameters. Consume the
21322 `,'. */
21323 cp_lexer_consume_token (parser->lexer);
21324 /* When parsing something like:
21326 int i(float f, double d)
21328 we can tell after seeing the declaration for "f" that we
21329 are not looking at an initialization of a variable "i",
21330 but rather at the declaration of a function "i".
21332 Due to the fact that the parsing of template arguments
21333 (as specified to a template-id) requires backtracking we
21334 cannot use this technique when inside a template argument
21335 list. */
21336 if (!parser->in_template_argument_list_p
21337 && !parser->in_type_id_in_expr_p
21338 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21339 /* However, a parameter-declaration of the form
21340 "float(f)" (which is a valid declaration of a
21341 parameter "f") can also be interpreted as an
21342 expression (the conversion of "f" to "float"). */
21343 && !parenthesized_p)
21344 cp_parser_commit_to_tentative_parse (parser);
21346 else
21348 cp_parser_error (parser, "expected %<,%> or %<...%>");
21349 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21350 cp_parser_skip_to_closing_parenthesis (parser,
21351 /*recovering=*/true,
21352 /*or_comma=*/false,
21353 /*consume_paren=*/false);
21354 break;
21358 parser->in_unbraced_linkage_specification_p
21359 = saved_in_unbraced_linkage_specification_p;
21361 /* Reset implicit_template_scope if we are about to leave the function
21362 parameter list that introduced it. Note that for out-of-line member
21363 definitions, there will be one or more class scopes before we get to
21364 the template parameter scope. */
21366 if (cp_binding_level *its = parser->implicit_template_scope)
21367 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21369 while (maybe_its->kind == sk_class)
21370 maybe_its = maybe_its->level_chain;
21371 if (maybe_its == its)
21373 parser->implicit_template_parms = 0;
21374 parser->implicit_template_scope = 0;
21378 return parameters;
21381 /* Parse a parameter declaration.
21383 parameter-declaration:
21384 decl-specifier-seq ... [opt] declarator
21385 decl-specifier-seq declarator = assignment-expression
21386 decl-specifier-seq ... [opt] abstract-declarator [opt]
21387 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21389 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21390 declares a template parameter. (In that case, a non-nested `>'
21391 token encountered during the parsing of the assignment-expression
21392 is not interpreted as a greater-than operator.)
21394 Returns a representation of the parameter, or NULL if an error
21395 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21396 true iff the declarator is of the form "(p)". */
21398 static cp_parameter_declarator *
21399 cp_parser_parameter_declaration (cp_parser *parser,
21400 bool template_parm_p,
21401 bool *parenthesized_p)
21403 int declares_class_or_enum;
21404 cp_decl_specifier_seq decl_specifiers;
21405 cp_declarator *declarator;
21406 tree default_argument;
21407 cp_token *token = NULL, *declarator_token_start = NULL;
21408 const char *saved_message;
21409 bool template_parameter_pack_p = false;
21411 /* In a template parameter, `>' is not an operator.
21413 [temp.param]
21415 When parsing a default template-argument for a non-type
21416 template-parameter, the first non-nested `>' is taken as the end
21417 of the template parameter-list rather than a greater-than
21418 operator. */
21420 /* Type definitions may not appear in parameter types. */
21421 saved_message = parser->type_definition_forbidden_message;
21422 parser->type_definition_forbidden_message
21423 = G_("types may not be defined in parameter types");
21425 /* Parse the declaration-specifiers. */
21426 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21427 cp_parser_decl_specifier_seq (parser,
21428 CP_PARSER_FLAGS_NONE,
21429 &decl_specifiers,
21430 &declares_class_or_enum);
21432 /* Complain about missing 'typename' or other invalid type names. */
21433 if (!decl_specifiers.any_type_specifiers_p
21434 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21435 decl_specifiers.type = error_mark_node;
21437 /* If an error occurred, there's no reason to attempt to parse the
21438 rest of the declaration. */
21439 if (cp_parser_error_occurred (parser))
21441 parser->type_definition_forbidden_message = saved_message;
21442 return NULL;
21445 /* Peek at the next token. */
21446 token = cp_lexer_peek_token (parser->lexer);
21448 /* If the next token is a `)', `,', `=', `>', or `...', then there
21449 is no declarator. However, when variadic templates are enabled,
21450 there may be a declarator following `...'. */
21451 if (token->type == CPP_CLOSE_PAREN
21452 || token->type == CPP_COMMA
21453 || token->type == CPP_EQ
21454 || token->type == CPP_GREATER)
21456 declarator = NULL;
21457 if (parenthesized_p)
21458 *parenthesized_p = false;
21460 /* Otherwise, there should be a declarator. */
21461 else
21463 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21464 parser->default_arg_ok_p = false;
21466 /* After seeing a decl-specifier-seq, if the next token is not a
21467 "(", there is no possibility that the code is a valid
21468 expression. Therefore, if parsing tentatively, we commit at
21469 this point. */
21470 if (!parser->in_template_argument_list_p
21471 /* In an expression context, having seen:
21473 (int((char ...
21475 we cannot be sure whether we are looking at a
21476 function-type (taking a "char" as a parameter) or a cast
21477 of some object of type "char" to "int". */
21478 && !parser->in_type_id_in_expr_p
21479 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21480 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21481 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21482 cp_parser_commit_to_tentative_parse (parser);
21483 /* Parse the declarator. */
21484 declarator_token_start = token;
21485 declarator = cp_parser_declarator (parser,
21486 CP_PARSER_DECLARATOR_EITHER,
21487 /*ctor_dtor_or_conv_p=*/NULL,
21488 parenthesized_p,
21489 /*member_p=*/false,
21490 /*friend_p=*/false);
21491 parser->default_arg_ok_p = saved_default_arg_ok_p;
21492 /* After the declarator, allow more attributes. */
21493 decl_specifiers.attributes
21494 = chainon (decl_specifiers.attributes,
21495 cp_parser_attributes_opt (parser));
21497 /* If the declarator is a template parameter pack, remember that and
21498 clear the flag in the declarator itself so we don't get errors
21499 from grokdeclarator. */
21500 if (template_parm_p && declarator && declarator->parameter_pack_p)
21502 declarator->parameter_pack_p = false;
21503 template_parameter_pack_p = true;
21507 /* If the next token is an ellipsis, and we have not seen a declarator
21508 name, and if either the type of the declarator contains parameter
21509 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21510 for, eg, abbreviated integral type names), then we actually have a
21511 parameter pack expansion expression. Otherwise, leave the ellipsis
21512 for a C-style variadic function. */
21513 token = cp_lexer_peek_token (parser->lexer);
21514 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21516 tree type = decl_specifiers.type;
21518 if (type && DECL_P (type))
21519 type = TREE_TYPE (type);
21521 if (((type
21522 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21523 && (template_parm_p || uses_parameter_packs (type)))
21524 || (!type && template_parm_p))
21525 && declarator_can_be_parameter_pack (declarator))
21527 /* Consume the `...'. */
21528 cp_lexer_consume_token (parser->lexer);
21529 maybe_warn_variadic_templates ();
21531 /* Build a pack expansion type */
21532 if (template_parm_p)
21533 template_parameter_pack_p = true;
21534 else if (declarator)
21535 declarator->parameter_pack_p = true;
21536 else
21537 decl_specifiers.type = make_pack_expansion (type);
21541 /* The restriction on defining new types applies only to the type
21542 of the parameter, not to the default argument. */
21543 parser->type_definition_forbidden_message = saved_message;
21545 /* If the next token is `=', then process a default argument. */
21546 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21548 tree type = decl_specifiers.type;
21549 token = cp_lexer_peek_token (parser->lexer);
21550 /* If we are defining a class, then the tokens that make up the
21551 default argument must be saved and processed later. */
21552 if (!template_parm_p && at_class_scope_p ()
21553 && TYPE_BEING_DEFINED (current_class_type)
21554 && !LAMBDA_TYPE_P (current_class_type))
21555 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21557 // A constrained-type-specifier may declare a type template-parameter.
21558 else if (declares_constrained_type_template_parameter (type))
21559 default_argument
21560 = cp_parser_default_type_template_argument (parser);
21562 // A constrained-type-specifier may declare a template-template-parameter.
21563 else if (declares_constrained_template_template_parameter (type))
21564 default_argument
21565 = cp_parser_default_template_template_argument (parser);
21567 /* Outside of a class definition, we can just parse the
21568 assignment-expression. */
21569 else
21570 default_argument
21571 = cp_parser_default_argument (parser, template_parm_p);
21573 if (!parser->default_arg_ok_p)
21575 permerror (token->location,
21576 "default arguments are only "
21577 "permitted for function parameters");
21579 else if ((declarator && declarator->parameter_pack_p)
21580 || template_parameter_pack_p
21581 || (decl_specifiers.type
21582 && PACK_EXPANSION_P (decl_specifiers.type)))
21584 /* Find the name of the parameter pack. */
21585 cp_declarator *id_declarator = declarator;
21586 while (id_declarator && id_declarator->kind != cdk_id)
21587 id_declarator = id_declarator->declarator;
21589 if (id_declarator && id_declarator->kind == cdk_id)
21590 error_at (declarator_token_start->location,
21591 template_parm_p
21592 ? G_("template parameter pack %qD "
21593 "cannot have a default argument")
21594 : G_("parameter pack %qD cannot have "
21595 "a default argument"),
21596 id_declarator->u.id.unqualified_name);
21597 else
21598 error_at (declarator_token_start->location,
21599 template_parm_p
21600 ? G_("template parameter pack cannot have "
21601 "a default argument")
21602 : G_("parameter pack cannot have a "
21603 "default argument"));
21605 default_argument = NULL_TREE;
21608 else
21609 default_argument = NULL_TREE;
21611 /* Generate a location for the parameter, ranging from the start of the
21612 initial token to the end of the final token (using input_location for
21613 the latter, set up by cp_lexer_set_source_position_from_token when
21614 consuming tokens).
21616 If we have a identifier, then use it for the caret location, e.g.
21618 extern int callee (int one, int (*two)(int, int), float three);
21619 ~~~~~~^~~~~~~~~~~~~~
21621 otherwise, reuse the start location for the caret location e.g.:
21623 extern int callee (int one, int (*)(int, int), float three);
21624 ^~~~~~~~~~~~~~~~~
21627 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21628 ? declarator->id_loc
21629 : decl_spec_token_start->location);
21630 location_t param_loc = make_location (caret_loc,
21631 decl_spec_token_start->location,
21632 input_location);
21634 return make_parameter_declarator (&decl_specifiers,
21635 declarator,
21636 default_argument,
21637 param_loc,
21638 template_parameter_pack_p);
21641 /* Parse a default argument and return it.
21643 TEMPLATE_PARM_P is true if this is a default argument for a
21644 non-type template parameter. */
21645 static tree
21646 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21648 tree default_argument = NULL_TREE;
21649 bool saved_greater_than_is_operator_p;
21650 bool saved_local_variables_forbidden_p;
21651 bool non_constant_p, is_direct_init;
21653 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21654 set correctly. */
21655 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21656 parser->greater_than_is_operator_p = !template_parm_p;
21657 /* Local variable names (and the `this' keyword) may not
21658 appear in a default argument. */
21659 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21660 parser->local_variables_forbidden_p = true;
21661 /* Parse the assignment-expression. */
21662 if (template_parm_p)
21663 push_deferring_access_checks (dk_no_deferred);
21664 tree saved_class_ptr = NULL_TREE;
21665 tree saved_class_ref = NULL_TREE;
21666 /* The "this" pointer is not valid in a default argument. */
21667 if (cfun)
21669 saved_class_ptr = current_class_ptr;
21670 cp_function_chain->x_current_class_ptr = NULL_TREE;
21671 saved_class_ref = current_class_ref;
21672 cp_function_chain->x_current_class_ref = NULL_TREE;
21674 default_argument
21675 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21676 /* Restore the "this" pointer. */
21677 if (cfun)
21679 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21680 cp_function_chain->x_current_class_ref = saved_class_ref;
21682 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21683 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21684 if (template_parm_p)
21685 pop_deferring_access_checks ();
21686 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21687 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21689 return default_argument;
21692 /* Parse a function-body.
21694 function-body:
21695 compound_statement */
21697 static void
21698 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21700 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21701 ? BCS_TRY_BLOCK : BCS_NORMAL),
21702 true);
21705 /* Parse a ctor-initializer-opt followed by a function-body. Return
21706 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21707 is true we are parsing a function-try-block. */
21709 static void
21710 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21711 bool in_function_try_block)
21713 tree body, list;
21714 const bool check_body_p =
21715 DECL_CONSTRUCTOR_P (current_function_decl)
21716 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21717 tree last = NULL;
21719 /* Begin the function body. */
21720 body = begin_function_body ();
21721 /* Parse the optional ctor-initializer. */
21722 cp_parser_ctor_initializer_opt (parser);
21724 /* If we're parsing a constexpr constructor definition, we need
21725 to check that the constructor body is indeed empty. However,
21726 before we get to cp_parser_function_body lot of junk has been
21727 generated, so we can't just check that we have an empty block.
21728 Rather we take a snapshot of the outermost block, and check whether
21729 cp_parser_function_body changed its state. */
21730 if (check_body_p)
21732 list = cur_stmt_list;
21733 if (STATEMENT_LIST_TAIL (list))
21734 last = STATEMENT_LIST_TAIL (list)->stmt;
21736 /* Parse the function-body. */
21737 cp_parser_function_body (parser, in_function_try_block);
21738 if (check_body_p)
21739 check_constexpr_ctor_body (last, list, /*complain=*/true);
21740 /* Finish the function body. */
21741 finish_function_body (body);
21744 /* Parse an initializer.
21746 initializer:
21747 = initializer-clause
21748 ( expression-list )
21750 Returns an expression representing the initializer. If no
21751 initializer is present, NULL_TREE is returned.
21753 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21754 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21755 set to TRUE if there is no initializer present. If there is an
21756 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21757 is set to true; otherwise it is set to false. */
21759 static tree
21760 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21761 bool* non_constant_p)
21763 cp_token *token;
21764 tree init;
21766 /* Peek at the next token. */
21767 token = cp_lexer_peek_token (parser->lexer);
21769 /* Let our caller know whether or not this initializer was
21770 parenthesized. */
21771 *is_direct_init = (token->type != CPP_EQ);
21772 /* Assume that the initializer is constant. */
21773 *non_constant_p = false;
21775 if (token->type == CPP_EQ)
21777 /* Consume the `='. */
21778 cp_lexer_consume_token (parser->lexer);
21779 /* Parse the initializer-clause. */
21780 init = cp_parser_initializer_clause (parser, non_constant_p);
21782 else if (token->type == CPP_OPEN_PAREN)
21784 vec<tree, va_gc> *vec;
21785 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21786 /*cast_p=*/false,
21787 /*allow_expansion_p=*/true,
21788 non_constant_p);
21789 if (vec == NULL)
21790 return error_mark_node;
21791 init = build_tree_list_vec (vec);
21792 release_tree_vector (vec);
21794 else if (token->type == CPP_OPEN_BRACE)
21796 cp_lexer_set_source_position (parser->lexer);
21797 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21798 init = cp_parser_braced_list (parser, non_constant_p);
21799 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21801 else
21803 /* Anything else is an error. */
21804 cp_parser_error (parser, "expected initializer");
21805 init = error_mark_node;
21808 if (check_for_bare_parameter_packs (init))
21809 init = error_mark_node;
21811 return init;
21814 /* Parse an initializer-clause.
21816 initializer-clause:
21817 assignment-expression
21818 braced-init-list
21820 Returns an expression representing the initializer.
21822 If the `assignment-expression' production is used the value
21823 returned is simply a representation for the expression.
21825 Otherwise, calls cp_parser_braced_list. */
21827 static cp_expr
21828 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21830 cp_expr initializer;
21832 /* Assume the expression is constant. */
21833 *non_constant_p = false;
21835 /* If it is not a `{', then we are looking at an
21836 assignment-expression. */
21837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21839 initializer
21840 = cp_parser_constant_expression (parser,
21841 /*allow_non_constant_p=*/true,
21842 non_constant_p);
21844 else
21845 initializer = cp_parser_braced_list (parser, non_constant_p);
21847 return initializer;
21850 /* Parse a brace-enclosed initializer list.
21852 braced-init-list:
21853 { initializer-list , [opt] }
21854 { designated-initializer-list , [opt] }
21857 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21858 the elements of the initializer-list (or NULL, if the last
21859 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21860 NULL_TREE. There is no way to detect whether or not the optional
21861 trailing `,' was provided. NON_CONSTANT_P is as for
21862 cp_parser_initializer. */
21864 static cp_expr
21865 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21867 tree initializer;
21868 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21870 /* Consume the `{' token. */
21871 matching_braces braces;
21872 braces.consume_open (parser);
21873 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21874 initializer = make_node (CONSTRUCTOR);
21875 /* If it's not a `}', then there is a non-trivial initializer. */
21876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21878 /* Parse the initializer list. */
21879 CONSTRUCTOR_ELTS (initializer)
21880 = cp_parser_initializer_list (parser, non_constant_p);
21881 /* A trailing `,' token is allowed. */
21882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21883 cp_lexer_consume_token (parser->lexer);
21885 else
21886 *non_constant_p = false;
21887 /* Now, there should be a trailing `}'. */
21888 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21889 braces.require_close (parser);
21890 TREE_TYPE (initializer) = init_list_type_node;
21892 cp_expr result (initializer);
21893 /* Build a location of the form:
21894 { ... }
21895 ^~~~~~~
21896 with caret==start at the open brace, finish at the close brace. */
21897 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21898 result.set_location (combined_loc);
21899 return result;
21902 /* Consume tokens up to, and including, the next non-nested closing `]'.
21903 Returns true iff we found a closing `]'. */
21905 static bool
21906 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21908 unsigned square_depth = 0;
21910 while (true)
21912 cp_token * token = cp_lexer_peek_token (parser->lexer);
21914 switch (token->type)
21916 case CPP_EOF:
21917 case CPP_PRAGMA_EOL:
21918 /* If we've run out of tokens, then there is no closing `]'. */
21919 return false;
21921 case CPP_OPEN_SQUARE:
21922 ++square_depth;
21923 break;
21925 case CPP_CLOSE_SQUARE:
21926 if (!square_depth--)
21928 cp_lexer_consume_token (parser->lexer);
21929 return true;
21931 break;
21933 default:
21934 break;
21937 /* Consume the token. */
21938 cp_lexer_consume_token (parser->lexer);
21942 /* Return true if we are looking at an array-designator, false otherwise. */
21944 static bool
21945 cp_parser_array_designator_p (cp_parser *parser)
21947 /* Consume the `['. */
21948 cp_lexer_consume_token (parser->lexer);
21950 cp_lexer_save_tokens (parser->lexer);
21952 /* Skip tokens until the next token is a closing square bracket.
21953 If we find the closing `]', and the next token is a `=', then
21954 we are looking at an array designator. */
21955 bool array_designator_p
21956 = (cp_parser_skip_to_closing_square_bracket (parser)
21957 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21959 /* Roll back the tokens we skipped. */
21960 cp_lexer_rollback_tokens (parser->lexer);
21962 return array_designator_p;
21965 /* Parse an initializer-list.
21967 initializer-list:
21968 initializer-clause ... [opt]
21969 initializer-list , initializer-clause ... [opt]
21971 C++2A Extension:
21973 designated-initializer-list:
21974 designated-initializer-clause
21975 designated-initializer-list , designated-initializer-clause
21977 designated-initializer-clause:
21978 designator brace-or-equal-initializer
21980 designator:
21981 . identifier
21983 GNU Extension:
21985 initializer-list:
21986 designation initializer-clause ...[opt]
21987 initializer-list , designation initializer-clause ...[opt]
21989 designation:
21990 . identifier =
21991 identifier :
21992 [ constant-expression ] =
21994 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21995 for the initializer. If the INDEX of the elt is non-NULL, it is the
21996 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21997 as for cp_parser_initializer. */
21999 static vec<constructor_elt, va_gc> *
22000 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22002 vec<constructor_elt, va_gc> *v = NULL;
22003 bool first_p = true;
22004 tree first_designator = NULL_TREE;
22006 /* Assume all of the expressions are constant. */
22007 *non_constant_p = false;
22009 /* Parse the rest of the list. */
22010 while (true)
22012 cp_token *token;
22013 tree designator;
22014 tree initializer;
22015 bool clause_non_constant_p;
22016 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22018 /* Handle the C++2A syntax, '. id ='. */
22019 if ((cxx_dialect >= cxx2a
22020 || cp_parser_allow_gnu_extensions_p (parser))
22021 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22022 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22023 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22024 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22025 == CPP_OPEN_BRACE)))
22027 if (cxx_dialect < cxx2a)
22028 pedwarn (loc, OPT_Wpedantic,
22029 "C++ designated initializers only available with "
22030 "-std=c++2a or -std=gnu++2a");
22031 /* Consume the `.'. */
22032 cp_lexer_consume_token (parser->lexer);
22033 /* Consume the identifier. */
22034 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22035 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22036 /* Consume the `='. */
22037 cp_lexer_consume_token (parser->lexer);
22039 /* Also, if the next token is an identifier and the following one is a
22040 colon, we are looking at the GNU designated-initializer
22041 syntax. */
22042 else if (cp_parser_allow_gnu_extensions_p (parser)
22043 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22044 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22045 == CPP_COLON))
22047 /* Warn the user that they are using an extension. */
22048 pedwarn (loc, OPT_Wpedantic,
22049 "ISO C++ does not allow GNU designated initializers");
22050 /* Consume the identifier. */
22051 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22052 /* Consume the `:'. */
22053 cp_lexer_consume_token (parser->lexer);
22055 /* Also handle C99 array designators, '[ const ] ='. */
22056 else if (cp_parser_allow_gnu_extensions_p (parser)
22057 && !c_dialect_objc ()
22058 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22060 /* In C++11, [ could start a lambda-introducer. */
22061 bool non_const = false;
22063 cp_parser_parse_tentatively (parser);
22065 if (!cp_parser_array_designator_p (parser))
22067 cp_parser_simulate_error (parser);
22068 designator = NULL_TREE;
22070 else
22072 designator = cp_parser_constant_expression (parser, true,
22073 &non_const);
22074 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22075 cp_parser_require (parser, CPP_EQ, RT_EQ);
22078 if (!cp_parser_parse_definitely (parser))
22079 designator = NULL_TREE;
22080 else if (non_const
22081 && (!require_potential_rvalue_constant_expression
22082 (designator)))
22083 designator = NULL_TREE;
22084 if (designator)
22085 /* Warn the user that they are using an extension. */
22086 pedwarn (loc, OPT_Wpedantic,
22087 "ISO C++ does not allow C99 designated initializers");
22089 else
22090 designator = NULL_TREE;
22092 if (first_p)
22094 first_designator = designator;
22095 first_p = false;
22097 else if (cxx_dialect >= cxx2a
22098 && first_designator != error_mark_node
22099 && (!first_designator != !designator))
22101 error_at (loc, "either all initializer clauses should be designated "
22102 "or none of them should be");
22103 first_designator = error_mark_node;
22105 else if (cxx_dialect < cxx2a && !first_designator)
22106 first_designator = designator;
22108 /* Parse the initializer. */
22109 initializer = cp_parser_initializer_clause (parser,
22110 &clause_non_constant_p);
22111 /* If any clause is non-constant, so is the entire initializer. */
22112 if (clause_non_constant_p)
22113 *non_constant_p = true;
22115 /* If we have an ellipsis, this is an initializer pack
22116 expansion. */
22117 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22121 /* Consume the `...'. */
22122 cp_lexer_consume_token (parser->lexer);
22124 if (designator && cxx_dialect >= cxx2a)
22125 error_at (loc,
22126 "%<...%> not allowed in designated initializer list");
22128 /* Turn the initializer into an initializer expansion. */
22129 initializer = make_pack_expansion (initializer);
22132 /* Add it to the vector. */
22133 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22135 /* If the next token is not a comma, we have reached the end of
22136 the list. */
22137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22138 break;
22140 /* Peek at the next token. */
22141 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22142 /* If the next token is a `}', then we're still done. An
22143 initializer-clause can have a trailing `,' after the
22144 initializer-list and before the closing `}'. */
22145 if (token->type == CPP_CLOSE_BRACE)
22146 break;
22148 /* Consume the `,' token. */
22149 cp_lexer_consume_token (parser->lexer);
22152 /* The same identifier shall not appear in multiple designators
22153 of a designated-initializer-list. */
22154 if (first_designator)
22156 unsigned int i;
22157 tree designator, val;
22158 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22159 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22161 if (IDENTIFIER_MARKED (designator))
22163 error_at (EXPR_LOC_OR_LOC (val, input_location),
22164 "%<.%s%> designator used multiple times in "
22165 "the same initializer list",
22166 IDENTIFIER_POINTER (designator));
22167 (*v)[i].index = NULL_TREE;
22169 else
22170 IDENTIFIER_MARKED (designator) = 1;
22172 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22173 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22174 IDENTIFIER_MARKED (designator) = 0;
22177 return v;
22180 /* Classes [gram.class] */
22182 /* Parse a class-name.
22184 class-name:
22185 identifier
22186 template-id
22188 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22189 to indicate that names looked up in dependent types should be
22190 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22191 keyword has been used to indicate that the name that appears next
22192 is a template. TAG_TYPE indicates the explicit tag given before
22193 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22194 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22195 is the class being defined in a class-head. If ENUM_OK is TRUE,
22196 enum-names are also accepted.
22198 Returns the TYPE_DECL representing the class. */
22200 static tree
22201 cp_parser_class_name (cp_parser *parser,
22202 bool typename_keyword_p,
22203 bool template_keyword_p,
22204 enum tag_types tag_type,
22205 bool check_dependency_p,
22206 bool class_head_p,
22207 bool is_declaration,
22208 bool enum_ok)
22210 tree decl;
22211 tree scope;
22212 bool typename_p;
22213 cp_token *token;
22214 tree identifier = NULL_TREE;
22216 /* All class-names start with an identifier. */
22217 token = cp_lexer_peek_token (parser->lexer);
22218 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22220 cp_parser_error (parser, "expected class-name");
22221 return error_mark_node;
22224 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22225 to a template-id, so we save it here. */
22226 scope = parser->scope;
22227 if (scope == error_mark_node)
22228 return error_mark_node;
22230 /* Any name names a type if we're following the `typename' keyword
22231 in a qualified name where the enclosing scope is type-dependent. */
22232 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22233 && dependent_type_p (scope));
22234 /* Handle the common case (an identifier, but not a template-id)
22235 efficiently. */
22236 if (token->type == CPP_NAME
22237 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22239 cp_token *identifier_token;
22240 bool ambiguous_p;
22242 /* Look for the identifier. */
22243 identifier_token = cp_lexer_peek_token (parser->lexer);
22244 ambiguous_p = identifier_token->error_reported;
22245 identifier = cp_parser_identifier (parser);
22246 /* If the next token isn't an identifier, we are certainly not
22247 looking at a class-name. */
22248 if (identifier == error_mark_node)
22249 decl = error_mark_node;
22250 /* If we know this is a type-name, there's no need to look it
22251 up. */
22252 else if (typename_p)
22253 decl = identifier;
22254 else
22256 tree ambiguous_decls;
22257 /* If we already know that this lookup is ambiguous, then
22258 we've already issued an error message; there's no reason
22259 to check again. */
22260 if (ambiguous_p)
22262 cp_parser_simulate_error (parser);
22263 return error_mark_node;
22265 /* If the next token is a `::', then the name must be a type
22266 name.
22268 [basic.lookup.qual]
22270 During the lookup for a name preceding the :: scope
22271 resolution operator, object, function, and enumerator
22272 names are ignored. */
22273 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22274 tag_type = scope_type;
22275 /* Look up the name. */
22276 decl = cp_parser_lookup_name (parser, identifier,
22277 tag_type,
22278 /*is_template=*/false,
22279 /*is_namespace=*/false,
22280 check_dependency_p,
22281 &ambiguous_decls,
22282 identifier_token->location);
22283 if (ambiguous_decls)
22285 if (cp_parser_parsing_tentatively (parser))
22286 cp_parser_simulate_error (parser);
22287 return error_mark_node;
22291 else
22293 /* Try a template-id. */
22294 decl = cp_parser_template_id (parser, template_keyword_p,
22295 check_dependency_p,
22296 tag_type,
22297 is_declaration);
22298 if (decl == error_mark_node)
22299 return error_mark_node;
22302 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22304 /* If this is a typename, create a TYPENAME_TYPE. */
22305 if (typename_p && decl != error_mark_node)
22307 decl = make_typename_type (scope, decl, typename_type,
22308 /*complain=*/tf_error);
22309 if (decl != error_mark_node)
22310 decl = TYPE_NAME (decl);
22313 decl = strip_using_decl (decl);
22315 /* Check to see that it is really the name of a class. */
22316 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22317 && identifier_p (TREE_OPERAND (decl, 0))
22318 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22319 /* Situations like this:
22321 template <typename T> struct A {
22322 typename T::template X<int>::I i;
22325 are problematic. Is `T::template X<int>' a class-name? The
22326 standard does not seem to be definitive, but there is no other
22327 valid interpretation of the following `::'. Therefore, those
22328 names are considered class-names. */
22330 decl = make_typename_type (scope, decl, tag_type, tf_error);
22331 if (decl != error_mark_node)
22332 decl = TYPE_NAME (decl);
22334 else if (TREE_CODE (decl) != TYPE_DECL
22335 || TREE_TYPE (decl) == error_mark_node
22336 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22337 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22338 /* In Objective-C 2.0, a classname followed by '.' starts a
22339 dot-syntax expression, and it's not a type-name. */
22340 || (c_dialect_objc ()
22341 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22342 && objc_is_class_name (decl)))
22343 decl = error_mark_node;
22345 if (decl == error_mark_node)
22346 cp_parser_error (parser, "expected class-name");
22347 else if (identifier && !parser->scope)
22348 maybe_note_name_used_in_class (identifier, decl);
22350 return decl;
22353 /* Parse a class-specifier.
22355 class-specifier:
22356 class-head { member-specification [opt] }
22358 Returns the TREE_TYPE representing the class. */
22360 static tree
22361 cp_parser_class_specifier_1 (cp_parser* parser)
22363 tree type;
22364 tree attributes = NULL_TREE;
22365 bool nested_name_specifier_p;
22366 unsigned saved_num_template_parameter_lists;
22367 bool saved_in_function_body;
22368 unsigned char in_statement;
22369 bool in_switch_statement_p;
22370 bool saved_in_unbraced_linkage_specification_p;
22371 tree old_scope = NULL_TREE;
22372 tree scope = NULL_TREE;
22373 cp_token *closing_brace;
22375 push_deferring_access_checks (dk_no_deferred);
22377 /* Parse the class-head. */
22378 type = cp_parser_class_head (parser,
22379 &nested_name_specifier_p);
22380 /* If the class-head was a semantic disaster, skip the entire body
22381 of the class. */
22382 if (!type)
22384 cp_parser_skip_to_end_of_block_or_statement (parser);
22385 pop_deferring_access_checks ();
22386 return error_mark_node;
22389 /* Look for the `{'. */
22390 matching_braces braces;
22391 if (!braces.require_open (parser))
22393 pop_deferring_access_checks ();
22394 return error_mark_node;
22397 cp_ensure_no_omp_declare_simd (parser);
22398 cp_ensure_no_oacc_routine (parser);
22400 /* Issue an error message if type-definitions are forbidden here. */
22401 cp_parser_check_type_definition (parser);
22402 /* Remember that we are defining one more class. */
22403 ++parser->num_classes_being_defined;
22404 /* Inside the class, surrounding template-parameter-lists do not
22405 apply. */
22406 saved_num_template_parameter_lists
22407 = parser->num_template_parameter_lists;
22408 parser->num_template_parameter_lists = 0;
22409 /* We are not in a function body. */
22410 saved_in_function_body = parser->in_function_body;
22411 parser->in_function_body = false;
22412 /* Or in a loop. */
22413 in_statement = parser->in_statement;
22414 parser->in_statement = 0;
22415 /* Or in a switch. */
22416 in_switch_statement_p = parser->in_switch_statement_p;
22417 parser->in_switch_statement_p = false;
22418 /* We are not immediately inside an extern "lang" block. */
22419 saved_in_unbraced_linkage_specification_p
22420 = parser->in_unbraced_linkage_specification_p;
22421 parser->in_unbraced_linkage_specification_p = false;
22423 // Associate constraints with the type.
22424 if (flag_concepts)
22425 type = associate_classtype_constraints (type);
22427 /* Start the class. */
22428 if (nested_name_specifier_p)
22430 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22431 old_scope = push_inner_scope (scope);
22433 type = begin_class_definition (type);
22435 if (type == error_mark_node)
22436 /* If the type is erroneous, skip the entire body of the class. */
22437 cp_parser_skip_to_closing_brace (parser);
22438 else
22439 /* Parse the member-specification. */
22440 cp_parser_member_specification_opt (parser);
22442 /* Look for the trailing `}'. */
22443 closing_brace = braces.require_close (parser);
22444 /* Look for trailing attributes to apply to this class. */
22445 if (cp_parser_allow_gnu_extensions_p (parser))
22446 attributes = cp_parser_gnu_attributes_opt (parser);
22447 if (type != error_mark_node)
22448 type = finish_struct (type, attributes);
22449 if (nested_name_specifier_p)
22450 pop_inner_scope (old_scope, scope);
22452 /* We've finished a type definition. Check for the common syntax
22453 error of forgetting a semicolon after the definition. We need to
22454 be careful, as we can't just check for not-a-semicolon and be done
22455 with it; the user might have typed:
22457 class X { } c = ...;
22458 class X { } *p = ...;
22460 and so forth. Instead, enumerate all the possible tokens that
22461 might follow this production; if we don't see one of them, then
22462 complain and silently insert the semicolon. */
22464 cp_token *token = cp_lexer_peek_token (parser->lexer);
22465 bool want_semicolon = true;
22467 if (cp_next_tokens_can_be_std_attribute_p (parser))
22468 /* Don't try to parse c++11 attributes here. As per the
22469 grammar, that should be a task for
22470 cp_parser_decl_specifier_seq. */
22471 want_semicolon = false;
22473 switch (token->type)
22475 case CPP_NAME:
22476 case CPP_SEMICOLON:
22477 case CPP_MULT:
22478 case CPP_AND:
22479 case CPP_OPEN_PAREN:
22480 case CPP_CLOSE_PAREN:
22481 case CPP_COMMA:
22482 want_semicolon = false;
22483 break;
22485 /* While it's legal for type qualifiers and storage class
22486 specifiers to follow type definitions in the grammar, only
22487 compiler testsuites contain code like that. Assume that if
22488 we see such code, then what we're really seeing is a case
22489 like:
22491 class X { }
22492 const <type> var = ...;
22496 class Y { }
22497 static <type> func (...) ...
22499 i.e. the qualifier or specifier applies to the next
22500 declaration. To do so, however, we need to look ahead one
22501 more token to see if *that* token is a type specifier.
22503 This code could be improved to handle:
22505 class Z { }
22506 static const <type> var = ...; */
22507 case CPP_KEYWORD:
22508 if (keyword_is_decl_specifier (token->keyword))
22510 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22512 /* Handling user-defined types here would be nice, but very
22513 tricky. */
22514 want_semicolon
22515 = (lookahead->type == CPP_KEYWORD
22516 && keyword_begins_type_specifier (lookahead->keyword));
22518 break;
22519 default:
22520 break;
22523 /* If we don't have a type, then something is very wrong and we
22524 shouldn't try to do anything clever. Likewise for not seeing the
22525 closing brace. */
22526 if (closing_brace && TYPE_P (type) && want_semicolon)
22528 /* Locate the closing brace. */
22529 cp_token_position prev
22530 = cp_lexer_previous_token_position (parser->lexer);
22531 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22532 location_t loc = prev_token->location;
22534 /* We want to suggest insertion of a ';' immediately *after* the
22535 closing brace, so, if we can, offset the location by 1 column. */
22536 location_t next_loc = loc;
22537 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22538 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22540 rich_location richloc (line_table, next_loc);
22542 /* If we successfully offset the location, suggest the fix-it. */
22543 if (next_loc != loc)
22544 richloc.add_fixit_insert_before (next_loc, ";");
22546 if (CLASSTYPE_DECLARED_CLASS (type))
22547 error_at (&richloc,
22548 "expected %<;%> after class definition");
22549 else if (TREE_CODE (type) == RECORD_TYPE)
22550 error_at (&richloc,
22551 "expected %<;%> after struct definition");
22552 else if (TREE_CODE (type) == UNION_TYPE)
22553 error_at (&richloc,
22554 "expected %<;%> after union definition");
22555 else
22556 gcc_unreachable ();
22558 /* Unget one token and smash it to look as though we encountered
22559 a semicolon in the input stream. */
22560 cp_lexer_set_token_position (parser->lexer, prev);
22561 token = cp_lexer_peek_token (parser->lexer);
22562 token->type = CPP_SEMICOLON;
22563 token->keyword = RID_MAX;
22567 /* If this class is not itself within the scope of another class,
22568 then we need to parse the bodies of all of the queued function
22569 definitions. Note that the queued functions defined in a class
22570 are not always processed immediately following the
22571 class-specifier for that class. Consider:
22573 struct A {
22574 struct B { void f() { sizeof (A); } };
22577 If `f' were processed before the processing of `A' were
22578 completed, there would be no way to compute the size of `A'.
22579 Note that the nesting we are interested in here is lexical --
22580 not the semantic nesting given by TYPE_CONTEXT. In particular,
22581 for:
22583 struct A { struct B; };
22584 struct A::B { void f() { } };
22586 there is no need to delay the parsing of `A::B::f'. */
22587 if (--parser->num_classes_being_defined == 0)
22589 tree decl;
22590 tree class_type = NULL_TREE;
22591 tree pushed_scope = NULL_TREE;
22592 unsigned ix;
22593 cp_default_arg_entry *e;
22594 tree save_ccp, save_ccr;
22596 /* In a first pass, parse default arguments to the functions.
22597 Then, in a second pass, parse the bodies of the functions.
22598 This two-phased approach handles cases like:
22600 struct S {
22601 void f() { g(); }
22602 void g(int i = 3);
22606 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22608 decl = e->decl;
22609 /* If there are default arguments that have not yet been processed,
22610 take care of them now. */
22611 if (class_type != e->class_type)
22613 if (pushed_scope)
22614 pop_scope (pushed_scope);
22615 class_type = e->class_type;
22616 pushed_scope = push_scope (class_type);
22618 /* Make sure that any template parameters are in scope. */
22619 maybe_begin_member_template_processing (decl);
22620 /* Parse the default argument expressions. */
22621 cp_parser_late_parsing_default_args (parser, decl);
22622 /* Remove any template parameters from the symbol table. */
22623 maybe_end_member_template_processing ();
22625 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22626 /* Now parse any NSDMIs. */
22627 save_ccp = current_class_ptr;
22628 save_ccr = current_class_ref;
22629 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22631 if (class_type != DECL_CONTEXT (decl))
22633 if (pushed_scope)
22634 pop_scope (pushed_scope);
22635 class_type = DECL_CONTEXT (decl);
22636 pushed_scope = push_scope (class_type);
22638 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22639 cp_parser_late_parsing_nsdmi (parser, decl);
22641 vec_safe_truncate (unparsed_nsdmis, 0);
22642 current_class_ptr = save_ccp;
22643 current_class_ref = save_ccr;
22644 if (pushed_scope)
22645 pop_scope (pushed_scope);
22647 /* Now do some post-NSDMI bookkeeping. */
22648 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22649 after_nsdmi_defaulted_late_checks (class_type);
22650 vec_safe_truncate (unparsed_classes, 0);
22651 after_nsdmi_defaulted_late_checks (type);
22653 /* Now parse the body of the functions. */
22654 if (flag_openmp)
22656 /* OpenMP UDRs need to be parsed before all other functions. */
22657 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22658 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22659 cp_parser_late_parsing_for_member (parser, decl);
22660 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22661 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22662 cp_parser_late_parsing_for_member (parser, decl);
22664 else
22665 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22666 cp_parser_late_parsing_for_member (parser, decl);
22667 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22669 else
22670 vec_safe_push (unparsed_classes, type);
22672 /* Put back any saved access checks. */
22673 pop_deferring_access_checks ();
22675 /* Restore saved state. */
22676 parser->in_switch_statement_p = in_switch_statement_p;
22677 parser->in_statement = in_statement;
22678 parser->in_function_body = saved_in_function_body;
22679 parser->num_template_parameter_lists
22680 = saved_num_template_parameter_lists;
22681 parser->in_unbraced_linkage_specification_p
22682 = saved_in_unbraced_linkage_specification_p;
22684 return type;
22687 static tree
22688 cp_parser_class_specifier (cp_parser* parser)
22690 tree ret;
22691 timevar_push (TV_PARSE_STRUCT);
22692 ret = cp_parser_class_specifier_1 (parser);
22693 timevar_pop (TV_PARSE_STRUCT);
22694 return ret;
22697 /* Parse a class-head.
22699 class-head:
22700 class-key identifier [opt] base-clause [opt]
22701 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22702 class-key nested-name-specifier [opt] template-id
22703 base-clause [opt]
22705 class-virt-specifier:
22706 final
22708 GNU Extensions:
22709 class-key attributes identifier [opt] base-clause [opt]
22710 class-key attributes nested-name-specifier identifier base-clause [opt]
22711 class-key attributes nested-name-specifier [opt] template-id
22712 base-clause [opt]
22714 Upon return BASES is initialized to the list of base classes (or
22715 NULL, if there are none) in the same form returned by
22716 cp_parser_base_clause.
22718 Returns the TYPE of the indicated class. Sets
22719 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22720 involving a nested-name-specifier was used, and FALSE otherwise.
22722 Returns error_mark_node if this is not a class-head.
22724 Returns NULL_TREE if the class-head is syntactically valid, but
22725 semantically invalid in a way that means we should skip the entire
22726 body of the class. */
22728 static tree
22729 cp_parser_class_head (cp_parser* parser,
22730 bool* nested_name_specifier_p)
22732 tree nested_name_specifier;
22733 enum tag_types class_key;
22734 tree id = NULL_TREE;
22735 tree type = NULL_TREE;
22736 tree attributes;
22737 tree bases;
22738 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22739 bool template_id_p = false;
22740 bool qualified_p = false;
22741 bool invalid_nested_name_p = false;
22742 bool invalid_explicit_specialization_p = false;
22743 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22744 tree pushed_scope = NULL_TREE;
22745 unsigned num_templates;
22746 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22747 /* Assume no nested-name-specifier will be present. */
22748 *nested_name_specifier_p = false;
22749 /* Assume no template parameter lists will be used in defining the
22750 type. */
22751 num_templates = 0;
22752 parser->colon_corrects_to_scope_p = false;
22754 /* Look for the class-key. */
22755 class_key = cp_parser_class_key (parser);
22756 if (class_key == none_type)
22757 return error_mark_node;
22759 location_t class_head_start_location = input_location;
22761 /* Parse the attributes. */
22762 attributes = cp_parser_attributes_opt (parser);
22764 /* If the next token is `::', that is invalid -- but sometimes
22765 people do try to write:
22767 struct ::S {};
22769 Handle this gracefully by accepting the extra qualifier, and then
22770 issuing an error about it later if this really is a
22771 class-head. If it turns out just to be an elaborated type
22772 specifier, remain silent. */
22773 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22774 qualified_p = true;
22776 push_deferring_access_checks (dk_no_check);
22778 /* Determine the name of the class. Begin by looking for an
22779 optional nested-name-specifier. */
22780 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22781 nested_name_specifier
22782 = cp_parser_nested_name_specifier_opt (parser,
22783 /*typename_keyword_p=*/false,
22784 /*check_dependency_p=*/false,
22785 /*type_p=*/true,
22786 /*is_declaration=*/false);
22787 /* If there was a nested-name-specifier, then there *must* be an
22788 identifier. */
22790 cp_token *bad_template_keyword = NULL;
22792 if (nested_name_specifier)
22794 type_start_token = cp_lexer_peek_token (parser->lexer);
22795 /* Although the grammar says `identifier', it really means
22796 `class-name' or `template-name'. You are only allowed to
22797 define a class that has already been declared with this
22798 syntax.
22800 The proposed resolution for Core Issue 180 says that wherever
22801 you see `class T::X' you should treat `X' as a type-name.
22803 It is OK to define an inaccessible class; for example:
22805 class A { class B; };
22806 class A::B {};
22808 We do not know if we will see a class-name, or a
22809 template-name. We look for a class-name first, in case the
22810 class-name is a template-id; if we looked for the
22811 template-name first we would stop after the template-name. */
22812 cp_parser_parse_tentatively (parser);
22813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22814 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22815 type = cp_parser_class_name (parser,
22816 /*typename_keyword_p=*/false,
22817 /*template_keyword_p=*/false,
22818 class_type,
22819 /*check_dependency_p=*/false,
22820 /*class_head_p=*/true,
22821 /*is_declaration=*/false);
22822 /* If that didn't work, ignore the nested-name-specifier. */
22823 if (!cp_parser_parse_definitely (parser))
22825 invalid_nested_name_p = true;
22826 type_start_token = cp_lexer_peek_token (parser->lexer);
22827 id = cp_parser_identifier (parser);
22828 if (id == error_mark_node)
22829 id = NULL_TREE;
22831 /* If we could not find a corresponding TYPE, treat this
22832 declaration like an unqualified declaration. */
22833 if (type == error_mark_node)
22834 nested_name_specifier = NULL_TREE;
22835 /* Otherwise, count the number of templates used in TYPE and its
22836 containing scopes. */
22837 else
22839 tree scope;
22841 for (scope = TREE_TYPE (type);
22842 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22843 scope = get_containing_scope (scope))
22844 if (TYPE_P (scope)
22845 && CLASS_TYPE_P (scope)
22846 && CLASSTYPE_TEMPLATE_INFO (scope)
22847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22848 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22849 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22850 ++num_templates;
22853 /* Otherwise, the identifier is optional. */
22854 else
22856 /* We don't know whether what comes next is a template-id,
22857 an identifier, or nothing at all. */
22858 cp_parser_parse_tentatively (parser);
22859 /* Check for a template-id. */
22860 type_start_token = cp_lexer_peek_token (parser->lexer);
22861 id = cp_parser_template_id (parser,
22862 /*template_keyword_p=*/false,
22863 /*check_dependency_p=*/true,
22864 class_key,
22865 /*is_declaration=*/true);
22866 /* If that didn't work, it could still be an identifier. */
22867 if (!cp_parser_parse_definitely (parser))
22869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22871 type_start_token = cp_lexer_peek_token (parser->lexer);
22872 id = cp_parser_identifier (parser);
22874 else
22875 id = NULL_TREE;
22877 else
22879 template_id_p = true;
22880 ++num_templates;
22884 pop_deferring_access_checks ();
22886 if (id)
22888 cp_parser_check_for_invalid_template_id (parser, id,
22889 class_key,
22890 type_start_token->location);
22892 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22894 /* If it's not a `:' or a `{' then we can't really be looking at a
22895 class-head, since a class-head only appears as part of a
22896 class-specifier. We have to detect this situation before calling
22897 xref_tag, since that has irreversible side-effects. */
22898 if (!cp_parser_next_token_starts_class_definition_p (parser))
22900 cp_parser_error (parser, "expected %<{%> or %<:%>");
22901 type = error_mark_node;
22902 goto out;
22905 /* At this point, we're going ahead with the class-specifier, even
22906 if some other problem occurs. */
22907 cp_parser_commit_to_tentative_parse (parser);
22908 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22910 cp_parser_error (parser,
22911 "cannot specify %<override%> for a class");
22912 type = error_mark_node;
22913 goto out;
22915 /* Issue the error about the overly-qualified name now. */
22916 if (qualified_p)
22918 cp_parser_error (parser,
22919 "global qualification of class name is invalid");
22920 type = error_mark_node;
22921 goto out;
22923 else if (invalid_nested_name_p)
22925 cp_parser_error (parser,
22926 "qualified name does not name a class");
22927 type = error_mark_node;
22928 goto out;
22930 else if (nested_name_specifier)
22932 tree scope;
22934 if (bad_template_keyword)
22935 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22936 keyword template shall not appear at the top level. */
22937 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22938 "keyword %<template%> not allowed in class-head-name");
22940 /* Reject typedef-names in class heads. */
22941 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22943 error_at (type_start_token->location,
22944 "invalid class name in declaration of %qD",
22945 type);
22946 type = NULL_TREE;
22947 goto done;
22950 /* Figure out in what scope the declaration is being placed. */
22951 scope = current_scope ();
22952 /* If that scope does not contain the scope in which the
22953 class was originally declared, the program is invalid. */
22954 if (scope && !is_ancestor (scope, nested_name_specifier))
22956 if (at_namespace_scope_p ())
22957 error_at (type_start_token->location,
22958 "declaration of %qD in namespace %qD which does not "
22959 "enclose %qD",
22960 type, scope, nested_name_specifier);
22961 else
22962 error_at (type_start_token->location,
22963 "declaration of %qD in %qD which does not enclose %qD",
22964 type, scope, nested_name_specifier);
22965 type = NULL_TREE;
22966 goto done;
22968 /* [dcl.meaning]
22970 A declarator-id shall not be qualified except for the
22971 definition of a ... nested class outside of its class
22972 ... [or] the definition or explicit instantiation of a
22973 class member of a namespace outside of its namespace. */
22974 if (scope == nested_name_specifier)
22976 permerror (nested_name_specifier_token_start->location,
22977 "extra qualification not allowed");
22978 nested_name_specifier = NULL_TREE;
22979 num_templates = 0;
22982 /* An explicit-specialization must be preceded by "template <>". If
22983 it is not, try to recover gracefully. */
22984 if (at_namespace_scope_p ()
22985 && parser->num_template_parameter_lists == 0
22986 && !processing_template_parmlist
22987 && template_id_p)
22989 /* Build a location of this form:
22990 struct typename <ARGS>
22991 ^~~~~~~~~~~~~~~~~~~~~~
22992 with caret==start at the start token, and
22993 finishing at the end of the type. */
22994 location_t reported_loc
22995 = make_location (class_head_start_location,
22996 class_head_start_location,
22997 get_finish (type_start_token->location));
22998 rich_location richloc (line_table, reported_loc);
22999 richloc.add_fixit_insert_before (class_head_start_location,
23000 "template <> ");
23001 error_at (&richloc,
23002 "an explicit specialization must be preceded by"
23003 " %<template <>%>");
23004 invalid_explicit_specialization_p = true;
23005 /* Take the same action that would have been taken by
23006 cp_parser_explicit_specialization. */
23007 ++parser->num_template_parameter_lists;
23008 begin_specialization ();
23010 /* There must be no "return" statements between this point and the
23011 end of this function; set "type "to the correct return value and
23012 use "goto done;" to return. */
23013 /* Make sure that the right number of template parameters were
23014 present. */
23015 if (!cp_parser_check_template_parameters (parser, num_templates,
23016 type_start_token->location,
23017 /*declarator=*/NULL))
23019 /* If something went wrong, there is no point in even trying to
23020 process the class-definition. */
23021 type = NULL_TREE;
23022 goto done;
23025 /* Look up the type. */
23026 if (template_id_p)
23028 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23029 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23030 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23032 error_at (type_start_token->location,
23033 "function template %qD redeclared as a class template", id);
23034 type = error_mark_node;
23036 else
23038 type = TREE_TYPE (id);
23039 type = maybe_process_partial_specialization (type);
23041 /* Check the scope while we still know whether or not we had a
23042 nested-name-specifier. */
23043 if (type != error_mark_node)
23044 check_unqualified_spec_or_inst (type, type_start_token->location);
23046 if (nested_name_specifier)
23047 pushed_scope = push_scope (nested_name_specifier);
23049 else if (nested_name_specifier)
23051 tree class_type;
23053 /* Given:
23055 template <typename T> struct S { struct T };
23056 template <typename T> struct S<T>::T { };
23058 we will get a TYPENAME_TYPE when processing the definition of
23059 `S::T'. We need to resolve it to the actual type before we
23060 try to define it. */
23061 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23063 class_type = resolve_typename_type (TREE_TYPE (type),
23064 /*only_current_p=*/false);
23065 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23066 type = TYPE_NAME (class_type);
23067 else
23069 cp_parser_error (parser, "could not resolve typename type");
23070 type = error_mark_node;
23074 if (maybe_process_partial_specialization (TREE_TYPE (type))
23075 == error_mark_node)
23077 type = NULL_TREE;
23078 goto done;
23081 class_type = current_class_type;
23082 /* Enter the scope indicated by the nested-name-specifier. */
23083 pushed_scope = push_scope (nested_name_specifier);
23084 /* Get the canonical version of this type. */
23085 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23086 /* Call push_template_decl if it seems like we should be defining a
23087 template either from the template headers or the type we're
23088 defining, so that we diagnose both extra and missing headers. */
23089 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23090 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23091 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23093 type = push_template_decl (type);
23094 if (type == error_mark_node)
23096 type = NULL_TREE;
23097 goto done;
23101 type = TREE_TYPE (type);
23102 *nested_name_specifier_p = true;
23104 else /* The name is not a nested name. */
23106 /* If the class was unnamed, create a dummy name. */
23107 if (!id)
23108 id = make_anon_name ();
23109 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23110 ? ts_within_enclosing_non_class
23111 : ts_current);
23112 type = xref_tag (class_key, id, tag_scope,
23113 parser->num_template_parameter_lists);
23116 /* Indicate whether this class was declared as a `class' or as a
23117 `struct'. */
23118 if (TREE_CODE (type) == RECORD_TYPE)
23119 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23120 cp_parser_check_class_key (class_key, type);
23122 /* If this type was already complete, and we see another definition,
23123 that's an error. */
23124 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23126 error_at (type_start_token->location, "redefinition of %q#T",
23127 type);
23128 inform (location_of (type), "previous definition of %q#T",
23129 type);
23130 type = NULL_TREE;
23131 goto done;
23133 else if (type == error_mark_node)
23134 type = NULL_TREE;
23136 if (type)
23138 /* Apply attributes now, before any use of the class as a template
23139 argument in its base list. */
23140 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23141 fixup_attribute_variants (type);
23144 /* We will have entered the scope containing the class; the names of
23145 base classes should be looked up in that context. For example:
23147 struct A { struct B {}; struct C; };
23148 struct A::C : B {};
23150 is valid. */
23152 /* Get the list of base-classes, if there is one. */
23153 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23155 /* PR59482: enter the class scope so that base-specifiers are looked
23156 up correctly. */
23157 if (type)
23158 pushclass (type);
23159 bases = cp_parser_base_clause (parser);
23160 /* PR59482: get out of the previously pushed class scope so that the
23161 subsequent pops pop the right thing. */
23162 if (type)
23163 popclass ();
23165 else
23166 bases = NULL_TREE;
23168 /* If we're really defining a class, process the base classes.
23169 If they're invalid, fail. */
23170 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23171 xref_basetypes (type, bases);
23173 done:
23174 /* Leave the scope given by the nested-name-specifier. We will
23175 enter the class scope itself while processing the members. */
23176 if (pushed_scope)
23177 pop_scope (pushed_scope);
23179 if (invalid_explicit_specialization_p)
23181 end_specialization ();
23182 --parser->num_template_parameter_lists;
23185 if (type)
23186 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23187 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23188 CLASSTYPE_FINAL (type) = 1;
23189 out:
23190 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23191 return type;
23194 /* Parse a class-key.
23196 class-key:
23197 class
23198 struct
23199 union
23201 Returns the kind of class-key specified, or none_type to indicate
23202 error. */
23204 static enum tag_types
23205 cp_parser_class_key (cp_parser* parser)
23207 cp_token *token;
23208 enum tag_types tag_type;
23210 /* Look for the class-key. */
23211 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23212 if (!token)
23213 return none_type;
23215 /* Check to see if the TOKEN is a class-key. */
23216 tag_type = cp_parser_token_is_class_key (token);
23217 if (!tag_type)
23218 cp_parser_error (parser, "expected class-key");
23219 return tag_type;
23222 /* Parse a type-parameter-key.
23224 type-parameter-key:
23225 class
23226 typename
23229 static void
23230 cp_parser_type_parameter_key (cp_parser* parser)
23232 /* Look for the type-parameter-key. */
23233 enum tag_types tag_type = none_type;
23234 cp_token *token = cp_lexer_peek_token (parser->lexer);
23235 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23237 cp_lexer_consume_token (parser->lexer);
23238 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23239 /* typename is not allowed in a template template parameter
23240 by the standard until C++17. */
23241 pedwarn (token->location, OPT_Wpedantic,
23242 "ISO C++ forbids typename key in template template parameter;"
23243 " use -std=c++17 or -std=gnu++17");
23245 else
23246 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23248 return;
23251 /* Parse an (optional) member-specification.
23253 member-specification:
23254 member-declaration member-specification [opt]
23255 access-specifier : member-specification [opt] */
23257 static void
23258 cp_parser_member_specification_opt (cp_parser* parser)
23260 while (true)
23262 cp_token *token;
23263 enum rid keyword;
23265 /* Peek at the next token. */
23266 token = cp_lexer_peek_token (parser->lexer);
23267 /* If it's a `}', or EOF then we've seen all the members. */
23268 if (token->type == CPP_CLOSE_BRACE
23269 || token->type == CPP_EOF
23270 || token->type == CPP_PRAGMA_EOL)
23271 break;
23273 /* See if this token is a keyword. */
23274 keyword = token->keyword;
23275 switch (keyword)
23277 case RID_PUBLIC:
23278 case RID_PROTECTED:
23279 case RID_PRIVATE:
23280 /* Consume the access-specifier. */
23281 cp_lexer_consume_token (parser->lexer);
23282 /* Remember which access-specifier is active. */
23283 current_access_specifier = token->u.value;
23284 /* Look for the `:'. */
23285 cp_parser_require (parser, CPP_COLON, RT_COLON);
23286 break;
23288 default:
23289 /* Accept #pragmas at class scope. */
23290 if (token->type == CPP_PRAGMA)
23292 cp_parser_pragma (parser, pragma_member, NULL);
23293 break;
23296 /* Otherwise, the next construction must be a
23297 member-declaration. */
23298 cp_parser_member_declaration (parser);
23303 /* Parse a member-declaration.
23305 member-declaration:
23306 decl-specifier-seq [opt] member-declarator-list [opt] ;
23307 function-definition ; [opt]
23308 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23309 using-declaration
23310 template-declaration
23311 alias-declaration
23313 member-declarator-list:
23314 member-declarator
23315 member-declarator-list , member-declarator
23317 member-declarator:
23318 declarator pure-specifier [opt]
23319 declarator constant-initializer [opt]
23320 identifier [opt] : constant-expression
23322 GNU Extensions:
23324 member-declaration:
23325 __extension__ member-declaration
23327 member-declarator:
23328 declarator attributes [opt] pure-specifier [opt]
23329 declarator attributes [opt] constant-initializer [opt]
23330 identifier [opt] attributes [opt] : constant-expression
23332 C++0x Extensions:
23334 member-declaration:
23335 static_assert-declaration */
23337 static void
23338 cp_parser_member_declaration (cp_parser* parser)
23340 cp_decl_specifier_seq decl_specifiers;
23341 tree prefix_attributes;
23342 tree decl;
23343 int declares_class_or_enum;
23344 bool friend_p;
23345 cp_token *token = NULL;
23346 cp_token *decl_spec_token_start = NULL;
23347 cp_token *initializer_token_start = NULL;
23348 int saved_pedantic;
23349 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23351 /* Check for the `__extension__' keyword. */
23352 if (cp_parser_extension_opt (parser, &saved_pedantic))
23354 /* Recurse. */
23355 cp_parser_member_declaration (parser);
23356 /* Restore the old value of the PEDANTIC flag. */
23357 pedantic = saved_pedantic;
23359 return;
23362 /* Check for a template-declaration. */
23363 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23365 /* An explicit specialization here is an error condition, and we
23366 expect the specialization handler to detect and report this. */
23367 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23368 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23369 cp_parser_explicit_specialization (parser);
23370 else
23371 cp_parser_template_declaration (parser, /*member_p=*/true);
23373 return;
23375 /* Check for a template introduction. */
23376 else if (cp_parser_template_declaration_after_export (parser, true))
23377 return;
23379 /* Check for a using-declaration. */
23380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23382 if (cxx_dialect < cxx11)
23384 /* Parse the using-declaration. */
23385 cp_parser_using_declaration (parser,
23386 /*access_declaration_p=*/false);
23387 return;
23389 else
23391 tree decl;
23392 bool alias_decl_expected;
23393 cp_parser_parse_tentatively (parser);
23394 decl = cp_parser_alias_declaration (parser);
23395 /* Note that if we actually see the '=' token after the
23396 identifier, cp_parser_alias_declaration commits the
23397 tentative parse. In that case, we really expect an
23398 alias-declaration. Otherwise, we expect a using
23399 declaration. */
23400 alias_decl_expected =
23401 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23402 cp_parser_parse_definitely (parser);
23404 if (alias_decl_expected)
23405 finish_member_declaration (decl);
23406 else
23407 cp_parser_using_declaration (parser,
23408 /*access_declaration_p=*/false);
23409 return;
23413 /* Check for @defs. */
23414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23416 tree ivar, member;
23417 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23418 ivar = ivar_chains;
23419 while (ivar)
23421 member = ivar;
23422 ivar = TREE_CHAIN (member);
23423 TREE_CHAIN (member) = NULL_TREE;
23424 finish_member_declaration (member);
23426 return;
23429 /* If the next token is `static_assert' we have a static assertion. */
23430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23432 cp_parser_static_assert (parser, /*member_p=*/true);
23433 return;
23436 parser->colon_corrects_to_scope_p = false;
23438 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23439 goto out;
23441 /* Parse the decl-specifier-seq. */
23442 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23443 cp_parser_decl_specifier_seq (parser,
23444 CP_PARSER_FLAGS_OPTIONAL,
23445 &decl_specifiers,
23446 &declares_class_or_enum);
23447 /* Check for an invalid type-name. */
23448 if (!decl_specifiers.any_type_specifiers_p
23449 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23450 goto out;
23451 /* If there is no declarator, then the decl-specifier-seq should
23452 specify a type. */
23453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23455 /* If there was no decl-specifier-seq, and the next token is a
23456 `;', then we have something like:
23458 struct S { ; };
23460 [class.mem]
23462 Each member-declaration shall declare at least one member
23463 name of the class. */
23464 if (!decl_specifiers.any_specifiers_p)
23466 cp_token *token = cp_lexer_peek_token (parser->lexer);
23467 if (!in_system_header_at (token->location))
23469 gcc_rich_location richloc (token->location);
23470 richloc.add_fixit_remove ();
23471 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23474 else
23476 tree type;
23478 /* See if this declaration is a friend. */
23479 friend_p = cp_parser_friend_p (&decl_specifiers);
23480 /* If there were decl-specifiers, check to see if there was
23481 a class-declaration. */
23482 type = check_tag_decl (&decl_specifiers,
23483 /*explicit_type_instantiation_p=*/false);
23484 /* Nested classes have already been added to the class, but
23485 a `friend' needs to be explicitly registered. */
23486 if (friend_p)
23488 /* If the `friend' keyword was present, the friend must
23489 be introduced with a class-key. */
23490 if (!declares_class_or_enum && cxx_dialect < cxx11)
23491 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23492 "in C++03 a class-key must be used "
23493 "when declaring a friend");
23494 /* In this case:
23496 template <typename T> struct A {
23497 friend struct A<T>::B;
23500 A<T>::B will be represented by a TYPENAME_TYPE, and
23501 therefore not recognized by check_tag_decl. */
23502 if (!type)
23504 type = decl_specifiers.type;
23505 if (type && TREE_CODE (type) == TYPE_DECL)
23506 type = TREE_TYPE (type);
23508 if (!type || !TYPE_P (type))
23509 error_at (decl_spec_token_start->location,
23510 "friend declaration does not name a class or "
23511 "function");
23512 else
23513 make_friend_class (current_class_type, type,
23514 /*complain=*/true);
23516 /* If there is no TYPE, an error message will already have
23517 been issued. */
23518 else if (!type || type == error_mark_node)
23520 /* An anonymous aggregate has to be handled specially; such
23521 a declaration really declares a data member (with a
23522 particular type), as opposed to a nested class. */
23523 else if (ANON_AGGR_TYPE_P (type))
23525 /* C++11 9.5/6. */
23526 if (decl_specifiers.storage_class != sc_none)
23527 error_at (decl_spec_token_start->location,
23528 "a storage class on an anonymous aggregate "
23529 "in class scope is not allowed");
23531 /* Remove constructors and such from TYPE, now that we
23532 know it is an anonymous aggregate. */
23533 fixup_anonymous_aggr (type);
23534 /* And make the corresponding data member. */
23535 decl = build_decl (decl_spec_token_start->location,
23536 FIELD_DECL, NULL_TREE, type);
23537 /* Add it to the class. */
23538 finish_member_declaration (decl);
23540 else
23541 cp_parser_check_access_in_redeclaration
23542 (TYPE_NAME (type),
23543 decl_spec_token_start->location);
23546 else
23548 bool assume_semicolon = false;
23550 /* Clear attributes from the decl_specifiers but keep them
23551 around as prefix attributes that apply them to the entity
23552 being declared. */
23553 prefix_attributes = decl_specifiers.attributes;
23554 decl_specifiers.attributes = NULL_TREE;
23556 /* See if these declarations will be friends. */
23557 friend_p = cp_parser_friend_p (&decl_specifiers);
23559 /* Keep going until we hit the `;' at the end of the
23560 declaration. */
23561 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23563 tree attributes = NULL_TREE;
23564 tree first_attribute;
23565 tree initializer;
23566 bool is_bitfld = false;
23567 bool named_bitfld = false;
23569 /* Peek at the next token. */
23570 token = cp_lexer_peek_token (parser->lexer);
23572 /* The following code wants to know early if it is a bit-field
23573 or some other declaration. Attributes can appear before
23574 the `:' token, but are hopefully rare enough that the
23575 simplicity of the tentative lookup pays off. */
23576 if (cp_next_tokens_can_be_attribute_p (parser)
23577 || (token->type == CPP_NAME
23578 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23579 && (named_bitfld = true)))
23581 cp_parser_parse_tentatively (parser);
23582 if (named_bitfld)
23583 cp_lexer_consume_token (parser->lexer);
23584 cp_parser_attributes_opt (parser);
23585 token = cp_lexer_peek_token (parser->lexer);
23586 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23587 cp_parser_abort_tentative_parse (parser);
23590 /* Check for a bitfield declaration. */
23591 if (is_bitfld
23592 || token->type == CPP_COLON
23593 || (token->type == CPP_NAME
23594 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23595 && (named_bitfld = true)))
23597 tree identifier;
23598 tree width;
23599 tree late_attributes = NULL_TREE;
23601 if (named_bitfld)
23602 identifier = cp_parser_identifier (parser);
23603 else
23604 identifier = NULL_TREE;
23606 /* Look for attributes that apply to the bitfield. */
23607 attributes = cp_parser_attributes_opt (parser);
23609 /* Consume the `:' token. */
23610 cp_lexer_consume_token (parser->lexer);
23612 /* Get the width of the bitfield. */
23613 width = cp_parser_constant_expression (parser, false, NULL,
23614 cxx_dialect >= cxx11);
23616 /* In C++2A and as extension for C++11 and above we allow
23617 default member initializers for bit-fields. */
23618 initializer = NULL_TREE;
23619 if (cxx_dialect >= cxx11
23620 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23621 || cp_lexer_next_token_is (parser->lexer,
23622 CPP_OPEN_BRACE)))
23624 location_t loc
23625 = cp_lexer_peek_token (parser->lexer)->location;
23626 if (cxx_dialect < cxx2a
23627 && !in_system_header_at (loc)
23628 && identifier != NULL_TREE)
23629 pedwarn (loc, 0,
23630 "default member initializers for bit-fields "
23631 "only available with -std=c++2a or "
23632 "-std=gnu++2a");
23634 initializer = cp_parser_save_nsdmi (parser);
23635 if (identifier == NULL_TREE)
23637 error_at (loc, "default member initializer for "
23638 "unnamed bit-field");
23639 initializer = NULL_TREE;
23642 else
23644 /* Look for attributes that apply to the bitfield after
23645 the `:' token and width. This is where GCC used to
23646 parse attributes in the past, pedwarn if there is
23647 a std attribute. */
23648 if (cp_next_tokens_can_be_std_attribute_p (parser))
23649 pedwarn (input_location, OPT_Wpedantic,
23650 "ISO C++ allows bit-field attributes only "
23651 "before the %<:%> token");
23653 late_attributes = cp_parser_attributes_opt (parser);
23656 attributes = chainon (attributes, late_attributes);
23658 /* Remember which attributes are prefix attributes and
23659 which are not. */
23660 first_attribute = attributes;
23661 /* Combine the attributes. */
23662 attributes = chainon (prefix_attributes, attributes);
23664 /* Create the bitfield declaration. */
23665 decl = grokbitfield (identifier
23666 ? make_id_declarator (NULL_TREE,
23667 identifier,
23668 sfk_none)
23669 : NULL,
23670 &decl_specifiers,
23671 width, initializer,
23672 attributes);
23674 else
23676 cp_declarator *declarator;
23677 tree asm_specification;
23678 int ctor_dtor_or_conv_p;
23680 /* Parse the declarator. */
23681 declarator
23682 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23683 &ctor_dtor_or_conv_p,
23684 /*parenthesized_p=*/NULL,
23685 /*member_p=*/true,
23686 friend_p);
23688 /* If something went wrong parsing the declarator, make sure
23689 that we at least consume some tokens. */
23690 if (declarator == cp_error_declarator)
23692 /* Skip to the end of the statement. */
23693 cp_parser_skip_to_end_of_statement (parser);
23694 /* If the next token is not a semicolon, that is
23695 probably because we just skipped over the body of
23696 a function. So, we consume a semicolon if
23697 present, but do not issue an error message if it
23698 is not present. */
23699 if (cp_lexer_next_token_is (parser->lexer,
23700 CPP_SEMICOLON))
23701 cp_lexer_consume_token (parser->lexer);
23702 goto out;
23705 if (declares_class_or_enum & 2)
23706 cp_parser_check_for_definition_in_return_type
23707 (declarator, decl_specifiers.type,
23708 decl_specifiers.locations[ds_type_spec]);
23710 /* Look for an asm-specification. */
23711 asm_specification = cp_parser_asm_specification_opt (parser);
23712 /* Look for attributes that apply to the declaration. */
23713 attributes = cp_parser_attributes_opt (parser);
23714 /* Remember which attributes are prefix attributes and
23715 which are not. */
23716 first_attribute = attributes;
23717 /* Combine the attributes. */
23718 attributes = chainon (prefix_attributes, attributes);
23720 /* If it's an `=', then we have a constant-initializer or a
23721 pure-specifier. It is not correct to parse the
23722 initializer before registering the member declaration
23723 since the member declaration should be in scope while
23724 its initializer is processed. However, the rest of the
23725 front end does not yet provide an interface that allows
23726 us to handle this correctly. */
23727 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23729 /* In [class.mem]:
23731 A pure-specifier shall be used only in the declaration of
23732 a virtual function.
23734 A member-declarator can contain a constant-initializer
23735 only if it declares a static member of integral or
23736 enumeration type.
23738 Therefore, if the DECLARATOR is for a function, we look
23739 for a pure-specifier; otherwise, we look for a
23740 constant-initializer. When we call `grokfield', it will
23741 perform more stringent semantics checks. */
23742 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23743 if (function_declarator_p (declarator)
23744 || (decl_specifiers.type
23745 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23746 && declarator->kind == cdk_id
23747 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23748 == FUNCTION_TYPE)))
23749 initializer = cp_parser_pure_specifier (parser);
23750 else if (decl_specifiers.storage_class != sc_static)
23751 initializer = cp_parser_save_nsdmi (parser);
23752 else if (cxx_dialect >= cxx11)
23754 bool nonconst;
23755 /* Don't require a constant rvalue in C++11, since we
23756 might want a reference constant. We'll enforce
23757 constancy later. */
23758 cp_lexer_consume_token (parser->lexer);
23759 /* Parse the initializer. */
23760 initializer = cp_parser_initializer_clause (parser,
23761 &nonconst);
23763 else
23764 /* Parse the initializer. */
23765 initializer = cp_parser_constant_initializer (parser);
23767 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23768 && !function_declarator_p (declarator))
23770 bool x;
23771 if (decl_specifiers.storage_class != sc_static)
23772 initializer = cp_parser_save_nsdmi (parser);
23773 else
23774 initializer = cp_parser_initializer (parser, &x, &x);
23776 /* Otherwise, there is no initializer. */
23777 else
23778 initializer = NULL_TREE;
23780 /* See if we are probably looking at a function
23781 definition. We are certainly not looking at a
23782 member-declarator. Calling `grokfield' has
23783 side-effects, so we must not do it unless we are sure
23784 that we are looking at a member-declarator. */
23785 if (cp_parser_token_starts_function_definition_p
23786 (cp_lexer_peek_token (parser->lexer)))
23788 /* The grammar does not allow a pure-specifier to be
23789 used when a member function is defined. (It is
23790 possible that this fact is an oversight in the
23791 standard, since a pure function may be defined
23792 outside of the class-specifier. */
23793 if (initializer && initializer_token_start)
23794 error_at (initializer_token_start->location,
23795 "pure-specifier on function-definition");
23796 decl = cp_parser_save_member_function_body (parser,
23797 &decl_specifiers,
23798 declarator,
23799 attributes);
23800 if (parser->fully_implicit_function_template_p)
23801 decl = finish_fully_implicit_template (parser, decl);
23802 /* If the member was not a friend, declare it here. */
23803 if (!friend_p)
23804 finish_member_declaration (decl);
23805 /* Peek at the next token. */
23806 token = cp_lexer_peek_token (parser->lexer);
23807 /* If the next token is a semicolon, consume it. */
23808 if (token->type == CPP_SEMICOLON)
23810 location_t semicolon_loc
23811 = cp_lexer_consume_token (parser->lexer)->location;
23812 gcc_rich_location richloc (semicolon_loc);
23813 richloc.add_fixit_remove ();
23814 warning_at (&richloc, OPT_Wextra_semi,
23815 "extra %<;%> after in-class "
23816 "function definition");
23818 goto out;
23820 else
23821 if (declarator->kind == cdk_function)
23822 declarator->id_loc = token->location;
23823 /* Create the declaration. */
23824 decl = grokfield (declarator, &decl_specifiers,
23825 initializer, /*init_const_expr_p=*/true,
23826 asm_specification, attributes);
23827 if (parser->fully_implicit_function_template_p)
23829 if (friend_p)
23830 finish_fully_implicit_template (parser, 0);
23831 else
23832 decl = finish_fully_implicit_template (parser, decl);
23836 cp_finalize_omp_declare_simd (parser, decl);
23837 cp_finalize_oacc_routine (parser, decl, false);
23839 /* Reset PREFIX_ATTRIBUTES. */
23840 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23841 attributes = TREE_CHAIN (attributes);
23842 if (attributes)
23843 TREE_CHAIN (attributes) = NULL_TREE;
23845 /* If there is any qualification still in effect, clear it
23846 now; we will be starting fresh with the next declarator. */
23847 parser->scope = NULL_TREE;
23848 parser->qualifying_scope = NULL_TREE;
23849 parser->object_scope = NULL_TREE;
23850 /* If it's a `,', then there are more declarators. */
23851 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23853 cp_lexer_consume_token (parser->lexer);
23854 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23856 cp_token *token = cp_lexer_previous_token (parser->lexer);
23857 gcc_rich_location richloc (token->location);
23858 richloc.add_fixit_remove ();
23859 error_at (&richloc, "stray %<,%> at end of "
23860 "member declaration");
23863 /* If the next token isn't a `;', then we have a parse error. */
23864 else if (cp_lexer_next_token_is_not (parser->lexer,
23865 CPP_SEMICOLON))
23867 /* The next token might be a ways away from where the
23868 actual semicolon is missing. Find the previous token
23869 and use that for our error position. */
23870 cp_token *token = cp_lexer_previous_token (parser->lexer);
23871 gcc_rich_location richloc (token->location);
23872 richloc.add_fixit_insert_after (";");
23873 error_at (&richloc, "expected %<;%> at end of "
23874 "member declaration");
23876 /* Assume that the user meant to provide a semicolon. If
23877 we were to cp_parser_skip_to_end_of_statement, we might
23878 skip to a semicolon inside a member function definition
23879 and issue nonsensical error messages. */
23880 assume_semicolon = true;
23883 if (decl)
23885 /* Add DECL to the list of members. */
23886 if (!friend_p
23887 /* Explicitly include, eg, NSDMIs, for better error
23888 recovery (c++/58650). */
23889 || !DECL_DECLARES_FUNCTION_P (decl))
23890 finish_member_declaration (decl);
23892 if (TREE_CODE (decl) == FUNCTION_DECL)
23893 cp_parser_save_default_args (parser, decl);
23894 else if (TREE_CODE (decl) == FIELD_DECL
23895 && DECL_INITIAL (decl))
23896 /* Add DECL to the queue of NSDMI to be parsed later. */
23897 vec_safe_push (unparsed_nsdmis, decl);
23900 if (assume_semicolon)
23901 goto out;
23905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23906 out:
23907 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23910 /* Parse a pure-specifier.
23912 pure-specifier:
23915 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23916 Otherwise, ERROR_MARK_NODE is returned. */
23918 static tree
23919 cp_parser_pure_specifier (cp_parser* parser)
23921 cp_token *token;
23923 /* Look for the `=' token. */
23924 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23925 return error_mark_node;
23926 /* Look for the `0' token. */
23927 token = cp_lexer_peek_token (parser->lexer);
23929 if (token->type == CPP_EOF
23930 || token->type == CPP_PRAGMA_EOL)
23931 return error_mark_node;
23933 cp_lexer_consume_token (parser->lexer);
23935 /* Accept = default or = delete in c++0x mode. */
23936 if (token->keyword == RID_DEFAULT
23937 || token->keyword == RID_DELETE)
23939 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23940 return token->u.value;
23943 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23944 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23946 cp_parser_error (parser,
23947 "invalid pure specifier (only %<= 0%> is allowed)");
23948 cp_parser_skip_to_end_of_statement (parser);
23949 return error_mark_node;
23951 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23953 error_at (token->location, "templates may not be %<virtual%>");
23954 return error_mark_node;
23957 return integer_zero_node;
23960 /* Parse a constant-initializer.
23962 constant-initializer:
23963 = constant-expression
23965 Returns a representation of the constant-expression. */
23967 static tree
23968 cp_parser_constant_initializer (cp_parser* parser)
23970 /* Look for the `=' token. */
23971 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23972 return error_mark_node;
23974 /* It is invalid to write:
23976 struct S { static const int i = { 7 }; };
23979 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23981 cp_parser_error (parser,
23982 "a brace-enclosed initializer is not allowed here");
23983 /* Consume the opening brace. */
23984 matching_braces braces;
23985 braces.consume_open (parser);
23986 /* Skip the initializer. */
23987 cp_parser_skip_to_closing_brace (parser);
23988 /* Look for the trailing `}'. */
23989 braces.require_close (parser);
23991 return error_mark_node;
23994 return cp_parser_constant_expression (parser);
23997 /* Derived classes [gram.class.derived] */
23999 /* Parse a base-clause.
24001 base-clause:
24002 : base-specifier-list
24004 base-specifier-list:
24005 base-specifier ... [opt]
24006 base-specifier-list , base-specifier ... [opt]
24008 Returns a TREE_LIST representing the base-classes, in the order in
24009 which they were declared. The representation of each node is as
24010 described by cp_parser_base_specifier.
24012 In the case that no bases are specified, this function will return
24013 NULL_TREE, not ERROR_MARK_NODE. */
24015 static tree
24016 cp_parser_base_clause (cp_parser* parser)
24018 tree bases = NULL_TREE;
24020 /* Look for the `:' that begins the list. */
24021 cp_parser_require (parser, CPP_COLON, RT_COLON);
24023 /* Scan the base-specifier-list. */
24024 while (true)
24026 cp_token *token;
24027 tree base;
24028 bool pack_expansion_p = false;
24030 /* Look for the base-specifier. */
24031 base = cp_parser_base_specifier (parser);
24032 /* Look for the (optional) ellipsis. */
24033 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24035 /* Consume the `...'. */
24036 cp_lexer_consume_token (parser->lexer);
24038 pack_expansion_p = true;
24041 /* Add BASE to the front of the list. */
24042 if (base && base != error_mark_node)
24044 if (pack_expansion_p)
24045 /* Make this a pack expansion type. */
24046 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24048 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24050 TREE_CHAIN (base) = bases;
24051 bases = base;
24054 /* Peek at the next token. */
24055 token = cp_lexer_peek_token (parser->lexer);
24056 /* If it's not a comma, then the list is complete. */
24057 if (token->type != CPP_COMMA)
24058 break;
24059 /* Consume the `,'. */
24060 cp_lexer_consume_token (parser->lexer);
24063 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24064 base class had a qualified name. However, the next name that
24065 appears is certainly not qualified. */
24066 parser->scope = NULL_TREE;
24067 parser->qualifying_scope = NULL_TREE;
24068 parser->object_scope = NULL_TREE;
24070 return nreverse (bases);
24073 /* Parse a base-specifier.
24075 base-specifier:
24076 :: [opt] nested-name-specifier [opt] class-name
24077 virtual access-specifier [opt] :: [opt] nested-name-specifier
24078 [opt] class-name
24079 access-specifier virtual [opt] :: [opt] nested-name-specifier
24080 [opt] class-name
24082 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24083 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24084 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24085 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24087 static tree
24088 cp_parser_base_specifier (cp_parser* parser)
24090 cp_token *token;
24091 bool done = false;
24092 bool virtual_p = false;
24093 bool duplicate_virtual_error_issued_p = false;
24094 bool duplicate_access_error_issued_p = false;
24095 bool class_scope_p, template_p;
24096 tree access = access_default_node;
24097 tree type;
24099 /* Process the optional `virtual' and `access-specifier'. */
24100 while (!done)
24102 /* Peek at the next token. */
24103 token = cp_lexer_peek_token (parser->lexer);
24104 /* Process `virtual'. */
24105 switch (token->keyword)
24107 case RID_VIRTUAL:
24108 /* If `virtual' appears more than once, issue an error. */
24109 if (virtual_p && !duplicate_virtual_error_issued_p)
24111 cp_parser_error (parser,
24112 "%<virtual%> specified more than once in base-specifier");
24113 duplicate_virtual_error_issued_p = true;
24116 virtual_p = true;
24118 /* Consume the `virtual' token. */
24119 cp_lexer_consume_token (parser->lexer);
24121 break;
24123 case RID_PUBLIC:
24124 case RID_PROTECTED:
24125 case RID_PRIVATE:
24126 /* If more than one access specifier appears, issue an
24127 error. */
24128 if (access != access_default_node
24129 && !duplicate_access_error_issued_p)
24131 cp_parser_error (parser,
24132 "more than one access specifier in base-specifier");
24133 duplicate_access_error_issued_p = true;
24136 access = ridpointers[(int) token->keyword];
24138 /* Consume the access-specifier. */
24139 cp_lexer_consume_token (parser->lexer);
24141 break;
24143 default:
24144 done = true;
24145 break;
24148 /* It is not uncommon to see programs mechanically, erroneously, use
24149 the 'typename' keyword to denote (dependent) qualified types
24150 as base classes. */
24151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24153 token = cp_lexer_peek_token (parser->lexer);
24154 if (!processing_template_decl)
24155 error_at (token->location,
24156 "keyword %<typename%> not allowed outside of templates");
24157 else
24158 error_at (token->location,
24159 "keyword %<typename%> not allowed in this context "
24160 "(the base class is implicitly a type)");
24161 cp_lexer_consume_token (parser->lexer);
24164 /* Look for the optional `::' operator. */
24165 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24166 /* Look for the nested-name-specifier. The simplest way to
24167 implement:
24169 [temp.res]
24171 The keyword `typename' is not permitted in a base-specifier or
24172 mem-initializer; in these contexts a qualified name that
24173 depends on a template-parameter is implicitly assumed to be a
24174 type name.
24176 is to pretend that we have seen the `typename' keyword at this
24177 point. */
24178 cp_parser_nested_name_specifier_opt (parser,
24179 /*typename_keyword_p=*/true,
24180 /*check_dependency_p=*/true,
24181 /*type_p=*/true,
24182 /*is_declaration=*/true);
24183 /* If the base class is given by a qualified name, assume that names
24184 we see are type names or templates, as appropriate. */
24185 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24186 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24188 if (!parser->scope
24189 && cp_lexer_next_token_is_decltype (parser->lexer))
24190 /* DR 950 allows decltype as a base-specifier. */
24191 type = cp_parser_decltype (parser);
24192 else
24194 /* Otherwise, look for the class-name. */
24195 type = cp_parser_class_name (parser,
24196 class_scope_p,
24197 template_p,
24198 typename_type,
24199 /*check_dependency_p=*/true,
24200 /*class_head_p=*/false,
24201 /*is_declaration=*/true);
24202 type = TREE_TYPE (type);
24205 if (type == error_mark_node)
24206 return error_mark_node;
24208 return finish_base_specifier (type, access, virtual_p);
24211 /* Exception handling [gram.exception] */
24213 /* Parse an (optional) noexcept-specification.
24215 noexcept-specification:
24216 noexcept ( constant-expression ) [opt]
24218 If no noexcept-specification is present, returns NULL_TREE.
24219 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24220 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24221 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24222 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24223 in which case a boolean condition is returned instead. */
24225 static tree
24226 cp_parser_noexcept_specification_opt (cp_parser* parser,
24227 bool require_constexpr,
24228 bool* consumed_expr,
24229 bool return_cond)
24231 cp_token *token;
24232 const char *saved_message;
24234 /* Peek at the next token. */
24235 token = cp_lexer_peek_token (parser->lexer);
24237 /* Is it a noexcept-specification? */
24238 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24240 tree expr;
24241 cp_lexer_consume_token (parser->lexer);
24243 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24245 matching_parens parens;
24246 parens.consume_open (parser);
24248 if (require_constexpr)
24250 /* Types may not be defined in an exception-specification. */
24251 saved_message = parser->type_definition_forbidden_message;
24252 parser->type_definition_forbidden_message
24253 = G_("types may not be defined in an exception-specification");
24255 expr = cp_parser_constant_expression (parser);
24257 /* Restore the saved message. */
24258 parser->type_definition_forbidden_message = saved_message;
24260 else
24262 expr = cp_parser_expression (parser);
24263 *consumed_expr = true;
24266 parens.require_close (parser);
24268 else
24270 expr = boolean_true_node;
24271 if (!require_constexpr)
24272 *consumed_expr = false;
24275 /* We cannot build a noexcept-spec right away because this will check
24276 that expr is a constexpr. */
24277 if (!return_cond)
24278 return build_noexcept_spec (expr, tf_warning_or_error);
24279 else
24280 return expr;
24282 else
24283 return NULL_TREE;
24286 /* Parse an (optional) exception-specification.
24288 exception-specification:
24289 throw ( type-id-list [opt] )
24291 Returns a TREE_LIST representing the exception-specification. The
24292 TREE_VALUE of each node is a type. */
24294 static tree
24295 cp_parser_exception_specification_opt (cp_parser* parser)
24297 cp_token *token;
24298 tree type_id_list;
24299 const char *saved_message;
24301 /* Peek at the next token. */
24302 token = cp_lexer_peek_token (parser->lexer);
24304 /* Is it a noexcept-specification? */
24305 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24306 false);
24307 if (type_id_list != NULL_TREE)
24308 return type_id_list;
24310 /* If it's not `throw', then there's no exception-specification. */
24311 if (!cp_parser_is_keyword (token, RID_THROW))
24312 return NULL_TREE;
24314 location_t loc = token->location;
24316 /* Consume the `throw'. */
24317 cp_lexer_consume_token (parser->lexer);
24319 /* Look for the `('. */
24320 matching_parens parens;
24321 parens.require_open (parser);
24323 /* Peek at the next token. */
24324 token = cp_lexer_peek_token (parser->lexer);
24325 /* If it's not a `)', then there is a type-id-list. */
24326 if (token->type != CPP_CLOSE_PAREN)
24328 /* Types may not be defined in an exception-specification. */
24329 saved_message = parser->type_definition_forbidden_message;
24330 parser->type_definition_forbidden_message
24331 = G_("types may not be defined in an exception-specification");
24332 /* Parse the type-id-list. */
24333 type_id_list = cp_parser_type_id_list (parser);
24334 /* Restore the saved message. */
24335 parser->type_definition_forbidden_message = saved_message;
24337 if (cxx_dialect >= cxx17)
24339 error_at (loc, "ISO C++17 does not allow dynamic exception "
24340 "specifications");
24341 type_id_list = NULL_TREE;
24343 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24344 warning_at (loc, OPT_Wdeprecated,
24345 "dynamic exception specifications are deprecated in "
24346 "C++11");
24348 /* In C++17, throw() is equivalent to noexcept (true). throw()
24349 is deprecated in C++11 and above as well, but is still widely used,
24350 so don't warn about it yet. */
24351 else if (cxx_dialect >= cxx17)
24352 type_id_list = noexcept_true_spec;
24353 else
24354 type_id_list = empty_except_spec;
24356 /* Look for the `)'. */
24357 parens.require_close (parser);
24359 return type_id_list;
24362 /* Parse an (optional) type-id-list.
24364 type-id-list:
24365 type-id ... [opt]
24366 type-id-list , type-id ... [opt]
24368 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24369 in the order that the types were presented. */
24371 static tree
24372 cp_parser_type_id_list (cp_parser* parser)
24374 tree types = NULL_TREE;
24376 while (true)
24378 cp_token *token;
24379 tree type;
24381 token = cp_lexer_peek_token (parser->lexer);
24383 /* Get the next type-id. */
24384 type = cp_parser_type_id (parser);
24385 /* Check for invalid 'auto'. */
24386 if (flag_concepts && type_uses_auto (type))
24388 error_at (token->location,
24389 "invalid use of %<auto%> in exception-specification");
24390 type = error_mark_node;
24392 /* Parse the optional ellipsis. */
24393 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24395 /* Consume the `...'. */
24396 cp_lexer_consume_token (parser->lexer);
24398 /* Turn the type into a pack expansion expression. */
24399 type = make_pack_expansion (type);
24401 /* Add it to the list. */
24402 types = add_exception_specifier (types, type, /*complain=*/1);
24403 /* Peek at the next token. */
24404 token = cp_lexer_peek_token (parser->lexer);
24405 /* If it is not a `,', we are done. */
24406 if (token->type != CPP_COMMA)
24407 break;
24408 /* Consume the `,'. */
24409 cp_lexer_consume_token (parser->lexer);
24412 return nreverse (types);
24415 /* Parse a try-block.
24417 try-block:
24418 try compound-statement handler-seq */
24420 static tree
24421 cp_parser_try_block (cp_parser* parser)
24423 tree try_block;
24425 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24426 if (parser->in_function_body
24427 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24428 error ("%<try%> in %<constexpr%> function");
24430 try_block = begin_try_block ();
24431 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24432 finish_try_block (try_block);
24433 cp_parser_handler_seq (parser);
24434 finish_handler_sequence (try_block);
24436 return try_block;
24439 /* Parse a function-try-block.
24441 function-try-block:
24442 try ctor-initializer [opt] function-body handler-seq */
24444 static void
24445 cp_parser_function_try_block (cp_parser* parser)
24447 tree compound_stmt;
24448 tree try_block;
24450 /* Look for the `try' keyword. */
24451 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24452 return;
24453 /* Let the rest of the front end know where we are. */
24454 try_block = begin_function_try_block (&compound_stmt);
24455 /* Parse the function-body. */
24456 cp_parser_ctor_initializer_opt_and_function_body
24457 (parser, /*in_function_try_block=*/true);
24458 /* We're done with the `try' part. */
24459 finish_function_try_block (try_block);
24460 /* Parse the handlers. */
24461 cp_parser_handler_seq (parser);
24462 /* We're done with the handlers. */
24463 finish_function_handler_sequence (try_block, compound_stmt);
24466 /* Parse a handler-seq.
24468 handler-seq:
24469 handler handler-seq [opt] */
24471 static void
24472 cp_parser_handler_seq (cp_parser* parser)
24474 while (true)
24476 cp_token *token;
24478 /* Parse the handler. */
24479 cp_parser_handler (parser);
24480 /* Peek at the next token. */
24481 token = cp_lexer_peek_token (parser->lexer);
24482 /* If it's not `catch' then there are no more handlers. */
24483 if (!cp_parser_is_keyword (token, RID_CATCH))
24484 break;
24488 /* Parse a handler.
24490 handler:
24491 catch ( exception-declaration ) compound-statement */
24493 static void
24494 cp_parser_handler (cp_parser* parser)
24496 tree handler;
24497 tree declaration;
24499 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24500 handler = begin_handler ();
24501 matching_parens parens;
24502 parens.require_open (parser);
24503 declaration = cp_parser_exception_declaration (parser);
24504 finish_handler_parms (declaration, handler);
24505 parens.require_close (parser);
24506 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24507 finish_handler (handler);
24510 /* Parse an exception-declaration.
24512 exception-declaration:
24513 type-specifier-seq declarator
24514 type-specifier-seq abstract-declarator
24515 type-specifier-seq
24518 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24519 ellipsis variant is used. */
24521 static tree
24522 cp_parser_exception_declaration (cp_parser* parser)
24524 cp_decl_specifier_seq type_specifiers;
24525 cp_declarator *declarator;
24526 const char *saved_message;
24528 /* If it's an ellipsis, it's easy to handle. */
24529 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24531 /* Consume the `...' token. */
24532 cp_lexer_consume_token (parser->lexer);
24533 return NULL_TREE;
24536 /* Types may not be defined in exception-declarations. */
24537 saved_message = parser->type_definition_forbidden_message;
24538 parser->type_definition_forbidden_message
24539 = G_("types may not be defined in exception-declarations");
24541 /* Parse the type-specifier-seq. */
24542 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24543 /*is_trailing_return=*/false,
24544 &type_specifiers);
24545 /* If it's a `)', then there is no declarator. */
24546 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24547 declarator = NULL;
24548 else
24549 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24550 /*ctor_dtor_or_conv_p=*/NULL,
24551 /*parenthesized_p=*/NULL,
24552 /*member_p=*/false,
24553 /*friend_p=*/false);
24555 /* Restore the saved message. */
24556 parser->type_definition_forbidden_message = saved_message;
24558 if (!type_specifiers.any_specifiers_p)
24559 return error_mark_node;
24561 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24564 /* Parse a throw-expression.
24566 throw-expression:
24567 throw assignment-expression [opt]
24569 Returns a THROW_EXPR representing the throw-expression. */
24571 static tree
24572 cp_parser_throw_expression (cp_parser* parser)
24574 tree expression;
24575 cp_token* token;
24577 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24578 token = cp_lexer_peek_token (parser->lexer);
24579 /* Figure out whether or not there is an assignment-expression
24580 following the "throw" keyword. */
24581 if (token->type == CPP_COMMA
24582 || token->type == CPP_SEMICOLON
24583 || token->type == CPP_CLOSE_PAREN
24584 || token->type == CPP_CLOSE_SQUARE
24585 || token->type == CPP_CLOSE_BRACE
24586 || token->type == CPP_COLON)
24587 expression = NULL_TREE;
24588 else
24589 expression = cp_parser_assignment_expression (parser);
24591 return build_throw (expression);
24594 /* GNU Extensions */
24596 /* Parse an (optional) asm-specification.
24598 asm-specification:
24599 asm ( string-literal )
24601 If the asm-specification is present, returns a STRING_CST
24602 corresponding to the string-literal. Otherwise, returns
24603 NULL_TREE. */
24605 static tree
24606 cp_parser_asm_specification_opt (cp_parser* parser)
24608 cp_token *token;
24609 tree asm_specification;
24611 /* Peek at the next token. */
24612 token = cp_lexer_peek_token (parser->lexer);
24613 /* If the next token isn't the `asm' keyword, then there's no
24614 asm-specification. */
24615 if (!cp_parser_is_keyword (token, RID_ASM))
24616 return NULL_TREE;
24618 /* Consume the `asm' token. */
24619 cp_lexer_consume_token (parser->lexer);
24620 /* Look for the `('. */
24621 matching_parens parens;
24622 parens.require_open (parser);
24624 /* Look for the string-literal. */
24625 asm_specification = cp_parser_string_literal (parser, false, false);
24627 /* Look for the `)'. */
24628 parens.require_close (parser);
24630 return asm_specification;
24633 /* Parse an asm-operand-list.
24635 asm-operand-list:
24636 asm-operand
24637 asm-operand-list , asm-operand
24639 asm-operand:
24640 string-literal ( expression )
24641 [ string-literal ] string-literal ( expression )
24643 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24644 each node is the expression. The TREE_PURPOSE is itself a
24645 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24646 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24647 is a STRING_CST for the string literal before the parenthesis. Returns
24648 ERROR_MARK_NODE if any of the operands are invalid. */
24650 static tree
24651 cp_parser_asm_operand_list (cp_parser* parser)
24653 tree asm_operands = NULL_TREE;
24654 bool invalid_operands = false;
24656 while (true)
24658 tree string_literal;
24659 tree expression;
24660 tree name;
24662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24664 /* Consume the `[' token. */
24665 cp_lexer_consume_token (parser->lexer);
24666 /* Read the operand name. */
24667 name = cp_parser_identifier (parser);
24668 if (name != error_mark_node)
24669 name = build_string (IDENTIFIER_LENGTH (name),
24670 IDENTIFIER_POINTER (name));
24671 /* Look for the closing `]'. */
24672 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24674 else
24675 name = NULL_TREE;
24676 /* Look for the string-literal. */
24677 string_literal = cp_parser_string_literal (parser, false, false);
24679 /* Look for the `('. */
24680 matching_parens parens;
24681 parens.require_open (parser);
24682 /* Parse the expression. */
24683 expression = cp_parser_expression (parser);
24684 /* Look for the `)'. */
24685 parens.require_close (parser);
24687 if (name == error_mark_node
24688 || string_literal == error_mark_node
24689 || expression == error_mark_node)
24690 invalid_operands = true;
24692 /* Add this operand to the list. */
24693 asm_operands = tree_cons (build_tree_list (name, string_literal),
24694 expression,
24695 asm_operands);
24696 /* If the next token is not a `,', there are no more
24697 operands. */
24698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24699 break;
24700 /* Consume the `,'. */
24701 cp_lexer_consume_token (parser->lexer);
24704 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24707 /* Parse an asm-clobber-list.
24709 asm-clobber-list:
24710 string-literal
24711 asm-clobber-list , string-literal
24713 Returns a TREE_LIST, indicating the clobbers in the order that they
24714 appeared. The TREE_VALUE of each node is a STRING_CST. */
24716 static tree
24717 cp_parser_asm_clobber_list (cp_parser* parser)
24719 tree clobbers = NULL_TREE;
24721 while (true)
24723 tree string_literal;
24725 /* Look for the string literal. */
24726 string_literal = cp_parser_string_literal (parser, false, false);
24727 /* Add it to the list. */
24728 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24729 /* If the next token is not a `,', then the list is
24730 complete. */
24731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24732 break;
24733 /* Consume the `,' token. */
24734 cp_lexer_consume_token (parser->lexer);
24737 return clobbers;
24740 /* Parse an asm-label-list.
24742 asm-label-list:
24743 identifier
24744 asm-label-list , identifier
24746 Returns a TREE_LIST, indicating the labels in the order that they
24747 appeared. The TREE_VALUE of each node is a label. */
24749 static tree
24750 cp_parser_asm_label_list (cp_parser* parser)
24752 tree labels = NULL_TREE;
24754 while (true)
24756 tree identifier, label, name;
24758 /* Look for the identifier. */
24759 identifier = cp_parser_identifier (parser);
24760 if (!error_operand_p (identifier))
24762 label = lookup_label (identifier);
24763 if (TREE_CODE (label) == LABEL_DECL)
24765 TREE_USED (label) = 1;
24766 check_goto (label);
24767 name = build_string (IDENTIFIER_LENGTH (identifier),
24768 IDENTIFIER_POINTER (identifier));
24769 labels = tree_cons (name, label, labels);
24772 /* If the next token is not a `,', then the list is
24773 complete. */
24774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24775 break;
24776 /* Consume the `,' token. */
24777 cp_lexer_consume_token (parser->lexer);
24780 return nreverse (labels);
24783 /* Return TRUE iff the next tokens in the stream are possibly the
24784 beginning of a GNU extension attribute. */
24786 static bool
24787 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24789 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24792 /* Return TRUE iff the next tokens in the stream are possibly the
24793 beginning of a standard C++-11 attribute specifier. */
24795 static bool
24796 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24798 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24801 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24802 beginning of a standard C++-11 attribute specifier. */
24804 static bool
24805 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24807 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24809 return (cxx_dialect >= cxx11
24810 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24811 || (token->type == CPP_OPEN_SQUARE
24812 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24813 && token->type == CPP_OPEN_SQUARE)));
24816 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24817 beginning of a GNU extension attribute. */
24819 static bool
24820 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24822 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24824 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24827 /* Return true iff the next tokens can be the beginning of either a
24828 GNU attribute list, or a standard C++11 attribute sequence. */
24830 static bool
24831 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24833 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24834 || cp_next_tokens_can_be_std_attribute_p (parser));
24837 /* Return true iff the next Nth tokens can be the beginning of either
24838 a GNU attribute list, or a standard C++11 attribute sequence. */
24840 static bool
24841 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24843 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24844 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24847 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24848 of GNU attributes, or return NULL. */
24850 static tree
24851 cp_parser_attributes_opt (cp_parser *parser)
24853 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24854 return cp_parser_gnu_attributes_opt (parser);
24855 return cp_parser_std_attribute_spec_seq (parser);
24858 /* Parse an (optional) series of attributes.
24860 attributes:
24861 attributes attribute
24863 attribute:
24864 __attribute__ (( attribute-list [opt] ))
24866 The return value is as for cp_parser_gnu_attribute_list. */
24868 static tree
24869 cp_parser_gnu_attributes_opt (cp_parser* parser)
24871 tree attributes = NULL_TREE;
24873 while (true)
24875 cp_token *token;
24876 tree attribute_list;
24877 bool ok = true;
24879 /* Peek at the next token. */
24880 token = cp_lexer_peek_token (parser->lexer);
24881 /* If it's not `__attribute__', then we're done. */
24882 if (token->keyword != RID_ATTRIBUTE)
24883 break;
24885 /* Consume the `__attribute__' keyword. */
24886 cp_lexer_consume_token (parser->lexer);
24887 /* Look for the two `(' tokens. */
24888 matching_parens outer_parens;
24889 outer_parens.require_open (parser);
24890 matching_parens inner_parens;
24891 inner_parens.require_open (parser);
24893 /* Peek at the next token. */
24894 token = cp_lexer_peek_token (parser->lexer);
24895 if (token->type != CPP_CLOSE_PAREN)
24896 /* Parse the attribute-list. */
24897 attribute_list = cp_parser_gnu_attribute_list (parser);
24898 else
24899 /* If the next token is a `)', then there is no attribute
24900 list. */
24901 attribute_list = NULL;
24903 /* Look for the two `)' tokens. */
24904 if (!inner_parens.require_close (parser))
24905 ok = false;
24906 if (!outer_parens.require_close (parser))
24907 ok = false;
24908 if (!ok)
24909 cp_parser_skip_to_end_of_statement (parser);
24911 /* Add these new attributes to the list. */
24912 attributes = chainon (attributes, attribute_list);
24915 return attributes;
24918 /* Parse a GNU attribute-list.
24920 attribute-list:
24921 attribute
24922 attribute-list , attribute
24924 attribute:
24925 identifier
24926 identifier ( identifier )
24927 identifier ( identifier , expression-list )
24928 identifier ( expression-list )
24930 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24931 to an attribute. The TREE_PURPOSE of each node is the identifier
24932 indicating which attribute is in use. The TREE_VALUE represents
24933 the arguments, if any. */
24935 static tree
24936 cp_parser_gnu_attribute_list (cp_parser* parser)
24938 tree attribute_list = NULL_TREE;
24939 bool save_translate_strings_p = parser->translate_strings_p;
24941 parser->translate_strings_p = false;
24942 while (true)
24944 cp_token *token;
24945 tree identifier;
24946 tree attribute;
24948 /* Look for the identifier. We also allow keywords here; for
24949 example `__attribute__ ((const))' is legal. */
24950 token = cp_lexer_peek_token (parser->lexer);
24951 if (token->type == CPP_NAME
24952 || token->type == CPP_KEYWORD)
24954 tree arguments = NULL_TREE;
24956 /* Consume the token, but save it since we need it for the
24957 SIMD enabled function parsing. */
24958 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24960 /* Save away the identifier that indicates which attribute
24961 this is. */
24962 identifier = (token->type == CPP_KEYWORD)
24963 /* For keywords, use the canonical spelling, not the
24964 parsed identifier. */
24965 ? ridpointers[(int) token->keyword]
24966 : id_token->u.value;
24968 identifier = canonicalize_attr_name (identifier);
24969 attribute = build_tree_list (identifier, NULL_TREE);
24971 /* Peek at the next token. */
24972 token = cp_lexer_peek_token (parser->lexer);
24973 /* If it's an `(', then parse the attribute arguments. */
24974 if (token->type == CPP_OPEN_PAREN)
24976 vec<tree, va_gc> *vec;
24977 int attr_flag = (attribute_takes_identifier_p (identifier)
24978 ? id_attr : normal_attr);
24979 vec = cp_parser_parenthesized_expression_list
24980 (parser, attr_flag, /*cast_p=*/false,
24981 /*allow_expansion_p=*/false,
24982 /*non_constant_p=*/NULL);
24983 if (vec == NULL)
24984 arguments = error_mark_node;
24985 else
24987 arguments = build_tree_list_vec (vec);
24988 release_tree_vector (vec);
24990 /* Save the arguments away. */
24991 TREE_VALUE (attribute) = arguments;
24994 if (arguments != error_mark_node)
24996 /* Add this attribute to the list. */
24997 TREE_CHAIN (attribute) = attribute_list;
24998 attribute_list = attribute;
25001 token = cp_lexer_peek_token (parser->lexer);
25003 /* Now, look for more attributes. If the next token isn't a
25004 `,', we're done. */
25005 if (token->type != CPP_COMMA)
25006 break;
25008 /* Consume the comma and keep going. */
25009 cp_lexer_consume_token (parser->lexer);
25011 parser->translate_strings_p = save_translate_strings_p;
25013 /* We built up the list in reverse order. */
25014 return nreverse (attribute_list);
25017 /* Parse a standard C++11 attribute.
25019 The returned representation is a TREE_LIST which TREE_PURPOSE is
25020 the scoped name of the attribute, and the TREE_VALUE is its
25021 arguments list.
25023 Note that the scoped name of the attribute is itself a TREE_LIST
25024 which TREE_PURPOSE is the namespace of the attribute, and
25025 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25026 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25027 and which TREE_PURPOSE is directly the attribute name.
25029 Clients of the attribute code should use get_attribute_namespace
25030 and get_attribute_name to get the actual namespace and name of
25031 attributes, regardless of their being GNU or C++11 attributes.
25033 attribute:
25034 attribute-token attribute-argument-clause [opt]
25036 attribute-token:
25037 identifier
25038 attribute-scoped-token
25040 attribute-scoped-token:
25041 attribute-namespace :: identifier
25043 attribute-namespace:
25044 identifier
25046 attribute-argument-clause:
25047 ( balanced-token-seq )
25049 balanced-token-seq:
25050 balanced-token [opt]
25051 balanced-token-seq balanced-token
25053 balanced-token:
25054 ( balanced-token-seq )
25055 [ balanced-token-seq ]
25056 { balanced-token-seq }. */
25058 static tree
25059 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25061 tree attribute, attr_id = NULL_TREE, arguments;
25062 cp_token *token;
25064 /* First, parse name of the attribute, a.k.a attribute-token. */
25066 token = cp_lexer_peek_token (parser->lexer);
25067 if (token->type == CPP_NAME)
25068 attr_id = token->u.value;
25069 else if (token->type == CPP_KEYWORD)
25070 attr_id = ridpointers[(int) token->keyword];
25071 else if (token->flags & NAMED_OP)
25072 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25074 if (attr_id == NULL_TREE)
25075 return NULL_TREE;
25077 cp_lexer_consume_token (parser->lexer);
25079 token = cp_lexer_peek_token (parser->lexer);
25080 if (token->type == CPP_SCOPE)
25082 /* We are seeing a scoped attribute token. */
25084 cp_lexer_consume_token (parser->lexer);
25085 if (attr_ns)
25086 error_at (token->location, "attribute using prefix used together "
25087 "with scoped attribute token");
25088 attr_ns = attr_id;
25090 token = cp_lexer_consume_token (parser->lexer);
25091 if (token->type == CPP_NAME)
25092 attr_id = token->u.value;
25093 else if (token->type == CPP_KEYWORD)
25094 attr_id = ridpointers[(int) token->keyword];
25095 else if (token->flags & NAMED_OP)
25096 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25097 else
25099 error_at (token->location,
25100 "expected an identifier for the attribute name");
25101 return error_mark_node;
25104 attr_id = canonicalize_attr_name (attr_id);
25105 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25106 NULL_TREE);
25107 token = cp_lexer_peek_token (parser->lexer);
25109 else if (attr_ns)
25110 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25111 NULL_TREE);
25112 else
25114 attr_id = canonicalize_attr_name (attr_id);
25115 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25116 NULL_TREE);
25117 /* C++11 noreturn attribute is equivalent to GNU's. */
25118 if (is_attribute_p ("noreturn", attr_id))
25119 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25120 /* C++14 deprecated attribute is equivalent to GNU's. */
25121 else if (is_attribute_p ("deprecated", attr_id))
25122 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25123 /* C++17 fallthrough attribute is equivalent to GNU's. */
25124 else if (is_attribute_p ("fallthrough", attr_id))
25125 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25126 /* Transactional Memory TS optimize_for_synchronized attribute is
25127 equivalent to GNU transaction_callable. */
25128 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25129 TREE_PURPOSE (attribute)
25130 = get_identifier ("transaction_callable");
25131 /* Transactional Memory attributes are GNU attributes. */
25132 else if (tm_attr_to_mask (attr_id))
25133 TREE_PURPOSE (attribute) = attr_id;
25136 /* Now parse the optional argument clause of the attribute. */
25138 if (token->type != CPP_OPEN_PAREN)
25139 return attribute;
25142 vec<tree, va_gc> *vec;
25143 int attr_flag = normal_attr;
25145 if (attr_ns == get_identifier ("gnu")
25146 && attribute_takes_identifier_p (attr_id))
25147 /* A GNU attribute that takes an identifier in parameter. */
25148 attr_flag = id_attr;
25150 vec = cp_parser_parenthesized_expression_list
25151 (parser, attr_flag, /*cast_p=*/false,
25152 /*allow_expansion_p=*/true,
25153 /*non_constant_p=*/NULL);
25154 if (vec == NULL)
25155 arguments = error_mark_node;
25156 else
25158 arguments = build_tree_list_vec (vec);
25159 release_tree_vector (vec);
25162 if (arguments == error_mark_node)
25163 attribute = error_mark_node;
25164 else
25165 TREE_VALUE (attribute) = arguments;
25168 return attribute;
25171 /* Check that the attribute ATTRIBUTE appears at most once in the
25172 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25173 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25174 isn't implemented yet in GCC. */
25176 static void
25177 cp_parser_check_std_attribute (tree attributes, tree attribute)
25179 if (attributes)
25181 tree name = get_attribute_name (attribute);
25182 if (is_attribute_p ("noreturn", name)
25183 && lookup_attribute ("noreturn", attributes))
25184 error ("attribute %<noreturn%> can appear at most once "
25185 "in an attribute-list");
25186 else if (is_attribute_p ("deprecated", name)
25187 && lookup_attribute ("deprecated", attributes))
25188 error ("attribute %<deprecated%> can appear at most once "
25189 "in an attribute-list");
25193 /* Parse a list of standard C++-11 attributes.
25195 attribute-list:
25196 attribute [opt]
25197 attribute-list , attribute[opt]
25198 attribute ...
25199 attribute-list , attribute ...
25202 static tree
25203 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25205 tree attributes = NULL_TREE, attribute = NULL_TREE;
25206 cp_token *token = NULL;
25208 while (true)
25210 attribute = cp_parser_std_attribute (parser, attr_ns);
25211 if (attribute == error_mark_node)
25212 break;
25213 if (attribute != NULL_TREE)
25215 cp_parser_check_std_attribute (attributes, attribute);
25216 TREE_CHAIN (attribute) = attributes;
25217 attributes = attribute;
25219 token = cp_lexer_peek_token (parser->lexer);
25220 if (token->type == CPP_ELLIPSIS)
25222 cp_lexer_consume_token (parser->lexer);
25223 if (attribute == NULL_TREE)
25224 error_at (token->location,
25225 "expected attribute before %<...%>");
25226 else
25228 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25229 if (pack == error_mark_node)
25230 return error_mark_node;
25231 TREE_VALUE (attribute) = pack;
25233 token = cp_lexer_peek_token (parser->lexer);
25235 if (token->type != CPP_COMMA)
25236 break;
25237 cp_lexer_consume_token (parser->lexer);
25239 attributes = nreverse (attributes);
25240 return attributes;
25243 /* Parse a standard C++-11 attribute specifier.
25245 attribute-specifier:
25246 [ [ attribute-using-prefix [opt] attribute-list ] ]
25247 alignment-specifier
25249 attribute-using-prefix:
25250 using attribute-namespace :
25252 alignment-specifier:
25253 alignas ( type-id ... [opt] )
25254 alignas ( alignment-expression ... [opt] ). */
25256 static tree
25257 cp_parser_std_attribute_spec (cp_parser *parser)
25259 tree attributes = NULL_TREE;
25260 cp_token *token = cp_lexer_peek_token (parser->lexer);
25262 if (token->type == CPP_OPEN_SQUARE
25263 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25265 tree attr_ns = NULL_TREE;
25267 cp_lexer_consume_token (parser->lexer);
25268 cp_lexer_consume_token (parser->lexer);
25270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25272 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25273 if (token->type == CPP_NAME)
25274 attr_ns = token->u.value;
25275 else if (token->type == CPP_KEYWORD)
25276 attr_ns = ridpointers[(int) token->keyword];
25277 else if (token->flags & NAMED_OP)
25278 attr_ns = get_identifier (cpp_type2name (token->type,
25279 token->flags));
25280 if (attr_ns
25281 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25283 if (cxx_dialect < cxx17
25284 && !in_system_header_at (input_location))
25285 pedwarn (input_location, 0,
25286 "attribute using prefix only available "
25287 "with -std=c++17 or -std=gnu++17");
25289 cp_lexer_consume_token (parser->lexer);
25290 cp_lexer_consume_token (parser->lexer);
25291 cp_lexer_consume_token (parser->lexer);
25293 else
25294 attr_ns = NULL_TREE;
25297 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25299 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25300 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25301 cp_parser_skip_to_end_of_statement (parser);
25302 else
25303 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25304 when we are sure that we have actually parsed them. */
25305 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25307 else
25309 tree alignas_expr;
25311 /* Look for an alignment-specifier. */
25313 token = cp_lexer_peek_token (parser->lexer);
25315 if (token->type != CPP_KEYWORD
25316 || token->keyword != RID_ALIGNAS)
25317 return NULL_TREE;
25319 cp_lexer_consume_token (parser->lexer);
25320 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25322 matching_parens parens;
25323 if (!parens.require_open (parser))
25324 return error_mark_node;
25326 cp_parser_parse_tentatively (parser);
25327 alignas_expr = cp_parser_type_id (parser);
25329 if (!cp_parser_parse_definitely (parser))
25331 alignas_expr = cp_parser_assignment_expression (parser);
25332 if (alignas_expr == error_mark_node)
25333 cp_parser_skip_to_end_of_statement (parser);
25334 if (alignas_expr == NULL_TREE
25335 || alignas_expr == error_mark_node)
25336 return alignas_expr;
25339 alignas_expr = cxx_alignas_expr (alignas_expr);
25340 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25342 /* Handle alignas (pack...). */
25343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25345 cp_lexer_consume_token (parser->lexer);
25346 alignas_expr = make_pack_expansion (alignas_expr);
25349 /* Something went wrong, so don't build the attribute. */
25350 if (alignas_expr == error_mark_node)
25351 return error_mark_node;
25353 if (!parens.require_close (parser))
25354 return error_mark_node;
25356 /* Build the C++-11 representation of an 'aligned'
25357 attribute. */
25358 attributes =
25359 build_tree_list (build_tree_list (get_identifier ("gnu"),
25360 get_identifier ("aligned")),
25361 alignas_expr);
25364 return attributes;
25367 /* Parse a standard C++-11 attribute-specifier-seq.
25369 attribute-specifier-seq:
25370 attribute-specifier-seq [opt] attribute-specifier
25373 static tree
25374 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25376 tree attr_specs = NULL_TREE;
25377 tree attr_last = NULL_TREE;
25379 while (true)
25381 tree attr_spec = cp_parser_std_attribute_spec (parser);
25382 if (attr_spec == NULL_TREE)
25383 break;
25384 if (attr_spec == error_mark_node)
25385 return error_mark_node;
25387 if (attr_last)
25388 TREE_CHAIN (attr_last) = attr_spec;
25389 else
25390 attr_specs = attr_last = attr_spec;
25391 attr_last = tree_last (attr_last);
25394 return attr_specs;
25397 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25398 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25399 current value of the PEDANTIC flag, regardless of whether or not
25400 the `__extension__' keyword is present. The caller is responsible
25401 for restoring the value of the PEDANTIC flag. */
25403 static bool
25404 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25406 /* Save the old value of the PEDANTIC flag. */
25407 *saved_pedantic = pedantic;
25409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25411 /* Consume the `__extension__' token. */
25412 cp_lexer_consume_token (parser->lexer);
25413 /* We're not being pedantic while the `__extension__' keyword is
25414 in effect. */
25415 pedantic = 0;
25417 return true;
25420 return false;
25423 /* Parse a label declaration.
25425 label-declaration:
25426 __label__ label-declarator-seq ;
25428 label-declarator-seq:
25429 identifier , label-declarator-seq
25430 identifier */
25432 static void
25433 cp_parser_label_declaration (cp_parser* parser)
25435 /* Look for the `__label__' keyword. */
25436 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25438 while (true)
25440 tree identifier;
25442 /* Look for an identifier. */
25443 identifier = cp_parser_identifier (parser);
25444 /* If we failed, stop. */
25445 if (identifier == error_mark_node)
25446 break;
25447 /* Declare it as a label. */
25448 finish_label_decl (identifier);
25449 /* If the next token is a `;', stop. */
25450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25451 break;
25452 /* Look for the `,' separating the label declarations. */
25453 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25456 /* Look for the final `;'. */
25457 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25460 // -------------------------------------------------------------------------- //
25461 // Requires Clause
25463 // Parse a requires clause.
25465 // requires-clause:
25466 // 'requires' logical-or-expression
25468 // The required logical-or-expression must be a constant expression. Note
25469 // that we don't check that the expression is constepxr here. We defer until
25470 // we analyze constraints and then, we only check atomic constraints.
25471 static tree
25472 cp_parser_requires_clause (cp_parser *parser)
25474 // Parse the requires clause so that it is not automatically folded.
25475 ++processing_template_decl;
25476 tree expr = cp_parser_binary_expression (parser, false, false,
25477 PREC_NOT_OPERATOR, NULL);
25478 if (check_for_bare_parameter_packs (expr))
25479 expr = error_mark_node;
25480 --processing_template_decl;
25481 return expr;
25484 // Optionally parse a requires clause:
25485 static tree
25486 cp_parser_requires_clause_opt (cp_parser *parser)
25488 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25489 if (tok->keyword != RID_REQUIRES)
25491 if (!flag_concepts && tok->type == CPP_NAME
25492 && tok->u.value == ridpointers[RID_REQUIRES])
25494 error_at (cp_lexer_peek_token (parser->lexer)->location,
25495 "%<requires%> only available with -fconcepts");
25496 /* Parse and discard the requires-clause. */
25497 cp_lexer_consume_token (parser->lexer);
25498 cp_parser_requires_clause (parser);
25500 return NULL_TREE;
25502 cp_lexer_consume_token (parser->lexer);
25503 return cp_parser_requires_clause (parser);
25507 /*---------------------------------------------------------------------------
25508 Requires expressions
25509 ---------------------------------------------------------------------------*/
25511 /* Parse a requires expression
25513 requirement-expression:
25514 'requires' requirement-parameter-list [opt] requirement-body */
25515 static tree
25516 cp_parser_requires_expression (cp_parser *parser)
25518 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25519 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25521 /* A requires-expression shall appear only within a concept
25522 definition or a requires-clause.
25524 TODO: Implement this diagnostic correctly. */
25525 if (!processing_template_decl)
25527 error_at (loc, "a requires expression cannot appear outside a template");
25528 cp_parser_skip_to_end_of_statement (parser);
25529 return error_mark_node;
25532 tree parms, reqs;
25534 /* Local parameters are delared as variables within the scope
25535 of the expression. They are not visible past the end of
25536 the expression. Expressions within the requires-expression
25537 are unevaluated. */
25538 struct scope_sentinel
25540 scope_sentinel ()
25542 ++cp_unevaluated_operand;
25543 begin_scope (sk_block, NULL_TREE);
25546 ~scope_sentinel ()
25548 pop_bindings_and_leave_scope ();
25549 --cp_unevaluated_operand;
25551 } s;
25553 /* Parse the optional parameter list. */
25554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25556 parms = cp_parser_requirement_parameter_list (parser);
25557 if (parms == error_mark_node)
25558 return error_mark_node;
25560 else
25561 parms = NULL_TREE;
25563 /* Parse the requirement body. */
25564 reqs = cp_parser_requirement_body (parser);
25565 if (reqs == error_mark_node)
25566 return error_mark_node;
25569 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25570 the parm chain. */
25571 grokparms (parms, &parms);
25572 return finish_requires_expr (parms, reqs);
25575 /* Parse a parameterized requirement.
25577 requirement-parameter-list:
25578 '(' parameter-declaration-clause ')' */
25579 static tree
25580 cp_parser_requirement_parameter_list (cp_parser *parser)
25582 matching_parens parens;
25583 if (!parens.require_open (parser))
25584 return error_mark_node;
25586 tree parms = cp_parser_parameter_declaration_clause (parser);
25588 if (!parens.require_close (parser))
25589 return error_mark_node;
25591 return parms;
25594 /* Parse the body of a requirement.
25596 requirement-body:
25597 '{' requirement-list '}' */
25598 static tree
25599 cp_parser_requirement_body (cp_parser *parser)
25601 matching_braces braces;
25602 if (!braces.require_open (parser))
25603 return error_mark_node;
25605 tree reqs = cp_parser_requirement_list (parser);
25607 if (!braces.require_close (parser))
25608 return error_mark_node;
25610 return reqs;
25613 /* Parse a list of requirements.
25615 requirement-list:
25616 requirement
25617 requirement-list ';' requirement[opt] */
25618 static tree
25619 cp_parser_requirement_list (cp_parser *parser)
25621 tree result = NULL_TREE;
25622 while (true)
25624 tree req = cp_parser_requirement (parser);
25625 if (req == error_mark_node)
25626 return error_mark_node;
25628 result = tree_cons (NULL_TREE, req, result);
25630 /* If we see a semi-colon, consume it. */
25631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25632 cp_lexer_consume_token (parser->lexer);
25634 /* Stop processing at the end of the list. */
25635 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25636 break;
25639 /* Reverse the order of requirements so they are analyzed in
25640 declaration order. */
25641 return nreverse (result);
25644 /* Parse a syntactic requirement or type requirement.
25646 requirement:
25647 simple-requirement
25648 compound-requirement
25649 type-requirement
25650 nested-requirement */
25651 static tree
25652 cp_parser_requirement (cp_parser *parser)
25654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25655 return cp_parser_compound_requirement (parser);
25656 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25657 return cp_parser_type_requirement (parser);
25658 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25659 return cp_parser_nested_requirement (parser);
25660 else
25661 return cp_parser_simple_requirement (parser);
25664 /* Parse a simple requirement.
25666 simple-requirement:
25667 expression ';' */
25668 static tree
25669 cp_parser_simple_requirement (cp_parser *parser)
25671 tree expr = cp_parser_expression (parser, NULL, false, false);
25672 if (!expr || expr == error_mark_node)
25673 return error_mark_node;
25675 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25676 return error_mark_node;
25678 return finish_simple_requirement (expr);
25681 /* Parse a type requirement
25683 type-requirement
25684 nested-name-specifier [opt] required-type-name ';'
25686 required-type-name:
25687 type-name
25688 'template' [opt] simple-template-id */
25689 static tree
25690 cp_parser_type_requirement (cp_parser *parser)
25692 cp_lexer_consume_token (parser->lexer);
25694 // Save the scope before parsing name specifiers.
25695 tree saved_scope = parser->scope;
25696 tree saved_object_scope = parser->object_scope;
25697 tree saved_qualifying_scope = parser->qualifying_scope;
25698 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25699 cp_parser_nested_name_specifier_opt (parser,
25700 /*typename_keyword_p=*/true,
25701 /*check_dependency_p=*/false,
25702 /*type_p=*/true,
25703 /*is_declaration=*/false);
25705 tree type;
25706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25708 cp_lexer_consume_token (parser->lexer);
25709 type = cp_parser_template_id (parser,
25710 /*template_keyword_p=*/true,
25711 /*check_dependency=*/false,
25712 /*tag_type=*/none_type,
25713 /*is_declaration=*/false);
25714 type = make_typename_type (parser->scope, type, typename_type,
25715 /*complain=*/tf_error);
25717 else
25718 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25720 if (TREE_CODE (type) == TYPE_DECL)
25721 type = TREE_TYPE (type);
25723 parser->scope = saved_scope;
25724 parser->object_scope = saved_object_scope;
25725 parser->qualifying_scope = saved_qualifying_scope;
25727 if (type == error_mark_node)
25728 cp_parser_skip_to_end_of_statement (parser);
25730 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25731 return error_mark_node;
25732 if (type == error_mark_node)
25733 return error_mark_node;
25735 return finish_type_requirement (type);
25738 /* Parse a compound requirement
25740 compound-requirement:
25741 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25742 static tree
25743 cp_parser_compound_requirement (cp_parser *parser)
25745 /* Parse an expression enclosed in '{ }'s. */
25746 matching_braces braces;
25747 if (!braces.require_open (parser))
25748 return error_mark_node;
25750 tree expr = cp_parser_expression (parser, NULL, false, false);
25751 if (!expr || expr == error_mark_node)
25752 return error_mark_node;
25754 if (!braces.require_close (parser))
25755 return error_mark_node;
25757 /* Parse the optional noexcept. */
25758 bool noexcept_p = false;
25759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25761 cp_lexer_consume_token (parser->lexer);
25762 noexcept_p = true;
25765 /* Parse the optional trailing return type. */
25766 tree type = NULL_TREE;
25767 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25769 cp_lexer_consume_token (parser->lexer);
25770 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25771 parser->in_result_type_constraint_p = true;
25772 type = cp_parser_trailing_type_id (parser);
25773 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25774 if (type == error_mark_node)
25775 return error_mark_node;
25778 return finish_compound_requirement (expr, type, noexcept_p);
25781 /* Parse a nested requirement. This is the same as a requires clause.
25783 nested-requirement:
25784 requires-clause */
25785 static tree
25786 cp_parser_nested_requirement (cp_parser *parser)
25788 cp_lexer_consume_token (parser->lexer);
25789 tree req = cp_parser_requires_clause (parser);
25790 if (req == error_mark_node)
25791 return error_mark_node;
25792 return finish_nested_requirement (req);
25795 /* Support Functions */
25797 /* Return the appropriate prefer_type argument for lookup_name_real based on
25798 tag_type and template_mem_access. */
25800 static inline int
25801 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25803 /* DR 141: When looking in the current enclosing context for a template-name
25804 after -> or ., only consider class templates. */
25805 if (template_mem_access)
25806 return 2;
25807 switch (tag_type)
25809 case none_type: return 0; // No preference.
25810 case scope_type: return 1; // Type or namespace.
25811 default: return 2; // Type only.
25815 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25816 NAME should have one of the representations used for an
25817 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25818 is returned. If PARSER->SCOPE is a dependent type, then a
25819 SCOPE_REF is returned.
25821 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25822 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25823 was formed. Abstractly, such entities should not be passed to this
25824 function, because they do not need to be looked up, but it is
25825 simpler to check for this special case here, rather than at the
25826 call-sites.
25828 In cases not explicitly covered above, this function returns a
25829 DECL, OVERLOAD, or baselink representing the result of the lookup.
25830 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25831 is returned.
25833 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25834 (e.g., "struct") that was used. In that case bindings that do not
25835 refer to types are ignored.
25837 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25838 ignored.
25840 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25841 are ignored.
25843 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25844 types.
25846 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25847 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25848 NULL_TREE otherwise. */
25850 static cp_expr
25851 cp_parser_lookup_name (cp_parser *parser, tree name,
25852 enum tag_types tag_type,
25853 bool is_template,
25854 bool is_namespace,
25855 bool check_dependency,
25856 tree *ambiguous_decls,
25857 location_t name_location)
25859 tree decl;
25860 tree object_type = parser->context->object_type;
25862 /* Assume that the lookup will be unambiguous. */
25863 if (ambiguous_decls)
25864 *ambiguous_decls = NULL_TREE;
25866 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25867 no longer valid. Note that if we are parsing tentatively, and
25868 the parse fails, OBJECT_TYPE will be automatically restored. */
25869 parser->context->object_type = NULL_TREE;
25871 if (name == error_mark_node)
25872 return error_mark_node;
25874 /* A template-id has already been resolved; there is no lookup to
25875 do. */
25876 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25877 return name;
25878 if (BASELINK_P (name))
25880 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25881 == TEMPLATE_ID_EXPR);
25882 return name;
25885 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25886 it should already have been checked to make sure that the name
25887 used matches the type being destroyed. */
25888 if (TREE_CODE (name) == BIT_NOT_EXPR)
25890 tree type;
25892 /* Figure out to which type this destructor applies. */
25893 if (parser->scope)
25894 type = parser->scope;
25895 else if (object_type)
25896 type = object_type;
25897 else
25898 type = current_class_type;
25899 /* If that's not a class type, there is no destructor. */
25900 if (!type || !CLASS_TYPE_P (type))
25901 return error_mark_node;
25903 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25904 lazily_declare_fn (sfk_destructor, type);
25906 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25907 return dtor;
25909 return error_mark_node;
25912 /* By this point, the NAME should be an ordinary identifier. If
25913 the id-expression was a qualified name, the qualifying scope is
25914 stored in PARSER->SCOPE at this point. */
25915 gcc_assert (identifier_p (name));
25917 /* Perform the lookup. */
25918 if (parser->scope)
25920 bool dependent_p;
25922 if (parser->scope == error_mark_node)
25923 return error_mark_node;
25925 /* If the SCOPE is dependent, the lookup must be deferred until
25926 the template is instantiated -- unless we are explicitly
25927 looking up names in uninstantiated templates. Even then, we
25928 cannot look up the name if the scope is not a class type; it
25929 might, for example, be a template type parameter. */
25930 dependent_p = (TYPE_P (parser->scope)
25931 && dependent_scope_p (parser->scope));
25932 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25933 && dependent_p)
25934 /* Defer lookup. */
25935 decl = error_mark_node;
25936 else
25938 tree pushed_scope = NULL_TREE;
25940 /* If PARSER->SCOPE is a dependent type, then it must be a
25941 class type, and we must not be checking dependencies;
25942 otherwise, we would have processed this lookup above. So
25943 that PARSER->SCOPE is not considered a dependent base by
25944 lookup_member, we must enter the scope here. */
25945 if (dependent_p)
25946 pushed_scope = push_scope (parser->scope);
25948 /* If the PARSER->SCOPE is a template specialization, it
25949 may be instantiated during name lookup. In that case,
25950 errors may be issued. Even if we rollback the current
25951 tentative parse, those errors are valid. */
25952 decl = lookup_qualified_name (parser->scope, name,
25953 prefer_type_arg (tag_type),
25954 /*complain=*/true);
25956 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25957 lookup result and the nested-name-specifier nominates a class C:
25958 * if the name specified after the nested-name-specifier, when
25959 looked up in C, is the injected-class-name of C (Clause 9), or
25960 * if the name specified after the nested-name-specifier is the
25961 same as the identifier or the simple-template-id's template-
25962 name in the last component of the nested-name-specifier,
25963 the name is instead considered to name the constructor of
25964 class C. [ Note: for example, the constructor is not an
25965 acceptable lookup result in an elaborated-type-specifier so
25966 the constructor would not be used in place of the
25967 injected-class-name. --end note ] Such a constructor name
25968 shall be used only in the declarator-id of a declaration that
25969 names a constructor or in a using-declaration. */
25970 if (tag_type == none_type
25971 && DECL_SELF_REFERENCE_P (decl)
25972 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25973 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25974 prefer_type_arg (tag_type),
25975 /*complain=*/true);
25977 /* If we have a single function from a using decl, pull it out. */
25978 if (TREE_CODE (decl) == OVERLOAD
25979 && !really_overloaded_fn (decl))
25980 decl = OVL_FUNCTION (decl);
25982 if (pushed_scope)
25983 pop_scope (pushed_scope);
25986 /* If the scope is a dependent type and either we deferred lookup or
25987 we did lookup but didn't find the name, rememeber the name. */
25988 if (decl == error_mark_node && TYPE_P (parser->scope)
25989 && dependent_type_p (parser->scope))
25991 if (tag_type)
25993 tree type;
25995 /* The resolution to Core Issue 180 says that `struct
25996 A::B' should be considered a type-name, even if `A'
25997 is dependent. */
25998 type = make_typename_type (parser->scope, name, tag_type,
25999 /*complain=*/tf_error);
26000 if (type != error_mark_node)
26001 decl = TYPE_NAME (type);
26003 else if (is_template
26004 && (cp_parser_next_token_ends_template_argument_p (parser)
26005 || cp_lexer_next_token_is (parser->lexer,
26006 CPP_CLOSE_PAREN)))
26007 decl = make_unbound_class_template (parser->scope,
26008 name, NULL_TREE,
26009 /*complain=*/tf_error);
26010 else
26011 decl = build_qualified_name (/*type=*/NULL_TREE,
26012 parser->scope, name,
26013 is_template);
26015 parser->qualifying_scope = parser->scope;
26016 parser->object_scope = NULL_TREE;
26018 else if (object_type)
26020 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26021 OBJECT_TYPE is not a class. */
26022 if (CLASS_TYPE_P (object_type))
26023 /* If the OBJECT_TYPE is a template specialization, it may
26024 be instantiated during name lookup. In that case, errors
26025 may be issued. Even if we rollback the current tentative
26026 parse, those errors are valid. */
26027 decl = lookup_member (object_type,
26028 name,
26029 /*protect=*/0,
26030 prefer_type_arg (tag_type),
26031 tf_warning_or_error);
26032 else
26033 decl = NULL_TREE;
26035 if (!decl)
26036 /* Look it up in the enclosing context. DR 141: When looking for a
26037 template-name after -> or ., only consider class templates. */
26038 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26039 /*nonclass=*/0,
26040 /*block_p=*/true, is_namespace, 0);
26041 if (object_type == unknown_type_node)
26042 /* The object is type-dependent, so we can't look anything up; we used
26043 this to get the DR 141 behavior. */
26044 object_type = NULL_TREE;
26045 parser->object_scope = object_type;
26046 parser->qualifying_scope = NULL_TREE;
26048 else
26050 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26051 /*nonclass=*/0,
26052 /*block_p=*/true, is_namespace, 0);
26053 parser->qualifying_scope = NULL_TREE;
26054 parser->object_scope = NULL_TREE;
26057 /* If the lookup failed, let our caller know. */
26058 if (!decl || decl == error_mark_node)
26059 return error_mark_node;
26061 /* Pull out the template from an injected-class-name (or multiple). */
26062 if (is_template)
26063 decl = maybe_get_template_decl_from_type_decl (decl);
26065 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26066 if (TREE_CODE (decl) == TREE_LIST)
26068 if (ambiguous_decls)
26069 *ambiguous_decls = decl;
26070 /* The error message we have to print is too complicated for
26071 cp_parser_error, so we incorporate its actions directly. */
26072 if (!cp_parser_simulate_error (parser))
26074 error_at (name_location, "reference to %qD is ambiguous",
26075 name);
26076 print_candidates (decl);
26078 return error_mark_node;
26081 gcc_assert (DECL_P (decl)
26082 || TREE_CODE (decl) == OVERLOAD
26083 || TREE_CODE (decl) == SCOPE_REF
26084 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26085 || BASELINK_P (decl));
26087 /* If we have resolved the name of a member declaration, check to
26088 see if the declaration is accessible. When the name resolves to
26089 set of overloaded functions, accessibility is checked when
26090 overload resolution is done.
26092 During an explicit instantiation, access is not checked at all,
26093 as per [temp.explicit]. */
26094 if (DECL_P (decl))
26095 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26097 maybe_record_typedef_use (decl);
26099 return cp_expr (decl, name_location);
26102 /* Like cp_parser_lookup_name, but for use in the typical case where
26103 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26104 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26106 static tree
26107 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26109 return cp_parser_lookup_name (parser, name,
26110 none_type,
26111 /*is_template=*/false,
26112 /*is_namespace=*/false,
26113 /*check_dependency=*/true,
26114 /*ambiguous_decls=*/NULL,
26115 location);
26118 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26119 the current context, return the TYPE_DECL. If TAG_NAME_P is
26120 true, the DECL indicates the class being defined in a class-head,
26121 or declared in an elaborated-type-specifier.
26123 Otherwise, return DECL. */
26125 static tree
26126 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26128 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26129 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26131 struct A {
26132 template <typename T> struct B;
26135 template <typename T> struct A::B {};
26137 Similarly, in an elaborated-type-specifier:
26139 namespace N { struct X{}; }
26141 struct A {
26142 template <typename T> friend struct N::X;
26145 However, if the DECL refers to a class type, and we are in
26146 the scope of the class, then the name lookup automatically
26147 finds the TYPE_DECL created by build_self_reference rather
26148 than a TEMPLATE_DECL. For example, in:
26150 template <class T> struct S {
26151 S s;
26154 there is no need to handle such case. */
26156 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26157 return DECL_TEMPLATE_RESULT (decl);
26159 return decl;
26162 /* If too many, or too few, template-parameter lists apply to the
26163 declarator, issue an error message. Returns TRUE if all went well,
26164 and FALSE otherwise. */
26166 static bool
26167 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26168 cp_declarator *declarator,
26169 location_t declarator_location)
26171 switch (declarator->kind)
26173 case cdk_id:
26175 unsigned num_templates = 0;
26176 tree scope = declarator->u.id.qualifying_scope;
26178 if (scope)
26179 num_templates = num_template_headers_for_class (scope);
26180 else if (TREE_CODE (declarator->u.id.unqualified_name)
26181 == TEMPLATE_ID_EXPR)
26182 /* If the DECLARATOR has the form `X<y>' then it uses one
26183 additional level of template parameters. */
26184 ++num_templates;
26186 return cp_parser_check_template_parameters
26187 (parser, num_templates, declarator_location, declarator);
26190 case cdk_function:
26191 case cdk_array:
26192 case cdk_pointer:
26193 case cdk_reference:
26194 case cdk_ptrmem:
26195 return (cp_parser_check_declarator_template_parameters
26196 (parser, declarator->declarator, declarator_location));
26198 case cdk_decomp:
26199 case cdk_error:
26200 return true;
26202 default:
26203 gcc_unreachable ();
26205 return false;
26208 /* NUM_TEMPLATES were used in the current declaration. If that is
26209 invalid, return FALSE and issue an error messages. Otherwise,
26210 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26211 declarator and we can print more accurate diagnostics. */
26213 static bool
26214 cp_parser_check_template_parameters (cp_parser* parser,
26215 unsigned num_templates,
26216 location_t location,
26217 cp_declarator *declarator)
26219 /* If there are the same number of template classes and parameter
26220 lists, that's OK. */
26221 if (parser->num_template_parameter_lists == num_templates)
26222 return true;
26223 /* If there are more, but only one more, then we are referring to a
26224 member template. That's OK too. */
26225 if (parser->num_template_parameter_lists == num_templates + 1)
26226 return true;
26227 /* If there are more template classes than parameter lists, we have
26228 something like:
26230 template <class T> void S<T>::R<T>::f (); */
26231 if (parser->num_template_parameter_lists < num_templates)
26233 if (declarator && !current_function_decl)
26234 error_at (location, "specializing member %<%T::%E%> "
26235 "requires %<template<>%> syntax",
26236 declarator->u.id.qualifying_scope,
26237 declarator->u.id.unqualified_name);
26238 else if (declarator)
26239 error_at (location, "invalid declaration of %<%T::%E%>",
26240 declarator->u.id.qualifying_scope,
26241 declarator->u.id.unqualified_name);
26242 else
26243 error_at (location, "too few template-parameter-lists");
26244 return false;
26246 /* Otherwise, there are too many template parameter lists. We have
26247 something like:
26249 template <class T> template <class U> void S::f(); */
26250 error_at (location, "too many template-parameter-lists");
26251 return false;
26254 /* Parse an optional `::' token indicating that the following name is
26255 from the global namespace. If so, PARSER->SCOPE is set to the
26256 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26257 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26258 Returns the new value of PARSER->SCOPE, if the `::' token is
26259 present, and NULL_TREE otherwise. */
26261 static tree
26262 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26264 cp_token *token;
26266 /* Peek at the next token. */
26267 token = cp_lexer_peek_token (parser->lexer);
26268 /* If we're looking at a `::' token then we're starting from the
26269 global namespace, not our current location. */
26270 if (token->type == CPP_SCOPE)
26272 /* Consume the `::' token. */
26273 cp_lexer_consume_token (parser->lexer);
26274 /* Set the SCOPE so that we know where to start the lookup. */
26275 parser->scope = global_namespace;
26276 parser->qualifying_scope = global_namespace;
26277 parser->object_scope = NULL_TREE;
26279 return parser->scope;
26281 else if (!current_scope_valid_p)
26283 parser->scope = NULL_TREE;
26284 parser->qualifying_scope = NULL_TREE;
26285 parser->object_scope = NULL_TREE;
26288 return NULL_TREE;
26291 /* Returns TRUE if the upcoming token sequence is the start of a
26292 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26293 declarator is preceded by the `friend' specifier. */
26295 static bool
26296 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26298 bool constructor_p;
26299 bool outside_class_specifier_p;
26300 tree nested_name_specifier;
26301 cp_token *next_token;
26303 /* The common case is that this is not a constructor declarator, so
26304 try to avoid doing lots of work if at all possible. It's not
26305 valid declare a constructor at function scope. */
26306 if (parser->in_function_body)
26307 return false;
26308 /* And only certain tokens can begin a constructor declarator. */
26309 next_token = cp_lexer_peek_token (parser->lexer);
26310 if (next_token->type != CPP_NAME
26311 && next_token->type != CPP_SCOPE
26312 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26313 && next_token->type != CPP_TEMPLATE_ID)
26314 return false;
26316 /* Parse tentatively; we are going to roll back all of the tokens
26317 consumed here. */
26318 cp_parser_parse_tentatively (parser);
26319 /* Assume that we are looking at a constructor declarator. */
26320 constructor_p = true;
26322 /* Look for the optional `::' operator. */
26323 cp_parser_global_scope_opt (parser,
26324 /*current_scope_valid_p=*/false);
26325 /* Look for the nested-name-specifier. */
26326 nested_name_specifier
26327 = (cp_parser_nested_name_specifier_opt (parser,
26328 /*typename_keyword_p=*/false,
26329 /*check_dependency_p=*/false,
26330 /*type_p=*/false,
26331 /*is_declaration=*/false));
26333 outside_class_specifier_p = (!at_class_scope_p ()
26334 || !TYPE_BEING_DEFINED (current_class_type)
26335 || friend_p);
26337 /* Outside of a class-specifier, there must be a
26338 nested-name-specifier. Except in C++17 mode, where we
26339 might be declaring a guiding declaration. */
26340 if (!nested_name_specifier && outside_class_specifier_p
26341 && cxx_dialect < cxx17)
26342 constructor_p = false;
26343 else if (nested_name_specifier == error_mark_node)
26344 constructor_p = false;
26346 /* If we have a class scope, this is easy; DR 147 says that S::S always
26347 names the constructor, and no other qualified name could. */
26348 if (constructor_p && nested_name_specifier
26349 && CLASS_TYPE_P (nested_name_specifier))
26351 tree id = cp_parser_unqualified_id (parser,
26352 /*template_keyword_p=*/false,
26353 /*check_dependency_p=*/false,
26354 /*declarator_p=*/true,
26355 /*optional_p=*/false);
26356 if (is_overloaded_fn (id))
26357 id = DECL_NAME (get_first_fn (id));
26358 if (!constructor_name_p (id, nested_name_specifier))
26359 constructor_p = false;
26361 /* If we still think that this might be a constructor-declarator,
26362 look for a class-name. */
26363 else if (constructor_p)
26365 /* If we have:
26367 template <typename T> struct S {
26368 S();
26371 we must recognize that the nested `S' names a class. */
26372 if (cxx_dialect >= cxx17)
26373 cp_parser_parse_tentatively (parser);
26375 tree type_decl;
26376 type_decl = cp_parser_class_name (parser,
26377 /*typename_keyword_p=*/false,
26378 /*template_keyword_p=*/false,
26379 none_type,
26380 /*check_dependency_p=*/false,
26381 /*class_head_p=*/false,
26382 /*is_declaration=*/false);
26384 if (cxx_dialect >= cxx17
26385 && !cp_parser_parse_definitely (parser))
26387 type_decl = NULL_TREE;
26388 tree tmpl = cp_parser_template_name (parser,
26389 /*template_keyword*/false,
26390 /*check_dependency_p*/false,
26391 /*is_declaration*/false,
26392 none_type,
26393 /*is_identifier*/NULL);
26394 if (DECL_CLASS_TEMPLATE_P (tmpl)
26395 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26396 /* It's a deduction guide, return true. */;
26397 else
26398 cp_parser_simulate_error (parser);
26401 /* If there was no class-name, then this is not a constructor.
26402 Otherwise, if we are in a class-specifier and we aren't
26403 handling a friend declaration, check that its type matches
26404 current_class_type (c++/38313). Note: error_mark_node
26405 is left alone for error recovery purposes. */
26406 constructor_p = (!cp_parser_error_occurred (parser)
26407 && (outside_class_specifier_p
26408 || type_decl == NULL_TREE
26409 || type_decl == error_mark_node
26410 || same_type_p (current_class_type,
26411 TREE_TYPE (type_decl))));
26413 /* If we're still considering a constructor, we have to see a `(',
26414 to begin the parameter-declaration-clause, followed by either a
26415 `)', an `...', or a decl-specifier. We need to check for a
26416 type-specifier to avoid being fooled into thinking that:
26418 S (f) (int);
26420 is a constructor. (It is actually a function named `f' that
26421 takes one parameter (of type `int') and returns a value of type
26422 `S'. */
26423 if (constructor_p
26424 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26425 constructor_p = false;
26427 if (constructor_p
26428 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26429 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26430 /* A parameter declaration begins with a decl-specifier,
26431 which is either the "attribute" keyword, a storage class
26432 specifier, or (usually) a type-specifier. */
26433 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26435 tree type;
26436 tree pushed_scope = NULL_TREE;
26437 unsigned saved_num_template_parameter_lists;
26439 /* Names appearing in the type-specifier should be looked up
26440 in the scope of the class. */
26441 if (current_class_type)
26442 type = NULL_TREE;
26443 else if (type_decl)
26445 type = TREE_TYPE (type_decl);
26446 if (TREE_CODE (type) == TYPENAME_TYPE)
26448 type = resolve_typename_type (type,
26449 /*only_current_p=*/false);
26450 if (TREE_CODE (type) == TYPENAME_TYPE)
26452 cp_parser_abort_tentative_parse (parser);
26453 return false;
26456 pushed_scope = push_scope (type);
26459 /* Inside the constructor parameter list, surrounding
26460 template-parameter-lists do not apply. */
26461 saved_num_template_parameter_lists
26462 = parser->num_template_parameter_lists;
26463 parser->num_template_parameter_lists = 0;
26465 /* Look for the type-specifier. */
26466 cp_parser_type_specifier (parser,
26467 CP_PARSER_FLAGS_NONE,
26468 /*decl_specs=*/NULL,
26469 /*is_declarator=*/true,
26470 /*declares_class_or_enum=*/NULL,
26471 /*is_cv_qualifier=*/NULL);
26473 parser->num_template_parameter_lists
26474 = saved_num_template_parameter_lists;
26476 /* Leave the scope of the class. */
26477 if (pushed_scope)
26478 pop_scope (pushed_scope);
26480 constructor_p = !cp_parser_error_occurred (parser);
26484 /* We did not really want to consume any tokens. */
26485 cp_parser_abort_tentative_parse (parser);
26487 return constructor_p;
26490 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26491 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26492 they must be performed once we are in the scope of the function.
26494 Returns the function defined. */
26496 static tree
26497 cp_parser_function_definition_from_specifiers_and_declarator
26498 (cp_parser* parser,
26499 cp_decl_specifier_seq *decl_specifiers,
26500 tree attributes,
26501 const cp_declarator *declarator)
26503 tree fn;
26504 bool success_p;
26506 /* Begin the function-definition. */
26507 success_p = start_function (decl_specifiers, declarator, attributes);
26509 /* The things we're about to see are not directly qualified by any
26510 template headers we've seen thus far. */
26511 reset_specialization ();
26513 /* If there were names looked up in the decl-specifier-seq that we
26514 did not check, check them now. We must wait until we are in the
26515 scope of the function to perform the checks, since the function
26516 might be a friend. */
26517 perform_deferred_access_checks (tf_warning_or_error);
26519 if (success_p)
26521 cp_finalize_omp_declare_simd (parser, current_function_decl);
26522 parser->omp_declare_simd = NULL;
26523 cp_finalize_oacc_routine (parser, current_function_decl, true);
26524 parser->oacc_routine = NULL;
26527 if (!success_p)
26529 /* Skip the entire function. */
26530 cp_parser_skip_to_end_of_block_or_statement (parser);
26531 fn = error_mark_node;
26533 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26535 /* Seen already, skip it. An error message has already been output. */
26536 cp_parser_skip_to_end_of_block_or_statement (parser);
26537 fn = current_function_decl;
26538 current_function_decl = NULL_TREE;
26539 /* If this is a function from a class, pop the nested class. */
26540 if (current_class_name)
26541 pop_nested_class ();
26543 else
26545 timevar_id_t tv;
26546 if (DECL_DECLARED_INLINE_P (current_function_decl))
26547 tv = TV_PARSE_INLINE;
26548 else
26549 tv = TV_PARSE_FUNC;
26550 timevar_push (tv);
26551 fn = cp_parser_function_definition_after_declarator (parser,
26552 /*inline_p=*/false);
26553 timevar_pop (tv);
26556 return fn;
26559 /* Parse the part of a function-definition that follows the
26560 declarator. INLINE_P is TRUE iff this function is an inline
26561 function defined within a class-specifier.
26563 Returns the function defined. */
26565 static tree
26566 cp_parser_function_definition_after_declarator (cp_parser* parser,
26567 bool inline_p)
26569 tree fn;
26570 bool saved_in_unbraced_linkage_specification_p;
26571 bool saved_in_function_body;
26572 unsigned saved_num_template_parameter_lists;
26573 cp_token *token;
26574 bool fully_implicit_function_template_p
26575 = parser->fully_implicit_function_template_p;
26576 parser->fully_implicit_function_template_p = false;
26577 tree implicit_template_parms
26578 = parser->implicit_template_parms;
26579 parser->implicit_template_parms = 0;
26580 cp_binding_level* implicit_template_scope
26581 = parser->implicit_template_scope;
26582 parser->implicit_template_scope = 0;
26584 saved_in_function_body = parser->in_function_body;
26585 parser->in_function_body = true;
26586 /* If the next token is `return', then the code may be trying to
26587 make use of the "named return value" extension that G++ used to
26588 support. */
26589 token = cp_lexer_peek_token (parser->lexer);
26590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26592 /* Consume the `return' keyword. */
26593 cp_lexer_consume_token (parser->lexer);
26594 /* Look for the identifier that indicates what value is to be
26595 returned. */
26596 cp_parser_identifier (parser);
26597 /* Issue an error message. */
26598 error_at (token->location,
26599 "named return values are no longer supported");
26600 /* Skip tokens until we reach the start of the function body. */
26601 while (true)
26603 cp_token *token = cp_lexer_peek_token (parser->lexer);
26604 if (token->type == CPP_OPEN_BRACE
26605 || token->type == CPP_EOF
26606 || token->type == CPP_PRAGMA_EOL)
26607 break;
26608 cp_lexer_consume_token (parser->lexer);
26611 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26612 anything declared inside `f'. */
26613 saved_in_unbraced_linkage_specification_p
26614 = parser->in_unbraced_linkage_specification_p;
26615 parser->in_unbraced_linkage_specification_p = false;
26616 /* Inside the function, surrounding template-parameter-lists do not
26617 apply. */
26618 saved_num_template_parameter_lists
26619 = parser->num_template_parameter_lists;
26620 parser->num_template_parameter_lists = 0;
26622 /* If the next token is `try', `__transaction_atomic', or
26623 `__transaction_relaxed`, then we are looking at either function-try-block
26624 or function-transaction-block. Note that all of these include the
26625 function-body. */
26626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26627 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26628 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26629 RID_TRANSACTION_RELAXED))
26630 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26631 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26632 cp_parser_function_try_block (parser);
26633 else
26634 cp_parser_ctor_initializer_opt_and_function_body
26635 (parser, /*in_function_try_block=*/false);
26637 /* Finish the function. */
26638 fn = finish_function (inline_p);
26639 /* Generate code for it, if necessary. */
26640 expand_or_defer_fn (fn);
26641 /* Restore the saved values. */
26642 parser->in_unbraced_linkage_specification_p
26643 = saved_in_unbraced_linkage_specification_p;
26644 parser->num_template_parameter_lists
26645 = saved_num_template_parameter_lists;
26646 parser->in_function_body = saved_in_function_body;
26648 parser->fully_implicit_function_template_p
26649 = fully_implicit_function_template_p;
26650 parser->implicit_template_parms
26651 = implicit_template_parms;
26652 parser->implicit_template_scope
26653 = implicit_template_scope;
26655 if (parser->fully_implicit_function_template_p)
26656 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26658 return fn;
26661 /* Parse a template-declaration body (following argument list). */
26663 static void
26664 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26665 tree parameter_list,
26666 bool member_p)
26668 tree decl = NULL_TREE;
26669 bool friend_p = false;
26671 /* We just processed one more parameter list. */
26672 ++parser->num_template_parameter_lists;
26674 /* Get the deferred access checks from the parameter list. These
26675 will be checked once we know what is being declared, as for a
26676 member template the checks must be performed in the scope of the
26677 class containing the member. */
26678 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26680 /* Tentatively parse for a new template parameter list, which can either be
26681 the template keyword or a template introduction. */
26682 if (cp_parser_template_declaration_after_export (parser, member_p))
26683 /* OK */;
26684 else if (cxx_dialect >= cxx11
26685 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26686 decl = cp_parser_alias_declaration (parser);
26687 else
26689 /* There are no access checks when parsing a template, as we do not
26690 know if a specialization will be a friend. */
26691 push_deferring_access_checks (dk_no_check);
26692 cp_token *token = cp_lexer_peek_token (parser->lexer);
26693 decl = cp_parser_single_declaration (parser,
26694 checks,
26695 member_p,
26696 /*explicit_specialization_p=*/false,
26697 &friend_p);
26698 pop_deferring_access_checks ();
26700 /* If this is a member template declaration, let the front
26701 end know. */
26702 if (member_p && !friend_p && decl)
26704 if (TREE_CODE (decl) == TYPE_DECL)
26705 cp_parser_check_access_in_redeclaration (decl, token->location);
26707 decl = finish_member_template_decl (decl);
26709 else if (friend_p && decl
26710 && DECL_DECLARES_TYPE_P (decl))
26711 make_friend_class (current_class_type, TREE_TYPE (decl),
26712 /*complain=*/true);
26714 /* We are done with the current parameter list. */
26715 --parser->num_template_parameter_lists;
26717 pop_deferring_access_checks ();
26719 /* Finish up. */
26720 finish_template_decl (parameter_list);
26722 /* Check the template arguments for a literal operator template. */
26723 if (decl
26724 && DECL_DECLARES_FUNCTION_P (decl)
26725 && UDLIT_OPER_P (DECL_NAME (decl)))
26727 bool ok = true;
26728 if (parameter_list == NULL_TREE)
26729 ok = false;
26730 else
26732 int num_parms = TREE_VEC_LENGTH (parameter_list);
26733 if (num_parms == 1)
26735 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26736 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26737 if (TREE_TYPE (parm) != char_type_node
26738 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26739 ok = false;
26741 else if (num_parms == 2 && cxx_dialect >= cxx14)
26743 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26744 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26745 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26746 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26747 if (parm == error_mark_node
26748 || TREE_TYPE (parm) != TREE_TYPE (type)
26749 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26750 ok = false;
26752 else
26753 ok = false;
26755 if (!ok)
26757 if (cxx_dialect >= cxx14)
26758 error ("literal operator template %qD has invalid parameter list."
26759 " Expected non-type template argument pack <char...>"
26760 " or <typename CharT, CharT...>",
26761 decl);
26762 else
26763 error ("literal operator template %qD has invalid parameter list."
26764 " Expected non-type template argument pack <char...>",
26765 decl);
26769 /* Register member declarations. */
26770 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26771 finish_member_declaration (decl);
26772 /* If DECL is a function template, we must return to parse it later.
26773 (Even though there is no definition, there might be default
26774 arguments that need handling.) */
26775 if (member_p && decl
26776 && DECL_DECLARES_FUNCTION_P (decl))
26777 vec_safe_push (unparsed_funs_with_definitions, decl);
26780 /* Parse a template introduction header for a template-declaration. Returns
26781 false if tentative parse fails. */
26783 static bool
26784 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26786 cp_parser_parse_tentatively (parser);
26788 tree saved_scope = parser->scope;
26789 tree saved_object_scope = parser->object_scope;
26790 tree saved_qualifying_scope = parser->qualifying_scope;
26792 /* Look for the optional `::' operator. */
26793 cp_parser_global_scope_opt (parser,
26794 /*current_scope_valid_p=*/false);
26795 /* Look for the nested-name-specifier. */
26796 cp_parser_nested_name_specifier_opt (parser,
26797 /*typename_keyword_p=*/false,
26798 /*check_dependency_p=*/true,
26799 /*type_p=*/false,
26800 /*is_declaration=*/false);
26802 cp_token *token = cp_lexer_peek_token (parser->lexer);
26803 tree concept_name = cp_parser_identifier (parser);
26805 /* Look up the concept for which we will be matching
26806 template parameters. */
26807 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26808 token->location);
26809 parser->scope = saved_scope;
26810 parser->object_scope = saved_object_scope;
26811 parser->qualifying_scope = saved_qualifying_scope;
26813 if (concept_name == error_mark_node)
26814 cp_parser_simulate_error (parser);
26816 /* Look for opening brace for introduction. */
26817 matching_braces braces;
26818 braces.require_open (parser);
26820 if (!cp_parser_parse_definitely (parser))
26821 return false;
26823 push_deferring_access_checks (dk_deferred);
26825 /* Build vector of placeholder parameters and grab
26826 matching identifiers. */
26827 tree introduction_list = cp_parser_introduction_list (parser);
26829 /* The introduction-list shall not be empty. */
26830 int nargs = TREE_VEC_LENGTH (introduction_list);
26831 if (nargs == 0)
26833 error ("empty introduction-list");
26834 return true;
26837 /* Look for closing brace for introduction. */
26838 if (!braces.require_close (parser))
26839 return true;
26841 if (tmpl_decl == error_mark_node)
26843 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26844 token->location);
26845 return true;
26848 /* Build and associate the constraint. */
26849 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26850 if (parms && parms != error_mark_node)
26852 cp_parser_template_declaration_after_parameters (parser, parms,
26853 member_p);
26854 return true;
26857 error_at (token->location, "no matching concept for template-introduction");
26858 return true;
26861 /* Parse a normal template-declaration following the template keyword. */
26863 static void
26864 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26866 tree parameter_list;
26867 bool need_lang_pop;
26868 location_t location = input_location;
26870 /* Look for the `<' token. */
26871 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26872 return;
26873 if (at_class_scope_p () && current_function_decl)
26875 /* 14.5.2.2 [temp.mem]
26877 A local class shall not have member templates. */
26878 error_at (location,
26879 "invalid declaration of member template in local class");
26880 cp_parser_skip_to_end_of_block_or_statement (parser);
26881 return;
26883 /* [temp]
26885 A template ... shall not have C linkage. */
26886 if (current_lang_name == lang_name_c)
26888 error_at (location, "template with C linkage");
26889 maybe_show_extern_c_location ();
26890 /* Give it C++ linkage to avoid confusing other parts of the
26891 front end. */
26892 push_lang_context (lang_name_cplusplus);
26893 need_lang_pop = true;
26895 else
26896 need_lang_pop = false;
26898 /* We cannot perform access checks on the template parameter
26899 declarations until we know what is being declared, just as we
26900 cannot check the decl-specifier list. */
26901 push_deferring_access_checks (dk_deferred);
26903 /* If the next token is `>', then we have an invalid
26904 specialization. Rather than complain about an invalid template
26905 parameter, issue an error message here. */
26906 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26908 cp_parser_error (parser, "invalid explicit specialization");
26909 begin_specialization ();
26910 parameter_list = NULL_TREE;
26912 else
26914 /* Parse the template parameters. */
26915 parameter_list = cp_parser_template_parameter_list (parser);
26918 /* Look for the `>'. */
26919 cp_parser_skip_to_end_of_template_parameter_list (parser);
26921 /* Manage template requirements */
26922 if (flag_concepts)
26924 tree reqs = get_shorthand_constraints (current_template_parms);
26925 if (tree r = cp_parser_requires_clause_opt (parser))
26926 reqs = conjoin_constraints (reqs, normalize_expression (r));
26927 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26930 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26931 member_p);
26933 /* For the erroneous case of a template with C linkage, we pushed an
26934 implicit C++ linkage scope; exit that scope now. */
26935 if (need_lang_pop)
26936 pop_lang_context ();
26939 /* Parse a template-declaration, assuming that the `export' (and
26940 `extern') keywords, if present, has already been scanned. MEMBER_P
26941 is as for cp_parser_template_declaration. */
26943 static bool
26944 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26948 cp_lexer_consume_token (parser->lexer);
26949 cp_parser_explicit_template_declaration (parser, member_p);
26950 return true;
26952 else if (flag_concepts)
26953 return cp_parser_template_introduction (parser, member_p);
26955 return false;
26958 /* Perform the deferred access checks from a template-parameter-list.
26959 CHECKS is a TREE_LIST of access checks, as returned by
26960 get_deferred_access_checks. */
26962 static void
26963 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26965 ++processing_template_parmlist;
26966 perform_access_checks (checks, tf_warning_or_error);
26967 --processing_template_parmlist;
26970 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26971 `function-definition' sequence that follows a template header.
26972 If MEMBER_P is true, this declaration appears in a class scope.
26974 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26975 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26977 static tree
26978 cp_parser_single_declaration (cp_parser* parser,
26979 vec<deferred_access_check, va_gc> *checks,
26980 bool member_p,
26981 bool explicit_specialization_p,
26982 bool* friend_p)
26984 int declares_class_or_enum;
26985 tree decl = NULL_TREE;
26986 cp_decl_specifier_seq decl_specifiers;
26987 bool function_definition_p = false;
26988 cp_token *decl_spec_token_start;
26990 /* This function is only used when processing a template
26991 declaration. */
26992 gcc_assert (innermost_scope_kind () == sk_template_parms
26993 || innermost_scope_kind () == sk_template_spec);
26995 /* Defer access checks until we know what is being declared. */
26996 push_deferring_access_checks (dk_deferred);
26998 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26999 alternative. */
27000 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27001 cp_parser_decl_specifier_seq (parser,
27002 CP_PARSER_FLAGS_OPTIONAL,
27003 &decl_specifiers,
27004 &declares_class_or_enum);
27005 if (friend_p)
27006 *friend_p = cp_parser_friend_p (&decl_specifiers);
27008 /* There are no template typedefs. */
27009 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27011 error_at (decl_spec_token_start->location,
27012 "template declaration of %<typedef%>");
27013 decl = error_mark_node;
27016 /* Gather up the access checks that occurred the
27017 decl-specifier-seq. */
27018 stop_deferring_access_checks ();
27020 /* Check for the declaration of a template class. */
27021 if (declares_class_or_enum)
27023 if (cp_parser_declares_only_class_p (parser)
27024 || (declares_class_or_enum & 2))
27026 // If this is a declaration, but not a definition, associate
27027 // any constraints with the type declaration. Constraints
27028 // are associated with definitions in cp_parser_class_specifier.
27029 if (declares_class_or_enum == 1)
27030 associate_classtype_constraints (decl_specifiers.type);
27032 decl = shadow_tag (&decl_specifiers);
27034 /* In this case:
27036 struct C {
27037 friend template <typename T> struct A<T>::B;
27040 A<T>::B will be represented by a TYPENAME_TYPE, and
27041 therefore not recognized by shadow_tag. */
27042 if (friend_p && *friend_p
27043 && !decl
27044 && decl_specifiers.type
27045 && TYPE_P (decl_specifiers.type))
27046 decl = decl_specifiers.type;
27048 if (decl && decl != error_mark_node)
27049 decl = TYPE_NAME (decl);
27050 else
27051 decl = error_mark_node;
27053 /* Perform access checks for template parameters. */
27054 cp_parser_perform_template_parameter_access_checks (checks);
27056 /* Give a helpful diagnostic for
27057 template <class T> struct A { } a;
27058 if we aren't already recovering from an error. */
27059 if (!cp_parser_declares_only_class_p (parser)
27060 && !seen_error ())
27062 error_at (cp_lexer_peek_token (parser->lexer)->location,
27063 "a class template declaration must not declare "
27064 "anything else");
27065 cp_parser_skip_to_end_of_block_or_statement (parser);
27066 goto out;
27071 /* Complain about missing 'typename' or other invalid type names. */
27072 if (!decl_specifiers.any_type_specifiers_p
27073 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27075 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27076 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27077 the rest of this declaration. */
27078 decl = error_mark_node;
27079 goto out;
27082 /* If it's not a template class, try for a template function. If
27083 the next token is a `;', then this declaration does not declare
27084 anything. But, if there were errors in the decl-specifiers, then
27085 the error might well have come from an attempted class-specifier.
27086 In that case, there's no need to warn about a missing declarator. */
27087 if (!decl
27088 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27089 || decl_specifiers.type != error_mark_node))
27091 decl = cp_parser_init_declarator (parser,
27092 &decl_specifiers,
27093 checks,
27094 /*function_definition_allowed_p=*/true,
27095 member_p,
27096 declares_class_or_enum,
27097 &function_definition_p,
27098 NULL, NULL, NULL);
27100 /* 7.1.1-1 [dcl.stc]
27102 A storage-class-specifier shall not be specified in an explicit
27103 specialization... */
27104 if (decl
27105 && explicit_specialization_p
27106 && decl_specifiers.storage_class != sc_none)
27108 error_at (decl_spec_token_start->location,
27109 "explicit template specialization cannot have a storage class");
27110 decl = error_mark_node;
27113 if (decl && VAR_P (decl))
27114 check_template_variable (decl);
27117 /* Look for a trailing `;' after the declaration. */
27118 if (!function_definition_p
27119 && (decl == error_mark_node
27120 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27121 cp_parser_skip_to_end_of_block_or_statement (parser);
27123 out:
27124 pop_deferring_access_checks ();
27126 /* Clear any current qualification; whatever comes next is the start
27127 of something new. */
27128 parser->scope = NULL_TREE;
27129 parser->qualifying_scope = NULL_TREE;
27130 parser->object_scope = NULL_TREE;
27132 return decl;
27135 /* Parse a cast-expression that is not the operand of a unary "&". */
27137 static cp_expr
27138 cp_parser_simple_cast_expression (cp_parser *parser)
27140 return cp_parser_cast_expression (parser, /*address_p=*/false,
27141 /*cast_p=*/false, /*decltype*/false, NULL);
27144 /* Parse a functional cast to TYPE. Returns an expression
27145 representing the cast. */
27147 static cp_expr
27148 cp_parser_functional_cast (cp_parser* parser, tree type)
27150 vec<tree, va_gc> *vec;
27151 tree expression_list;
27152 cp_expr cast;
27153 bool nonconst_p;
27155 location_t start_loc = input_location;
27157 if (!type)
27158 type = error_mark_node;
27160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27162 cp_lexer_set_source_position (parser->lexer);
27163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27164 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27165 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27166 if (TREE_CODE (type) == TYPE_DECL)
27167 type = TREE_TYPE (type);
27169 cast = finish_compound_literal (type, expression_list,
27170 tf_warning_or_error, fcl_functional);
27171 /* Create a location of the form:
27172 type_name{i, f}
27173 ^~~~~~~~~~~~~~~
27174 with caret == start at the start of the type name,
27175 finishing at the closing brace. */
27176 location_t finish_loc
27177 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27178 location_t combined_loc = make_location (start_loc, start_loc,
27179 finish_loc);
27180 cast.set_location (combined_loc);
27181 return cast;
27185 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27186 /*cast_p=*/true,
27187 /*allow_expansion_p=*/true,
27188 /*non_constant_p=*/NULL);
27189 if (vec == NULL)
27190 expression_list = error_mark_node;
27191 else
27193 expression_list = build_tree_list_vec (vec);
27194 release_tree_vector (vec);
27197 cast = build_functional_cast (type, expression_list,
27198 tf_warning_or_error);
27199 /* [expr.const]/1: In an integral constant expression "only type
27200 conversions to integral or enumeration type can be used". */
27201 if (TREE_CODE (type) == TYPE_DECL)
27202 type = TREE_TYPE (type);
27203 if (cast != error_mark_node
27204 && !cast_valid_in_integral_constant_expression_p (type)
27205 && cp_parser_non_integral_constant_expression (parser,
27206 NIC_CONSTRUCTOR))
27207 return error_mark_node;
27209 /* Create a location of the form:
27210 float(i)
27211 ^~~~~~~~
27212 with caret == start at the start of the type name,
27213 finishing at the closing paren. */
27214 location_t finish_loc
27215 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27216 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27217 cast.set_location (combined_loc);
27218 return cast;
27221 /* Save the tokens that make up the body of a member function defined
27222 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27223 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27224 specifiers applied to the declaration. Returns the FUNCTION_DECL
27225 for the member function. */
27227 static tree
27228 cp_parser_save_member_function_body (cp_parser* parser,
27229 cp_decl_specifier_seq *decl_specifiers,
27230 cp_declarator *declarator,
27231 tree attributes)
27233 cp_token *first;
27234 cp_token *last;
27235 tree fn;
27236 bool function_try_block = false;
27238 /* Create the FUNCTION_DECL. */
27239 fn = grokmethod (decl_specifiers, declarator, attributes);
27240 cp_finalize_omp_declare_simd (parser, fn);
27241 cp_finalize_oacc_routine (parser, fn, true);
27242 /* If something went badly wrong, bail out now. */
27243 if (fn == error_mark_node)
27245 /* If there's a function-body, skip it. */
27246 if (cp_parser_token_starts_function_definition_p
27247 (cp_lexer_peek_token (parser->lexer)))
27248 cp_parser_skip_to_end_of_block_or_statement (parser);
27249 return error_mark_node;
27252 /* Remember it, if there default args to post process. */
27253 cp_parser_save_default_args (parser, fn);
27255 /* Save away the tokens that make up the body of the
27256 function. */
27257 first = parser->lexer->next_token;
27259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27260 cp_lexer_consume_token (parser->lexer);
27261 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27262 RID_TRANSACTION_ATOMIC))
27264 cp_lexer_consume_token (parser->lexer);
27265 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27267 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27268 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27269 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27270 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27271 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27273 cp_lexer_consume_token (parser->lexer);
27274 cp_lexer_consume_token (parser->lexer);
27275 cp_lexer_consume_token (parser->lexer);
27276 cp_lexer_consume_token (parser->lexer);
27277 cp_lexer_consume_token (parser->lexer);
27279 else
27280 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27281 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27283 cp_lexer_consume_token (parser->lexer);
27284 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27285 break;
27289 /* Handle function try blocks. */
27290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27292 cp_lexer_consume_token (parser->lexer);
27293 function_try_block = true;
27295 /* We can have braced-init-list mem-initializers before the fn body. */
27296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27298 cp_lexer_consume_token (parser->lexer);
27299 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27301 /* cache_group will stop after an un-nested { } pair, too. */
27302 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27303 break;
27305 /* variadic mem-inits have ... after the ')'. */
27306 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27307 cp_lexer_consume_token (parser->lexer);
27310 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27311 /* Handle function try blocks. */
27312 if (function_try_block)
27313 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27314 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27315 last = parser->lexer->next_token;
27317 /* Save away the inline definition; we will process it when the
27318 class is complete. */
27319 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27320 DECL_PENDING_INLINE_P (fn) = 1;
27322 /* We need to know that this was defined in the class, so that
27323 friend templates are handled correctly. */
27324 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27326 /* Add FN to the queue of functions to be parsed later. */
27327 vec_safe_push (unparsed_funs_with_definitions, fn);
27329 return fn;
27332 /* Save the tokens that make up the in-class initializer for a non-static
27333 data member. Returns a DEFAULT_ARG. */
27335 static tree
27336 cp_parser_save_nsdmi (cp_parser* parser)
27338 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27341 /* Parse a template-argument-list, as well as the trailing ">" (but
27342 not the opening "<"). See cp_parser_template_argument_list for the
27343 return value. */
27345 static tree
27346 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27348 tree arguments;
27349 tree saved_scope;
27350 tree saved_qualifying_scope;
27351 tree saved_object_scope;
27352 bool saved_greater_than_is_operator_p;
27353 int saved_unevaluated_operand;
27354 int saved_inhibit_evaluation_warnings;
27356 /* [temp.names]
27358 When parsing a template-id, the first non-nested `>' is taken as
27359 the end of the template-argument-list rather than a greater-than
27360 operator. */
27361 saved_greater_than_is_operator_p
27362 = parser->greater_than_is_operator_p;
27363 parser->greater_than_is_operator_p = false;
27364 /* Parsing the argument list may modify SCOPE, so we save it
27365 here. */
27366 saved_scope = parser->scope;
27367 saved_qualifying_scope = parser->qualifying_scope;
27368 saved_object_scope = parser->object_scope;
27369 /* We need to evaluate the template arguments, even though this
27370 template-id may be nested within a "sizeof". */
27371 saved_unevaluated_operand = cp_unevaluated_operand;
27372 cp_unevaluated_operand = 0;
27373 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27374 c_inhibit_evaluation_warnings = 0;
27375 /* Parse the template-argument-list itself. */
27376 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27377 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27378 arguments = NULL_TREE;
27379 else
27380 arguments = cp_parser_template_argument_list (parser);
27381 /* Look for the `>' that ends the template-argument-list. If we find
27382 a '>>' instead, it's probably just a typo. */
27383 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27385 if (cxx_dialect != cxx98)
27387 /* In C++0x, a `>>' in a template argument list or cast
27388 expression is considered to be two separate `>'
27389 tokens. So, change the current token to a `>', but don't
27390 consume it: it will be consumed later when the outer
27391 template argument list (or cast expression) is parsed.
27392 Note that this replacement of `>' for `>>' is necessary
27393 even if we are parsing tentatively: in the tentative
27394 case, after calling
27395 cp_parser_enclosed_template_argument_list we will always
27396 throw away all of the template arguments and the first
27397 closing `>', either because the template argument list
27398 was erroneous or because we are replacing those tokens
27399 with a CPP_TEMPLATE_ID token. The second `>' (which will
27400 not have been thrown away) is needed either to close an
27401 outer template argument list or to complete a new-style
27402 cast. */
27403 cp_token *token = cp_lexer_peek_token (parser->lexer);
27404 token->type = CPP_GREATER;
27406 else if (!saved_greater_than_is_operator_p)
27408 /* If we're in a nested template argument list, the '>>' has
27409 to be a typo for '> >'. We emit the error message, but we
27410 continue parsing and we push a '>' as next token, so that
27411 the argument list will be parsed correctly. Note that the
27412 global source location is still on the token before the
27413 '>>', so we need to say explicitly where we want it. */
27414 cp_token *token = cp_lexer_peek_token (parser->lexer);
27415 gcc_rich_location richloc (token->location);
27416 richloc.add_fixit_replace ("> >");
27417 error_at (&richloc, "%<>>%> should be %<> >%> "
27418 "within a nested template argument list");
27420 token->type = CPP_GREATER;
27422 else
27424 /* If this is not a nested template argument list, the '>>'
27425 is a typo for '>'. Emit an error message and continue.
27426 Same deal about the token location, but here we can get it
27427 right by consuming the '>>' before issuing the diagnostic. */
27428 cp_token *token = cp_lexer_consume_token (parser->lexer);
27429 error_at (token->location,
27430 "spurious %<>>%>, use %<>%> to terminate "
27431 "a template argument list");
27434 else
27435 cp_parser_skip_to_end_of_template_parameter_list (parser);
27436 /* The `>' token might be a greater-than operator again now. */
27437 parser->greater_than_is_operator_p
27438 = saved_greater_than_is_operator_p;
27439 /* Restore the SAVED_SCOPE. */
27440 parser->scope = saved_scope;
27441 parser->qualifying_scope = saved_qualifying_scope;
27442 parser->object_scope = saved_object_scope;
27443 cp_unevaluated_operand = saved_unevaluated_operand;
27444 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27446 return arguments;
27449 /* MEMBER_FUNCTION is a member function, or a friend. If default
27450 arguments, or the body of the function have not yet been parsed,
27451 parse them now. */
27453 static void
27454 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27456 timevar_push (TV_PARSE_INMETH);
27457 /* If this member is a template, get the underlying
27458 FUNCTION_DECL. */
27459 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27460 member_function = DECL_TEMPLATE_RESULT (member_function);
27462 /* There should not be any class definitions in progress at this
27463 point; the bodies of members are only parsed outside of all class
27464 definitions. */
27465 gcc_assert (parser->num_classes_being_defined == 0);
27466 /* While we're parsing the member functions we might encounter more
27467 classes. We want to handle them right away, but we don't want
27468 them getting mixed up with functions that are currently in the
27469 queue. */
27470 push_unparsed_function_queues (parser);
27472 /* Make sure that any template parameters are in scope. */
27473 maybe_begin_member_template_processing (member_function);
27475 /* If the body of the function has not yet been parsed, parse it
27476 now. */
27477 if (DECL_PENDING_INLINE_P (member_function))
27479 tree function_scope;
27480 cp_token_cache *tokens;
27482 /* The function is no longer pending; we are processing it. */
27483 tokens = DECL_PENDING_INLINE_INFO (member_function);
27484 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27485 DECL_PENDING_INLINE_P (member_function) = 0;
27487 /* If this is a local class, enter the scope of the containing
27488 function. */
27489 function_scope = current_function_decl;
27490 if (function_scope)
27491 push_function_context ();
27493 /* Push the body of the function onto the lexer stack. */
27494 cp_parser_push_lexer_for_tokens (parser, tokens);
27496 /* Let the front end know that we going to be defining this
27497 function. */
27498 start_preparsed_function (member_function, NULL_TREE,
27499 SF_PRE_PARSED | SF_INCLASS_INLINE);
27501 /* Don't do access checking if it is a templated function. */
27502 if (processing_template_decl)
27503 push_deferring_access_checks (dk_no_check);
27505 /* #pragma omp declare reduction needs special parsing. */
27506 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27508 parser->lexer->in_pragma = true;
27509 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27510 finish_function (/*inline_p=*/true);
27511 cp_check_omp_declare_reduction (member_function);
27513 else
27514 /* Now, parse the body of the function. */
27515 cp_parser_function_definition_after_declarator (parser,
27516 /*inline_p=*/true);
27518 if (processing_template_decl)
27519 pop_deferring_access_checks ();
27521 /* Leave the scope of the containing function. */
27522 if (function_scope)
27523 pop_function_context ();
27524 cp_parser_pop_lexer (parser);
27527 /* Remove any template parameters from the symbol table. */
27528 maybe_end_member_template_processing ();
27530 /* Restore the queue. */
27531 pop_unparsed_function_queues (parser);
27532 timevar_pop (TV_PARSE_INMETH);
27535 /* If DECL contains any default args, remember it on the unparsed
27536 functions queue. */
27538 static void
27539 cp_parser_save_default_args (cp_parser* parser, tree decl)
27541 tree probe;
27543 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27544 probe;
27545 probe = TREE_CHAIN (probe))
27546 if (TREE_PURPOSE (probe))
27548 cp_default_arg_entry entry = {current_class_type, decl};
27549 vec_safe_push (unparsed_funs_with_default_args, entry);
27550 break;
27554 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27555 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27556 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27557 from the parameter-type-list. */
27559 static tree
27560 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27561 tree default_arg, tree parmtype)
27563 cp_token_cache *tokens;
27564 tree parsed_arg;
27565 bool dummy;
27567 if (default_arg == error_mark_node)
27568 return error_mark_node;
27570 /* Push the saved tokens for the default argument onto the parser's
27571 lexer stack. */
27572 tokens = DEFARG_TOKENS (default_arg);
27573 cp_parser_push_lexer_for_tokens (parser, tokens);
27575 start_lambda_scope (decl);
27577 /* Parse the default argument. */
27578 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27579 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27580 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27582 finish_lambda_scope ();
27584 if (parsed_arg == error_mark_node)
27585 cp_parser_skip_to_end_of_statement (parser);
27587 if (!processing_template_decl)
27589 /* In a non-template class, check conversions now. In a template,
27590 we'll wait and instantiate these as needed. */
27591 if (TREE_CODE (decl) == PARM_DECL)
27592 parsed_arg = check_default_argument (parmtype, parsed_arg,
27593 tf_warning_or_error);
27594 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27595 parsed_arg = error_mark_node;
27596 else
27597 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27600 /* If the token stream has not been completely used up, then
27601 there was extra junk after the end of the default
27602 argument. */
27603 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27605 if (TREE_CODE (decl) == PARM_DECL)
27606 cp_parser_error (parser, "expected %<,%>");
27607 else
27608 cp_parser_error (parser, "expected %<;%>");
27611 /* Revert to the main lexer. */
27612 cp_parser_pop_lexer (parser);
27614 return parsed_arg;
27617 /* FIELD is a non-static data member with an initializer which we saved for
27618 later; parse it now. */
27620 static void
27621 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27623 tree def;
27625 maybe_begin_member_template_processing (field);
27627 push_unparsed_function_queues (parser);
27628 def = cp_parser_late_parse_one_default_arg (parser, field,
27629 DECL_INITIAL (field),
27630 NULL_TREE);
27631 pop_unparsed_function_queues (parser);
27633 maybe_end_member_template_processing ();
27635 DECL_INITIAL (field) = def;
27638 /* FN is a FUNCTION_DECL which may contains a parameter with an
27639 unparsed DEFAULT_ARG. Parse the default args now. This function
27640 assumes that the current scope is the scope in which the default
27641 argument should be processed. */
27643 static void
27644 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27646 bool saved_local_variables_forbidden_p;
27647 tree parm, parmdecl;
27649 /* While we're parsing the default args, we might (due to the
27650 statement expression extension) encounter more classes. We want
27651 to handle them right away, but we don't want them getting mixed
27652 up with default args that are currently in the queue. */
27653 push_unparsed_function_queues (parser);
27655 /* Local variable names (and the `this' keyword) may not appear
27656 in a default argument. */
27657 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27658 parser->local_variables_forbidden_p = true;
27660 push_defarg_context (fn);
27662 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27663 parmdecl = DECL_ARGUMENTS (fn);
27664 parm && parm != void_list_node;
27665 parm = TREE_CHAIN (parm),
27666 parmdecl = DECL_CHAIN (parmdecl))
27668 tree default_arg = TREE_PURPOSE (parm);
27669 tree parsed_arg;
27670 vec<tree, va_gc> *insts;
27671 tree copy;
27672 unsigned ix;
27674 if (!default_arg)
27675 continue;
27677 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27678 /* This can happen for a friend declaration for a function
27679 already declared with default arguments. */
27680 continue;
27682 parsed_arg
27683 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27684 default_arg,
27685 TREE_VALUE (parm));
27686 TREE_PURPOSE (parm) = parsed_arg;
27688 /* Update any instantiations we've already created. */
27689 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27690 vec_safe_iterate (insts, ix, &copy); ix++)
27691 TREE_PURPOSE (copy) = parsed_arg;
27694 pop_defarg_context ();
27696 /* Make sure no default arg is missing. */
27697 check_default_args (fn);
27699 /* Restore the state of local_variables_forbidden_p. */
27700 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27702 /* Restore the queue. */
27703 pop_unparsed_function_queues (parser);
27706 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27708 sizeof ... ( identifier )
27710 where the 'sizeof' token has already been consumed. */
27712 static tree
27713 cp_parser_sizeof_pack (cp_parser *parser)
27715 /* Consume the `...'. */
27716 cp_lexer_consume_token (parser->lexer);
27717 maybe_warn_variadic_templates ();
27719 matching_parens parens;
27720 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27721 if (paren)
27722 parens.consume_open (parser);
27723 else
27724 permerror (cp_lexer_peek_token (parser->lexer)->location,
27725 "%<sizeof...%> argument must be surrounded by parentheses");
27727 cp_token *token = cp_lexer_peek_token (parser->lexer);
27728 tree name = cp_parser_identifier (parser);
27729 if (name == error_mark_node)
27730 return error_mark_node;
27731 /* The name is not qualified. */
27732 parser->scope = NULL_TREE;
27733 parser->qualifying_scope = NULL_TREE;
27734 parser->object_scope = NULL_TREE;
27735 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27736 if (expr == error_mark_node)
27737 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27738 token->location);
27739 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27740 expr = TREE_TYPE (expr);
27741 else if (TREE_CODE (expr) == CONST_DECL)
27742 expr = DECL_INITIAL (expr);
27743 expr = make_pack_expansion (expr);
27744 PACK_EXPANSION_SIZEOF_P (expr) = true;
27746 if (paren)
27747 parens.require_close (parser);
27749 return expr;
27752 /* Parse the operand of `sizeof' (or a similar operator). Returns
27753 either a TYPE or an expression, depending on the form of the
27754 input. The KEYWORD indicates which kind of expression we have
27755 encountered. */
27757 static tree
27758 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27760 tree expr = NULL_TREE;
27761 const char *saved_message;
27762 char *tmp;
27763 bool saved_integral_constant_expression_p;
27764 bool saved_non_integral_constant_expression_p;
27766 /* If it's a `...', then we are computing the length of a parameter
27767 pack. */
27768 if (keyword == RID_SIZEOF
27769 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27770 return cp_parser_sizeof_pack (parser);
27772 /* Types cannot be defined in a `sizeof' expression. Save away the
27773 old message. */
27774 saved_message = parser->type_definition_forbidden_message;
27775 /* And create the new one. */
27776 tmp = concat ("types may not be defined in %<",
27777 IDENTIFIER_POINTER (ridpointers[keyword]),
27778 "%> expressions", NULL);
27779 parser->type_definition_forbidden_message = tmp;
27781 /* The restrictions on constant-expressions do not apply inside
27782 sizeof expressions. */
27783 saved_integral_constant_expression_p
27784 = parser->integral_constant_expression_p;
27785 saved_non_integral_constant_expression_p
27786 = parser->non_integral_constant_expression_p;
27787 parser->integral_constant_expression_p = false;
27789 /* Do not actually evaluate the expression. */
27790 ++cp_unevaluated_operand;
27791 ++c_inhibit_evaluation_warnings;
27792 /* If it's a `(', then we might be looking at the type-id
27793 construction. */
27794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27796 tree type = NULL_TREE;
27798 /* We can't be sure yet whether we're looking at a type-id or an
27799 expression. */
27800 cp_parser_parse_tentatively (parser);
27802 matching_parens parens;
27803 parens.consume_open (parser);
27805 /* Note: as a GNU Extension, compound literals are considered
27806 postfix-expressions as they are in C99, so they are valid
27807 arguments to sizeof. See comment in cp_parser_cast_expression
27808 for details. */
27809 if (cp_parser_compound_literal_p (parser))
27810 cp_parser_simulate_error (parser);
27811 else
27813 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27814 parser->in_type_id_in_expr_p = true;
27815 /* Look for the type-id. */
27816 type = cp_parser_type_id (parser);
27817 /* Look for the closing `)'. */
27818 parens.require_close (parser);
27819 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27822 /* If all went well, then we're done. */
27823 if (cp_parser_parse_definitely (parser))
27825 cp_decl_specifier_seq decl_specs;
27827 /* Build a trivial decl-specifier-seq. */
27828 clear_decl_specs (&decl_specs);
27829 decl_specs.type = type;
27831 /* Call grokdeclarator to figure out what type this is. */
27832 expr = grokdeclarator (NULL,
27833 &decl_specs,
27834 TYPENAME,
27835 /*initialized=*/0,
27836 /*attrlist=*/NULL);
27840 /* If the type-id production did not work out, then we must be
27841 looking at the unary-expression production. */
27842 if (!expr)
27843 expr = cp_parser_unary_expression (parser);
27845 /* Go back to evaluating expressions. */
27846 --cp_unevaluated_operand;
27847 --c_inhibit_evaluation_warnings;
27849 /* Free the message we created. */
27850 free (tmp);
27851 /* And restore the old one. */
27852 parser->type_definition_forbidden_message = saved_message;
27853 parser->integral_constant_expression_p
27854 = saved_integral_constant_expression_p;
27855 parser->non_integral_constant_expression_p
27856 = saved_non_integral_constant_expression_p;
27858 return expr;
27861 /* If the current declaration has no declarator, return true. */
27863 static bool
27864 cp_parser_declares_only_class_p (cp_parser *parser)
27866 /* If the next token is a `;' or a `,' then there is no
27867 declarator. */
27868 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27869 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27872 /* Update the DECL_SPECS to reflect the storage class indicated by
27873 KEYWORD. */
27875 static void
27876 cp_parser_set_storage_class (cp_parser *parser,
27877 cp_decl_specifier_seq *decl_specs,
27878 enum rid keyword,
27879 cp_token *token)
27881 cp_storage_class storage_class;
27883 if (parser->in_unbraced_linkage_specification_p)
27885 error_at (token->location, "invalid use of %qD in linkage specification",
27886 ridpointers[keyword]);
27887 return;
27889 else if (decl_specs->storage_class != sc_none)
27891 decl_specs->conflicting_specifiers_p = true;
27892 return;
27895 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27896 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27897 && decl_specs->gnu_thread_keyword_p)
27899 pedwarn (decl_specs->locations[ds_thread], 0,
27900 "%<__thread%> before %qD", ridpointers[keyword]);
27903 switch (keyword)
27905 case RID_AUTO:
27906 storage_class = sc_auto;
27907 break;
27908 case RID_REGISTER:
27909 storage_class = sc_register;
27910 break;
27911 case RID_STATIC:
27912 storage_class = sc_static;
27913 break;
27914 case RID_EXTERN:
27915 storage_class = sc_extern;
27916 break;
27917 case RID_MUTABLE:
27918 storage_class = sc_mutable;
27919 break;
27920 default:
27921 gcc_unreachable ();
27923 decl_specs->storage_class = storage_class;
27924 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27926 /* A storage class specifier cannot be applied alongside a typedef
27927 specifier. If there is a typedef specifier present then set
27928 conflicting_specifiers_p which will trigger an error later
27929 on in grokdeclarator. */
27930 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27931 decl_specs->conflicting_specifiers_p = true;
27934 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27935 is true, the type is a class or enum definition. */
27937 static void
27938 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27939 tree type_spec,
27940 cp_token *token,
27941 bool type_definition_p)
27943 decl_specs->any_specifiers_p = true;
27945 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27946 (with, for example, in "typedef int wchar_t;") we remember that
27947 this is what happened. In system headers, we ignore these
27948 declarations so that G++ can work with system headers that are not
27949 C++-safe. */
27950 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27951 && !type_definition_p
27952 && (type_spec == boolean_type_node
27953 || type_spec == char16_type_node
27954 || type_spec == char32_type_node
27955 || type_spec == wchar_type_node)
27956 && (decl_specs->type
27957 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27958 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27959 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27960 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27962 decl_specs->redefined_builtin_type = type_spec;
27963 set_and_check_decl_spec_loc (decl_specs,
27964 ds_redefined_builtin_type_spec,
27965 token);
27966 if (!decl_specs->type)
27968 decl_specs->type = type_spec;
27969 decl_specs->type_definition_p = false;
27970 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27973 else if (decl_specs->type)
27974 decl_specs->multiple_types_p = true;
27975 else
27977 decl_specs->type = type_spec;
27978 decl_specs->type_definition_p = type_definition_p;
27979 decl_specs->redefined_builtin_type = NULL_TREE;
27980 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27984 /* True iff TOKEN is the GNU keyword __thread. */
27986 static bool
27987 token_is__thread (cp_token *token)
27989 gcc_assert (token->keyword == RID_THREAD);
27990 return id_equal (token->u.value, "__thread");
27993 /* Set the location for a declarator specifier and check if it is
27994 duplicated.
27996 DECL_SPECS is the sequence of declarator specifiers onto which to
27997 set the location.
27999 DS is the single declarator specifier to set which location is to
28000 be set onto the existing sequence of declarators.
28002 LOCATION is the location for the declarator specifier to
28003 consider. */
28005 static void
28006 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28007 cp_decl_spec ds, cp_token *token)
28009 gcc_assert (ds < ds_last);
28011 if (decl_specs == NULL)
28012 return;
28014 source_location location = token->location;
28016 if (decl_specs->locations[ds] == 0)
28018 decl_specs->locations[ds] = location;
28019 if (ds == ds_thread)
28020 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28022 else
28024 if (ds == ds_long)
28026 if (decl_specs->locations[ds_long_long] != 0)
28027 error_at (location,
28028 "%<long long long%> is too long for GCC");
28029 else
28031 decl_specs->locations[ds_long_long] = location;
28032 pedwarn_cxx98 (location,
28033 OPT_Wlong_long,
28034 "ISO C++ 1998 does not support %<long long%>");
28037 else if (ds == ds_thread)
28039 bool gnu = token_is__thread (token);
28040 if (gnu != decl_specs->gnu_thread_keyword_p)
28041 error_at (location,
28042 "both %<__thread%> and %<thread_local%> specified");
28043 else
28045 gcc_rich_location richloc (location);
28046 richloc.add_fixit_remove ();
28047 error_at (&richloc, "duplicate %qD", token->u.value);
28050 else
28052 static const char *const decl_spec_names[] = {
28053 "signed",
28054 "unsigned",
28055 "short",
28056 "long",
28057 "const",
28058 "volatile",
28059 "restrict",
28060 "inline",
28061 "virtual",
28062 "explicit",
28063 "friend",
28064 "typedef",
28065 "using",
28066 "constexpr",
28067 "__complex"
28069 gcc_rich_location richloc (location);
28070 richloc.add_fixit_remove ();
28071 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28076 /* Return true iff the declarator specifier DS is present in the
28077 sequence of declarator specifiers DECL_SPECS. */
28079 bool
28080 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28081 cp_decl_spec ds)
28083 gcc_assert (ds < ds_last);
28085 if (decl_specs == NULL)
28086 return false;
28088 return decl_specs->locations[ds] != 0;
28091 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28092 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28094 static bool
28095 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28097 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28100 /* Issue an error message indicating that TOKEN_DESC was expected.
28101 If KEYWORD is true, it indicated this function is called by
28102 cp_parser_require_keword and the required token can only be
28103 a indicated keyword.
28105 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28106 within any error as the location of an "opening" token matching
28107 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28108 RT_CLOSE_PAREN). */
28110 static void
28111 cp_parser_required_error (cp_parser *parser,
28112 required_token token_desc,
28113 bool keyword,
28114 location_t matching_location)
28116 if (cp_parser_simulate_error (parser))
28117 return;
28119 const char *gmsgid = NULL;
28120 switch (token_desc)
28122 case RT_NEW:
28123 gmsgid = G_("expected %<new%>");
28124 break;
28125 case RT_DELETE:
28126 gmsgid = G_("expected %<delete%>");
28127 break;
28128 case RT_RETURN:
28129 gmsgid = G_("expected %<return%>");
28130 break;
28131 case RT_WHILE:
28132 gmsgid = G_("expected %<while%>");
28133 break;
28134 case RT_EXTERN:
28135 gmsgid = G_("expected %<extern%>");
28136 break;
28137 case RT_STATIC_ASSERT:
28138 gmsgid = G_("expected %<static_assert%>");
28139 break;
28140 case RT_DECLTYPE:
28141 gmsgid = G_("expected %<decltype%>");
28142 break;
28143 case RT_OPERATOR:
28144 gmsgid = G_("expected %<operator%>");
28145 break;
28146 case RT_CLASS:
28147 gmsgid = G_("expected %<class%>");
28148 break;
28149 case RT_TEMPLATE:
28150 gmsgid = G_("expected %<template%>");
28151 break;
28152 case RT_NAMESPACE:
28153 gmsgid = G_("expected %<namespace%>");
28154 break;
28155 case RT_USING:
28156 gmsgid = G_("expected %<using%>");
28157 break;
28158 case RT_ASM:
28159 gmsgid = G_("expected %<asm%>");
28160 break;
28161 case RT_TRY:
28162 gmsgid = G_("expected %<try%>");
28163 break;
28164 case RT_CATCH:
28165 gmsgid = G_("expected %<catch%>");
28166 break;
28167 case RT_THROW:
28168 gmsgid = G_("expected %<throw%>");
28169 break;
28170 case RT_LABEL:
28171 gmsgid = G_("expected %<__label__%>");
28172 break;
28173 case RT_AT_TRY:
28174 gmsgid = G_("expected %<@try%>");
28175 break;
28176 case RT_AT_SYNCHRONIZED:
28177 gmsgid = G_("expected %<@synchronized%>");
28178 break;
28179 case RT_AT_THROW:
28180 gmsgid = G_("expected %<@throw%>");
28181 break;
28182 case RT_TRANSACTION_ATOMIC:
28183 gmsgid = G_("expected %<__transaction_atomic%>");
28184 break;
28185 case RT_TRANSACTION_RELAXED:
28186 gmsgid = G_("expected %<__transaction_relaxed%>");
28187 break;
28188 default:
28189 break;
28192 if (!gmsgid && !keyword)
28194 switch (token_desc)
28196 case RT_SEMICOLON:
28197 gmsgid = G_("expected %<;%>");
28198 break;
28199 case RT_OPEN_PAREN:
28200 gmsgid = G_("expected %<(%>");
28201 break;
28202 case RT_CLOSE_BRACE:
28203 gmsgid = G_("expected %<}%>");
28204 break;
28205 case RT_OPEN_BRACE:
28206 gmsgid = G_("expected %<{%>");
28207 break;
28208 case RT_CLOSE_SQUARE:
28209 gmsgid = G_("expected %<]%>");
28210 break;
28211 case RT_OPEN_SQUARE:
28212 gmsgid = G_("expected %<[%>");
28213 break;
28214 case RT_COMMA:
28215 gmsgid = G_("expected %<,%>");
28216 break;
28217 case RT_SCOPE:
28218 gmsgid = G_("expected %<::%>");
28219 break;
28220 case RT_LESS:
28221 gmsgid = G_("expected %<<%>");
28222 break;
28223 case RT_GREATER:
28224 gmsgid = G_("expected %<>%>");
28225 break;
28226 case RT_EQ:
28227 gmsgid = G_("expected %<=%>");
28228 break;
28229 case RT_ELLIPSIS:
28230 gmsgid = G_("expected %<...%>");
28231 break;
28232 case RT_MULT:
28233 gmsgid = G_("expected %<*%>");
28234 break;
28235 case RT_COMPL:
28236 gmsgid = G_("expected %<~%>");
28237 break;
28238 case RT_COLON:
28239 gmsgid = G_("expected %<:%>");
28240 break;
28241 case RT_COLON_SCOPE:
28242 gmsgid = G_("expected %<:%> or %<::%>");
28243 break;
28244 case RT_CLOSE_PAREN:
28245 gmsgid = G_("expected %<)%>");
28246 break;
28247 case RT_COMMA_CLOSE_PAREN:
28248 gmsgid = G_("expected %<,%> or %<)%>");
28249 break;
28250 case RT_PRAGMA_EOL:
28251 gmsgid = G_("expected end of line");
28252 break;
28253 case RT_NAME:
28254 gmsgid = G_("expected identifier");
28255 break;
28256 case RT_SELECT:
28257 gmsgid = G_("expected selection-statement");
28258 break;
28259 case RT_ITERATION:
28260 gmsgid = G_("expected iteration-statement");
28261 break;
28262 case RT_JUMP:
28263 gmsgid = G_("expected jump-statement");
28264 break;
28265 case RT_CLASS_KEY:
28266 gmsgid = G_("expected class-key");
28267 break;
28268 case RT_CLASS_TYPENAME_TEMPLATE:
28269 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28270 break;
28271 default:
28272 gcc_unreachable ();
28276 if (gmsgid)
28277 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28281 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28282 issue an error message indicating that TOKEN_DESC was expected.
28284 Returns the token consumed, if the token had the appropriate type.
28285 Otherwise, returns NULL.
28287 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28288 within any error as the location of an "opening" token matching
28289 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28290 RT_CLOSE_PAREN). */
28292 static cp_token *
28293 cp_parser_require (cp_parser* parser,
28294 enum cpp_ttype type,
28295 required_token token_desc,
28296 location_t matching_location)
28298 if (cp_lexer_next_token_is (parser->lexer, type))
28299 return cp_lexer_consume_token (parser->lexer);
28300 else
28302 /* Output the MESSAGE -- unless we're parsing tentatively. */
28303 if (!cp_parser_simulate_error (parser))
28304 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28305 matching_location);
28306 return NULL;
28310 /* An error message is produced if the next token is not '>'.
28311 All further tokens are skipped until the desired token is
28312 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28314 static void
28315 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28317 /* Current level of '< ... >'. */
28318 unsigned level = 0;
28319 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28320 unsigned nesting_depth = 0;
28322 /* Are we ready, yet? If not, issue error message. */
28323 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28324 return;
28326 /* Skip tokens until the desired token is found. */
28327 while (true)
28329 /* Peek at the next token. */
28330 switch (cp_lexer_peek_token (parser->lexer)->type)
28332 case CPP_LESS:
28333 if (!nesting_depth)
28334 ++level;
28335 break;
28337 case CPP_RSHIFT:
28338 if (cxx_dialect == cxx98)
28339 /* C++0x views the `>>' operator as two `>' tokens, but
28340 C++98 does not. */
28341 break;
28342 else if (!nesting_depth && level-- == 0)
28344 /* We've hit a `>>' where the first `>' closes the
28345 template argument list, and the second `>' is
28346 spurious. Just consume the `>>' and stop; we've
28347 already produced at least one error. */
28348 cp_lexer_consume_token (parser->lexer);
28349 return;
28351 /* Fall through for C++0x, so we handle the second `>' in
28352 the `>>'. */
28353 gcc_fallthrough ();
28355 case CPP_GREATER:
28356 if (!nesting_depth && level-- == 0)
28358 /* We've reached the token we want, consume it and stop. */
28359 cp_lexer_consume_token (parser->lexer);
28360 return;
28362 break;
28364 case CPP_OPEN_PAREN:
28365 case CPP_OPEN_SQUARE:
28366 ++nesting_depth;
28367 break;
28369 case CPP_CLOSE_PAREN:
28370 case CPP_CLOSE_SQUARE:
28371 if (nesting_depth-- == 0)
28372 return;
28373 break;
28375 case CPP_EOF:
28376 case CPP_PRAGMA_EOL:
28377 case CPP_SEMICOLON:
28378 case CPP_OPEN_BRACE:
28379 case CPP_CLOSE_BRACE:
28380 /* The '>' was probably forgotten, don't look further. */
28381 return;
28383 default:
28384 break;
28387 /* Consume this token. */
28388 cp_lexer_consume_token (parser->lexer);
28392 /* If the next token is the indicated keyword, consume it. Otherwise,
28393 issue an error message indicating that TOKEN_DESC was expected.
28395 Returns the token consumed, if the token had the appropriate type.
28396 Otherwise, returns NULL. */
28398 static cp_token *
28399 cp_parser_require_keyword (cp_parser* parser,
28400 enum rid keyword,
28401 required_token token_desc)
28403 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28405 if (token && token->keyword != keyword)
28407 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28408 UNKNOWN_LOCATION);
28409 return NULL;
28412 return token;
28415 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28416 function-definition. */
28418 static bool
28419 cp_parser_token_starts_function_definition_p (cp_token* token)
28421 return (/* An ordinary function-body begins with an `{'. */
28422 token->type == CPP_OPEN_BRACE
28423 /* A ctor-initializer begins with a `:'. */
28424 || token->type == CPP_COLON
28425 /* A function-try-block begins with `try'. */
28426 || token->keyword == RID_TRY
28427 /* A function-transaction-block begins with `__transaction_atomic'
28428 or `__transaction_relaxed'. */
28429 || token->keyword == RID_TRANSACTION_ATOMIC
28430 || token->keyword == RID_TRANSACTION_RELAXED
28431 /* The named return value extension begins with `return'. */
28432 || token->keyword == RID_RETURN);
28435 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28436 definition. */
28438 static bool
28439 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28441 cp_token *token;
28443 token = cp_lexer_peek_token (parser->lexer);
28444 return (token->type == CPP_OPEN_BRACE
28445 || (token->type == CPP_COLON
28446 && !parser->colon_doesnt_start_class_def_p));
28449 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28450 C++0x) ending a template-argument. */
28452 static bool
28453 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28455 cp_token *token;
28457 token = cp_lexer_peek_token (parser->lexer);
28458 return (token->type == CPP_COMMA
28459 || token->type == CPP_GREATER
28460 || token->type == CPP_ELLIPSIS
28461 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28464 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28465 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28467 static bool
28468 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28469 size_t n)
28471 cp_token *token;
28473 token = cp_lexer_peek_nth_token (parser->lexer, n);
28474 if (token->type == CPP_LESS)
28475 return true;
28476 /* Check for the sequence `<::' in the original code. It would be lexed as
28477 `[:', where `[' is a digraph, and there is no whitespace before
28478 `:'. */
28479 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28481 cp_token *token2;
28482 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28483 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28484 return true;
28486 return false;
28489 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28490 or none_type otherwise. */
28492 static enum tag_types
28493 cp_parser_token_is_class_key (cp_token* token)
28495 switch (token->keyword)
28497 case RID_CLASS:
28498 return class_type;
28499 case RID_STRUCT:
28500 return record_type;
28501 case RID_UNION:
28502 return union_type;
28504 default:
28505 return none_type;
28509 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28510 or none_type otherwise or if the token is null. */
28512 static enum tag_types
28513 cp_parser_token_is_type_parameter_key (cp_token* token)
28515 if (!token)
28516 return none_type;
28518 switch (token->keyword)
28520 case RID_CLASS:
28521 return class_type;
28522 case RID_TYPENAME:
28523 return typename_type;
28525 default:
28526 return none_type;
28530 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28532 static void
28533 cp_parser_check_class_key (enum tag_types class_key, tree type)
28535 if (type == error_mark_node)
28536 return;
28537 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28539 if (permerror (input_location, "%qs tag used in naming %q#T",
28540 class_key == union_type ? "union"
28541 : class_key == record_type ? "struct" : "class",
28542 type))
28543 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28544 "%q#T was previously declared here", type);
28548 /* Issue an error message if DECL is redeclared with different
28549 access than its original declaration [class.access.spec/3].
28550 This applies to nested classes, nested class templates and
28551 enumerations [class.mem/1]. */
28553 static void
28554 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28556 if (!decl
28557 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28558 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28559 return;
28561 if ((TREE_PRIVATE (decl)
28562 != (current_access_specifier == access_private_node))
28563 || (TREE_PROTECTED (decl)
28564 != (current_access_specifier == access_protected_node)))
28565 error_at (location, "%qD redeclared with different access", decl);
28568 /* Look for the `template' keyword, as a syntactic disambiguator.
28569 Return TRUE iff it is present, in which case it will be
28570 consumed. */
28572 static bool
28573 cp_parser_optional_template_keyword (cp_parser *parser)
28575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28577 /* In C++98 the `template' keyword can only be used within templates;
28578 outside templates the parser can always figure out what is a
28579 template and what is not. In C++11, per the resolution of DR 468,
28580 `template' is allowed in cases where it is not strictly necessary. */
28581 if (!processing_template_decl
28582 && pedantic && cxx_dialect == cxx98)
28584 cp_token *token = cp_lexer_peek_token (parser->lexer);
28585 pedwarn (token->location, OPT_Wpedantic,
28586 "in C++98 %<template%> (as a disambiguator) is only "
28587 "allowed within templates");
28588 /* If this part of the token stream is rescanned, the same
28589 error message would be generated. So, we purge the token
28590 from the stream. */
28591 cp_lexer_purge_token (parser->lexer);
28592 return false;
28594 else
28596 /* Consume the `template' keyword. */
28597 cp_lexer_consume_token (parser->lexer);
28598 return true;
28601 return false;
28604 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28605 set PARSER->SCOPE, and perform other related actions. */
28607 static void
28608 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28610 struct tree_check *check_value;
28612 /* Get the stored value. */
28613 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28614 /* Set the scope from the stored value. */
28615 parser->scope = saved_checks_value (check_value);
28616 parser->qualifying_scope = check_value->qualifying_scope;
28617 parser->object_scope = NULL_TREE;
28620 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28621 encounter the end of a block before what we were looking for. */
28623 static bool
28624 cp_parser_cache_group (cp_parser *parser,
28625 enum cpp_ttype end,
28626 unsigned depth)
28628 while (true)
28630 cp_token *token = cp_lexer_peek_token (parser->lexer);
28632 /* Abort a parenthesized expression if we encounter a semicolon. */
28633 if ((end == CPP_CLOSE_PAREN || depth == 0)
28634 && token->type == CPP_SEMICOLON)
28635 return true;
28636 /* If we've reached the end of the file, stop. */
28637 if (token->type == CPP_EOF
28638 || (end != CPP_PRAGMA_EOL
28639 && token->type == CPP_PRAGMA_EOL))
28640 return true;
28641 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28642 /* We've hit the end of an enclosing block, so there's been some
28643 kind of syntax error. */
28644 return true;
28646 /* Consume the token. */
28647 cp_lexer_consume_token (parser->lexer);
28648 /* See if it starts a new group. */
28649 if (token->type == CPP_OPEN_BRACE)
28651 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28652 /* In theory this should probably check end == '}', but
28653 cp_parser_save_member_function_body needs it to exit
28654 after either '}' or ')' when called with ')'. */
28655 if (depth == 0)
28656 return false;
28658 else if (token->type == CPP_OPEN_PAREN)
28660 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28661 if (depth == 0 && end == CPP_CLOSE_PAREN)
28662 return false;
28664 else if (token->type == CPP_PRAGMA)
28665 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28666 else if (token->type == end)
28667 return false;
28671 /* Like above, for caching a default argument or NSDMI. Both of these are
28672 terminated by a non-nested comma, but it can be unclear whether or not a
28673 comma is nested in a template argument list unless we do more parsing.
28674 In order to handle this ambiguity, when we encounter a ',' after a '<'
28675 we try to parse what follows as a parameter-declaration-list (in the
28676 case of a default argument) or a member-declarator (in the case of an
28677 NSDMI). If that succeeds, then we stop caching. */
28679 static tree
28680 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28682 unsigned depth = 0;
28683 int maybe_template_id = 0;
28684 cp_token *first_token;
28685 cp_token *token;
28686 tree default_argument;
28688 /* Add tokens until we have processed the entire default
28689 argument. We add the range [first_token, token). */
28690 first_token = cp_lexer_peek_token (parser->lexer);
28691 if (first_token->type == CPP_OPEN_BRACE)
28693 /* For list-initialization, this is straightforward. */
28694 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28695 token = cp_lexer_peek_token (parser->lexer);
28697 else while (true)
28699 bool done = false;
28701 /* Peek at the next token. */
28702 token = cp_lexer_peek_token (parser->lexer);
28703 /* What we do depends on what token we have. */
28704 switch (token->type)
28706 /* In valid code, a default argument must be
28707 immediately followed by a `,' `)', or `...'. */
28708 case CPP_COMMA:
28709 if (depth == 0 && maybe_template_id)
28711 /* If we've seen a '<', we might be in a
28712 template-argument-list. Until Core issue 325 is
28713 resolved, we don't know how this situation ought
28714 to be handled, so try to DTRT. We check whether
28715 what comes after the comma is a valid parameter
28716 declaration list. If it is, then the comma ends
28717 the default argument; otherwise the default
28718 argument continues. */
28719 bool error = false;
28720 cp_token *peek;
28722 /* Set ITALP so cp_parser_parameter_declaration_list
28723 doesn't decide to commit to this parse. */
28724 bool saved_italp = parser->in_template_argument_list_p;
28725 parser->in_template_argument_list_p = true;
28727 cp_parser_parse_tentatively (parser);
28729 if (nsdmi)
28731 /* Parse declarators until we reach a non-comma or
28732 somthing that cannot be an initializer.
28733 Just checking whether we're looking at a single
28734 declarator is insufficient. Consider:
28735 int var = tuple<T,U>::x;
28736 The template parameter 'U' looks exactly like a
28737 declarator. */
28740 int ctor_dtor_or_conv_p;
28741 cp_lexer_consume_token (parser->lexer);
28742 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28743 &ctor_dtor_or_conv_p,
28744 /*parenthesized_p=*/NULL,
28745 /*member_p=*/true,
28746 /*friend_p=*/false);
28747 peek = cp_lexer_peek_token (parser->lexer);
28748 if (cp_parser_error_occurred (parser))
28749 break;
28751 while (peek->type == CPP_COMMA);
28752 /* If we met an '=' or ';' then the original comma
28753 was the end of the NSDMI. Otherwise assume
28754 we're still in the NSDMI. */
28755 error = (peek->type != CPP_EQ
28756 && peek->type != CPP_SEMICOLON);
28758 else
28760 cp_lexer_consume_token (parser->lexer);
28761 begin_scope (sk_function_parms, NULL_TREE);
28762 cp_parser_parameter_declaration_list (parser, &error);
28763 pop_bindings_and_leave_scope ();
28765 if (!cp_parser_error_occurred (parser) && !error)
28766 done = true;
28767 cp_parser_abort_tentative_parse (parser);
28769 parser->in_template_argument_list_p = saved_italp;
28770 break;
28772 /* FALLTHRU */
28773 case CPP_CLOSE_PAREN:
28774 case CPP_ELLIPSIS:
28775 /* If we run into a non-nested `;', `}', or `]',
28776 then the code is invalid -- but the default
28777 argument is certainly over. */
28778 case CPP_SEMICOLON:
28779 case CPP_CLOSE_BRACE:
28780 case CPP_CLOSE_SQUARE:
28781 if (depth == 0
28782 /* Handle correctly int n = sizeof ... ( p ); */
28783 && token->type != CPP_ELLIPSIS)
28784 done = true;
28785 /* Update DEPTH, if necessary. */
28786 else if (token->type == CPP_CLOSE_PAREN
28787 || token->type == CPP_CLOSE_BRACE
28788 || token->type == CPP_CLOSE_SQUARE)
28789 --depth;
28790 break;
28792 case CPP_OPEN_PAREN:
28793 case CPP_OPEN_SQUARE:
28794 case CPP_OPEN_BRACE:
28795 ++depth;
28796 break;
28798 case CPP_LESS:
28799 if (depth == 0)
28800 /* This might be the comparison operator, or it might
28801 start a template argument list. */
28802 ++maybe_template_id;
28803 break;
28805 case CPP_RSHIFT:
28806 if (cxx_dialect == cxx98)
28807 break;
28808 /* Fall through for C++0x, which treats the `>>'
28809 operator like two `>' tokens in certain
28810 cases. */
28811 gcc_fallthrough ();
28813 case CPP_GREATER:
28814 if (depth == 0)
28816 /* This might be an operator, or it might close a
28817 template argument list. But if a previous '<'
28818 started a template argument list, this will have
28819 closed it, so we can't be in one anymore. */
28820 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28821 if (maybe_template_id < 0)
28822 maybe_template_id = 0;
28824 break;
28826 /* If we run out of tokens, issue an error message. */
28827 case CPP_EOF:
28828 case CPP_PRAGMA_EOL:
28829 error_at (token->location, "file ends in default argument");
28830 return error_mark_node;
28832 case CPP_NAME:
28833 case CPP_SCOPE:
28834 /* In these cases, we should look for template-ids.
28835 For example, if the default argument is
28836 `X<int, double>()', we need to do name lookup to
28837 figure out whether or not `X' is a template; if
28838 so, the `,' does not end the default argument.
28840 That is not yet done. */
28841 break;
28843 default:
28844 break;
28847 /* If we've reached the end, stop. */
28848 if (done)
28849 break;
28851 /* Add the token to the token block. */
28852 token = cp_lexer_consume_token (parser->lexer);
28855 /* Create a DEFAULT_ARG to represent the unparsed default
28856 argument. */
28857 default_argument = make_node (DEFAULT_ARG);
28858 DEFARG_TOKENS (default_argument)
28859 = cp_token_cache_new (first_token, token);
28860 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28862 return default_argument;
28865 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28867 location_t
28868 defarg_location (tree default_argument)
28870 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28871 location_t start = tokens->first->location;
28872 location_t end = tokens->last->location;
28873 return make_location (start, start, end);
28876 /* Begin parsing tentatively. We always save tokens while parsing
28877 tentatively so that if the tentative parsing fails we can restore the
28878 tokens. */
28880 static void
28881 cp_parser_parse_tentatively (cp_parser* parser)
28883 /* Enter a new parsing context. */
28884 parser->context = cp_parser_context_new (parser->context);
28885 /* Begin saving tokens. */
28886 cp_lexer_save_tokens (parser->lexer);
28887 /* In order to avoid repetitive access control error messages,
28888 access checks are queued up until we are no longer parsing
28889 tentatively. */
28890 push_deferring_access_checks (dk_deferred);
28893 /* Commit to the currently active tentative parse. */
28895 static void
28896 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28898 cp_parser_context *context;
28899 cp_lexer *lexer;
28901 /* Mark all of the levels as committed. */
28902 lexer = parser->lexer;
28903 for (context = parser->context; context->next; context = context->next)
28905 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28906 break;
28907 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28908 while (!cp_lexer_saving_tokens (lexer))
28909 lexer = lexer->next;
28910 cp_lexer_commit_tokens (lexer);
28914 /* Commit to the topmost currently active tentative parse.
28916 Note that this function shouldn't be called when there are
28917 irreversible side-effects while in a tentative state. For
28918 example, we shouldn't create a permanent entry in the symbol
28919 table, or issue an error message that might not apply if the
28920 tentative parse is aborted. */
28922 static void
28923 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28925 cp_parser_context *context = parser->context;
28926 cp_lexer *lexer = parser->lexer;
28928 if (context)
28930 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28931 return;
28932 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28934 while (!cp_lexer_saving_tokens (lexer))
28935 lexer = lexer->next;
28936 cp_lexer_commit_tokens (lexer);
28940 /* Abort the currently active tentative parse. All consumed tokens
28941 will be rolled back, and no diagnostics will be issued. */
28943 static void
28944 cp_parser_abort_tentative_parse (cp_parser* parser)
28946 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28947 || errorcount > 0);
28948 cp_parser_simulate_error (parser);
28949 /* Now, pretend that we want to see if the construct was
28950 successfully parsed. */
28951 cp_parser_parse_definitely (parser);
28954 /* Stop parsing tentatively. If a parse error has occurred, restore the
28955 token stream. Otherwise, commit to the tokens we have consumed.
28956 Returns true if no error occurred; false otherwise. */
28958 static bool
28959 cp_parser_parse_definitely (cp_parser* parser)
28961 bool error_occurred;
28962 cp_parser_context *context;
28964 /* Remember whether or not an error occurred, since we are about to
28965 destroy that information. */
28966 error_occurred = cp_parser_error_occurred (parser);
28967 /* Remove the topmost context from the stack. */
28968 context = parser->context;
28969 parser->context = context->next;
28970 /* If no parse errors occurred, commit to the tentative parse. */
28971 if (!error_occurred)
28973 /* Commit to the tokens read tentatively, unless that was
28974 already done. */
28975 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28976 cp_lexer_commit_tokens (parser->lexer);
28978 pop_to_parent_deferring_access_checks ();
28980 /* Otherwise, if errors occurred, roll back our state so that things
28981 are just as they were before we began the tentative parse. */
28982 else
28984 cp_lexer_rollback_tokens (parser->lexer);
28985 pop_deferring_access_checks ();
28987 /* Add the context to the front of the free list. */
28988 context->next = cp_parser_context_free_list;
28989 cp_parser_context_free_list = context;
28991 return !error_occurred;
28994 /* Returns true if we are parsing tentatively and are not committed to
28995 this tentative parse. */
28997 static bool
28998 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29000 return (cp_parser_parsing_tentatively (parser)
29001 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29004 /* Returns nonzero iff an error has occurred during the most recent
29005 tentative parse. */
29007 static bool
29008 cp_parser_error_occurred (cp_parser* parser)
29010 return (cp_parser_parsing_tentatively (parser)
29011 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29014 /* Returns nonzero if GNU extensions are allowed. */
29016 static bool
29017 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29019 return parser->allow_gnu_extensions_p;
29022 /* Objective-C++ Productions */
29025 /* Parse an Objective-C expression, which feeds into a primary-expression
29026 above.
29028 objc-expression:
29029 objc-message-expression
29030 objc-string-literal
29031 objc-encode-expression
29032 objc-protocol-expression
29033 objc-selector-expression
29035 Returns a tree representation of the expression. */
29037 static cp_expr
29038 cp_parser_objc_expression (cp_parser* parser)
29040 /* Try to figure out what kind of declaration is present. */
29041 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29043 switch (kwd->type)
29045 case CPP_OPEN_SQUARE:
29046 return cp_parser_objc_message_expression (parser);
29048 case CPP_OBJC_STRING:
29049 kwd = cp_lexer_consume_token (parser->lexer);
29050 return objc_build_string_object (kwd->u.value);
29052 case CPP_KEYWORD:
29053 switch (kwd->keyword)
29055 case RID_AT_ENCODE:
29056 return cp_parser_objc_encode_expression (parser);
29058 case RID_AT_PROTOCOL:
29059 return cp_parser_objc_protocol_expression (parser);
29061 case RID_AT_SELECTOR:
29062 return cp_parser_objc_selector_expression (parser);
29064 default:
29065 break;
29067 /* FALLTHRU */
29068 default:
29069 error_at (kwd->location,
29070 "misplaced %<@%D%> Objective-C++ construct",
29071 kwd->u.value);
29072 cp_parser_skip_to_end_of_block_or_statement (parser);
29075 return error_mark_node;
29078 /* Parse an Objective-C message expression.
29080 objc-message-expression:
29081 [ objc-message-receiver objc-message-args ]
29083 Returns a representation of an Objective-C message. */
29085 static tree
29086 cp_parser_objc_message_expression (cp_parser* parser)
29088 tree receiver, messageargs;
29090 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29091 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29092 receiver = cp_parser_objc_message_receiver (parser);
29093 messageargs = cp_parser_objc_message_args (parser);
29094 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29097 tree result = objc_build_message_expr (receiver, messageargs);
29099 /* Construct a location e.g.
29100 [self func1:5]
29101 ^~~~~~~~~~~~~~
29102 ranging from the '[' to the ']', with the caret at the start. */
29103 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29104 protected_set_expr_location (result, combined_loc);
29106 return result;
29109 /* Parse an objc-message-receiver.
29111 objc-message-receiver:
29112 expression
29113 simple-type-specifier
29115 Returns a representation of the type or expression. */
29117 static tree
29118 cp_parser_objc_message_receiver (cp_parser* parser)
29120 tree rcv;
29122 /* An Objective-C message receiver may be either (1) a type
29123 or (2) an expression. */
29124 cp_parser_parse_tentatively (parser);
29125 rcv = cp_parser_expression (parser);
29127 /* If that worked out, fine. */
29128 if (cp_parser_parse_definitely (parser))
29129 return rcv;
29131 cp_parser_parse_tentatively (parser);
29132 rcv = cp_parser_simple_type_specifier (parser,
29133 /*decl_specs=*/NULL,
29134 CP_PARSER_FLAGS_NONE);
29136 if (cp_parser_parse_definitely (parser))
29137 return objc_get_class_reference (rcv);
29139 cp_parser_error (parser, "objective-c++ message receiver expected");
29140 return error_mark_node;
29143 /* Parse the arguments and selectors comprising an Objective-C message.
29145 objc-message-args:
29146 objc-selector
29147 objc-selector-args
29148 objc-selector-args , objc-comma-args
29150 objc-selector-args:
29151 objc-selector [opt] : assignment-expression
29152 objc-selector-args objc-selector [opt] : assignment-expression
29154 objc-comma-args:
29155 assignment-expression
29156 objc-comma-args , assignment-expression
29158 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29159 selector arguments and TREE_VALUE containing a list of comma
29160 arguments. */
29162 static tree
29163 cp_parser_objc_message_args (cp_parser* parser)
29165 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29166 bool maybe_unary_selector_p = true;
29167 cp_token *token = cp_lexer_peek_token (parser->lexer);
29169 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29171 tree selector = NULL_TREE, arg;
29173 if (token->type != CPP_COLON)
29174 selector = cp_parser_objc_selector (parser);
29176 /* Detect if we have a unary selector. */
29177 if (maybe_unary_selector_p
29178 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29179 return build_tree_list (selector, NULL_TREE);
29181 maybe_unary_selector_p = false;
29182 cp_parser_require (parser, CPP_COLON, RT_COLON);
29183 arg = cp_parser_assignment_expression (parser);
29185 sel_args
29186 = chainon (sel_args,
29187 build_tree_list (selector, arg));
29189 token = cp_lexer_peek_token (parser->lexer);
29192 /* Handle non-selector arguments, if any. */
29193 while (token->type == CPP_COMMA)
29195 tree arg;
29197 cp_lexer_consume_token (parser->lexer);
29198 arg = cp_parser_assignment_expression (parser);
29200 addl_args
29201 = chainon (addl_args,
29202 build_tree_list (NULL_TREE, arg));
29204 token = cp_lexer_peek_token (parser->lexer);
29207 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29209 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29210 return build_tree_list (error_mark_node, error_mark_node);
29213 return build_tree_list (sel_args, addl_args);
29216 /* Parse an Objective-C encode expression.
29218 objc-encode-expression:
29219 @encode objc-typename
29221 Returns an encoded representation of the type argument. */
29223 static cp_expr
29224 cp_parser_objc_encode_expression (cp_parser* parser)
29226 tree type;
29227 cp_token *token;
29228 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29230 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29231 matching_parens parens;
29232 parens.require_open (parser);
29233 token = cp_lexer_peek_token (parser->lexer);
29234 type = complete_type (cp_parser_type_id (parser));
29235 parens.require_close (parser);
29237 if (!type)
29239 error_at (token->location,
29240 "%<@encode%> must specify a type as an argument");
29241 return error_mark_node;
29244 /* This happens if we find @encode(T) (where T is a template
29245 typename or something dependent on a template typename) when
29246 parsing a template. In that case, we can't compile it
29247 immediately, but we rather create an AT_ENCODE_EXPR which will
29248 need to be instantiated when the template is used.
29250 if (dependent_type_p (type))
29252 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29253 TREE_READONLY (value) = 1;
29254 return value;
29258 /* Build a location of the form:
29259 @encode(int)
29260 ^~~~~~~~~~~~
29261 with caret==start at the @ token, finishing at the close paren. */
29262 location_t combined_loc
29263 = make_location (start_loc, start_loc,
29264 cp_lexer_previous_token (parser->lexer)->location);
29266 return cp_expr (objc_build_encode_expr (type), combined_loc);
29269 /* Parse an Objective-C @defs expression. */
29271 static tree
29272 cp_parser_objc_defs_expression (cp_parser *parser)
29274 tree name;
29276 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29277 matching_parens parens;
29278 parens.require_open (parser);
29279 name = cp_parser_identifier (parser);
29280 parens.require_close (parser);
29282 return objc_get_class_ivars (name);
29285 /* Parse an Objective-C protocol expression.
29287 objc-protocol-expression:
29288 @protocol ( identifier )
29290 Returns a representation of the protocol expression. */
29292 static tree
29293 cp_parser_objc_protocol_expression (cp_parser* parser)
29295 tree proto;
29296 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29298 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29299 matching_parens parens;
29300 parens.require_open (parser);
29301 proto = cp_parser_identifier (parser);
29302 parens.require_close (parser);
29304 /* Build a location of the form:
29305 @protocol(prot)
29306 ^~~~~~~~~~~~~~~
29307 with caret==start at the @ token, finishing at the close paren. */
29308 location_t combined_loc
29309 = make_location (start_loc, start_loc,
29310 cp_lexer_previous_token (parser->lexer)->location);
29311 tree result = objc_build_protocol_expr (proto);
29312 protected_set_expr_location (result, combined_loc);
29313 return result;
29316 /* Parse an Objective-C selector expression.
29318 objc-selector-expression:
29319 @selector ( objc-method-signature )
29321 objc-method-signature:
29322 objc-selector
29323 objc-selector-seq
29325 objc-selector-seq:
29326 objc-selector :
29327 objc-selector-seq objc-selector :
29329 Returns a representation of the method selector. */
29331 static tree
29332 cp_parser_objc_selector_expression (cp_parser* parser)
29334 tree sel_seq = NULL_TREE;
29335 bool maybe_unary_selector_p = true;
29336 cp_token *token;
29337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29339 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29340 matching_parens parens;
29341 parens.require_open (parser);
29342 token = cp_lexer_peek_token (parser->lexer);
29344 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29345 || token->type == CPP_SCOPE)
29347 tree selector = NULL_TREE;
29349 if (token->type != CPP_COLON
29350 || token->type == CPP_SCOPE)
29351 selector = cp_parser_objc_selector (parser);
29353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29354 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29356 /* Detect if we have a unary selector. */
29357 if (maybe_unary_selector_p)
29359 sel_seq = selector;
29360 goto finish_selector;
29362 else
29364 cp_parser_error (parser, "expected %<:%>");
29367 maybe_unary_selector_p = false;
29368 token = cp_lexer_consume_token (parser->lexer);
29370 if (token->type == CPP_SCOPE)
29372 sel_seq
29373 = chainon (sel_seq,
29374 build_tree_list (selector, NULL_TREE));
29375 sel_seq
29376 = chainon (sel_seq,
29377 build_tree_list (NULL_TREE, NULL_TREE));
29379 else
29380 sel_seq
29381 = chainon (sel_seq,
29382 build_tree_list (selector, NULL_TREE));
29384 token = cp_lexer_peek_token (parser->lexer);
29387 finish_selector:
29388 parens.require_close (parser);
29391 /* Build a location of the form:
29392 @selector(func)
29393 ^~~~~~~~~~~~~~~
29394 with caret==start at the @ token, finishing at the close paren. */
29395 location_t combined_loc
29396 = make_location (loc, loc,
29397 cp_lexer_previous_token (parser->lexer)->location);
29398 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29399 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29400 protected_set_expr_location (result, combined_loc);
29401 return result;
29404 /* Parse a list of identifiers.
29406 objc-identifier-list:
29407 identifier
29408 objc-identifier-list , identifier
29410 Returns a TREE_LIST of identifier nodes. */
29412 static tree
29413 cp_parser_objc_identifier_list (cp_parser* parser)
29415 tree identifier;
29416 tree list;
29417 cp_token *sep;
29419 identifier = cp_parser_identifier (parser);
29420 if (identifier == error_mark_node)
29421 return error_mark_node;
29423 list = build_tree_list (NULL_TREE, identifier);
29424 sep = cp_lexer_peek_token (parser->lexer);
29426 while (sep->type == CPP_COMMA)
29428 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29429 identifier = cp_parser_identifier (parser);
29430 if (identifier == error_mark_node)
29431 return list;
29433 list = chainon (list, build_tree_list (NULL_TREE,
29434 identifier));
29435 sep = cp_lexer_peek_token (parser->lexer);
29438 return list;
29441 /* Parse an Objective-C alias declaration.
29443 objc-alias-declaration:
29444 @compatibility_alias identifier identifier ;
29446 This function registers the alias mapping with the Objective-C front end.
29447 It returns nothing. */
29449 static void
29450 cp_parser_objc_alias_declaration (cp_parser* parser)
29452 tree alias, orig;
29454 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29455 alias = cp_parser_identifier (parser);
29456 orig = cp_parser_identifier (parser);
29457 objc_declare_alias (alias, orig);
29458 cp_parser_consume_semicolon_at_end_of_statement (parser);
29461 /* Parse an Objective-C class forward-declaration.
29463 objc-class-declaration:
29464 @class objc-identifier-list ;
29466 The function registers the forward declarations with the Objective-C
29467 front end. It returns nothing. */
29469 static void
29470 cp_parser_objc_class_declaration (cp_parser* parser)
29472 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29473 while (true)
29475 tree id;
29477 id = cp_parser_identifier (parser);
29478 if (id == error_mark_node)
29479 break;
29481 objc_declare_class (id);
29483 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29484 cp_lexer_consume_token (parser->lexer);
29485 else
29486 break;
29488 cp_parser_consume_semicolon_at_end_of_statement (parser);
29491 /* Parse a list of Objective-C protocol references.
29493 objc-protocol-refs-opt:
29494 objc-protocol-refs [opt]
29496 objc-protocol-refs:
29497 < objc-identifier-list >
29499 Returns a TREE_LIST of identifiers, if any. */
29501 static tree
29502 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29504 tree protorefs = NULL_TREE;
29506 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29508 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29509 protorefs = cp_parser_objc_identifier_list (parser);
29510 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29513 return protorefs;
29516 /* Parse a Objective-C visibility specification. */
29518 static void
29519 cp_parser_objc_visibility_spec (cp_parser* parser)
29521 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29523 switch (vis->keyword)
29525 case RID_AT_PRIVATE:
29526 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29527 break;
29528 case RID_AT_PROTECTED:
29529 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29530 break;
29531 case RID_AT_PUBLIC:
29532 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29533 break;
29534 case RID_AT_PACKAGE:
29535 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29536 break;
29537 default:
29538 return;
29541 /* Eat '@private'/'@protected'/'@public'. */
29542 cp_lexer_consume_token (parser->lexer);
29545 /* Parse an Objective-C method type. Return 'true' if it is a class
29546 (+) method, and 'false' if it is an instance (-) method. */
29548 static inline bool
29549 cp_parser_objc_method_type (cp_parser* parser)
29551 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29552 return true;
29553 else
29554 return false;
29557 /* Parse an Objective-C protocol qualifier. */
29559 static tree
29560 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29562 tree quals = NULL_TREE, node;
29563 cp_token *token = cp_lexer_peek_token (parser->lexer);
29565 node = token->u.value;
29567 while (node && identifier_p (node)
29568 && (node == ridpointers [(int) RID_IN]
29569 || node == ridpointers [(int) RID_OUT]
29570 || node == ridpointers [(int) RID_INOUT]
29571 || node == ridpointers [(int) RID_BYCOPY]
29572 || node == ridpointers [(int) RID_BYREF]
29573 || node == ridpointers [(int) RID_ONEWAY]))
29575 quals = tree_cons (NULL_TREE, node, quals);
29576 cp_lexer_consume_token (parser->lexer);
29577 token = cp_lexer_peek_token (parser->lexer);
29578 node = token->u.value;
29581 return quals;
29584 /* Parse an Objective-C typename. */
29586 static tree
29587 cp_parser_objc_typename (cp_parser* parser)
29589 tree type_name = NULL_TREE;
29591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29593 tree proto_quals, cp_type = NULL_TREE;
29595 matching_parens parens;
29596 parens.consume_open (parser); /* Eat '('. */
29597 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29599 /* An ObjC type name may consist of just protocol qualifiers, in which
29600 case the type shall default to 'id'. */
29601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29603 cp_type = cp_parser_type_id (parser);
29605 /* If the type could not be parsed, an error has already
29606 been produced. For error recovery, behave as if it had
29607 not been specified, which will use the default type
29608 'id'. */
29609 if (cp_type == error_mark_node)
29611 cp_type = NULL_TREE;
29612 /* We need to skip to the closing parenthesis as
29613 cp_parser_type_id() does not seem to do it for
29614 us. */
29615 cp_parser_skip_to_closing_parenthesis (parser,
29616 /*recovering=*/true,
29617 /*or_comma=*/false,
29618 /*consume_paren=*/false);
29622 parens.require_close (parser);
29623 type_name = build_tree_list (proto_quals, cp_type);
29626 return type_name;
29629 /* Check to see if TYPE refers to an Objective-C selector name. */
29631 static bool
29632 cp_parser_objc_selector_p (enum cpp_ttype type)
29634 return (type == CPP_NAME || type == CPP_KEYWORD
29635 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29636 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29637 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29638 || type == CPP_XOR || type == CPP_XOR_EQ);
29641 /* Parse an Objective-C selector. */
29643 static tree
29644 cp_parser_objc_selector (cp_parser* parser)
29646 cp_token *token = cp_lexer_consume_token (parser->lexer);
29648 if (!cp_parser_objc_selector_p (token->type))
29650 error_at (token->location, "invalid Objective-C++ selector name");
29651 return error_mark_node;
29654 /* C++ operator names are allowed to appear in ObjC selectors. */
29655 switch (token->type)
29657 case CPP_AND_AND: return get_identifier ("and");
29658 case CPP_AND_EQ: return get_identifier ("and_eq");
29659 case CPP_AND: return get_identifier ("bitand");
29660 case CPP_OR: return get_identifier ("bitor");
29661 case CPP_COMPL: return get_identifier ("compl");
29662 case CPP_NOT: return get_identifier ("not");
29663 case CPP_NOT_EQ: return get_identifier ("not_eq");
29664 case CPP_OR_OR: return get_identifier ("or");
29665 case CPP_OR_EQ: return get_identifier ("or_eq");
29666 case CPP_XOR: return get_identifier ("xor");
29667 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29668 default: return token->u.value;
29672 /* Parse an Objective-C params list. */
29674 static tree
29675 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29677 tree params = NULL_TREE;
29678 bool maybe_unary_selector_p = true;
29679 cp_token *token = cp_lexer_peek_token (parser->lexer);
29681 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29683 tree selector = NULL_TREE, type_name, identifier;
29684 tree parm_attr = NULL_TREE;
29686 if (token->keyword == RID_ATTRIBUTE)
29687 break;
29689 if (token->type != CPP_COLON)
29690 selector = cp_parser_objc_selector (parser);
29692 /* Detect if we have a unary selector. */
29693 if (maybe_unary_selector_p
29694 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29696 params = selector; /* Might be followed by attributes. */
29697 break;
29700 maybe_unary_selector_p = false;
29701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29703 /* Something went quite wrong. There should be a colon
29704 here, but there is not. Stop parsing parameters. */
29705 break;
29707 type_name = cp_parser_objc_typename (parser);
29708 /* New ObjC allows attributes on parameters too. */
29709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29710 parm_attr = cp_parser_attributes_opt (parser);
29711 identifier = cp_parser_identifier (parser);
29713 params
29714 = chainon (params,
29715 objc_build_keyword_decl (selector,
29716 type_name,
29717 identifier,
29718 parm_attr));
29720 token = cp_lexer_peek_token (parser->lexer);
29723 if (params == NULL_TREE)
29725 cp_parser_error (parser, "objective-c++ method declaration is expected");
29726 return error_mark_node;
29729 /* We allow tail attributes for the method. */
29730 if (token->keyword == RID_ATTRIBUTE)
29732 *attributes = cp_parser_attributes_opt (parser);
29733 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29734 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29735 return params;
29736 cp_parser_error (parser,
29737 "method attributes must be specified at the end");
29738 return error_mark_node;
29741 if (params == NULL_TREE)
29743 cp_parser_error (parser, "objective-c++ method declaration is expected");
29744 return error_mark_node;
29746 return params;
29749 /* Parse the non-keyword Objective-C params. */
29751 static tree
29752 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29753 tree* attributes)
29755 tree params = make_node (TREE_LIST);
29756 cp_token *token = cp_lexer_peek_token (parser->lexer);
29757 *ellipsisp = false; /* Initially, assume no ellipsis. */
29759 while (token->type == CPP_COMMA)
29761 cp_parameter_declarator *parmdecl;
29762 tree parm;
29764 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29765 token = cp_lexer_peek_token (parser->lexer);
29767 if (token->type == CPP_ELLIPSIS)
29769 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29770 *ellipsisp = true;
29771 token = cp_lexer_peek_token (parser->lexer);
29772 break;
29775 /* TODO: parse attributes for tail parameters. */
29776 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29777 parm = grokdeclarator (parmdecl->declarator,
29778 &parmdecl->decl_specifiers,
29779 PARM, /*initialized=*/0,
29780 /*attrlist=*/NULL);
29782 chainon (params, build_tree_list (NULL_TREE, parm));
29783 token = cp_lexer_peek_token (parser->lexer);
29786 /* We allow tail attributes for the method. */
29787 if (token->keyword == RID_ATTRIBUTE)
29789 if (*attributes == NULL_TREE)
29791 *attributes = cp_parser_attributes_opt (parser);
29792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29793 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29794 return params;
29796 else
29797 /* We have an error, but parse the attributes, so that we can
29798 carry on. */
29799 *attributes = cp_parser_attributes_opt (parser);
29801 cp_parser_error (parser,
29802 "method attributes must be specified at the end");
29803 return error_mark_node;
29806 return params;
29809 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29811 static void
29812 cp_parser_objc_interstitial_code (cp_parser* parser)
29814 cp_token *token = cp_lexer_peek_token (parser->lexer);
29816 /* If the next token is `extern' and the following token is a string
29817 literal, then we have a linkage specification. */
29818 if (token->keyword == RID_EXTERN
29819 && cp_parser_is_pure_string_literal
29820 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29821 cp_parser_linkage_specification (parser);
29822 /* Handle #pragma, if any. */
29823 else if (token->type == CPP_PRAGMA)
29824 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29825 /* Allow stray semicolons. */
29826 else if (token->type == CPP_SEMICOLON)
29827 cp_lexer_consume_token (parser->lexer);
29828 /* Mark methods as optional or required, when building protocols. */
29829 else if (token->keyword == RID_AT_OPTIONAL)
29831 cp_lexer_consume_token (parser->lexer);
29832 objc_set_method_opt (true);
29834 else if (token->keyword == RID_AT_REQUIRED)
29836 cp_lexer_consume_token (parser->lexer);
29837 objc_set_method_opt (false);
29839 else if (token->keyword == RID_NAMESPACE)
29840 cp_parser_namespace_definition (parser);
29841 /* Other stray characters must generate errors. */
29842 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29844 cp_lexer_consume_token (parser->lexer);
29845 error ("stray %qs between Objective-C++ methods",
29846 token->type == CPP_OPEN_BRACE ? "{" : "}");
29848 /* Finally, try to parse a block-declaration, or a function-definition. */
29849 else
29850 cp_parser_block_declaration (parser, /*statement_p=*/false);
29853 /* Parse a method signature. */
29855 static tree
29856 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29858 tree rettype, kwdparms, optparms;
29859 bool ellipsis = false;
29860 bool is_class_method;
29862 is_class_method = cp_parser_objc_method_type (parser);
29863 rettype = cp_parser_objc_typename (parser);
29864 *attributes = NULL_TREE;
29865 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29866 if (kwdparms == error_mark_node)
29867 return error_mark_node;
29868 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29869 if (optparms == error_mark_node)
29870 return error_mark_node;
29872 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29875 static bool
29876 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29878 tree tattr;
29879 cp_lexer_save_tokens (parser->lexer);
29880 tattr = cp_parser_attributes_opt (parser);
29881 gcc_assert (tattr) ;
29883 /* If the attributes are followed by a method introducer, this is not allowed.
29884 Dump the attributes and flag the situation. */
29885 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29886 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29887 return true;
29889 /* Otherwise, the attributes introduce some interstitial code, possibly so
29890 rewind to allow that check. */
29891 cp_lexer_rollback_tokens (parser->lexer);
29892 return false;
29895 /* Parse an Objective-C method prototype list. */
29897 static void
29898 cp_parser_objc_method_prototype_list (cp_parser* parser)
29900 cp_token *token = cp_lexer_peek_token (parser->lexer);
29902 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29904 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29906 tree attributes, sig;
29907 bool is_class_method;
29908 if (token->type == CPP_PLUS)
29909 is_class_method = true;
29910 else
29911 is_class_method = false;
29912 sig = cp_parser_objc_method_signature (parser, &attributes);
29913 if (sig == error_mark_node)
29915 cp_parser_skip_to_end_of_block_or_statement (parser);
29916 token = cp_lexer_peek_token (parser->lexer);
29917 continue;
29919 objc_add_method_declaration (is_class_method, sig, attributes);
29920 cp_parser_consume_semicolon_at_end_of_statement (parser);
29922 else if (token->keyword == RID_AT_PROPERTY)
29923 cp_parser_objc_at_property_declaration (parser);
29924 else if (token->keyword == RID_ATTRIBUTE
29925 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29926 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29927 OPT_Wattributes,
29928 "prefix attributes are ignored for methods");
29929 else
29930 /* Allow for interspersed non-ObjC++ code. */
29931 cp_parser_objc_interstitial_code (parser);
29933 token = cp_lexer_peek_token (parser->lexer);
29936 if (token->type != CPP_EOF)
29937 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29938 else
29939 cp_parser_error (parser, "expected %<@end%>");
29941 objc_finish_interface ();
29944 /* Parse an Objective-C method definition list. */
29946 static void
29947 cp_parser_objc_method_definition_list (cp_parser* parser)
29949 cp_token *token = cp_lexer_peek_token (parser->lexer);
29951 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29953 tree meth;
29955 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29957 cp_token *ptk;
29958 tree sig, attribute;
29959 bool is_class_method;
29960 if (token->type == CPP_PLUS)
29961 is_class_method = true;
29962 else
29963 is_class_method = false;
29964 push_deferring_access_checks (dk_deferred);
29965 sig = cp_parser_objc_method_signature (parser, &attribute);
29966 if (sig == error_mark_node)
29968 cp_parser_skip_to_end_of_block_or_statement (parser);
29969 token = cp_lexer_peek_token (parser->lexer);
29970 continue;
29972 objc_start_method_definition (is_class_method, sig, attribute,
29973 NULL_TREE);
29975 /* For historical reasons, we accept an optional semicolon. */
29976 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29977 cp_lexer_consume_token (parser->lexer);
29979 ptk = cp_lexer_peek_token (parser->lexer);
29980 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29981 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29983 perform_deferred_access_checks (tf_warning_or_error);
29984 stop_deferring_access_checks ();
29985 meth = cp_parser_function_definition_after_declarator (parser,
29986 false);
29987 pop_deferring_access_checks ();
29988 objc_finish_method_definition (meth);
29991 /* The following case will be removed once @synthesize is
29992 completely implemented. */
29993 else if (token->keyword == RID_AT_PROPERTY)
29994 cp_parser_objc_at_property_declaration (parser);
29995 else if (token->keyword == RID_AT_SYNTHESIZE)
29996 cp_parser_objc_at_synthesize_declaration (parser);
29997 else if (token->keyword == RID_AT_DYNAMIC)
29998 cp_parser_objc_at_dynamic_declaration (parser);
29999 else if (token->keyword == RID_ATTRIBUTE
30000 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30001 warning_at (token->location, OPT_Wattributes,
30002 "prefix attributes are ignored for methods");
30003 else
30004 /* Allow for interspersed non-ObjC++ code. */
30005 cp_parser_objc_interstitial_code (parser);
30007 token = cp_lexer_peek_token (parser->lexer);
30010 if (token->type != CPP_EOF)
30011 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30012 else
30013 cp_parser_error (parser, "expected %<@end%>");
30015 objc_finish_implementation ();
30018 /* Parse Objective-C ivars. */
30020 static void
30021 cp_parser_objc_class_ivars (cp_parser* parser)
30023 cp_token *token = cp_lexer_peek_token (parser->lexer);
30025 if (token->type != CPP_OPEN_BRACE)
30026 return; /* No ivars specified. */
30028 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30029 token = cp_lexer_peek_token (parser->lexer);
30031 while (token->type != CPP_CLOSE_BRACE
30032 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30034 cp_decl_specifier_seq declspecs;
30035 int decl_class_or_enum_p;
30036 tree prefix_attributes;
30038 cp_parser_objc_visibility_spec (parser);
30040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30041 break;
30043 cp_parser_decl_specifier_seq (parser,
30044 CP_PARSER_FLAGS_OPTIONAL,
30045 &declspecs,
30046 &decl_class_or_enum_p);
30048 /* auto, register, static, extern, mutable. */
30049 if (declspecs.storage_class != sc_none)
30051 cp_parser_error (parser, "invalid type for instance variable");
30052 declspecs.storage_class = sc_none;
30055 /* thread_local. */
30056 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30058 cp_parser_error (parser, "invalid type for instance variable");
30059 declspecs.locations[ds_thread] = 0;
30062 /* typedef. */
30063 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30065 cp_parser_error (parser, "invalid type for instance variable");
30066 declspecs.locations[ds_typedef] = 0;
30069 prefix_attributes = declspecs.attributes;
30070 declspecs.attributes = NULL_TREE;
30072 /* Keep going until we hit the `;' at the end of the
30073 declaration. */
30074 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30076 tree width = NULL_TREE, attributes, first_attribute, decl;
30077 cp_declarator *declarator = NULL;
30078 int ctor_dtor_or_conv_p;
30080 /* Check for a (possibly unnamed) bitfield declaration. */
30081 token = cp_lexer_peek_token (parser->lexer);
30082 if (token->type == CPP_COLON)
30083 goto eat_colon;
30085 if (token->type == CPP_NAME
30086 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30087 == CPP_COLON))
30089 /* Get the name of the bitfield. */
30090 declarator = make_id_declarator (NULL_TREE,
30091 cp_parser_identifier (parser),
30092 sfk_none);
30094 eat_colon:
30095 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30096 /* Get the width of the bitfield. */
30097 width
30098 = cp_parser_constant_expression (parser);
30100 else
30102 /* Parse the declarator. */
30103 declarator
30104 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30105 &ctor_dtor_or_conv_p,
30106 /*parenthesized_p=*/NULL,
30107 /*member_p=*/false,
30108 /*friend_p=*/false);
30111 /* Look for attributes that apply to the ivar. */
30112 attributes = cp_parser_attributes_opt (parser);
30113 /* Remember which attributes are prefix attributes and
30114 which are not. */
30115 first_attribute = attributes;
30116 /* Combine the attributes. */
30117 attributes = chainon (prefix_attributes, attributes);
30119 if (width)
30120 /* Create the bitfield declaration. */
30121 decl = grokbitfield (declarator, &declspecs,
30122 width, NULL_TREE, attributes);
30123 else
30124 decl = grokfield (declarator, &declspecs,
30125 NULL_TREE, /*init_const_expr_p=*/false,
30126 NULL_TREE, attributes);
30128 /* Add the instance variable. */
30129 if (decl != error_mark_node && decl != NULL_TREE)
30130 objc_add_instance_variable (decl);
30132 /* Reset PREFIX_ATTRIBUTES. */
30133 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30134 attributes = TREE_CHAIN (attributes);
30135 if (attributes)
30136 TREE_CHAIN (attributes) = NULL_TREE;
30138 token = cp_lexer_peek_token (parser->lexer);
30140 if (token->type == CPP_COMMA)
30142 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30143 continue;
30145 break;
30148 cp_parser_consume_semicolon_at_end_of_statement (parser);
30149 token = cp_lexer_peek_token (parser->lexer);
30152 if (token->keyword == RID_AT_END)
30153 cp_parser_error (parser, "expected %<}%>");
30155 /* Do not consume the RID_AT_END, so it will be read again as terminating
30156 the @interface of @implementation. */
30157 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30158 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30160 /* For historical reasons, we accept an optional semicolon. */
30161 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30162 cp_lexer_consume_token (parser->lexer);
30165 /* Parse an Objective-C protocol declaration. */
30167 static void
30168 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30170 tree proto, protorefs;
30171 cp_token *tok;
30173 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30176 tok = cp_lexer_peek_token (parser->lexer);
30177 error_at (tok->location, "identifier expected after %<@protocol%>");
30178 cp_parser_consume_semicolon_at_end_of_statement (parser);
30179 return;
30182 /* See if we have a forward declaration or a definition. */
30183 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30185 /* Try a forward declaration first. */
30186 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30188 while (true)
30190 tree id;
30192 id = cp_parser_identifier (parser);
30193 if (id == error_mark_node)
30194 break;
30196 objc_declare_protocol (id, attributes);
30198 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30199 cp_lexer_consume_token (parser->lexer);
30200 else
30201 break;
30203 cp_parser_consume_semicolon_at_end_of_statement (parser);
30206 /* Ok, we got a full-fledged definition (or at least should). */
30207 else
30209 proto = cp_parser_identifier (parser);
30210 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30211 objc_start_protocol (proto, protorefs, attributes);
30212 cp_parser_objc_method_prototype_list (parser);
30216 /* Parse an Objective-C superclass or category. */
30218 static void
30219 cp_parser_objc_superclass_or_category (cp_parser *parser,
30220 bool iface_p,
30221 tree *super,
30222 tree *categ, bool *is_class_extension)
30224 cp_token *next = cp_lexer_peek_token (parser->lexer);
30226 *super = *categ = NULL_TREE;
30227 *is_class_extension = false;
30228 if (next->type == CPP_COLON)
30230 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30231 *super = cp_parser_identifier (parser);
30233 else if (next->type == CPP_OPEN_PAREN)
30235 matching_parens parens;
30236 parens.consume_open (parser); /* Eat '('. */
30238 /* If there is no category name, and this is an @interface, we
30239 have a class extension. */
30240 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30242 *categ = NULL_TREE;
30243 *is_class_extension = true;
30245 else
30246 *categ = cp_parser_identifier (parser);
30248 parens.require_close (parser);
30252 /* Parse an Objective-C class interface. */
30254 static void
30255 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30257 tree name, super, categ, protos;
30258 bool is_class_extension;
30260 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30261 name = cp_parser_identifier (parser);
30262 if (name == error_mark_node)
30264 /* It's hard to recover because even if valid @interface stuff
30265 is to follow, we can't compile it (or validate it) if we
30266 don't even know which class it refers to. Let's assume this
30267 was a stray '@interface' token in the stream and skip it.
30269 return;
30271 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30272 &is_class_extension);
30273 protos = cp_parser_objc_protocol_refs_opt (parser);
30275 /* We have either a class or a category on our hands. */
30276 if (categ || is_class_extension)
30277 objc_start_category_interface (name, categ, protos, attributes);
30278 else
30280 objc_start_class_interface (name, super, protos, attributes);
30281 /* Handle instance variable declarations, if any. */
30282 cp_parser_objc_class_ivars (parser);
30283 objc_continue_interface ();
30286 cp_parser_objc_method_prototype_list (parser);
30289 /* Parse an Objective-C class implementation. */
30291 static void
30292 cp_parser_objc_class_implementation (cp_parser* parser)
30294 tree name, super, categ;
30295 bool is_class_extension;
30297 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30298 name = cp_parser_identifier (parser);
30299 if (name == error_mark_node)
30301 /* It's hard to recover because even if valid @implementation
30302 stuff is to follow, we can't compile it (or validate it) if
30303 we don't even know which class it refers to. Let's assume
30304 this was a stray '@implementation' token in the stream and
30305 skip it.
30307 return;
30309 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30310 &is_class_extension);
30312 /* We have either a class or a category on our hands. */
30313 if (categ)
30314 objc_start_category_implementation (name, categ);
30315 else
30317 objc_start_class_implementation (name, super);
30318 /* Handle instance variable declarations, if any. */
30319 cp_parser_objc_class_ivars (parser);
30320 objc_continue_implementation ();
30323 cp_parser_objc_method_definition_list (parser);
30326 /* Consume the @end token and finish off the implementation. */
30328 static void
30329 cp_parser_objc_end_implementation (cp_parser* parser)
30331 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30332 objc_finish_implementation ();
30335 /* Parse an Objective-C declaration. */
30337 static void
30338 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30340 /* Try to figure out what kind of declaration is present. */
30341 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30343 if (attributes)
30344 switch (kwd->keyword)
30346 case RID_AT_ALIAS:
30347 case RID_AT_CLASS:
30348 case RID_AT_END:
30349 error_at (kwd->location, "attributes may not be specified before"
30350 " the %<@%D%> Objective-C++ keyword",
30351 kwd->u.value);
30352 attributes = NULL;
30353 break;
30354 case RID_AT_IMPLEMENTATION:
30355 warning_at (kwd->location, OPT_Wattributes,
30356 "prefix attributes are ignored before %<@%D%>",
30357 kwd->u.value);
30358 attributes = NULL;
30359 default:
30360 break;
30363 switch (kwd->keyword)
30365 case RID_AT_ALIAS:
30366 cp_parser_objc_alias_declaration (parser);
30367 break;
30368 case RID_AT_CLASS:
30369 cp_parser_objc_class_declaration (parser);
30370 break;
30371 case RID_AT_PROTOCOL:
30372 cp_parser_objc_protocol_declaration (parser, attributes);
30373 break;
30374 case RID_AT_INTERFACE:
30375 cp_parser_objc_class_interface (parser, attributes);
30376 break;
30377 case RID_AT_IMPLEMENTATION:
30378 cp_parser_objc_class_implementation (parser);
30379 break;
30380 case RID_AT_END:
30381 cp_parser_objc_end_implementation (parser);
30382 break;
30383 default:
30384 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30385 kwd->u.value);
30386 cp_parser_skip_to_end_of_block_or_statement (parser);
30390 /* Parse an Objective-C try-catch-finally statement.
30392 objc-try-catch-finally-stmt:
30393 @try compound-statement objc-catch-clause-seq [opt]
30394 objc-finally-clause [opt]
30396 objc-catch-clause-seq:
30397 objc-catch-clause objc-catch-clause-seq [opt]
30399 objc-catch-clause:
30400 @catch ( objc-exception-declaration ) compound-statement
30402 objc-finally-clause:
30403 @finally compound-statement
30405 objc-exception-declaration:
30406 parameter-declaration
30407 '...'
30409 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30411 Returns NULL_TREE.
30413 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30414 for C. Keep them in sync. */
30416 static tree
30417 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30419 location_t location;
30420 tree stmt;
30422 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30423 location = cp_lexer_peek_token (parser->lexer)->location;
30424 objc_maybe_warn_exceptions (location);
30425 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30426 node, lest it get absorbed into the surrounding block. */
30427 stmt = push_stmt_list ();
30428 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30429 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30431 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30433 cp_parameter_declarator *parm;
30434 tree parameter_declaration = error_mark_node;
30435 bool seen_open_paren = false;
30436 matching_parens parens;
30438 cp_lexer_consume_token (parser->lexer);
30439 if (parens.require_open (parser))
30440 seen_open_paren = true;
30441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30443 /* We have "@catch (...)" (where the '...' are literally
30444 what is in the code). Skip the '...'.
30445 parameter_declaration is set to NULL_TREE, and
30446 objc_being_catch_clauses() knows that that means
30447 '...'. */
30448 cp_lexer_consume_token (parser->lexer);
30449 parameter_declaration = NULL_TREE;
30451 else
30453 /* We have "@catch (NSException *exception)" or something
30454 like that. Parse the parameter declaration. */
30455 parm = cp_parser_parameter_declaration (parser, false, NULL);
30456 if (parm == NULL)
30457 parameter_declaration = error_mark_node;
30458 else
30459 parameter_declaration = grokdeclarator (parm->declarator,
30460 &parm->decl_specifiers,
30461 PARM, /*initialized=*/0,
30462 /*attrlist=*/NULL);
30464 if (seen_open_paren)
30465 parens.require_close (parser);
30466 else
30468 /* If there was no open parenthesis, we are recovering from
30469 an error, and we are trying to figure out what mistake
30470 the user has made. */
30472 /* If there is an immediate closing parenthesis, the user
30473 probably forgot the opening one (ie, they typed "@catch
30474 NSException *e)". Parse the closing parenthesis and keep
30475 going. */
30476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30477 cp_lexer_consume_token (parser->lexer);
30479 /* If these is no immediate closing parenthesis, the user
30480 probably doesn't know that parenthesis are required at
30481 all (ie, they typed "@catch NSException *e"). So, just
30482 forget about the closing parenthesis and keep going. */
30484 objc_begin_catch_clause (parameter_declaration);
30485 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30486 objc_finish_catch_clause ();
30488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30490 cp_lexer_consume_token (parser->lexer);
30491 location = cp_lexer_peek_token (parser->lexer)->location;
30492 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30493 node, lest it get absorbed into the surrounding block. */
30494 stmt = push_stmt_list ();
30495 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30496 objc_build_finally_clause (location, pop_stmt_list (stmt));
30499 return objc_finish_try_stmt ();
30502 /* Parse an Objective-C synchronized statement.
30504 objc-synchronized-stmt:
30505 @synchronized ( expression ) compound-statement
30507 Returns NULL_TREE. */
30509 static tree
30510 cp_parser_objc_synchronized_statement (cp_parser *parser)
30512 location_t location;
30513 tree lock, stmt;
30515 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30517 location = cp_lexer_peek_token (parser->lexer)->location;
30518 objc_maybe_warn_exceptions (location);
30519 matching_parens parens;
30520 parens.require_open (parser);
30521 lock = cp_parser_expression (parser);
30522 parens.require_close (parser);
30524 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30525 node, lest it get absorbed into the surrounding block. */
30526 stmt = push_stmt_list ();
30527 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30529 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30532 /* Parse an Objective-C throw statement.
30534 objc-throw-stmt:
30535 @throw assignment-expression [opt] ;
30537 Returns a constructed '@throw' statement. */
30539 static tree
30540 cp_parser_objc_throw_statement (cp_parser *parser)
30542 tree expr = NULL_TREE;
30543 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30545 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30548 expr = cp_parser_expression (parser);
30550 cp_parser_consume_semicolon_at_end_of_statement (parser);
30552 return objc_build_throw_stmt (loc, expr);
30555 /* Parse an Objective-C statement. */
30557 static tree
30558 cp_parser_objc_statement (cp_parser * parser)
30560 /* Try to figure out what kind of declaration is present. */
30561 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30563 switch (kwd->keyword)
30565 case RID_AT_TRY:
30566 return cp_parser_objc_try_catch_finally_statement (parser);
30567 case RID_AT_SYNCHRONIZED:
30568 return cp_parser_objc_synchronized_statement (parser);
30569 case RID_AT_THROW:
30570 return cp_parser_objc_throw_statement (parser);
30571 default:
30572 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30573 kwd->u.value);
30574 cp_parser_skip_to_end_of_block_or_statement (parser);
30577 return error_mark_node;
30580 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30581 look ahead to see if an objc keyword follows the attributes. This
30582 is to detect the use of prefix attributes on ObjC @interface and
30583 @protocol. */
30585 static bool
30586 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30588 cp_lexer_save_tokens (parser->lexer);
30589 *attrib = cp_parser_attributes_opt (parser);
30590 gcc_assert (*attrib);
30591 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30593 cp_lexer_commit_tokens (parser->lexer);
30594 return true;
30596 cp_lexer_rollback_tokens (parser->lexer);
30597 return false;
30600 /* This routine is a minimal replacement for
30601 c_parser_struct_declaration () used when parsing the list of
30602 types/names or ObjC++ properties. For example, when parsing the
30603 code
30605 @property (readonly) int a, b, c;
30607 this function is responsible for parsing "int a, int b, int c" and
30608 returning the declarations as CHAIN of DECLs.
30610 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30611 similar parsing. */
30612 static tree
30613 cp_parser_objc_struct_declaration (cp_parser *parser)
30615 tree decls = NULL_TREE;
30616 cp_decl_specifier_seq declspecs;
30617 int decl_class_or_enum_p;
30618 tree prefix_attributes;
30620 cp_parser_decl_specifier_seq (parser,
30621 CP_PARSER_FLAGS_NONE,
30622 &declspecs,
30623 &decl_class_or_enum_p);
30625 if (declspecs.type == error_mark_node)
30626 return error_mark_node;
30628 /* auto, register, static, extern, mutable. */
30629 if (declspecs.storage_class != sc_none)
30631 cp_parser_error (parser, "invalid type for property");
30632 declspecs.storage_class = sc_none;
30635 /* thread_local. */
30636 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30638 cp_parser_error (parser, "invalid type for property");
30639 declspecs.locations[ds_thread] = 0;
30642 /* typedef. */
30643 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30645 cp_parser_error (parser, "invalid type for property");
30646 declspecs.locations[ds_typedef] = 0;
30649 prefix_attributes = declspecs.attributes;
30650 declspecs.attributes = NULL_TREE;
30652 /* Keep going until we hit the `;' at the end of the declaration. */
30653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30655 tree attributes, first_attribute, decl;
30656 cp_declarator *declarator;
30657 cp_token *token;
30659 /* Parse the declarator. */
30660 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30661 NULL, NULL, false, false);
30663 /* Look for attributes that apply to the ivar. */
30664 attributes = cp_parser_attributes_opt (parser);
30665 /* Remember which attributes are prefix attributes and
30666 which are not. */
30667 first_attribute = attributes;
30668 /* Combine the attributes. */
30669 attributes = chainon (prefix_attributes, attributes);
30671 decl = grokfield (declarator, &declspecs,
30672 NULL_TREE, /*init_const_expr_p=*/false,
30673 NULL_TREE, attributes);
30675 if (decl == error_mark_node || decl == NULL_TREE)
30676 return error_mark_node;
30678 /* Reset PREFIX_ATTRIBUTES. */
30679 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30680 attributes = TREE_CHAIN (attributes);
30681 if (attributes)
30682 TREE_CHAIN (attributes) = NULL_TREE;
30684 DECL_CHAIN (decl) = decls;
30685 decls = decl;
30687 token = cp_lexer_peek_token (parser->lexer);
30688 if (token->type == CPP_COMMA)
30690 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30691 continue;
30693 else
30694 break;
30696 return decls;
30699 /* Parse an Objective-C @property declaration. The syntax is:
30701 objc-property-declaration:
30702 '@property' objc-property-attributes[opt] struct-declaration ;
30704 objc-property-attributes:
30705 '(' objc-property-attribute-list ')'
30707 objc-property-attribute-list:
30708 objc-property-attribute
30709 objc-property-attribute-list, objc-property-attribute
30711 objc-property-attribute
30712 'getter' = identifier
30713 'setter' = identifier
30714 'readonly'
30715 'readwrite'
30716 'assign'
30717 'retain'
30718 'copy'
30719 'nonatomic'
30721 For example:
30722 @property NSString *name;
30723 @property (readonly) id object;
30724 @property (retain, nonatomic, getter=getTheName) id name;
30725 @property int a, b, c;
30727 PS: This function is identical to
30728 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30729 static void
30730 cp_parser_objc_at_property_declaration (cp_parser *parser)
30732 /* The following variables hold the attributes of the properties as
30733 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30734 seen. When we see an attribute, we set them to 'true' (if they
30735 are boolean properties) or to the identifier (if they have an
30736 argument, ie, for getter and setter). Note that here we only
30737 parse the list of attributes, check the syntax and accumulate the
30738 attributes that we find. objc_add_property_declaration() will
30739 then process the information. */
30740 bool property_assign = false;
30741 bool property_copy = false;
30742 tree property_getter_ident = NULL_TREE;
30743 bool property_nonatomic = false;
30744 bool property_readonly = false;
30745 bool property_readwrite = false;
30746 bool property_retain = false;
30747 tree property_setter_ident = NULL_TREE;
30749 /* 'properties' is the list of properties that we read. Usually a
30750 single one, but maybe more (eg, in "@property int a, b, c;" there
30751 are three). */
30752 tree properties;
30753 location_t loc;
30755 loc = cp_lexer_peek_token (parser->lexer)->location;
30757 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30759 /* Parse the optional attribute list... */
30760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30762 /* Eat the '('. */
30763 matching_parens parens;
30764 parens.consume_open (parser);
30766 while (true)
30768 bool syntax_error = false;
30769 cp_token *token = cp_lexer_peek_token (parser->lexer);
30770 enum rid keyword;
30772 if (token->type != CPP_NAME)
30774 cp_parser_error (parser, "expected identifier");
30775 break;
30777 keyword = C_RID_CODE (token->u.value);
30778 cp_lexer_consume_token (parser->lexer);
30779 switch (keyword)
30781 case RID_ASSIGN: property_assign = true; break;
30782 case RID_COPY: property_copy = true; break;
30783 case RID_NONATOMIC: property_nonatomic = true; break;
30784 case RID_READONLY: property_readonly = true; break;
30785 case RID_READWRITE: property_readwrite = true; break;
30786 case RID_RETAIN: property_retain = true; break;
30788 case RID_GETTER:
30789 case RID_SETTER:
30790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30792 if (keyword == RID_GETTER)
30793 cp_parser_error (parser,
30794 "missing %<=%> (after %<getter%> attribute)");
30795 else
30796 cp_parser_error (parser,
30797 "missing %<=%> (after %<setter%> attribute)");
30798 syntax_error = true;
30799 break;
30801 cp_lexer_consume_token (parser->lexer); /* eat the = */
30802 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30804 cp_parser_error (parser, "expected identifier");
30805 syntax_error = true;
30806 break;
30808 if (keyword == RID_SETTER)
30810 if (property_setter_ident != NULL_TREE)
30812 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30813 cp_lexer_consume_token (parser->lexer);
30815 else
30816 property_setter_ident = cp_parser_objc_selector (parser);
30817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30818 cp_parser_error (parser, "setter name must terminate with %<:%>");
30819 else
30820 cp_lexer_consume_token (parser->lexer);
30822 else
30824 if (property_getter_ident != NULL_TREE)
30826 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30827 cp_lexer_consume_token (parser->lexer);
30829 else
30830 property_getter_ident = cp_parser_objc_selector (parser);
30832 break;
30833 default:
30834 cp_parser_error (parser, "unknown property attribute");
30835 syntax_error = true;
30836 break;
30839 if (syntax_error)
30840 break;
30842 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30843 cp_lexer_consume_token (parser->lexer);
30844 else
30845 break;
30848 /* FIXME: "@property (setter, assign);" will generate a spurious
30849 "error: expected ‘)’ before ‘,’ token". This is because
30850 cp_parser_require, unlike the C counterpart, will produce an
30851 error even if we are in error recovery. */
30852 if (!parens.require_close (parser))
30854 cp_parser_skip_to_closing_parenthesis (parser,
30855 /*recovering=*/true,
30856 /*or_comma=*/false,
30857 /*consume_paren=*/true);
30861 /* ... and the property declaration(s). */
30862 properties = cp_parser_objc_struct_declaration (parser);
30864 if (properties == error_mark_node)
30866 cp_parser_skip_to_end_of_statement (parser);
30867 /* If the next token is now a `;', consume it. */
30868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30869 cp_lexer_consume_token (parser->lexer);
30870 return;
30873 if (properties == NULL_TREE)
30874 cp_parser_error (parser, "expected identifier");
30875 else
30877 /* Comma-separated properties are chained together in
30878 reverse order; add them one by one. */
30879 properties = nreverse (properties);
30881 for (; properties; properties = TREE_CHAIN (properties))
30882 objc_add_property_declaration (loc, copy_node (properties),
30883 property_readonly, property_readwrite,
30884 property_assign, property_retain,
30885 property_copy, property_nonatomic,
30886 property_getter_ident, property_setter_ident);
30889 cp_parser_consume_semicolon_at_end_of_statement (parser);
30892 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30894 objc-synthesize-declaration:
30895 @synthesize objc-synthesize-identifier-list ;
30897 objc-synthesize-identifier-list:
30898 objc-synthesize-identifier
30899 objc-synthesize-identifier-list, objc-synthesize-identifier
30901 objc-synthesize-identifier
30902 identifier
30903 identifier = identifier
30905 For example:
30906 @synthesize MyProperty;
30907 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30909 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30910 for C. Keep them in sync.
30912 static void
30913 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30915 tree list = NULL_TREE;
30916 location_t loc;
30917 loc = cp_lexer_peek_token (parser->lexer)->location;
30919 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30920 while (true)
30922 tree property, ivar;
30923 property = cp_parser_identifier (parser);
30924 if (property == error_mark_node)
30926 cp_parser_consume_semicolon_at_end_of_statement (parser);
30927 return;
30929 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30931 cp_lexer_consume_token (parser->lexer);
30932 ivar = cp_parser_identifier (parser);
30933 if (ivar == error_mark_node)
30935 cp_parser_consume_semicolon_at_end_of_statement (parser);
30936 return;
30939 else
30940 ivar = NULL_TREE;
30941 list = chainon (list, build_tree_list (ivar, property));
30942 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30943 cp_lexer_consume_token (parser->lexer);
30944 else
30945 break;
30947 cp_parser_consume_semicolon_at_end_of_statement (parser);
30948 objc_add_synthesize_declaration (loc, list);
30951 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30953 objc-dynamic-declaration:
30954 @dynamic identifier-list ;
30956 For example:
30957 @dynamic MyProperty;
30958 @dynamic MyProperty, AnotherProperty;
30960 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30961 for C. Keep them in sync.
30963 static void
30964 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30966 tree list = NULL_TREE;
30967 location_t loc;
30968 loc = cp_lexer_peek_token (parser->lexer)->location;
30970 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30971 while (true)
30973 tree property;
30974 property = cp_parser_identifier (parser);
30975 if (property == error_mark_node)
30977 cp_parser_consume_semicolon_at_end_of_statement (parser);
30978 return;
30980 list = chainon (list, build_tree_list (NULL, property));
30981 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30982 cp_lexer_consume_token (parser->lexer);
30983 else
30984 break;
30986 cp_parser_consume_semicolon_at_end_of_statement (parser);
30987 objc_add_dynamic_declaration (loc, list);
30991 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30993 /* Returns name of the next clause.
30994 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30995 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30996 returned and the token is consumed. */
30998 static pragma_omp_clause
30999 cp_parser_omp_clause_name (cp_parser *parser)
31001 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31003 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31004 result = PRAGMA_OACC_CLAUSE_AUTO;
31005 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31006 result = PRAGMA_OMP_CLAUSE_IF;
31007 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31008 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31009 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31010 result = PRAGMA_OACC_CLAUSE_DELETE;
31011 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31012 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31013 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31014 result = PRAGMA_OMP_CLAUSE_FOR;
31015 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31017 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31018 const char *p = IDENTIFIER_POINTER (id);
31020 switch (p[0])
31022 case 'a':
31023 if (!strcmp ("aligned", p))
31024 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31025 else if (!strcmp ("async", p))
31026 result = PRAGMA_OACC_CLAUSE_ASYNC;
31027 break;
31028 case 'c':
31029 if (!strcmp ("collapse", p))
31030 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31031 else if (!strcmp ("copy", p))
31032 result = PRAGMA_OACC_CLAUSE_COPY;
31033 else if (!strcmp ("copyin", p))
31034 result = PRAGMA_OMP_CLAUSE_COPYIN;
31035 else if (!strcmp ("copyout", p))
31036 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31037 else if (!strcmp ("copyprivate", p))
31038 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31039 else if (!strcmp ("create", p))
31040 result = PRAGMA_OACC_CLAUSE_CREATE;
31041 break;
31042 case 'd':
31043 if (!strcmp ("defaultmap", p))
31044 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31045 else if (!strcmp ("depend", p))
31046 result = PRAGMA_OMP_CLAUSE_DEPEND;
31047 else if (!strcmp ("device", p))
31048 result = PRAGMA_OMP_CLAUSE_DEVICE;
31049 else if (!strcmp ("deviceptr", p))
31050 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31051 else if (!strcmp ("device_resident", p))
31052 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31053 else if (!strcmp ("dist_schedule", p))
31054 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31055 break;
31056 case 'f':
31057 if (!strcmp ("final", p))
31058 result = PRAGMA_OMP_CLAUSE_FINAL;
31059 else if (!strcmp ("firstprivate", p))
31060 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31061 else if (!strcmp ("from", p))
31062 result = PRAGMA_OMP_CLAUSE_FROM;
31063 break;
31064 case 'g':
31065 if (!strcmp ("gang", p))
31066 result = PRAGMA_OACC_CLAUSE_GANG;
31067 else if (!strcmp ("grainsize", p))
31068 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31069 break;
31070 case 'h':
31071 if (!strcmp ("hint", p))
31072 result = PRAGMA_OMP_CLAUSE_HINT;
31073 else if (!strcmp ("host", p))
31074 result = PRAGMA_OACC_CLAUSE_HOST;
31075 break;
31076 case 'i':
31077 if (!strcmp ("inbranch", p))
31078 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31079 else if (!strcmp ("independent", p))
31080 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31081 else if (!strcmp ("is_device_ptr", p))
31082 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31083 break;
31084 case 'l':
31085 if (!strcmp ("lastprivate", p))
31086 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31087 else if (!strcmp ("linear", p))
31088 result = PRAGMA_OMP_CLAUSE_LINEAR;
31089 else if (!strcmp ("link", p))
31090 result = PRAGMA_OMP_CLAUSE_LINK;
31091 break;
31092 case 'm':
31093 if (!strcmp ("map", p))
31094 result = PRAGMA_OMP_CLAUSE_MAP;
31095 else if (!strcmp ("mergeable", p))
31096 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31097 break;
31098 case 'n':
31099 if (!strcmp ("nogroup", p))
31100 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31101 else if (!strcmp ("notinbranch", p))
31102 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31103 else if (!strcmp ("nowait", p))
31104 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31105 else if (!strcmp ("num_gangs", p))
31106 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31107 else if (!strcmp ("num_tasks", p))
31108 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31109 else if (!strcmp ("num_teams", p))
31110 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31111 else if (!strcmp ("num_threads", p))
31112 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31113 else if (!strcmp ("num_workers", p))
31114 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31115 break;
31116 case 'o':
31117 if (!strcmp ("ordered", p))
31118 result = PRAGMA_OMP_CLAUSE_ORDERED;
31119 break;
31120 case 'p':
31121 if (!strcmp ("parallel", p))
31122 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31123 else if (!strcmp ("present", p))
31124 result = PRAGMA_OACC_CLAUSE_PRESENT;
31125 else if (!strcmp ("present_or_copy", p)
31126 || !strcmp ("pcopy", p))
31127 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31128 else if (!strcmp ("present_or_copyin", p)
31129 || !strcmp ("pcopyin", p))
31130 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31131 else if (!strcmp ("present_or_copyout", p)
31132 || !strcmp ("pcopyout", p))
31133 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31134 else if (!strcmp ("present_or_create", p)
31135 || !strcmp ("pcreate", p))
31136 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31137 else if (!strcmp ("priority", p))
31138 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31139 else if (!strcmp ("proc_bind", p))
31140 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31141 break;
31142 case 'r':
31143 if (!strcmp ("reduction", p))
31144 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31145 break;
31146 case 's':
31147 if (!strcmp ("safelen", p))
31148 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31149 else if (!strcmp ("schedule", p))
31150 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31151 else if (!strcmp ("sections", p))
31152 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31153 else if (!strcmp ("self", p))
31154 result = PRAGMA_OACC_CLAUSE_SELF;
31155 else if (!strcmp ("seq", p))
31156 result = PRAGMA_OACC_CLAUSE_SEQ;
31157 else if (!strcmp ("shared", p))
31158 result = PRAGMA_OMP_CLAUSE_SHARED;
31159 else if (!strcmp ("simd", p))
31160 result = PRAGMA_OMP_CLAUSE_SIMD;
31161 else if (!strcmp ("simdlen", p))
31162 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31163 break;
31164 case 't':
31165 if (!strcmp ("taskgroup", p))
31166 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31167 else if (!strcmp ("thread_limit", p))
31168 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31169 else if (!strcmp ("threads", p))
31170 result = PRAGMA_OMP_CLAUSE_THREADS;
31171 else if (!strcmp ("tile", p))
31172 result = PRAGMA_OACC_CLAUSE_TILE;
31173 else if (!strcmp ("to", p))
31174 result = PRAGMA_OMP_CLAUSE_TO;
31175 break;
31176 case 'u':
31177 if (!strcmp ("uniform", p))
31178 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31179 else if (!strcmp ("untied", p))
31180 result = PRAGMA_OMP_CLAUSE_UNTIED;
31181 else if (!strcmp ("use_device", p))
31182 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31183 else if (!strcmp ("use_device_ptr", p))
31184 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31185 break;
31186 case 'v':
31187 if (!strcmp ("vector", p))
31188 result = PRAGMA_OACC_CLAUSE_VECTOR;
31189 else if (!strcmp ("vector_length", p))
31190 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31191 break;
31192 case 'w':
31193 if (!strcmp ("wait", p))
31194 result = PRAGMA_OACC_CLAUSE_WAIT;
31195 else if (!strcmp ("worker", p))
31196 result = PRAGMA_OACC_CLAUSE_WORKER;
31197 break;
31201 if (result != PRAGMA_OMP_CLAUSE_NONE)
31202 cp_lexer_consume_token (parser->lexer);
31204 return result;
31207 /* Validate that a clause of the given type does not already exist. */
31209 static void
31210 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31211 const char *name, location_t location)
31213 tree c;
31215 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31216 if (OMP_CLAUSE_CODE (c) == code)
31218 error_at (location, "too many %qs clauses", name);
31219 break;
31223 /* OpenMP 2.5:
31224 variable-list:
31225 identifier
31226 variable-list , identifier
31228 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31229 colon). An opening parenthesis will have been consumed by the caller.
31231 If KIND is nonzero, create the appropriate node and install the decl
31232 in OMP_CLAUSE_DECL and add the node to the head of the list.
31234 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31235 return the list created.
31237 COLON can be NULL if only closing parenthesis should end the list,
31238 or pointer to bool which will receive false if the list is terminated
31239 by closing parenthesis or true if the list is terminated by colon. */
31241 static tree
31242 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31243 tree list, bool *colon)
31245 cp_token *token;
31246 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31247 if (colon)
31249 parser->colon_corrects_to_scope_p = false;
31250 *colon = false;
31252 while (1)
31254 tree name, decl;
31256 token = cp_lexer_peek_token (parser->lexer);
31257 if (kind != 0
31258 && current_class_ptr
31259 && cp_parser_is_keyword (token, RID_THIS))
31261 decl = finish_this_expr ();
31262 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31263 || CONVERT_EXPR_P (decl))
31264 decl = TREE_OPERAND (decl, 0);
31265 cp_lexer_consume_token (parser->lexer);
31267 else
31269 name = cp_parser_id_expression (parser, /*template_p=*/false,
31270 /*check_dependency_p=*/true,
31271 /*template_p=*/NULL,
31272 /*declarator_p=*/false,
31273 /*optional_p=*/false);
31274 if (name == error_mark_node)
31275 goto skip_comma;
31277 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31278 if (decl == error_mark_node)
31279 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31280 token->location);
31282 if (decl == error_mark_node)
31284 else if (kind != 0)
31286 switch (kind)
31288 case OMP_CLAUSE__CACHE_:
31289 /* The OpenACC cache directive explicitly only allows "array
31290 elements or subarrays". */
31291 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31293 error_at (token->location, "expected %<[%>");
31294 decl = error_mark_node;
31295 break;
31297 /* FALLTHROUGH. */
31298 case OMP_CLAUSE_MAP:
31299 case OMP_CLAUSE_FROM:
31300 case OMP_CLAUSE_TO:
31301 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31303 location_t loc
31304 = cp_lexer_peek_token (parser->lexer)->location;
31305 cp_id_kind idk = CP_ID_KIND_NONE;
31306 cp_lexer_consume_token (parser->lexer);
31307 decl = convert_from_reference (decl);
31308 decl
31309 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31310 decl, false,
31311 &idk, loc);
31313 /* FALLTHROUGH. */
31314 case OMP_CLAUSE_DEPEND:
31315 case OMP_CLAUSE_REDUCTION:
31316 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31318 tree low_bound = NULL_TREE, length = NULL_TREE;
31320 parser->colon_corrects_to_scope_p = false;
31321 cp_lexer_consume_token (parser->lexer);
31322 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31323 low_bound = cp_parser_expression (parser);
31324 if (!colon)
31325 parser->colon_corrects_to_scope_p
31326 = saved_colon_corrects_to_scope_p;
31327 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31328 length = integer_one_node;
31329 else
31331 /* Look for `:'. */
31332 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31333 goto skip_comma;
31334 if (!cp_lexer_next_token_is (parser->lexer,
31335 CPP_CLOSE_SQUARE))
31336 length = cp_parser_expression (parser);
31338 /* Look for the closing `]'. */
31339 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31340 RT_CLOSE_SQUARE))
31341 goto skip_comma;
31343 decl = tree_cons (low_bound, length, decl);
31345 break;
31346 default:
31347 break;
31350 tree u = build_omp_clause (token->location, kind);
31351 OMP_CLAUSE_DECL (u) = decl;
31352 OMP_CLAUSE_CHAIN (u) = list;
31353 list = u;
31355 else
31356 list = tree_cons (decl, NULL_TREE, list);
31358 get_comma:
31359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31360 break;
31361 cp_lexer_consume_token (parser->lexer);
31364 if (colon)
31365 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31367 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31369 *colon = true;
31370 cp_parser_require (parser, CPP_COLON, RT_COLON);
31371 return list;
31374 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31376 int ending;
31378 /* Try to resync to an unnested comma. Copied from
31379 cp_parser_parenthesized_expression_list. */
31380 skip_comma:
31381 if (colon)
31382 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31383 ending = cp_parser_skip_to_closing_parenthesis (parser,
31384 /*recovering=*/true,
31385 /*or_comma=*/true,
31386 /*consume_paren=*/true);
31387 if (ending < 0)
31388 goto get_comma;
31391 return list;
31394 /* Similarly, but expect leading and trailing parenthesis. This is a very
31395 common case for omp clauses. */
31397 static tree
31398 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31400 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31401 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31402 return list;
31405 /* OpenACC 2.0:
31406 copy ( variable-list )
31407 copyin ( variable-list )
31408 copyout ( variable-list )
31409 create ( variable-list )
31410 delete ( variable-list )
31411 present ( variable-list )
31412 present_or_copy ( variable-list )
31413 pcopy ( variable-list )
31414 present_or_copyin ( variable-list )
31415 pcopyin ( variable-list )
31416 present_or_copyout ( variable-list )
31417 pcopyout ( variable-list )
31418 present_or_create ( variable-list )
31419 pcreate ( variable-list ) */
31421 static tree
31422 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31423 tree list)
31425 enum gomp_map_kind kind;
31426 switch (c_kind)
31428 case PRAGMA_OACC_CLAUSE_COPY:
31429 kind = GOMP_MAP_FORCE_TOFROM;
31430 break;
31431 case PRAGMA_OACC_CLAUSE_COPYIN:
31432 kind = GOMP_MAP_FORCE_TO;
31433 break;
31434 case PRAGMA_OACC_CLAUSE_COPYOUT:
31435 kind = GOMP_MAP_FORCE_FROM;
31436 break;
31437 case PRAGMA_OACC_CLAUSE_CREATE:
31438 kind = GOMP_MAP_FORCE_ALLOC;
31439 break;
31440 case PRAGMA_OACC_CLAUSE_DELETE:
31441 kind = GOMP_MAP_DELETE;
31442 break;
31443 case PRAGMA_OACC_CLAUSE_DEVICE:
31444 kind = GOMP_MAP_FORCE_TO;
31445 break;
31446 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31447 kind = GOMP_MAP_DEVICE_RESIDENT;
31448 break;
31449 case PRAGMA_OACC_CLAUSE_HOST:
31450 case PRAGMA_OACC_CLAUSE_SELF:
31451 kind = GOMP_MAP_FORCE_FROM;
31452 break;
31453 case PRAGMA_OACC_CLAUSE_LINK:
31454 kind = GOMP_MAP_LINK;
31455 break;
31456 case PRAGMA_OACC_CLAUSE_PRESENT:
31457 kind = GOMP_MAP_FORCE_PRESENT;
31458 break;
31459 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31460 kind = GOMP_MAP_TOFROM;
31461 break;
31462 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31463 kind = GOMP_MAP_TO;
31464 break;
31465 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31466 kind = GOMP_MAP_FROM;
31467 break;
31468 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31469 kind = GOMP_MAP_ALLOC;
31470 break;
31471 default:
31472 gcc_unreachable ();
31474 tree nl, c;
31475 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31477 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31478 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31480 return nl;
31483 /* OpenACC 2.0:
31484 deviceptr ( variable-list ) */
31486 static tree
31487 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31489 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31490 tree vars, t;
31492 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31493 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31494 variable-list must only allow for pointer variables. */
31495 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31496 for (t = vars; t; t = TREE_CHAIN (t))
31498 tree v = TREE_PURPOSE (t);
31499 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31500 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31501 OMP_CLAUSE_DECL (u) = v;
31502 OMP_CLAUSE_CHAIN (u) = list;
31503 list = u;
31506 return list;
31509 /* OpenACC 2.0:
31510 auto
31511 independent
31512 nohost
31513 seq */
31515 static tree
31516 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31517 enum omp_clause_code code,
31518 tree list, location_t location)
31520 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31521 tree c = build_omp_clause (location, code);
31522 OMP_CLAUSE_CHAIN (c) = list;
31523 return c;
31526 /* OpenACC:
31527 num_gangs ( expression )
31528 num_workers ( expression )
31529 vector_length ( expression ) */
31531 static tree
31532 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31533 const char *str, tree list)
31535 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31537 matching_parens parens;
31538 if (!parens.require_open (parser))
31539 return list;
31541 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31543 if (t == error_mark_node
31544 || !parens.require_close (parser))
31546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31547 /*or_comma=*/false,
31548 /*consume_paren=*/true);
31549 return list;
31552 check_no_duplicate_clause (list, code, str, loc);
31554 tree c = build_omp_clause (loc, code);
31555 OMP_CLAUSE_OPERAND (c, 0) = t;
31556 OMP_CLAUSE_CHAIN (c) = list;
31557 return c;
31560 /* OpenACC:
31562 gang [( gang-arg-list )]
31563 worker [( [num:] int-expr )]
31564 vector [( [length:] int-expr )]
31566 where gang-arg is one of:
31568 [num:] int-expr
31569 static: size-expr
31571 and size-expr may be:
31574 int-expr
31577 static tree
31578 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31579 const char *str, tree list)
31581 const char *id = "num";
31582 cp_lexer *lexer = parser->lexer;
31583 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31584 location_t loc = cp_lexer_peek_token (lexer)->location;
31586 if (kind == OMP_CLAUSE_VECTOR)
31587 id = "length";
31589 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31591 matching_parens parens;
31592 parens.consume_open (parser);
31596 cp_token *next = cp_lexer_peek_token (lexer);
31597 int idx = 0;
31599 /* Gang static argument. */
31600 if (kind == OMP_CLAUSE_GANG
31601 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31603 cp_lexer_consume_token (lexer);
31605 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31606 goto cleanup_error;
31608 idx = 1;
31609 if (ops[idx] != NULL)
31611 cp_parser_error (parser, "too many %<static%> arguments");
31612 goto cleanup_error;
31615 /* Check for the '*' argument. */
31616 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31617 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31618 || cp_lexer_nth_token_is (parser->lexer, 2,
31619 CPP_CLOSE_PAREN)))
31621 cp_lexer_consume_token (lexer);
31622 ops[idx] = integer_minus_one_node;
31624 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31626 cp_lexer_consume_token (lexer);
31627 continue;
31629 else break;
31632 /* Worker num: argument and vector length: arguments. */
31633 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31634 && id_equal (next->u.value, id)
31635 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31637 cp_lexer_consume_token (lexer); /* id */
31638 cp_lexer_consume_token (lexer); /* ':' */
31641 /* Now collect the actual argument. */
31642 if (ops[idx] != NULL_TREE)
31644 cp_parser_error (parser, "unexpected argument");
31645 goto cleanup_error;
31648 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31649 false);
31650 if (expr == error_mark_node)
31651 goto cleanup_error;
31653 mark_exp_read (expr);
31654 ops[idx] = expr;
31656 if (kind == OMP_CLAUSE_GANG
31657 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31659 cp_lexer_consume_token (lexer);
31660 continue;
31662 break;
31664 while (1);
31666 if (!parens.require_close (parser))
31667 goto cleanup_error;
31670 check_no_duplicate_clause (list, kind, str, loc);
31672 c = build_omp_clause (loc, kind);
31674 if (ops[1])
31675 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31677 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31678 OMP_CLAUSE_CHAIN (c) = list;
31680 return c;
31682 cleanup_error:
31683 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31684 return list;
31687 /* OpenACC 2.0:
31688 tile ( size-expr-list ) */
31690 static tree
31691 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31693 tree c, expr = error_mark_node;
31694 tree tile = NULL_TREE;
31696 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31697 so, but the spec authors never considered such a case and have
31698 differing opinions on what it might mean, including 'not
31699 allowed'.) */
31700 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31701 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31702 clause_loc);
31704 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31705 return list;
31709 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31710 return list;
31712 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31713 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31714 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31716 cp_lexer_consume_token (parser->lexer);
31717 expr = integer_zero_node;
31719 else
31720 expr = cp_parser_constant_expression (parser);
31722 tile = tree_cons (NULL_TREE, expr, tile);
31724 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31726 /* Consume the trailing ')'. */
31727 cp_lexer_consume_token (parser->lexer);
31729 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31730 tile = nreverse (tile);
31731 OMP_CLAUSE_TILE_LIST (c) = tile;
31732 OMP_CLAUSE_CHAIN (c) = list;
31733 return c;
31736 /* OpenACC 2.0
31737 Parse wait clause or directive parameters. */
31739 static tree
31740 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31742 vec<tree, va_gc> *args;
31743 tree t, args_tree;
31745 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31746 /*cast_p=*/false,
31747 /*allow_expansion_p=*/true,
31748 /*non_constant_p=*/NULL);
31750 if (args == NULL || args->length () == 0)
31752 cp_parser_error (parser, "expected integer expression before ')'");
31753 if (args != NULL)
31754 release_tree_vector (args);
31755 return list;
31758 args_tree = build_tree_list_vec (args);
31760 release_tree_vector (args);
31762 for (t = args_tree; t; t = TREE_CHAIN (t))
31764 tree targ = TREE_VALUE (t);
31766 if (targ != error_mark_node)
31768 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31769 error ("%<wait%> expression must be integral");
31770 else
31772 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31774 targ = mark_rvalue_use (targ);
31775 OMP_CLAUSE_DECL (c) = targ;
31776 OMP_CLAUSE_CHAIN (c) = list;
31777 list = c;
31782 return list;
31785 /* OpenACC:
31786 wait ( int-expr-list ) */
31788 static tree
31789 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31791 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31793 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31794 return list;
31796 list = cp_parser_oacc_wait_list (parser, location, list);
31798 return list;
31801 /* OpenMP 3.0:
31802 collapse ( constant-expression ) */
31804 static tree
31805 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31807 tree c, num;
31808 location_t loc;
31809 HOST_WIDE_INT n;
31811 loc = cp_lexer_peek_token (parser->lexer)->location;
31812 matching_parens parens;
31813 if (!parens.require_open (parser))
31814 return list;
31816 num = cp_parser_constant_expression (parser);
31818 if (!parens.require_close (parser))
31819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31820 /*or_comma=*/false,
31821 /*consume_paren=*/true);
31823 if (num == error_mark_node)
31824 return list;
31825 num = fold_non_dependent_expr (num);
31826 if (!tree_fits_shwi_p (num)
31827 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31828 || (n = tree_to_shwi (num)) <= 0
31829 || (int) n != n)
31831 error_at (loc, "collapse argument needs positive constant integer expression");
31832 return list;
31835 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31836 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31837 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31838 OMP_CLAUSE_CHAIN (c) = list;
31839 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31841 return c;
31844 /* OpenMP 2.5:
31845 default ( none | shared )
31847 OpenACC:
31848 default ( none | present ) */
31850 static tree
31851 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31852 location_t location, bool is_oacc)
31854 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31855 tree c;
31857 matching_parens parens;
31858 if (!parens.require_open (parser))
31859 return list;
31860 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31862 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31863 const char *p = IDENTIFIER_POINTER (id);
31865 switch (p[0])
31867 case 'n':
31868 if (strcmp ("none", p) != 0)
31869 goto invalid_kind;
31870 kind = OMP_CLAUSE_DEFAULT_NONE;
31871 break;
31873 case 'p':
31874 if (strcmp ("present", p) != 0 || !is_oacc)
31875 goto invalid_kind;
31876 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31877 break;
31879 case 's':
31880 if (strcmp ("shared", p) != 0 || is_oacc)
31881 goto invalid_kind;
31882 kind = OMP_CLAUSE_DEFAULT_SHARED;
31883 break;
31885 default:
31886 goto invalid_kind;
31889 cp_lexer_consume_token (parser->lexer);
31891 else
31893 invalid_kind:
31894 if (is_oacc)
31895 cp_parser_error (parser, "expected %<none%> or %<present%>");
31896 else
31897 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31900 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31901 || !parens.require_close (parser))
31902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31903 /*or_comma=*/false,
31904 /*consume_paren=*/true);
31906 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31907 return list;
31909 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31910 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31911 OMP_CLAUSE_CHAIN (c) = list;
31912 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31914 return c;
31917 /* OpenMP 3.1:
31918 final ( expression ) */
31920 static tree
31921 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31923 tree t, c;
31925 matching_parens parens;
31926 if (!parens.require_open (parser))
31927 return list;
31929 t = cp_parser_condition (parser);
31931 if (t == error_mark_node
31932 || !parens.require_close (parser))
31933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31934 /*or_comma=*/false,
31935 /*consume_paren=*/true);
31937 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31939 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31940 OMP_CLAUSE_FINAL_EXPR (c) = t;
31941 OMP_CLAUSE_CHAIN (c) = list;
31943 return c;
31946 /* OpenMP 2.5:
31947 if ( expression )
31949 OpenMP 4.5:
31950 if ( directive-name-modifier : expression )
31952 directive-name-modifier:
31953 parallel | task | taskloop | target data | target | target update
31954 | target enter data | target exit data */
31956 static tree
31957 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31958 bool is_omp)
31960 tree t, c;
31961 enum tree_code if_modifier = ERROR_MARK;
31963 matching_parens parens;
31964 if (!parens.require_open (parser))
31965 return list;
31967 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31970 const char *p = IDENTIFIER_POINTER (id);
31971 int n = 2;
31973 if (strcmp ("parallel", p) == 0)
31974 if_modifier = OMP_PARALLEL;
31975 else if (strcmp ("task", p) == 0)
31976 if_modifier = OMP_TASK;
31977 else if (strcmp ("taskloop", p) == 0)
31978 if_modifier = OMP_TASKLOOP;
31979 else if (strcmp ("target", p) == 0)
31981 if_modifier = OMP_TARGET;
31982 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31984 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31985 p = IDENTIFIER_POINTER (id);
31986 if (strcmp ("data", p) == 0)
31987 if_modifier = OMP_TARGET_DATA;
31988 else if (strcmp ("update", p) == 0)
31989 if_modifier = OMP_TARGET_UPDATE;
31990 else if (strcmp ("enter", p) == 0)
31991 if_modifier = OMP_TARGET_ENTER_DATA;
31992 else if (strcmp ("exit", p) == 0)
31993 if_modifier = OMP_TARGET_EXIT_DATA;
31994 if (if_modifier != OMP_TARGET)
31995 n = 3;
31996 else
31998 location_t loc
31999 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32000 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32001 "or %<exit%>");
32002 if_modifier = ERROR_MARK;
32004 if (if_modifier == OMP_TARGET_ENTER_DATA
32005 || if_modifier == OMP_TARGET_EXIT_DATA)
32007 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32009 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32010 p = IDENTIFIER_POINTER (id);
32011 if (strcmp ("data", p) == 0)
32012 n = 4;
32014 if (n != 4)
32016 location_t loc
32017 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32018 error_at (loc, "expected %<data%>");
32019 if_modifier = ERROR_MARK;
32024 if (if_modifier != ERROR_MARK)
32026 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32028 while (n-- > 0)
32029 cp_lexer_consume_token (parser->lexer);
32031 else
32033 if (n > 2)
32035 location_t loc
32036 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32037 error_at (loc, "expected %<:%>");
32039 if_modifier = ERROR_MARK;
32044 t = cp_parser_condition (parser);
32046 if (t == error_mark_node
32047 || !parens.require_close (parser))
32048 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32049 /*or_comma=*/false,
32050 /*consume_paren=*/true);
32052 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32053 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32055 if (if_modifier != ERROR_MARK
32056 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32058 const char *p = NULL;
32059 switch (if_modifier)
32061 case OMP_PARALLEL: p = "parallel"; break;
32062 case OMP_TASK: p = "task"; break;
32063 case OMP_TASKLOOP: p = "taskloop"; break;
32064 case OMP_TARGET_DATA: p = "target data"; break;
32065 case OMP_TARGET: p = "target"; break;
32066 case OMP_TARGET_UPDATE: p = "target update"; break;
32067 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32068 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32069 default: gcc_unreachable ();
32071 error_at (location, "too many %<if%> clauses with %qs modifier",
32073 return list;
32075 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32077 if (!is_omp)
32078 error_at (location, "too many %<if%> clauses");
32079 else
32080 error_at (location, "too many %<if%> clauses without modifier");
32081 return list;
32083 else if (if_modifier == ERROR_MARK
32084 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32086 error_at (location, "if any %<if%> clause has modifier, then all "
32087 "%<if%> clauses have to use modifier");
32088 return list;
32092 c = build_omp_clause (location, OMP_CLAUSE_IF);
32093 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32094 OMP_CLAUSE_IF_EXPR (c) = t;
32095 OMP_CLAUSE_CHAIN (c) = list;
32097 return c;
32100 /* OpenMP 3.1:
32101 mergeable */
32103 static tree
32104 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32105 tree list, location_t location)
32107 tree c;
32109 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32110 location);
32112 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32113 OMP_CLAUSE_CHAIN (c) = list;
32114 return c;
32117 /* OpenMP 2.5:
32118 nowait */
32120 static tree
32121 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32122 tree list, location_t location)
32124 tree c;
32126 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32128 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32129 OMP_CLAUSE_CHAIN (c) = list;
32130 return c;
32133 /* OpenMP 2.5:
32134 num_threads ( expression ) */
32136 static tree
32137 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32138 location_t location)
32140 tree t, c;
32142 matching_parens parens;
32143 if (!parens.require_open (parser))
32144 return list;
32146 t = cp_parser_expression (parser);
32148 if (t == error_mark_node
32149 || !parens.require_close (parser))
32150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32151 /*or_comma=*/false,
32152 /*consume_paren=*/true);
32154 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32155 "num_threads", location);
32157 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32158 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32159 OMP_CLAUSE_CHAIN (c) = list;
32161 return c;
32164 /* OpenMP 4.5:
32165 num_tasks ( expression ) */
32167 static tree
32168 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32169 location_t location)
32171 tree t, c;
32173 matching_parens parens;
32174 if (!parens.require_open (parser))
32175 return list;
32177 t = cp_parser_expression (parser);
32179 if (t == error_mark_node
32180 || !parens.require_close (parser))
32181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32182 /*or_comma=*/false,
32183 /*consume_paren=*/true);
32185 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32186 "num_tasks", location);
32188 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32189 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32190 OMP_CLAUSE_CHAIN (c) = list;
32192 return c;
32195 /* OpenMP 4.5:
32196 grainsize ( expression ) */
32198 static tree
32199 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32200 location_t location)
32202 tree t, c;
32204 matching_parens parens;
32205 if (!parens.require_open (parser))
32206 return list;
32208 t = cp_parser_expression (parser);
32210 if (t == error_mark_node
32211 || !parens.require_close (parser))
32212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32213 /*or_comma=*/false,
32214 /*consume_paren=*/true);
32216 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32217 "grainsize", location);
32219 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32220 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32221 OMP_CLAUSE_CHAIN (c) = list;
32223 return c;
32226 /* OpenMP 4.5:
32227 priority ( expression ) */
32229 static tree
32230 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32231 location_t location)
32233 tree t, c;
32235 matching_parens parens;
32236 if (!parens.require_open (parser))
32237 return list;
32239 t = cp_parser_expression (parser);
32241 if (t == error_mark_node
32242 || !parens.require_close (parser))
32243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32244 /*or_comma=*/false,
32245 /*consume_paren=*/true);
32247 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32248 "priority", location);
32250 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32251 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32252 OMP_CLAUSE_CHAIN (c) = list;
32254 return c;
32257 /* OpenMP 4.5:
32258 hint ( expression ) */
32260 static tree
32261 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32262 location_t location)
32264 tree t, c;
32266 matching_parens parens;
32267 if (!parens.require_open (parser))
32268 return list;
32270 t = cp_parser_expression (parser);
32272 if (t == error_mark_node
32273 || !parens.require_close (parser))
32274 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32275 /*or_comma=*/false,
32276 /*consume_paren=*/true);
32278 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32280 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32281 OMP_CLAUSE_HINT_EXPR (c) = t;
32282 OMP_CLAUSE_CHAIN (c) = list;
32284 return c;
32287 /* OpenMP 4.5:
32288 defaultmap ( tofrom : scalar ) */
32290 static tree
32291 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32292 location_t location)
32294 tree c, id;
32295 const char *p;
32297 matching_parens parens;
32298 if (!parens.require_open (parser))
32299 return list;
32301 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32303 cp_parser_error (parser, "expected %<tofrom%>");
32304 goto out_err;
32306 id = cp_lexer_peek_token (parser->lexer)->u.value;
32307 p = IDENTIFIER_POINTER (id);
32308 if (strcmp (p, "tofrom") != 0)
32310 cp_parser_error (parser, "expected %<tofrom%>");
32311 goto out_err;
32313 cp_lexer_consume_token (parser->lexer);
32314 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32315 goto out_err;
32317 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32319 cp_parser_error (parser, "expected %<scalar%>");
32320 goto out_err;
32322 id = cp_lexer_peek_token (parser->lexer)->u.value;
32323 p = IDENTIFIER_POINTER (id);
32324 if (strcmp (p, "scalar") != 0)
32326 cp_parser_error (parser, "expected %<scalar%>");
32327 goto out_err;
32329 cp_lexer_consume_token (parser->lexer);
32330 if (!parens.require_close (parser))
32331 goto out_err;
32333 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32334 location);
32336 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32337 OMP_CLAUSE_CHAIN (c) = list;
32338 return c;
32340 out_err:
32341 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32342 /*or_comma=*/false,
32343 /*consume_paren=*/true);
32344 return list;
32347 /* OpenMP 2.5:
32348 ordered
32350 OpenMP 4.5:
32351 ordered ( constant-expression ) */
32353 static tree
32354 cp_parser_omp_clause_ordered (cp_parser *parser,
32355 tree list, location_t location)
32357 tree c, num = NULL_TREE;
32358 HOST_WIDE_INT n;
32360 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32361 "ordered", location);
32363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32365 matching_parens parens;
32366 parens.consume_open (parser);
32368 num = cp_parser_constant_expression (parser);
32370 if (!parens.require_close (parser))
32371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 if (num == error_mark_node)
32376 return list;
32377 num = fold_non_dependent_expr (num);
32378 if (!tree_fits_shwi_p (num)
32379 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32380 || (n = tree_to_shwi (num)) <= 0
32381 || (int) n != n)
32383 error_at (location,
32384 "ordered argument needs positive constant integer "
32385 "expression");
32386 return list;
32390 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32391 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32392 OMP_CLAUSE_CHAIN (c) = list;
32393 return c;
32396 /* OpenMP 2.5:
32397 reduction ( reduction-operator : variable-list )
32399 reduction-operator:
32400 One of: + * - & ^ | && ||
32402 OpenMP 3.1:
32404 reduction-operator:
32405 One of: + * - & ^ | && || min max
32407 OpenMP 4.0:
32409 reduction-operator:
32410 One of: + * - & ^ | && ||
32411 id-expression */
32413 static tree
32414 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32416 enum tree_code code = ERROR_MARK;
32417 tree nlist, c, id = NULL_TREE;
32419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32420 return list;
32422 switch (cp_lexer_peek_token (parser->lexer)->type)
32424 case CPP_PLUS: code = PLUS_EXPR; break;
32425 case CPP_MULT: code = MULT_EXPR; break;
32426 case CPP_MINUS: code = MINUS_EXPR; break;
32427 case CPP_AND: code = BIT_AND_EXPR; break;
32428 case CPP_XOR: code = BIT_XOR_EXPR; break;
32429 case CPP_OR: code = BIT_IOR_EXPR; break;
32430 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32431 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32432 default: break;
32435 if (code != ERROR_MARK)
32436 cp_lexer_consume_token (parser->lexer);
32437 else
32439 bool saved_colon_corrects_to_scope_p;
32440 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32441 parser->colon_corrects_to_scope_p = false;
32442 id = cp_parser_id_expression (parser, /*template_p=*/false,
32443 /*check_dependency_p=*/true,
32444 /*template_p=*/NULL,
32445 /*declarator_p=*/false,
32446 /*optional_p=*/false);
32447 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32448 if (identifier_p (id))
32450 const char *p = IDENTIFIER_POINTER (id);
32452 if (strcmp (p, "min") == 0)
32453 code = MIN_EXPR;
32454 else if (strcmp (p, "max") == 0)
32455 code = MAX_EXPR;
32456 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32457 code = PLUS_EXPR;
32458 else if (id == ovl_op_identifier (false, MULT_EXPR))
32459 code = MULT_EXPR;
32460 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32461 code = MINUS_EXPR;
32462 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32463 code = BIT_AND_EXPR;
32464 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32465 code = BIT_IOR_EXPR;
32466 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32467 code = BIT_XOR_EXPR;
32468 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32469 code = TRUTH_ANDIF_EXPR;
32470 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32471 code = TRUTH_ORIF_EXPR;
32472 id = omp_reduction_id (code, id, NULL_TREE);
32473 tree scope = parser->scope;
32474 if (scope)
32475 id = build_qualified_name (NULL_TREE, scope, id, false);
32476 parser->scope = NULL_TREE;
32477 parser->qualifying_scope = NULL_TREE;
32478 parser->object_scope = NULL_TREE;
32480 else
32482 error ("invalid reduction-identifier");
32483 resync_fail:
32484 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32485 /*or_comma=*/false,
32486 /*consume_paren=*/true);
32487 return list;
32491 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32492 goto resync_fail;
32494 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32495 NULL);
32496 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32498 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32499 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32502 return nlist;
32505 /* OpenMP 2.5:
32506 schedule ( schedule-kind )
32507 schedule ( schedule-kind , expression )
32509 schedule-kind:
32510 static | dynamic | guided | runtime | auto
32512 OpenMP 4.5:
32513 schedule ( schedule-modifier : schedule-kind )
32514 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32516 schedule-modifier:
32517 simd
32518 monotonic
32519 nonmonotonic */
32521 static tree
32522 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32524 tree c, t;
32525 int modifiers = 0, nmodifiers = 0;
32527 matching_parens parens;
32528 if (!parens.require_open (parser))
32529 return list;
32531 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32533 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32536 const char *p = IDENTIFIER_POINTER (id);
32537 if (strcmp ("simd", p) == 0)
32538 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32539 else if (strcmp ("monotonic", p) == 0)
32540 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32541 else if (strcmp ("nonmonotonic", p) == 0)
32542 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32543 else
32544 break;
32545 cp_lexer_consume_token (parser->lexer);
32546 if (nmodifiers++ == 0
32547 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32548 cp_lexer_consume_token (parser->lexer);
32549 else
32551 cp_parser_require (parser, CPP_COLON, RT_COLON);
32552 break;
32556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32559 const char *p = IDENTIFIER_POINTER (id);
32561 switch (p[0])
32563 case 'd':
32564 if (strcmp ("dynamic", p) != 0)
32565 goto invalid_kind;
32566 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32567 break;
32569 case 'g':
32570 if (strcmp ("guided", p) != 0)
32571 goto invalid_kind;
32572 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32573 break;
32575 case 'r':
32576 if (strcmp ("runtime", p) != 0)
32577 goto invalid_kind;
32578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32579 break;
32581 default:
32582 goto invalid_kind;
32585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32589 else
32590 goto invalid_kind;
32591 cp_lexer_consume_token (parser->lexer);
32593 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32594 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32595 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32596 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32598 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32599 "specified");
32600 modifiers = 0;
32603 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32605 cp_token *token;
32606 cp_lexer_consume_token (parser->lexer);
32608 token = cp_lexer_peek_token (parser->lexer);
32609 t = cp_parser_assignment_expression (parser);
32611 if (t == error_mark_node)
32612 goto resync_fail;
32613 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32614 error_at (token->location, "schedule %<runtime%> does not take "
32615 "a %<chunk_size%> parameter");
32616 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32617 error_at (token->location, "schedule %<auto%> does not take "
32618 "a %<chunk_size%> parameter");
32619 else
32620 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32622 if (!parens.require_close (parser))
32623 goto resync_fail;
32625 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32626 goto resync_fail;
32628 OMP_CLAUSE_SCHEDULE_KIND (c)
32629 = (enum omp_clause_schedule_kind)
32630 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32632 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32633 OMP_CLAUSE_CHAIN (c) = list;
32634 return c;
32636 invalid_kind:
32637 cp_parser_error (parser, "invalid schedule kind");
32638 resync_fail:
32639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32640 /*or_comma=*/false,
32641 /*consume_paren=*/true);
32642 return list;
32645 /* OpenMP 3.0:
32646 untied */
32648 static tree
32649 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32650 tree list, location_t location)
32652 tree c;
32654 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32656 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32657 OMP_CLAUSE_CHAIN (c) = list;
32658 return c;
32661 /* OpenMP 4.0:
32662 inbranch
32663 notinbranch */
32665 static tree
32666 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32667 tree list, location_t location)
32669 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32670 tree c = build_omp_clause (location, code);
32671 OMP_CLAUSE_CHAIN (c) = list;
32672 return c;
32675 /* OpenMP 4.0:
32676 parallel
32678 sections
32679 taskgroup */
32681 static tree
32682 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32683 enum omp_clause_code code,
32684 tree list, location_t location)
32686 tree c = build_omp_clause (location, code);
32687 OMP_CLAUSE_CHAIN (c) = list;
32688 return c;
32691 /* OpenMP 4.5:
32692 nogroup */
32694 static tree
32695 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32696 tree list, location_t location)
32698 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32699 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32700 OMP_CLAUSE_CHAIN (c) = list;
32701 return c;
32704 /* OpenMP 4.5:
32705 simd
32706 threads */
32708 static tree
32709 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32710 enum omp_clause_code code,
32711 tree list, location_t location)
32713 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32714 tree c = build_omp_clause (location, code);
32715 OMP_CLAUSE_CHAIN (c) = list;
32716 return c;
32719 /* OpenMP 4.0:
32720 num_teams ( expression ) */
32722 static tree
32723 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32724 location_t location)
32726 tree t, c;
32728 matching_parens parens;
32729 if (!parens.require_open (parser))
32730 return list;
32732 t = cp_parser_expression (parser);
32734 if (t == error_mark_node
32735 || !parens.require_close (parser))
32736 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32737 /*or_comma=*/false,
32738 /*consume_paren=*/true);
32740 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32741 "num_teams", location);
32743 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32744 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32745 OMP_CLAUSE_CHAIN (c) = list;
32747 return c;
32750 /* OpenMP 4.0:
32751 thread_limit ( expression ) */
32753 static tree
32754 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32755 location_t location)
32757 tree t, c;
32759 matching_parens parens;
32760 if (!parens.require_open (parser))
32761 return list;
32763 t = cp_parser_expression (parser);
32765 if (t == error_mark_node
32766 || !parens.require_close (parser))
32767 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32768 /*or_comma=*/false,
32769 /*consume_paren=*/true);
32771 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32772 "thread_limit", location);
32774 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32775 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32776 OMP_CLAUSE_CHAIN (c) = list;
32778 return c;
32781 /* OpenMP 4.0:
32782 aligned ( variable-list )
32783 aligned ( variable-list : constant-expression ) */
32785 static tree
32786 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32788 tree nlist, c, alignment = NULL_TREE;
32789 bool colon;
32791 matching_parens parens;
32792 if (!parens.require_open (parser))
32793 return list;
32795 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32796 &colon);
32798 if (colon)
32800 alignment = cp_parser_constant_expression (parser);
32802 if (!parens.require_close (parser))
32803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32804 /*or_comma=*/false,
32805 /*consume_paren=*/true);
32807 if (alignment == error_mark_node)
32808 alignment = NULL_TREE;
32811 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32812 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32814 return nlist;
32817 /* OpenMP 4.0:
32818 linear ( variable-list )
32819 linear ( variable-list : expression )
32821 OpenMP 4.5:
32822 linear ( modifier ( variable-list ) )
32823 linear ( modifier ( variable-list ) : expression ) */
32825 static tree
32826 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32827 bool declare_simd)
32829 tree nlist, c, step = integer_one_node;
32830 bool colon;
32831 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32833 matching_parens parens;
32834 if (!parens.require_open (parser))
32835 return list;
32837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32840 const char *p = IDENTIFIER_POINTER (id);
32842 if (strcmp ("ref", p) == 0)
32843 kind = OMP_CLAUSE_LINEAR_REF;
32844 else if (strcmp ("val", p) == 0)
32845 kind = OMP_CLAUSE_LINEAR_VAL;
32846 else if (strcmp ("uval", p) == 0)
32847 kind = OMP_CLAUSE_LINEAR_UVAL;
32848 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32849 cp_lexer_consume_token (parser->lexer);
32850 else
32851 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32854 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32855 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32856 &colon);
32857 else
32859 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32860 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32861 if (colon)
32862 cp_parser_require (parser, CPP_COLON, RT_COLON);
32863 else if (!parens.require_close (parser))
32864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32865 /*or_comma=*/false,
32866 /*consume_paren=*/true);
32869 if (colon)
32871 step = NULL_TREE;
32872 if (declare_simd
32873 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32874 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32876 cp_token *token = cp_lexer_peek_token (parser->lexer);
32877 cp_parser_parse_tentatively (parser);
32878 step = cp_parser_id_expression (parser, /*template_p=*/false,
32879 /*check_dependency_p=*/true,
32880 /*template_p=*/NULL,
32881 /*declarator_p=*/false,
32882 /*optional_p=*/false);
32883 if (step != error_mark_node)
32884 step = cp_parser_lookup_name_simple (parser, step, token->location);
32885 if (step == error_mark_node)
32887 step = NULL_TREE;
32888 cp_parser_abort_tentative_parse (parser);
32890 else if (!cp_parser_parse_definitely (parser))
32891 step = NULL_TREE;
32893 if (!step)
32894 step = cp_parser_expression (parser);
32896 if (!parens.require_close (parser))
32897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32898 /*or_comma=*/false,
32899 /*consume_paren=*/true);
32901 if (step == error_mark_node)
32902 return list;
32905 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32907 OMP_CLAUSE_LINEAR_STEP (c) = step;
32908 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32911 return nlist;
32914 /* OpenMP 4.0:
32915 safelen ( constant-expression ) */
32917 static tree
32918 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32919 location_t location)
32921 tree t, c;
32923 matching_parens parens;
32924 if (!parens.require_open (parser))
32925 return list;
32927 t = cp_parser_constant_expression (parser);
32929 if (t == error_mark_node
32930 || !parens.require_close (parser))
32931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32932 /*or_comma=*/false,
32933 /*consume_paren=*/true);
32935 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32937 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32938 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32939 OMP_CLAUSE_CHAIN (c) = list;
32941 return c;
32944 /* OpenMP 4.0:
32945 simdlen ( constant-expression ) */
32947 static tree
32948 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32949 location_t location)
32951 tree t, c;
32953 matching_parens parens;
32954 if (!parens.require_open (parser))
32955 return list;
32957 t = cp_parser_constant_expression (parser);
32959 if (t == error_mark_node
32960 || !parens.require_close (parser))
32961 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32962 /*or_comma=*/false,
32963 /*consume_paren=*/true);
32965 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32967 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32968 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32969 OMP_CLAUSE_CHAIN (c) = list;
32971 return c;
32974 /* OpenMP 4.5:
32975 vec:
32976 identifier [+/- integer]
32977 vec , identifier [+/- integer]
32980 static tree
32981 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32982 tree list)
32984 tree vec = NULL;
32986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32988 cp_parser_error (parser, "expected identifier");
32989 return list;
32992 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32994 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32995 tree t, identifier = cp_parser_identifier (parser);
32996 tree addend = NULL;
32998 if (identifier == error_mark_node)
32999 t = error_mark_node;
33000 else
33002 t = cp_parser_lookup_name_simple
33003 (parser, identifier,
33004 cp_lexer_peek_token (parser->lexer)->location);
33005 if (t == error_mark_node)
33006 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33007 id_loc);
33010 bool neg = false;
33011 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33012 neg = true;
33013 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33015 addend = integer_zero_node;
33016 goto add_to_vector;
33018 cp_lexer_consume_token (parser->lexer);
33020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33022 cp_parser_error (parser, "expected integer");
33023 return list;
33026 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33027 if (TREE_CODE (addend) != INTEGER_CST)
33029 cp_parser_error (parser, "expected integer");
33030 return list;
33032 cp_lexer_consume_token (parser->lexer);
33034 add_to_vector:
33035 if (t != error_mark_node)
33037 vec = tree_cons (addend, t, vec);
33038 if (neg)
33039 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33043 break;
33045 cp_lexer_consume_token (parser->lexer);
33048 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33050 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33051 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33052 OMP_CLAUSE_DECL (u) = nreverse (vec);
33053 OMP_CLAUSE_CHAIN (u) = list;
33054 return u;
33056 return list;
33059 /* OpenMP 4.0:
33060 depend ( depend-kind : variable-list )
33062 depend-kind:
33063 in | out | inout
33065 OpenMP 4.5:
33066 depend ( source )
33068 depend ( sink : vec ) */
33070 static tree
33071 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33073 tree nlist, c;
33074 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33076 matching_parens parens;
33077 if (!parens.require_open (parser))
33078 return list;
33080 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33082 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33083 const char *p = IDENTIFIER_POINTER (id);
33085 if (strcmp ("in", p) == 0)
33086 kind = OMP_CLAUSE_DEPEND_IN;
33087 else if (strcmp ("inout", p) == 0)
33088 kind = OMP_CLAUSE_DEPEND_INOUT;
33089 else if (strcmp ("out", p) == 0)
33090 kind = OMP_CLAUSE_DEPEND_OUT;
33091 else if (strcmp ("source", p) == 0)
33092 kind = OMP_CLAUSE_DEPEND_SOURCE;
33093 else if (strcmp ("sink", p) == 0)
33094 kind = OMP_CLAUSE_DEPEND_SINK;
33095 else
33096 goto invalid_kind;
33098 else
33099 goto invalid_kind;
33101 cp_lexer_consume_token (parser->lexer);
33103 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33105 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33106 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33107 OMP_CLAUSE_DECL (c) = NULL_TREE;
33108 OMP_CLAUSE_CHAIN (c) = list;
33109 if (!parens.require_close (parser))
33110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33111 /*or_comma=*/false,
33112 /*consume_paren=*/true);
33113 return c;
33116 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33117 goto resync_fail;
33119 if (kind == OMP_CLAUSE_DEPEND_SINK)
33120 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33121 else
33123 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33124 list, NULL);
33126 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33127 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33129 return nlist;
33131 invalid_kind:
33132 cp_parser_error (parser, "invalid depend kind");
33133 resync_fail:
33134 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33135 /*or_comma=*/false,
33136 /*consume_paren=*/true);
33137 return list;
33140 /* OpenMP 4.0:
33141 map ( map-kind : variable-list )
33142 map ( variable-list )
33144 map-kind:
33145 alloc | to | from | tofrom
33147 OpenMP 4.5:
33148 map-kind:
33149 alloc | to | from | tofrom | release | delete
33151 map ( always [,] map-kind: variable-list ) */
33153 static tree
33154 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33156 tree nlist, c;
33157 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33158 bool always = false;
33160 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33161 return list;
33163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33166 const char *p = IDENTIFIER_POINTER (id);
33168 if (strcmp ("always", p) == 0)
33170 int nth = 2;
33171 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33172 nth++;
33173 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33174 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33175 == RID_DELETE))
33176 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33177 == CPP_COLON))
33179 always = true;
33180 cp_lexer_consume_token (parser->lexer);
33181 if (nth == 3)
33182 cp_lexer_consume_token (parser->lexer);
33187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33188 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33191 const char *p = IDENTIFIER_POINTER (id);
33193 if (strcmp ("alloc", p) == 0)
33194 kind = GOMP_MAP_ALLOC;
33195 else if (strcmp ("to", p) == 0)
33196 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33197 else if (strcmp ("from", p) == 0)
33198 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33199 else if (strcmp ("tofrom", p) == 0)
33200 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33201 else if (strcmp ("release", p) == 0)
33202 kind = GOMP_MAP_RELEASE;
33203 else
33205 cp_parser_error (parser, "invalid map kind");
33206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33207 /*or_comma=*/false,
33208 /*consume_paren=*/true);
33209 return list;
33211 cp_lexer_consume_token (parser->lexer);
33212 cp_lexer_consume_token (parser->lexer);
33214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33215 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33217 kind = GOMP_MAP_DELETE;
33218 cp_lexer_consume_token (parser->lexer);
33219 cp_lexer_consume_token (parser->lexer);
33222 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33223 NULL);
33225 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33226 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33228 return nlist;
33231 /* OpenMP 4.0:
33232 device ( expression ) */
33234 static tree
33235 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33236 location_t location)
33238 tree t, c;
33240 matching_parens parens;
33241 if (!parens.require_open (parser))
33242 return list;
33244 t = cp_parser_expression (parser);
33246 if (t == error_mark_node
33247 || !parens.require_close (parser))
33248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33249 /*or_comma=*/false,
33250 /*consume_paren=*/true);
33252 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33253 "device", location);
33255 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33256 OMP_CLAUSE_DEVICE_ID (c) = t;
33257 OMP_CLAUSE_CHAIN (c) = list;
33259 return c;
33262 /* OpenMP 4.0:
33263 dist_schedule ( static )
33264 dist_schedule ( static , expression ) */
33266 static tree
33267 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33268 location_t location)
33270 tree c, t;
33272 matching_parens parens;
33273 if (!parens.require_open (parser))
33274 return list;
33276 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33278 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33279 goto invalid_kind;
33280 cp_lexer_consume_token (parser->lexer);
33282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33284 cp_lexer_consume_token (parser->lexer);
33286 t = cp_parser_assignment_expression (parser);
33288 if (t == error_mark_node)
33289 goto resync_fail;
33290 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33292 if (!parens.require_close (parser))
33293 goto resync_fail;
33295 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33296 goto resync_fail;
33298 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33299 location);
33300 OMP_CLAUSE_CHAIN (c) = list;
33301 return c;
33303 invalid_kind:
33304 cp_parser_error (parser, "invalid dist_schedule kind");
33305 resync_fail:
33306 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33307 /*or_comma=*/false,
33308 /*consume_paren=*/true);
33309 return list;
33312 /* OpenMP 4.0:
33313 proc_bind ( proc-bind-kind )
33315 proc-bind-kind:
33316 master | close | spread */
33318 static tree
33319 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33320 location_t location)
33322 tree c;
33323 enum omp_clause_proc_bind_kind kind;
33325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33326 return list;
33328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33330 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33331 const char *p = IDENTIFIER_POINTER (id);
33333 if (strcmp ("master", p) == 0)
33334 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33335 else if (strcmp ("close", p) == 0)
33336 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33337 else if (strcmp ("spread", p) == 0)
33338 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33339 else
33340 goto invalid_kind;
33342 else
33343 goto invalid_kind;
33345 cp_lexer_consume_token (parser->lexer);
33346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33347 goto resync_fail;
33349 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33350 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33351 location);
33352 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33353 OMP_CLAUSE_CHAIN (c) = list;
33354 return c;
33356 invalid_kind:
33357 cp_parser_error (parser, "invalid depend kind");
33358 resync_fail:
33359 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33360 /*or_comma=*/false,
33361 /*consume_paren=*/true);
33362 return list;
33365 /* OpenACC:
33366 async [( int-expr )] */
33368 static tree
33369 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33371 tree c, t;
33372 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33374 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33376 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33378 matching_parens parens;
33379 parens.consume_open (parser);
33381 t = cp_parser_expression (parser);
33382 if (t == error_mark_node
33383 || !parens.require_close (parser))
33384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33385 /*or_comma=*/false,
33386 /*consume_paren=*/true);
33389 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33391 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33392 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33393 OMP_CLAUSE_CHAIN (c) = list;
33394 list = c;
33396 return list;
33399 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33400 is a bitmask in MASK. Return the list of clauses found. */
33402 static tree
33403 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33404 const char *where, cp_token *pragma_tok,
33405 bool finish_p = true)
33407 tree clauses = NULL;
33408 bool first = true;
33410 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33412 location_t here;
33413 pragma_omp_clause c_kind;
33414 omp_clause_code code;
33415 const char *c_name;
33416 tree prev = clauses;
33418 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33419 cp_lexer_consume_token (parser->lexer);
33421 here = cp_lexer_peek_token (parser->lexer)->location;
33422 c_kind = cp_parser_omp_clause_name (parser);
33424 switch (c_kind)
33426 case PRAGMA_OACC_CLAUSE_ASYNC:
33427 clauses = cp_parser_oacc_clause_async (parser, clauses);
33428 c_name = "async";
33429 break;
33430 case PRAGMA_OACC_CLAUSE_AUTO:
33431 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33432 clauses, here);
33433 c_name = "auto";
33434 break;
33435 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33436 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33437 c_name = "collapse";
33438 break;
33439 case PRAGMA_OACC_CLAUSE_COPY:
33440 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33441 c_name = "copy";
33442 break;
33443 case PRAGMA_OACC_CLAUSE_COPYIN:
33444 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33445 c_name = "copyin";
33446 break;
33447 case PRAGMA_OACC_CLAUSE_COPYOUT:
33448 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33449 c_name = "copyout";
33450 break;
33451 case PRAGMA_OACC_CLAUSE_CREATE:
33452 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33453 c_name = "create";
33454 break;
33455 case PRAGMA_OACC_CLAUSE_DELETE:
33456 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33457 c_name = "delete";
33458 break;
33459 case PRAGMA_OMP_CLAUSE_DEFAULT:
33460 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33461 c_name = "default";
33462 break;
33463 case PRAGMA_OACC_CLAUSE_DEVICE:
33464 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33465 c_name = "device";
33466 break;
33467 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33468 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33469 c_name = "deviceptr";
33470 break;
33471 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33472 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33473 c_name = "device_resident";
33474 break;
33475 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33477 clauses);
33478 c_name = "firstprivate";
33479 break;
33480 case PRAGMA_OACC_CLAUSE_GANG:
33481 c_name = "gang";
33482 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33483 c_name, clauses);
33484 break;
33485 case PRAGMA_OACC_CLAUSE_HOST:
33486 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33487 c_name = "host";
33488 break;
33489 case PRAGMA_OACC_CLAUSE_IF:
33490 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33491 c_name = "if";
33492 break;
33493 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33494 clauses = cp_parser_oacc_simple_clause (parser,
33495 OMP_CLAUSE_INDEPENDENT,
33496 clauses, here);
33497 c_name = "independent";
33498 break;
33499 case PRAGMA_OACC_CLAUSE_LINK:
33500 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33501 c_name = "link";
33502 break;
33503 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33504 code = OMP_CLAUSE_NUM_GANGS;
33505 c_name = "num_gangs";
33506 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33507 clauses);
33508 break;
33509 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33510 c_name = "num_workers";
33511 code = OMP_CLAUSE_NUM_WORKERS;
33512 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33513 clauses);
33514 break;
33515 case PRAGMA_OACC_CLAUSE_PRESENT:
33516 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33517 c_name = "present";
33518 break;
33519 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33520 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33521 c_name = "present_or_copy";
33522 break;
33523 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33524 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33525 c_name = "present_or_copyin";
33526 break;
33527 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33528 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33529 c_name = "present_or_copyout";
33530 break;
33531 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33532 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33533 c_name = "present_or_create";
33534 break;
33535 case PRAGMA_OACC_CLAUSE_PRIVATE:
33536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33537 clauses);
33538 c_name = "private";
33539 break;
33540 case PRAGMA_OACC_CLAUSE_REDUCTION:
33541 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33542 c_name = "reduction";
33543 break;
33544 case PRAGMA_OACC_CLAUSE_SELF:
33545 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33546 c_name = "self";
33547 break;
33548 case PRAGMA_OACC_CLAUSE_SEQ:
33549 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33550 clauses, here);
33551 c_name = "seq";
33552 break;
33553 case PRAGMA_OACC_CLAUSE_TILE:
33554 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33555 c_name = "tile";
33556 break;
33557 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33559 clauses);
33560 c_name = "use_device";
33561 break;
33562 case PRAGMA_OACC_CLAUSE_VECTOR:
33563 c_name = "vector";
33564 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33565 c_name, clauses);
33566 break;
33567 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33568 c_name = "vector_length";
33569 code = OMP_CLAUSE_VECTOR_LENGTH;
33570 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33571 clauses);
33572 break;
33573 case PRAGMA_OACC_CLAUSE_WAIT:
33574 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33575 c_name = "wait";
33576 break;
33577 case PRAGMA_OACC_CLAUSE_WORKER:
33578 c_name = "worker";
33579 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33580 c_name, clauses);
33581 break;
33582 default:
33583 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33584 goto saw_error;
33587 first = false;
33589 if (((mask >> c_kind) & 1) == 0)
33591 /* Remove the invalid clause(s) from the list to avoid
33592 confusing the rest of the compiler. */
33593 clauses = prev;
33594 error_at (here, "%qs is not valid for %qs", c_name, where);
33598 saw_error:
33599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33601 if (finish_p)
33602 return finish_omp_clauses (clauses, C_ORT_ACC);
33604 return clauses;
33607 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33608 is a bitmask in MASK. Return the list of clauses found; the result
33609 of clause default goes in *pdefault. */
33611 static tree
33612 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33613 const char *where, cp_token *pragma_tok,
33614 bool finish_p = true)
33616 tree clauses = NULL;
33617 bool first = true;
33618 cp_token *token = NULL;
33620 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33622 pragma_omp_clause c_kind;
33623 const char *c_name;
33624 tree prev = clauses;
33626 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33627 cp_lexer_consume_token (parser->lexer);
33629 token = cp_lexer_peek_token (parser->lexer);
33630 c_kind = cp_parser_omp_clause_name (parser);
33632 switch (c_kind)
33634 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33635 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33636 token->location);
33637 c_name = "collapse";
33638 break;
33639 case PRAGMA_OMP_CLAUSE_COPYIN:
33640 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33641 c_name = "copyin";
33642 break;
33643 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33644 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33645 clauses);
33646 c_name = "copyprivate";
33647 break;
33648 case PRAGMA_OMP_CLAUSE_DEFAULT:
33649 clauses = cp_parser_omp_clause_default (parser, clauses,
33650 token->location, false);
33651 c_name = "default";
33652 break;
33653 case PRAGMA_OMP_CLAUSE_FINAL:
33654 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33655 c_name = "final";
33656 break;
33657 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33659 clauses);
33660 c_name = "firstprivate";
33661 break;
33662 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33663 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33664 token->location);
33665 c_name = "grainsize";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_HINT:
33668 clauses = cp_parser_omp_clause_hint (parser, clauses,
33669 token->location);
33670 c_name = "hint";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33673 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33674 token->location);
33675 c_name = "defaultmap";
33676 break;
33677 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33678 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33679 clauses);
33680 c_name = "use_device_ptr";
33681 break;
33682 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33684 clauses);
33685 c_name = "is_device_ptr";
33686 break;
33687 case PRAGMA_OMP_CLAUSE_IF:
33688 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33689 true);
33690 c_name = "if";
33691 break;
33692 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33693 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33694 clauses);
33695 c_name = "lastprivate";
33696 break;
33697 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33698 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33699 token->location);
33700 c_name = "mergeable";
33701 break;
33702 case PRAGMA_OMP_CLAUSE_NOWAIT:
33703 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33704 c_name = "nowait";
33705 break;
33706 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33707 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33708 token->location);
33709 c_name = "num_tasks";
33710 break;
33711 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33712 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33713 token->location);
33714 c_name = "num_threads";
33715 break;
33716 case PRAGMA_OMP_CLAUSE_ORDERED:
33717 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33718 token->location);
33719 c_name = "ordered";
33720 break;
33721 case PRAGMA_OMP_CLAUSE_PRIORITY:
33722 clauses = cp_parser_omp_clause_priority (parser, clauses,
33723 token->location);
33724 c_name = "priority";
33725 break;
33726 case PRAGMA_OMP_CLAUSE_PRIVATE:
33727 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33728 clauses);
33729 c_name = "private";
33730 break;
33731 case PRAGMA_OMP_CLAUSE_REDUCTION:
33732 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33733 c_name = "reduction";
33734 break;
33735 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33736 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33737 token->location);
33738 c_name = "schedule";
33739 break;
33740 case PRAGMA_OMP_CLAUSE_SHARED:
33741 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33742 clauses);
33743 c_name = "shared";
33744 break;
33745 case PRAGMA_OMP_CLAUSE_UNTIED:
33746 clauses = cp_parser_omp_clause_untied (parser, clauses,
33747 token->location);
33748 c_name = "untied";
33749 break;
33750 case PRAGMA_OMP_CLAUSE_INBRANCH:
33751 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33752 clauses, token->location);
33753 c_name = "inbranch";
33754 break;
33755 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33756 clauses = cp_parser_omp_clause_branch (parser,
33757 OMP_CLAUSE_NOTINBRANCH,
33758 clauses, token->location);
33759 c_name = "notinbranch";
33760 break;
33761 case PRAGMA_OMP_CLAUSE_PARALLEL:
33762 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33763 clauses, token->location);
33764 c_name = "parallel";
33765 if (!first)
33767 clause_not_first:
33768 error_at (token->location, "%qs must be the first clause of %qs",
33769 c_name, where);
33770 clauses = prev;
33772 break;
33773 case PRAGMA_OMP_CLAUSE_FOR:
33774 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33775 clauses, token->location);
33776 c_name = "for";
33777 if (!first)
33778 goto clause_not_first;
33779 break;
33780 case PRAGMA_OMP_CLAUSE_SECTIONS:
33781 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33782 clauses, token->location);
33783 c_name = "sections";
33784 if (!first)
33785 goto clause_not_first;
33786 break;
33787 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33788 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33789 clauses, token->location);
33790 c_name = "taskgroup";
33791 if (!first)
33792 goto clause_not_first;
33793 break;
33794 case PRAGMA_OMP_CLAUSE_LINK:
33795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33796 c_name = "to";
33797 break;
33798 case PRAGMA_OMP_CLAUSE_TO:
33799 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33800 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33801 clauses);
33802 else
33803 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33804 c_name = "to";
33805 break;
33806 case PRAGMA_OMP_CLAUSE_FROM:
33807 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33808 c_name = "from";
33809 break;
33810 case PRAGMA_OMP_CLAUSE_UNIFORM:
33811 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33812 clauses);
33813 c_name = "uniform";
33814 break;
33815 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33816 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33817 token->location);
33818 c_name = "num_teams";
33819 break;
33820 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33821 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33822 token->location);
33823 c_name = "thread_limit";
33824 break;
33825 case PRAGMA_OMP_CLAUSE_ALIGNED:
33826 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33827 c_name = "aligned";
33828 break;
33829 case PRAGMA_OMP_CLAUSE_LINEAR:
33831 bool declare_simd = false;
33832 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33833 declare_simd = true;
33834 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33836 c_name = "linear";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_DEPEND:
33839 clauses = cp_parser_omp_clause_depend (parser, clauses,
33840 token->location);
33841 c_name = "depend";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_MAP:
33844 clauses = cp_parser_omp_clause_map (parser, clauses);
33845 c_name = "map";
33846 break;
33847 case PRAGMA_OMP_CLAUSE_DEVICE:
33848 clauses = cp_parser_omp_clause_device (parser, clauses,
33849 token->location);
33850 c_name = "device";
33851 break;
33852 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33853 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33854 token->location);
33855 c_name = "dist_schedule";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33858 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33859 token->location);
33860 c_name = "proc_bind";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_SAFELEN:
33863 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33864 token->location);
33865 c_name = "safelen";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33868 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33869 token->location);
33870 c_name = "simdlen";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_NOGROUP:
33873 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33874 token->location);
33875 c_name = "nogroup";
33876 break;
33877 case PRAGMA_OMP_CLAUSE_THREADS:
33878 clauses
33879 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33880 clauses, token->location);
33881 c_name = "threads";
33882 break;
33883 case PRAGMA_OMP_CLAUSE_SIMD:
33884 clauses
33885 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33886 clauses, token->location);
33887 c_name = "simd";
33888 break;
33889 default:
33890 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33891 goto saw_error;
33894 first = false;
33896 if (((mask >> c_kind) & 1) == 0)
33898 /* Remove the invalid clause(s) from the list to avoid
33899 confusing the rest of the compiler. */
33900 clauses = prev;
33901 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33904 saw_error:
33905 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33906 if (finish_p)
33908 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33909 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33910 else
33911 return finish_omp_clauses (clauses, C_ORT_OMP);
33913 return clauses;
33916 /* OpenMP 2.5:
33917 structured-block:
33918 statement
33920 In practice, we're also interested in adding the statement to an
33921 outer node. So it is convenient if we work around the fact that
33922 cp_parser_statement calls add_stmt. */
33924 static unsigned
33925 cp_parser_begin_omp_structured_block (cp_parser *parser)
33927 unsigned save = parser->in_statement;
33929 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33930 This preserves the "not within loop or switch" style error messages
33931 for nonsense cases like
33932 void foo() {
33933 #pragma omp single
33934 break;
33937 if (parser->in_statement)
33938 parser->in_statement = IN_OMP_BLOCK;
33940 return save;
33943 static void
33944 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33946 parser->in_statement = save;
33949 static tree
33950 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33952 tree stmt = begin_omp_structured_block ();
33953 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33955 cp_parser_statement (parser, NULL_TREE, false, if_p);
33957 cp_parser_end_omp_structured_block (parser, save);
33958 return finish_omp_structured_block (stmt);
33961 /* OpenMP 2.5:
33962 # pragma omp atomic new-line
33963 expression-stmt
33965 expression-stmt:
33966 x binop= expr | x++ | ++x | x-- | --x
33967 binop:
33968 +, *, -, /, &, ^, |, <<, >>
33970 where x is an lvalue expression with scalar type.
33972 OpenMP 3.1:
33973 # pragma omp atomic new-line
33974 update-stmt
33976 # pragma omp atomic read new-line
33977 read-stmt
33979 # pragma omp atomic write new-line
33980 write-stmt
33982 # pragma omp atomic update new-line
33983 update-stmt
33985 # pragma omp atomic capture new-line
33986 capture-stmt
33988 # pragma omp atomic capture new-line
33989 capture-block
33991 read-stmt:
33992 v = x
33993 write-stmt:
33994 x = expr
33995 update-stmt:
33996 expression-stmt | x = x binop expr
33997 capture-stmt:
33998 v = expression-stmt
33999 capture-block:
34000 { v = x; update-stmt; } | { update-stmt; v = x; }
34002 OpenMP 4.0:
34003 update-stmt:
34004 expression-stmt | x = x binop expr | x = expr binop x
34005 capture-stmt:
34006 v = update-stmt
34007 capture-block:
34008 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34010 where x and v are lvalue expressions with scalar type. */
34012 static void
34013 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34015 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34016 tree rhs1 = NULL_TREE, orig_lhs;
34017 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34018 bool structured_block = false;
34019 bool seq_cst = false;
34021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34023 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34024 const char *p = IDENTIFIER_POINTER (id);
34026 if (!strcmp (p, "seq_cst"))
34028 seq_cst = true;
34029 cp_lexer_consume_token (parser->lexer);
34030 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34031 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34032 cp_lexer_consume_token (parser->lexer);
34035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34037 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34038 const char *p = IDENTIFIER_POINTER (id);
34040 if (!strcmp (p, "read"))
34041 code = OMP_ATOMIC_READ;
34042 else if (!strcmp (p, "write"))
34043 code = NOP_EXPR;
34044 else if (!strcmp (p, "update"))
34045 code = OMP_ATOMIC;
34046 else if (!strcmp (p, "capture"))
34047 code = OMP_ATOMIC_CAPTURE_NEW;
34048 else
34049 p = NULL;
34050 if (p)
34051 cp_lexer_consume_token (parser->lexer);
34053 if (!seq_cst)
34055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34056 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34057 cp_lexer_consume_token (parser->lexer);
34059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34061 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34062 const char *p = IDENTIFIER_POINTER (id);
34064 if (!strcmp (p, "seq_cst"))
34066 seq_cst = true;
34067 cp_lexer_consume_token (parser->lexer);
34071 cp_parser_require_pragma_eol (parser, pragma_tok);
34073 switch (code)
34075 case OMP_ATOMIC_READ:
34076 case NOP_EXPR: /* atomic write */
34077 v = cp_parser_unary_expression (parser);
34078 if (v == error_mark_node)
34079 goto saw_error;
34080 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34081 goto saw_error;
34082 if (code == NOP_EXPR)
34083 lhs = cp_parser_expression (parser);
34084 else
34085 lhs = cp_parser_unary_expression (parser);
34086 if (lhs == error_mark_node)
34087 goto saw_error;
34088 if (code == NOP_EXPR)
34090 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34091 opcode. */
34092 code = OMP_ATOMIC;
34093 rhs = lhs;
34094 lhs = v;
34095 v = NULL_TREE;
34097 goto done;
34098 case OMP_ATOMIC_CAPTURE_NEW:
34099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34101 cp_lexer_consume_token (parser->lexer);
34102 structured_block = true;
34104 else
34106 v = cp_parser_unary_expression (parser);
34107 if (v == error_mark_node)
34108 goto saw_error;
34109 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34110 goto saw_error;
34112 default:
34113 break;
34116 restart:
34117 lhs = cp_parser_unary_expression (parser);
34118 orig_lhs = lhs;
34119 switch (TREE_CODE (lhs))
34121 case ERROR_MARK:
34122 goto saw_error;
34124 case POSTINCREMENT_EXPR:
34125 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34126 code = OMP_ATOMIC_CAPTURE_OLD;
34127 /* FALLTHROUGH */
34128 case PREINCREMENT_EXPR:
34129 lhs = TREE_OPERAND (lhs, 0);
34130 opcode = PLUS_EXPR;
34131 rhs = integer_one_node;
34132 break;
34134 case POSTDECREMENT_EXPR:
34135 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34136 code = OMP_ATOMIC_CAPTURE_OLD;
34137 /* FALLTHROUGH */
34138 case PREDECREMENT_EXPR:
34139 lhs = TREE_OPERAND (lhs, 0);
34140 opcode = MINUS_EXPR;
34141 rhs = integer_one_node;
34142 break;
34144 case COMPOUND_EXPR:
34145 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34146 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34147 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34148 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34149 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34150 (TREE_OPERAND (lhs, 1), 0), 0)))
34151 == BOOLEAN_TYPE)
34152 /* Undo effects of boolean_increment for post {in,de}crement. */
34153 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34154 /* FALLTHRU */
34155 case MODIFY_EXPR:
34156 if (TREE_CODE (lhs) == MODIFY_EXPR
34157 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34159 /* Undo effects of boolean_increment. */
34160 if (integer_onep (TREE_OPERAND (lhs, 1)))
34162 /* This is pre or post increment. */
34163 rhs = TREE_OPERAND (lhs, 1);
34164 lhs = TREE_OPERAND (lhs, 0);
34165 opcode = NOP_EXPR;
34166 if (code == OMP_ATOMIC_CAPTURE_NEW
34167 && !structured_block
34168 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34169 code = OMP_ATOMIC_CAPTURE_OLD;
34170 break;
34173 /* FALLTHRU */
34174 default:
34175 switch (cp_lexer_peek_token (parser->lexer)->type)
34177 case CPP_MULT_EQ:
34178 opcode = MULT_EXPR;
34179 break;
34180 case CPP_DIV_EQ:
34181 opcode = TRUNC_DIV_EXPR;
34182 break;
34183 case CPP_PLUS_EQ:
34184 opcode = PLUS_EXPR;
34185 break;
34186 case CPP_MINUS_EQ:
34187 opcode = MINUS_EXPR;
34188 break;
34189 case CPP_LSHIFT_EQ:
34190 opcode = LSHIFT_EXPR;
34191 break;
34192 case CPP_RSHIFT_EQ:
34193 opcode = RSHIFT_EXPR;
34194 break;
34195 case CPP_AND_EQ:
34196 opcode = BIT_AND_EXPR;
34197 break;
34198 case CPP_OR_EQ:
34199 opcode = BIT_IOR_EXPR;
34200 break;
34201 case CPP_XOR_EQ:
34202 opcode = BIT_XOR_EXPR;
34203 break;
34204 case CPP_EQ:
34205 enum cp_parser_prec oprec;
34206 cp_token *token;
34207 cp_lexer_consume_token (parser->lexer);
34208 cp_parser_parse_tentatively (parser);
34209 rhs1 = cp_parser_simple_cast_expression (parser);
34210 if (rhs1 == error_mark_node)
34212 cp_parser_abort_tentative_parse (parser);
34213 cp_parser_simple_cast_expression (parser);
34214 goto saw_error;
34216 token = cp_lexer_peek_token (parser->lexer);
34217 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34219 cp_parser_abort_tentative_parse (parser);
34220 cp_parser_parse_tentatively (parser);
34221 rhs = cp_parser_binary_expression (parser, false, true,
34222 PREC_NOT_OPERATOR, NULL);
34223 if (rhs == error_mark_node)
34225 cp_parser_abort_tentative_parse (parser);
34226 cp_parser_binary_expression (parser, false, true,
34227 PREC_NOT_OPERATOR, NULL);
34228 goto saw_error;
34230 switch (TREE_CODE (rhs))
34232 case MULT_EXPR:
34233 case TRUNC_DIV_EXPR:
34234 case RDIV_EXPR:
34235 case PLUS_EXPR:
34236 case MINUS_EXPR:
34237 case LSHIFT_EXPR:
34238 case RSHIFT_EXPR:
34239 case BIT_AND_EXPR:
34240 case BIT_IOR_EXPR:
34241 case BIT_XOR_EXPR:
34242 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34244 if (cp_parser_parse_definitely (parser))
34246 opcode = TREE_CODE (rhs);
34247 rhs1 = TREE_OPERAND (rhs, 0);
34248 rhs = TREE_OPERAND (rhs, 1);
34249 goto stmt_done;
34251 else
34252 goto saw_error;
34254 break;
34255 default:
34256 break;
34258 cp_parser_abort_tentative_parse (parser);
34259 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34261 rhs = cp_parser_expression (parser);
34262 if (rhs == error_mark_node)
34263 goto saw_error;
34264 opcode = NOP_EXPR;
34265 rhs1 = NULL_TREE;
34266 goto stmt_done;
34268 cp_parser_error (parser,
34269 "invalid form of %<#pragma omp atomic%>");
34270 goto saw_error;
34272 if (!cp_parser_parse_definitely (parser))
34273 goto saw_error;
34274 switch (token->type)
34276 case CPP_SEMICOLON:
34277 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34279 code = OMP_ATOMIC_CAPTURE_OLD;
34280 v = lhs;
34281 lhs = NULL_TREE;
34282 lhs1 = rhs1;
34283 rhs1 = NULL_TREE;
34284 cp_lexer_consume_token (parser->lexer);
34285 goto restart;
34287 else if (structured_block)
34289 opcode = NOP_EXPR;
34290 rhs = rhs1;
34291 rhs1 = NULL_TREE;
34292 goto stmt_done;
34294 cp_parser_error (parser,
34295 "invalid form of %<#pragma omp atomic%>");
34296 goto saw_error;
34297 case CPP_MULT:
34298 opcode = MULT_EXPR;
34299 break;
34300 case CPP_DIV:
34301 opcode = TRUNC_DIV_EXPR;
34302 break;
34303 case CPP_PLUS:
34304 opcode = PLUS_EXPR;
34305 break;
34306 case CPP_MINUS:
34307 opcode = MINUS_EXPR;
34308 break;
34309 case CPP_LSHIFT:
34310 opcode = LSHIFT_EXPR;
34311 break;
34312 case CPP_RSHIFT:
34313 opcode = RSHIFT_EXPR;
34314 break;
34315 case CPP_AND:
34316 opcode = BIT_AND_EXPR;
34317 break;
34318 case CPP_OR:
34319 opcode = BIT_IOR_EXPR;
34320 break;
34321 case CPP_XOR:
34322 opcode = BIT_XOR_EXPR;
34323 break;
34324 default:
34325 cp_parser_error (parser,
34326 "invalid operator for %<#pragma omp atomic%>");
34327 goto saw_error;
34329 oprec = TOKEN_PRECEDENCE (token);
34330 gcc_assert (oprec != PREC_NOT_OPERATOR);
34331 if (commutative_tree_code (opcode))
34332 oprec = (enum cp_parser_prec) (oprec - 1);
34333 cp_lexer_consume_token (parser->lexer);
34334 rhs = cp_parser_binary_expression (parser, false, false,
34335 oprec, NULL);
34336 if (rhs == error_mark_node)
34337 goto saw_error;
34338 goto stmt_done;
34339 /* FALLTHROUGH */
34340 default:
34341 cp_parser_error (parser,
34342 "invalid operator for %<#pragma omp atomic%>");
34343 goto saw_error;
34345 cp_lexer_consume_token (parser->lexer);
34347 rhs = cp_parser_expression (parser);
34348 if (rhs == error_mark_node)
34349 goto saw_error;
34350 break;
34352 stmt_done:
34353 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34355 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34356 goto saw_error;
34357 v = cp_parser_unary_expression (parser);
34358 if (v == error_mark_node)
34359 goto saw_error;
34360 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34361 goto saw_error;
34362 lhs1 = cp_parser_unary_expression (parser);
34363 if (lhs1 == error_mark_node)
34364 goto saw_error;
34366 if (structured_block)
34368 cp_parser_consume_semicolon_at_end_of_statement (parser);
34369 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34371 done:
34372 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34373 if (!structured_block)
34374 cp_parser_consume_semicolon_at_end_of_statement (parser);
34375 return;
34377 saw_error:
34378 cp_parser_skip_to_end_of_block_or_statement (parser);
34379 if (structured_block)
34381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34382 cp_lexer_consume_token (parser->lexer);
34383 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34385 cp_parser_skip_to_end_of_block_or_statement (parser);
34386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34387 cp_lexer_consume_token (parser->lexer);
34393 /* OpenMP 2.5:
34394 # pragma omp barrier new-line */
34396 static void
34397 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34399 cp_parser_require_pragma_eol (parser, pragma_tok);
34400 finish_omp_barrier ();
34403 /* OpenMP 2.5:
34404 # pragma omp critical [(name)] new-line
34405 structured-block
34407 OpenMP 4.5:
34408 # pragma omp critical [(name) [hint(expression)]] new-line
34409 structured-block */
34411 #define OMP_CRITICAL_CLAUSE_MASK \
34412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34414 static tree
34415 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34417 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34421 matching_parens parens;
34422 parens.consume_open (parser);
34424 name = cp_parser_identifier (parser);
34426 if (name == error_mark_node
34427 || !parens.require_close (parser))
34428 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34429 /*or_comma=*/false,
34430 /*consume_paren=*/true);
34431 if (name == error_mark_node)
34432 name = NULL;
34434 clauses = cp_parser_omp_all_clauses (parser,
34435 OMP_CRITICAL_CLAUSE_MASK,
34436 "#pragma omp critical", pragma_tok);
34438 else
34439 cp_parser_require_pragma_eol (parser, pragma_tok);
34441 stmt = cp_parser_omp_structured_block (parser, if_p);
34442 return c_finish_omp_critical (input_location, stmt, name, clauses);
34445 /* OpenMP 2.5:
34446 # pragma omp flush flush-vars[opt] new-line
34448 flush-vars:
34449 ( variable-list ) */
34451 static void
34452 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34455 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34456 cp_parser_require_pragma_eol (parser, pragma_tok);
34458 finish_omp_flush ();
34461 /* Helper function, to parse omp for increment expression. */
34463 static tree
34464 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34466 tree cond = cp_parser_binary_expression (parser, false, true,
34467 PREC_NOT_OPERATOR, NULL);
34468 if (cond == error_mark_node
34469 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34471 cp_parser_skip_to_end_of_statement (parser);
34472 return error_mark_node;
34475 switch (TREE_CODE (cond))
34477 case GT_EXPR:
34478 case GE_EXPR:
34479 case LT_EXPR:
34480 case LE_EXPR:
34481 break;
34482 case NE_EXPR:
34483 /* Fall through: OpenMP disallows NE_EXPR. */
34484 gcc_fallthrough ();
34485 default:
34486 return error_mark_node;
34489 /* If decl is an iterator, preserve LHS and RHS of the relational
34490 expr until finish_omp_for. */
34491 if (decl
34492 && (type_dependent_expression_p (decl)
34493 || CLASS_TYPE_P (TREE_TYPE (decl))))
34494 return cond;
34496 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34497 TREE_CODE (cond),
34498 TREE_OPERAND (cond, 0), ERROR_MARK,
34499 TREE_OPERAND (cond, 1), ERROR_MARK,
34500 /*overload=*/NULL, tf_warning_or_error);
34503 /* Helper function, to parse omp for increment expression. */
34505 static tree
34506 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34508 cp_token *token = cp_lexer_peek_token (parser->lexer);
34509 enum tree_code op;
34510 tree lhs, rhs;
34511 cp_id_kind idk;
34512 bool decl_first;
34514 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34516 op = (token->type == CPP_PLUS_PLUS
34517 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34518 cp_lexer_consume_token (parser->lexer);
34519 lhs = cp_parser_simple_cast_expression (parser);
34520 if (lhs != decl
34521 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34522 return error_mark_node;
34523 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34526 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34527 if (lhs != decl
34528 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34529 return error_mark_node;
34531 token = cp_lexer_peek_token (parser->lexer);
34532 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34534 op = (token->type == CPP_PLUS_PLUS
34535 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34536 cp_lexer_consume_token (parser->lexer);
34537 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34540 op = cp_parser_assignment_operator_opt (parser);
34541 if (op == ERROR_MARK)
34542 return error_mark_node;
34544 if (op != NOP_EXPR)
34546 rhs = cp_parser_assignment_expression (parser);
34547 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34548 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34551 lhs = cp_parser_binary_expression (parser, false, false,
34552 PREC_ADDITIVE_EXPRESSION, NULL);
34553 token = cp_lexer_peek_token (parser->lexer);
34554 decl_first = (lhs == decl
34555 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34556 if (decl_first)
34557 lhs = NULL_TREE;
34558 if (token->type != CPP_PLUS
34559 && token->type != CPP_MINUS)
34560 return error_mark_node;
34564 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34565 cp_lexer_consume_token (parser->lexer);
34566 rhs = cp_parser_binary_expression (parser, false, false,
34567 PREC_ADDITIVE_EXPRESSION, NULL);
34568 token = cp_lexer_peek_token (parser->lexer);
34569 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34571 if (lhs == NULL_TREE)
34573 if (op == PLUS_EXPR)
34574 lhs = rhs;
34575 else
34576 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34577 tf_warning_or_error);
34579 else
34580 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34581 ERROR_MARK, NULL, tf_warning_or_error);
34584 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34586 if (!decl_first)
34588 if ((rhs != decl
34589 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34590 || op == MINUS_EXPR)
34591 return error_mark_node;
34592 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34594 else
34595 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34597 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34600 /* Parse the initialization statement of an OpenMP for loop.
34602 Return true if the resulting construct should have an
34603 OMP_CLAUSE_PRIVATE added to it. */
34605 static tree
34606 cp_parser_omp_for_loop_init (cp_parser *parser,
34607 tree &this_pre_body,
34608 vec<tree, va_gc> *for_block,
34609 tree &init,
34610 tree &orig_init,
34611 tree &decl,
34612 tree &real_decl)
34614 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34615 return NULL_TREE;
34617 tree add_private_clause = NULL_TREE;
34619 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34621 init-expr:
34622 var = lb
34623 integer-type var = lb
34624 random-access-iterator-type var = lb
34625 pointer-type var = lb
34627 cp_decl_specifier_seq type_specifiers;
34629 /* First, try to parse as an initialized declaration. See
34630 cp_parser_condition, from whence the bulk of this is copied. */
34632 cp_parser_parse_tentatively (parser);
34633 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34634 /*is_trailing_return=*/false,
34635 &type_specifiers);
34636 if (cp_parser_parse_definitely (parser))
34638 /* If parsing a type specifier seq succeeded, then this
34639 MUST be a initialized declaration. */
34640 tree asm_specification, attributes;
34641 cp_declarator *declarator;
34643 declarator = cp_parser_declarator (parser,
34644 CP_PARSER_DECLARATOR_NAMED,
34645 /*ctor_dtor_or_conv_p=*/NULL,
34646 /*parenthesized_p=*/NULL,
34647 /*member_p=*/false,
34648 /*friend_p=*/false);
34649 attributes = cp_parser_attributes_opt (parser);
34650 asm_specification = cp_parser_asm_specification_opt (parser);
34652 if (declarator == cp_error_declarator)
34653 cp_parser_skip_to_end_of_statement (parser);
34655 else
34657 tree pushed_scope, auto_node;
34659 decl = start_decl (declarator, &type_specifiers,
34660 SD_INITIALIZED, attributes,
34661 /*prefix_attributes=*/NULL_TREE,
34662 &pushed_scope);
34664 auto_node = type_uses_auto (TREE_TYPE (decl));
34665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34667 if (cp_lexer_next_token_is (parser->lexer,
34668 CPP_OPEN_PAREN))
34669 error ("parenthesized initialization is not allowed in "
34670 "OpenMP %<for%> loop");
34671 else
34672 /* Trigger an error. */
34673 cp_parser_require (parser, CPP_EQ, RT_EQ);
34675 init = error_mark_node;
34676 cp_parser_skip_to_end_of_statement (parser);
34678 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34679 || type_dependent_expression_p (decl)
34680 || auto_node)
34682 bool is_direct_init, is_non_constant_init;
34684 init = cp_parser_initializer (parser,
34685 &is_direct_init,
34686 &is_non_constant_init);
34688 if (auto_node)
34690 TREE_TYPE (decl)
34691 = do_auto_deduction (TREE_TYPE (decl), init,
34692 auto_node);
34694 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34695 && !type_dependent_expression_p (decl))
34696 goto non_class;
34699 cp_finish_decl (decl, init, !is_non_constant_init,
34700 asm_specification,
34701 LOOKUP_ONLYCONVERTING);
34702 orig_init = init;
34703 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34705 vec_safe_push (for_block, this_pre_body);
34706 init = NULL_TREE;
34708 else
34710 init = pop_stmt_list (this_pre_body);
34711 if (init && TREE_CODE (init) == STATEMENT_LIST)
34713 tree_stmt_iterator i = tsi_start (init);
34714 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34715 while (!tsi_end_p (i))
34717 tree t = tsi_stmt (i);
34718 if (TREE_CODE (t) == DECL_EXPR
34719 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34721 tsi_delink (&i);
34722 vec_safe_push (for_block, t);
34723 continue;
34725 break;
34727 if (tsi_one_before_end_p (i))
34729 tree t = tsi_stmt (i);
34730 tsi_delink (&i);
34731 free_stmt_list (init);
34732 init = t;
34736 this_pre_body = NULL_TREE;
34738 else
34740 /* Consume '='. */
34741 cp_lexer_consume_token (parser->lexer);
34742 init = cp_parser_assignment_expression (parser);
34744 non_class:
34745 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34746 init = error_mark_node;
34747 else
34748 cp_finish_decl (decl, NULL_TREE,
34749 /*init_const_expr_p=*/false,
34750 asm_specification,
34751 LOOKUP_ONLYCONVERTING);
34754 if (pushed_scope)
34755 pop_scope (pushed_scope);
34758 else
34760 cp_id_kind idk;
34761 /* If parsing a type specifier sequence failed, then
34762 this MUST be a simple expression. */
34763 cp_parser_parse_tentatively (parser);
34764 decl = cp_parser_primary_expression (parser, false, false,
34765 false, &idk);
34766 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34767 if (!cp_parser_error_occurred (parser)
34768 && decl
34769 && (TREE_CODE (decl) == COMPONENT_REF
34770 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34772 cp_parser_abort_tentative_parse (parser);
34773 cp_parser_parse_tentatively (parser);
34774 cp_token *token = cp_lexer_peek_token (parser->lexer);
34775 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34776 /*check_dependency_p=*/true,
34777 /*template_p=*/NULL,
34778 /*declarator_p=*/false,
34779 /*optional_p=*/false);
34780 if (name != error_mark_node
34781 && last_tok == cp_lexer_peek_token (parser->lexer))
34783 decl = cp_parser_lookup_name_simple (parser, name,
34784 token->location);
34785 if (TREE_CODE (decl) == FIELD_DECL)
34786 add_private_clause = omp_privatize_field (decl, false);
34788 cp_parser_abort_tentative_parse (parser);
34789 cp_parser_parse_tentatively (parser);
34790 decl = cp_parser_primary_expression (parser, false, false,
34791 false, &idk);
34793 if (!cp_parser_error_occurred (parser)
34794 && decl
34795 && DECL_P (decl)
34796 && CLASS_TYPE_P (TREE_TYPE (decl)))
34798 tree rhs;
34800 cp_parser_parse_definitely (parser);
34801 cp_parser_require (parser, CPP_EQ, RT_EQ);
34802 rhs = cp_parser_assignment_expression (parser);
34803 orig_init = rhs;
34804 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34805 decl, NOP_EXPR,
34806 rhs,
34807 tf_warning_or_error));
34808 if (!add_private_clause)
34809 add_private_clause = decl;
34811 else
34813 decl = NULL;
34814 cp_parser_abort_tentative_parse (parser);
34815 init = cp_parser_expression (parser);
34816 if (init)
34818 if (TREE_CODE (init) == MODIFY_EXPR
34819 || TREE_CODE (init) == MODOP_EXPR)
34820 real_decl = TREE_OPERAND (init, 0);
34824 return add_private_clause;
34827 /* Parse the restricted form of the for statement allowed by OpenMP. */
34829 static tree
34830 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34831 tree *cclauses, bool *if_p)
34833 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34834 tree real_decl, initv, condv, incrv, declv;
34835 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34836 location_t loc_first;
34837 bool collapse_err = false;
34838 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34839 vec<tree, va_gc> *for_block = make_tree_vector ();
34840 auto_vec<tree, 4> orig_inits;
34841 bool tiling = false;
34843 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34844 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34845 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34846 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34848 tiling = true;
34849 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34851 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34852 && OMP_CLAUSE_ORDERED_EXPR (cl))
34854 ordered_cl = cl;
34855 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34858 if (ordered && ordered < collapse)
34860 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34861 "%<ordered%> clause parameter is less than %<collapse%>");
34862 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34863 = build_int_cst (NULL_TREE, collapse);
34864 ordered = collapse;
34866 if (ordered)
34868 for (tree *pc = &clauses; *pc; )
34869 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34871 error_at (OMP_CLAUSE_LOCATION (*pc),
34872 "%<linear%> clause may not be specified together "
34873 "with %<ordered%> clause with a parameter");
34874 *pc = OMP_CLAUSE_CHAIN (*pc);
34876 else
34877 pc = &OMP_CLAUSE_CHAIN (*pc);
34880 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34881 count = ordered ? ordered : collapse;
34883 declv = make_tree_vec (count);
34884 initv = make_tree_vec (count);
34885 condv = make_tree_vec (count);
34886 incrv = make_tree_vec (count);
34888 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34890 for (i = 0; i < count; i++)
34892 int bracecount = 0;
34893 tree add_private_clause = NULL_TREE;
34894 location_t loc;
34896 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34898 if (!collapse_err)
34899 cp_parser_error (parser, "for statement expected");
34900 return NULL;
34902 loc = cp_lexer_consume_token (parser->lexer)->location;
34904 matching_parens parens;
34905 if (!parens.require_open (parser))
34906 return NULL;
34908 init = orig_init = decl = real_decl = NULL;
34909 this_pre_body = push_stmt_list ();
34911 add_private_clause
34912 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34913 init, orig_init, decl, real_decl);
34915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34916 if (this_pre_body)
34918 this_pre_body = pop_stmt_list (this_pre_body);
34919 if (pre_body)
34921 tree t = pre_body;
34922 pre_body = push_stmt_list ();
34923 add_stmt (t);
34924 add_stmt (this_pre_body);
34925 pre_body = pop_stmt_list (pre_body);
34927 else
34928 pre_body = this_pre_body;
34931 if (decl)
34932 real_decl = decl;
34933 if (cclauses != NULL
34934 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34935 && real_decl != NULL_TREE)
34937 tree *c;
34938 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34939 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34940 && OMP_CLAUSE_DECL (*c) == real_decl)
34942 error_at (loc, "iteration variable %qD"
34943 " should not be firstprivate", real_decl);
34944 *c = OMP_CLAUSE_CHAIN (*c);
34946 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34947 && OMP_CLAUSE_DECL (*c) == real_decl)
34949 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34950 tree l = *c;
34951 *c = OMP_CLAUSE_CHAIN (*c);
34952 if (code == OMP_SIMD)
34954 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34955 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34957 else
34959 OMP_CLAUSE_CHAIN (l) = clauses;
34960 clauses = l;
34962 add_private_clause = NULL_TREE;
34964 else
34966 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34967 && OMP_CLAUSE_DECL (*c) == real_decl)
34968 add_private_clause = NULL_TREE;
34969 c = &OMP_CLAUSE_CHAIN (*c);
34973 if (add_private_clause)
34975 tree c;
34976 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34978 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34979 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34980 && OMP_CLAUSE_DECL (c) == decl)
34981 break;
34982 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34983 && OMP_CLAUSE_DECL (c) == decl)
34984 error_at (loc, "iteration variable %qD "
34985 "should not be firstprivate",
34986 decl);
34987 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34988 && OMP_CLAUSE_DECL (c) == decl)
34989 error_at (loc, "iteration variable %qD should not be reduction",
34990 decl);
34992 if (c == NULL)
34994 if (code != OMP_SIMD)
34995 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34996 else if (collapse == 1)
34997 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34998 else
34999 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35000 OMP_CLAUSE_DECL (c) = add_private_clause;
35001 c = finish_omp_clauses (c, C_ORT_OMP);
35002 if (c)
35004 OMP_CLAUSE_CHAIN (c) = clauses;
35005 clauses = c;
35006 /* For linear, signal that we need to fill up
35007 the so far unknown linear step. */
35008 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35009 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35014 cond = NULL;
35015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35016 cond = cp_parser_omp_for_cond (parser, decl);
35017 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35019 incr = NULL;
35020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35022 /* If decl is an iterator, preserve the operator on decl
35023 until finish_omp_for. */
35024 if (real_decl
35025 && ((processing_template_decl
35026 && (TREE_TYPE (real_decl) == NULL_TREE
35027 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35028 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35029 incr = cp_parser_omp_for_incr (parser, real_decl);
35030 else
35031 incr = cp_parser_expression (parser);
35032 if (!EXPR_HAS_LOCATION (incr))
35033 protected_set_expr_location (incr, input_location);
35036 if (!parens.require_close (parser))
35037 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35038 /*or_comma=*/false,
35039 /*consume_paren=*/true);
35041 TREE_VEC_ELT (declv, i) = decl;
35042 TREE_VEC_ELT (initv, i) = init;
35043 TREE_VEC_ELT (condv, i) = cond;
35044 TREE_VEC_ELT (incrv, i) = incr;
35045 if (orig_init)
35047 orig_inits.safe_grow_cleared (i + 1);
35048 orig_inits[i] = orig_init;
35051 if (i == count - 1)
35052 break;
35054 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35055 in between the collapsed for loops to be still considered perfectly
35056 nested. Hopefully the final version clarifies this.
35057 For now handle (multiple) {'s and empty statements. */
35058 cp_parser_parse_tentatively (parser);
35059 for (;;)
35061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35062 break;
35063 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35065 cp_lexer_consume_token (parser->lexer);
35066 bracecount++;
35068 else if (bracecount
35069 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35070 cp_lexer_consume_token (parser->lexer);
35071 else
35073 loc = cp_lexer_peek_token (parser->lexer)->location;
35074 error_at (loc, "not enough for loops to collapse");
35075 collapse_err = true;
35076 cp_parser_abort_tentative_parse (parser);
35077 declv = NULL_TREE;
35078 break;
35082 if (declv)
35084 cp_parser_parse_definitely (parser);
35085 nbraces += bracecount;
35089 if (nbraces)
35090 if_p = NULL;
35092 /* Note that we saved the original contents of this flag when we entered
35093 the structured block, and so we don't need to re-save it here. */
35094 parser->in_statement = IN_OMP_FOR;
35096 /* Note that the grammar doesn't call for a structured block here,
35097 though the loop as a whole is a structured block. */
35098 body = push_stmt_list ();
35099 cp_parser_statement (parser, NULL_TREE, false, if_p);
35100 body = pop_stmt_list (body);
35102 if (declv == NULL_TREE)
35103 ret = NULL_TREE;
35104 else
35105 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35106 body, pre_body, &orig_inits, clauses);
35108 while (nbraces)
35110 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35112 cp_lexer_consume_token (parser->lexer);
35113 nbraces--;
35115 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35116 cp_lexer_consume_token (parser->lexer);
35117 else
35119 if (!collapse_err)
35121 error_at (cp_lexer_peek_token (parser->lexer)->location,
35122 "collapsed loops not perfectly nested");
35124 collapse_err = true;
35125 cp_parser_statement_seq_opt (parser, NULL);
35126 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35127 break;
35131 while (!for_block->is_empty ())
35133 tree t = for_block->pop ();
35134 if (TREE_CODE (t) == STATEMENT_LIST)
35135 add_stmt (pop_stmt_list (t));
35136 else
35137 add_stmt (t);
35139 release_tree_vector (for_block);
35141 return ret;
35144 /* Helper function for OpenMP parsing, split clauses and call
35145 finish_omp_clauses on each of the set of clauses afterwards. */
35147 static void
35148 cp_omp_split_clauses (location_t loc, enum tree_code code,
35149 omp_clause_mask mask, tree clauses, tree *cclauses)
35151 int i;
35152 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35153 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35154 if (cclauses[i])
35155 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35158 /* OpenMP 4.0:
35159 #pragma omp simd simd-clause[optseq] new-line
35160 for-loop */
35162 #define OMP_SIMD_CLAUSE_MASK \
35163 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35172 static tree
35173 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35174 char *p_name, omp_clause_mask mask, tree *cclauses,
35175 bool *if_p)
35177 tree clauses, sb, ret;
35178 unsigned int save;
35179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35181 strcat (p_name, " simd");
35182 mask |= OMP_SIMD_CLAUSE_MASK;
35184 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35185 cclauses == NULL);
35186 if (cclauses)
35188 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35189 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35190 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35191 OMP_CLAUSE_ORDERED);
35192 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35194 error_at (OMP_CLAUSE_LOCATION (c),
35195 "%<ordered%> clause with parameter may not be specified "
35196 "on %qs construct", p_name);
35197 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35201 sb = begin_omp_structured_block ();
35202 save = cp_parser_begin_omp_structured_block (parser);
35204 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35206 cp_parser_end_omp_structured_block (parser, save);
35207 add_stmt (finish_omp_structured_block (sb));
35209 return ret;
35212 /* OpenMP 2.5:
35213 #pragma omp for for-clause[optseq] new-line
35214 for-loop
35216 OpenMP 4.0:
35217 #pragma omp for simd for-simd-clause[optseq] new-line
35218 for-loop */
35220 #define OMP_FOR_CLAUSE_MASK \
35221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35231 static tree
35232 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35233 char *p_name, omp_clause_mask mask, tree *cclauses,
35234 bool *if_p)
35236 tree clauses, sb, ret;
35237 unsigned int save;
35238 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35240 strcat (p_name, " for");
35241 mask |= OMP_FOR_CLAUSE_MASK;
35242 /* parallel for{, simd} disallows nowait clause, but for
35243 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35244 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35245 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35246 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35247 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35248 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35250 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35252 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35253 const char *p = IDENTIFIER_POINTER (id);
35255 if (strcmp (p, "simd") == 0)
35257 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35258 if (cclauses == NULL)
35259 cclauses = cclauses_buf;
35261 cp_lexer_consume_token (parser->lexer);
35262 if (!flag_openmp) /* flag_openmp_simd */
35263 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35264 cclauses, if_p);
35265 sb = begin_omp_structured_block ();
35266 save = cp_parser_begin_omp_structured_block (parser);
35267 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35268 cclauses, if_p);
35269 cp_parser_end_omp_structured_block (parser, save);
35270 tree body = finish_omp_structured_block (sb);
35271 if (ret == NULL)
35272 return ret;
35273 ret = make_node (OMP_FOR);
35274 TREE_TYPE (ret) = void_type_node;
35275 OMP_FOR_BODY (ret) = body;
35276 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35277 SET_EXPR_LOCATION (ret, loc);
35278 add_stmt (ret);
35279 return ret;
35282 if (!flag_openmp) /* flag_openmp_simd */
35284 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35285 return NULL_TREE;
35288 /* Composite distribute parallel for disallows linear clause. */
35289 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35290 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35292 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35293 cclauses == NULL);
35294 if (cclauses)
35296 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35297 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35300 sb = begin_omp_structured_block ();
35301 save = cp_parser_begin_omp_structured_block (parser);
35303 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35305 cp_parser_end_omp_structured_block (parser, save);
35306 add_stmt (finish_omp_structured_block (sb));
35308 return ret;
35311 /* OpenMP 2.5:
35312 # pragma omp master new-line
35313 structured-block */
35315 static tree
35316 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35318 cp_parser_require_pragma_eol (parser, pragma_tok);
35319 return c_finish_omp_master (input_location,
35320 cp_parser_omp_structured_block (parser, if_p));
35323 /* OpenMP 2.5:
35324 # pragma omp ordered new-line
35325 structured-block
35327 OpenMP 4.5:
35328 # pragma omp ordered ordered-clauses new-line
35329 structured-block */
35331 #define OMP_ORDERED_CLAUSE_MASK \
35332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35335 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35336 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35338 static bool
35339 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35340 enum pragma_context context, bool *if_p)
35342 location_t loc = pragma_tok->location;
35344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35347 const char *p = IDENTIFIER_POINTER (id);
35349 if (strcmp (p, "depend") == 0)
35351 if (!flag_openmp) /* flag_openmp_simd */
35353 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35354 return false;
35356 if (context == pragma_stmt)
35358 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35359 "%<depend%> clause may only be used in compound "
35360 "statements");
35361 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35362 return false;
35364 tree clauses
35365 = cp_parser_omp_all_clauses (parser,
35366 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35367 "#pragma omp ordered", pragma_tok);
35368 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35369 return false;
35373 tree clauses
35374 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35375 "#pragma omp ordered", pragma_tok);
35377 if (!flag_openmp /* flag_openmp_simd */
35378 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35379 return false;
35381 c_finish_omp_ordered (loc, clauses,
35382 cp_parser_omp_structured_block (parser, if_p));
35383 return true;
35386 /* OpenMP 2.5:
35388 section-scope:
35389 { section-sequence }
35391 section-sequence:
35392 section-directive[opt] structured-block
35393 section-sequence section-directive structured-block */
35395 static tree
35396 cp_parser_omp_sections_scope (cp_parser *parser)
35398 tree stmt, substmt;
35399 bool error_suppress = false;
35400 cp_token *tok;
35402 matching_braces braces;
35403 if (!braces.require_open (parser))
35404 return NULL_TREE;
35406 stmt = push_stmt_list ();
35408 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35409 != PRAGMA_OMP_SECTION)
35411 substmt = cp_parser_omp_structured_block (parser, NULL);
35412 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35413 add_stmt (substmt);
35416 while (1)
35418 tok = cp_lexer_peek_token (parser->lexer);
35419 if (tok->type == CPP_CLOSE_BRACE)
35420 break;
35421 if (tok->type == CPP_EOF)
35422 break;
35424 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35426 cp_lexer_consume_token (parser->lexer);
35427 cp_parser_require_pragma_eol (parser, tok);
35428 error_suppress = false;
35430 else if (!error_suppress)
35432 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35433 error_suppress = true;
35436 substmt = cp_parser_omp_structured_block (parser, NULL);
35437 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35438 add_stmt (substmt);
35440 braces.require_close (parser);
35442 substmt = pop_stmt_list (stmt);
35444 stmt = make_node (OMP_SECTIONS);
35445 TREE_TYPE (stmt) = void_type_node;
35446 OMP_SECTIONS_BODY (stmt) = substmt;
35448 add_stmt (stmt);
35449 return stmt;
35452 /* OpenMP 2.5:
35453 # pragma omp sections sections-clause[optseq] newline
35454 sections-scope */
35456 #define OMP_SECTIONS_CLAUSE_MASK \
35457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35463 static tree
35464 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35465 char *p_name, omp_clause_mask mask, tree *cclauses)
35467 tree clauses, ret;
35468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35470 strcat (p_name, " sections");
35471 mask |= OMP_SECTIONS_CLAUSE_MASK;
35472 if (cclauses)
35473 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35475 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35476 cclauses == NULL);
35477 if (cclauses)
35479 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35480 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35483 ret = cp_parser_omp_sections_scope (parser);
35484 if (ret)
35485 OMP_SECTIONS_CLAUSES (ret) = clauses;
35487 return ret;
35490 /* OpenMP 2.5:
35491 # pragma omp parallel parallel-clause[optseq] new-line
35492 structured-block
35493 # pragma omp parallel for parallel-for-clause[optseq] new-line
35494 structured-block
35495 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35496 structured-block
35498 OpenMP 4.0:
35499 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35500 structured-block */
35502 #define OMP_PARALLEL_CLAUSE_MASK \
35503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35513 static tree
35514 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35515 char *p_name, omp_clause_mask mask, tree *cclauses,
35516 bool *if_p)
35518 tree stmt, clauses, block;
35519 unsigned int save;
35520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35522 strcat (p_name, " parallel");
35523 mask |= OMP_PARALLEL_CLAUSE_MASK;
35524 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35525 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35526 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35527 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35531 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35532 if (cclauses == NULL)
35533 cclauses = cclauses_buf;
35535 cp_lexer_consume_token (parser->lexer);
35536 if (!flag_openmp) /* flag_openmp_simd */
35537 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35538 if_p);
35539 block = begin_omp_parallel ();
35540 save = cp_parser_begin_omp_structured_block (parser);
35541 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35542 if_p);
35543 cp_parser_end_omp_structured_block (parser, save);
35544 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35545 block);
35546 if (ret == NULL_TREE)
35547 return ret;
35548 OMP_PARALLEL_COMBINED (stmt) = 1;
35549 return stmt;
35551 /* When combined with distribute, parallel has to be followed by for.
35552 #pragma omp target parallel is allowed though. */
35553 else if (cclauses
35554 && (mask & (OMP_CLAUSE_MASK_1
35555 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35557 error_at (loc, "expected %<for%> after %qs", p_name);
35558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35559 return NULL_TREE;
35561 else if (!flag_openmp) /* flag_openmp_simd */
35563 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35564 return NULL_TREE;
35566 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35569 const char *p = IDENTIFIER_POINTER (id);
35570 if (strcmp (p, "sections") == 0)
35572 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35573 cclauses = cclauses_buf;
35575 cp_lexer_consume_token (parser->lexer);
35576 block = begin_omp_parallel ();
35577 save = cp_parser_begin_omp_structured_block (parser);
35578 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35579 cp_parser_end_omp_structured_block (parser, save);
35580 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35581 block);
35582 OMP_PARALLEL_COMBINED (stmt) = 1;
35583 return stmt;
35587 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35588 cclauses == NULL);
35589 if (cclauses)
35591 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35592 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35595 block = begin_omp_parallel ();
35596 save = cp_parser_begin_omp_structured_block (parser);
35597 cp_parser_statement (parser, NULL_TREE, false, if_p);
35598 cp_parser_end_omp_structured_block (parser, save);
35599 stmt = finish_omp_parallel (clauses, block);
35600 return stmt;
35603 /* OpenMP 2.5:
35604 # pragma omp single single-clause[optseq] new-line
35605 structured-block */
35607 #define OMP_SINGLE_CLAUSE_MASK \
35608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35613 static tree
35614 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35616 tree stmt = make_node (OMP_SINGLE);
35617 TREE_TYPE (stmt) = void_type_node;
35619 OMP_SINGLE_CLAUSES (stmt)
35620 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35621 "#pragma omp single", pragma_tok);
35622 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35624 return add_stmt (stmt);
35627 /* OpenMP 3.0:
35628 # pragma omp task task-clause[optseq] new-line
35629 structured-block */
35631 #define OMP_TASK_CLAUSE_MASK \
35632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35643 static tree
35644 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35646 tree clauses, block;
35647 unsigned int save;
35649 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35650 "#pragma omp task", pragma_tok);
35651 block = begin_omp_task ();
35652 save = cp_parser_begin_omp_structured_block (parser);
35653 cp_parser_statement (parser, NULL_TREE, false, if_p);
35654 cp_parser_end_omp_structured_block (parser, save);
35655 return finish_omp_task (clauses, block);
35658 /* OpenMP 3.0:
35659 # pragma omp taskwait new-line */
35661 static void
35662 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35664 cp_parser_require_pragma_eol (parser, pragma_tok);
35665 finish_omp_taskwait ();
35668 /* OpenMP 3.1:
35669 # pragma omp taskyield new-line */
35671 static void
35672 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35674 cp_parser_require_pragma_eol (parser, pragma_tok);
35675 finish_omp_taskyield ();
35678 /* OpenMP 4.0:
35679 # pragma omp taskgroup new-line
35680 structured-block */
35682 static tree
35683 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35685 cp_parser_require_pragma_eol (parser, pragma_tok);
35686 return c_finish_omp_taskgroup (input_location,
35687 cp_parser_omp_structured_block (parser,
35688 if_p));
35692 /* OpenMP 2.5:
35693 # pragma omp threadprivate (variable-list) */
35695 static void
35696 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35698 tree vars;
35700 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35701 cp_parser_require_pragma_eol (parser, pragma_tok);
35703 finish_omp_threadprivate (vars);
35706 /* OpenMP 4.0:
35707 # pragma omp cancel cancel-clause[optseq] new-line */
35709 #define OMP_CANCEL_CLAUSE_MASK \
35710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35716 static void
35717 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35719 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35720 "#pragma omp cancel", pragma_tok);
35721 finish_omp_cancel (clauses);
35724 /* OpenMP 4.0:
35725 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35727 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35733 static void
35734 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35735 enum pragma_context context)
35737 tree clauses;
35738 bool point_seen = false;
35740 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35743 const char *p = IDENTIFIER_POINTER (id);
35745 if (strcmp (p, "point") == 0)
35747 cp_lexer_consume_token (parser->lexer);
35748 point_seen = true;
35751 if (!point_seen)
35753 cp_parser_error (parser, "expected %<point%>");
35754 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35755 return;
35758 if (context != pragma_compound)
35760 if (context == pragma_stmt)
35761 error_at (pragma_tok->location,
35762 "%<#pragma %s%> may only be used in compound statements",
35763 "omp cancellation point");
35764 else
35765 cp_parser_error (parser, "expected declaration specifiers");
35766 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35767 return;
35770 clauses = cp_parser_omp_all_clauses (parser,
35771 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35772 "#pragma omp cancellation point",
35773 pragma_tok);
35774 finish_omp_cancellation_point (clauses);
35777 /* OpenMP 4.0:
35778 #pragma omp distribute distribute-clause[optseq] new-line
35779 for-loop */
35781 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35788 static tree
35789 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35790 char *p_name, omp_clause_mask mask, tree *cclauses,
35791 bool *if_p)
35793 tree clauses, sb, ret;
35794 unsigned int save;
35795 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35797 strcat (p_name, " distribute");
35798 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35802 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35803 const char *p = IDENTIFIER_POINTER (id);
35804 bool simd = false;
35805 bool parallel = false;
35807 if (strcmp (p, "simd") == 0)
35808 simd = true;
35809 else
35810 parallel = strcmp (p, "parallel") == 0;
35811 if (parallel || simd)
35813 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35814 if (cclauses == NULL)
35815 cclauses = cclauses_buf;
35816 cp_lexer_consume_token (parser->lexer);
35817 if (!flag_openmp) /* flag_openmp_simd */
35819 if (simd)
35820 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35821 cclauses, if_p);
35822 else
35823 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35824 cclauses, if_p);
35826 sb = begin_omp_structured_block ();
35827 save = cp_parser_begin_omp_structured_block (parser);
35828 if (simd)
35829 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35830 cclauses, if_p);
35831 else
35832 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35833 cclauses, if_p);
35834 cp_parser_end_omp_structured_block (parser, save);
35835 tree body = finish_omp_structured_block (sb);
35836 if (ret == NULL)
35837 return ret;
35838 ret = make_node (OMP_DISTRIBUTE);
35839 TREE_TYPE (ret) = void_type_node;
35840 OMP_FOR_BODY (ret) = body;
35841 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35842 SET_EXPR_LOCATION (ret, loc);
35843 add_stmt (ret);
35844 return ret;
35847 if (!flag_openmp) /* flag_openmp_simd */
35849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35850 return NULL_TREE;
35853 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35854 cclauses == NULL);
35855 if (cclauses)
35857 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35858 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35861 sb = begin_omp_structured_block ();
35862 save = cp_parser_begin_omp_structured_block (parser);
35864 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35866 cp_parser_end_omp_structured_block (parser, save);
35867 add_stmt (finish_omp_structured_block (sb));
35869 return ret;
35872 /* OpenMP 4.0:
35873 # pragma omp teams teams-clause[optseq] new-line
35874 structured-block */
35876 #define OMP_TEAMS_CLAUSE_MASK \
35877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35885 static tree
35886 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35887 char *p_name, omp_clause_mask mask, tree *cclauses,
35888 bool *if_p)
35890 tree clauses, sb, ret;
35891 unsigned int save;
35892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35894 strcat (p_name, " teams");
35895 mask |= OMP_TEAMS_CLAUSE_MASK;
35897 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35899 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35900 const char *p = IDENTIFIER_POINTER (id);
35901 if (strcmp (p, "distribute") == 0)
35903 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35904 if (cclauses == NULL)
35905 cclauses = cclauses_buf;
35907 cp_lexer_consume_token (parser->lexer);
35908 if (!flag_openmp) /* flag_openmp_simd */
35909 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35910 cclauses, if_p);
35911 sb = begin_omp_structured_block ();
35912 save = cp_parser_begin_omp_structured_block (parser);
35913 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35914 cclauses, if_p);
35915 cp_parser_end_omp_structured_block (parser, save);
35916 tree body = finish_omp_structured_block (sb);
35917 if (ret == NULL)
35918 return ret;
35919 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35920 ret = make_node (OMP_TEAMS);
35921 TREE_TYPE (ret) = void_type_node;
35922 OMP_TEAMS_CLAUSES (ret) = clauses;
35923 OMP_TEAMS_BODY (ret) = body;
35924 OMP_TEAMS_COMBINED (ret) = 1;
35925 SET_EXPR_LOCATION (ret, loc);
35926 return add_stmt (ret);
35929 if (!flag_openmp) /* flag_openmp_simd */
35931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35932 return NULL_TREE;
35935 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35936 cclauses == NULL);
35937 if (cclauses)
35939 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35940 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35943 tree stmt = make_node (OMP_TEAMS);
35944 TREE_TYPE (stmt) = void_type_node;
35945 OMP_TEAMS_CLAUSES (stmt) = clauses;
35946 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35947 SET_EXPR_LOCATION (stmt, loc);
35949 return add_stmt (stmt);
35952 /* OpenMP 4.0:
35953 # pragma omp target data target-data-clause[optseq] new-line
35954 structured-block */
35956 #define OMP_TARGET_DATA_CLAUSE_MASK \
35957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35962 static tree
35963 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35965 tree clauses
35966 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35967 "#pragma omp target data", pragma_tok);
35968 int map_seen = 0;
35969 for (tree *pc = &clauses; *pc;)
35971 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35972 switch (OMP_CLAUSE_MAP_KIND (*pc))
35974 case GOMP_MAP_TO:
35975 case GOMP_MAP_ALWAYS_TO:
35976 case GOMP_MAP_FROM:
35977 case GOMP_MAP_ALWAYS_FROM:
35978 case GOMP_MAP_TOFROM:
35979 case GOMP_MAP_ALWAYS_TOFROM:
35980 case GOMP_MAP_ALLOC:
35981 map_seen = 3;
35982 break;
35983 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35984 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35985 case GOMP_MAP_ALWAYS_POINTER:
35986 break;
35987 default:
35988 map_seen |= 1;
35989 error_at (OMP_CLAUSE_LOCATION (*pc),
35990 "%<#pragma omp target data%> with map-type other "
35991 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35992 "on %<map%> clause");
35993 *pc = OMP_CLAUSE_CHAIN (*pc);
35994 continue;
35996 pc = &OMP_CLAUSE_CHAIN (*pc);
35999 if (map_seen != 3)
36001 if (map_seen == 0)
36002 error_at (pragma_tok->location,
36003 "%<#pragma omp target data%> must contain at least "
36004 "one %<map%> clause");
36005 return NULL_TREE;
36008 tree stmt = make_node (OMP_TARGET_DATA);
36009 TREE_TYPE (stmt) = void_type_node;
36010 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36012 keep_next_level (true);
36013 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36015 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36016 return add_stmt (stmt);
36019 /* OpenMP 4.5:
36020 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36021 structured-block */
36023 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36030 static tree
36031 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36032 enum pragma_context context)
36034 bool data_seen = false;
36035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36037 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36038 const char *p = IDENTIFIER_POINTER (id);
36040 if (strcmp (p, "data") == 0)
36042 cp_lexer_consume_token (parser->lexer);
36043 data_seen = true;
36046 if (!data_seen)
36048 cp_parser_error (parser, "expected %<data%>");
36049 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36050 return NULL_TREE;
36053 if (context == pragma_stmt)
36055 error_at (pragma_tok->location,
36056 "%<#pragma %s%> may only be used in compound statements",
36057 "omp target enter data");
36058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36059 return NULL_TREE;
36062 tree clauses
36063 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36064 "#pragma omp target enter data", pragma_tok);
36065 int map_seen = 0;
36066 for (tree *pc = &clauses; *pc;)
36068 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36069 switch (OMP_CLAUSE_MAP_KIND (*pc))
36071 case GOMP_MAP_TO:
36072 case GOMP_MAP_ALWAYS_TO:
36073 case GOMP_MAP_ALLOC:
36074 map_seen = 3;
36075 break;
36076 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36078 case GOMP_MAP_ALWAYS_POINTER:
36079 break;
36080 default:
36081 map_seen |= 1;
36082 error_at (OMP_CLAUSE_LOCATION (*pc),
36083 "%<#pragma omp target enter data%> with map-type other "
36084 "than %<to%> or %<alloc%> on %<map%> clause");
36085 *pc = OMP_CLAUSE_CHAIN (*pc);
36086 continue;
36088 pc = &OMP_CLAUSE_CHAIN (*pc);
36091 if (map_seen != 3)
36093 if (map_seen == 0)
36094 error_at (pragma_tok->location,
36095 "%<#pragma omp target enter data%> must contain at least "
36096 "one %<map%> clause");
36097 return NULL_TREE;
36100 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36101 TREE_TYPE (stmt) = void_type_node;
36102 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36103 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36104 return add_stmt (stmt);
36107 /* OpenMP 4.5:
36108 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36109 structured-block */
36111 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36118 static tree
36119 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36120 enum pragma_context context)
36122 bool data_seen = false;
36123 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36125 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36126 const char *p = IDENTIFIER_POINTER (id);
36128 if (strcmp (p, "data") == 0)
36130 cp_lexer_consume_token (parser->lexer);
36131 data_seen = true;
36134 if (!data_seen)
36136 cp_parser_error (parser, "expected %<data%>");
36137 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36138 return NULL_TREE;
36141 if (context == pragma_stmt)
36143 error_at (pragma_tok->location,
36144 "%<#pragma %s%> may only be used in compound statements",
36145 "omp target exit data");
36146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36147 return NULL_TREE;
36150 tree clauses
36151 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36152 "#pragma omp target exit data", pragma_tok);
36153 int map_seen = 0;
36154 for (tree *pc = &clauses; *pc;)
36156 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36157 switch (OMP_CLAUSE_MAP_KIND (*pc))
36159 case GOMP_MAP_FROM:
36160 case GOMP_MAP_ALWAYS_FROM:
36161 case GOMP_MAP_RELEASE:
36162 case GOMP_MAP_DELETE:
36163 map_seen = 3;
36164 break;
36165 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36166 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36167 case GOMP_MAP_ALWAYS_POINTER:
36168 break;
36169 default:
36170 map_seen |= 1;
36171 error_at (OMP_CLAUSE_LOCATION (*pc),
36172 "%<#pragma omp target exit data%> with map-type other "
36173 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36174 " clause");
36175 *pc = OMP_CLAUSE_CHAIN (*pc);
36176 continue;
36178 pc = &OMP_CLAUSE_CHAIN (*pc);
36181 if (map_seen != 3)
36183 if (map_seen == 0)
36184 error_at (pragma_tok->location,
36185 "%<#pragma omp target exit data%> must contain at least "
36186 "one %<map%> clause");
36187 return NULL_TREE;
36190 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36191 TREE_TYPE (stmt) = void_type_node;
36192 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36193 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36194 return add_stmt (stmt);
36197 /* OpenMP 4.0:
36198 # pragma omp target update target-update-clause[optseq] new-line */
36200 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36208 static bool
36209 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36210 enum pragma_context context)
36212 if (context == pragma_stmt)
36214 error_at (pragma_tok->location,
36215 "%<#pragma %s%> may only be used in compound statements",
36216 "omp target update");
36217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36218 return false;
36221 tree clauses
36222 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36223 "#pragma omp target update", pragma_tok);
36224 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36225 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36227 error_at (pragma_tok->location,
36228 "%<#pragma omp target update%> must contain at least one "
36229 "%<from%> or %<to%> clauses");
36230 return false;
36233 tree stmt = make_node (OMP_TARGET_UPDATE);
36234 TREE_TYPE (stmt) = void_type_node;
36235 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36236 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36237 add_stmt (stmt);
36238 return false;
36241 /* OpenMP 4.0:
36242 # pragma omp target target-clause[optseq] new-line
36243 structured-block */
36245 #define OMP_TARGET_CLAUSE_MASK \
36246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36256 static bool
36257 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36258 enum pragma_context context, bool *if_p)
36260 tree *pc = NULL, stmt;
36262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36264 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36265 const char *p = IDENTIFIER_POINTER (id);
36266 enum tree_code ccode = ERROR_MARK;
36268 if (strcmp (p, "teams") == 0)
36269 ccode = OMP_TEAMS;
36270 else if (strcmp (p, "parallel") == 0)
36271 ccode = OMP_PARALLEL;
36272 else if (strcmp (p, "simd") == 0)
36273 ccode = OMP_SIMD;
36274 if (ccode != ERROR_MARK)
36276 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36277 char p_name[sizeof ("#pragma omp target teams distribute "
36278 "parallel for simd")];
36280 cp_lexer_consume_token (parser->lexer);
36281 strcpy (p_name, "#pragma omp target");
36282 if (!flag_openmp) /* flag_openmp_simd */
36284 tree stmt;
36285 switch (ccode)
36287 case OMP_TEAMS:
36288 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36289 OMP_TARGET_CLAUSE_MASK,
36290 cclauses, if_p);
36291 break;
36292 case OMP_PARALLEL:
36293 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36294 OMP_TARGET_CLAUSE_MASK,
36295 cclauses, if_p);
36296 break;
36297 case OMP_SIMD:
36298 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36299 OMP_TARGET_CLAUSE_MASK,
36300 cclauses, if_p);
36301 break;
36302 default:
36303 gcc_unreachable ();
36305 return stmt != NULL_TREE;
36307 keep_next_level (true);
36308 tree sb = begin_omp_structured_block (), ret;
36309 unsigned save = cp_parser_begin_omp_structured_block (parser);
36310 switch (ccode)
36312 case OMP_TEAMS:
36313 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36314 OMP_TARGET_CLAUSE_MASK, cclauses,
36315 if_p);
36316 break;
36317 case OMP_PARALLEL:
36318 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36319 OMP_TARGET_CLAUSE_MASK, cclauses,
36320 if_p);
36321 break;
36322 case OMP_SIMD:
36323 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36324 OMP_TARGET_CLAUSE_MASK, cclauses,
36325 if_p);
36326 break;
36327 default:
36328 gcc_unreachable ();
36330 cp_parser_end_omp_structured_block (parser, save);
36331 tree body = finish_omp_structured_block (sb);
36332 if (ret == NULL_TREE)
36333 return false;
36334 if (ccode == OMP_TEAMS && !processing_template_decl)
36336 /* For combined target teams, ensure the num_teams and
36337 thread_limit clause expressions are evaluated on the host,
36338 before entering the target construct. */
36339 tree c;
36340 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36341 c; c = OMP_CLAUSE_CHAIN (c))
36342 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36343 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36344 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36346 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36347 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36348 if (expr == error_mark_node)
36349 continue;
36350 tree tmp = TARGET_EXPR_SLOT (expr);
36351 add_stmt (expr);
36352 OMP_CLAUSE_OPERAND (c, 0) = expr;
36353 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36354 OMP_CLAUSE_FIRSTPRIVATE);
36355 OMP_CLAUSE_DECL (tc) = tmp;
36356 OMP_CLAUSE_CHAIN (tc)
36357 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36358 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36361 tree stmt = make_node (OMP_TARGET);
36362 TREE_TYPE (stmt) = void_type_node;
36363 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36364 OMP_TARGET_BODY (stmt) = body;
36365 OMP_TARGET_COMBINED (stmt) = 1;
36366 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36367 add_stmt (stmt);
36368 pc = &OMP_TARGET_CLAUSES (stmt);
36369 goto check_clauses;
36371 else if (!flag_openmp) /* flag_openmp_simd */
36373 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36374 return false;
36376 else if (strcmp (p, "data") == 0)
36378 cp_lexer_consume_token (parser->lexer);
36379 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36380 return true;
36382 else if (strcmp (p, "enter") == 0)
36384 cp_lexer_consume_token (parser->lexer);
36385 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36386 return false;
36388 else if (strcmp (p, "exit") == 0)
36390 cp_lexer_consume_token (parser->lexer);
36391 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36392 return false;
36394 else if (strcmp (p, "update") == 0)
36396 cp_lexer_consume_token (parser->lexer);
36397 return cp_parser_omp_target_update (parser, pragma_tok, context);
36400 if (!flag_openmp) /* flag_openmp_simd */
36402 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36403 return false;
36406 stmt = make_node (OMP_TARGET);
36407 TREE_TYPE (stmt) = void_type_node;
36409 OMP_TARGET_CLAUSES (stmt)
36410 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36411 "#pragma omp target", pragma_tok);
36412 pc = &OMP_TARGET_CLAUSES (stmt);
36413 keep_next_level (true);
36414 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36416 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36417 add_stmt (stmt);
36419 check_clauses:
36420 while (*pc)
36422 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36423 switch (OMP_CLAUSE_MAP_KIND (*pc))
36425 case GOMP_MAP_TO:
36426 case GOMP_MAP_ALWAYS_TO:
36427 case GOMP_MAP_FROM:
36428 case GOMP_MAP_ALWAYS_FROM:
36429 case GOMP_MAP_TOFROM:
36430 case GOMP_MAP_ALWAYS_TOFROM:
36431 case GOMP_MAP_ALLOC:
36432 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36433 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36434 case GOMP_MAP_ALWAYS_POINTER:
36435 break;
36436 default:
36437 error_at (OMP_CLAUSE_LOCATION (*pc),
36438 "%<#pragma omp target%> with map-type other "
36439 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36440 "on %<map%> clause");
36441 *pc = OMP_CLAUSE_CHAIN (*pc);
36442 continue;
36444 pc = &OMP_CLAUSE_CHAIN (*pc);
36446 return true;
36449 /* OpenACC 2.0:
36450 # pragma acc cache (variable-list) new-line
36453 static tree
36454 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36456 tree stmt, clauses;
36458 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36459 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36461 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36463 stmt = make_node (OACC_CACHE);
36464 TREE_TYPE (stmt) = void_type_node;
36465 OACC_CACHE_CLAUSES (stmt) = clauses;
36466 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36467 add_stmt (stmt);
36469 return stmt;
36472 /* OpenACC 2.0:
36473 # pragma acc data oacc-data-clause[optseq] new-line
36474 structured-block */
36476 #define OACC_DATA_CLAUSE_MASK \
36477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36489 static tree
36490 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36492 tree stmt, clauses, block;
36493 unsigned int save;
36495 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36496 "#pragma acc data", pragma_tok);
36498 block = begin_omp_parallel ();
36499 save = cp_parser_begin_omp_structured_block (parser);
36500 cp_parser_statement (parser, NULL_TREE, false, if_p);
36501 cp_parser_end_omp_structured_block (parser, save);
36502 stmt = finish_oacc_data (clauses, block);
36503 return stmt;
36506 /* OpenACC 2.0:
36507 # pragma acc host_data <clauses> new-line
36508 structured-block */
36510 #define OACC_HOST_DATA_CLAUSE_MASK \
36511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36513 static tree
36514 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36516 tree stmt, clauses, block;
36517 unsigned int save;
36519 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36520 "#pragma acc host_data", pragma_tok);
36522 block = begin_omp_parallel ();
36523 save = cp_parser_begin_omp_structured_block (parser);
36524 cp_parser_statement (parser, NULL_TREE, false, if_p);
36525 cp_parser_end_omp_structured_block (parser, save);
36526 stmt = finish_oacc_host_data (clauses, block);
36527 return stmt;
36530 /* OpenACC 2.0:
36531 # pragma acc declare oacc-data-clause[optseq] new-line
36534 #define OACC_DECLARE_CLAUSE_MASK \
36535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36548 static tree
36549 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36551 tree clauses, stmt;
36552 bool error = false;
36554 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36555 "#pragma acc declare", pragma_tok, true);
36558 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36560 error_at (pragma_tok->location,
36561 "no valid clauses specified in %<#pragma acc declare%>");
36562 return NULL_TREE;
36565 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36567 location_t loc = OMP_CLAUSE_LOCATION (t);
36568 tree decl = OMP_CLAUSE_DECL (t);
36569 if (!DECL_P (decl))
36571 error_at (loc, "array section in %<#pragma acc declare%>");
36572 error = true;
36573 continue;
36575 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36576 switch (OMP_CLAUSE_MAP_KIND (t))
36578 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36579 case GOMP_MAP_FORCE_ALLOC:
36580 case GOMP_MAP_FORCE_TO:
36581 case GOMP_MAP_FORCE_DEVICEPTR:
36582 case GOMP_MAP_DEVICE_RESIDENT:
36583 break;
36585 case GOMP_MAP_LINK:
36586 if (!global_bindings_p ()
36587 && (TREE_STATIC (decl)
36588 || !DECL_EXTERNAL (decl)))
36590 error_at (loc,
36591 "%qD must be a global variable in "
36592 "%<#pragma acc declare link%>",
36593 decl);
36594 error = true;
36595 continue;
36597 break;
36599 default:
36600 if (global_bindings_p ())
36602 error_at (loc, "invalid OpenACC clause at file scope");
36603 error = true;
36604 continue;
36606 if (DECL_EXTERNAL (decl))
36608 error_at (loc,
36609 "invalid use of %<extern%> variable %qD "
36610 "in %<#pragma acc declare%>", decl);
36611 error = true;
36612 continue;
36614 else if (TREE_PUBLIC (decl))
36616 error_at (loc,
36617 "invalid use of %<global%> variable %qD "
36618 "in %<#pragma acc declare%>", decl);
36619 error = true;
36620 continue;
36622 break;
36625 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36626 || lookup_attribute ("omp declare target link",
36627 DECL_ATTRIBUTES (decl)))
36629 error_at (loc, "variable %qD used more than once with "
36630 "%<#pragma acc declare%>", decl);
36631 error = true;
36632 continue;
36635 if (!error)
36637 tree id;
36639 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36640 id = get_identifier ("omp declare target link");
36641 else
36642 id = get_identifier ("omp declare target");
36644 DECL_ATTRIBUTES (decl)
36645 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36646 if (global_bindings_p ())
36648 symtab_node *node = symtab_node::get (decl);
36649 if (node != NULL)
36651 node->offloadable = 1;
36652 if (ENABLE_OFFLOADING)
36654 g->have_offload = true;
36655 if (is_a <varpool_node *> (node))
36656 vec_safe_push (offload_vars, decl);
36663 if (error || global_bindings_p ())
36664 return NULL_TREE;
36666 stmt = make_node (OACC_DECLARE);
36667 TREE_TYPE (stmt) = void_type_node;
36668 OACC_DECLARE_CLAUSES (stmt) = clauses;
36669 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36671 add_stmt (stmt);
36673 return NULL_TREE;
36676 /* OpenACC 2.0:
36677 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36681 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36683 LOC is the location of the #pragma token.
36686 #define OACC_ENTER_DATA_CLAUSE_MASK \
36687 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36695 #define OACC_EXIT_DATA_CLAUSE_MASK \
36696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36702 static tree
36703 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36704 bool enter)
36706 location_t loc = pragma_tok->location;
36707 tree stmt, clauses;
36708 const char *p = "";
36710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36711 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36713 if (strcmp (p, "data") != 0)
36715 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36716 enter ? "enter" : "exit");
36717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36718 return NULL_TREE;
36721 cp_lexer_consume_token (parser->lexer);
36723 if (enter)
36724 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36725 "#pragma acc enter data", pragma_tok);
36726 else
36727 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36728 "#pragma acc exit data", pragma_tok);
36730 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36732 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36733 enter ? "enter" : "exit");
36734 return NULL_TREE;
36737 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36738 TREE_TYPE (stmt) = void_type_node;
36739 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36740 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36741 add_stmt (stmt);
36742 return stmt;
36745 /* OpenACC 2.0:
36746 # pragma acc loop oacc-loop-clause[optseq] new-line
36747 structured-block */
36749 #define OACC_LOOP_CLAUSE_MASK \
36750 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36761 static tree
36762 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36763 omp_clause_mask mask, tree *cclauses, bool *if_p)
36765 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36767 strcat (p_name, " loop");
36768 mask |= OACC_LOOP_CLAUSE_MASK;
36770 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36771 cclauses == NULL);
36772 if (cclauses)
36774 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36775 if (*cclauses)
36776 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36777 if (clauses)
36778 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36781 tree block = begin_omp_structured_block ();
36782 int save = cp_parser_begin_omp_structured_block (parser);
36783 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36784 cp_parser_end_omp_structured_block (parser, save);
36785 add_stmt (finish_omp_structured_block (block));
36787 return stmt;
36790 /* OpenACC 2.0:
36791 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36792 structured-block
36796 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36797 structured-block
36800 #define OACC_KERNELS_CLAUSE_MASK \
36801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36819 #define OACC_PARALLEL_CLAUSE_MASK \
36820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36841 static tree
36842 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36843 char *p_name, bool *if_p)
36845 omp_clause_mask mask;
36846 enum tree_code code;
36847 switch (cp_parser_pragma_kind (pragma_tok))
36849 case PRAGMA_OACC_KERNELS:
36850 strcat (p_name, " kernels");
36851 mask = OACC_KERNELS_CLAUSE_MASK;
36852 code = OACC_KERNELS;
36853 break;
36854 case PRAGMA_OACC_PARALLEL:
36855 strcat (p_name, " parallel");
36856 mask = OACC_PARALLEL_CLAUSE_MASK;
36857 code = OACC_PARALLEL;
36858 break;
36859 default:
36860 gcc_unreachable ();
36863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36865 const char *p
36866 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36867 if (strcmp (p, "loop") == 0)
36869 cp_lexer_consume_token (parser->lexer);
36870 tree block = begin_omp_parallel ();
36871 tree clauses;
36872 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36873 if_p);
36874 return finish_omp_construct (code, block, clauses);
36878 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36880 tree block = begin_omp_parallel ();
36881 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36882 cp_parser_statement (parser, NULL_TREE, false, if_p);
36883 cp_parser_end_omp_structured_block (parser, save);
36884 return finish_omp_construct (code, block, clauses);
36887 /* OpenACC 2.0:
36888 # pragma acc update oacc-update-clause[optseq] new-line
36891 #define OACC_UPDATE_CLAUSE_MASK \
36892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36899 static tree
36900 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36902 tree stmt, clauses;
36904 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36905 "#pragma acc update", pragma_tok);
36907 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36909 error_at (pragma_tok->location,
36910 "%<#pragma acc update%> must contain at least one "
36911 "%<device%> or %<host%> or %<self%> clause");
36912 return NULL_TREE;
36915 stmt = make_node (OACC_UPDATE);
36916 TREE_TYPE (stmt) = void_type_node;
36917 OACC_UPDATE_CLAUSES (stmt) = clauses;
36918 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36919 add_stmt (stmt);
36920 return stmt;
36923 /* OpenACC 2.0:
36924 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36926 LOC is the location of the #pragma token.
36929 #define OACC_WAIT_CLAUSE_MASK \
36930 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36932 static tree
36933 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36935 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36936 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36938 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36939 list = cp_parser_oacc_wait_list (parser, loc, list);
36941 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36942 "#pragma acc wait", pragma_tok);
36944 stmt = c_finish_oacc_wait (loc, list, clauses);
36945 stmt = finish_expr_stmt (stmt);
36947 return stmt;
36950 /* OpenMP 4.0:
36951 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36953 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36961 static void
36962 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36963 enum pragma_context context)
36965 bool first_p = parser->omp_declare_simd == NULL;
36966 cp_omp_declare_simd_data data;
36967 if (first_p)
36969 data.error_seen = false;
36970 data.fndecl_seen = false;
36971 data.tokens = vNULL;
36972 data.clauses = NULL_TREE;
36973 /* It is safe to take the address of a local variable; it will only be
36974 used while this scope is live. */
36975 parser->omp_declare_simd = &data;
36978 /* Store away all pragma tokens. */
36979 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36980 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36981 cp_lexer_consume_token (parser->lexer);
36982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36983 parser->omp_declare_simd->error_seen = true;
36984 cp_parser_require_pragma_eol (parser, pragma_tok);
36985 struct cp_token_cache *cp
36986 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36987 parser->omp_declare_simd->tokens.safe_push (cp);
36989 if (first_p)
36991 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36992 cp_parser_pragma (parser, context, NULL);
36993 switch (context)
36995 case pragma_external:
36996 cp_parser_declaration (parser);
36997 break;
36998 case pragma_member:
36999 cp_parser_member_declaration (parser);
37000 break;
37001 case pragma_objc_icode:
37002 cp_parser_block_declaration (parser, /*statement_p=*/false);
37003 break;
37004 default:
37005 cp_parser_declaration_statement (parser);
37006 break;
37008 if (parser->omp_declare_simd
37009 && !parser->omp_declare_simd->error_seen
37010 && !parser->omp_declare_simd->fndecl_seen)
37011 error_at (pragma_tok->location,
37012 "%<#pragma omp declare simd%> not immediately followed by "
37013 "function declaration or definition");
37014 data.tokens.release ();
37015 parser->omp_declare_simd = NULL;
37019 /* Finalize #pragma omp declare simd clauses after direct declarator has
37020 been parsed, and put that into "omp declare simd" attribute. */
37022 static tree
37023 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37025 struct cp_token_cache *ce;
37026 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37027 int i;
37029 if (!data->error_seen && data->fndecl_seen)
37031 error ("%<#pragma omp declare simd%> not immediately followed by "
37032 "a single function declaration or definition");
37033 data->error_seen = true;
37035 if (data->error_seen)
37036 return attrs;
37038 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37040 tree c, cl;
37042 cp_parser_push_lexer_for_tokens (parser, ce);
37043 parser->lexer->in_pragma = true;
37044 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37045 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37046 cp_lexer_consume_token (parser->lexer);
37047 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37048 "#pragma omp declare simd", pragma_tok);
37049 cp_parser_pop_lexer (parser);
37050 if (cl)
37051 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37052 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37053 TREE_CHAIN (c) = attrs;
37054 if (processing_template_decl)
37055 ATTR_IS_DEPENDENT (c) = 1;
37056 attrs = c;
37059 data->fndecl_seen = true;
37060 return attrs;
37064 /* OpenMP 4.0:
37065 # pragma omp declare target new-line
37066 declarations and definitions
37067 # pragma omp end declare target new-line
37069 OpenMP 4.5:
37070 # pragma omp declare target ( extended-list ) new-line
37072 # pragma omp declare target declare-target-clauses[seq] new-line */
37074 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37078 static void
37079 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37081 tree clauses = NULL_TREE;
37082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37083 clauses
37084 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37085 "#pragma omp declare target", pragma_tok);
37086 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37088 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37089 clauses);
37090 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37091 cp_parser_require_pragma_eol (parser, pragma_tok);
37093 else
37095 cp_parser_require_pragma_eol (parser, pragma_tok);
37096 scope_chain->omp_declare_target_attribute++;
37097 return;
37099 if (scope_chain->omp_declare_target_attribute)
37100 error_at (pragma_tok->location,
37101 "%<#pragma omp declare target%> with clauses in between "
37102 "%<#pragma omp declare target%> without clauses and "
37103 "%<#pragma omp end declare target%>");
37104 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37106 tree t = OMP_CLAUSE_DECL (c), id;
37107 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37108 tree at2 = lookup_attribute ("omp declare target link",
37109 DECL_ATTRIBUTES (t));
37110 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37112 id = get_identifier ("omp declare target link");
37113 std::swap (at1, at2);
37115 else
37116 id = get_identifier ("omp declare target");
37117 if (at2)
37119 error_at (OMP_CLAUSE_LOCATION (c),
37120 "%qD specified both in declare target %<link%> and %<to%>"
37121 " clauses", t);
37122 continue;
37124 if (!at1)
37126 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37127 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37128 continue;
37130 symtab_node *node = symtab_node::get (t);
37131 if (node != NULL)
37133 node->offloadable = 1;
37134 if (ENABLE_OFFLOADING)
37136 g->have_offload = true;
37137 if (is_a <varpool_node *> (node))
37138 vec_safe_push (offload_vars, t);
37145 static void
37146 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37148 const char *p = "";
37149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37152 p = IDENTIFIER_POINTER (id);
37154 if (strcmp (p, "declare") == 0)
37156 cp_lexer_consume_token (parser->lexer);
37157 p = "";
37158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37160 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37161 p = IDENTIFIER_POINTER (id);
37163 if (strcmp (p, "target") == 0)
37164 cp_lexer_consume_token (parser->lexer);
37165 else
37167 cp_parser_error (parser, "expected %<target%>");
37168 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37169 return;
37172 else
37174 cp_parser_error (parser, "expected %<declare%>");
37175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37176 return;
37178 cp_parser_require_pragma_eol (parser, pragma_tok);
37179 if (!scope_chain->omp_declare_target_attribute)
37180 error_at (pragma_tok->location,
37181 "%<#pragma omp end declare target%> without corresponding "
37182 "%<#pragma omp declare target%>");
37183 else
37184 scope_chain->omp_declare_target_attribute--;
37187 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37188 expression and optional initializer clause of
37189 #pragma omp declare reduction. We store the expression(s) as
37190 either 3, 6 or 7 special statements inside of the artificial function's
37191 body. The first two statements are DECL_EXPRs for the artificial
37192 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37193 expression that uses those variables.
37194 If there was any INITIALIZER clause, this is followed by further statements,
37195 the fourth and fifth statements are DECL_EXPRs for the artificial
37196 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37197 constructor variant (first token after open paren is not omp_priv),
37198 then the sixth statement is a statement with the function call expression
37199 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37200 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37201 to initialize the OMP_PRIV artificial variable and there is seventh
37202 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37204 static bool
37205 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37207 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37208 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37209 type = TREE_TYPE (type);
37210 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37211 DECL_ARTIFICIAL (omp_out) = 1;
37212 pushdecl (omp_out);
37213 add_decl_expr (omp_out);
37214 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37215 DECL_ARTIFICIAL (omp_in) = 1;
37216 pushdecl (omp_in);
37217 add_decl_expr (omp_in);
37218 tree combiner;
37219 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37221 keep_next_level (true);
37222 tree block = begin_omp_structured_block ();
37223 combiner = cp_parser_expression (parser);
37224 finish_expr_stmt (combiner);
37225 block = finish_omp_structured_block (block);
37226 add_stmt (block);
37228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37229 return false;
37231 const char *p = "";
37232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37235 p = IDENTIFIER_POINTER (id);
37238 if (strcmp (p, "initializer") == 0)
37240 cp_lexer_consume_token (parser->lexer);
37241 matching_parens parens;
37242 if (!parens.require_open (parser))
37243 return false;
37245 p = "";
37246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37248 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37249 p = IDENTIFIER_POINTER (id);
37252 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37253 DECL_ARTIFICIAL (omp_priv) = 1;
37254 pushdecl (omp_priv);
37255 add_decl_expr (omp_priv);
37256 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37257 DECL_ARTIFICIAL (omp_orig) = 1;
37258 pushdecl (omp_orig);
37259 add_decl_expr (omp_orig);
37261 keep_next_level (true);
37262 block = begin_omp_structured_block ();
37264 bool ctor = false;
37265 if (strcmp (p, "omp_priv") == 0)
37267 bool is_direct_init, is_non_constant_init;
37268 ctor = true;
37269 cp_lexer_consume_token (parser->lexer);
37270 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37272 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37273 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37274 == CPP_CLOSE_PAREN
37275 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37276 == CPP_CLOSE_PAREN))
37278 finish_omp_structured_block (block);
37279 error ("invalid initializer clause");
37280 return false;
37282 initializer = cp_parser_initializer (parser, &is_direct_init,
37283 &is_non_constant_init);
37284 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37285 NULL_TREE, LOOKUP_ONLYCONVERTING);
37287 else
37289 cp_parser_parse_tentatively (parser);
37290 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37291 /*check_dependency_p=*/true,
37292 /*template_p=*/NULL,
37293 /*declarator_p=*/false,
37294 /*optional_p=*/false);
37295 vec<tree, va_gc> *args;
37296 if (fn_name == error_mark_node
37297 || cp_parser_error_occurred (parser)
37298 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37299 || ((args = cp_parser_parenthesized_expression_list
37300 (parser, non_attr, /*cast_p=*/false,
37301 /*allow_expansion_p=*/true,
37302 /*non_constant_p=*/NULL)),
37303 cp_parser_error_occurred (parser)))
37305 finish_omp_structured_block (block);
37306 cp_parser_abort_tentative_parse (parser);
37307 cp_parser_error (parser, "expected id-expression (arguments)");
37308 return false;
37310 unsigned int i;
37311 tree arg;
37312 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37313 if (arg == omp_priv
37314 || (TREE_CODE (arg) == ADDR_EXPR
37315 && TREE_OPERAND (arg, 0) == omp_priv))
37316 break;
37317 cp_parser_abort_tentative_parse (parser);
37318 if (arg == NULL_TREE)
37319 error ("one of the initializer call arguments should be %<omp_priv%>"
37320 " or %<&omp_priv%>");
37321 initializer = cp_parser_postfix_expression (parser, false, false, false,
37322 false, NULL);
37323 finish_expr_stmt (initializer);
37326 block = finish_omp_structured_block (block);
37327 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37328 add_stmt (block);
37330 if (ctor)
37331 add_decl_expr (omp_orig);
37333 if (!parens.require_close (parser))
37334 return false;
37337 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37338 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37339 UNKNOWN_LOCATION);
37341 return true;
37344 /* OpenMP 4.0
37345 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37346 initializer-clause[opt] new-line
37348 initializer-clause:
37349 initializer (omp_priv initializer)
37350 initializer (function-name (argument-list)) */
37352 static void
37353 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37354 enum pragma_context)
37356 auto_vec<tree> types;
37357 enum tree_code reduc_code = ERROR_MARK;
37358 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37359 unsigned int i;
37360 cp_token *first_token;
37361 cp_token_cache *cp;
37362 int errs;
37363 void *p;
37365 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37366 p = obstack_alloc (&declarator_obstack, 0);
37368 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37369 goto fail;
37371 switch (cp_lexer_peek_token (parser->lexer)->type)
37373 case CPP_PLUS:
37374 reduc_code = PLUS_EXPR;
37375 break;
37376 case CPP_MULT:
37377 reduc_code = MULT_EXPR;
37378 break;
37379 case CPP_MINUS:
37380 reduc_code = MINUS_EXPR;
37381 break;
37382 case CPP_AND:
37383 reduc_code = BIT_AND_EXPR;
37384 break;
37385 case CPP_XOR:
37386 reduc_code = BIT_XOR_EXPR;
37387 break;
37388 case CPP_OR:
37389 reduc_code = BIT_IOR_EXPR;
37390 break;
37391 case CPP_AND_AND:
37392 reduc_code = TRUTH_ANDIF_EXPR;
37393 break;
37394 case CPP_OR_OR:
37395 reduc_code = TRUTH_ORIF_EXPR;
37396 break;
37397 case CPP_NAME:
37398 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37399 break;
37400 default:
37401 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37402 "%<|%>, %<&&%>, %<||%> or identifier");
37403 goto fail;
37406 if (reduc_code != ERROR_MARK)
37407 cp_lexer_consume_token (parser->lexer);
37409 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37410 if (reduc_id == error_mark_node)
37411 goto fail;
37413 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37414 goto fail;
37416 /* Types may not be defined in declare reduction type list. */
37417 const char *saved_message;
37418 saved_message = parser->type_definition_forbidden_message;
37419 parser->type_definition_forbidden_message
37420 = G_("types may not be defined in declare reduction type list");
37421 bool saved_colon_corrects_to_scope_p;
37422 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37423 parser->colon_corrects_to_scope_p = false;
37424 bool saved_colon_doesnt_start_class_def_p;
37425 saved_colon_doesnt_start_class_def_p
37426 = parser->colon_doesnt_start_class_def_p;
37427 parser->colon_doesnt_start_class_def_p = true;
37429 while (true)
37431 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37432 type = cp_parser_type_id (parser);
37433 if (type == error_mark_node)
37435 else if (ARITHMETIC_TYPE_P (type)
37436 && (orig_reduc_id == NULL_TREE
37437 || (TREE_CODE (type) != COMPLEX_TYPE
37438 && (id_equal (orig_reduc_id, "min")
37439 || id_equal (orig_reduc_id, "max")))))
37440 error_at (loc, "predeclared arithmetic type %qT in "
37441 "%<#pragma omp declare reduction%>", type);
37442 else if (TREE_CODE (type) == FUNCTION_TYPE
37443 || TREE_CODE (type) == METHOD_TYPE
37444 || TREE_CODE (type) == ARRAY_TYPE)
37445 error_at (loc, "function or array type %qT in "
37446 "%<#pragma omp declare reduction%>", type);
37447 else if (TREE_CODE (type) == REFERENCE_TYPE)
37448 error_at (loc, "reference type %qT in "
37449 "%<#pragma omp declare reduction%>", type);
37450 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37451 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37452 "%<#pragma omp declare reduction%>", type);
37453 else
37454 types.safe_push (type);
37456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37457 cp_lexer_consume_token (parser->lexer);
37458 else
37459 break;
37462 /* Restore the saved message. */
37463 parser->type_definition_forbidden_message = saved_message;
37464 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37465 parser->colon_doesnt_start_class_def_p
37466 = saved_colon_doesnt_start_class_def_p;
37468 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37469 || types.is_empty ())
37471 fail:
37472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37473 goto done;
37476 first_token = cp_lexer_peek_token (parser->lexer);
37477 cp = NULL;
37478 errs = errorcount;
37479 FOR_EACH_VEC_ELT (types, i, type)
37481 tree fntype
37482 = build_function_type_list (void_type_node,
37483 cp_build_reference_type (type, false),
37484 NULL_TREE);
37485 tree this_reduc_id = reduc_id;
37486 if (!dependent_type_p (type))
37487 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37488 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37489 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37490 DECL_ARTIFICIAL (fndecl) = 1;
37491 DECL_EXTERNAL (fndecl) = 1;
37492 DECL_DECLARED_INLINE_P (fndecl) = 1;
37493 DECL_IGNORED_P (fndecl) = 1;
37494 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37495 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37496 DECL_ATTRIBUTES (fndecl)
37497 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37498 DECL_ATTRIBUTES (fndecl));
37499 if (processing_template_decl)
37500 fndecl = push_template_decl (fndecl);
37501 bool block_scope = false;
37502 tree block = NULL_TREE;
37503 if (current_function_decl)
37505 block_scope = true;
37506 DECL_CONTEXT (fndecl) = global_namespace;
37507 if (!processing_template_decl)
37508 pushdecl (fndecl);
37510 else if (current_class_type)
37512 if (cp == NULL)
37514 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37515 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37516 cp_lexer_consume_token (parser->lexer);
37517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37518 goto fail;
37519 cp = cp_token_cache_new (first_token,
37520 cp_lexer_peek_nth_token (parser->lexer,
37521 2));
37523 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37524 finish_member_declaration (fndecl);
37525 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37526 DECL_PENDING_INLINE_P (fndecl) = 1;
37527 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37528 continue;
37530 else
37532 DECL_CONTEXT (fndecl) = current_namespace;
37533 pushdecl (fndecl);
37535 if (!block_scope)
37536 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37537 else
37538 block = begin_omp_structured_block ();
37539 if (cp)
37541 cp_parser_push_lexer_for_tokens (parser, cp);
37542 parser->lexer->in_pragma = true;
37544 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37546 if (!block_scope)
37547 finish_function (/*inline_p=*/false);
37548 else
37549 DECL_CONTEXT (fndecl) = current_function_decl;
37550 if (cp)
37551 cp_parser_pop_lexer (parser);
37552 goto fail;
37554 if (cp)
37555 cp_parser_pop_lexer (parser);
37556 if (!block_scope)
37557 finish_function (/*inline_p=*/false);
37558 else
37560 DECL_CONTEXT (fndecl) = current_function_decl;
37561 block = finish_omp_structured_block (block);
37562 if (TREE_CODE (block) == BIND_EXPR)
37563 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37564 else if (TREE_CODE (block) == STATEMENT_LIST)
37565 DECL_SAVED_TREE (fndecl) = block;
37566 if (processing_template_decl)
37567 add_decl_expr (fndecl);
37569 cp_check_omp_declare_reduction (fndecl);
37570 if (cp == NULL && types.length () > 1)
37571 cp = cp_token_cache_new (first_token,
37572 cp_lexer_peek_nth_token (parser->lexer, 2));
37573 if (errs != errorcount)
37574 break;
37577 cp_parser_require_pragma_eol (parser, pragma_tok);
37579 done:
37580 /* Free any declarators allocated. */
37581 obstack_free (&declarator_obstack, p);
37584 /* OpenMP 4.0
37585 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37586 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37587 initializer-clause[opt] new-line
37588 #pragma omp declare target new-line */
37590 static bool
37591 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37592 enum pragma_context context)
37594 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37597 const char *p = IDENTIFIER_POINTER (id);
37599 if (strcmp (p, "simd") == 0)
37601 cp_lexer_consume_token (parser->lexer);
37602 cp_parser_omp_declare_simd (parser, pragma_tok,
37603 context);
37604 return true;
37606 cp_ensure_no_omp_declare_simd (parser);
37607 if (strcmp (p, "reduction") == 0)
37609 cp_lexer_consume_token (parser->lexer);
37610 cp_parser_omp_declare_reduction (parser, pragma_tok,
37611 context);
37612 return false;
37614 if (!flag_openmp) /* flag_openmp_simd */
37616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37617 return false;
37619 if (strcmp (p, "target") == 0)
37621 cp_lexer_consume_token (parser->lexer);
37622 cp_parser_omp_declare_target (parser, pragma_tok);
37623 return false;
37626 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37627 "or %<target%>");
37628 cp_parser_require_pragma_eol (parser, pragma_tok);
37629 return false;
37632 /* OpenMP 4.5:
37633 #pragma omp taskloop taskloop-clause[optseq] new-line
37634 for-loop
37636 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37637 for-loop */
37639 #define OMP_TASKLOOP_CLAUSE_MASK \
37640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37655 static tree
37656 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37657 char *p_name, omp_clause_mask mask, tree *cclauses,
37658 bool *if_p)
37660 tree clauses, sb, ret;
37661 unsigned int save;
37662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37664 strcat (p_name, " taskloop");
37665 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37670 const char *p = IDENTIFIER_POINTER (id);
37672 if (strcmp (p, "simd") == 0)
37674 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37675 if (cclauses == NULL)
37676 cclauses = cclauses_buf;
37678 cp_lexer_consume_token (parser->lexer);
37679 if (!flag_openmp) /* flag_openmp_simd */
37680 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37681 cclauses, if_p);
37682 sb = begin_omp_structured_block ();
37683 save = cp_parser_begin_omp_structured_block (parser);
37684 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37685 cclauses, if_p);
37686 cp_parser_end_omp_structured_block (parser, save);
37687 tree body = finish_omp_structured_block (sb);
37688 if (ret == NULL)
37689 return ret;
37690 ret = make_node (OMP_TASKLOOP);
37691 TREE_TYPE (ret) = void_type_node;
37692 OMP_FOR_BODY (ret) = body;
37693 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37694 SET_EXPR_LOCATION (ret, loc);
37695 add_stmt (ret);
37696 return ret;
37699 if (!flag_openmp) /* flag_openmp_simd */
37701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37702 return NULL_TREE;
37705 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37706 cclauses == NULL);
37707 if (cclauses)
37709 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37710 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37713 sb = begin_omp_structured_block ();
37714 save = cp_parser_begin_omp_structured_block (parser);
37716 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37717 if_p);
37719 cp_parser_end_omp_structured_block (parser, save);
37720 add_stmt (finish_omp_structured_block (sb));
37722 return ret;
37726 /* OpenACC 2.0:
37727 # pragma acc routine oacc-routine-clause[optseq] new-line
37728 function-definition
37730 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37733 #define OACC_ROUTINE_CLAUSE_MASK \
37734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37740 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37741 component, which must resolve to a declared namespace-scope
37742 function. The clauses are either processed directly (for a named
37743 function), or defered until the immediatley following declaration
37744 is parsed. */
37746 static void
37747 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37748 enum pragma_context context)
37750 gcc_checking_assert (context == pragma_external);
37751 /* The checking for "another pragma following this one" in the "no optional
37752 '( name )'" case makes sure that we dont re-enter. */
37753 gcc_checking_assert (parser->oacc_routine == NULL);
37755 cp_oacc_routine_data data;
37756 data.error_seen = false;
37757 data.fndecl_seen = false;
37758 data.tokens = vNULL;
37759 data.clauses = NULL_TREE;
37760 data.loc = pragma_tok->location;
37761 /* It is safe to take the address of a local variable; it will only be
37762 used while this scope is live. */
37763 parser->oacc_routine = &data;
37765 /* Look for optional '( name )'. */
37766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37768 matching_parens parens;
37769 parens.consume_open (parser); /* '(' */
37771 /* We parse the name as an id-expression. If it resolves to
37772 anything other than a non-overloaded function at namespace
37773 scope, it's an error. */
37774 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37775 tree name = cp_parser_id_expression (parser,
37776 /*template_keyword_p=*/false,
37777 /*check_dependency_p=*/false,
37778 /*template_p=*/NULL,
37779 /*declarator_p=*/false,
37780 /*optional_p=*/false);
37781 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37782 if (name != error_mark_node && decl == error_mark_node)
37783 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37785 if (decl == error_mark_node
37786 || !parens.require_close (parser))
37788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37789 parser->oacc_routine = NULL;
37790 return;
37793 data.clauses
37794 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37795 "#pragma acc routine",
37796 cp_lexer_peek_token (parser->lexer));
37798 if (decl && is_overloaded_fn (decl)
37799 && (TREE_CODE (decl) != FUNCTION_DECL
37800 || DECL_FUNCTION_TEMPLATE_P (decl)))
37802 error_at (name_loc,
37803 "%<#pragma acc routine%> names a set of overloads");
37804 parser->oacc_routine = NULL;
37805 return;
37808 /* Perhaps we should use the same rule as declarations in different
37809 namespaces? */
37810 if (!DECL_NAMESPACE_SCOPE_P (decl))
37812 error_at (name_loc,
37813 "%qD does not refer to a namespace scope function", decl);
37814 parser->oacc_routine = NULL;
37815 return;
37818 if (TREE_CODE (decl) != FUNCTION_DECL)
37820 error_at (name_loc, "%qD does not refer to a function", decl);
37821 parser->oacc_routine = NULL;
37822 return;
37825 cp_finalize_oacc_routine (parser, decl, false);
37826 parser->oacc_routine = NULL;
37828 else /* No optional '( name )'. */
37830 /* Store away all pragma tokens. */
37831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37832 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37833 cp_lexer_consume_token (parser->lexer);
37834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37835 parser->oacc_routine->error_seen = true;
37836 cp_parser_require_pragma_eol (parser, pragma_tok);
37837 struct cp_token_cache *cp
37838 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37839 parser->oacc_routine->tokens.safe_push (cp);
37841 /* Emit a helpful diagnostic if there's another pragma following this
37842 one. */
37843 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37845 cp_ensure_no_oacc_routine (parser);
37846 data.tokens.release ();
37847 /* ..., and then just keep going. */
37848 return;
37851 /* We only have to consider the pragma_external case here. */
37852 cp_parser_declaration (parser);
37853 if (parser->oacc_routine
37854 && !parser->oacc_routine->fndecl_seen)
37855 cp_ensure_no_oacc_routine (parser);
37856 else
37857 parser->oacc_routine = NULL;
37858 data.tokens.release ();
37862 /* Finalize #pragma acc routine clauses after direct declarator has
37863 been parsed. */
37865 static tree
37866 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37868 struct cp_token_cache *ce;
37869 cp_oacc_routine_data *data = parser->oacc_routine;
37871 if (!data->error_seen && data->fndecl_seen)
37873 error_at (data->loc,
37874 "%<#pragma acc routine%> not immediately followed by "
37875 "a single function declaration or definition");
37876 data->error_seen = true;
37878 if (data->error_seen)
37879 return attrs;
37881 gcc_checking_assert (data->tokens.length () == 1);
37882 ce = data->tokens[0];
37884 cp_parser_push_lexer_for_tokens (parser, ce);
37885 parser->lexer->in_pragma = true;
37886 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37888 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37889 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37890 parser->oacc_routine->clauses
37891 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37892 "#pragma acc routine", pragma_tok);
37893 cp_parser_pop_lexer (parser);
37894 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37895 fndecl_seen. */
37897 return attrs;
37900 /* Apply any saved OpenACC routine clauses to a just-parsed
37901 declaration. */
37903 static void
37904 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37906 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37908 /* Keep going if we're in error reporting mode. */
37909 if (parser->oacc_routine->error_seen
37910 || fndecl == error_mark_node)
37911 return;
37913 if (parser->oacc_routine->fndecl_seen)
37915 error_at (parser->oacc_routine->loc,
37916 "%<#pragma acc routine%> not immediately followed by"
37917 " a single function declaration or definition");
37918 parser->oacc_routine = NULL;
37919 return;
37921 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37923 cp_ensure_no_oacc_routine (parser);
37924 return;
37927 if (oacc_get_fn_attrib (fndecl))
37929 error_at (parser->oacc_routine->loc,
37930 "%<#pragma acc routine%> already applied to %qD", fndecl);
37931 parser->oacc_routine = NULL;
37932 return;
37935 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37937 error_at (parser->oacc_routine->loc,
37938 TREE_USED (fndecl)
37939 ? G_("%<#pragma acc routine%> must be applied before use")
37940 : G_("%<#pragma acc routine%> must be applied before "
37941 "definition"));
37942 parser->oacc_routine = NULL;
37943 return;
37946 /* Process the routine's dimension clauses. */
37947 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37948 oacc_replace_fn_attrib (fndecl, dims);
37950 /* Add an "omp declare target" attribute. */
37951 DECL_ATTRIBUTES (fndecl)
37952 = tree_cons (get_identifier ("omp declare target"),
37953 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37955 /* Don't unset parser->oacc_routine here: we may still need it to
37956 diagnose wrong usage. But, remember that we've used this "#pragma acc
37957 routine". */
37958 parser->oacc_routine->fndecl_seen = true;
37962 /* Main entry point to OpenMP statement pragmas. */
37964 static void
37965 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37967 tree stmt;
37968 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37969 omp_clause_mask mask (0);
37971 switch (cp_parser_pragma_kind (pragma_tok))
37973 case PRAGMA_OACC_ATOMIC:
37974 cp_parser_omp_atomic (parser, pragma_tok);
37975 return;
37976 case PRAGMA_OACC_CACHE:
37977 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37978 break;
37979 case PRAGMA_OACC_DATA:
37980 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37981 break;
37982 case PRAGMA_OACC_ENTER_DATA:
37983 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37984 break;
37985 case PRAGMA_OACC_EXIT_DATA:
37986 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37987 break;
37988 case PRAGMA_OACC_HOST_DATA:
37989 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37990 break;
37991 case PRAGMA_OACC_KERNELS:
37992 case PRAGMA_OACC_PARALLEL:
37993 strcpy (p_name, "#pragma acc");
37994 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37995 if_p);
37996 break;
37997 case PRAGMA_OACC_LOOP:
37998 strcpy (p_name, "#pragma acc");
37999 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38000 if_p);
38001 break;
38002 case PRAGMA_OACC_UPDATE:
38003 stmt = cp_parser_oacc_update (parser, pragma_tok);
38004 break;
38005 case PRAGMA_OACC_WAIT:
38006 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38007 break;
38008 case PRAGMA_OMP_ATOMIC:
38009 cp_parser_omp_atomic (parser, pragma_tok);
38010 return;
38011 case PRAGMA_OMP_CRITICAL:
38012 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38013 break;
38014 case PRAGMA_OMP_DISTRIBUTE:
38015 strcpy (p_name, "#pragma omp");
38016 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38017 if_p);
38018 break;
38019 case PRAGMA_OMP_FOR:
38020 strcpy (p_name, "#pragma omp");
38021 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38022 if_p);
38023 break;
38024 case PRAGMA_OMP_MASTER:
38025 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38026 break;
38027 case PRAGMA_OMP_PARALLEL:
38028 strcpy (p_name, "#pragma omp");
38029 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38030 if_p);
38031 break;
38032 case PRAGMA_OMP_SECTIONS:
38033 strcpy (p_name, "#pragma omp");
38034 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38035 break;
38036 case PRAGMA_OMP_SIMD:
38037 strcpy (p_name, "#pragma omp");
38038 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38039 if_p);
38040 break;
38041 case PRAGMA_OMP_SINGLE:
38042 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38043 break;
38044 case PRAGMA_OMP_TASK:
38045 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38046 break;
38047 case PRAGMA_OMP_TASKGROUP:
38048 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38049 break;
38050 case PRAGMA_OMP_TASKLOOP:
38051 strcpy (p_name, "#pragma omp");
38052 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38053 if_p);
38054 break;
38055 case PRAGMA_OMP_TEAMS:
38056 strcpy (p_name, "#pragma omp");
38057 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38058 if_p);
38059 break;
38060 default:
38061 gcc_unreachable ();
38064 protected_set_expr_location (stmt, pragma_tok->location);
38067 /* Transactional Memory parsing routines. */
38069 /* Parse a transaction attribute.
38071 txn-attribute:
38072 attribute
38073 [ [ identifier ] ]
38075 We use this instead of cp_parser_attributes_opt for transactions to avoid
38076 the pedwarn in C++98 mode. */
38078 static tree
38079 cp_parser_txn_attribute_opt (cp_parser *parser)
38081 cp_token *token;
38082 tree attr_name, attr = NULL;
38084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38085 return cp_parser_attributes_opt (parser);
38087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38088 return NULL_TREE;
38089 cp_lexer_consume_token (parser->lexer);
38090 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38091 goto error1;
38093 token = cp_lexer_peek_token (parser->lexer);
38094 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38096 token = cp_lexer_consume_token (parser->lexer);
38098 attr_name = (token->type == CPP_KEYWORD
38099 /* For keywords, use the canonical spelling,
38100 not the parsed identifier. */
38101 ? ridpointers[(int) token->keyword]
38102 : token->u.value);
38103 attr = build_tree_list (attr_name, NULL_TREE);
38105 else
38106 cp_parser_error (parser, "expected identifier");
38108 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38109 error1:
38110 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38111 return attr;
38114 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38116 transaction-statement:
38117 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38118 compound-statement
38119 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38122 static tree
38123 cp_parser_transaction (cp_parser *parser, cp_token *token)
38125 unsigned char old_in = parser->in_transaction;
38126 unsigned char this_in = 1, new_in;
38127 enum rid keyword = token->keyword;
38128 tree stmt, attrs, noex;
38130 cp_lexer_consume_token (parser->lexer);
38132 if (keyword == RID_TRANSACTION_RELAXED
38133 || keyword == RID_SYNCHRONIZED)
38134 this_in |= TM_STMT_ATTR_RELAXED;
38135 else
38137 attrs = cp_parser_txn_attribute_opt (parser);
38138 if (attrs)
38139 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38142 /* Parse a noexcept specification. */
38143 if (keyword == RID_ATOMIC_NOEXCEPT)
38144 noex = boolean_true_node;
38145 else if (keyword == RID_ATOMIC_CANCEL)
38147 /* cancel-and-throw is unimplemented. */
38148 sorry ("atomic_cancel");
38149 noex = NULL_TREE;
38151 else
38152 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38154 /* Keep track if we're in the lexical scope of an outer transaction. */
38155 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38157 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38159 parser->in_transaction = new_in;
38160 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38161 parser->in_transaction = old_in;
38163 finish_transaction_stmt (stmt, NULL, this_in, noex);
38165 return stmt;
38168 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38170 transaction-expression:
38171 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38172 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38175 static tree
38176 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38178 unsigned char old_in = parser->in_transaction;
38179 unsigned char this_in = 1;
38180 cp_token *token;
38181 tree expr, noex;
38182 bool noex_expr;
38183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38185 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38186 || keyword == RID_TRANSACTION_RELAXED);
38188 if (!flag_tm)
38189 error_at (loc,
38190 keyword == RID_TRANSACTION_RELAXED
38191 ? G_("%<__transaction_relaxed%> without transactional memory "
38192 "support enabled")
38193 : G_("%<__transaction_atomic%> without transactional memory "
38194 "support enabled"));
38196 token = cp_parser_require_keyword (parser, keyword,
38197 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38198 : RT_TRANSACTION_RELAXED));
38199 gcc_assert (token != NULL);
38201 if (keyword == RID_TRANSACTION_RELAXED)
38202 this_in |= TM_STMT_ATTR_RELAXED;
38204 /* Set this early. This might mean that we allow transaction_cancel in
38205 an expression that we find out later actually has to be a constexpr.
38206 However, we expect that cxx_constant_value will be able to deal with
38207 this; also, if the noexcept has no constexpr, then what we parse next
38208 really is a transaction's body. */
38209 parser->in_transaction = this_in;
38211 /* Parse a noexcept specification. */
38212 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38213 true);
38215 if (!noex || !noex_expr
38216 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38218 matching_parens parens;
38219 parens.require_open (parser);
38221 expr = cp_parser_expression (parser);
38222 expr = finish_parenthesized_expr (expr);
38224 parens.require_close (parser);
38226 else
38228 /* The only expression that is available got parsed for the noexcept
38229 already. noexcept is true then. */
38230 expr = noex;
38231 noex = boolean_true_node;
38234 expr = build_transaction_expr (token->location, expr, this_in, noex);
38235 parser->in_transaction = old_in;
38237 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38238 return error_mark_node;
38240 return (flag_tm ? expr : error_mark_node);
38243 /* Parse a function-transaction-block.
38245 function-transaction-block:
38246 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38247 function-body
38248 __transaction_atomic txn-attribute[opt] function-try-block
38249 __transaction_relaxed ctor-initializer[opt] function-body
38250 __transaction_relaxed function-try-block
38253 static void
38254 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38256 unsigned char old_in = parser->in_transaction;
38257 unsigned char new_in = 1;
38258 tree compound_stmt, stmt, attrs;
38259 cp_token *token;
38261 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38262 || keyword == RID_TRANSACTION_RELAXED);
38263 token = cp_parser_require_keyword (parser, keyword,
38264 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38265 : RT_TRANSACTION_RELAXED));
38266 gcc_assert (token != NULL);
38268 if (keyword == RID_TRANSACTION_RELAXED)
38269 new_in |= TM_STMT_ATTR_RELAXED;
38270 else
38272 attrs = cp_parser_txn_attribute_opt (parser);
38273 if (attrs)
38274 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38277 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38279 parser->in_transaction = new_in;
38281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38282 cp_parser_function_try_block (parser);
38283 else
38284 cp_parser_ctor_initializer_opt_and_function_body
38285 (parser, /*in_function_try_block=*/false);
38287 parser->in_transaction = old_in;
38289 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38292 /* Parse a __transaction_cancel statement.
38294 cancel-statement:
38295 __transaction_cancel txn-attribute[opt] ;
38296 __transaction_cancel txn-attribute[opt] throw-expression ;
38298 ??? Cancel and throw is not yet implemented. */
38300 static tree
38301 cp_parser_transaction_cancel (cp_parser *parser)
38303 cp_token *token;
38304 bool is_outer = false;
38305 tree stmt, attrs;
38307 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38308 RT_TRANSACTION_CANCEL);
38309 gcc_assert (token != NULL);
38311 attrs = cp_parser_txn_attribute_opt (parser);
38312 if (attrs)
38313 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38315 /* ??? Parse cancel-and-throw here. */
38317 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38319 if (!flag_tm)
38321 error_at (token->location, "%<__transaction_cancel%> without "
38322 "transactional memory support enabled");
38323 return error_mark_node;
38325 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38327 error_at (token->location, "%<__transaction_cancel%> within a "
38328 "%<__transaction_relaxed%>");
38329 return error_mark_node;
38331 else if (is_outer)
38333 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38334 && !is_tm_may_cancel_outer (current_function_decl))
38336 error_at (token->location, "outer %<__transaction_cancel%> not "
38337 "within outer %<__transaction_atomic%>");
38338 error_at (token->location,
38339 " or a %<transaction_may_cancel_outer%> function");
38340 return error_mark_node;
38343 else if (parser->in_transaction == 0)
38345 error_at (token->location, "%<__transaction_cancel%> not within "
38346 "%<__transaction_atomic%>");
38347 return error_mark_node;
38350 stmt = build_tm_abort_call (token->location, is_outer);
38351 add_stmt (stmt);
38353 return stmt;
38356 /* The parser. */
38358 static GTY (()) cp_parser *the_parser;
38361 /* Special handling for the first token or line in the file. The first
38362 thing in the file might be #pragma GCC pch_preprocess, which loads a
38363 PCH file, which is a GC collection point. So we need to handle this
38364 first pragma without benefit of an existing lexer structure.
38366 Always returns one token to the caller in *FIRST_TOKEN. This is
38367 either the true first token of the file, or the first token after
38368 the initial pragma. */
38370 static void
38371 cp_parser_initial_pragma (cp_token *first_token)
38373 tree name = NULL;
38375 cp_lexer_get_preprocessor_token (NULL, first_token);
38376 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38377 return;
38379 cp_lexer_get_preprocessor_token (NULL, first_token);
38380 if (first_token->type == CPP_STRING)
38382 name = first_token->u.value;
38384 cp_lexer_get_preprocessor_token (NULL, first_token);
38385 if (first_token->type != CPP_PRAGMA_EOL)
38386 error_at (first_token->location,
38387 "junk at end of %<#pragma GCC pch_preprocess%>");
38389 else
38390 error_at (first_token->location, "expected string literal");
38392 /* Skip to the end of the pragma. */
38393 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38394 cp_lexer_get_preprocessor_token (NULL, first_token);
38396 /* Now actually load the PCH file. */
38397 if (name)
38398 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38400 /* Read one more token to return to our caller. We have to do this
38401 after reading the PCH file in, since its pointers have to be
38402 live. */
38403 cp_lexer_get_preprocessor_token (NULL, first_token);
38406 /* Parse a pragma GCC ivdep. */
38408 static bool
38409 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38411 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38412 return true;
38415 /* Parse a pragma GCC unroll. */
38417 static unsigned short
38418 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38420 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38421 tree expr = cp_parser_constant_expression (parser);
38422 unsigned short unroll;
38423 expr = maybe_constant_value (expr);
38424 HOST_WIDE_INT lunroll = 0;
38425 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38426 || TREE_CODE (expr) != INTEGER_CST
38427 || (lunroll = tree_to_shwi (expr)) < 0
38428 || lunroll >= USHRT_MAX)
38430 error_at (location, "%<#pragma GCC unroll%> requires an"
38431 " assignment-expression that evaluates to a non-negative"
38432 " integral constant less than %u", USHRT_MAX);
38433 unroll = 0;
38435 else
38437 unroll = (unsigned short)lunroll;
38438 if (unroll == 0)
38439 unroll = 1;
38441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38442 return unroll;
38445 /* Normal parsing of a pragma token. Here we can (and must) use the
38446 regular lexer. */
38448 static bool
38449 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38451 cp_token *pragma_tok;
38452 unsigned int id;
38453 tree stmt;
38454 bool ret;
38456 pragma_tok = cp_lexer_consume_token (parser->lexer);
38457 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38458 parser->lexer->in_pragma = true;
38460 id = cp_parser_pragma_kind (pragma_tok);
38461 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38462 cp_ensure_no_omp_declare_simd (parser);
38463 switch (id)
38465 case PRAGMA_GCC_PCH_PREPROCESS:
38466 error_at (pragma_tok->location,
38467 "%<#pragma GCC pch_preprocess%> must be first");
38468 break;
38470 case PRAGMA_OMP_BARRIER:
38471 switch (context)
38473 case pragma_compound:
38474 cp_parser_omp_barrier (parser, pragma_tok);
38475 return false;
38476 case pragma_stmt:
38477 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38478 "used in compound statements", "omp barrier");
38479 break;
38480 default:
38481 goto bad_stmt;
38483 break;
38485 case PRAGMA_OMP_FLUSH:
38486 switch (context)
38488 case pragma_compound:
38489 cp_parser_omp_flush (parser, pragma_tok);
38490 return false;
38491 case pragma_stmt:
38492 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38493 "used in compound statements", "omp flush");
38494 break;
38495 default:
38496 goto bad_stmt;
38498 break;
38500 case PRAGMA_OMP_TASKWAIT:
38501 switch (context)
38503 case pragma_compound:
38504 cp_parser_omp_taskwait (parser, pragma_tok);
38505 return false;
38506 case pragma_stmt:
38507 error_at (pragma_tok->location,
38508 "%<#pragma %s%> may only be used in compound statements",
38509 "omp taskwait");
38510 break;
38511 default:
38512 goto bad_stmt;
38514 break;
38516 case PRAGMA_OMP_TASKYIELD:
38517 switch (context)
38519 case pragma_compound:
38520 cp_parser_omp_taskyield (parser, pragma_tok);
38521 return false;
38522 case pragma_stmt:
38523 error_at (pragma_tok->location,
38524 "%<#pragma %s%> may only be used in compound statements",
38525 "omp taskyield");
38526 break;
38527 default:
38528 goto bad_stmt;
38530 break;
38532 case PRAGMA_OMP_CANCEL:
38533 switch (context)
38535 case pragma_compound:
38536 cp_parser_omp_cancel (parser, pragma_tok);
38537 return false;
38538 case pragma_stmt:
38539 error_at (pragma_tok->location,
38540 "%<#pragma %s%> may only be used in compound statements",
38541 "omp cancel");
38542 break;
38543 default:
38544 goto bad_stmt;
38546 break;
38548 case PRAGMA_OMP_CANCELLATION_POINT:
38549 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38550 return false;
38552 case PRAGMA_OMP_THREADPRIVATE:
38553 cp_parser_omp_threadprivate (parser, pragma_tok);
38554 return false;
38556 case PRAGMA_OMP_DECLARE:
38557 return cp_parser_omp_declare (parser, pragma_tok, context);
38559 case PRAGMA_OACC_DECLARE:
38560 cp_parser_oacc_declare (parser, pragma_tok);
38561 return false;
38563 case PRAGMA_OACC_ENTER_DATA:
38564 if (context == pragma_stmt)
38566 error_at (pragma_tok->location,
38567 "%<#pragma %s%> may only be used in compound statements",
38568 "acc enter data");
38569 break;
38571 else if (context != pragma_compound)
38572 goto bad_stmt;
38573 cp_parser_omp_construct (parser, pragma_tok, if_p);
38574 return true;
38576 case PRAGMA_OACC_EXIT_DATA:
38577 if (context == pragma_stmt)
38579 error_at (pragma_tok->location,
38580 "%<#pragma %s%> may only be used in compound statements",
38581 "acc exit data");
38582 break;
38584 else if (context != pragma_compound)
38585 goto bad_stmt;
38586 cp_parser_omp_construct (parser, pragma_tok, if_p);
38587 return true;
38589 case PRAGMA_OACC_ROUTINE:
38590 if (context != pragma_external)
38592 error_at (pragma_tok->location,
38593 "%<#pragma acc routine%> must be at file scope");
38594 break;
38596 cp_parser_oacc_routine (parser, pragma_tok, context);
38597 return false;
38599 case PRAGMA_OACC_UPDATE:
38600 if (context == pragma_stmt)
38602 error_at (pragma_tok->location,
38603 "%<#pragma %s%> may only be used in compound statements",
38604 "acc update");
38605 break;
38607 else if (context != pragma_compound)
38608 goto bad_stmt;
38609 cp_parser_omp_construct (parser, pragma_tok, if_p);
38610 return true;
38612 case PRAGMA_OACC_WAIT:
38613 if (context == pragma_stmt)
38615 error_at (pragma_tok->location,
38616 "%<#pragma %s%> may only be used in compound statements",
38617 "acc wait");
38618 break;
38620 else if (context != pragma_compound)
38621 goto bad_stmt;
38622 cp_parser_omp_construct (parser, pragma_tok, if_p);
38623 return true;
38625 case PRAGMA_OACC_ATOMIC:
38626 case PRAGMA_OACC_CACHE:
38627 case PRAGMA_OACC_DATA:
38628 case PRAGMA_OACC_HOST_DATA:
38629 case PRAGMA_OACC_KERNELS:
38630 case PRAGMA_OACC_PARALLEL:
38631 case PRAGMA_OACC_LOOP:
38632 case PRAGMA_OMP_ATOMIC:
38633 case PRAGMA_OMP_CRITICAL:
38634 case PRAGMA_OMP_DISTRIBUTE:
38635 case PRAGMA_OMP_FOR:
38636 case PRAGMA_OMP_MASTER:
38637 case PRAGMA_OMP_PARALLEL:
38638 case PRAGMA_OMP_SECTIONS:
38639 case PRAGMA_OMP_SIMD:
38640 case PRAGMA_OMP_SINGLE:
38641 case PRAGMA_OMP_TASK:
38642 case PRAGMA_OMP_TASKGROUP:
38643 case PRAGMA_OMP_TASKLOOP:
38644 case PRAGMA_OMP_TEAMS:
38645 if (context != pragma_stmt && context != pragma_compound)
38646 goto bad_stmt;
38647 stmt = push_omp_privatization_clauses (false);
38648 cp_parser_omp_construct (parser, pragma_tok, if_p);
38649 pop_omp_privatization_clauses (stmt);
38650 return true;
38652 case PRAGMA_OMP_ORDERED:
38653 if (context != pragma_stmt && context != pragma_compound)
38654 goto bad_stmt;
38655 stmt = push_omp_privatization_clauses (false);
38656 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38657 pop_omp_privatization_clauses (stmt);
38658 return ret;
38660 case PRAGMA_OMP_TARGET:
38661 if (context != pragma_stmt && context != pragma_compound)
38662 goto bad_stmt;
38663 stmt = push_omp_privatization_clauses (false);
38664 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38665 pop_omp_privatization_clauses (stmt);
38666 return ret;
38668 case PRAGMA_OMP_END_DECLARE_TARGET:
38669 cp_parser_omp_end_declare_target (parser, pragma_tok);
38670 return false;
38672 case PRAGMA_OMP_SECTION:
38673 error_at (pragma_tok->location,
38674 "%<#pragma omp section%> may only be used in "
38675 "%<#pragma omp sections%> construct");
38676 break;
38678 case PRAGMA_IVDEP:
38680 if (context == pragma_external)
38682 error_at (pragma_tok->location,
38683 "%<#pragma GCC ivdep%> must be inside a function");
38684 break;
38686 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38687 unsigned short unroll;
38688 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38689 if (tok->type == CPP_PRAGMA
38690 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38692 tok = cp_lexer_consume_token (parser->lexer);
38693 unroll = cp_parser_pragma_unroll (parser, tok);
38694 tok = cp_lexer_peek_token (the_parser->lexer);
38696 else
38697 unroll = 0;
38698 if (tok->type != CPP_KEYWORD
38699 || (tok->keyword != RID_FOR
38700 && tok->keyword != RID_WHILE
38701 && tok->keyword != RID_DO))
38703 cp_parser_error (parser, "for, while or do statement expected");
38704 return false;
38706 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38707 return true;
38710 case PRAGMA_UNROLL:
38712 if (context == pragma_external)
38714 error_at (pragma_tok->location,
38715 "%<#pragma GCC unroll%> must be inside a function");
38716 break;
38718 const unsigned short unroll
38719 = cp_parser_pragma_unroll (parser, pragma_tok);
38720 bool ivdep;
38721 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38722 if (tok->type == CPP_PRAGMA
38723 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38725 tok = cp_lexer_consume_token (parser->lexer);
38726 ivdep = cp_parser_pragma_ivdep (parser, tok);
38727 tok = cp_lexer_peek_token (the_parser->lexer);
38729 else
38730 ivdep = false;
38731 if (tok->type != CPP_KEYWORD
38732 || (tok->keyword != RID_FOR
38733 && tok->keyword != RID_WHILE
38734 && tok->keyword != RID_DO))
38736 cp_parser_error (parser, "for, while or do statement expected");
38737 return false;
38739 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38740 return true;
38743 default:
38744 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38745 c_invoke_pragma_handler (id);
38746 break;
38748 bad_stmt:
38749 cp_parser_error (parser, "expected declaration specifiers");
38750 break;
38753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38754 return false;
38757 /* The interface the pragma parsers have to the lexer. */
38759 enum cpp_ttype
38760 pragma_lex (tree *value, location_t *loc)
38762 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38763 enum cpp_ttype ret = tok->type;
38765 *value = tok->u.value;
38766 if (loc)
38767 *loc = tok->location;
38769 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38770 ret = CPP_EOF;
38771 else if (ret == CPP_STRING)
38772 *value = cp_parser_string_literal (the_parser, false, false);
38773 else
38775 if (ret == CPP_KEYWORD)
38776 ret = CPP_NAME;
38777 cp_lexer_consume_token (the_parser->lexer);
38780 return ret;
38784 /* External interface. */
38786 /* Parse one entire translation unit. */
38788 void
38789 c_parse_file (void)
38791 static bool already_called = false;
38793 if (already_called)
38794 fatal_error (input_location,
38795 "inter-module optimizations not implemented for C++");
38796 already_called = true;
38798 the_parser = cp_parser_new ();
38799 push_deferring_access_checks (flag_access_control
38800 ? dk_no_deferred : dk_no_check);
38801 cp_parser_translation_unit (the_parser);
38802 the_parser = NULL;
38805 /* Create an identifier for a generic parameter type (a synthesized
38806 template parameter implied by `auto' or a concept identifier). */
38808 static GTY(()) int generic_parm_count;
38809 static tree
38810 make_generic_type_name ()
38812 char buf[32];
38813 sprintf (buf, "auto:%d", ++generic_parm_count);
38814 return get_identifier (buf);
38817 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38818 (creating a new template parameter list if necessary). Returns the newly
38819 created template type parm. */
38821 static tree
38822 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38824 gcc_assert (current_binding_level->kind == sk_function_parms);
38826 /* Before committing to modifying any scope, if we're in an
38827 implicit template scope, and we're trying to synthesize a
38828 constrained parameter, try to find a previous parameter with
38829 the same name. This is the same-type rule for abbreviated
38830 function templates.
38832 NOTE: We can generate implicit parameters when tentatively
38833 parsing a nested name specifier, only to reject that parse
38834 later. However, matching the same template-id as part of a
38835 direct-declarator should generate an identical template
38836 parameter, so this rule will merge them. */
38837 if (parser->implicit_template_scope && constr)
38839 tree t = parser->implicit_template_parms;
38840 while (t)
38842 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38844 tree d = TREE_VALUE (t);
38845 if (TREE_CODE (d) == PARM_DECL)
38846 /* Return the TEMPLATE_PARM_INDEX. */
38847 d = DECL_INITIAL (d);
38848 return d;
38850 t = TREE_CHAIN (t);
38854 /* We are either continuing a function template that already contains implicit
38855 template parameters, creating a new fully-implicit function template, or
38856 extending an existing explicit function template with implicit template
38857 parameters. */
38859 cp_binding_level *const entry_scope = current_binding_level;
38861 bool become_template = false;
38862 cp_binding_level *parent_scope = 0;
38864 if (parser->implicit_template_scope)
38866 gcc_assert (parser->implicit_template_parms);
38868 current_binding_level = parser->implicit_template_scope;
38870 else
38872 /* Roll back to the existing template parameter scope (in the case of
38873 extending an explicit function template) or introduce a new template
38874 parameter scope ahead of the function parameter scope (or class scope
38875 in the case of out-of-line member definitions). The function scope is
38876 added back after template parameter synthesis below. */
38878 cp_binding_level *scope = entry_scope;
38880 while (scope->kind == sk_function_parms)
38882 parent_scope = scope;
38883 scope = scope->level_chain;
38885 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38887 /* If not defining a class, then any class scope is a scope level in
38888 an out-of-line member definition. In this case simply wind back
38889 beyond the first such scope to inject the template parameter list.
38890 Otherwise wind back to the class being defined. The latter can
38891 occur in class member friend declarations such as:
38893 class A {
38894 void foo (auto);
38896 class B {
38897 friend void A::foo (auto);
38900 The template parameter list synthesized for the friend declaration
38901 must be injected in the scope of 'B'. This can also occur in
38902 erroneous cases such as:
38904 struct A {
38905 struct B {
38906 void foo (auto);
38908 void B::foo (auto) {}
38911 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38912 but, nevertheless, the template parameter list synthesized for the
38913 declarator should be injected into the scope of 'A' as if the
38914 ill-formed template was specified explicitly. */
38916 while (scope->kind == sk_class && !scope->defining_class_p)
38918 parent_scope = scope;
38919 scope = scope->level_chain;
38923 current_binding_level = scope;
38925 if (scope->kind != sk_template_parms
38926 || !function_being_declared_is_template_p (parser))
38928 /* Introduce a new template parameter list for implicit template
38929 parameters. */
38931 become_template = true;
38933 parser->implicit_template_scope
38934 = begin_scope (sk_template_parms, NULL);
38936 ++processing_template_decl;
38938 parser->fully_implicit_function_template_p = true;
38939 ++parser->num_template_parameter_lists;
38941 else
38943 /* Synthesize implicit template parameters at the end of the explicit
38944 template parameter list. */
38946 gcc_assert (current_template_parms);
38948 parser->implicit_template_scope = scope;
38950 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38951 parser->implicit_template_parms
38952 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38956 /* Synthesize a new template parameter and track the current template
38957 parameter chain with implicit_template_parms. */
38959 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38960 tree synth_id = make_generic_type_name ();
38961 tree synth_tmpl_parm;
38962 bool non_type = false;
38964 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38965 synth_tmpl_parm
38966 = finish_template_type_parm (class_type_node, synth_id);
38967 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38968 synth_tmpl_parm
38969 = finish_constrained_template_template_parm (proto, synth_id);
38970 else
38972 synth_tmpl_parm = copy_decl (proto);
38973 DECL_NAME (synth_tmpl_parm) = synth_id;
38974 non_type = true;
38977 // Attach the constraint to the parm before processing.
38978 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38979 TREE_TYPE (node) = constr;
38980 tree new_parm
38981 = process_template_parm (parser->implicit_template_parms,
38982 input_location,
38983 node,
38984 /*non_type=*/non_type,
38985 /*param_pack=*/false);
38987 // Chain the new parameter to the list of implicit parameters.
38988 if (parser->implicit_template_parms)
38989 parser->implicit_template_parms
38990 = TREE_CHAIN (parser->implicit_template_parms);
38991 else
38992 parser->implicit_template_parms = new_parm;
38994 tree new_decl = get_local_decls ();
38995 if (non_type)
38996 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38997 new_decl = DECL_INITIAL (new_decl);
38999 /* If creating a fully implicit function template, start the new implicit
39000 template parameter list with this synthesized type, otherwise grow the
39001 current template parameter list. */
39003 if (become_template)
39005 parent_scope->level_chain = current_binding_level;
39007 tree new_parms = make_tree_vec (1);
39008 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39009 current_template_parms = tree_cons (size_int (processing_template_decl),
39010 new_parms, current_template_parms);
39012 else
39014 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39015 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39016 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39017 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39020 // If the new parameter was constrained, we need to add that to the
39021 // constraints in the template parameter list.
39022 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39024 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39025 reqs = conjoin_constraints (reqs, req);
39026 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39029 current_binding_level = entry_scope;
39031 return new_decl;
39034 /* Finish the declaration of a fully implicit function template. Such a
39035 template has no explicit template parameter list so has not been through the
39036 normal template head and tail processing. synthesize_implicit_template_parm
39037 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39038 provided if the declaration is a class member such that its template
39039 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39040 form is returned. Otherwise NULL_TREE is returned. */
39042 static tree
39043 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39045 gcc_assert (parser->fully_implicit_function_template_p);
39047 if (member_decl_opt && member_decl_opt != error_mark_node
39048 && DECL_VIRTUAL_P (member_decl_opt))
39050 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39051 "implicit templates may not be %<virtual%>");
39052 DECL_VIRTUAL_P (member_decl_opt) = false;
39055 if (member_decl_opt)
39056 member_decl_opt = finish_member_template_decl (member_decl_opt);
39057 end_template_decl ();
39059 parser->fully_implicit_function_template_p = false;
39060 --parser->num_template_parameter_lists;
39062 return member_decl_opt;
39065 /* Helper function for diagnostics that have complained about things
39066 being used with 'extern "C"' linkage.
39068 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39070 void
39071 maybe_show_extern_c_location (void)
39073 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39074 inform (the_parser->innermost_linkage_specification_location,
39075 "%<extern \"C\"%> linkage started here");
39078 #include "gt-cp-parser.h"