PR c/81544 - attribute noreturn and warn_unused_result on the same function accepted
[official-gcc.git] / gcc / cp / parser.c
blob891f742eb2159c642abde71faf6f92f58cb1c94e
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_block_declaration
2147 (cp_parser *, bool);
2148 static void cp_parser_simple_declaration
2149 (cp_parser *, bool, tree *);
2150 static void cp_parser_decl_specifier_seq
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2152 static tree cp_parser_storage_class_specifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_function_specifier_opt
2155 (cp_parser *, cp_decl_specifier_seq *);
2156 static tree cp_parser_type_specifier
2157 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2158 int *, bool *);
2159 static tree cp_parser_simple_type_specifier
2160 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2161 static tree cp_parser_type_name
2162 (cp_parser *, bool);
2163 static tree cp_parser_type_name
2164 (cp_parser *);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2206 bool, bool, int, bool *, tree *, location_t *, tree *);
2207 static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2209 static cp_declarator *cp_parser_direct_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2211 static enum tree_code cp_parser_ptr_operator
2212 (cp_parser *, tree *, cp_cv_quals *, tree *);
2213 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2214 (cp_parser *);
2215 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2216 (cp_parser *);
2217 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2218 (cp_parser *);
2219 static tree cp_parser_tx_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_late_return_type_opt
2222 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2223 static tree cp_parser_declarator_id
2224 (cp_parser *, bool);
2225 static tree cp_parser_type_id
2226 (cp_parser *);
2227 static tree cp_parser_template_type_arg
2228 (cp_parser *);
2229 static tree cp_parser_trailing_type_id (cp_parser *);
2230 static tree cp_parser_type_id_1
2231 (cp_parser *, bool, bool);
2232 static void cp_parser_type_specifier_seq
2233 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2234 static tree cp_parser_parameter_declaration_clause
2235 (cp_parser *);
2236 static tree cp_parser_parameter_declaration_list
2237 (cp_parser *, bool *);
2238 static cp_parameter_declarator *cp_parser_parameter_declaration
2239 (cp_parser *, bool, bool *);
2240 static tree cp_parser_default_argument
2241 (cp_parser *, bool);
2242 static void cp_parser_function_body
2243 (cp_parser *, bool);
2244 static tree cp_parser_initializer
2245 (cp_parser *, bool *, bool *);
2246 static cp_expr cp_parser_initializer_clause
2247 (cp_parser *, bool *);
2248 static cp_expr cp_parser_braced_list
2249 (cp_parser*, bool*);
2250 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2251 (cp_parser *, bool *);
2253 static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser *, bool);
2256 static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser *, tree);
2259 static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser *, tree);
2262 static tree synthesize_implicit_template_parm
2263 (cp_parser *, tree);
2264 static tree finish_fully_implicit_template
2265 (cp_parser *, tree);
2267 /* Classes [gram.class] */
2269 static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2272 (cp_parser *);
2273 static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275 static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277 static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279 static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281 static void cp_parser_member_declaration
2282 (cp_parser *);
2283 static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285 static tree cp_parser_constant_initializer
2286 (cp_parser *);
2288 /* Derived classes [gram.class.derived] */
2290 static tree cp_parser_base_clause
2291 (cp_parser *);
2292 static tree cp_parser_base_specifier
2293 (cp_parser *);
2295 /* Special member functions [gram.special] */
2297 static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299 static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301 static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303 static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305 static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309 static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2312 /* Overloading [gram.over] */
2314 static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316 static cp_expr cp_parser_operator
2317 (cp_parser *);
2319 /* Templates [gram.temp] */
2321 static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323 static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325 static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333 static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335 static tree cp_parser_template_argument
2336 (cp_parser *);
2337 static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339 static void cp_parser_explicit_specialization
2340 (cp_parser *);
2342 /* Exception handling [gram.exception] */
2344 static tree cp_parser_try_block
2345 (cp_parser *);
2346 static void cp_parser_function_try_block
2347 (cp_parser *);
2348 static void cp_parser_handler_seq
2349 (cp_parser *);
2350 static void cp_parser_handler
2351 (cp_parser *);
2352 static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354 static tree cp_parser_throw_expression
2355 (cp_parser *);
2356 static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_type_id_list
2359 (cp_parser *);
2361 /* GNU Extensions */
2363 static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369 static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383 static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391 static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393 static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2645 /* Concept-related syntactic transformations */
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2674 return parser->context->next != NULL;
2677 /* Returns nonzero if TOKEN is a string literal. */
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2708 return token->keyword == keyword;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2760 return true;
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2769 switch (token_desc)
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2787 switch (token_desc)
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2843 if (token->type == CPP_PRAGMA)
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2859 error_at (loc, "version control conflict marker in file");
2860 return;
2864 gcc_rich_location richloc (input_location);
2866 bool added_matching_location = false;
2868 if (missing_token_desc != RT_NONE)
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser->lexer)->location;
2876 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location != UNKNOWN_LOCATION)
2882 added_matching_location
2883 = richloc.add_location_if_nearby (matching_location);
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2890 identifiers. */
2891 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2892 token->u.value, token->flags, &richloc);
2894 if (missing_token_desc != RT_NONE)
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location != UNKNOWN_LOCATION
2899 && !added_matching_location)
2900 inform (matching_location, "to match this %qs",
2901 get_matching_symbol (missing_token_desc));
2905 /* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2909 OTHER-TOKEN". */
2911 static void
2912 cp_parser_error (cp_parser* parser, const char* gmsgid)
2914 if (!cp_parser_simulate_error (parser))
2915 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2918 /* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2923 static void
2924 cp_parser_name_lookup_error (cp_parser* parser,
2925 tree name,
2926 tree decl,
2927 name_lookup_error desired,
2928 location_t location)
2930 /* If name lookup completely failed, tell the user that NAME was not
2931 declared. */
2932 if (decl == error_mark_node)
2934 if (parser->scope && parser->scope != global_namespace)
2935 error_at (location, "%<%E::%E%> has not been declared",
2936 parser->scope, name);
2937 else if (parser->scope == global_namespace)
2938 error_at (location, "%<::%E%> has not been declared", name);
2939 else if (parser->object_scope
2940 && !CLASS_TYPE_P (parser->object_scope))
2941 error_at (location, "request for member %qE in non-class type %qT",
2942 name, parser->object_scope);
2943 else if (parser->object_scope)
2944 error_at (location, "%<%T::%E%> has not been declared",
2945 parser->object_scope, name);
2946 else
2947 error_at (location, "%qE has not been declared", name);
2949 else if (parser->scope && parser->scope != global_namespace)
2951 switch (desired)
2953 case NLE_TYPE:
2954 error_at (location, "%<%E::%E%> is not a type",
2955 parser->scope, name);
2956 break;
2957 case NLE_CXX98:
2958 error_at (location, "%<%E::%E%> is not a class or namespace",
2959 parser->scope, name);
2960 break;
2961 case NLE_NOT_CXX98:
2962 error_at (location,
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser->scope, name);
2965 break;
2966 default:
2967 gcc_unreachable ();
2971 else if (parser->scope == global_namespace)
2973 switch (desired)
2975 case NLE_TYPE:
2976 error_at (location, "%<::%E%> is not a type", name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<::%E%> is not a class or namespace", name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<::%E%> is not a class, namespace, or enumeration",
2984 name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2990 else
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%qE is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%qE is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%qE is not a class, namespace, or enumeration", name);
3003 break;
3004 default:
3005 gcc_unreachable ();
3010 /* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3014 static bool
3015 cp_parser_simulate_error (cp_parser* parser)
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3019 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3020 return true;
3022 return false;
3025 /* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3027 issued. */
3029 static bool
3030 cp_parser_check_type_definition (cp_parser* parser)
3032 /* If types are forbidden here, issue a message. */
3033 if (parser->type_definition_forbidden_message)
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser->type_definition_forbidden_message);
3038 return false;
3040 return true;
3043 /* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3049 static void
3050 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3051 tree type, location_t type_location)
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3056 while (declarator
3057 && (declarator->kind == cdk_pointer
3058 || declarator->kind == cdk_reference
3059 || declarator->kind == cdk_ptrmem))
3060 declarator = declarator->declarator;
3061 if (declarator
3062 && declarator->kind == cdk_function)
3064 error_at (type_location,
3065 "new types may not be defined in a return type");
3066 inform (type_location,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3068 type);
3072 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3078 static void
3079 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3080 tree type,
3081 enum tag_types tag_type,
3082 location_t location)
3084 cp_token_position start = 0;
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3088 if (TREE_CODE (type) == TYPE_DECL)
3089 type = TREE_TYPE (type);
3090 if (TYPE_P (type) && !template_placeholder_p (type))
3091 error_at (location, "%qT is not a template", type);
3092 else if (identifier_p (type))
3094 if (tag_type != none_type)
3095 error_at (location, "%qE is not a class template", type);
3096 else
3097 error_at (location, "%qE is not a template", type);
3099 else
3100 error_at (location, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3103 start = cp_lexer_token_position (parser->lexer, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3110 if (start)
3111 cp_lexer_purge_tokens_after (parser->lexer, start);
3115 /* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3120 static bool
3121 cp_parser_non_integral_constant_expression (cp_parser *parser,
3122 non_integral_constant thing)
3124 parser->non_integral_constant_expression_p = true;
3125 if (parser->integral_constant_expression_p)
3127 if (!parser->allow_non_integral_constant_expression_p)
3129 const char *msg = NULL;
3130 switch (thing)
3132 case NIC_FLOAT:
3133 pedwarn (input_location, OPT_Wpedantic,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3136 return true;
3137 case NIC_CAST:
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3141 return true;
3142 case NIC_TYPEID:
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3145 return true;
3146 case NIC_NCC:
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3149 return true;
3150 case NIC_FUNC_CALL:
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3153 return true;
3154 case NIC_INC:
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_DEC:
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_ARRAY_REF:
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_ADDR_LABEL:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_OVERLOADED:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ASSIGNMENT:
3175 error ("an assignment cannot appear in a constant-expression");
3176 return true;
3177 case NIC_COMMA:
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_CONSTRUCTOR:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_TRANSACTION:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_THIS:
3190 msg = "this";
3191 break;
3192 case NIC_FUNC_NAME:
3193 msg = "__FUNCTION__";
3194 break;
3195 case NIC_PRETTY_FUNC:
3196 msg = "__PRETTY_FUNCTION__";
3197 break;
3198 case NIC_C99_FUNC:
3199 msg = "__func__";
3200 break;
3201 case NIC_VA_ARG:
3202 msg = "va_arg";
3203 break;
3204 case NIC_ARROW:
3205 msg = "->";
3206 break;
3207 case NIC_POINT:
3208 msg = ".";
3209 break;
3210 case NIC_STAR:
3211 msg = "*";
3212 break;
3213 case NIC_ADDR:
3214 msg = "&";
3215 break;
3216 case NIC_PREINCREMENT:
3217 msg = "++";
3218 break;
3219 case NIC_PREDECREMENT:
3220 msg = "--";
3221 break;
3222 case NIC_NEW:
3223 msg = "new";
3224 break;
3225 case NIC_DEL:
3226 msg = "delete";
3227 break;
3228 default:
3229 gcc_unreachable ();
3231 if (msg)
3232 error ("%qs cannot appear in a constant-expression", msg);
3233 return true;
3236 return false;
3239 /* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3244 static void
3245 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3246 location_t location)
3248 tree decl, ambiguous_decls;
3249 cp_parser_commit_to_tentative_parse (parser);
3250 /* Try to lookup the identifier. */
3251 decl = cp_parser_lookup_name (parser, id, none_type,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls, location);
3256 if (ambiguous_decls)
3257 /* If the lookup was ambiguous, an error will already have
3258 been issued. */
3259 return;
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl))
3264 error_at (location,
3265 "invalid use of template-name %qE without an argument list",
3266 decl);
3267 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3268 inform (location, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3272 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3273 error_at (location, "invalid use of destructor %qD as a type", id);
3274 else if (TREE_CODE (decl) == TYPE_DECL)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location, "invalid combination of multiple type-specifiers");
3277 else if (!parser->scope)
3279 /* Issue an error message. */
3280 name_hint hint;
3281 if (TREE_CODE (id) == IDENTIFIER_NODE)
3282 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3283 if (hint)
3285 gcc_rich_location richloc (location);
3286 richloc.add_fixit_replace (hint.suggestion ());
3287 error_at (&richloc,
3288 "%qE does not name a type; did you mean %qs?",
3289 id, hint.suggestion ());
3291 else
3292 error_at (location, "%qE does not name a type", id);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3301 inform (location, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3304 inform (location, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11
3307 && TREE_CODE (id) == IDENTIFIER_NODE
3308 && id_equal (id, "thread_local"))
3309 inform (location, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3312 inform (location, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl && current_class_type
3314 && TYPE_BINFO (current_class_type))
3316 tree b;
3318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3320 b = TREE_CHAIN (b))
3322 tree base_type = BINFO_TYPE (b);
3323 if (CLASS_TYPE_P (base_type)
3324 && dependent_type_p (base_type))
3326 tree field;
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3331 for (field = TYPE_FIELDS (base_type);
3332 field;
3333 field = DECL_CHAIN (field))
3334 if (TREE_CODE (field) == TYPE_DECL
3335 && DECL_NAME (field) == id)
3337 inform (location,
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b), id);
3340 break;
3342 if (field)
3343 break;
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser->scope != error_mark_node)
3352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3355 error_at (location_of (id),
3356 "%qE in namespace %qE does not name a template type",
3357 id, parser->scope);
3358 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3359 error_at (location_of (id),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id, 0), parser->scope);
3362 else
3363 error_at (location_of (id),
3364 "%qE in namespace %qE does not name a type",
3365 id, parser->scope);
3366 if (DECL_P (decl))
3367 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3368 else if (decl == error_mark_node)
3369 suggest_alternative_in_explicit_scope (location, id,
3370 parser->scope);
3372 else if (CLASS_TYPE_P (parser->scope)
3373 && constructor_name_p (id, parser->scope))
3375 /* A<T>::A<T>() */
3376 error_at (location, "%<%T::%E%> names the constructor, not"
3377 " the type", parser->scope, id);
3378 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3379 error_at (location, "and %qT has no template constructors",
3380 parser->scope);
3382 else if (TYPE_P (parser->scope)
3383 && dependent_scope_p (parser->scope))
3385 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3386 error_at (location,
3387 "need %<typename%> before %<%T::%D::%E%> because "
3388 "%<%T::%D%> is a dependent scope",
3389 TYPE_CONTEXT (parser->scope),
3390 TYPENAME_TYPE_FULLNAME (parser->scope),
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope));
3394 else
3395 error_at (location, "need %<typename%> before %<%T::%E%> because "
3396 "%qT is a dependent scope",
3397 parser->scope, id, parser->scope);
3399 else if (TYPE_P (parser->scope))
3401 if (!COMPLETE_TYPE_P (parser->scope))
3402 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3403 parser->scope);
3404 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3405 error_at (location_of (id),
3406 "%qE in %q#T does not name a template type",
3407 id, parser->scope);
3408 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3409 error_at (location_of (id),
3410 "%qE in %q#T does not name a template type",
3411 TREE_OPERAND (id, 0), parser->scope);
3412 else
3413 error_at (location_of (id),
3414 "%qE in %q#T does not name a type",
3415 id, parser->scope);
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3419 else
3420 gcc_unreachable ();
3424 /* Check for a common situation where a type-name should be present,
3425 but is not, and issue a sensible error message. Returns true if an
3426 invalid type-name was detected.
3428 The situation handled by this function are variable declarations of the
3429 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3430 Usually, `ID' should name a type, but if we got here it means that it
3431 does not. We try to emit the best possible error message depending on
3432 how exactly the id-expression looks like. */
3434 static bool
3435 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3437 tree id;
3438 cp_token *token = cp_lexer_peek_token (parser->lexer);
3440 /* Avoid duplicate error about ambiguous lookup. */
3441 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3443 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3444 if (next->type == CPP_NAME && next->error_reported)
3445 goto out;
3448 cp_parser_parse_tentatively (parser);
3449 id = cp_parser_id_expression (parser,
3450 /*template_keyword_p=*/false,
3451 /*check_dependency_p=*/true,
3452 /*template_p=*/NULL,
3453 /*declarator_p=*/true,
3454 /*optional_p=*/false);
3455 /* If the next token is a (, this is a function with no explicit return
3456 type, i.e. constructor, destructor or conversion op. */
3457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3458 || TREE_CODE (id) == TYPE_DECL)
3460 cp_parser_abort_tentative_parse (parser);
3461 return false;
3463 if (!cp_parser_parse_definitely (parser))
3464 return false;
3466 /* Emit a diagnostic for the invalid type. */
3467 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3468 out:
3469 /* If we aren't in the middle of a declarator (i.e. in a
3470 parameter-declaration-clause), skip to the end of the declaration;
3471 there's no point in trying to process it. */
3472 if (!parser->in_declarator_p)
3473 cp_parser_skip_to_end_of_block_or_statement (parser);
3474 return true;
3477 /* Consume tokens up to, and including, the next non-nested closing `)'.
3478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3479 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3480 found an unnested token of that type. */
3482 static int
3483 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3484 bool recovering,
3485 cpp_ttype or_ttype,
3486 bool consume_paren)
3488 unsigned paren_depth = 0;
3489 unsigned brace_depth = 0;
3490 unsigned square_depth = 0;
3492 if (recovering && or_ttype == CPP_EOF
3493 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3494 return 0;
3496 while (true)
3498 cp_token * token = cp_lexer_peek_token (parser->lexer);
3500 /* Have we found what we're looking for before the closing paren? */
3501 if (token->type == or_ttype && or_ttype != CPP_EOF
3502 && !brace_depth && !paren_depth && !square_depth)
3503 return -1;
3505 switch (token->type)
3507 case CPP_EOF:
3508 case CPP_PRAGMA_EOL:
3509 /* If we've run out of tokens, then there is no closing `)'. */
3510 return 0;
3512 /* This is good for lambda expression capture-lists. */
3513 case CPP_OPEN_SQUARE:
3514 ++square_depth;
3515 break;
3516 case CPP_CLOSE_SQUARE:
3517 if (!square_depth--)
3518 return 0;
3519 break;
3521 case CPP_SEMICOLON:
3522 /* This matches the processing in skip_to_end_of_statement. */
3523 if (!brace_depth)
3524 return 0;
3525 break;
3527 case CPP_OPEN_BRACE:
3528 ++brace_depth;
3529 break;
3530 case CPP_CLOSE_BRACE:
3531 if (!brace_depth--)
3532 return 0;
3533 break;
3535 case CPP_OPEN_PAREN:
3536 if (!brace_depth)
3537 ++paren_depth;
3538 break;
3540 case CPP_CLOSE_PAREN:
3541 if (!brace_depth && !paren_depth--)
3543 if (consume_paren)
3544 cp_lexer_consume_token (parser->lexer);
3545 return 1;
3547 break;
3549 default:
3550 break;
3553 /* Consume the token. */
3554 cp_lexer_consume_token (parser->lexer);
3558 /* Consume tokens up to, and including, the next non-nested closing `)'.
3559 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3560 are doing error recovery. Returns -1 if OR_COMMA is true and we
3561 found an unnested token of that type. */
3563 static int
3564 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3565 bool recovering,
3566 bool or_comma,
3567 bool consume_paren)
3569 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3570 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3571 ttype, consume_paren);
3574 /* Consume tokens until we reach the end of the current statement.
3575 Normally, that will be just before consuming a `;'. However, if a
3576 non-nested `}' comes first, then we stop before consuming that. */
3578 static void
3579 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3581 unsigned nesting_depth = 0;
3583 /* Unwind generic function template scope if necessary. */
3584 if (parser->fully_implicit_function_template_p)
3585 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3587 while (true)
3589 cp_token *token = cp_lexer_peek_token (parser->lexer);
3591 switch (token->type)
3593 case CPP_EOF:
3594 case CPP_PRAGMA_EOL:
3595 /* If we've run out of tokens, stop. */
3596 return;
3598 case CPP_SEMICOLON:
3599 /* If the next token is a `;', we have reached the end of the
3600 statement. */
3601 if (!nesting_depth)
3602 return;
3603 break;
3605 case CPP_CLOSE_BRACE:
3606 /* If this is a non-nested '}', stop before consuming it.
3607 That way, when confronted with something like:
3609 { 3 + }
3611 we stop before consuming the closing '}', even though we
3612 have not yet reached a `;'. */
3613 if (nesting_depth == 0)
3614 return;
3616 /* If it is the closing '}' for a block that we have
3617 scanned, stop -- but only after consuming the token.
3618 That way given:
3620 void f g () { ... }
3621 typedef int I;
3623 we will stop after the body of the erroneously declared
3624 function, but before consuming the following `typedef'
3625 declaration. */
3626 if (--nesting_depth == 0)
3628 cp_lexer_consume_token (parser->lexer);
3629 return;
3631 break;
3633 case CPP_OPEN_BRACE:
3634 ++nesting_depth;
3635 break;
3637 default:
3638 break;
3641 /* Consume the token. */
3642 cp_lexer_consume_token (parser->lexer);
3646 /* This function is called at the end of a statement or declaration.
3647 If the next token is a semicolon, it is consumed; otherwise, error
3648 recovery is attempted. */
3650 static void
3651 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3653 /* Look for the trailing `;'. */
3654 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3656 /* If there is additional (erroneous) input, skip to the end of
3657 the statement. */
3658 cp_parser_skip_to_end_of_statement (parser);
3659 /* If the next token is now a `;', consume it. */
3660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3661 cp_lexer_consume_token (parser->lexer);
3665 /* Skip tokens until we have consumed an entire block, or until we
3666 have consumed a non-nested `;'. */
3668 static void
3669 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3671 int nesting_depth = 0;
3673 /* Unwind generic function template scope if necessary. */
3674 if (parser->fully_implicit_function_template_p)
3675 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3677 while (nesting_depth >= 0)
3679 cp_token *token = cp_lexer_peek_token (parser->lexer);
3681 switch (token->type)
3683 case CPP_EOF:
3684 case CPP_PRAGMA_EOL:
3685 /* If we've run out of tokens, stop. */
3686 return;
3688 case CPP_SEMICOLON:
3689 /* Stop if this is an unnested ';'. */
3690 if (!nesting_depth)
3691 nesting_depth = -1;
3692 break;
3694 case CPP_CLOSE_BRACE:
3695 /* Stop if this is an unnested '}', or closes the outermost
3696 nesting level. */
3697 nesting_depth--;
3698 if (nesting_depth < 0)
3699 return;
3700 if (!nesting_depth)
3701 nesting_depth = -1;
3702 break;
3704 case CPP_OPEN_BRACE:
3705 /* Nest. */
3706 nesting_depth++;
3707 break;
3709 default:
3710 break;
3713 /* Consume the token. */
3714 cp_lexer_consume_token (parser->lexer);
3718 /* Skip tokens until a non-nested closing curly brace is the next
3719 token, or there are no more tokens. Return true in the first case,
3720 false otherwise. */
3722 static bool
3723 cp_parser_skip_to_closing_brace (cp_parser *parser)
3725 unsigned nesting_depth = 0;
3727 while (true)
3729 cp_token *token = cp_lexer_peek_token (parser->lexer);
3731 switch (token->type)
3733 case CPP_EOF:
3734 case CPP_PRAGMA_EOL:
3735 /* If we've run out of tokens, stop. */
3736 return false;
3738 case CPP_CLOSE_BRACE:
3739 /* If the next token is a non-nested `}', then we have reached
3740 the end of the current block. */
3741 if (nesting_depth-- == 0)
3742 return true;
3743 break;
3745 case CPP_OPEN_BRACE:
3746 /* If it the next token is a `{', then we are entering a new
3747 block. Consume the entire block. */
3748 ++nesting_depth;
3749 break;
3751 default:
3752 break;
3755 /* Consume the token. */
3756 cp_lexer_consume_token (parser->lexer);
3760 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3761 parameter is the PRAGMA token, allowing us to purge the entire pragma
3762 sequence. */
3764 static void
3765 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3767 cp_token *token;
3769 parser->lexer->in_pragma = false;
3772 token = cp_lexer_consume_token (parser->lexer);
3773 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3775 /* Ensure that the pragma is not parsed again. */
3776 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3779 /* Require pragma end of line, resyncing with it as necessary. The
3780 arguments are as for cp_parser_skip_to_pragma_eol. */
3782 static void
3783 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3785 parser->lexer->in_pragma = false;
3786 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3790 /* This is a simple wrapper around make_typename_type. When the id is
3791 an unresolved identifier node, we can provide a superior diagnostic
3792 using cp_parser_diagnose_invalid_type_name. */
3794 static tree
3795 cp_parser_make_typename_type (cp_parser *parser, tree id,
3796 location_t id_location)
3798 tree result;
3799 if (identifier_p (id))
3801 result = make_typename_type (parser->scope, id, typename_type,
3802 /*complain=*/tf_none);
3803 if (result == error_mark_node)
3804 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3805 return result;
3807 return make_typename_type (parser->scope, id, typename_type, tf_error);
3810 /* This is a wrapper around the
3811 make_{pointer,ptrmem,reference}_declarator functions that decides
3812 which one to call based on the CODE and CLASS_TYPE arguments. The
3813 CODE argument should be one of the values returned by
3814 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3815 appertain to the pointer or reference. */
3817 static cp_declarator *
3818 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3819 cp_cv_quals cv_qualifiers,
3820 cp_declarator *target,
3821 tree attributes)
3823 if (code == ERROR_MARK)
3824 return cp_error_declarator;
3826 if (code == INDIRECT_REF)
3827 if (class_type == NULL_TREE)
3828 return make_pointer_declarator (cv_qualifiers, target, attributes);
3829 else
3830 return make_ptrmem_declarator (cv_qualifiers, class_type,
3831 target, attributes);
3832 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3833 return make_reference_declarator (cv_qualifiers, target,
3834 false, attributes);
3835 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 true, attributes);
3838 gcc_unreachable ();
3841 /* Create a new C++ parser. */
3843 static cp_parser *
3844 cp_parser_new (void)
3846 cp_parser *parser;
3847 cp_lexer *lexer;
3848 unsigned i;
3850 /* cp_lexer_new_main is called before doing GC allocation because
3851 cp_lexer_new_main might load a PCH file. */
3852 lexer = cp_lexer_new_main ();
3854 /* Initialize the binops_by_token so that we can get the tree
3855 directly from the token. */
3856 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3857 binops_by_token[binops[i].token_type] = binops[i];
3859 parser = ggc_cleared_alloc<cp_parser> ();
3860 parser->lexer = lexer;
3861 parser->context = cp_parser_context_new (NULL);
3863 /* For now, we always accept GNU extensions. */
3864 parser->allow_gnu_extensions_p = 1;
3866 /* The `>' token is a greater-than operator, not the end of a
3867 template-id. */
3868 parser->greater_than_is_operator_p = true;
3870 parser->default_arg_ok_p = true;
3872 /* We are not parsing a constant-expression. */
3873 parser->integral_constant_expression_p = false;
3874 parser->allow_non_integral_constant_expression_p = false;
3875 parser->non_integral_constant_expression_p = false;
3877 /* Local variable names are not forbidden. */
3878 parser->local_variables_forbidden_p = false;
3880 /* We are not processing an `extern "C"' declaration. */
3881 parser->in_unbraced_linkage_specification_p = false;
3883 /* We are not processing a declarator. */
3884 parser->in_declarator_p = false;
3886 /* We are not processing a template-argument-list. */
3887 parser->in_template_argument_list_p = false;
3889 /* We are not in an iteration statement. */
3890 parser->in_statement = 0;
3892 /* We are not in a switch statement. */
3893 parser->in_switch_statement_p = false;
3895 /* We are not parsing a type-id inside an expression. */
3896 parser->in_type_id_in_expr_p = false;
3898 /* Declarations aren't implicitly extern "C". */
3899 parser->implicit_extern_c = false;
3901 /* String literals should be translated to the execution character set. */
3902 parser->translate_strings_p = true;
3904 /* We are not parsing a function body. */
3905 parser->in_function_body = false;
3907 /* We can correct until told otherwise. */
3908 parser->colon_corrects_to_scope_p = true;
3910 /* The unparsed function queue is empty. */
3911 push_unparsed_function_queues (parser);
3913 /* There are no classes being defined. */
3914 parser->num_classes_being_defined = 0;
3916 /* No template parameters apply. */
3917 parser->num_template_parameter_lists = 0;
3919 /* Special parsing data structures. */
3920 parser->omp_declare_simd = NULL;
3921 parser->oacc_routine = NULL;
3923 /* Not declaring an implicit function template. */
3924 parser->auto_is_implicit_function_template_parm_p = false;
3925 parser->fully_implicit_function_template_p = false;
3926 parser->implicit_template_parms = 0;
3927 parser->implicit_template_scope = 0;
3929 /* Allow constrained-type-specifiers. */
3930 parser->prevent_constrained_type_specifiers = 0;
3932 /* We haven't yet seen an 'extern "C"'. */
3933 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3935 return parser;
3938 /* Create a cp_lexer structure which will emit the tokens in CACHE
3939 and push it onto the parser's lexer stack. This is used for delayed
3940 parsing of in-class method bodies and default arguments, and should
3941 not be confused with tentative parsing. */
3942 static void
3943 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3945 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3946 lexer->next = parser->lexer;
3947 parser->lexer = lexer;
3949 /* Move the current source position to that of the first token in the
3950 new lexer. */
3951 cp_lexer_set_source_position_from_token (lexer->next_token);
3954 /* Pop the top lexer off the parser stack. This is never used for the
3955 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3956 static void
3957 cp_parser_pop_lexer (cp_parser *parser)
3959 cp_lexer *lexer = parser->lexer;
3960 parser->lexer = lexer->next;
3961 cp_lexer_destroy (lexer);
3963 /* Put the current source position back where it was before this
3964 lexer was pushed. */
3965 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3968 /* Lexical conventions [gram.lex] */
3970 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3971 identifier. */
3973 static cp_expr
3974 cp_parser_identifier (cp_parser* parser)
3976 cp_token *token;
3978 /* Look for the identifier. */
3979 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3980 /* Return the value. */
3981 if (token)
3982 return cp_expr (token->u.value, token->location);
3983 else
3984 return error_mark_node;
3987 /* Parse a sequence of adjacent string constants. Returns a
3988 TREE_STRING representing the combined, nul-terminated string
3989 constant. If TRANSLATE is true, translate the string to the
3990 execution character set. If WIDE_OK is true, a wide string is
3991 invalid here.
3993 C++98 [lex.string] says that if a narrow string literal token is
3994 adjacent to a wide string literal token, the behavior is undefined.
3995 However, C99 6.4.5p4 says that this results in a wide string literal.
3996 We follow C99 here, for consistency with the C front end.
3998 This code is largely lifted from lex_string() in c-lex.c.
4000 FUTURE: ObjC++ will need to handle @-strings here. */
4001 static cp_expr
4002 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4003 bool lookup_udlit = true)
4005 tree value;
4006 size_t count;
4007 struct obstack str_ob;
4008 cpp_string str, istr, *strs;
4009 cp_token *tok;
4010 enum cpp_ttype type, curr_type;
4011 int have_suffix_p = 0;
4012 tree string_tree;
4013 tree suffix_id = NULL_TREE;
4014 bool curr_tok_is_userdef_p = false;
4016 tok = cp_lexer_peek_token (parser->lexer);
4017 if (!cp_parser_is_string_literal (tok))
4019 cp_parser_error (parser, "expected string-literal");
4020 return error_mark_node;
4023 location_t loc = tok->location;
4025 if (cpp_userdef_string_p (tok->type))
4027 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4028 curr_type = cpp_userdef_string_remove_type (tok->type);
4029 curr_tok_is_userdef_p = true;
4031 else
4033 string_tree = tok->u.value;
4034 curr_type = tok->type;
4036 type = curr_type;
4038 /* Try to avoid the overhead of creating and destroying an obstack
4039 for the common case of just one string. */
4040 if (!cp_parser_is_string_literal
4041 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4043 cp_lexer_consume_token (parser->lexer);
4045 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4046 str.len = TREE_STRING_LENGTH (string_tree);
4047 count = 1;
4049 if (curr_tok_is_userdef_p)
4051 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4052 have_suffix_p = 1;
4053 curr_type = cpp_userdef_string_remove_type (tok->type);
4055 else
4056 curr_type = tok->type;
4058 strs = &str;
4060 else
4062 location_t last_tok_loc = tok->location;
4063 gcc_obstack_init (&str_ob);
4064 count = 0;
4068 cp_lexer_consume_token (parser->lexer);
4069 count++;
4070 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4071 str.len = TREE_STRING_LENGTH (string_tree);
4073 if (curr_tok_is_userdef_p)
4075 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4076 if (have_suffix_p == 0)
4078 suffix_id = curr_suffix_id;
4079 have_suffix_p = 1;
4081 else if (have_suffix_p == 1
4082 && curr_suffix_id != suffix_id)
4084 error ("inconsistent user-defined literal suffixes"
4085 " %qD and %qD in string literal",
4086 suffix_id, curr_suffix_id);
4087 have_suffix_p = -1;
4089 curr_type = cpp_userdef_string_remove_type (tok->type);
4091 else
4092 curr_type = tok->type;
4094 if (type != curr_type)
4096 if (type == CPP_STRING)
4097 type = curr_type;
4098 else if (curr_type != CPP_STRING)
4100 rich_location rich_loc (line_table, tok->location);
4101 rich_loc.add_range (last_tok_loc, false);
4102 error_at (&rich_loc,
4103 "unsupported non-standard concatenation "
4104 "of string literals");
4108 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4110 last_tok_loc = tok->location;
4112 tok = cp_lexer_peek_token (parser->lexer);
4113 if (cpp_userdef_string_p (tok->type))
4115 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4116 curr_type = cpp_userdef_string_remove_type (tok->type);
4117 curr_tok_is_userdef_p = true;
4119 else
4121 string_tree = tok->u.value;
4122 curr_type = tok->type;
4123 curr_tok_is_userdef_p = false;
4126 while (cp_parser_is_string_literal (tok));
4128 /* A string literal built by concatenation has its caret=start at
4129 the start of the initial string, and its finish at the finish of
4130 the final string literal. */
4131 loc = make_location (loc, loc, get_finish (last_tok_loc));
4133 strs = (cpp_string *) obstack_finish (&str_ob);
4136 if (type != CPP_STRING && !wide_ok)
4138 cp_parser_error (parser, "a wide string is invalid in this context");
4139 type = CPP_STRING;
4142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4143 (parse_in, strs, count, &istr, type))
4145 value = build_string (istr.len, (const char *)istr.text);
4146 free (CONST_CAST (unsigned char *, istr.text));
4148 switch (type)
4150 default:
4151 case CPP_STRING:
4152 case CPP_UTF8STRING:
4153 TREE_TYPE (value) = char_array_type_node;
4154 break;
4155 case CPP_STRING16:
4156 TREE_TYPE (value) = char16_array_type_node;
4157 break;
4158 case CPP_STRING32:
4159 TREE_TYPE (value) = char32_array_type_node;
4160 break;
4161 case CPP_WSTRING:
4162 TREE_TYPE (value) = wchar_array_type_node;
4163 break;
4166 value = fix_string_type (value);
4168 if (have_suffix_p)
4170 tree literal = build_userdef_literal (suffix_id, value,
4171 OT_NONE, NULL_TREE);
4172 if (lookup_udlit)
4173 value = cp_parser_userdef_string_literal (literal);
4174 else
4175 value = literal;
4178 else
4179 /* cpp_interpret_string has issued an error. */
4180 value = error_mark_node;
4182 if (count > 1)
4183 obstack_free (&str_ob, 0);
4185 return cp_expr (value, loc);
4188 /* Look up a literal operator with the name and the exact arguments. */
4190 static tree
4191 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4193 tree decl;
4194 decl = lookup_name (name);
4195 if (!decl || !is_overloaded_fn (decl))
4196 return error_mark_node;
4198 for (lkp_iterator iter (decl); iter; ++iter)
4200 unsigned int ix;
4201 bool found = true;
4202 tree fn = *iter;
4203 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4204 if (parmtypes != NULL_TREE)
4206 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4207 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4209 tree tparm = TREE_VALUE (parmtypes);
4210 tree targ = TREE_TYPE ((*args)[ix]);
4211 bool ptr = TYPE_PTR_P (tparm);
4212 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4213 if ((ptr || arr || !same_type_p (tparm, targ))
4214 && (!ptr || !arr
4215 || !same_type_p (TREE_TYPE (tparm),
4216 TREE_TYPE (targ))))
4217 found = false;
4219 if (found
4220 && ix == vec_safe_length (args)
4221 /* May be this should be sufficient_parms_p instead,
4222 depending on how exactly should user-defined literals
4223 work in presence of default arguments on the literal
4224 operator parameters. */
4225 && parmtypes == void_list_node)
4226 return decl;
4230 return error_mark_node;
4233 /* Parse a user-defined char constant. Returns a call to a user-defined
4234 literal operator taking the character as an argument. */
4236 static cp_expr
4237 cp_parser_userdef_char_literal (cp_parser *parser)
4239 cp_token *token = cp_lexer_consume_token (parser->lexer);
4240 tree literal = token->u.value;
4241 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4242 tree value = USERDEF_LITERAL_VALUE (literal);
4243 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4244 tree decl, result;
4246 /* Build up a call to the user-defined operator */
4247 /* Lookup the name we got back from the id-expression. */
4248 vec<tree, va_gc> *args = make_tree_vector ();
4249 vec_safe_push (args, value);
4250 decl = lookup_literal_operator (name, args);
4251 if (!decl || decl == error_mark_node)
4253 error ("unable to find character literal operator %qD with %qT argument",
4254 name, TREE_TYPE (value));
4255 release_tree_vector (args);
4256 return error_mark_node;
4258 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4259 release_tree_vector (args);
4260 return result;
4263 /* A subroutine of cp_parser_userdef_numeric_literal to
4264 create a char... template parameter pack from a string node. */
4266 static tree
4267 make_char_string_pack (tree value)
4269 tree charvec;
4270 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4271 const char *str = TREE_STRING_POINTER (value);
4272 int i, len = TREE_STRING_LENGTH (value) - 1;
4273 tree argvec = make_tree_vec (1);
4275 /* Fill in CHARVEC with all of the parameters. */
4276 charvec = make_tree_vec (len);
4277 for (i = 0; i < len; ++i)
4278 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4280 /* Build the argument packs. */
4281 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4283 TREE_VEC_ELT (argvec, 0) = argpack;
4285 return argvec;
4288 /* A subroutine of cp_parser_userdef_numeric_literal to
4289 create a char... template parameter pack from a string node. */
4291 static tree
4292 make_string_pack (tree value)
4294 tree charvec;
4295 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4296 const unsigned char *str
4297 = (const unsigned char *) TREE_STRING_POINTER (value);
4298 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4299 int len = TREE_STRING_LENGTH (value) / sz - 1;
4300 tree argvec = make_tree_vec (2);
4302 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4303 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4305 /* First template parm is character type. */
4306 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4308 /* Fill in CHARVEC with all of the parameters. */
4309 charvec = make_tree_vec (len);
4310 for (int i = 0; i < len; ++i)
4311 TREE_VEC_ELT (charvec, i)
4312 = double_int_to_tree (str_char_type_node,
4313 double_int::from_buffer (str + i * sz, sz));
4315 /* Build the argument packs. */
4316 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4318 TREE_VEC_ELT (argvec, 1) = argpack;
4320 return argvec;
4323 /* Parse a user-defined numeric constant. returns a call to a user-defined
4324 literal operator. */
4326 static cp_expr
4327 cp_parser_userdef_numeric_literal (cp_parser *parser)
4329 cp_token *token = cp_lexer_consume_token (parser->lexer);
4330 tree literal = token->u.value;
4331 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4332 tree value = USERDEF_LITERAL_VALUE (literal);
4333 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4334 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4335 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4336 tree decl, result;
4337 vec<tree, va_gc> *args;
4339 /* Look for a literal operator taking the exact type of numeric argument
4340 as the literal value. */
4341 args = make_tree_vector ();
4342 vec_safe_push (args, value);
4343 decl = lookup_literal_operator (name, args);
4344 if (decl && decl != error_mark_node)
4346 result = finish_call_expr (decl, &args, false, true,
4347 tf_warning_or_error);
4349 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4351 warning_at (token->location, OPT_Woverflow,
4352 "integer literal exceeds range of %qT type",
4353 long_long_unsigned_type_node);
4355 else
4357 if (overflow > 0)
4358 warning_at (token->location, OPT_Woverflow,
4359 "floating literal exceeds range of %qT type",
4360 long_double_type_node);
4361 else if (overflow < 0)
4362 warning_at (token->location, OPT_Woverflow,
4363 "floating literal truncated to zero");
4366 release_tree_vector (args);
4367 return result;
4369 release_tree_vector (args);
4371 /* If the numeric argument didn't work, look for a raw literal
4372 operator taking a const char* argument consisting of the number
4373 in string format. */
4374 args = make_tree_vector ();
4375 vec_safe_push (args, num_string);
4376 decl = lookup_literal_operator (name, args);
4377 if (decl && decl != error_mark_node)
4379 result = finish_call_expr (decl, &args, false, true,
4380 tf_warning_or_error);
4381 release_tree_vector (args);
4382 return result;
4384 release_tree_vector (args);
4386 /* If the raw literal didn't work, look for a non-type template
4387 function with parameter pack char.... Call the function with
4388 template parameter characters representing the number. */
4389 args = make_tree_vector ();
4390 decl = lookup_literal_operator (name, args);
4391 if (decl && decl != error_mark_node)
4393 tree tmpl_args = make_char_string_pack (num_string);
4394 decl = lookup_template_function (decl, tmpl_args);
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4401 release_tree_vector (args);
4403 /* In C++14 the standard library defines complex number suffixes that
4404 conflict with GNU extensions. Prefer them if <complex> is #included. */
4405 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4406 bool i14 = (cxx_dialect > cxx11
4407 && (id_equal (suffix_id, "i")
4408 || id_equal (suffix_id, "if")
4409 || id_equal (suffix_id, "il")));
4410 diagnostic_t kind = DK_ERROR;
4411 int opt = 0;
4413 if (i14 && ext)
4415 tree cxlit = lookup_qualified_name (std_node,
4416 get_identifier ("complex_literals"),
4417 0, false, false);
4418 if (cxlit == error_mark_node)
4420 /* No <complex>, so pedwarn and use GNU semantics. */
4421 kind = DK_PEDWARN;
4422 opt = OPT_Wpedantic;
4426 bool complained
4427 = emit_diagnostic (kind, input_location, opt,
4428 "unable to find numeric literal operator %qD", name);
4430 if (!complained)
4431 /* Don't inform either. */;
4432 else if (i14)
4434 inform (token->location, "add %<using namespace std::complex_literals%> "
4435 "(from <complex>) to enable the C++14 user-defined literal "
4436 "suffixes");
4437 if (ext)
4438 inform (token->location, "or use %<j%> instead of %<i%> for the "
4439 "GNU built-in suffix");
4441 else if (!ext)
4442 inform (token->location, "use -fext-numeric-literals "
4443 "to enable more built-in suffixes");
4445 if (kind == DK_ERROR)
4446 value = error_mark_node;
4447 else
4449 /* Use the built-in semantics. */
4450 tree type;
4451 if (id_equal (suffix_id, "i"))
4453 if (TREE_CODE (value) == INTEGER_CST)
4454 type = integer_type_node;
4455 else
4456 type = double_type_node;
4458 else if (id_equal (suffix_id, "if"))
4459 type = float_type_node;
4460 else /* if (id_equal (suffix_id, "il")) */
4461 type = long_double_type_node;
4463 value = build_complex (build_complex_type (type),
4464 fold_convert (type, integer_zero_node),
4465 fold_convert (type, value));
4468 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4469 /* Avoid repeated diagnostics. */
4470 token->u.value = value;
4471 return value;
4474 /* Parse a user-defined string constant. Returns a call to a user-defined
4475 literal operator taking a character pointer and the length of the string
4476 as arguments. */
4478 static tree
4479 cp_parser_userdef_string_literal (tree literal)
4481 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4482 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4483 tree value = USERDEF_LITERAL_VALUE (literal);
4484 int len = TREE_STRING_LENGTH (value)
4485 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4486 tree decl, result;
4487 vec<tree, va_gc> *args;
4489 /* Build up a call to the user-defined operator. */
4490 /* Lookup the name we got back from the id-expression. */
4491 args = make_tree_vector ();
4492 vec_safe_push (args, value);
4493 vec_safe_push (args, build_int_cst (size_type_node, len));
4494 decl = lookup_literal_operator (name, args);
4496 if (decl && decl != error_mark_node)
4498 result = finish_call_expr (decl, &args, false, true,
4499 tf_warning_or_error);
4500 release_tree_vector (args);
4501 return result;
4503 release_tree_vector (args);
4505 /* Look for a template function with typename parameter CharT
4506 and parameter pack CharT... Call the function with
4507 template parameter characters representing the string. */
4508 args = make_tree_vector ();
4509 decl = lookup_literal_operator (name, args);
4510 if (decl && decl != error_mark_node)
4512 tree tmpl_args = make_string_pack (value);
4513 decl = lookup_template_function (decl, tmpl_args);
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4519 release_tree_vector (args);
4521 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4522 name, TREE_TYPE (value), size_type_node);
4523 return error_mark_node;
4527 /* Basic concepts [gram.basic] */
4529 /* Parse a translation-unit.
4531 translation-unit:
4532 declaration-seq [opt]
4534 Returns TRUE if all went well. */
4536 static bool
4537 cp_parser_translation_unit (cp_parser* parser)
4539 /* The address of the first non-permanent object on the declarator
4540 obstack. */
4541 static void *declarator_obstack_base;
4543 bool success;
4545 /* Create the declarator obstack, if necessary. */
4546 if (!cp_error_declarator)
4548 gcc_obstack_init (&declarator_obstack);
4549 /* Create the error declarator. */
4550 cp_error_declarator = make_declarator (cdk_error);
4551 /* Create the empty parameter list. */
4552 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4553 UNKNOWN_LOCATION);
4554 /* Remember where the base of the declarator obstack lies. */
4555 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4558 cp_parser_declaration_seq_opt (parser);
4560 /* If there are no tokens left then all went well. */
4561 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4563 /* Get rid of the token array; we don't need it any more. */
4564 cp_lexer_destroy (parser->lexer);
4565 parser->lexer = NULL;
4567 /* This file might have been a context that's implicitly extern
4568 "C". If so, pop the lang context. (Only relevant for PCH.) */
4569 if (parser->implicit_extern_c)
4571 pop_lang_context ();
4572 parser->implicit_extern_c = false;
4575 /* Finish up. */
4576 finish_translation_unit ();
4578 success = true;
4580 else
4582 cp_parser_error (parser, "expected declaration");
4583 success = false;
4586 /* Make sure the declarator obstack was fully cleaned up. */
4587 gcc_assert (obstack_next_free (&declarator_obstack)
4588 == declarator_obstack_base);
4590 /* All went well. */
4591 return success;
4594 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4595 decltype context. */
4597 static inline tsubst_flags_t
4598 complain_flags (bool decltype_p)
4600 tsubst_flags_t complain = tf_warning_or_error;
4601 if (decltype_p)
4602 complain |= tf_decltype;
4603 return complain;
4606 /* We're about to parse a collection of statements. If we're currently
4607 parsing tentatively, set up a firewall so that any nested
4608 cp_parser_commit_to_tentative_parse won't affect the current context. */
4610 static cp_token_position
4611 cp_parser_start_tentative_firewall (cp_parser *parser)
4613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4614 return 0;
4616 cp_parser_parse_tentatively (parser);
4617 cp_parser_commit_to_topmost_tentative_parse (parser);
4618 return cp_lexer_token_position (parser->lexer, false);
4621 /* We've finished parsing the collection of statements. Wrap up the
4622 firewall and replace the relevant tokens with the parsed form. */
4624 static void
4625 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4626 tree expr)
4628 if (!start)
4629 return;
4631 /* Finish the firewall level. */
4632 cp_parser_parse_definitely (parser);
4633 /* And remember the result of the parse for when we try again. */
4634 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4635 token->type = CPP_PREPARSED_EXPR;
4636 token->u.value = expr;
4637 token->keyword = RID_MAX;
4638 cp_lexer_purge_tokens_after (parser->lexer, start);
4641 /* Like the above functions, but let the user modify the tokens. Used by
4642 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4643 later parses, so it makes sense to localize the effects of
4644 cp_parser_commit_to_tentative_parse. */
4646 struct tentative_firewall
4648 cp_parser *parser;
4649 bool set;
4651 tentative_firewall (cp_parser *p): parser(p)
4653 /* If we're currently parsing tentatively, start a committed level as a
4654 firewall and then an inner tentative parse. */
4655 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4657 cp_parser_parse_tentatively (parser);
4658 cp_parser_commit_to_topmost_tentative_parse (parser);
4659 cp_parser_parse_tentatively (parser);
4663 ~tentative_firewall()
4665 if (set)
4667 /* Finish the inner tentative parse and the firewall, propagating any
4668 uncommitted error state to the outer tentative parse. */
4669 bool err = cp_parser_error_occurred (parser);
4670 cp_parser_parse_definitely (parser);
4671 cp_parser_parse_definitely (parser);
4672 if (err)
4673 cp_parser_simulate_error (parser);
4678 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4679 This class is for tracking such a matching pair of symbols.
4680 In particular, it tracks the location of the first token,
4681 so that if the second token is missing, we can highlight the
4682 location of the first token when notifying the user about the
4683 problem. */
4685 template <typename traits_t>
4686 class token_pair
4688 public:
4689 /* token_pair's ctor. */
4690 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4692 /* If the next token is the opening symbol for this pair, consume it and
4693 return true.
4694 Otherwise, issue an error and return false.
4695 In either case, record the location of the opening token. */
4697 bool require_open (cp_parser *parser)
4699 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4700 return cp_parser_require (parser, traits_t::open_token_type,
4701 traits_t::required_token_open);
4704 /* Consume the next token from PARSER, recording its location as
4705 that of the opening token within the pair. */
4707 cp_token * consume_open (cp_parser *parser)
4709 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4710 gcc_assert (tok->type == traits_t::open_token_type);
4711 m_open_loc = tok->location;
4712 return tok;
4715 /* If the next token is the closing symbol for this pair, consume it
4716 and return it.
4717 Otherwise, issue an error, highlighting the location of the
4718 corresponding opening token, and return NULL. */
4720 cp_token *require_close (cp_parser *parser) const
4722 return cp_parser_require (parser, traits_t::close_token_type,
4723 traits_t::required_token_close,
4724 m_open_loc);
4727 private:
4728 location_t m_open_loc;
4731 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4733 struct matching_paren_traits
4735 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4736 static const enum required_token required_token_open = RT_OPEN_PAREN;
4737 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4738 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4741 /* "matching_parens" is a token_pair<T> class for tracking matching
4742 pairs of parentheses. */
4744 typedef token_pair<matching_paren_traits> matching_parens;
4746 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4748 struct matching_brace_traits
4750 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4751 static const enum required_token required_token_open = RT_OPEN_BRACE;
4752 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4753 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4756 /* "matching_braces" is a token_pair<T> class for tracking matching
4757 pairs of braces. */
4759 typedef token_pair<matching_brace_traits> matching_braces;
4762 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4763 enclosing parentheses. */
4765 static cp_expr
4766 cp_parser_statement_expr (cp_parser *parser)
4768 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4770 /* Consume the '('. */
4771 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4772 matching_parens parens;
4773 parens.consume_open (parser);
4774 /* Start the statement-expression. */
4775 tree expr = begin_stmt_expr ();
4776 /* Parse the compound-statement. */
4777 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4778 /* Finish up. */
4779 expr = finish_stmt_expr (expr, false);
4780 /* Consume the ')'. */
4781 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4782 if (!parens.require_close (parser))
4783 cp_parser_skip_to_end_of_statement (parser);
4785 cp_parser_end_tentative_firewall (parser, start, expr);
4786 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4787 return cp_expr (expr, combined_loc);
4790 /* Expressions [gram.expr] */
4792 /* Parse a fold-operator.
4794 fold-operator:
4795 - * / % ^ & | = < > << >>
4796 = -= *= /= %= ^= &= |= <<= >>=
4797 == != <= >= && || , .* ->*
4799 This returns the tree code corresponding to the matched operator
4800 as an int. When the current token matches a compound assignment
4801 opertor, the resulting tree code is the negative value of the
4802 non-assignment operator. */
4804 static int
4805 cp_parser_fold_operator (cp_token *token)
4807 switch (token->type)
4809 case CPP_PLUS: return PLUS_EXPR;
4810 case CPP_MINUS: return MINUS_EXPR;
4811 case CPP_MULT: return MULT_EXPR;
4812 case CPP_DIV: return TRUNC_DIV_EXPR;
4813 case CPP_MOD: return TRUNC_MOD_EXPR;
4814 case CPP_XOR: return BIT_XOR_EXPR;
4815 case CPP_AND: return BIT_AND_EXPR;
4816 case CPP_OR: return BIT_IOR_EXPR;
4817 case CPP_LSHIFT: return LSHIFT_EXPR;
4818 case CPP_RSHIFT: return RSHIFT_EXPR;
4820 case CPP_EQ: return -NOP_EXPR;
4821 case CPP_PLUS_EQ: return -PLUS_EXPR;
4822 case CPP_MINUS_EQ: return -MINUS_EXPR;
4823 case CPP_MULT_EQ: return -MULT_EXPR;
4824 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4825 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4826 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4827 case CPP_AND_EQ: return -BIT_AND_EXPR;
4828 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4829 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4830 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4832 case CPP_EQ_EQ: return EQ_EXPR;
4833 case CPP_NOT_EQ: return NE_EXPR;
4834 case CPP_LESS: return LT_EXPR;
4835 case CPP_GREATER: return GT_EXPR;
4836 case CPP_LESS_EQ: return LE_EXPR;
4837 case CPP_GREATER_EQ: return GE_EXPR;
4839 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4840 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4842 case CPP_COMMA: return COMPOUND_EXPR;
4844 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4845 case CPP_DEREF_STAR: return MEMBER_REF;
4847 default: return ERROR_MARK;
4851 /* Returns true if CODE indicates a binary expression, which is not allowed in
4852 the LHS of a fold-expression. More codes will need to be added to use this
4853 function in other contexts. */
4855 static bool
4856 is_binary_op (tree_code code)
4858 switch (code)
4860 case PLUS_EXPR:
4861 case POINTER_PLUS_EXPR:
4862 case MINUS_EXPR:
4863 case MULT_EXPR:
4864 case TRUNC_DIV_EXPR:
4865 case TRUNC_MOD_EXPR:
4866 case BIT_XOR_EXPR:
4867 case BIT_AND_EXPR:
4868 case BIT_IOR_EXPR:
4869 case LSHIFT_EXPR:
4870 case RSHIFT_EXPR:
4872 case MODOP_EXPR:
4874 case EQ_EXPR:
4875 case NE_EXPR:
4876 case LE_EXPR:
4877 case GE_EXPR:
4878 case LT_EXPR:
4879 case GT_EXPR:
4881 case TRUTH_ANDIF_EXPR:
4882 case TRUTH_ORIF_EXPR:
4884 case COMPOUND_EXPR:
4886 case DOTSTAR_EXPR:
4887 case MEMBER_REF:
4888 return true;
4890 default:
4891 return false;
4895 /* If the next token is a suitable fold operator, consume it and return as
4896 the function above. */
4898 static int
4899 cp_parser_fold_operator (cp_parser *parser)
4901 cp_token* token = cp_lexer_peek_token (parser->lexer);
4902 int code = cp_parser_fold_operator (token);
4903 if (code != ERROR_MARK)
4904 cp_lexer_consume_token (parser->lexer);
4905 return code;
4908 /* Parse a fold-expression.
4910 fold-expression:
4911 ( ... folding-operator cast-expression)
4912 ( cast-expression folding-operator ... )
4913 ( cast-expression folding operator ... folding-operator cast-expression)
4915 Note that the '(' and ')' are matched in primary expression. */
4917 static cp_expr
4918 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4920 cp_id_kind pidk;
4922 // Left fold.
4923 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4925 cp_lexer_consume_token (parser->lexer);
4926 int op = cp_parser_fold_operator (parser);
4927 if (op == ERROR_MARK)
4929 cp_parser_error (parser, "expected binary operator");
4930 return error_mark_node;
4933 tree expr = cp_parser_cast_expression (parser, false, false,
4934 false, &pidk);
4935 if (expr == error_mark_node)
4936 return error_mark_node;
4937 return finish_left_unary_fold_expr (expr, op);
4940 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4941 int op = cp_parser_fold_operator (parser);
4942 if (op == ERROR_MARK)
4944 cp_parser_error (parser, "expected binary operator");
4945 return error_mark_node;
4948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4950 cp_parser_error (parser, "expected ...");
4951 return error_mark_node;
4953 cp_lexer_consume_token (parser->lexer);
4955 /* The operands of a fold-expression are cast-expressions, so binary or
4956 conditional expressions are not allowed. We check this here to avoid
4957 tentative parsing. */
4958 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4959 /* OK, the expression was parenthesized. */;
4960 else if (is_binary_op (TREE_CODE (expr1)))
4961 error_at (location_of (expr1),
4962 "binary expression in operand of fold-expression");
4963 else if (TREE_CODE (expr1) == COND_EXPR)
4964 error_at (location_of (expr1),
4965 "conditional expression in operand of fold-expression");
4967 // Right fold.
4968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4969 return finish_right_unary_fold_expr (expr1, op);
4971 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4973 cp_parser_error (parser, "mismatched operator in fold-expression");
4974 return error_mark_node;
4976 cp_lexer_consume_token (parser->lexer);
4978 // Binary left or right fold.
4979 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4980 if (expr2 == error_mark_node)
4981 return error_mark_node;
4982 return finish_binary_fold_expr (expr1, expr2, op);
4985 /* Parse a primary-expression.
4987 primary-expression:
4988 literal
4989 this
4990 ( expression )
4991 id-expression
4992 lambda-expression (C++11)
4994 GNU Extensions:
4996 primary-expression:
4997 ( compound-statement )
4998 __builtin_va_arg ( assignment-expression , type-id )
4999 __builtin_offsetof ( type-id , offsetof-expression )
5001 C++ Extensions:
5002 __has_nothrow_assign ( type-id )
5003 __has_nothrow_constructor ( type-id )
5004 __has_nothrow_copy ( type-id )
5005 __has_trivial_assign ( type-id )
5006 __has_trivial_constructor ( type-id )
5007 __has_trivial_copy ( type-id )
5008 __has_trivial_destructor ( type-id )
5009 __has_virtual_destructor ( type-id )
5010 __is_abstract ( type-id )
5011 __is_base_of ( type-id , type-id )
5012 __is_class ( type-id )
5013 __is_empty ( type-id )
5014 __is_enum ( type-id )
5015 __is_final ( type-id )
5016 __is_literal_type ( type-id )
5017 __is_pod ( type-id )
5018 __is_polymorphic ( type-id )
5019 __is_std_layout ( type-id )
5020 __is_trivial ( type-id )
5021 __is_union ( type-id )
5023 Objective-C++ Extension:
5025 primary-expression:
5026 objc-expression
5028 literal:
5029 __null
5031 ADDRESS_P is true iff this expression was immediately preceded by
5032 "&" and therefore might denote a pointer-to-member. CAST_P is true
5033 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5034 true iff this expression is a template argument.
5036 Returns a representation of the expression. Upon return, *IDK
5037 indicates what kind of id-expression (if any) was present. */
5039 static cp_expr
5040 cp_parser_primary_expression (cp_parser *parser,
5041 bool address_p,
5042 bool cast_p,
5043 bool template_arg_p,
5044 bool decltype_p,
5045 cp_id_kind *idk)
5047 cp_token *token = NULL;
5049 /* Assume the primary expression is not an id-expression. */
5050 *idk = CP_ID_KIND_NONE;
5052 /* Peek at the next token. */
5053 token = cp_lexer_peek_token (parser->lexer);
5054 switch ((int) token->type)
5056 /* literal:
5057 integer-literal
5058 character-literal
5059 floating-literal
5060 string-literal
5061 boolean-literal
5062 pointer-literal
5063 user-defined-literal */
5064 case CPP_CHAR:
5065 case CPP_CHAR16:
5066 case CPP_CHAR32:
5067 case CPP_WCHAR:
5068 case CPP_UTF8CHAR:
5069 case CPP_NUMBER:
5070 case CPP_PREPARSED_EXPR:
5071 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5072 return cp_parser_userdef_numeric_literal (parser);
5073 token = cp_lexer_consume_token (parser->lexer);
5074 if (TREE_CODE (token->u.value) == FIXED_CST)
5076 error_at (token->location,
5077 "fixed-point types not supported in C++");
5078 return error_mark_node;
5080 /* Floating-point literals are only allowed in an integral
5081 constant expression if they are cast to an integral or
5082 enumeration type. */
5083 if (TREE_CODE (token->u.value) == REAL_CST
5084 && parser->integral_constant_expression_p
5085 && pedantic)
5087 /* CAST_P will be set even in invalid code like "int(2.7 +
5088 ...)". Therefore, we have to check that the next token
5089 is sure to end the cast. */
5090 if (cast_p)
5092 cp_token *next_token;
5094 next_token = cp_lexer_peek_token (parser->lexer);
5095 if (/* The comma at the end of an
5096 enumerator-definition. */
5097 next_token->type != CPP_COMMA
5098 /* The curly brace at the end of an enum-specifier. */
5099 && next_token->type != CPP_CLOSE_BRACE
5100 /* The end of a statement. */
5101 && next_token->type != CPP_SEMICOLON
5102 /* The end of the cast-expression. */
5103 && next_token->type != CPP_CLOSE_PAREN
5104 /* The end of an array bound. */
5105 && next_token->type != CPP_CLOSE_SQUARE
5106 /* The closing ">" in a template-argument-list. */
5107 && (next_token->type != CPP_GREATER
5108 || parser->greater_than_is_operator_p)
5109 /* C++0x only: A ">>" treated like two ">" tokens,
5110 in a template-argument-list. */
5111 && (next_token->type != CPP_RSHIFT
5112 || (cxx_dialect == cxx98)
5113 || parser->greater_than_is_operator_p))
5114 cast_p = false;
5117 /* If we are within a cast, then the constraint that the
5118 cast is to an integral or enumeration type will be
5119 checked at that point. If we are not within a cast, then
5120 this code is invalid. */
5121 if (!cast_p)
5122 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5124 return cp_expr (token->u.value, token->location);
5126 case CPP_CHAR_USERDEF:
5127 case CPP_CHAR16_USERDEF:
5128 case CPP_CHAR32_USERDEF:
5129 case CPP_WCHAR_USERDEF:
5130 case CPP_UTF8CHAR_USERDEF:
5131 return cp_parser_userdef_char_literal (parser);
5133 case CPP_STRING:
5134 case CPP_STRING16:
5135 case CPP_STRING32:
5136 case CPP_WSTRING:
5137 case CPP_UTF8STRING:
5138 case CPP_STRING_USERDEF:
5139 case CPP_STRING16_USERDEF:
5140 case CPP_STRING32_USERDEF:
5141 case CPP_WSTRING_USERDEF:
5142 case CPP_UTF8STRING_USERDEF:
5143 /* ??? Should wide strings be allowed when parser->translate_strings_p
5144 is false (i.e. in attributes)? If not, we can kill the third
5145 argument to cp_parser_string_literal. */
5146 return cp_parser_string_literal (parser,
5147 parser->translate_strings_p,
5148 true);
5150 case CPP_OPEN_PAREN:
5151 /* If we see `( { ' then we are looking at the beginning of
5152 a GNU statement-expression. */
5153 if (cp_parser_allow_gnu_extensions_p (parser)
5154 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5156 /* Statement-expressions are not allowed by the standard. */
5157 pedwarn (token->location, OPT_Wpedantic,
5158 "ISO C++ forbids braced-groups within expressions");
5160 /* And they're not allowed outside of a function-body; you
5161 cannot, for example, write:
5163 int i = ({ int j = 3; j + 1; });
5165 at class or namespace scope. */
5166 if (!parser->in_function_body
5167 || parser->in_template_argument_list_p)
5169 error_at (token->location,
5170 "statement-expressions are not allowed outside "
5171 "functions nor in template-argument lists");
5172 cp_parser_skip_to_end_of_block_or_statement (parser);
5173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5174 cp_lexer_consume_token (parser->lexer);
5175 return error_mark_node;
5177 else
5178 return cp_parser_statement_expr (parser);
5180 /* Otherwise it's a normal parenthesized expression. */
5182 cp_expr expr;
5183 bool saved_greater_than_is_operator_p;
5185 location_t open_paren_loc = token->location;
5187 /* Consume the `('. */
5188 matching_parens parens;
5189 parens.consume_open (parser);
5190 /* Within a parenthesized expression, a `>' token is always
5191 the greater-than operator. */
5192 saved_greater_than_is_operator_p
5193 = parser->greater_than_is_operator_p;
5194 parser->greater_than_is_operator_p = true;
5196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5197 /* Left fold expression. */
5198 expr = NULL_TREE;
5199 else
5200 /* Parse the parenthesized expression. */
5201 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5203 token = cp_lexer_peek_token (parser->lexer);
5204 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5206 expr = cp_parser_fold_expression (parser, expr);
5207 if (expr != error_mark_node
5208 && cxx_dialect < cxx17
5209 && !in_system_header_at (input_location))
5210 pedwarn (input_location, 0, "fold-expressions only available "
5211 "with -std=c++17 or -std=gnu++17");
5213 else
5214 /* Let the front end know that this expression was
5215 enclosed in parentheses. This matters in case, for
5216 example, the expression is of the form `A::B', since
5217 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5218 not. */
5219 expr = finish_parenthesized_expr (expr);
5221 /* DR 705: Wrapping an unqualified name in parentheses
5222 suppresses arg-dependent lookup. We want to pass back
5223 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5224 (c++/37862), but none of the others. */
5225 if (*idk != CP_ID_KIND_QUALIFIED)
5226 *idk = CP_ID_KIND_NONE;
5228 /* The `>' token might be the end of a template-id or
5229 template-parameter-list now. */
5230 parser->greater_than_is_operator_p
5231 = saved_greater_than_is_operator_p;
5233 /* Consume the `)'. */
5234 token = cp_lexer_peek_token (parser->lexer);
5235 location_t close_paren_loc = token->location;
5236 expr.set_range (open_paren_loc, close_paren_loc);
5237 if (!parens.require_close (parser)
5238 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5239 cp_parser_skip_to_end_of_statement (parser);
5241 return expr;
5244 case CPP_OPEN_SQUARE:
5246 if (c_dialect_objc ())
5248 /* We might have an Objective-C++ message. */
5249 cp_parser_parse_tentatively (parser);
5250 tree msg = cp_parser_objc_message_expression (parser);
5251 /* If that works out, we're done ... */
5252 if (cp_parser_parse_definitely (parser))
5253 return msg;
5254 /* ... else, fall though to see if it's a lambda. */
5256 cp_expr lam = cp_parser_lambda_expression (parser);
5257 /* Don't warn about a failed tentative parse. */
5258 if (cp_parser_error_occurred (parser))
5259 return error_mark_node;
5260 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5261 return lam;
5264 case CPP_OBJC_STRING:
5265 if (c_dialect_objc ())
5266 /* We have an Objective-C++ string literal. */
5267 return cp_parser_objc_expression (parser);
5268 cp_parser_error (parser, "expected primary-expression");
5269 return error_mark_node;
5271 case CPP_KEYWORD:
5272 switch (token->keyword)
5274 /* These two are the boolean literals. */
5275 case RID_TRUE:
5276 cp_lexer_consume_token (parser->lexer);
5277 return cp_expr (boolean_true_node, token->location);
5278 case RID_FALSE:
5279 cp_lexer_consume_token (parser->lexer);
5280 return cp_expr (boolean_false_node, token->location);
5282 /* The `__null' literal. */
5283 case RID_NULL:
5284 cp_lexer_consume_token (parser->lexer);
5285 return cp_expr (null_node, token->location);
5287 /* The `nullptr' literal. */
5288 case RID_NULLPTR:
5289 cp_lexer_consume_token (parser->lexer);
5290 return cp_expr (nullptr_node, token->location);
5292 /* Recognize the `this' keyword. */
5293 case RID_THIS:
5294 cp_lexer_consume_token (parser->lexer);
5295 if (parser->local_variables_forbidden_p)
5297 error_at (token->location,
5298 "%<this%> may not be used in this context");
5299 return error_mark_node;
5301 /* Pointers cannot appear in constant-expressions. */
5302 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5303 return error_mark_node;
5304 return cp_expr (finish_this_expr (), token->location);
5306 /* The `operator' keyword can be the beginning of an
5307 id-expression. */
5308 case RID_OPERATOR:
5309 goto id_expression;
5311 case RID_FUNCTION_NAME:
5312 case RID_PRETTY_FUNCTION_NAME:
5313 case RID_C99_FUNCTION_NAME:
5315 non_integral_constant name;
5317 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5318 __func__ are the names of variables -- but they are
5319 treated specially. Therefore, they are handled here,
5320 rather than relying on the generic id-expression logic
5321 below. Grammatically, these names are id-expressions.
5323 Consume the token. */
5324 token = cp_lexer_consume_token (parser->lexer);
5326 switch (token->keyword)
5328 case RID_FUNCTION_NAME:
5329 name = NIC_FUNC_NAME;
5330 break;
5331 case RID_PRETTY_FUNCTION_NAME:
5332 name = NIC_PRETTY_FUNC;
5333 break;
5334 case RID_C99_FUNCTION_NAME:
5335 name = NIC_C99_FUNC;
5336 break;
5337 default:
5338 gcc_unreachable ();
5341 if (cp_parser_non_integral_constant_expression (parser, name))
5342 return error_mark_node;
5344 /* Look up the name. */
5345 return finish_fname (token->u.value);
5348 case RID_VA_ARG:
5350 tree expression;
5351 tree type;
5352 source_location type_location;
5353 location_t start_loc
5354 = cp_lexer_peek_token (parser->lexer)->location;
5355 /* The `__builtin_va_arg' construct is used to handle
5356 `va_arg'. Consume the `__builtin_va_arg' token. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* Look for the opening `('. */
5359 matching_parens parens;
5360 parens.require_open (parser);
5361 /* Now, parse the assignment-expression. */
5362 expression = cp_parser_assignment_expression (parser);
5363 /* Look for the `,'. */
5364 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5365 type_location = cp_lexer_peek_token (parser->lexer)->location;
5366 /* Parse the type-id. */
5368 type_id_in_expr_sentinel s (parser);
5369 type = cp_parser_type_id (parser);
5371 /* Look for the closing `)'. */
5372 location_t finish_loc
5373 = cp_lexer_peek_token (parser->lexer)->location;
5374 parens.require_close (parser);
5375 /* Using `va_arg' in a constant-expression is not
5376 allowed. */
5377 if (cp_parser_non_integral_constant_expression (parser,
5378 NIC_VA_ARG))
5379 return error_mark_node;
5380 /* Construct a location of the form:
5381 __builtin_va_arg (v, int)
5382 ~~~~~~~~~~~~~~~~~~~~~^~~~
5383 with the caret at the type, ranging from the start of the
5384 "__builtin_va_arg" token to the close paren. */
5385 location_t combined_loc
5386 = make_location (type_location, start_loc, finish_loc);
5387 return build_x_va_arg (combined_loc, expression, type);
5390 case RID_OFFSETOF:
5391 return cp_parser_builtin_offsetof (parser);
5393 case RID_HAS_NOTHROW_ASSIGN:
5394 case RID_HAS_NOTHROW_CONSTRUCTOR:
5395 case RID_HAS_NOTHROW_COPY:
5396 case RID_HAS_TRIVIAL_ASSIGN:
5397 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5398 case RID_HAS_TRIVIAL_COPY:
5399 case RID_HAS_TRIVIAL_DESTRUCTOR:
5400 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5401 case RID_HAS_VIRTUAL_DESTRUCTOR:
5402 case RID_IS_ABSTRACT:
5403 case RID_IS_AGGREGATE:
5404 case RID_IS_BASE_OF:
5405 case RID_IS_CLASS:
5406 case RID_IS_EMPTY:
5407 case RID_IS_ENUM:
5408 case RID_IS_FINAL:
5409 case RID_IS_LITERAL_TYPE:
5410 case RID_IS_POD:
5411 case RID_IS_POLYMORPHIC:
5412 case RID_IS_SAME_AS:
5413 case RID_IS_STD_LAYOUT:
5414 case RID_IS_TRIVIAL:
5415 case RID_IS_TRIVIALLY_ASSIGNABLE:
5416 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5417 case RID_IS_TRIVIALLY_COPYABLE:
5418 case RID_IS_UNION:
5419 case RID_IS_ASSIGNABLE:
5420 case RID_IS_CONSTRUCTIBLE:
5421 return cp_parser_trait_expr (parser, token->keyword);
5423 // C++ concepts
5424 case RID_REQUIRES:
5425 return cp_parser_requires_expression (parser);
5427 /* Objective-C++ expressions. */
5428 case RID_AT_ENCODE:
5429 case RID_AT_PROTOCOL:
5430 case RID_AT_SELECTOR:
5431 return cp_parser_objc_expression (parser);
5433 case RID_TEMPLATE:
5434 if (parser->in_function_body
5435 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5436 == CPP_LESS))
5438 error_at (token->location,
5439 "a template declaration cannot appear at block scope");
5440 cp_parser_skip_to_end_of_block_or_statement (parser);
5441 return error_mark_node;
5443 /* FALLTHRU */
5444 default:
5445 cp_parser_error (parser, "expected primary-expression");
5446 return error_mark_node;
5449 /* An id-expression can start with either an identifier, a
5450 `::' as the beginning of a qualified-id, or the "operator"
5451 keyword. */
5452 case CPP_NAME:
5453 case CPP_SCOPE:
5454 case CPP_TEMPLATE_ID:
5455 case CPP_NESTED_NAME_SPECIFIER:
5457 id_expression:
5458 cp_expr id_expression;
5459 cp_expr decl;
5460 const char *error_msg;
5461 bool template_p;
5462 bool done;
5463 cp_token *id_expr_token;
5465 /* Parse the id-expression. */
5466 id_expression
5467 = cp_parser_id_expression (parser,
5468 /*template_keyword_p=*/false,
5469 /*check_dependency_p=*/true,
5470 &template_p,
5471 /*declarator_p=*/false,
5472 /*optional_p=*/false);
5473 if (id_expression == error_mark_node)
5474 return error_mark_node;
5475 id_expr_token = token;
5476 token = cp_lexer_peek_token (parser->lexer);
5477 done = (token->type != CPP_OPEN_SQUARE
5478 && token->type != CPP_OPEN_PAREN
5479 && token->type != CPP_DOT
5480 && token->type != CPP_DEREF
5481 && token->type != CPP_PLUS_PLUS
5482 && token->type != CPP_MINUS_MINUS);
5483 /* If we have a template-id, then no further lookup is
5484 required. If the template-id was for a template-class, we
5485 will sometimes have a TYPE_DECL at this point. */
5486 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5487 || TREE_CODE (id_expression) == TYPE_DECL)
5488 decl = id_expression;
5489 /* Look up the name. */
5490 else
5492 tree ambiguous_decls;
5494 /* If we already know that this lookup is ambiguous, then
5495 we've already issued an error message; there's no reason
5496 to check again. */
5497 if (id_expr_token->type == CPP_NAME
5498 && id_expr_token->error_reported)
5500 cp_parser_simulate_error (parser);
5501 return error_mark_node;
5504 decl = cp_parser_lookup_name (parser, id_expression,
5505 none_type,
5506 template_p,
5507 /*is_namespace=*/false,
5508 /*check_dependency=*/true,
5509 &ambiguous_decls,
5510 id_expr_token->location);
5511 /* If the lookup was ambiguous, an error will already have
5512 been issued. */
5513 if (ambiguous_decls)
5514 return error_mark_node;
5516 /* In Objective-C++, we may have an Objective-C 2.0
5517 dot-syntax for classes here. */
5518 if (c_dialect_objc ()
5519 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5520 && TREE_CODE (decl) == TYPE_DECL
5521 && objc_is_class_name (decl))
5523 tree component;
5524 cp_lexer_consume_token (parser->lexer);
5525 component = cp_parser_identifier (parser);
5526 if (component == error_mark_node)
5527 return error_mark_node;
5529 tree result = objc_build_class_component_ref (id_expression,
5530 component);
5531 /* Build a location of the form:
5532 expr.component
5533 ~~~~~^~~~~~~~~
5534 with caret at the start of the component name (at
5535 input_location), ranging from the start of the id_expression
5536 to the end of the component name. */
5537 location_t combined_loc
5538 = make_location (input_location, id_expression.get_start (),
5539 get_finish (input_location));
5540 protected_set_expr_location (result, combined_loc);
5541 return result;
5544 /* In Objective-C++, an instance variable (ivar) may be preferred
5545 to whatever cp_parser_lookup_name() found.
5546 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5547 rest of c-family, we have to do a little extra work to preserve
5548 any location information in cp_expr "decl". Given that
5549 objc_lookup_ivar is implemented in "c-family" and "objc", we
5550 have a trip through the pure "tree" type, rather than cp_expr.
5551 Naively copying it back to "decl" would implicitly give the
5552 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5553 store an EXPR_LOCATION. Hence we only update "decl" (and
5554 hence its location_t) if we get back a different tree node. */
5555 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5556 id_expression);
5557 if (decl_tree != decl.get_value ())
5558 decl = cp_expr (decl_tree);
5560 /* If name lookup gives us a SCOPE_REF, then the
5561 qualifying scope was dependent. */
5562 if (TREE_CODE (decl) == SCOPE_REF)
5564 /* At this point, we do not know if DECL is a valid
5565 integral constant expression. We assume that it is
5566 in fact such an expression, so that code like:
5568 template <int N> struct A {
5569 int a[B<N>::i];
5572 is accepted. At template-instantiation time, we
5573 will check that B<N>::i is actually a constant. */
5574 return decl;
5576 /* Check to see if DECL is a local variable in a context
5577 where that is forbidden. */
5578 if (parser->local_variables_forbidden_p
5579 && local_variable_p (decl))
5581 /* It might be that we only found DECL because we are
5582 trying to be generous with pre-ISO scoping rules.
5583 For example, consider:
5585 int i;
5586 void g() {
5587 for (int i = 0; i < 10; ++i) {}
5588 extern void f(int j = i);
5591 Here, name look up will originally find the out
5592 of scope `i'. We need to issue a warning message,
5593 but then use the global `i'. */
5594 decl = check_for_out_of_scope_variable (decl);
5595 if (local_variable_p (decl))
5597 error_at (id_expr_token->location,
5598 "local variable %qD may not appear in this context",
5599 decl.get_value ());
5600 return error_mark_node;
5605 decl = (finish_id_expression
5606 (id_expression, decl, parser->scope,
5607 idk,
5608 parser->integral_constant_expression_p,
5609 parser->allow_non_integral_constant_expression_p,
5610 &parser->non_integral_constant_expression_p,
5611 template_p, done, address_p,
5612 template_arg_p,
5613 &error_msg,
5614 id_expression.get_location ()));
5615 if (error_msg)
5616 cp_parser_error (parser, error_msg);
5617 decl.set_location (id_expr_token->location);
5618 return decl;
5621 /* Anything else is an error. */
5622 default:
5623 cp_parser_error (parser, "expected primary-expression");
5624 return error_mark_node;
5628 static inline cp_expr
5629 cp_parser_primary_expression (cp_parser *parser,
5630 bool address_p,
5631 bool cast_p,
5632 bool template_arg_p,
5633 cp_id_kind *idk)
5635 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5636 /*decltype*/false, idk);
5639 /* Parse an id-expression.
5641 id-expression:
5642 unqualified-id
5643 qualified-id
5645 qualified-id:
5646 :: [opt] nested-name-specifier template [opt] unqualified-id
5647 :: identifier
5648 :: operator-function-id
5649 :: template-id
5651 Return a representation of the unqualified portion of the
5652 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5653 a `::' or nested-name-specifier.
5655 Often, if the id-expression was a qualified-id, the caller will
5656 want to make a SCOPE_REF to represent the qualified-id. This
5657 function does not do this in order to avoid wastefully creating
5658 SCOPE_REFs when they are not required.
5660 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5661 `template' keyword.
5663 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5664 uninstantiated templates.
5666 If *TEMPLATE_P is non-NULL, it is set to true iff the
5667 `template' keyword is used to explicitly indicate that the entity
5668 named is a template.
5670 If DECLARATOR_P is true, the id-expression is appearing as part of
5671 a declarator, rather than as part of an expression. */
5673 static cp_expr
5674 cp_parser_id_expression (cp_parser *parser,
5675 bool template_keyword_p,
5676 bool check_dependency_p,
5677 bool *template_p,
5678 bool declarator_p,
5679 bool optional_p)
5681 bool global_scope_p;
5682 bool nested_name_specifier_p;
5684 /* Assume the `template' keyword was not used. */
5685 if (template_p)
5686 *template_p = template_keyword_p;
5688 /* Look for the optional `::' operator. */
5689 global_scope_p
5690 = (!template_keyword_p
5691 && (cp_parser_global_scope_opt (parser,
5692 /*current_scope_valid_p=*/false)
5693 != NULL_TREE));
5695 /* Look for the optional nested-name-specifier. */
5696 nested_name_specifier_p
5697 = (cp_parser_nested_name_specifier_opt (parser,
5698 /*typename_keyword_p=*/false,
5699 check_dependency_p,
5700 /*type_p=*/false,
5701 declarator_p,
5702 template_keyword_p)
5703 != NULL_TREE);
5705 /* If there is a nested-name-specifier, then we are looking at
5706 the first qualified-id production. */
5707 if (nested_name_specifier_p)
5709 tree saved_scope;
5710 tree saved_object_scope;
5711 tree saved_qualifying_scope;
5712 cp_expr unqualified_id;
5713 bool is_template;
5715 /* See if the next token is the `template' keyword. */
5716 if (!template_p)
5717 template_p = &is_template;
5718 *template_p = cp_parser_optional_template_keyword (parser);
5719 /* Name lookup we do during the processing of the
5720 unqualified-id might obliterate SCOPE. */
5721 saved_scope = parser->scope;
5722 saved_object_scope = parser->object_scope;
5723 saved_qualifying_scope = parser->qualifying_scope;
5724 /* Process the final unqualified-id. */
5725 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5726 check_dependency_p,
5727 declarator_p,
5728 /*optional_p=*/false);
5729 /* Restore the SAVED_SCOPE for our caller. */
5730 parser->scope = saved_scope;
5731 parser->object_scope = saved_object_scope;
5732 parser->qualifying_scope = saved_qualifying_scope;
5734 return unqualified_id;
5736 /* Otherwise, if we are in global scope, then we are looking at one
5737 of the other qualified-id productions. */
5738 else if (global_scope_p)
5740 cp_token *token;
5741 tree id;
5743 /* Peek at the next token. */
5744 token = cp_lexer_peek_token (parser->lexer);
5746 /* If it's an identifier, and the next token is not a "<", then
5747 we can avoid the template-id case. This is an optimization
5748 for this common case. */
5749 if (token->type == CPP_NAME
5750 && !cp_parser_nth_token_starts_template_argument_list_p
5751 (parser, 2))
5752 return cp_parser_identifier (parser);
5754 cp_parser_parse_tentatively (parser);
5755 /* Try a template-id. */
5756 id = cp_parser_template_id (parser,
5757 /*template_keyword_p=*/false,
5758 /*check_dependency_p=*/true,
5759 none_type,
5760 declarator_p);
5761 /* If that worked, we're done. */
5762 if (cp_parser_parse_definitely (parser))
5763 return id;
5765 /* Peek at the next token. (Changes in the token buffer may
5766 have invalidated the pointer obtained above.) */
5767 token = cp_lexer_peek_token (parser->lexer);
5769 switch (token->type)
5771 case CPP_NAME:
5772 return cp_parser_identifier (parser);
5774 case CPP_KEYWORD:
5775 if (token->keyword == RID_OPERATOR)
5776 return cp_parser_operator_function_id (parser);
5777 /* Fall through. */
5779 default:
5780 cp_parser_error (parser, "expected id-expression");
5781 return error_mark_node;
5784 else
5785 return cp_parser_unqualified_id (parser, template_keyword_p,
5786 /*check_dependency_p=*/true,
5787 declarator_p,
5788 optional_p);
5791 /* Parse an unqualified-id.
5793 unqualified-id:
5794 identifier
5795 operator-function-id
5796 conversion-function-id
5797 ~ class-name
5798 template-id
5800 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5801 keyword, in a construct like `A::template ...'.
5803 Returns a representation of unqualified-id. For the `identifier'
5804 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5805 production a BIT_NOT_EXPR is returned; the operand of the
5806 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5807 other productions, see the documentation accompanying the
5808 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5809 names are looked up in uninstantiated templates. If DECLARATOR_P
5810 is true, the unqualified-id is appearing as part of a declarator,
5811 rather than as part of an expression. */
5813 static cp_expr
5814 cp_parser_unqualified_id (cp_parser* parser,
5815 bool template_keyword_p,
5816 bool check_dependency_p,
5817 bool declarator_p,
5818 bool optional_p)
5820 cp_token *token;
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5825 switch ((int) token->type)
5827 case CPP_NAME:
5829 tree id;
5831 /* We don't know yet whether or not this will be a
5832 template-id. */
5833 cp_parser_parse_tentatively (parser);
5834 /* Try a template-id. */
5835 id = cp_parser_template_id (parser, template_keyword_p,
5836 check_dependency_p,
5837 none_type,
5838 declarator_p);
5839 /* If it worked, we're done. */
5840 if (cp_parser_parse_definitely (parser))
5841 return id;
5842 /* Otherwise, it's an ordinary identifier. */
5843 return cp_parser_identifier (parser);
5846 case CPP_TEMPLATE_ID:
5847 return cp_parser_template_id (parser, template_keyword_p,
5848 check_dependency_p,
5849 none_type,
5850 declarator_p);
5852 case CPP_COMPL:
5854 tree type_decl;
5855 tree qualifying_scope;
5856 tree object_scope;
5857 tree scope;
5858 bool done;
5860 /* Consume the `~' token. */
5861 cp_lexer_consume_token (parser->lexer);
5862 /* Parse the class-name. The standard, as written, seems to
5863 say that:
5865 template <typename T> struct S { ~S (); };
5866 template <typename T> S<T>::~S() {}
5868 is invalid, since `~' must be followed by a class-name, but
5869 `S<T>' is dependent, and so not known to be a class.
5870 That's not right; we need to look in uninstantiated
5871 templates. A further complication arises from:
5873 template <typename T> void f(T t) {
5874 t.T::~T();
5877 Here, it is not possible to look up `T' in the scope of `T'
5878 itself. We must look in both the current scope, and the
5879 scope of the containing complete expression.
5881 Yet another issue is:
5883 struct S {
5884 int S;
5885 ~S();
5888 S::~S() {}
5890 The standard does not seem to say that the `S' in `~S'
5891 should refer to the type `S' and not the data member
5892 `S::S'. */
5894 /* DR 244 says that we look up the name after the "~" in the
5895 same scope as we looked up the qualifying name. That idea
5896 isn't fully worked out; it's more complicated than that. */
5897 scope = parser->scope;
5898 object_scope = parser->object_scope;
5899 qualifying_scope = parser->qualifying_scope;
5901 /* Check for invalid scopes. */
5902 if (scope == error_mark_node)
5904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5905 cp_lexer_consume_token (parser->lexer);
5906 return error_mark_node;
5908 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5910 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5911 error_at (token->location,
5912 "scope %qT before %<~%> is not a class-name",
5913 scope);
5914 cp_parser_simulate_error (parser);
5915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5916 cp_lexer_consume_token (parser->lexer);
5917 return error_mark_node;
5919 gcc_assert (!scope || TYPE_P (scope));
5921 /* If the name is of the form "X::~X" it's OK even if X is a
5922 typedef. */
5923 token = cp_lexer_peek_token (parser->lexer);
5924 if (scope
5925 && token->type == CPP_NAME
5926 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5927 != CPP_LESS)
5928 && (token->u.value == TYPE_IDENTIFIER (scope)
5929 || (CLASS_TYPE_P (scope)
5930 && constructor_name_p (token->u.value, scope))))
5932 cp_lexer_consume_token (parser->lexer);
5933 return build_nt (BIT_NOT_EXPR, scope);
5936 /* ~auto means the destructor of whatever the object is. */
5937 if (cp_parser_is_keyword (token, RID_AUTO))
5939 if (cxx_dialect < cxx14)
5940 pedwarn (input_location, 0,
5941 "%<~auto%> only available with "
5942 "-std=c++14 or -std=gnu++14");
5943 cp_lexer_consume_token (parser->lexer);
5944 return build_nt (BIT_NOT_EXPR, make_auto ());
5947 /* If there was an explicit qualification (S::~T), first look
5948 in the scope given by the qualification (i.e., S).
5950 Note: in the calls to cp_parser_class_name below we pass
5951 typename_type so that lookup finds the injected-class-name
5952 rather than the constructor. */
5953 done = false;
5954 type_decl = NULL_TREE;
5955 if (scope)
5957 cp_parser_parse_tentatively (parser);
5958 type_decl = cp_parser_class_name (parser,
5959 /*typename_keyword_p=*/false,
5960 /*template_keyword_p=*/false,
5961 typename_type,
5962 /*check_dependency=*/false,
5963 /*class_head_p=*/false,
5964 declarator_p);
5965 if (cp_parser_parse_definitely (parser))
5966 done = true;
5968 /* In "N::S::~S", look in "N" as well. */
5969 if (!done && scope && qualifying_scope)
5971 cp_parser_parse_tentatively (parser);
5972 parser->scope = qualifying_scope;
5973 parser->object_scope = NULL_TREE;
5974 parser->qualifying_scope = NULL_TREE;
5975 type_decl
5976 = cp_parser_class_name (parser,
5977 /*typename_keyword_p=*/false,
5978 /*template_keyword_p=*/false,
5979 typename_type,
5980 /*check_dependency=*/false,
5981 /*class_head_p=*/false,
5982 declarator_p);
5983 if (cp_parser_parse_definitely (parser))
5984 done = true;
5986 /* In "p->S::~T", look in the scope given by "*p" as well. */
5987 else if (!done && object_scope)
5989 cp_parser_parse_tentatively (parser);
5990 parser->scope = object_scope;
5991 parser->object_scope = NULL_TREE;
5992 parser->qualifying_scope = NULL_TREE;
5993 type_decl
5994 = cp_parser_class_name (parser,
5995 /*typename_keyword_p=*/false,
5996 /*template_keyword_p=*/false,
5997 typename_type,
5998 /*check_dependency=*/false,
5999 /*class_head_p=*/false,
6000 declarator_p);
6001 if (cp_parser_parse_definitely (parser))
6002 done = true;
6004 /* Look in the surrounding context. */
6005 if (!done)
6007 parser->scope = NULL_TREE;
6008 parser->object_scope = NULL_TREE;
6009 parser->qualifying_scope = NULL_TREE;
6010 if (processing_template_decl)
6011 cp_parser_parse_tentatively (parser);
6012 type_decl
6013 = cp_parser_class_name (parser,
6014 /*typename_keyword_p=*/false,
6015 /*template_keyword_p=*/false,
6016 typename_type,
6017 /*check_dependency=*/false,
6018 /*class_head_p=*/false,
6019 declarator_p);
6020 if (processing_template_decl
6021 && ! cp_parser_parse_definitely (parser))
6023 /* We couldn't find a type with this name. If we're parsing
6024 tentatively, fail and try something else. */
6025 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6027 cp_parser_simulate_error (parser);
6028 return error_mark_node;
6030 /* Otherwise, accept it and check for a match at instantiation
6031 time. */
6032 type_decl = cp_parser_identifier (parser);
6033 if (type_decl != error_mark_node)
6034 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6035 return type_decl;
6038 /* If an error occurred, assume that the name of the
6039 destructor is the same as the name of the qualifying
6040 class. That allows us to keep parsing after running
6041 into ill-formed destructor names. */
6042 if (type_decl == error_mark_node && scope)
6043 return build_nt (BIT_NOT_EXPR, scope);
6044 else if (type_decl == error_mark_node)
6045 return error_mark_node;
6047 /* Check that destructor name and scope match. */
6048 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6051 error_at (token->location,
6052 "declaration of %<~%T%> as member of %qT",
6053 type_decl, scope);
6054 cp_parser_simulate_error (parser);
6055 return error_mark_node;
6058 /* [class.dtor]
6060 A typedef-name that names a class shall not be used as the
6061 identifier in the declarator for a destructor declaration. */
6062 if (declarator_p
6063 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6064 && !DECL_SELF_REFERENCE_P (type_decl)
6065 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6066 error_at (token->location,
6067 "typedef-name %qD used as destructor declarator",
6068 type_decl);
6070 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6073 case CPP_KEYWORD:
6074 if (token->keyword == RID_OPERATOR)
6076 cp_expr id;
6078 /* This could be a template-id, so we try that first. */
6079 cp_parser_parse_tentatively (parser);
6080 /* Try a template-id. */
6081 id = cp_parser_template_id (parser, template_keyword_p,
6082 /*check_dependency_p=*/true,
6083 none_type,
6084 declarator_p);
6085 /* If that worked, we're done. */
6086 if (cp_parser_parse_definitely (parser))
6087 return id;
6088 /* We still don't know whether we're looking at an
6089 operator-function-id or a conversion-function-id. */
6090 cp_parser_parse_tentatively (parser);
6091 /* Try an operator-function-id. */
6092 id = cp_parser_operator_function_id (parser);
6093 /* If that didn't work, try a conversion-function-id. */
6094 if (!cp_parser_parse_definitely (parser))
6095 id = cp_parser_conversion_function_id (parser);
6096 else if (UDLIT_OPER_P (id))
6098 /* 17.6.3.3.5 */
6099 const char *name = UDLIT_OP_SUFFIX (id);
6100 if (name[0] != '_' && !in_system_header_at (input_location)
6101 && declarator_p)
6102 warning (OPT_Wliteral_suffix,
6103 "literal operator suffixes not preceded by %<_%>"
6104 " are reserved for future standardization");
6107 return id;
6109 /* Fall through. */
6111 default:
6112 if (optional_p)
6113 return NULL_TREE;
6114 cp_parser_error (parser, "expected unqualified-id");
6115 return error_mark_node;
6119 /* Parse an (optional) nested-name-specifier.
6121 nested-name-specifier: [C++98]
6122 class-or-namespace-name :: nested-name-specifier [opt]
6123 class-or-namespace-name :: template nested-name-specifier [opt]
6125 nested-name-specifier: [C++0x]
6126 type-name ::
6127 namespace-name ::
6128 nested-name-specifier identifier ::
6129 nested-name-specifier template [opt] simple-template-id ::
6131 PARSER->SCOPE should be set appropriately before this function is
6132 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6134 in name lookups.
6136 Sets PARSER->SCOPE to the class (TYPE) or namespace
6137 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138 it unchanged if there is no nested-name-specifier. Returns the new
6139 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6141 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142 part of a declaration and/or decl-specifier. */
6144 static tree
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146 bool typename_keyword_p,
6147 bool check_dependency_p,
6148 bool type_p,
6149 bool is_declaration,
6150 bool template_keyword_p /* = false */)
6152 bool success = false;
6153 cp_token_position start = 0;
6154 cp_token *token;
6156 /* Remember where the nested-name-specifier starts. */
6157 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6159 start = cp_lexer_token_position (parser->lexer, false);
6160 push_deferring_access_checks (dk_deferred);
6163 while (true)
6165 tree new_scope;
6166 tree old_scope;
6167 tree saved_qualifying_scope;
6169 /* Spot cases that cannot be the beginning of a
6170 nested-name-specifier. */
6171 token = cp_lexer_peek_token (parser->lexer);
6173 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174 the already parsed nested-name-specifier. */
6175 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6177 /* Grab the nested-name-specifier and continue the loop. */
6178 cp_parser_pre_parsed_nested_name_specifier (parser);
6179 /* If we originally encountered this nested-name-specifier
6180 with IS_DECLARATION set to false, we will not have
6181 resolved TYPENAME_TYPEs, so we must do so here. */
6182 if (is_declaration
6183 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6185 new_scope = resolve_typename_type (parser->scope,
6186 /*only_current_p=*/false);
6187 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188 parser->scope = new_scope;
6190 success = true;
6191 continue;
6194 /* Spot cases that cannot be the beginning of a
6195 nested-name-specifier. On the second and subsequent times
6196 through the loop, we look for the `template' keyword. */
6197 if (success && token->keyword == RID_TEMPLATE)
6199 /* A template-id can start a nested-name-specifier. */
6200 else if (token->type == CPP_TEMPLATE_ID)
6202 /* DR 743: decltype can be used in a nested-name-specifier. */
6203 else if (token_is_decltype (token))
6205 else
6207 /* If the next token is not an identifier, then it is
6208 definitely not a type-name or namespace-name. */
6209 if (token->type != CPP_NAME)
6210 break;
6211 /* If the following token is neither a `<' (to begin a
6212 template-id), nor a `::', then we are not looking at a
6213 nested-name-specifier. */
6214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6216 if (token->type == CPP_COLON
6217 && parser->colon_corrects_to_scope_p
6218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6220 gcc_rich_location richloc (token->location);
6221 richloc.add_fixit_replace ("::");
6222 error_at (&richloc,
6223 "found %<:%> in nested-name-specifier, "
6224 "expected %<::%>");
6225 token->type = CPP_SCOPE;
6228 if (token->type != CPP_SCOPE
6229 && !cp_parser_nth_token_starts_template_argument_list_p
6230 (parser, 2))
6231 break;
6234 /* The nested-name-specifier is optional, so we parse
6235 tentatively. */
6236 cp_parser_parse_tentatively (parser);
6238 /* Look for the optional `template' keyword, if this isn't the
6239 first time through the loop. */
6240 if (success)
6241 template_keyword_p = cp_parser_optional_template_keyword (parser);
6243 /* Save the old scope since the name lookup we are about to do
6244 might destroy it. */
6245 old_scope = parser->scope;
6246 saved_qualifying_scope = parser->qualifying_scope;
6247 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248 look up names in "X<T>::I" in order to determine that "Y" is
6249 a template. So, if we have a typename at this point, we make
6250 an effort to look through it. */
6251 if (is_declaration
6252 && !typename_keyword_p
6253 && parser->scope
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255 parser->scope = resolve_typename_type (parser->scope,
6256 /*only_current_p=*/false);
6257 /* Parse the qualifying entity. */
6258 new_scope
6259 = cp_parser_qualifying_entity (parser,
6260 typename_keyword_p,
6261 template_keyword_p,
6262 check_dependency_p,
6263 type_p,
6264 is_declaration);
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6268 /* If we found what we wanted, we keep going; otherwise, we're
6269 done. */
6270 if (!cp_parser_parse_definitely (parser))
6272 bool error_p = false;
6274 /* Restore the OLD_SCOPE since it was valid before the
6275 failed attempt at finding the last
6276 class-or-namespace-name. */
6277 parser->scope = old_scope;
6278 parser->qualifying_scope = saved_qualifying_scope;
6280 /* If the next token is a decltype, and the one after that is a
6281 `::', then the decltype has failed to resolve to a class or
6282 enumeration type. Give this error even when parsing
6283 tentatively since it can't possibly be valid--and we're going
6284 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285 won't get another chance.*/
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6288 == CPP_SCOPE))
6290 token = cp_lexer_consume_token (parser->lexer);
6291 error_at (token->location, "decltype evaluates to %qT, "
6292 "which is not a class or enumeration type",
6293 token->u.tree_check_value->value);
6294 parser->scope = error_mark_node;
6295 error_p = true;
6296 /* As below. */
6297 success = true;
6298 cp_lexer_consume_token (parser->lexer);
6301 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6304 /* If we have a non-type template-id followed by ::, it can't
6305 possibly be valid. */
6306 token = cp_lexer_peek_token (parser->lexer);
6307 tree tid = token->u.tree_check_value->value;
6308 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6311 tree tmpl = NULL_TREE;
6312 if (is_overloaded_fn (tid))
6314 tree fns = get_fns (tid);
6315 if (OVL_SINGLE_P (fns))
6316 tmpl = OVL_FIRST (fns);
6317 error_at (token->location, "function template-id %qD "
6318 "in nested-name-specifier", tid);
6320 else
6322 /* Variable template. */
6323 tmpl = TREE_OPERAND (tid, 0);
6324 gcc_assert (variable_template_p (tmpl));
6325 error_at (token->location, "variable template-id %qD "
6326 "in nested-name-specifier", tid);
6328 if (tmpl)
6329 inform (DECL_SOURCE_LOCATION (tmpl),
6330 "%qD declared here", tmpl);
6332 parser->scope = error_mark_node;
6333 error_p = true;
6334 /* As below. */
6335 success = true;
6336 cp_lexer_consume_token (parser->lexer);
6337 cp_lexer_consume_token (parser->lexer);
6341 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6342 break;
6343 /* If the next token is an identifier, and the one after
6344 that is a `::', then any valid interpretation would have
6345 found a class-or-namespace-name. */
6346 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6348 == CPP_SCOPE)
6349 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6350 != CPP_COMPL))
6352 token = cp_lexer_consume_token (parser->lexer);
6353 if (!error_p)
6355 if (!token->error_reported)
6357 tree decl;
6358 tree ambiguous_decls;
6360 decl = cp_parser_lookup_name (parser, token->u.value,
6361 none_type,
6362 /*is_template=*/false,
6363 /*is_namespace=*/false,
6364 /*check_dependency=*/true,
6365 &ambiguous_decls,
6366 token->location);
6367 if (TREE_CODE (decl) == TEMPLATE_DECL)
6368 error_at (token->location,
6369 "%qD used without template parameters",
6370 decl);
6371 else if (ambiguous_decls)
6373 // cp_parser_lookup_name has the same diagnostic,
6374 // thus make sure to emit it at most once.
6375 if (cp_parser_uncommitted_to_tentative_parse_p
6376 (parser))
6378 error_at (token->location,
6379 "reference to %qD is ambiguous",
6380 token->u.value);
6381 print_candidates (ambiguous_decls);
6383 decl = error_mark_node;
6385 else
6387 if (cxx_dialect != cxx98)
6388 cp_parser_name_lookup_error
6389 (parser, token->u.value, decl, NLE_NOT_CXX98,
6390 token->location);
6391 else
6392 cp_parser_name_lookup_error
6393 (parser, token->u.value, decl, NLE_CXX98,
6394 token->location);
6397 parser->scope = error_mark_node;
6398 error_p = true;
6399 /* Treat this as a successful nested-name-specifier
6400 due to:
6402 [basic.lookup.qual]
6404 If the name found is not a class-name (clause
6405 _class_) or namespace-name (_namespace.def_), the
6406 program is ill-formed. */
6407 success = true;
6409 cp_lexer_consume_token (parser->lexer);
6411 break;
6413 /* We've found one valid nested-name-specifier. */
6414 success = true;
6415 /* Name lookup always gives us a DECL. */
6416 if (TREE_CODE (new_scope) == TYPE_DECL)
6417 new_scope = TREE_TYPE (new_scope);
6418 /* Uses of "template" must be followed by actual templates. */
6419 if (template_keyword_p
6420 && !(CLASS_TYPE_P (new_scope)
6421 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426 == TEMPLATE_ID_EXPR)))
6427 permerror (input_location, TYPE_P (new_scope)
6428 ? G_("%qT is not a template")
6429 : G_("%qD is not a template"),
6430 new_scope);
6431 /* If it is a class scope, try to complete it; we are about to
6432 be looking up names inside the class. */
6433 if (TYPE_P (new_scope)
6434 /* Since checking types for dependency can be expensive,
6435 avoid doing it if the type is already complete. */
6436 && !COMPLETE_TYPE_P (new_scope)
6437 /* Do not try to complete dependent types. */
6438 && !dependent_type_p (new_scope))
6440 new_scope = complete_type (new_scope);
6441 /* If it is a typedef to current class, use the current
6442 class instead, as the typedef won't have any names inside
6443 it yet. */
6444 if (!COMPLETE_TYPE_P (new_scope)
6445 && currently_open_class (new_scope))
6446 new_scope = TYPE_MAIN_VARIANT (new_scope);
6448 /* Make sure we look in the right scope the next time through
6449 the loop. */
6450 parser->scope = new_scope;
6453 /* If parsing tentatively, replace the sequence of tokens that makes
6454 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455 token. That way, should we re-parse the token stream, we will
6456 not have to repeat the effort required to do the parse, nor will
6457 we issue duplicate error messages. */
6458 if (success && start)
6460 cp_token *token;
6462 token = cp_lexer_token_at (parser->lexer, start);
6463 /* Reset the contents of the START token. */
6464 token->type = CPP_NESTED_NAME_SPECIFIER;
6465 /* Retrieve any deferred checks. Do not pop this access checks yet
6466 so the memory will not be reclaimed during token replacing below. */
6467 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468 token->u.tree_check_value->value = parser->scope;
6469 token->u.tree_check_value->checks = get_deferred_access_checks ();
6470 token->u.tree_check_value->qualifying_scope =
6471 parser->qualifying_scope;
6472 token->keyword = RID_MAX;
6474 /* Purge all subsequent tokens. */
6475 cp_lexer_purge_tokens_after (parser->lexer, start);
6478 if (start)
6479 pop_to_parent_deferring_access_checks ();
6481 return success ? parser->scope : NULL_TREE;
6484 /* Parse a nested-name-specifier. See
6485 cp_parser_nested_name_specifier_opt for details. This function
6486 behaves identically, except that it will an issue an error if no
6487 nested-name-specifier is present. */
6489 static tree
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491 bool typename_keyword_p,
6492 bool check_dependency_p,
6493 bool type_p,
6494 bool is_declaration)
6496 tree scope;
6498 /* Look for the nested-name-specifier. */
6499 scope = cp_parser_nested_name_specifier_opt (parser,
6500 typename_keyword_p,
6501 check_dependency_p,
6502 type_p,
6503 is_declaration);
6504 /* If it was not present, issue an error message. */
6505 if (!scope)
6507 cp_parser_error (parser, "expected nested-name-specifier");
6508 parser->scope = NULL_TREE;
6511 return scope;
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515 this is either a class-name or a namespace-name (which corresponds
6516 to the class-or-namespace-name production in the grammar). For
6517 C++0x, it can also be a type-name that refers to an enumeration
6518 type or a simple-template-id.
6520 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523 TYPE_P is TRUE iff the next name should be taken as a class-name,
6524 even the same name is declared to be another entity in the same
6525 scope.
6527 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528 specified by the class-or-namespace-name. If neither is found the
6529 ERROR_MARK_NODE is returned. */
6531 static tree
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533 bool typename_keyword_p,
6534 bool template_keyword_p,
6535 bool check_dependency_p,
6536 bool type_p,
6537 bool is_declaration)
6539 tree saved_scope;
6540 tree saved_qualifying_scope;
6541 tree saved_object_scope;
6542 tree scope;
6543 bool only_class_p;
6544 bool successful_parse_p;
6546 /* DR 743: decltype can appear in a nested-name-specifier. */
6547 if (cp_lexer_next_token_is_decltype (parser->lexer))
6549 scope = cp_parser_decltype (parser);
6550 if (TREE_CODE (scope) != ENUMERAL_TYPE
6551 && !MAYBE_CLASS_TYPE_P (scope))
6553 cp_parser_simulate_error (parser);
6554 return error_mark_node;
6556 if (TYPE_NAME (scope))
6557 scope = TYPE_NAME (scope);
6558 return scope;
6561 /* Before we try to parse the class-name, we must save away the
6562 current PARSER->SCOPE since cp_parser_class_name will destroy
6563 it. */
6564 saved_scope = parser->scope;
6565 saved_qualifying_scope = parser->qualifying_scope;
6566 saved_object_scope = parser->object_scope;
6567 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6568 there is no need to look for a namespace-name. */
6569 only_class_p = template_keyword_p
6570 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6571 if (!only_class_p)
6572 cp_parser_parse_tentatively (parser);
6573 scope = cp_parser_class_name (parser,
6574 typename_keyword_p,
6575 template_keyword_p,
6576 type_p ? class_type : none_type,
6577 check_dependency_p,
6578 /*class_head_p=*/false,
6579 is_declaration,
6580 /*enum_ok=*/cxx_dialect > cxx98);
6581 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582 /* If that didn't work, try for a namespace-name. */
6583 if (!only_class_p && !successful_parse_p)
6585 /* Restore the saved scope. */
6586 parser->scope = saved_scope;
6587 parser->qualifying_scope = saved_qualifying_scope;
6588 parser->object_scope = saved_object_scope;
6589 /* If we are not looking at an identifier followed by the scope
6590 resolution operator, then this is not part of a
6591 nested-name-specifier. (Note that this function is only used
6592 to parse the components of a nested-name-specifier.) */
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595 return error_mark_node;
6596 scope = cp_parser_namespace_name (parser);
6599 return scope;
6602 /* Return true if we are looking at a compound-literal, false otherwise. */
6604 static bool
6605 cp_parser_compound_literal_p (cp_parser *parser)
6607 cp_lexer_save_tokens (parser->lexer);
6609 /* Skip tokens until the next token is a closing parenthesis.
6610 If we find the closing `)', and the next token is a `{', then
6611 we are looking at a compound-literal. */
6612 bool compound_literal_p
6613 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614 /*consume_paren=*/true)
6615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6617 /* Roll back the tokens we skipped. */
6618 cp_lexer_rollback_tokens (parser->lexer);
6620 return compound_literal_p;
6623 /* Parse a postfix-expression.
6625 postfix-expression:
6626 primary-expression
6627 postfix-expression [ expression ]
6628 postfix-expression ( expression-list [opt] )
6629 simple-type-specifier ( expression-list [opt] )
6630 typename :: [opt] nested-name-specifier identifier
6631 ( expression-list [opt] )
6632 typename :: [opt] nested-name-specifier template [opt] template-id
6633 ( expression-list [opt] )
6634 postfix-expression . template [opt] id-expression
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression . pseudo-destructor-name
6637 postfix-expression -> pseudo-destructor-name
6638 postfix-expression ++
6639 postfix-expression --
6640 dynamic_cast < type-id > ( expression )
6641 static_cast < type-id > ( expression )
6642 reinterpret_cast < type-id > ( expression )
6643 const_cast < type-id > ( expression )
6644 typeid ( expression )
6645 typeid ( type-id )
6647 GNU Extension:
6649 postfix-expression:
6650 ( type-id ) { initializer-list , [opt] }
6652 This extension is a GNU version of the C99 compound-literal
6653 construct. (The C99 grammar uses `type-name' instead of `type-id',
6654 but they are essentially the same concept.)
6656 If ADDRESS_P is true, the postfix expression is the operand of the
6657 `&' operator. CAST_P is true if this expression is the target of a
6658 cast.
6660 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6661 class member access expressions [expr.ref].
6663 Returns a representation of the expression. */
6665 static cp_expr
6666 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6667 bool member_access_only_p, bool decltype_p,
6668 cp_id_kind * pidk_return)
6670 cp_token *token;
6671 location_t loc;
6672 enum rid keyword;
6673 cp_id_kind idk = CP_ID_KIND_NONE;
6674 cp_expr postfix_expression = NULL_TREE;
6675 bool is_member_access = false;
6677 /* Peek at the next token. */
6678 token = cp_lexer_peek_token (parser->lexer);
6679 loc = token->location;
6680 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6682 /* Some of the productions are determined by keywords. */
6683 keyword = token->keyword;
6684 switch (keyword)
6686 case RID_DYNCAST:
6687 case RID_STATCAST:
6688 case RID_REINTCAST:
6689 case RID_CONSTCAST:
6691 tree type;
6692 cp_expr expression;
6693 const char *saved_message;
6694 bool saved_in_type_id_in_expr_p;
6696 /* All of these can be handled in the same way from the point
6697 of view of parsing. Begin by consuming the token
6698 identifying the cast. */
6699 cp_lexer_consume_token (parser->lexer);
6701 /* New types cannot be defined in the cast. */
6702 saved_message = parser->type_definition_forbidden_message;
6703 parser->type_definition_forbidden_message
6704 = G_("types may not be defined in casts");
6706 /* Look for the opening `<'. */
6707 cp_parser_require (parser, CPP_LESS, RT_LESS);
6708 /* Parse the type to which we are casting. */
6709 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6710 parser->in_type_id_in_expr_p = true;
6711 type = cp_parser_type_id (parser);
6712 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6713 /* Look for the closing `>'. */
6714 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6715 /* Restore the old message. */
6716 parser->type_definition_forbidden_message = saved_message;
6718 bool saved_greater_than_is_operator_p
6719 = parser->greater_than_is_operator_p;
6720 parser->greater_than_is_operator_p = true;
6722 /* And the expression which is being cast. */
6723 matching_parens parens;
6724 parens.require_open (parser);
6725 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6726 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6727 RT_CLOSE_PAREN);
6728 location_t end_loc = close_paren ?
6729 close_paren->location : UNKNOWN_LOCATION;
6731 parser->greater_than_is_operator_p
6732 = saved_greater_than_is_operator_p;
6734 /* Only type conversions to integral or enumeration types
6735 can be used in constant-expressions. */
6736 if (!cast_valid_in_integral_constant_expression_p (type)
6737 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6739 postfix_expression = error_mark_node;
6740 break;
6743 switch (keyword)
6745 case RID_DYNCAST:
6746 postfix_expression
6747 = build_dynamic_cast (type, expression, tf_warning_or_error);
6748 break;
6749 case RID_STATCAST:
6750 postfix_expression
6751 = build_static_cast (type, expression, tf_warning_or_error);
6752 break;
6753 case RID_REINTCAST:
6754 postfix_expression
6755 = build_reinterpret_cast (type, expression,
6756 tf_warning_or_error);
6757 break;
6758 case RID_CONSTCAST:
6759 postfix_expression
6760 = build_const_cast (type, expression, tf_warning_or_error);
6761 break;
6762 default:
6763 gcc_unreachable ();
6766 /* Construct a location e.g. :
6767 reinterpret_cast <int *> (expr)
6768 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6769 ranging from the start of the "*_cast" token to the final closing
6770 paren, with the caret at the start. */
6771 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6772 postfix_expression.set_location (cp_cast_loc);
6774 break;
6776 case RID_TYPEID:
6778 tree type;
6779 const char *saved_message;
6780 bool saved_in_type_id_in_expr_p;
6782 /* Consume the `typeid' token. */
6783 cp_lexer_consume_token (parser->lexer);
6784 /* Look for the `(' token. */
6785 matching_parens parens;
6786 parens.require_open (parser);
6787 /* Types cannot be defined in a `typeid' expression. */
6788 saved_message = parser->type_definition_forbidden_message;
6789 parser->type_definition_forbidden_message
6790 = G_("types may not be defined in a %<typeid%> expression");
6791 /* We can't be sure yet whether we're looking at a type-id or an
6792 expression. */
6793 cp_parser_parse_tentatively (parser);
6794 /* Try a type-id first. */
6795 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6796 parser->in_type_id_in_expr_p = true;
6797 type = cp_parser_type_id (parser);
6798 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6799 /* Look for the `)' token. Otherwise, we can't be sure that
6800 we're not looking at an expression: consider `typeid (int
6801 (3))', for example. */
6802 cp_token *close_paren = parens.require_close (parser);
6803 /* If all went well, simply lookup the type-id. */
6804 if (cp_parser_parse_definitely (parser))
6805 postfix_expression = get_typeid (type, tf_warning_or_error);
6806 /* Otherwise, fall back to the expression variant. */
6807 else
6809 tree expression;
6811 /* Look for an expression. */
6812 expression = cp_parser_expression (parser, & idk);
6813 /* Compute its typeid. */
6814 postfix_expression = build_typeid (expression, tf_warning_or_error);
6815 /* Look for the `)' token. */
6816 close_paren = parens.require_close (parser);
6818 /* Restore the saved message. */
6819 parser->type_definition_forbidden_message = saved_message;
6820 /* `typeid' may not appear in an integral constant expression. */
6821 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6822 postfix_expression = error_mark_node;
6824 /* Construct a location e.g. :
6825 typeid (expr)
6826 ^~~~~~~~~~~~~
6827 ranging from the start of the "typeid" token to the final closing
6828 paren, with the caret at the start. */
6829 if (close_paren)
6831 location_t typeid_loc
6832 = make_location (start_loc, start_loc, close_paren->location);
6833 postfix_expression.set_location (typeid_loc);
6836 break;
6838 case RID_TYPENAME:
6840 tree type;
6841 /* The syntax permitted here is the same permitted for an
6842 elaborated-type-specifier. */
6843 ++parser->prevent_constrained_type_specifiers;
6844 type = cp_parser_elaborated_type_specifier (parser,
6845 /*is_friend=*/false,
6846 /*is_declaration=*/false);
6847 --parser->prevent_constrained_type_specifiers;
6848 postfix_expression = cp_parser_functional_cast (parser, type);
6850 break;
6852 case RID_ADDRESSOF:
6853 case RID_BUILTIN_SHUFFLE:
6854 case RID_BUILTIN_LAUNDER:
6856 vec<tree, va_gc> *vec;
6857 unsigned int i;
6858 tree p;
6860 cp_lexer_consume_token (parser->lexer);
6861 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6862 /*cast_p=*/false, /*allow_expansion_p=*/true,
6863 /*non_constant_p=*/NULL);
6864 if (vec == NULL)
6866 postfix_expression = error_mark_node;
6867 break;
6870 FOR_EACH_VEC_ELT (*vec, i, p)
6871 mark_exp_read (p);
6873 switch (keyword)
6875 case RID_ADDRESSOF:
6876 if (vec->length () == 1)
6877 postfix_expression
6878 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6879 else
6881 error_at (loc, "wrong number of arguments to "
6882 "%<__builtin_addressof%>");
6883 postfix_expression = error_mark_node;
6885 break;
6887 case RID_BUILTIN_LAUNDER:
6888 if (vec->length () == 1)
6889 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6890 tf_warning_or_error);
6891 else
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_launder%>");
6895 postfix_expression = error_mark_node;
6897 break;
6899 case RID_BUILTIN_SHUFFLE:
6900 if (vec->length () == 2)
6901 postfix_expression
6902 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6903 (*vec)[1], tf_warning_or_error);
6904 else if (vec->length () == 3)
6905 postfix_expression
6906 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6907 (*vec)[2], tf_warning_or_error);
6908 else
6910 error_at (loc, "wrong number of arguments to "
6911 "%<__builtin_shuffle%>");
6912 postfix_expression = error_mark_node;
6914 break;
6916 default:
6917 gcc_unreachable ();
6919 break;
6922 default:
6924 tree type;
6926 /* If the next thing is a simple-type-specifier, we may be
6927 looking at a functional cast. We could also be looking at
6928 an id-expression. So, we try the functional cast, and if
6929 that doesn't work we fall back to the primary-expression. */
6930 cp_parser_parse_tentatively (parser);
6931 /* Look for the simple-type-specifier. */
6932 ++parser->prevent_constrained_type_specifiers;
6933 type = cp_parser_simple_type_specifier (parser,
6934 /*decl_specs=*/NULL,
6935 CP_PARSER_FLAGS_NONE);
6936 --parser->prevent_constrained_type_specifiers;
6937 /* Parse the cast itself. */
6938 if (!cp_parser_error_occurred (parser))
6939 postfix_expression
6940 = cp_parser_functional_cast (parser, type);
6941 /* If that worked, we're done. */
6942 if (cp_parser_parse_definitely (parser))
6943 break;
6945 /* If the functional-cast didn't work out, try a
6946 compound-literal. */
6947 if (cp_parser_allow_gnu_extensions_p (parser)
6948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6950 cp_expr initializer = NULL_TREE;
6952 cp_parser_parse_tentatively (parser);
6954 matching_parens parens;
6955 parens.consume_open (parser);
6957 /* Avoid calling cp_parser_type_id pointlessly, see comment
6958 in cp_parser_cast_expression about c++/29234. */
6959 if (!cp_parser_compound_literal_p (parser))
6960 cp_parser_simulate_error (parser);
6961 else
6963 /* Parse the type. */
6964 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6965 parser->in_type_id_in_expr_p = true;
6966 type = cp_parser_type_id (parser);
6967 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6968 parens.require_close (parser);
6971 /* If things aren't going well, there's no need to
6972 keep going. */
6973 if (!cp_parser_error_occurred (parser))
6975 bool non_constant_p;
6976 /* Parse the brace-enclosed initializer list. */
6977 initializer = cp_parser_braced_list (parser,
6978 &non_constant_p);
6980 /* If that worked, we're definitely looking at a
6981 compound-literal expression. */
6982 if (cp_parser_parse_definitely (parser))
6984 /* Warn the user that a compound literal is not
6985 allowed in standard C++. */
6986 pedwarn (input_location, OPT_Wpedantic,
6987 "ISO C++ forbids compound-literals");
6988 /* For simplicity, we disallow compound literals in
6989 constant-expressions. We could
6990 allow compound literals of integer type, whose
6991 initializer was a constant, in constant
6992 expressions. Permitting that usage, as a further
6993 extension, would not change the meaning of any
6994 currently accepted programs. (Of course, as
6995 compound literals are not part of ISO C++, the
6996 standard has nothing to say.) */
6997 if (cp_parser_non_integral_constant_expression (parser,
6998 NIC_NCC))
7000 postfix_expression = error_mark_node;
7001 break;
7003 /* Form the representation of the compound-literal. */
7004 postfix_expression
7005 = finish_compound_literal (type, initializer,
7006 tf_warning_or_error, fcl_c99);
7007 postfix_expression.set_location (initializer.get_location ());
7008 break;
7012 /* It must be a primary-expression. */
7013 postfix_expression
7014 = cp_parser_primary_expression (parser, address_p, cast_p,
7015 /*template_arg_p=*/false,
7016 decltype_p,
7017 &idk);
7019 break;
7022 /* Note that we don't need to worry about calling build_cplus_new on a
7023 class-valued CALL_EXPR in decltype when it isn't the end of the
7024 postfix-expression; unary_complex_lvalue will take care of that for
7025 all these cases. */
7027 /* Keep looping until the postfix-expression is complete. */
7028 while (true)
7030 if (idk == CP_ID_KIND_UNQUALIFIED
7031 && identifier_p (postfix_expression)
7032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7033 /* It is not a Koenig lookup function call. */
7034 postfix_expression
7035 = unqualified_name_lookup_error (postfix_expression);
7037 /* Peek at the next token. */
7038 token = cp_lexer_peek_token (parser->lexer);
7040 switch (token->type)
7042 case CPP_OPEN_SQUARE:
7043 if (cp_next_tokens_can_be_std_attribute_p (parser))
7045 cp_parser_error (parser,
7046 "two consecutive %<[%> shall "
7047 "only introduce an attribute");
7048 return error_mark_node;
7050 postfix_expression
7051 = cp_parser_postfix_open_square_expression (parser,
7052 postfix_expression,
7053 false,
7054 decltype_p);
7055 postfix_expression.set_range (start_loc,
7056 postfix_expression.get_location ());
7058 idk = CP_ID_KIND_NONE;
7059 is_member_access = false;
7060 break;
7062 case CPP_OPEN_PAREN:
7063 /* postfix-expression ( expression-list [opt] ) */
7065 bool koenig_p;
7066 bool is_builtin_constant_p;
7067 bool saved_integral_constant_expression_p = false;
7068 bool saved_non_integral_constant_expression_p = false;
7069 tsubst_flags_t complain = complain_flags (decltype_p);
7070 vec<tree, va_gc> *args;
7071 location_t close_paren_loc = UNKNOWN_LOCATION;
7073 is_member_access = false;
7075 is_builtin_constant_p
7076 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7077 if (is_builtin_constant_p)
7079 /* The whole point of __builtin_constant_p is to allow
7080 non-constant expressions to appear as arguments. */
7081 saved_integral_constant_expression_p
7082 = parser->integral_constant_expression_p;
7083 saved_non_integral_constant_expression_p
7084 = parser->non_integral_constant_expression_p;
7085 parser->integral_constant_expression_p = false;
7087 args = (cp_parser_parenthesized_expression_list
7088 (parser, non_attr,
7089 /*cast_p=*/false, /*allow_expansion_p=*/true,
7090 /*non_constant_p=*/NULL,
7091 /*close_paren_loc=*/&close_paren_loc));
7092 if (is_builtin_constant_p)
7094 parser->integral_constant_expression_p
7095 = saved_integral_constant_expression_p;
7096 parser->non_integral_constant_expression_p
7097 = saved_non_integral_constant_expression_p;
7100 if (args == NULL)
7102 postfix_expression = error_mark_node;
7103 break;
7106 /* Function calls are not permitted in
7107 constant-expressions. */
7108 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7109 && cp_parser_non_integral_constant_expression (parser,
7110 NIC_FUNC_CALL))
7112 postfix_expression = error_mark_node;
7113 release_tree_vector (args);
7114 break;
7117 koenig_p = false;
7118 if (idk == CP_ID_KIND_UNQUALIFIED
7119 || idk == CP_ID_KIND_TEMPLATE_ID)
7121 if (identifier_p (postfix_expression))
7123 if (!args->is_empty ())
7125 koenig_p = true;
7126 if (!any_type_dependent_arguments_p (args))
7127 postfix_expression
7128 = perform_koenig_lookup (postfix_expression, args,
7129 complain);
7131 else
7132 postfix_expression
7133 = unqualified_fn_lookup_error (postfix_expression);
7135 /* We do not perform argument-dependent lookup if
7136 normal lookup finds a non-function, in accordance
7137 with the expected resolution of DR 218. */
7138 else if (!args->is_empty ()
7139 && is_overloaded_fn (postfix_expression))
7141 tree fn = get_first_fn (postfix_expression);
7142 fn = STRIP_TEMPLATE (fn);
7144 /* Do not do argument dependent lookup if regular
7145 lookup finds a member function or a block-scope
7146 function declaration. [basic.lookup.argdep]/3 */
7147 if (!DECL_FUNCTION_MEMBER_P (fn)
7148 && !DECL_LOCAL_FUNCTION_P (fn))
7150 koenig_p = true;
7151 if (!any_type_dependent_arguments_p (args))
7152 postfix_expression
7153 = perform_koenig_lookup (postfix_expression, args,
7154 complain);
7159 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7160 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7161 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7162 && vec_safe_length (args) == 3)
7164 tree arg0 = (*args)[0];
7165 tree arg1 = (*args)[1];
7166 tree arg2 = (*args)[2];
7167 int literal_mask = ((!!integer_zerop (arg1) << 1)
7168 | (!!integer_zerop (arg2) << 2));
7169 if (TREE_CODE (arg2) == CONST_DECL)
7170 arg2 = DECL_INITIAL (arg2);
7171 warn_for_memset (input_location, arg0, arg2, literal_mask);
7174 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7176 tree instance = TREE_OPERAND (postfix_expression, 0);
7177 tree fn = TREE_OPERAND (postfix_expression, 1);
7179 if (processing_template_decl
7180 && (type_dependent_object_expression_p (instance)
7181 || (!BASELINK_P (fn)
7182 && TREE_CODE (fn) != FIELD_DECL)
7183 || type_dependent_expression_p (fn)
7184 || any_type_dependent_arguments_p (args)))
7186 maybe_generic_this_capture (instance, fn);
7187 postfix_expression
7188 = build_min_nt_call_vec (postfix_expression, args);
7189 release_tree_vector (args);
7190 break;
7193 if (BASELINK_P (fn))
7195 postfix_expression
7196 = (build_new_method_call
7197 (instance, fn, &args, NULL_TREE,
7198 (idk == CP_ID_KIND_QUALIFIED
7199 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7200 : LOOKUP_NORMAL),
7201 /*fn_p=*/NULL,
7202 complain));
7204 else
7205 postfix_expression
7206 = finish_call_expr (postfix_expression, &args,
7207 /*disallow_virtual=*/false,
7208 /*koenig_p=*/false,
7209 complain);
7211 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7212 || TREE_CODE (postfix_expression) == MEMBER_REF
7213 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7214 postfix_expression = (build_offset_ref_call_from_tree
7215 (postfix_expression, &args,
7216 complain));
7217 else if (idk == CP_ID_KIND_QUALIFIED)
7218 /* A call to a static class member, or a namespace-scope
7219 function. */
7220 postfix_expression
7221 = finish_call_expr (postfix_expression, &args,
7222 /*disallow_virtual=*/true,
7223 koenig_p,
7224 complain);
7225 else
7226 /* All other function calls. */
7227 postfix_expression
7228 = finish_call_expr (postfix_expression, &args,
7229 /*disallow_virtual=*/false,
7230 koenig_p,
7231 complain);
7233 if (close_paren_loc != UNKNOWN_LOCATION)
7235 location_t combined_loc = make_location (token->location,
7236 start_loc,
7237 close_paren_loc);
7238 postfix_expression.set_location (combined_loc);
7241 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7242 idk = CP_ID_KIND_NONE;
7244 release_tree_vector (args);
7246 break;
7248 case CPP_DOT:
7249 case CPP_DEREF:
7250 /* postfix-expression . template [opt] id-expression
7251 postfix-expression . pseudo-destructor-name
7252 postfix-expression -> template [opt] id-expression
7253 postfix-expression -> pseudo-destructor-name */
7255 /* Consume the `.' or `->' operator. */
7256 cp_lexer_consume_token (parser->lexer);
7258 postfix_expression
7259 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7260 postfix_expression,
7261 false, &idk, loc);
7263 is_member_access = true;
7264 break;
7266 case CPP_PLUS_PLUS:
7267 /* postfix-expression ++ */
7268 /* Consume the `++' token. */
7269 cp_lexer_consume_token (parser->lexer);
7270 /* Generate a representation for the complete expression. */
7271 postfix_expression
7272 = finish_increment_expr (postfix_expression,
7273 POSTINCREMENT_EXPR);
7274 /* Increments may not appear in constant-expressions. */
7275 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7276 postfix_expression = error_mark_node;
7277 idk = CP_ID_KIND_NONE;
7278 is_member_access = false;
7279 break;
7281 case CPP_MINUS_MINUS:
7282 /* postfix-expression -- */
7283 /* Consume the `--' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Generate a representation for the complete expression. */
7286 postfix_expression
7287 = finish_increment_expr (postfix_expression,
7288 POSTDECREMENT_EXPR);
7289 /* Decrements may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7291 postfix_expression = error_mark_node;
7292 idk = CP_ID_KIND_NONE;
7293 is_member_access = false;
7294 break;
7296 default:
7297 if (pidk_return != NULL)
7298 * pidk_return = idk;
7299 if (member_access_only_p)
7300 return is_member_access
7301 ? postfix_expression
7302 : cp_expr (error_mark_node);
7303 else
7304 return postfix_expression;
7308 /* We should never get here. */
7309 gcc_unreachable ();
7310 return error_mark_node;
7313 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7314 by cp_parser_builtin_offsetof. We're looking for
7316 postfix-expression [ expression ]
7317 postfix-expression [ braced-init-list ] (C++11)
7319 FOR_OFFSETOF is set if we're being called in that context, which
7320 changes how we deal with integer constant expressions. */
7322 static tree
7323 cp_parser_postfix_open_square_expression (cp_parser *parser,
7324 tree postfix_expression,
7325 bool for_offsetof,
7326 bool decltype_p)
7328 tree index = NULL_TREE;
7329 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7330 bool saved_greater_than_is_operator_p;
7332 /* Consume the `[' token. */
7333 cp_lexer_consume_token (parser->lexer);
7335 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7336 parser->greater_than_is_operator_p = true;
7338 /* Parse the index expression. */
7339 /* ??? For offsetof, there is a question of what to allow here. If
7340 offsetof is not being used in an integral constant expression context,
7341 then we *could* get the right answer by computing the value at runtime.
7342 If we are in an integral constant expression context, then we might
7343 could accept any constant expression; hard to say without analysis.
7344 Rather than open the barn door too wide right away, allow only integer
7345 constant expressions here. */
7346 if (for_offsetof)
7347 index = cp_parser_constant_expression (parser);
7348 else
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7352 bool expr_nonconst_p;
7353 cp_lexer_set_source_position (parser->lexer);
7354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7355 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7357 else
7358 index = cp_parser_expression (parser);
7361 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression = grok_array_decl (loc, postfix_expression,
7368 index, decltype_p);
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7372 if (!for_offsetof
7373 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7374 postfix_expression = error_mark_node;
7376 return postfix_expression;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7392 static tree
7393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7394 enum cpp_ttype token_type,
7395 cp_expr postfix_expression,
7396 bool for_offsetof, cp_id_kind *idk,
7397 location_t location)
7399 tree name;
7400 bool dependent_p;
7401 bool pseudo_destructor_p;
7402 tree scope = NULL_TREE;
7403 location_t start_loc = postfix_expression.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type == CPP_DEREF)
7407 postfix_expression = build_x_arrow (location, postfix_expression,
7408 tf_warning_or_error);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p = type_dependent_object_expression_p (postfix_expression);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser->scope = NULL_TREE;
7414 parser->qualifying_scope = NULL_TREE;
7415 parser->object_scope = NULL_TREE;
7416 *idk = CP_ID_KIND_NONE;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7420 if (!dependent_p)
7422 scope = TREE_TYPE (postfix_expression);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope = non_reference (scope);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression != current_class_ref
7435 && scope != error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope, current_class_type))))
7441 scope = complete_type (scope);
7442 if (!COMPLETE_TYPE_P (scope)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind = (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope)
7450 ? DK_PEDWARN
7451 : DK_ERROR);
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression),
7454 postfix_expression, scope, kind);
7455 if (!MAYBE_CLASS_TYPE_P (scope))
7456 return error_mark_node;
7457 if (processing_template_decl)
7459 dependent_p = true;
7460 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7465 if (!dependent_p)
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser->context->object_type = scope;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7473 if (!scope)
7474 scope = error_mark_node;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7477 messages. */
7478 if (scope == error_mark_node)
7479 postfix_expression = error_mark_node;
7483 if (dependent_p)
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7485 type-dependent. */
7486 parser->context->object_type = unknown_type_node;
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p = false;
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7497 tree s;
7498 tree type;
7500 cp_parser_parse_tentatively (parser);
7501 /* Parse the pseudo-destructor-name. */
7502 s = NULL_TREE;
7503 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7504 &s, &type);
7505 if (dependent_p
7506 && (cp_parser_error_occurred (parser)
7507 || !SCALAR_TYPE_P (type)))
7508 cp_parser_abort_tentative_parse (parser);
7509 else if (cp_parser_parse_definitely (parser))
7511 pseudo_destructor_p = true;
7512 postfix_expression
7513 = finish_pseudo_destructor_expr (postfix_expression,
7514 s, type, location);
7518 if (!pseudo_destructor_p)
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7523 bool template_p;
7524 cp_token *token = cp_lexer_peek_token (parser->lexer);
7525 /* Parse the id-expression. */
7526 name = (cp_parser_id_expression
7527 (parser,
7528 cp_parser_optional_template_keyword (parser),
7529 /*check_dependency_p=*/true,
7530 &template_p,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7545 if (parser->scope)
7546 *idk = CP_ID_KIND_QUALIFIED;
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name) == TYPE_DECL)
7552 error_at (token->location, "invalid use of %qD", name);
7553 postfix_expression = error_mark_node;
7555 else
7557 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7559 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7561 error_at (token->location, "%<%D::%D%> is not a class member",
7562 parser->scope, name);
7563 postfix_expression = error_mark_node;
7565 else
7566 name = build_qualified_name (/*type=*/NULL_TREE,
7567 parser->scope,
7568 name,
7569 template_p);
7570 parser->scope = NULL_TREE;
7571 parser->qualifying_scope = NULL_TREE;
7572 parser->object_scope = NULL_TREE;
7574 if (parser->scope && name && BASELINK_P (name))
7575 adjust_result_of_qualified_name_lookup
7576 (name, parser->scope, scope);
7577 postfix_expression
7578 = finish_class_member_access_expr (postfix_expression, name,
7579 template_p,
7580 tf_warning_or_error);
7581 /* Build a location e.g.:
7582 ptr->access_expr
7583 ~~~^~~~~~~~~~~~~
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7586 location_t end_loc
7587 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7588 location_t combined_loc
7589 = make_location (input_location, start_loc, end_loc);
7590 protected_set_expr_location (postfix_expression, combined_loc);
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser->context->object_type = NULL_TREE;
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7600 if (!for_offsetof
7601 && (cp_parser_non_integral_constant_expression
7602 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7603 postfix_expression = error_mark_node;
7605 return postfix_expression;
7608 /* Parse a parenthesized expression-list.
7610 expression-list:
7611 assignment-expression
7612 expression-list, assignment-expression
7614 attribute-list:
7615 expression-list
7616 identifier
7617 identifier, expression-list
7619 CAST_P is true if this expression is the target of a cast.
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7622 argument pack.
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7637 static vec<tree, va_gc> *
7638 cp_parser_parenthesized_expression_list (cp_parser* parser,
7639 int is_attribute_list,
7640 bool cast_p,
7641 bool allow_expansion_p,
7642 bool *non_constant_p,
7643 location_t *close_paren_loc)
7645 vec<tree, va_gc> *expression_list;
7646 bool fold_expr_p = is_attribute_list != non_attr;
7647 tree identifier = NULL_TREE;
7648 bool saved_greater_than_is_operator_p;
7650 /* Assume all the expressions will be constant. */
7651 if (non_constant_p)
7652 *non_constant_p = false;
7654 matching_parens parens;
7655 if (!parens.require_open (parser))
7656 return NULL;
7658 expression_list = make_tree_vector ();
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser->greater_than_is_operator_p;
7664 parser->greater_than_is_operator_p = true;
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7668 while (true)
7670 tree expr;
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list == id_attr
7675 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7677 cp_token *token;
7679 /* Consume the identifier. */
7680 token = cp_lexer_consume_token (parser->lexer);
7681 /* Save the identifier. */
7682 identifier = token->u.value;
7684 else
7686 bool expr_non_constant_p;
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser->lexer);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7694 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7695 if (non_constant_p && expr_non_constant_p)
7696 *non_constant_p = true;
7698 else if (non_constant_p)
7700 expr = (cp_parser_constant_expression
7701 (parser, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p));
7703 if (expr_non_constant_p)
7704 *non_constant_p = true;
7706 else
7707 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7708 cast_p);
7710 if (fold_expr_p)
7711 expr = instantiate_non_dependent_expr (expr);
7713 /* If we have an ellipsis, then this is an expression
7714 expansion. */
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser->lexer);
7721 /* Build the argument pack. */
7722 expr = make_pack_expansion (expr);
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list, expr);
7731 if (expr == error_mark_node)
7732 goto skip_comma;
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list = non_attr;
7739 get_comma:;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7742 break;
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser->lexer);
7748 if (close_paren_loc)
7749 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7751 if (!parens.require_close (parser))
7753 int ending;
7755 skip_comma:;
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending = cp_parser_skip_to_closing_parenthesis (parser,
7759 /*recovering=*/true,
7760 /*or_comma=*/true,
7761 /*consume_paren=*/true);
7762 if (ending < 0)
7763 goto get_comma;
7764 if (!ending)
7766 parser->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p;
7768 return NULL;
7772 parser->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p;
7775 if (identifier)
7776 vec_safe_insert (expression_list, 0, identifier);
7778 return expression_list;
7781 /* Parse a pseudo-destructor-name.
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7793 static void
7794 cp_parser_pseudo_destructor_name (cp_parser* parser,
7795 tree object,
7796 tree* scope,
7797 tree* type)
7799 bool nested_name_specifier_p;
7801 /* Handle ~auto. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7803 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7804 && !type_dependent_expression_p (object))
7806 if (cxx_dialect < cxx14)
7807 pedwarn (input_location, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser->lexer);
7811 cp_lexer_consume_token (parser->lexer);
7812 *scope = NULL_TREE;
7813 *type = TREE_TYPE (object);
7814 return;
7817 /* Assume that things will not work out. */
7818 *type = error_mark_node;
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7827 /*type_p=*/false,
7828 /*is_declaration=*/false)
7829 != NULL_TREE);
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7841 class_type,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7860 cp_parser_error (parser, "non-scalar type");
7861 return;
7864 /* Look for the type-name. */
7865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7866 if (*scope == error_mark_node)
7867 return;
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7872 else
7873 *scope = NULL_TREE;
7875 /* Look for the `~'. */
7876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7880 cp_parser_commit_to_topmost_tentative_parse (parser);
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7887 /* Parse a unary-expression.
7889 unary-expression:
7890 postfix-expression
7891 ++ cast-expression
7892 -- cast-expression
7893 unary-operator cast-expression
7894 sizeof unary-expression
7895 sizeof ( type-id )
7896 alignof ( type-id ) [C++0x]
7897 new-expression
7898 delete-expression
7900 GNU Extensions:
7902 unary-expression:
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7909 && identifier
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7918 Returns a representation of the expression. */
7920 static cp_expr
7921 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7922 bool address_p, bool cast_p, bool decltype_p)
7924 cp_token *token;
7925 enum tree_code unary_operator;
7927 /* Peek at the next token. */
7928 token = cp_lexer_peek_token (parser->lexer);
7929 /* Some keywords give away the kind of expression. */
7930 if (token->type == CPP_KEYWORD)
7932 enum rid keyword = token->keyword;
7934 switch (keyword)
7936 case RID_ALIGNOF:
7937 case RID_SIZEOF:
7939 tree operand, ret;
7940 enum tree_code op;
7941 location_t start_loc = token->location;
7943 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser->lexer);
7946 /* Parse the operand. */
7947 operand = cp_parser_sizeof_operand (parser, keyword);
7949 if (TYPE_P (operand))
7950 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7951 else
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token->u.value, "alignof"))
7957 pedwarn (token->location, OPT_Wpedantic,
7958 "ISO C++ does not allow %<alignof%> "
7959 "with a non-type");
7961 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op == SIZEOF_EXPR && ret != error_mark_node)
7967 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7969 if (!processing_template_decl && TYPE_P (operand))
7971 ret = build_min (SIZEOF_EXPR, size_type_node,
7972 build1 (NOP_EXPR, operand,
7973 error_mark_node));
7974 SIZEOF_EXPR_TYPE_P (ret) = 1;
7976 else
7977 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7978 TREE_SIDE_EFFECTS (ret) = 0;
7979 TREE_READONLY (ret) = 1;
7983 /* Construct a location e.g. :
7984 alignof (expr)
7985 ^~~~~~~~~~~~~~
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser->lexer)->location;
7990 location_t compound_loc
7991 = make_location (start_loc, start_loc, finish_loc);
7993 cp_expr ret_expr (ret);
7994 ret_expr.set_location (compound_loc);
7995 return ret_expr;
7998 case RID_NEW:
7999 return cp_parser_new_expression (parser);
8001 case RID_DELETE:
8002 return cp_parser_delete_expression (parser);
8004 case RID_EXTENSION:
8006 /* The saved value of the PEDANTIC flag. */
8007 int saved_pedantic;
8008 tree expr;
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser, &saved_pedantic);
8012 /* Parse the cast-expression. */
8013 expr = cp_parser_simple_cast_expression (parser);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic = saved_pedantic;
8017 return expr;
8020 case RID_REALPART:
8021 case RID_IMAGPART:
8023 tree expression;
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Parse the cast-expression. */
8028 expression = cp_parser_simple_cast_expression (parser);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token->location,
8031 (keyword == RID_REALPART
8032 ? REALPART_EXPR : IMAGPART_EXPR),
8033 expression,
8034 tf_warning_or_error);
8036 break;
8038 case RID_TRANSACTION_ATOMIC:
8039 case RID_TRANSACTION_RELAXED:
8040 return cp_parser_transaction_expression (parser, keyword);
8042 case RID_NOEXCEPT:
8044 tree expr;
8045 const char *saved_message;
8046 bool saved_integral_constant_expression_p;
8047 bool saved_non_integral_constant_expression_p;
8048 bool saved_greater_than_is_operator_p;
8050 location_t start_loc = token->location;
8052 cp_lexer_consume_token (parser->lexer);
8053 matching_parens parens;
8054 parens.require_open (parser);
8056 saved_message = parser->type_definition_forbidden_message;
8057 parser->type_definition_forbidden_message
8058 = G_("types may not be defined in %<noexcept%> expressions");
8060 saved_integral_constant_expression_p
8061 = parser->integral_constant_expression_p;
8062 saved_non_integral_constant_expression_p
8063 = parser->non_integral_constant_expression_p;
8064 parser->integral_constant_expression_p = false;
8066 saved_greater_than_is_operator_p
8067 = parser->greater_than_is_operator_p;
8068 parser->greater_than_is_operator_p = true;
8070 ++cp_unevaluated_operand;
8071 ++c_inhibit_evaluation_warnings;
8072 ++cp_noexcept_operand;
8073 expr = cp_parser_expression (parser);
8074 --cp_noexcept_operand;
8075 --c_inhibit_evaluation_warnings;
8076 --cp_unevaluated_operand;
8078 parser->greater_than_is_operator_p
8079 = saved_greater_than_is_operator_p;
8081 parser->integral_constant_expression_p
8082 = saved_integral_constant_expression_p;
8083 parser->non_integral_constant_expression_p
8084 = saved_non_integral_constant_expression_p;
8086 parser->type_definition_forbidden_message = saved_message;
8088 location_t finish_loc
8089 = cp_lexer_peek_token (parser->lexer)->location;
8090 parens.require_close (parser);
8092 /* Construct a location of the form:
8093 noexcept (expr)
8094 ^~~~~~~~~~~~~~~
8095 with start == caret, finishing at the close-paren. */
8096 location_t noexcept_loc
8097 = make_location (start_loc, start_loc, finish_loc);
8099 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8100 noexcept_loc);
8103 default:
8104 break;
8108 /* Look for the `:: new' and `:: delete', which also signal the
8109 beginning of a new-expression, or delete-expression,
8110 respectively. If the next token is `::', then it might be one of
8111 these. */
8112 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8114 enum rid keyword;
8116 /* See if the token after the `::' is one of the keywords in
8117 which we're interested. */
8118 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8119 /* If it's `new', we have a new-expression. */
8120 if (keyword == RID_NEW)
8121 return cp_parser_new_expression (parser);
8122 /* Similarly, for `delete'. */
8123 else if (keyword == RID_DELETE)
8124 return cp_parser_delete_expression (parser);
8127 /* Look for a unary operator. */
8128 unary_operator = cp_parser_unary_operator (token);
8129 /* The `++' and `--' operators can be handled similarly, even though
8130 they are not technically unary-operators in the grammar. */
8131 if (unary_operator == ERROR_MARK)
8133 if (token->type == CPP_PLUS_PLUS)
8134 unary_operator = PREINCREMENT_EXPR;
8135 else if (token->type == CPP_MINUS_MINUS)
8136 unary_operator = PREDECREMENT_EXPR;
8137 /* Handle the GNU address-of-label extension. */
8138 else if (cp_parser_allow_gnu_extensions_p (parser)
8139 && token->type == CPP_AND_AND)
8141 tree identifier;
8142 tree expression;
8143 location_t start_loc = token->location;
8145 /* Consume the '&&' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Look for the identifier. */
8148 location_t finish_loc
8149 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8150 identifier = cp_parser_identifier (parser);
8151 /* Construct a location of the form:
8152 &&label
8153 ^~~~~~~
8154 with caret==start at the "&&", finish at the end of the label. */
8155 location_t combined_loc
8156 = make_location (start_loc, start_loc, finish_loc);
8157 /* Create an expression representing the address. */
8158 expression = finish_label_address_expr (identifier, combined_loc);
8159 if (cp_parser_non_integral_constant_expression (parser,
8160 NIC_ADDR_LABEL))
8161 expression = error_mark_node;
8162 return expression;
8165 if (unary_operator != ERROR_MARK)
8167 cp_expr cast_expression;
8168 cp_expr expression = error_mark_node;
8169 non_integral_constant non_constant_p = NIC_NONE;
8170 location_t loc = token->location;
8171 tsubst_flags_t complain = complain_flags (decltype_p);
8173 /* Consume the operator token. */
8174 token = cp_lexer_consume_token (parser->lexer);
8175 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8177 /* Parse the cast-expression. */
8178 cast_expression
8179 = cp_parser_cast_expression (parser,
8180 unary_operator == ADDR_EXPR,
8181 /*cast_p=*/false,
8182 /*decltype*/false,
8183 pidk);
8185 /* Make a location:
8186 OP_TOKEN CAST_EXPRESSION
8187 ^~~~~~~~~~~~~~~~~~~~~~~~~
8188 with start==caret at the operator token, and
8189 extending to the end of the cast_expression. */
8190 loc = make_location (loc, loc, cast_expression.get_finish ());
8192 /* Now, build an appropriate representation. */
8193 switch (unary_operator)
8195 case INDIRECT_REF:
8196 non_constant_p = NIC_STAR;
8197 expression = build_x_indirect_ref (loc, cast_expression,
8198 RO_UNARY_STAR,
8199 complain);
8200 /* TODO: build_x_indirect_ref does not always honor the
8201 location, so ensure it is set. */
8202 expression.set_location (loc);
8203 break;
8205 case ADDR_EXPR:
8206 non_constant_p = NIC_ADDR;
8207 /* Fall through. */
8208 case BIT_NOT_EXPR:
8209 expression = build_x_unary_op (loc, unary_operator,
8210 cast_expression,
8211 complain);
8212 /* TODO: build_x_unary_op does not always honor the location,
8213 so ensure it is set. */
8214 expression.set_location (loc);
8215 break;
8217 case PREINCREMENT_EXPR:
8218 case PREDECREMENT_EXPR:
8219 non_constant_p = unary_operator == PREINCREMENT_EXPR
8220 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8221 /* Fall through. */
8222 case NEGATE_EXPR:
8223 /* Immediately fold negation of a constant, unless the constant is 0
8224 (since -0 == 0) or it would overflow. */
8225 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8226 && CONSTANT_CLASS_P (cast_expression)
8227 && !integer_zerop (cast_expression)
8228 && !TREE_OVERFLOW (cast_expression))
8230 tree folded = fold_build1 (unary_operator,
8231 TREE_TYPE (cast_expression),
8232 cast_expression);
8233 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8235 expression = cp_expr (folded, loc);
8236 break;
8239 /* Fall through. */
8240 case UNARY_PLUS_EXPR:
8241 case TRUTH_NOT_EXPR:
8242 expression = finish_unary_op_expr (loc, unary_operator,
8243 cast_expression, complain);
8244 break;
8246 default:
8247 gcc_unreachable ();
8250 if (non_constant_p != NIC_NONE
8251 && cp_parser_non_integral_constant_expression (parser,
8252 non_constant_p))
8253 expression = error_mark_node;
8255 return expression;
8258 return cp_parser_postfix_expression (parser, address_p, cast_p,
8259 /*member_access_only_p=*/false,
8260 decltype_p,
8261 pidk);
8264 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8265 unary-operator, the corresponding tree code is returned. */
8267 static enum tree_code
8268 cp_parser_unary_operator (cp_token* token)
8270 switch (token->type)
8272 case CPP_MULT:
8273 return INDIRECT_REF;
8275 case CPP_AND:
8276 return ADDR_EXPR;
8278 case CPP_PLUS:
8279 return UNARY_PLUS_EXPR;
8281 case CPP_MINUS:
8282 return NEGATE_EXPR;
8284 case CPP_NOT:
8285 return TRUTH_NOT_EXPR;
8287 case CPP_COMPL:
8288 return BIT_NOT_EXPR;
8290 default:
8291 return ERROR_MARK;
8295 /* Parse a new-expression.
8297 new-expression:
8298 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8299 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8301 Returns a representation of the expression. */
8303 static tree
8304 cp_parser_new_expression (cp_parser* parser)
8306 bool global_scope_p;
8307 vec<tree, va_gc> *placement;
8308 tree type;
8309 vec<tree, va_gc> *initializer;
8310 tree nelts = NULL_TREE;
8311 tree ret;
8313 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8315 /* Look for the optional `::' operator. */
8316 global_scope_p
8317 = (cp_parser_global_scope_opt (parser,
8318 /*current_scope_valid_p=*/false)
8319 != NULL_TREE);
8320 /* Look for the `new' operator. */
8321 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8322 /* There's no easy way to tell a new-placement from the
8323 `( type-id )' construct. */
8324 cp_parser_parse_tentatively (parser);
8325 /* Look for a new-placement. */
8326 placement = cp_parser_new_placement (parser);
8327 /* If that didn't work out, there's no new-placement. */
8328 if (!cp_parser_parse_definitely (parser))
8330 if (placement != NULL)
8331 release_tree_vector (placement);
8332 placement = NULL;
8335 /* If the next token is a `(', then we have a parenthesized
8336 type-id. */
8337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8339 cp_token *token;
8340 const char *saved_message = parser->type_definition_forbidden_message;
8342 /* Consume the `('. */
8343 matching_parens parens;
8344 parens.consume_open (parser);
8346 /* Parse the type-id. */
8347 parser->type_definition_forbidden_message
8348 = G_("types may not be defined in a new-expression");
8350 type_id_in_expr_sentinel s (parser);
8351 type = cp_parser_type_id (parser);
8353 parser->type_definition_forbidden_message = saved_message;
8355 /* Look for the closing `)'. */
8356 parens.require_close (parser);
8357 token = cp_lexer_peek_token (parser->lexer);
8358 /* There should not be a direct-new-declarator in this production,
8359 but GCC used to allowed this, so we check and emit a sensible error
8360 message for this case. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8363 error_at (token->location,
8364 "array bound forbidden after parenthesized type-id");
8365 inform (token->location,
8366 "try removing the parentheses around the type-id");
8367 cp_parser_direct_new_declarator (parser);
8370 /* Otherwise, there must be a new-type-id. */
8371 else
8372 type = cp_parser_new_type_id (parser, &nelts);
8374 /* If the next token is a `(' or '{', then we have a new-initializer. */
8375 cp_token *token = cp_lexer_peek_token (parser->lexer);
8376 if (token->type == CPP_OPEN_PAREN
8377 || token->type == CPP_OPEN_BRACE)
8378 initializer = cp_parser_new_initializer (parser);
8379 else
8380 initializer = NULL;
8382 /* A new-expression may not appear in an integral constant
8383 expression. */
8384 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8385 ret = error_mark_node;
8386 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8387 of a new-type-id or type-id of a new-expression, the new-expression shall
8388 contain a new-initializer of the form ( assignment-expression )".
8389 Additionally, consistently with the spirit of DR 1467, we want to accept
8390 'new auto { 2 }' too. */
8391 else if ((ret = type_uses_auto (type))
8392 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8393 && (vec_safe_length (initializer) != 1
8394 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8395 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8397 error_at (token->location,
8398 "initialization of new-expression for type %<auto%> "
8399 "requires exactly one element");
8400 ret = error_mark_node;
8402 else
8404 /* Construct a location e.g.:
8405 ptr = new int[100]
8406 ^~~~~~~~~~~~
8407 with caret == start at the start of the "new" token, and the end
8408 at the end of the final token we consumed. */
8409 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8410 location_t end_loc = get_finish (end_tok->location);
8411 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8413 /* Create a representation of the new-expression. */
8414 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8415 tf_warning_or_error);
8416 protected_set_expr_location (ret, combined_loc);
8419 if (placement != NULL)
8420 release_tree_vector (placement);
8421 if (initializer != NULL)
8422 release_tree_vector (initializer);
8424 return ret;
8427 /* Parse a new-placement.
8429 new-placement:
8430 ( expression-list )
8432 Returns the same representation as for an expression-list. */
8434 static vec<tree, va_gc> *
8435 cp_parser_new_placement (cp_parser* parser)
8437 vec<tree, va_gc> *expression_list;
8439 /* Parse the expression-list. */
8440 expression_list = (cp_parser_parenthesized_expression_list
8441 (parser, non_attr, /*cast_p=*/false,
8442 /*allow_expansion_p=*/true,
8443 /*non_constant_p=*/NULL));
8445 if (expression_list && expression_list->is_empty ())
8446 error ("expected expression-list or type-id");
8448 return expression_list;
8451 /* Parse a new-type-id.
8453 new-type-id:
8454 type-specifier-seq new-declarator [opt]
8456 Returns the TYPE allocated. If the new-type-id indicates an array
8457 type, *NELTS is set to the number of elements in the last array
8458 bound; the TYPE will not include the last array bound. */
8460 static tree
8461 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8463 cp_decl_specifier_seq type_specifier_seq;
8464 cp_declarator *new_declarator;
8465 cp_declarator *declarator;
8466 cp_declarator *outer_declarator;
8467 const char *saved_message;
8469 /* The type-specifier sequence must not contain type definitions.
8470 (It cannot contain declarations of new types either, but if they
8471 are not definitions we will catch that because they are not
8472 complete.) */
8473 saved_message = parser->type_definition_forbidden_message;
8474 parser->type_definition_forbidden_message
8475 = G_("types may not be defined in a new-type-id");
8476 /* Parse the type-specifier-seq. */
8477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8478 /*is_trailing_return=*/false,
8479 &type_specifier_seq);
8480 /* Restore the old message. */
8481 parser->type_definition_forbidden_message = saved_message;
8483 if (type_specifier_seq.type == error_mark_node)
8484 return error_mark_node;
8486 /* Parse the new-declarator. */
8487 new_declarator = cp_parser_new_declarator_opt (parser);
8489 /* Determine the number of elements in the last array dimension, if
8490 any. */
8491 *nelts = NULL_TREE;
8492 /* Skip down to the last array dimension. */
8493 declarator = new_declarator;
8494 outer_declarator = NULL;
8495 while (declarator && (declarator->kind == cdk_pointer
8496 || declarator->kind == cdk_ptrmem))
8498 outer_declarator = declarator;
8499 declarator = declarator->declarator;
8501 while (declarator
8502 && declarator->kind == cdk_array
8503 && declarator->declarator
8504 && declarator->declarator->kind == cdk_array)
8506 outer_declarator = declarator;
8507 declarator = declarator->declarator;
8510 if (declarator && declarator->kind == cdk_array)
8512 *nelts = declarator->u.array.bounds;
8513 if (*nelts == error_mark_node)
8514 *nelts = integer_one_node;
8516 if (outer_declarator)
8517 outer_declarator->declarator = declarator->declarator;
8518 else
8519 new_declarator = NULL;
8522 return groktypename (&type_specifier_seq, new_declarator, false);
8525 /* Parse an (optional) new-declarator.
8527 new-declarator:
8528 ptr-operator new-declarator [opt]
8529 direct-new-declarator
8531 Returns the declarator. */
8533 static cp_declarator *
8534 cp_parser_new_declarator_opt (cp_parser* parser)
8536 enum tree_code code;
8537 tree type, std_attributes = NULL_TREE;
8538 cp_cv_quals cv_quals;
8540 /* We don't know if there's a ptr-operator next, or not. */
8541 cp_parser_parse_tentatively (parser);
8542 /* Look for a ptr-operator. */
8543 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8544 /* If that worked, look for more new-declarators. */
8545 if (cp_parser_parse_definitely (parser))
8547 cp_declarator *declarator;
8549 /* Parse another optional declarator. */
8550 declarator = cp_parser_new_declarator_opt (parser);
8552 declarator = cp_parser_make_indirect_declarator
8553 (code, type, cv_quals, declarator, std_attributes);
8555 return declarator;
8558 /* If the next token is a `[', there is a direct-new-declarator. */
8559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8560 return cp_parser_direct_new_declarator (parser);
8562 return NULL;
8565 /* Parse a direct-new-declarator.
8567 direct-new-declarator:
8568 [ expression ]
8569 direct-new-declarator [constant-expression]
8573 static cp_declarator *
8574 cp_parser_direct_new_declarator (cp_parser* parser)
8576 cp_declarator *declarator = NULL;
8578 while (true)
8580 tree expression;
8581 cp_token *token;
8583 /* Look for the opening `['. */
8584 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8586 token = cp_lexer_peek_token (parser->lexer);
8587 expression = cp_parser_expression (parser);
8588 /* The standard requires that the expression have integral
8589 type. DR 74 adds enumeration types. We believe that the
8590 real intent is that these expressions be handled like the
8591 expression in a `switch' condition, which also allows
8592 classes with a single conversion to integral or
8593 enumeration type. */
8594 if (!processing_template_decl)
8596 expression
8597 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8598 expression,
8599 /*complain=*/true);
8600 if (!expression)
8602 error_at (token->location,
8603 "expression in new-declarator must have integral "
8604 "or enumeration type");
8605 expression = error_mark_node;
8609 /* Look for the closing `]'. */
8610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8612 /* Add this bound to the declarator. */
8613 declarator = make_array_declarator (declarator, expression);
8615 /* If the next token is not a `[', then there are no more
8616 bounds. */
8617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8618 break;
8621 return declarator;
8624 /* Parse a new-initializer.
8626 new-initializer:
8627 ( expression-list [opt] )
8628 braced-init-list
8630 Returns a representation of the expression-list. */
8632 static vec<tree, va_gc> *
8633 cp_parser_new_initializer (cp_parser* parser)
8635 vec<tree, va_gc> *expression_list;
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8639 tree t;
8640 bool expr_non_constant_p;
8641 cp_lexer_set_source_position (parser->lexer);
8642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8643 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8644 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8645 expression_list = make_tree_vector_single (t);
8647 else
8648 expression_list = (cp_parser_parenthesized_expression_list
8649 (parser, non_attr, /*cast_p=*/false,
8650 /*allow_expansion_p=*/true,
8651 /*non_constant_p=*/NULL));
8653 return expression_list;
8656 /* Parse a delete-expression.
8658 delete-expression:
8659 :: [opt] delete cast-expression
8660 :: [opt] delete [ ] cast-expression
8662 Returns a representation of the expression. */
8664 static tree
8665 cp_parser_delete_expression (cp_parser* parser)
8667 bool global_scope_p;
8668 bool array_p;
8669 tree expression;
8671 /* Look for the optional `::' operator. */
8672 global_scope_p
8673 = (cp_parser_global_scope_opt (parser,
8674 /*current_scope_valid_p=*/false)
8675 != NULL_TREE);
8676 /* Look for the `delete' keyword. */
8677 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8678 /* See if the array syntax is in use. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8681 /* Consume the `[' token. */
8682 cp_lexer_consume_token (parser->lexer);
8683 /* Look for the `]' token. */
8684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8685 /* Remember that this is the `[]' construct. */
8686 array_p = true;
8688 else
8689 array_p = false;
8691 /* Parse the cast-expression. */
8692 expression = cp_parser_simple_cast_expression (parser);
8694 /* A delete-expression may not appear in an integral constant
8695 expression. */
8696 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8697 return error_mark_node;
8699 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8700 tf_warning_or_error);
8703 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8704 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8705 0 otherwise. */
8707 static int
8708 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8710 cp_token *token = cp_lexer_peek_token (parser->lexer);
8711 switch (token->type)
8713 case CPP_COMMA:
8714 case CPP_SEMICOLON:
8715 case CPP_QUERY:
8716 case CPP_COLON:
8717 case CPP_CLOSE_SQUARE:
8718 case CPP_CLOSE_PAREN:
8719 case CPP_CLOSE_BRACE:
8720 case CPP_OPEN_BRACE:
8721 case CPP_DOT:
8722 case CPP_DOT_STAR:
8723 case CPP_DEREF:
8724 case CPP_DEREF_STAR:
8725 case CPP_DIV:
8726 case CPP_MOD:
8727 case CPP_LSHIFT:
8728 case CPP_RSHIFT:
8729 case CPP_LESS:
8730 case CPP_GREATER:
8731 case CPP_LESS_EQ:
8732 case CPP_GREATER_EQ:
8733 case CPP_EQ_EQ:
8734 case CPP_NOT_EQ:
8735 case CPP_EQ:
8736 case CPP_MULT_EQ:
8737 case CPP_DIV_EQ:
8738 case CPP_MOD_EQ:
8739 case CPP_PLUS_EQ:
8740 case CPP_MINUS_EQ:
8741 case CPP_RSHIFT_EQ:
8742 case CPP_LSHIFT_EQ:
8743 case CPP_AND_EQ:
8744 case CPP_XOR_EQ:
8745 case CPP_OR_EQ:
8746 case CPP_XOR:
8747 case CPP_OR:
8748 case CPP_OR_OR:
8749 case CPP_EOF:
8750 case CPP_ELLIPSIS:
8751 return 0;
8753 case CPP_OPEN_PAREN:
8754 /* In ((type ()) () the last () isn't a valid cast-expression,
8755 so the whole must be parsed as postfix-expression. */
8756 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8757 != CPP_CLOSE_PAREN;
8759 case CPP_OPEN_SQUARE:
8760 /* '[' may start a primary-expression in obj-c++ and in C++11,
8761 as a lambda-expression, eg, '(void)[]{}'. */
8762 if (cxx_dialect >= cxx11)
8763 return -1;
8764 return c_dialect_objc ();
8766 case CPP_PLUS_PLUS:
8767 case CPP_MINUS_MINUS:
8768 /* '++' and '--' may or may not start a cast-expression:
8770 struct T { void operator++(int); };
8771 void f() { (T())++; }
8775 int a;
8776 (int)++a; */
8777 return -1;
8779 default:
8780 return 1;
8784 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8785 in the order: const_cast, static_cast, reinterpret_cast.
8787 Don't suggest dynamic_cast.
8789 Return the first legal cast kind found, or NULL otherwise. */
8791 static const char *
8792 get_cast_suggestion (tree dst_type, tree orig_expr)
8794 tree trial;
8796 /* Reuse the parser logic by attempting to build the various kinds of
8797 cast, with "complain" disabled.
8798 Identify the first such cast that is valid. */
8800 /* Don't attempt to run such logic within template processing. */
8801 if (processing_template_decl)
8802 return NULL;
8804 /* First try const_cast. */
8805 trial = build_const_cast (dst_type, orig_expr, tf_none);
8806 if (trial != error_mark_node)
8807 return "const_cast";
8809 /* If that fails, try static_cast. */
8810 trial = build_static_cast (dst_type, orig_expr, tf_none);
8811 if (trial != error_mark_node)
8812 return "static_cast";
8814 /* Finally, try reinterpret_cast. */
8815 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8816 if (trial != error_mark_node)
8817 return "reinterpret_cast";
8819 /* No such cast possible. */
8820 return NULL;
8823 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8824 suggesting how to convert a C-style cast of the form:
8826 (DST_TYPE)ORIG_EXPR
8828 to a C++-style cast.
8830 The primary range of RICHLOC is asssumed to be that of the original
8831 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8832 of the parens in the C-style cast. */
8834 static void
8835 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8836 location_t close_paren_loc, tree orig_expr,
8837 tree dst_type)
8839 /* This function is non-trivial, so bail out now if the warning isn't
8840 going to be emitted. */
8841 if (!warn_old_style_cast)
8842 return;
8844 /* Try to find a legal C++ cast, trying them in order:
8845 const_cast, static_cast, reinterpret_cast. */
8846 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8847 if (!cast_suggestion)
8848 return;
8850 /* Replace the open paren with "CAST_SUGGESTION<". */
8851 pretty_printer pp;
8852 pp_printf (&pp, "%s<", cast_suggestion);
8853 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8855 /* Replace the close paren with "> (". */
8856 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8858 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8859 rich_loc->add_fixit_insert_after (")");
8863 /* Parse a cast-expression.
8865 cast-expression:
8866 unary-expression
8867 ( type-id ) cast-expression
8869 ADDRESS_P is true iff the unary-expression is appearing as the
8870 operand of the `&' operator. CAST_P is true if this expression is
8871 the target of a cast.
8873 Returns a representation of the expression. */
8875 static cp_expr
8876 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8877 bool decltype_p, cp_id_kind * pidk)
8879 /* If it's a `(', then we might be looking at a cast. */
8880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8882 tree type = NULL_TREE;
8883 cp_expr expr (NULL_TREE);
8884 int cast_expression = 0;
8885 const char *saved_message;
8887 /* There's no way to know yet whether or not this is a cast.
8888 For example, `(int (3))' is a unary-expression, while `(int)
8889 3' is a cast. So, we resort to parsing tentatively. */
8890 cp_parser_parse_tentatively (parser);
8891 /* Types may not be defined in a cast. */
8892 saved_message = parser->type_definition_forbidden_message;
8893 parser->type_definition_forbidden_message
8894 = G_("types may not be defined in casts");
8895 /* Consume the `('. */
8896 matching_parens parens;
8897 cp_token *open_paren = parens.consume_open (parser);
8898 location_t open_paren_loc = open_paren->location;
8899 location_t close_paren_loc = UNKNOWN_LOCATION;
8901 /* A very tricky bit is that `(struct S) { 3 }' is a
8902 compound-literal (which we permit in C++ as an extension).
8903 But, that construct is not a cast-expression -- it is a
8904 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8905 is legal; if the compound-literal were a cast-expression,
8906 you'd need an extra set of parentheses.) But, if we parse
8907 the type-id, and it happens to be a class-specifier, then we
8908 will commit to the parse at that point, because we cannot
8909 undo the action that is done when creating a new class. So,
8910 then we cannot back up and do a postfix-expression.
8912 Another tricky case is the following (c++/29234):
8914 struct S { void operator () (); };
8916 void foo ()
8918 ( S()() );
8921 As a type-id we parse the parenthesized S()() as a function
8922 returning a function, groktypename complains and we cannot
8923 back up in this case either.
8925 Therefore, we scan ahead to the closing `)', and check to see
8926 if the tokens after the `)' can start a cast-expression. Otherwise
8927 we are dealing with an unary-expression, a postfix-expression
8928 or something else.
8930 Yet another tricky case, in C++11, is the following (c++/54891):
8932 (void)[]{};
8934 The issue is that usually, besides the case of lambda-expressions,
8935 the parenthesized type-id cannot be followed by '[', and, eg, we
8936 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8937 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8938 we don't commit, we try a cast-expression, then an unary-expression.
8940 Save tokens so that we can put them back. */
8941 cp_lexer_save_tokens (parser->lexer);
8943 /* We may be looking at a cast-expression. */
8944 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8945 /*consume_paren=*/true))
8946 cast_expression
8947 = cp_parser_tokens_start_cast_expression (parser);
8949 /* Roll back the tokens we skipped. */
8950 cp_lexer_rollback_tokens (parser->lexer);
8951 /* If we aren't looking at a cast-expression, simulate an error so
8952 that the call to cp_parser_error_occurred below returns true. */
8953 if (!cast_expression)
8954 cp_parser_simulate_error (parser);
8955 else
8957 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8958 parser->in_type_id_in_expr_p = true;
8959 /* Look for the type-id. */
8960 type = cp_parser_type_id (parser);
8961 /* Look for the closing `)'. */
8962 cp_token *close_paren = parens.require_close (parser);
8963 if (close_paren)
8964 close_paren_loc = close_paren->location;
8965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8968 /* Restore the saved message. */
8969 parser->type_definition_forbidden_message = saved_message;
8971 /* At this point this can only be either a cast or a
8972 parenthesized ctor such as `(T ())' that looks like a cast to
8973 function returning T. */
8974 if (!cp_parser_error_occurred (parser))
8976 /* Only commit if the cast-expression doesn't start with
8977 '++', '--', or '[' in C++11. */
8978 if (cast_expression > 0)
8979 cp_parser_commit_to_topmost_tentative_parse (parser);
8981 expr = cp_parser_cast_expression (parser,
8982 /*address_p=*/false,
8983 /*cast_p=*/true,
8984 /*decltype_p=*/false,
8985 pidk);
8987 if (cp_parser_parse_definitely (parser))
8989 /* Warn about old-style casts, if so requested. */
8990 if (warn_old_style_cast
8991 && !in_system_header_at (input_location)
8992 && !VOID_TYPE_P (type)
8993 && current_lang_name != lang_name_c)
8995 gcc_rich_location rich_loc (input_location);
8996 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8997 expr, type);
8998 warning_at (&rich_loc, OPT_Wold_style_cast,
8999 "use of old-style cast to %q#T", type);
9002 /* Only type conversions to integral or enumeration types
9003 can be used in constant-expressions. */
9004 if (!cast_valid_in_integral_constant_expression_p (type)
9005 && cp_parser_non_integral_constant_expression (parser,
9006 NIC_CAST))
9007 return error_mark_node;
9009 /* Perform the cast. */
9010 /* Make a location:
9011 (TYPE) EXPR
9012 ^~~~~~~~~~~
9013 with start==caret at the open paren, extending to the
9014 end of "expr". */
9015 location_t cast_loc = make_location (open_paren_loc,
9016 open_paren_loc,
9017 expr.get_finish ());
9018 expr = build_c_cast (cast_loc, type, expr);
9019 return expr;
9022 else
9023 cp_parser_abort_tentative_parse (parser);
9026 /* If we get here, then it's not a cast, so it must be a
9027 unary-expression. */
9028 return cp_parser_unary_expression (parser, pidk, address_p,
9029 cast_p, decltype_p);
9032 /* Parse a binary expression of the general form:
9034 pm-expression:
9035 cast-expression
9036 pm-expression .* cast-expression
9037 pm-expression ->* cast-expression
9039 multiplicative-expression:
9040 pm-expression
9041 multiplicative-expression * pm-expression
9042 multiplicative-expression / pm-expression
9043 multiplicative-expression % pm-expression
9045 additive-expression:
9046 multiplicative-expression
9047 additive-expression + multiplicative-expression
9048 additive-expression - multiplicative-expression
9050 shift-expression:
9051 additive-expression
9052 shift-expression << additive-expression
9053 shift-expression >> additive-expression
9055 relational-expression:
9056 shift-expression
9057 relational-expression < shift-expression
9058 relational-expression > shift-expression
9059 relational-expression <= shift-expression
9060 relational-expression >= shift-expression
9062 GNU Extension:
9064 relational-expression:
9065 relational-expression <? shift-expression
9066 relational-expression >? shift-expression
9068 equality-expression:
9069 relational-expression
9070 equality-expression == relational-expression
9071 equality-expression != relational-expression
9073 and-expression:
9074 equality-expression
9075 and-expression & equality-expression
9077 exclusive-or-expression:
9078 and-expression
9079 exclusive-or-expression ^ and-expression
9081 inclusive-or-expression:
9082 exclusive-or-expression
9083 inclusive-or-expression | exclusive-or-expression
9085 logical-and-expression:
9086 inclusive-or-expression
9087 logical-and-expression && inclusive-or-expression
9089 logical-or-expression:
9090 logical-and-expression
9091 logical-or-expression || logical-and-expression
9093 All these are implemented with a single function like:
9095 binary-expression:
9096 simple-cast-expression
9097 binary-expression <token> binary-expression
9099 CAST_P is true if this expression is the target of a cast.
9101 The binops_by_token map is used to get the tree codes for each <token> type.
9102 binary-expressions are associated according to a precedence table. */
9104 #define TOKEN_PRECEDENCE(token) \
9105 (((token->type == CPP_GREATER \
9106 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9107 && !parser->greater_than_is_operator_p) \
9108 ? PREC_NOT_OPERATOR \
9109 : binops_by_token[token->type].prec)
9111 static cp_expr
9112 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9113 bool no_toplevel_fold_p,
9114 bool decltype_p,
9115 enum cp_parser_prec prec,
9116 cp_id_kind * pidk)
9118 cp_parser_expression_stack stack;
9119 cp_parser_expression_stack_entry *sp = &stack[0];
9120 cp_parser_expression_stack_entry current;
9121 cp_expr rhs;
9122 cp_token *token;
9123 enum tree_code rhs_type;
9124 enum cp_parser_prec new_prec, lookahead_prec;
9125 tree overload;
9127 /* Parse the first expression. */
9128 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9129 ? TRUTH_NOT_EXPR : ERROR_MARK);
9130 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9131 cast_p, decltype_p, pidk);
9132 current.prec = prec;
9134 if (cp_parser_error_occurred (parser))
9135 return error_mark_node;
9137 for (;;)
9139 /* Get an operator token. */
9140 token = cp_lexer_peek_token (parser->lexer);
9142 if (warn_cxx11_compat
9143 && token->type == CPP_RSHIFT
9144 && !parser->greater_than_is_operator_p)
9146 if (warning_at (token->location, OPT_Wc__11_compat,
9147 "%<>>%> operator is treated"
9148 " as two right angle brackets in C++11"))
9149 inform (token->location,
9150 "suggest parentheses around %<>>%> expression");
9153 new_prec = TOKEN_PRECEDENCE (token);
9154 if (new_prec != PREC_NOT_OPERATOR
9155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9156 /* This is a fold-expression; handle it later. */
9157 new_prec = PREC_NOT_OPERATOR;
9159 /* Popping an entry off the stack means we completed a subexpression:
9160 - either we found a token which is not an operator (`>' where it is not
9161 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9162 will happen repeatedly;
9163 - or, we found an operator which has lower priority. This is the case
9164 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9165 parsing `3 * 4'. */
9166 if (new_prec <= current.prec)
9168 if (sp == stack)
9169 break;
9170 else
9171 goto pop;
9174 get_rhs:
9175 current.tree_type = binops_by_token[token->type].tree_type;
9176 current.loc = token->location;
9178 /* We used the operator token. */
9179 cp_lexer_consume_token (parser->lexer);
9181 /* For "false && x" or "true || x", x will never be executed;
9182 disable warnings while evaluating it. */
9183 if (current.tree_type == TRUTH_ANDIF_EXPR)
9184 c_inhibit_evaluation_warnings +=
9185 cp_fully_fold (current.lhs) == truthvalue_false_node;
9186 else if (current.tree_type == TRUTH_ORIF_EXPR)
9187 c_inhibit_evaluation_warnings +=
9188 cp_fully_fold (current.lhs) == truthvalue_true_node;
9190 /* Extract another operand. It may be the RHS of this expression
9191 or the LHS of a new, higher priority expression. */
9192 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9193 ? TRUTH_NOT_EXPR : ERROR_MARK);
9194 rhs = cp_parser_simple_cast_expression (parser);
9196 /* Get another operator token. Look up its precedence to avoid
9197 building a useless (immediately popped) stack entry for common
9198 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9199 token = cp_lexer_peek_token (parser->lexer);
9200 lookahead_prec = TOKEN_PRECEDENCE (token);
9201 if (lookahead_prec != PREC_NOT_OPERATOR
9202 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9203 lookahead_prec = PREC_NOT_OPERATOR;
9204 if (lookahead_prec > new_prec)
9206 /* ... and prepare to parse the RHS of the new, higher priority
9207 expression. Since precedence levels on the stack are
9208 monotonically increasing, we do not have to care about
9209 stack overflows. */
9210 *sp = current;
9211 ++sp;
9212 current.lhs = rhs;
9213 current.lhs_type = rhs_type;
9214 current.prec = new_prec;
9215 new_prec = lookahead_prec;
9216 goto get_rhs;
9218 pop:
9219 lookahead_prec = new_prec;
9220 /* If the stack is not empty, we have parsed into LHS the right side
9221 (`4' in the example above) of an expression we had suspended.
9222 We can use the information on the stack to recover the LHS (`3')
9223 from the stack together with the tree code (`MULT_EXPR'), and
9224 the precedence of the higher level subexpression
9225 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9226 which will be used to actually build the additive expression. */
9227 rhs = current.lhs;
9228 rhs_type = current.lhs_type;
9229 --sp;
9230 current = *sp;
9233 /* Undo the disabling of warnings done above. */
9234 if (current.tree_type == TRUTH_ANDIF_EXPR)
9235 c_inhibit_evaluation_warnings -=
9236 cp_fully_fold (current.lhs) == truthvalue_false_node;
9237 else if (current.tree_type == TRUTH_ORIF_EXPR)
9238 c_inhibit_evaluation_warnings -=
9239 cp_fully_fold (current.lhs) == truthvalue_true_node;
9241 if (warn_logical_not_paren
9242 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9243 && current.lhs_type == TRUTH_NOT_EXPR
9244 /* Avoid warning for !!x == y. */
9245 && (TREE_CODE (current.lhs) != NE_EXPR
9246 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9247 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9248 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9249 /* Avoid warning for !b == y where b is boolean. */
9250 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9251 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9252 != BOOLEAN_TYPE))))
9253 /* Avoid warning for !!b == y where b is boolean. */
9254 && (!DECL_P (current.lhs)
9255 || TREE_TYPE (current.lhs) == NULL_TREE
9256 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9257 warn_logical_not_parentheses (current.loc, current.tree_type,
9258 current.lhs, maybe_constant_value (rhs));
9260 overload = NULL;
9262 location_t combined_loc = make_location (current.loc,
9263 current.lhs.get_start (),
9264 rhs.get_finish ());
9266 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9267 ERROR_MARK for everything that is not a binary expression.
9268 This makes warn_about_parentheses miss some warnings that
9269 involve unary operators. For unary expressions we should
9270 pass the correct tree_code unless the unary expression was
9271 surrounded by parentheses.
9273 if (no_toplevel_fold_p
9274 && lookahead_prec <= current.prec
9275 && sp == stack)
9276 current.lhs = build2_loc (combined_loc,
9277 current.tree_type,
9278 TREE_CODE_CLASS (current.tree_type)
9279 == tcc_comparison
9280 ? boolean_type_node : TREE_TYPE (current.lhs),
9281 current.lhs, rhs);
9282 else
9284 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9285 current.lhs, current.lhs_type,
9286 rhs, rhs_type, &overload,
9287 complain_flags (decltype_p));
9288 /* TODO: build_x_binary_op doesn't always honor the location. */
9289 current.lhs.set_location (combined_loc);
9291 current.lhs_type = current.tree_type;
9293 /* If the binary operator required the use of an overloaded operator,
9294 then this expression cannot be an integral constant-expression.
9295 An overloaded operator can be used even if both operands are
9296 otherwise permissible in an integral constant-expression if at
9297 least one of the operands is of enumeration type. */
9299 if (overload
9300 && cp_parser_non_integral_constant_expression (parser,
9301 NIC_OVERLOADED))
9302 return error_mark_node;
9305 return current.lhs;
9308 static cp_expr
9309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9310 bool no_toplevel_fold_p,
9311 enum cp_parser_prec prec,
9312 cp_id_kind * pidk)
9314 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9315 /*decltype*/false, prec, pidk);
9318 /* Parse the `? expression : assignment-expression' part of a
9319 conditional-expression. The LOGICAL_OR_EXPR is the
9320 logical-or-expression that started the conditional-expression.
9321 Returns a representation of the entire conditional-expression.
9323 This routine is used by cp_parser_assignment_expression.
9325 ? expression : assignment-expression
9327 GNU Extensions:
9329 ? : assignment-expression */
9331 static tree
9332 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9334 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9335 cp_expr assignment_expr;
9336 struct cp_token *token;
9337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9339 /* Consume the `?' token. */
9340 cp_lexer_consume_token (parser->lexer);
9341 token = cp_lexer_peek_token (parser->lexer);
9342 if (cp_parser_allow_gnu_extensions_p (parser)
9343 && token->type == CPP_COLON)
9345 pedwarn (token->location, OPT_Wpedantic,
9346 "ISO C++ does not allow ?: with omitted middle operand");
9347 /* Implicit true clause. */
9348 expr = NULL_TREE;
9349 c_inhibit_evaluation_warnings +=
9350 folded_logical_or_expr == truthvalue_true_node;
9351 warn_for_omitted_condop (token->location, logical_or_expr);
9353 else
9355 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9356 parser->colon_corrects_to_scope_p = false;
9357 /* Parse the expression. */
9358 c_inhibit_evaluation_warnings +=
9359 folded_logical_or_expr == truthvalue_false_node;
9360 expr = cp_parser_expression (parser);
9361 c_inhibit_evaluation_warnings +=
9362 ((folded_logical_or_expr == truthvalue_true_node)
9363 - (folded_logical_or_expr == truthvalue_false_node));
9364 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9367 /* The next token should be a `:'. */
9368 cp_parser_require (parser, CPP_COLON, RT_COLON);
9369 /* Parse the assignment-expression. */
9370 assignment_expr = cp_parser_assignment_expression (parser);
9371 c_inhibit_evaluation_warnings -=
9372 folded_logical_or_expr == truthvalue_true_node;
9374 /* Make a location:
9375 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9376 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9377 with the caret at the "?", ranging from the start of
9378 the logical_or_expr to the end of the assignment_expr. */
9379 loc = make_location (loc,
9380 logical_or_expr.get_start (),
9381 assignment_expr.get_finish ());
9383 /* Build the conditional-expression. */
9384 return build_x_conditional_expr (loc, logical_or_expr,
9385 expr,
9386 assignment_expr,
9387 tf_warning_or_error);
9390 /* Parse an assignment-expression.
9392 assignment-expression:
9393 conditional-expression
9394 logical-or-expression assignment-operator assignment_expression
9395 throw-expression
9397 CAST_P is true if this expression is the target of a cast.
9398 DECLTYPE_P is true if this expression is the operand of decltype.
9400 Returns a representation for the expression. */
9402 static cp_expr
9403 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9404 bool cast_p, bool decltype_p)
9406 cp_expr expr;
9408 /* If the next token is the `throw' keyword, then we're looking at
9409 a throw-expression. */
9410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9411 expr = cp_parser_throw_expression (parser);
9412 /* Otherwise, it must be that we are looking at a
9413 logical-or-expression. */
9414 else
9416 /* Parse the binary expressions (logical-or-expression). */
9417 expr = cp_parser_binary_expression (parser, cast_p, false,
9418 decltype_p,
9419 PREC_NOT_OPERATOR, pidk);
9420 /* If the next token is a `?' then we're actually looking at a
9421 conditional-expression. */
9422 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9423 return cp_parser_question_colon_clause (parser, expr);
9424 else
9426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9428 /* If it's an assignment-operator, we're using the second
9429 production. */
9430 enum tree_code assignment_operator
9431 = cp_parser_assignment_operator_opt (parser);
9432 if (assignment_operator != ERROR_MARK)
9434 bool non_constant_p;
9436 /* Parse the right-hand side of the assignment. */
9437 cp_expr rhs = cp_parser_initializer_clause (parser,
9438 &non_constant_p);
9440 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9441 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9443 /* An assignment may not appear in a
9444 constant-expression. */
9445 if (cp_parser_non_integral_constant_expression (parser,
9446 NIC_ASSIGNMENT))
9447 return error_mark_node;
9448 /* Build the assignment expression. Its default
9449 location:
9450 LHS = RHS
9451 ~~~~^~~~~
9452 is the location of the '=' token as the
9453 caret, ranging from the start of the lhs to the
9454 end of the rhs. */
9455 loc = make_location (loc,
9456 expr.get_start (),
9457 rhs.get_finish ());
9458 expr = build_x_modify_expr (loc, expr,
9459 assignment_operator,
9460 rhs,
9461 complain_flags (decltype_p));
9462 /* TODO: build_x_modify_expr doesn't honor the location,
9463 so we must set it here. */
9464 expr.set_location (loc);
9469 return expr;
9472 /* Parse an (optional) assignment-operator.
9474 assignment-operator: one of
9475 = *= /= %= += -= >>= <<= &= ^= |=
9477 GNU Extension:
9479 assignment-operator: one of
9480 <?= >?=
9482 If the next token is an assignment operator, the corresponding tree
9483 code is returned, and the token is consumed. For example, for
9484 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9485 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9486 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9487 operator, ERROR_MARK is returned. */
9489 static enum tree_code
9490 cp_parser_assignment_operator_opt (cp_parser* parser)
9492 enum tree_code op;
9493 cp_token *token;
9495 /* Peek at the next token. */
9496 token = cp_lexer_peek_token (parser->lexer);
9498 switch (token->type)
9500 case CPP_EQ:
9501 op = NOP_EXPR;
9502 break;
9504 case CPP_MULT_EQ:
9505 op = MULT_EXPR;
9506 break;
9508 case CPP_DIV_EQ:
9509 op = TRUNC_DIV_EXPR;
9510 break;
9512 case CPP_MOD_EQ:
9513 op = TRUNC_MOD_EXPR;
9514 break;
9516 case CPP_PLUS_EQ:
9517 op = PLUS_EXPR;
9518 break;
9520 case CPP_MINUS_EQ:
9521 op = MINUS_EXPR;
9522 break;
9524 case CPP_RSHIFT_EQ:
9525 op = RSHIFT_EXPR;
9526 break;
9528 case CPP_LSHIFT_EQ:
9529 op = LSHIFT_EXPR;
9530 break;
9532 case CPP_AND_EQ:
9533 op = BIT_AND_EXPR;
9534 break;
9536 case CPP_XOR_EQ:
9537 op = BIT_XOR_EXPR;
9538 break;
9540 case CPP_OR_EQ:
9541 op = BIT_IOR_EXPR;
9542 break;
9544 default:
9545 /* Nothing else is an assignment operator. */
9546 op = ERROR_MARK;
9549 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9550 if (op != ERROR_MARK
9551 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9552 op = ERROR_MARK;
9554 /* If it was an assignment operator, consume it. */
9555 if (op != ERROR_MARK)
9556 cp_lexer_consume_token (parser->lexer);
9558 return op;
9561 /* Parse an expression.
9563 expression:
9564 assignment-expression
9565 expression , assignment-expression
9567 CAST_P is true if this expression is the target of a cast.
9568 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9569 except possibly parenthesized or on the RHS of a comma (N3276).
9571 Returns a representation of the expression. */
9573 static cp_expr
9574 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9575 bool cast_p, bool decltype_p)
9577 cp_expr expression = NULL_TREE;
9578 location_t loc = UNKNOWN_LOCATION;
9580 while (true)
9582 cp_expr assignment_expression;
9584 /* Parse the next assignment-expression. */
9585 assignment_expression
9586 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9588 /* We don't create a temporary for a call that is the immediate operand
9589 of decltype or on the RHS of a comma. But when we see a comma, we
9590 need to create a temporary for a call on the LHS. */
9591 if (decltype_p && !processing_template_decl
9592 && TREE_CODE (assignment_expression) == CALL_EXPR
9593 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9594 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9595 assignment_expression
9596 = build_cplus_new (TREE_TYPE (assignment_expression),
9597 assignment_expression, tf_warning_or_error);
9599 /* If this is the first assignment-expression, we can just
9600 save it away. */
9601 if (!expression)
9602 expression = assignment_expression;
9603 else
9605 /* Create a location with caret at the comma, ranging
9606 from the start of the LHS to the end of the RHS. */
9607 loc = make_location (loc,
9608 expression.get_start (),
9609 assignment_expression.get_finish ());
9610 expression = build_x_compound_expr (loc, expression,
9611 assignment_expression,
9612 complain_flags (decltype_p));
9613 expression.set_location (loc);
9615 /* If the next token is not a comma, or we're in a fold-expression, then
9616 we are done with the expression. */
9617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9618 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9619 break;
9620 /* Consume the `,'. */
9621 loc = cp_lexer_peek_token (parser->lexer)->location;
9622 cp_lexer_consume_token (parser->lexer);
9623 /* A comma operator cannot appear in a constant-expression. */
9624 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9625 expression = error_mark_node;
9628 return expression;
9631 /* Parse a constant-expression.
9633 constant-expression:
9634 conditional-expression
9636 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9637 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9638 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9639 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9640 only parse a conditional-expression, otherwise parse an
9641 assignment-expression. See below for rationale. */
9643 static cp_expr
9644 cp_parser_constant_expression (cp_parser* parser,
9645 bool allow_non_constant_p,
9646 bool *non_constant_p,
9647 bool strict_p)
9649 bool saved_integral_constant_expression_p;
9650 bool saved_allow_non_integral_constant_expression_p;
9651 bool saved_non_integral_constant_expression_p;
9652 cp_expr expression;
9654 /* It might seem that we could simply parse the
9655 conditional-expression, and then check to see if it were
9656 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9657 one that the compiler can figure out is constant, possibly after
9658 doing some simplifications or optimizations. The standard has a
9659 precise definition of constant-expression, and we must honor
9660 that, even though it is somewhat more restrictive.
9662 For example:
9664 int i[(2, 3)];
9666 is not a legal declaration, because `(2, 3)' is not a
9667 constant-expression. The `,' operator is forbidden in a
9668 constant-expression. However, GCC's constant-folding machinery
9669 will fold this operation to an INTEGER_CST for `3'. */
9671 /* Save the old settings. */
9672 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9673 saved_allow_non_integral_constant_expression_p
9674 = parser->allow_non_integral_constant_expression_p;
9675 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9676 /* We are now parsing a constant-expression. */
9677 parser->integral_constant_expression_p = true;
9678 parser->allow_non_integral_constant_expression_p
9679 = (allow_non_constant_p || cxx_dialect >= cxx11);
9680 parser->non_integral_constant_expression_p = false;
9681 /* Although the grammar says "conditional-expression", when not STRICT_P,
9682 we parse an "assignment-expression", which also permits
9683 "throw-expression" and the use of assignment operators. In the case
9684 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9685 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9686 actually essential that we look for an assignment-expression.
9687 For example, cp_parser_initializer_clauses uses this function to
9688 determine whether a particular assignment-expression is in fact
9689 constant. */
9690 if (strict_p)
9692 /* Parse the binary expressions (logical-or-expression). */
9693 expression = cp_parser_binary_expression (parser, false, false, false,
9694 PREC_NOT_OPERATOR, NULL);
9695 /* If the next token is a `?' then we're actually looking at
9696 a conditional-expression; otherwise we're done. */
9697 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9698 expression = cp_parser_question_colon_clause (parser, expression);
9700 else
9701 expression = cp_parser_assignment_expression (parser);
9702 /* Restore the old settings. */
9703 parser->integral_constant_expression_p
9704 = saved_integral_constant_expression_p;
9705 parser->allow_non_integral_constant_expression_p
9706 = saved_allow_non_integral_constant_expression_p;
9707 if (cxx_dialect >= cxx11)
9709 /* Require an rvalue constant expression here; that's what our
9710 callers expect. Reference constant expressions are handled
9711 separately in e.g. cp_parser_template_argument. */
9712 tree decay = expression;
9713 if (TREE_TYPE (expression)
9714 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9715 decay = build_address (expression);
9716 bool is_const = potential_rvalue_constant_expression (decay);
9717 parser->non_integral_constant_expression_p = !is_const;
9718 if (!is_const && !allow_non_constant_p)
9719 require_potential_rvalue_constant_expression (decay);
9721 if (allow_non_constant_p)
9722 *non_constant_p = parser->non_integral_constant_expression_p;
9723 parser->non_integral_constant_expression_p
9724 = saved_non_integral_constant_expression_p;
9726 return expression;
9729 /* Parse __builtin_offsetof.
9731 offsetof-expression:
9732 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9734 offsetof-member-designator:
9735 id-expression
9736 | offsetof-member-designator "." id-expression
9737 | offsetof-member-designator "[" expression "]"
9738 | offsetof-member-designator "->" id-expression */
9740 static cp_expr
9741 cp_parser_builtin_offsetof (cp_parser *parser)
9743 int save_ice_p, save_non_ice_p;
9744 tree type;
9745 cp_expr expr;
9746 cp_id_kind dummy;
9747 cp_token *token;
9748 location_t finish_loc;
9750 /* We're about to accept non-integral-constant things, but will
9751 definitely yield an integral constant expression. Save and
9752 restore these values around our local parsing. */
9753 save_ice_p = parser->integral_constant_expression_p;
9754 save_non_ice_p = parser->non_integral_constant_expression_p;
9756 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9758 /* Consume the "__builtin_offsetof" token. */
9759 cp_lexer_consume_token (parser->lexer);
9760 /* Consume the opening `('. */
9761 matching_parens parens;
9762 parens.require_open (parser);
9763 /* Parse the type-id. */
9764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9765 type = cp_parser_type_id (parser);
9766 /* Look for the `,'. */
9767 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9768 token = cp_lexer_peek_token (parser->lexer);
9770 /* Build the (type *)null that begins the traditional offsetof macro. */
9771 tree object_ptr
9772 = build_static_cast (build_pointer_type (type), null_pointer_node,
9773 tf_warning_or_error);
9775 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9776 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9777 true, &dummy, token->location);
9778 while (true)
9780 token = cp_lexer_peek_token (parser->lexer);
9781 switch (token->type)
9783 case CPP_OPEN_SQUARE:
9784 /* offsetof-member-designator "[" expression "]" */
9785 expr = cp_parser_postfix_open_square_expression (parser, expr,
9786 true, false);
9787 break;
9789 case CPP_DEREF:
9790 /* offsetof-member-designator "->" identifier */
9791 expr = grok_array_decl (token->location, expr,
9792 integer_zero_node, false);
9793 /* FALLTHRU */
9795 case CPP_DOT:
9796 /* offsetof-member-designator "." identifier */
9797 cp_lexer_consume_token (parser->lexer);
9798 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9799 expr, true, &dummy,
9800 token->location);
9801 break;
9803 case CPP_CLOSE_PAREN:
9804 /* Consume the ")" token. */
9805 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9806 cp_lexer_consume_token (parser->lexer);
9807 goto success;
9809 default:
9810 /* Error. We know the following require will fail, but
9811 that gives the proper error message. */
9812 parens.require_close (parser);
9813 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9814 expr = error_mark_node;
9815 goto failure;
9819 success:
9820 /* Make a location of the form:
9821 __builtin_offsetof (struct s, f)
9822 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9823 with caret at the type-id, ranging from the start of the
9824 "_builtin_offsetof" token to the close paren. */
9825 loc = make_location (loc, start_loc, finish_loc);
9826 /* The result will be an INTEGER_CST, so we need to explicitly
9827 preserve the location. */
9828 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9830 failure:
9831 parser->integral_constant_expression_p = save_ice_p;
9832 parser->non_integral_constant_expression_p = save_non_ice_p;
9834 return expr;
9837 /* Parse a trait expression.
9839 Returns a representation of the expression, the underlying type
9840 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9842 static cp_expr
9843 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9845 cp_trait_kind kind;
9846 tree type1, type2 = NULL_TREE;
9847 bool binary = false;
9848 bool variadic = false;
9850 switch (keyword)
9852 case RID_HAS_NOTHROW_ASSIGN:
9853 kind = CPTK_HAS_NOTHROW_ASSIGN;
9854 break;
9855 case RID_HAS_NOTHROW_CONSTRUCTOR:
9856 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9857 break;
9858 case RID_HAS_NOTHROW_COPY:
9859 kind = CPTK_HAS_NOTHROW_COPY;
9860 break;
9861 case RID_HAS_TRIVIAL_ASSIGN:
9862 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9863 break;
9864 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9865 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9866 break;
9867 case RID_HAS_TRIVIAL_COPY:
9868 kind = CPTK_HAS_TRIVIAL_COPY;
9869 break;
9870 case RID_HAS_TRIVIAL_DESTRUCTOR:
9871 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9872 break;
9873 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9874 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9875 break;
9876 case RID_HAS_VIRTUAL_DESTRUCTOR:
9877 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9878 break;
9879 case RID_IS_ABSTRACT:
9880 kind = CPTK_IS_ABSTRACT;
9881 break;
9882 case RID_IS_AGGREGATE:
9883 kind = CPTK_IS_AGGREGATE;
9884 break;
9885 case RID_IS_BASE_OF:
9886 kind = CPTK_IS_BASE_OF;
9887 binary = true;
9888 break;
9889 case RID_IS_CLASS:
9890 kind = CPTK_IS_CLASS;
9891 break;
9892 case RID_IS_EMPTY:
9893 kind = CPTK_IS_EMPTY;
9894 break;
9895 case RID_IS_ENUM:
9896 kind = CPTK_IS_ENUM;
9897 break;
9898 case RID_IS_FINAL:
9899 kind = CPTK_IS_FINAL;
9900 break;
9901 case RID_IS_LITERAL_TYPE:
9902 kind = CPTK_IS_LITERAL_TYPE;
9903 break;
9904 case RID_IS_POD:
9905 kind = CPTK_IS_POD;
9906 break;
9907 case RID_IS_POLYMORPHIC:
9908 kind = CPTK_IS_POLYMORPHIC;
9909 break;
9910 case RID_IS_SAME_AS:
9911 kind = CPTK_IS_SAME_AS;
9912 binary = true;
9913 break;
9914 case RID_IS_STD_LAYOUT:
9915 kind = CPTK_IS_STD_LAYOUT;
9916 break;
9917 case RID_IS_TRIVIAL:
9918 kind = CPTK_IS_TRIVIAL;
9919 break;
9920 case RID_IS_TRIVIALLY_ASSIGNABLE:
9921 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9922 binary = true;
9923 break;
9924 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9925 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9926 variadic = true;
9927 break;
9928 case RID_IS_TRIVIALLY_COPYABLE:
9929 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9930 break;
9931 case RID_IS_UNION:
9932 kind = CPTK_IS_UNION;
9933 break;
9934 case RID_UNDERLYING_TYPE:
9935 kind = CPTK_UNDERLYING_TYPE;
9936 break;
9937 case RID_BASES:
9938 kind = CPTK_BASES;
9939 break;
9940 case RID_DIRECT_BASES:
9941 kind = CPTK_DIRECT_BASES;
9942 break;
9943 case RID_IS_ASSIGNABLE:
9944 kind = CPTK_IS_ASSIGNABLE;
9945 binary = true;
9946 break;
9947 case RID_IS_CONSTRUCTIBLE:
9948 kind = CPTK_IS_CONSTRUCTIBLE;
9949 variadic = true;
9950 break;
9951 default:
9952 gcc_unreachable ();
9955 /* Get location of initial token. */
9956 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9958 /* Consume the token. */
9959 cp_lexer_consume_token (parser->lexer);
9961 matching_parens parens;
9962 parens.require_open (parser);
9965 type_id_in_expr_sentinel s (parser);
9966 type1 = cp_parser_type_id (parser);
9969 if (type1 == error_mark_node)
9970 return error_mark_node;
9972 if (binary)
9974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9977 type_id_in_expr_sentinel s (parser);
9978 type2 = cp_parser_type_id (parser);
9981 if (type2 == error_mark_node)
9982 return error_mark_node;
9984 else if (variadic)
9986 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9988 cp_lexer_consume_token (parser->lexer);
9989 tree elt = cp_parser_type_id (parser);
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9992 cp_lexer_consume_token (parser->lexer);
9993 elt = make_pack_expansion (elt);
9995 if (elt == error_mark_node)
9996 return error_mark_node;
9997 type2 = tree_cons (NULL_TREE, elt, type2);
10001 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10002 parens.require_close (parser);
10004 /* Construct a location of the form:
10005 __is_trivially_copyable(_Tp)
10006 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10007 with start == caret, finishing at the close-paren. */
10008 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10010 /* Complete the trait expression, which may mean either processing
10011 the trait expr now or saving it for template instantiation. */
10012 switch (kind)
10014 case CPTK_UNDERLYING_TYPE:
10015 return cp_expr (finish_underlying_type (type1), trait_loc);
10016 case CPTK_BASES:
10017 return cp_expr (finish_bases (type1, false), trait_loc);
10018 case CPTK_DIRECT_BASES:
10019 return cp_expr (finish_bases (type1, true), trait_loc);
10020 default:
10021 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10025 /* Parse a lambda expression.
10027 lambda-expression:
10028 lambda-introducer lambda-declarator [opt] compound-statement
10030 Returns a representation of the expression. */
10032 static cp_expr
10033 cp_parser_lambda_expression (cp_parser* parser)
10035 tree lambda_expr = build_lambda_expr ();
10036 tree type;
10037 bool ok = true;
10038 cp_token *token = cp_lexer_peek_token (parser->lexer);
10039 cp_token_position start = 0;
10041 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10043 if (cp_unevaluated_operand)
10045 if (!token->error_reported)
10047 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10048 "lambda-expression in unevaluated context");
10049 token->error_reported = true;
10051 ok = false;
10053 else if (parser->in_template_argument_list_p)
10055 if (!token->error_reported)
10057 error_at (token->location, "lambda-expression in template-argument");
10058 token->error_reported = true;
10060 ok = false;
10063 /* We may be in the middle of deferred access check. Disable
10064 it now. */
10065 push_deferring_access_checks (dk_no_deferred);
10067 cp_parser_lambda_introducer (parser, lambda_expr);
10069 type = begin_lambda_type (lambda_expr);
10070 if (type == error_mark_node)
10071 return error_mark_node;
10073 record_lambda_scope (lambda_expr);
10075 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10076 determine_visibility (TYPE_NAME (type));
10078 /* Now that we've started the type, add the capture fields for any
10079 explicit captures. */
10080 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10083 /* Inside the class, surrounding template-parameter-lists do not apply. */
10084 unsigned int saved_num_template_parameter_lists
10085 = parser->num_template_parameter_lists;
10086 unsigned char in_statement = parser->in_statement;
10087 bool in_switch_statement_p = parser->in_switch_statement_p;
10088 bool fully_implicit_function_template_p
10089 = parser->fully_implicit_function_template_p;
10090 tree implicit_template_parms = parser->implicit_template_parms;
10091 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10092 bool auto_is_implicit_function_template_parm_p
10093 = parser->auto_is_implicit_function_template_parm_p;
10095 parser->num_template_parameter_lists = 0;
10096 parser->in_statement = 0;
10097 parser->in_switch_statement_p = false;
10098 parser->fully_implicit_function_template_p = false;
10099 parser->implicit_template_parms = 0;
10100 parser->implicit_template_scope = 0;
10101 parser->auto_is_implicit_function_template_parm_p = false;
10103 /* By virtue of defining a local class, a lambda expression has access to
10104 the private variables of enclosing classes. */
10106 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10108 if (ok && cp_parser_error_occurred (parser))
10109 ok = false;
10111 if (ok)
10113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10114 && cp_parser_start_tentative_firewall (parser))
10115 start = token;
10116 cp_parser_lambda_body (parser, lambda_expr);
10118 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10120 if (cp_parser_skip_to_closing_brace (parser))
10121 cp_lexer_consume_token (parser->lexer);
10124 /* The capture list was built up in reverse order; fix that now. */
10125 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10126 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10128 if (ok)
10129 maybe_add_lambda_conv_op (type);
10131 type = finish_struct (type, /*attributes=*/NULL_TREE);
10133 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10134 parser->in_statement = in_statement;
10135 parser->in_switch_statement_p = in_switch_statement_p;
10136 parser->fully_implicit_function_template_p
10137 = fully_implicit_function_template_p;
10138 parser->implicit_template_parms = implicit_template_parms;
10139 parser->implicit_template_scope = implicit_template_scope;
10140 parser->auto_is_implicit_function_template_parm_p
10141 = auto_is_implicit_function_template_parm_p;
10144 /* This field is only used during parsing of the lambda. */
10145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10147 /* This lambda shouldn't have any proxies left at this point. */
10148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10149 /* And now that we're done, push proxies for an enclosing lambda. */
10150 insert_pending_capture_proxies ();
10152 if (ok)
10153 lambda_expr = build_lambda_object (lambda_expr);
10154 else
10155 lambda_expr = error_mark_node;
10157 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10159 pop_deferring_access_checks ();
10161 return lambda_expr;
10164 /* Parse the beginning of a lambda expression.
10166 lambda-introducer:
10167 [ lambda-capture [opt] ]
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10171 static void
10172 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10174 /* Need commas after the first capture. */
10175 bool first = true;
10177 /* Eat the leading `['. */
10178 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10180 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10181 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10182 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10184 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10185 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10187 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10189 cp_lexer_consume_token (parser->lexer);
10190 first = false;
10193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10195 cp_token* capture_token;
10196 tree capture_id;
10197 tree capture_init_expr;
10198 cp_id_kind idk = CP_ID_KIND_NONE;
10199 bool explicit_init_p = false;
10201 enum capture_kind_type
10203 BY_COPY,
10204 BY_REFERENCE
10206 enum capture_kind_type capture_kind = BY_COPY;
10208 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10210 error ("expected end of capture-list");
10211 return;
10214 if (first)
10215 first = false;
10216 else
10217 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10219 /* Possibly capture `this'. */
10220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10223 if (cxx_dialect < cxx2a
10224 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10225 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10226 "with by-copy capture default");
10227 cp_lexer_consume_token (parser->lexer);
10228 add_capture (lambda_expr,
10229 /*id=*/this_identifier,
10230 /*initializer=*/finish_this_expr (),
10231 /*by_reference_p=*/true,
10232 explicit_init_p);
10233 continue;
10236 /* Possibly capture `*this'. */
10237 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10238 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10241 if (cxx_dialect < cxx17)
10242 pedwarn (loc, 0, "%<*this%> capture only available with "
10243 "-std=c++17 or -std=gnu++17");
10244 cp_lexer_consume_token (parser->lexer);
10245 cp_lexer_consume_token (parser->lexer);
10246 add_capture (lambda_expr,
10247 /*id=*/this_identifier,
10248 /*initializer=*/finish_this_expr (),
10249 /*by_reference_p=*/false,
10250 explicit_init_p);
10251 continue;
10254 /* Remember whether we want to capture as a reference or not. */
10255 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10257 capture_kind = BY_REFERENCE;
10258 cp_lexer_consume_token (parser->lexer);
10261 /* Get the identifier. */
10262 capture_token = cp_lexer_peek_token (parser->lexer);
10263 capture_id = cp_parser_identifier (parser);
10265 if (capture_id == error_mark_node)
10266 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10267 delimiters, but I modified this to stop on unnested ']' as well. It
10268 was already changed to stop on unnested '}', so the
10269 "closing_parenthesis" name is no more misleading with my change. */
10271 cp_parser_skip_to_closing_parenthesis (parser,
10272 /*recovering=*/true,
10273 /*or_comma=*/true,
10274 /*consume_paren=*/true);
10275 break;
10278 /* Find the initializer for this capture. */
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10280 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10281 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10283 bool direct, non_constant;
10284 /* An explicit initializer exists. */
10285 if (cxx_dialect < cxx14)
10286 pedwarn (input_location, 0,
10287 "lambda capture initializers "
10288 "only available with -std=c++14 or -std=gnu++14");
10289 capture_init_expr = cp_parser_initializer (parser, &direct,
10290 &non_constant);
10291 explicit_init_p = true;
10292 if (capture_init_expr == NULL_TREE)
10294 error ("empty initializer for lambda init-capture");
10295 capture_init_expr = error_mark_node;
10298 else
10300 const char* error_msg;
10302 /* Turn the identifier into an id-expression. */
10303 capture_init_expr
10304 = cp_parser_lookup_name_simple (parser, capture_id,
10305 capture_token->location);
10307 if (capture_init_expr == error_mark_node)
10309 unqualified_name_lookup_error (capture_id);
10310 continue;
10312 else if (DECL_P (capture_init_expr)
10313 && (!VAR_P (capture_init_expr)
10314 && TREE_CODE (capture_init_expr) != PARM_DECL))
10316 error_at (capture_token->location,
10317 "capture of non-variable %qD ",
10318 capture_init_expr);
10319 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10320 "%q#D declared here", capture_init_expr);
10321 continue;
10323 if (VAR_P (capture_init_expr)
10324 && decl_storage_duration (capture_init_expr) != dk_auto)
10326 if (pedwarn (capture_token->location, 0, "capture of variable "
10327 "%qD with non-automatic storage duration",
10328 capture_init_expr))
10329 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10330 "%q#D declared here", capture_init_expr);
10331 continue;
10334 capture_init_expr
10335 = finish_id_expression
10336 (capture_id,
10337 capture_init_expr,
10338 parser->scope,
10339 &idk,
10340 /*integral_constant_expression_p=*/false,
10341 /*allow_non_integral_constant_expression_p=*/false,
10342 /*non_integral_constant_expression_p=*/NULL,
10343 /*template_p=*/false,
10344 /*done=*/true,
10345 /*address_p=*/false,
10346 /*template_arg_p=*/false,
10347 &error_msg,
10348 capture_token->location);
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10352 cp_lexer_consume_token (parser->lexer);
10353 capture_init_expr = make_pack_expansion (capture_init_expr);
10355 else
10356 check_for_bare_parameter_packs (capture_init_expr);
10359 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10360 && !explicit_init_p)
10362 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10363 && capture_kind == BY_COPY)
10364 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10365 "of %qD redundant with by-copy capture default",
10366 capture_id);
10367 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10368 && capture_kind == BY_REFERENCE)
10369 pedwarn (capture_token->location, 0, "explicit by-reference "
10370 "capture of %qD redundant with by-reference capture "
10371 "default", capture_id);
10374 add_capture (lambda_expr,
10375 capture_id,
10376 capture_init_expr,
10377 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10378 explicit_init_p);
10381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10384 /* Parse the (optional) middle of a lambda expression.
10386 lambda-declarator:
10387 < template-parameter-list [opt] >
10388 ( parameter-declaration-clause [opt] )
10389 attribute-specifier [opt]
10390 decl-specifier-seq [opt]
10391 exception-specification [opt]
10392 lambda-return-type-clause [opt]
10394 LAMBDA_EXPR is the current representation of the lambda expression. */
10396 static bool
10397 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10399 /* 5.1.1.4 of the standard says:
10400 If a lambda-expression does not include a lambda-declarator, it is as if
10401 the lambda-declarator were ().
10402 This means an empty parameter list, no attributes, and no exception
10403 specification. */
10404 tree param_list = void_list_node;
10405 tree attributes = NULL_TREE;
10406 tree exception_spec = NULL_TREE;
10407 tree template_param_list = NULL_TREE;
10408 tree tx_qual = NULL_TREE;
10409 tree return_type = NULL_TREE;
10410 cp_decl_specifier_seq lambda_specs;
10411 clear_decl_specs (&lambda_specs);
10413 /* The template-parameter-list is optional, but must begin with
10414 an opening angle if present. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10417 if (cxx_dialect < cxx14)
10418 pedwarn (parser->lexer->next_token->location, 0,
10419 "lambda templates are only available with "
10420 "-std=c++14 or -std=gnu++14");
10421 else if (cxx_dialect < cxx2a)
10422 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10423 "lambda templates are only available with "
10424 "-std=c++2a or -std=gnu++2a");
10426 cp_lexer_consume_token (parser->lexer);
10428 template_param_list = cp_parser_template_parameter_list (parser);
10430 cp_parser_skip_to_end_of_template_parameter_list (parser);
10432 /* We just processed one more parameter list. */
10433 ++parser->num_template_parameter_lists;
10436 /* The parameter-declaration-clause is optional (unless
10437 template-parameter-list was given), but must begin with an
10438 opening parenthesis if present. */
10439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10441 matching_parens parens;
10442 parens.consume_open (parser);
10444 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10446 /* Parse parameters. */
10447 param_list = cp_parser_parameter_declaration_clause (parser);
10449 /* Default arguments shall not be specified in the
10450 parameter-declaration-clause of a lambda-declarator. */
10451 if (cxx_dialect < cxx14)
10452 for (tree t = param_list; t; t = TREE_CHAIN (t))
10453 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10454 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10455 "default argument specified for lambda parameter");
10457 parens.require_close (parser);
10459 attributes = cp_parser_attributes_opt (parser);
10461 /* In the decl-specifier-seq of the lambda-declarator, each
10462 decl-specifier shall either be mutable or constexpr. */
10463 int declares_class_or_enum;
10464 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10465 cp_parser_decl_specifier_seq (parser,
10466 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10467 &lambda_specs, &declares_class_or_enum);
10468 if (lambda_specs.storage_class == sc_mutable)
10470 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10471 if (lambda_specs.conflicting_specifiers_p)
10472 error_at (lambda_specs.locations[ds_storage_class],
10473 "duplicate %<mutable%>");
10476 tx_qual = cp_parser_tx_qualifier_opt (parser);
10478 /* Parse optional exception specification. */
10479 exception_spec = cp_parser_exception_specification_opt (parser);
10481 /* Parse optional trailing return type. */
10482 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10484 cp_lexer_consume_token (parser->lexer);
10485 return_type = cp_parser_trailing_type_id (parser);
10488 /* The function parameters must be in scope all the way until after the
10489 trailing-return-type in case of decltype. */
10490 pop_bindings_and_leave_scope ();
10492 else if (template_param_list != NULL_TREE) // generate diagnostic
10493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10495 /* Create the function call operator.
10497 Messing with declarators like this is no uglier than building up the
10498 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10499 other code. */
10501 cp_decl_specifier_seq return_type_specs;
10502 cp_declarator* declarator;
10503 tree fco;
10504 int quals;
10505 void *p;
10507 clear_decl_specs (&return_type_specs);
10508 if (return_type)
10509 return_type_specs.type = return_type;
10510 else
10511 /* Maybe we will deduce the return type later. */
10512 return_type_specs.type = make_auto ();
10514 if (lambda_specs.locations[ds_constexpr])
10516 if (cxx_dialect >= cxx17)
10517 return_type_specs.locations[ds_constexpr]
10518 = lambda_specs.locations[ds_constexpr];
10519 else
10520 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10521 "lambda only available with -std=c++17 or -std=gnu++17");
10524 p = obstack_alloc (&declarator_obstack, 0);
10526 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10528 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10529 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10530 declarator = make_call_declarator (declarator, param_list, quals,
10531 VIRT_SPEC_UNSPECIFIED,
10532 REF_QUAL_NONE,
10533 tx_qual,
10534 exception_spec,
10535 /*late_return_type=*/NULL_TREE,
10536 /*requires_clause*/NULL_TREE);
10537 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10539 fco = grokmethod (&return_type_specs,
10540 declarator,
10541 attributes);
10542 if (fco != error_mark_node)
10544 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10545 DECL_ARTIFICIAL (fco) = 1;
10546 /* Give the object parameter a different name. */
10547 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10548 if (return_type)
10549 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10551 if (template_param_list)
10553 fco = finish_member_template_decl (fco);
10554 finish_template_decl (template_param_list);
10555 --parser->num_template_parameter_lists;
10557 else if (parser->fully_implicit_function_template_p)
10558 fco = finish_fully_implicit_template (parser, fco);
10560 finish_member_declaration (fco);
10562 obstack_free (&declarator_obstack, p);
10564 return (fco != error_mark_node);
10568 /* Parse the body of a lambda expression, which is simply
10570 compound-statement
10572 but which requires special handling.
10573 LAMBDA_EXPR is the current representation of the lambda expression. */
10575 static void
10576 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10578 bool nested = (current_function_decl != NULL_TREE);
10579 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10580 bool in_function_body = parser->in_function_body;
10581 if (nested)
10582 push_function_context ();
10583 else
10584 /* Still increment function_depth so that we don't GC in the
10585 middle of an expression. */
10586 ++function_depth;
10587 vec<tree> omp_privatization_save;
10588 save_omp_privatization_clauses (omp_privatization_save);
10589 /* Clear this in case we're in the middle of a default argument. */
10590 parser->local_variables_forbidden_p = false;
10591 parser->in_function_body = true;
10593 /* Finish the function call operator
10594 - class_specifier
10595 + late_parsing_for_member
10596 + function_definition_after_declarator
10597 + ctor_initializer_opt_and_function_body */
10599 local_specialization_stack s (lss_copy);
10601 tree fco = lambda_function (lambda_expr);
10602 tree body = start_lambda_function (fco, lambda_expr);
10603 bool done = false;
10604 tree compound_stmt;
10606 matching_braces braces;
10607 if (!braces.require_open (parser))
10608 goto out;
10610 compound_stmt = begin_compound_stmt (0);
10612 /* 5.1.1.4 of the standard says:
10613 If a lambda-expression does not include a trailing-return-type, it
10614 is as if the trailing-return-type denotes the following type:
10615 * if the compound-statement is of the form
10616 { return attribute-specifier [opt] expression ; }
10617 the type of the returned expression after lvalue-to-rvalue
10618 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10619 (_conv.array_ 4.2), and function-to-pointer conversion
10620 (_conv.func_ 4.3);
10621 * otherwise, void. */
10623 /* In a lambda that has neither a lambda-return-type-clause
10624 nor a deducible form, errors should be reported for return statements
10625 in the body. Since we used void as the placeholder return type, parsing
10626 the body as usual will give such desired behavior. */
10627 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10628 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10629 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10631 tree expr = NULL_TREE;
10632 cp_id_kind idk = CP_ID_KIND_NONE;
10634 /* Parse tentatively in case there's more after the initial return
10635 statement. */
10636 cp_parser_parse_tentatively (parser);
10638 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10640 expr = cp_parser_expression (parser, &idk);
10642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10643 braces.require_close (parser);
10645 if (cp_parser_parse_definitely (parser))
10647 if (!processing_template_decl)
10649 tree type = lambda_return_type (expr);
10650 apply_deduced_return_type (fco, type);
10651 if (type == error_mark_node)
10652 expr = error_mark_node;
10655 /* Will get error here if type not deduced yet. */
10656 finish_return_stmt (expr);
10658 done = true;
10662 if (!done)
10664 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10665 cp_parser_label_declaration (parser);
10666 cp_parser_statement_seq_opt (parser, NULL_TREE);
10667 braces.require_close (parser);
10670 finish_compound_stmt (compound_stmt);
10672 out:
10673 finish_lambda_function (body);
10676 restore_omp_privatization_clauses (omp_privatization_save);
10677 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10678 parser->in_function_body = in_function_body;
10679 if (nested)
10680 pop_function_context();
10681 else
10682 --function_depth;
10685 /* Statements [gram.stmt.stmt] */
10687 /* Parse a statement.
10689 statement:
10690 labeled-statement
10691 expression-statement
10692 compound-statement
10693 selection-statement
10694 iteration-statement
10695 jump-statement
10696 declaration-statement
10697 try-block
10699 C++11:
10701 statement:
10702 labeled-statement
10703 attribute-specifier-seq (opt) expression-statement
10704 attribute-specifier-seq (opt) compound-statement
10705 attribute-specifier-seq (opt) selection-statement
10706 attribute-specifier-seq (opt) iteration-statement
10707 attribute-specifier-seq (opt) jump-statement
10708 declaration-statement
10709 attribute-specifier-seq (opt) try-block
10711 init-statement:
10712 expression-statement
10713 simple-declaration
10715 TM Extension:
10717 statement:
10718 atomic-statement
10720 IN_COMPOUND is true when the statement is nested inside a
10721 cp_parser_compound_statement; this matters for certain pragmas.
10723 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10724 is a (possibly labeled) if statement which is not enclosed in braces
10725 and has an else clause. This is used to implement -Wparentheses.
10727 CHAIN is a vector of if-else-if conditions. */
10729 static void
10730 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10731 bool in_compound, bool *if_p, vec<tree> *chain,
10732 location_t *loc_after_labels)
10734 tree statement, std_attrs = NULL_TREE;
10735 cp_token *token;
10736 location_t statement_location, attrs_location;
10738 restart:
10739 if (if_p != NULL)
10740 *if_p = false;
10741 /* There is no statement yet. */
10742 statement = NULL_TREE;
10744 saved_token_sentinel saved_tokens (parser->lexer);
10745 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10746 if (c_dialect_objc ())
10747 /* In obj-c++, seeing '[[' might be the either the beginning of
10748 c++11 attributes, or a nested objc-message-expression. So
10749 let's parse the c++11 attributes tentatively. */
10750 cp_parser_parse_tentatively (parser);
10751 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10752 if (c_dialect_objc ())
10754 if (!cp_parser_parse_definitely (parser))
10755 std_attrs = NULL_TREE;
10758 /* Peek at the next token. */
10759 token = cp_lexer_peek_token (parser->lexer);
10760 /* Remember the location of the first token in the statement. */
10761 statement_location = token->location;
10762 /* If this is a keyword, then that will often determine what kind of
10763 statement we have. */
10764 if (token->type == CPP_KEYWORD)
10766 enum rid keyword = token->keyword;
10768 switch (keyword)
10770 case RID_CASE:
10771 case RID_DEFAULT:
10772 /* Looks like a labeled-statement with a case label.
10773 Parse the label, and then use tail recursion to parse
10774 the statement. */
10775 cp_parser_label_for_labeled_statement (parser, std_attrs);
10776 in_compound = false;
10777 goto restart;
10779 case RID_IF:
10780 case RID_SWITCH:
10781 statement = cp_parser_selection_statement (parser, if_p, chain);
10782 break;
10784 case RID_WHILE:
10785 case RID_DO:
10786 case RID_FOR:
10787 statement = cp_parser_iteration_statement (parser, if_p, false);
10788 break;
10790 case RID_BREAK:
10791 case RID_CONTINUE:
10792 case RID_RETURN:
10793 case RID_GOTO:
10794 statement = cp_parser_jump_statement (parser);
10795 break;
10797 /* Objective-C++ exception-handling constructs. */
10798 case RID_AT_TRY:
10799 case RID_AT_CATCH:
10800 case RID_AT_FINALLY:
10801 case RID_AT_SYNCHRONIZED:
10802 case RID_AT_THROW:
10803 statement = cp_parser_objc_statement (parser);
10804 break;
10806 case RID_TRY:
10807 statement = cp_parser_try_block (parser);
10808 break;
10810 case RID_NAMESPACE:
10811 /* This must be a namespace alias definition. */
10812 cp_parser_declaration_statement (parser);
10813 return;
10815 case RID_TRANSACTION_ATOMIC:
10816 case RID_TRANSACTION_RELAXED:
10817 case RID_SYNCHRONIZED:
10818 case RID_ATOMIC_NOEXCEPT:
10819 case RID_ATOMIC_CANCEL:
10820 statement = cp_parser_transaction (parser, token);
10821 break;
10822 case RID_TRANSACTION_CANCEL:
10823 statement = cp_parser_transaction_cancel (parser);
10824 break;
10826 default:
10827 /* It might be a keyword like `int' that can start a
10828 declaration-statement. */
10829 break;
10832 else if (token->type == CPP_NAME)
10834 /* If the next token is a `:', then we are looking at a
10835 labeled-statement. */
10836 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10837 if (token->type == CPP_COLON)
10839 /* Looks like a labeled-statement with an ordinary label.
10840 Parse the label, and then use tail recursion to parse
10841 the statement. */
10843 cp_parser_label_for_labeled_statement (parser, std_attrs);
10844 in_compound = false;
10845 goto restart;
10848 /* Anything that starts with a `{' must be a compound-statement. */
10849 else if (token->type == CPP_OPEN_BRACE)
10850 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10851 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10852 a statement all its own. */
10853 else if (token->type == CPP_PRAGMA)
10855 /* Only certain OpenMP pragmas are attached to statements, and thus
10856 are considered statements themselves. All others are not. In
10857 the context of a compound, accept the pragma as a "statement" and
10858 return so that we can check for a close brace. Otherwise we
10859 require a real statement and must go back and read one. */
10860 if (in_compound)
10861 cp_parser_pragma (parser, pragma_compound, if_p);
10862 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10863 goto restart;
10864 return;
10866 else if (token->type == CPP_EOF)
10868 cp_parser_error (parser, "expected statement");
10869 return;
10872 /* Everything else must be a declaration-statement or an
10873 expression-statement. Try for the declaration-statement
10874 first, unless we are looking at a `;', in which case we know that
10875 we have an expression-statement. */
10876 if (!statement)
10878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10880 if (std_attrs != NULL_TREE)
10882 /* Attributes should be parsed as part of the the
10883 declaration, so let's un-parse them. */
10884 saved_tokens.rollback();
10885 std_attrs = NULL_TREE;
10888 cp_parser_parse_tentatively (parser);
10889 /* Try to parse the declaration-statement. */
10890 cp_parser_declaration_statement (parser);
10891 /* If that worked, we're done. */
10892 if (cp_parser_parse_definitely (parser))
10893 return;
10895 /* All preceding labels have been parsed at this point. */
10896 if (loc_after_labels != NULL)
10897 *loc_after_labels = statement_location;
10899 /* Look for an expression-statement instead. */
10900 statement = cp_parser_expression_statement (parser, in_statement_expr);
10902 /* Handle [[fallthrough]];. */
10903 if (attribute_fallthrough_p (std_attrs))
10905 /* The next token after the fallthrough attribute is ';'. */
10906 if (statement == NULL_TREE)
10908 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10909 statement = build_call_expr_internal_loc (statement_location,
10910 IFN_FALLTHROUGH,
10911 void_type_node, 0);
10912 finish_expr_stmt (statement);
10914 else
10915 warning_at (statement_location, OPT_Wattributes,
10916 "%<fallthrough%> attribute not followed by %<;%>");
10917 std_attrs = NULL_TREE;
10921 /* Set the line number for the statement. */
10922 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10923 SET_EXPR_LOCATION (statement, statement_location);
10925 /* Allow "[[fallthrough]];", but warn otherwise. */
10926 if (std_attrs != NULL_TREE)
10927 warning_at (attrs_location,
10928 OPT_Wattributes,
10929 "attributes at the beginning of statement are ignored");
10932 /* Parse the label for a labeled-statement, i.e.
10934 identifier :
10935 case constant-expression :
10936 default :
10938 GNU Extension:
10939 case constant-expression ... constant-expression : statement
10941 When a label is parsed without errors, the label is added to the
10942 parse tree by the finish_* functions, so this function doesn't
10943 have to return the label. */
10945 static void
10946 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10948 cp_token *token;
10949 tree label = NULL_TREE;
10950 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10952 /* The next token should be an identifier. */
10953 token = cp_lexer_peek_token (parser->lexer);
10954 if (token->type != CPP_NAME
10955 && token->type != CPP_KEYWORD)
10957 cp_parser_error (parser, "expected labeled-statement");
10958 return;
10961 /* Remember whether this case or a user-defined label is allowed to fall
10962 through to. */
10963 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10965 parser->colon_corrects_to_scope_p = false;
10966 switch (token->keyword)
10968 case RID_CASE:
10970 tree expr, expr_hi;
10971 cp_token *ellipsis;
10973 /* Consume the `case' token. */
10974 cp_lexer_consume_token (parser->lexer);
10975 /* Parse the constant-expression. */
10976 expr = cp_parser_constant_expression (parser);
10977 if (check_for_bare_parameter_packs (expr))
10978 expr = error_mark_node;
10980 ellipsis = cp_lexer_peek_token (parser->lexer);
10981 if (ellipsis->type == CPP_ELLIPSIS)
10983 /* Consume the `...' token. */
10984 cp_lexer_consume_token (parser->lexer);
10985 expr_hi = cp_parser_constant_expression (parser);
10986 if (check_for_bare_parameter_packs (expr_hi))
10987 expr_hi = error_mark_node;
10989 /* We don't need to emit warnings here, as the common code
10990 will do this for us. */
10992 else
10993 expr_hi = NULL_TREE;
10995 if (parser->in_switch_statement_p)
10997 tree l = finish_case_label (token->location, expr, expr_hi);
10998 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10999 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11001 else
11002 error_at (token->location,
11003 "case label %qE not within a switch statement",
11004 expr);
11006 break;
11008 case RID_DEFAULT:
11009 /* Consume the `default' token. */
11010 cp_lexer_consume_token (parser->lexer);
11012 if (parser->in_switch_statement_p)
11014 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11015 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11016 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11018 else
11019 error_at (token->location, "case label not within a switch statement");
11020 break;
11022 default:
11023 /* Anything else must be an ordinary label. */
11024 label = finish_label_stmt (cp_parser_identifier (parser));
11025 if (label && TREE_CODE (label) == LABEL_DECL)
11026 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11027 break;
11030 /* Require the `:' token. */
11031 cp_parser_require (parser, CPP_COLON, RT_COLON);
11033 /* An ordinary label may optionally be followed by attributes.
11034 However, this is only permitted if the attributes are then
11035 followed by a semicolon. This is because, for backward
11036 compatibility, when parsing
11037 lab: __attribute__ ((unused)) int i;
11038 we want the attribute to attach to "i", not "lab". */
11039 if (label != NULL_TREE
11040 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11042 tree attrs;
11043 cp_parser_parse_tentatively (parser);
11044 attrs = cp_parser_gnu_attributes_opt (parser);
11045 if (attrs == NULL_TREE
11046 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11047 cp_parser_abort_tentative_parse (parser);
11048 else if (!cp_parser_parse_definitely (parser))
11050 else
11051 attributes = chainon (attributes, attrs);
11054 if (attributes != NULL_TREE)
11055 cplus_decl_attributes (&label, attributes, 0);
11057 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11060 /* Parse an expression-statement.
11062 expression-statement:
11063 expression [opt] ;
11065 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11066 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11067 indicates whether this expression-statement is part of an
11068 expression statement. */
11070 static tree
11071 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11073 tree statement = NULL_TREE;
11074 cp_token *token = cp_lexer_peek_token (parser->lexer);
11075 location_t loc = token->location;
11077 /* There might be attribute fallthrough. */
11078 tree attr = cp_parser_gnu_attributes_opt (parser);
11080 /* If the next token is a ';', then there is no expression
11081 statement. */
11082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11084 statement = cp_parser_expression (parser);
11085 if (statement == error_mark_node
11086 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11088 cp_parser_skip_to_end_of_block_or_statement (parser);
11089 return error_mark_node;
11093 /* Handle [[fallthrough]];. */
11094 if (attribute_fallthrough_p (attr))
11096 /* The next token after the fallthrough attribute is ';'. */
11097 if (statement == NULL_TREE)
11098 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11099 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11100 void_type_node, 0);
11101 else
11102 warning_at (loc, OPT_Wattributes,
11103 "%<fallthrough%> attribute not followed by %<;%>");
11104 attr = NULL_TREE;
11107 /* Allow "[[fallthrough]];", but warn otherwise. */
11108 if (attr != NULL_TREE)
11109 warning_at (loc, OPT_Wattributes,
11110 "attributes at the beginning of statement are ignored");
11112 /* Give a helpful message for "A<T>::type t;" and the like. */
11113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11114 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11116 if (TREE_CODE (statement) == SCOPE_REF)
11117 error_at (token->location, "need %<typename%> before %qE because "
11118 "%qT is a dependent scope",
11119 statement, TREE_OPERAND (statement, 0));
11120 else if (is_overloaded_fn (statement)
11121 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11123 /* A::A a; */
11124 tree fn = get_first_fn (statement);
11125 error_at (token->location,
11126 "%<%T::%D%> names the constructor, not the type",
11127 DECL_CONTEXT (fn), DECL_NAME (fn));
11131 /* Consume the final `;'. */
11132 cp_parser_consume_semicolon_at_end_of_statement (parser);
11134 if (in_statement_expr
11135 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11136 /* This is the final expression statement of a statement
11137 expression. */
11138 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11139 else if (statement)
11140 statement = finish_expr_stmt (statement);
11142 return statement;
11145 /* Parse a compound-statement.
11147 compound-statement:
11148 { statement-seq [opt] }
11150 GNU extension:
11152 compound-statement:
11153 { label-declaration-seq [opt] statement-seq [opt] }
11155 label-declaration-seq:
11156 label-declaration
11157 label-declaration-seq label-declaration
11159 Returns a tree representing the statement. */
11161 static tree
11162 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11163 int bcs_flags, bool function_body)
11165 tree compound_stmt;
11166 matching_braces braces;
11168 /* Consume the `{'. */
11169 if (!braces.require_open (parser))
11170 return error_mark_node;
11171 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11172 && !function_body && cxx_dialect < cxx14)
11173 pedwarn (input_location, OPT_Wpedantic,
11174 "compound-statement in %<constexpr%> function");
11175 /* Begin the compound-statement. */
11176 compound_stmt = begin_compound_stmt (bcs_flags);
11177 /* If the next keyword is `__label__' we have a label declaration. */
11178 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11179 cp_parser_label_declaration (parser);
11180 /* Parse an (optional) statement-seq. */
11181 cp_parser_statement_seq_opt (parser, in_statement_expr);
11182 /* Finish the compound-statement. */
11183 finish_compound_stmt (compound_stmt);
11184 /* Consume the `}'. */
11185 braces.require_close (parser);
11187 return compound_stmt;
11190 /* Parse an (optional) statement-seq.
11192 statement-seq:
11193 statement
11194 statement-seq [opt] statement */
11196 static void
11197 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11199 /* Scan statements until there aren't any more. */
11200 while (true)
11202 cp_token *token = cp_lexer_peek_token (parser->lexer);
11204 /* If we are looking at a `}', then we have run out of
11205 statements; the same is true if we have reached the end
11206 of file, or have stumbled upon a stray '@end'. */
11207 if (token->type == CPP_CLOSE_BRACE
11208 || token->type == CPP_EOF
11209 || token->type == CPP_PRAGMA_EOL
11210 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11211 break;
11213 /* If we are in a compound statement and find 'else' then
11214 something went wrong. */
11215 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11217 if (parser->in_statement & IN_IF_STMT)
11218 break;
11219 else
11221 token = cp_lexer_consume_token (parser->lexer);
11222 error_at (token->location, "%<else%> without a previous %<if%>");
11226 /* Parse the statement. */
11227 cp_parser_statement (parser, in_statement_expr, true, NULL);
11231 /* Return true if we're looking at (init; cond), false otherwise. */
11233 static bool
11234 cp_parser_init_statement_p (cp_parser *parser)
11236 /* Save tokens so that we can put them back. */
11237 cp_lexer_save_tokens (parser->lexer);
11239 /* Look for ';' that is not nested in () or {}. */
11240 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11241 /*recovering=*/false,
11242 CPP_SEMICOLON,
11243 /*consume_paren=*/false);
11245 /* Roll back the tokens we skipped. */
11246 cp_lexer_rollback_tokens (parser->lexer);
11248 return ret == -1;
11251 /* Parse a selection-statement.
11253 selection-statement:
11254 if ( init-statement [opt] condition ) statement
11255 if ( init-statement [opt] condition ) statement else statement
11256 switch ( init-statement [opt] condition ) statement
11258 Returns the new IF_STMT or SWITCH_STMT.
11260 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11261 is a (possibly labeled) if statement which is not enclosed in
11262 braces and has an else clause. This is used to implement
11263 -Wparentheses.
11265 CHAIN is a vector of if-else-if conditions. This is used to implement
11266 -Wduplicated-cond. */
11268 static tree
11269 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11270 vec<tree> *chain)
11272 cp_token *token;
11273 enum rid keyword;
11274 token_indent_info guard_tinfo;
11276 if (if_p != NULL)
11277 *if_p = false;
11279 /* Peek at the next token. */
11280 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11281 guard_tinfo = get_token_indent_info (token);
11283 /* See what kind of keyword it is. */
11284 keyword = token->keyword;
11285 switch (keyword)
11287 case RID_IF:
11288 case RID_SWITCH:
11290 tree statement;
11291 tree condition;
11293 bool cx = false;
11294 if (keyword == RID_IF
11295 && cp_lexer_next_token_is_keyword (parser->lexer,
11296 RID_CONSTEXPR))
11298 cx = true;
11299 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11300 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11301 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11302 "with -std=c++17 or -std=gnu++17");
11305 /* Look for the `('. */
11306 matching_parens parens;
11307 if (!parens.require_open (parser))
11309 cp_parser_skip_to_end_of_statement (parser);
11310 return error_mark_node;
11313 /* Begin the selection-statement. */
11314 if (keyword == RID_IF)
11316 statement = begin_if_stmt ();
11317 IF_STMT_CONSTEXPR_P (statement) = cx;
11319 else
11320 statement = begin_switch_stmt ();
11322 /* Parse the optional init-statement. */
11323 if (cp_parser_init_statement_p (parser))
11325 tree decl;
11326 if (cxx_dialect < cxx17)
11327 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11328 "init-statement in selection statements only available "
11329 "with -std=c++17 or -std=gnu++17");
11330 cp_parser_init_statement (parser, &decl);
11333 /* Parse the condition. */
11334 condition = cp_parser_condition (parser);
11335 /* Look for the `)'. */
11336 if (!parens.require_close (parser))
11337 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11338 /*consume_paren=*/true);
11340 if (keyword == RID_IF)
11342 bool nested_if;
11343 unsigned char in_statement;
11345 /* Add the condition. */
11346 condition = finish_if_stmt_cond (condition, statement);
11348 if (warn_duplicated_cond)
11349 warn_duplicated_cond_add_or_warn (token->location, condition,
11350 &chain);
11352 /* Parse the then-clause. */
11353 in_statement = parser->in_statement;
11354 parser->in_statement |= IN_IF_STMT;
11356 /* Outside a template, the non-selected branch of a constexpr
11357 if is a 'discarded statement', i.e. unevaluated. */
11358 bool was_discarded = in_discarded_stmt;
11359 bool discard_then = (cx && !processing_template_decl
11360 && integer_zerop (condition));
11361 if (discard_then)
11363 in_discarded_stmt = true;
11364 ++c_inhibit_evaluation_warnings;
11367 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11368 guard_tinfo);
11370 parser->in_statement = in_statement;
11372 finish_then_clause (statement);
11374 if (discard_then)
11376 THEN_CLAUSE (statement) = NULL_TREE;
11377 in_discarded_stmt = was_discarded;
11378 --c_inhibit_evaluation_warnings;
11381 /* If the next token is `else', parse the else-clause. */
11382 if (cp_lexer_next_token_is_keyword (parser->lexer,
11383 RID_ELSE))
11385 bool discard_else = (cx && !processing_template_decl
11386 && integer_nonzerop (condition));
11387 if (discard_else)
11389 in_discarded_stmt = true;
11390 ++c_inhibit_evaluation_warnings;
11393 guard_tinfo
11394 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11395 /* Consume the `else' keyword. */
11396 cp_lexer_consume_token (parser->lexer);
11397 if (warn_duplicated_cond)
11399 if (cp_lexer_next_token_is_keyword (parser->lexer,
11400 RID_IF)
11401 && chain == NULL)
11403 /* We've got "if (COND) else if (COND2)". Start
11404 the condition chain and add COND as the first
11405 element. */
11406 chain = new vec<tree> ();
11407 if (!CONSTANT_CLASS_P (condition)
11408 && !TREE_SIDE_EFFECTS (condition))
11410 /* Wrap it in a NOP_EXPR so that we can set the
11411 location of the condition. */
11412 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11413 condition);
11414 SET_EXPR_LOCATION (e, token->location);
11415 chain->safe_push (e);
11418 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11419 RID_IF))
11421 /* This is if-else without subsequent if. Zap the
11422 condition chain; we would have already warned at
11423 this point. */
11424 delete chain;
11425 chain = NULL;
11428 begin_else_clause (statement);
11429 /* Parse the else-clause. */
11430 cp_parser_implicitly_scoped_statement (parser, NULL,
11431 guard_tinfo, chain);
11433 finish_else_clause (statement);
11435 /* If we are currently parsing a then-clause, then
11436 IF_P will not be NULL. We set it to true to
11437 indicate that this if statement has an else clause.
11438 This may trigger the Wparentheses warning below
11439 when we get back up to the parent if statement. */
11440 if (if_p != NULL)
11441 *if_p = true;
11443 if (discard_else)
11445 ELSE_CLAUSE (statement) = NULL_TREE;
11446 in_discarded_stmt = was_discarded;
11447 --c_inhibit_evaluation_warnings;
11450 else
11452 /* This if statement does not have an else clause. If
11453 NESTED_IF is true, then the then-clause has an if
11454 statement which does have an else clause. We warn
11455 about the potential ambiguity. */
11456 if (nested_if)
11457 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11458 "suggest explicit braces to avoid ambiguous"
11459 " %<else%>");
11460 if (warn_duplicated_cond)
11462 /* We don't need the condition chain anymore. */
11463 delete chain;
11464 chain = NULL;
11468 /* Now we're all done with the if-statement. */
11469 finish_if_stmt (statement);
11471 else
11473 bool in_switch_statement_p;
11474 unsigned char in_statement;
11476 /* Add the condition. */
11477 finish_switch_cond (condition, statement);
11479 /* Parse the body of the switch-statement. */
11480 in_switch_statement_p = parser->in_switch_statement_p;
11481 in_statement = parser->in_statement;
11482 parser->in_switch_statement_p = true;
11483 parser->in_statement |= IN_SWITCH_STMT;
11484 cp_parser_implicitly_scoped_statement (parser, if_p,
11485 guard_tinfo);
11486 parser->in_switch_statement_p = in_switch_statement_p;
11487 parser->in_statement = in_statement;
11489 /* Now we're all done with the switch-statement. */
11490 finish_switch_stmt (statement);
11493 return statement;
11495 break;
11497 default:
11498 cp_parser_error (parser, "expected selection-statement");
11499 return error_mark_node;
11503 /* Parse a condition.
11505 condition:
11506 expression
11507 type-specifier-seq declarator = initializer-clause
11508 type-specifier-seq declarator braced-init-list
11510 GNU Extension:
11512 condition:
11513 type-specifier-seq declarator asm-specification [opt]
11514 attributes [opt] = assignment-expression
11516 Returns the expression that should be tested. */
11518 static tree
11519 cp_parser_condition (cp_parser* parser)
11521 cp_decl_specifier_seq type_specifiers;
11522 const char *saved_message;
11523 int declares_class_or_enum;
11525 /* Try the declaration first. */
11526 cp_parser_parse_tentatively (parser);
11527 /* New types are not allowed in the type-specifier-seq for a
11528 condition. */
11529 saved_message = parser->type_definition_forbidden_message;
11530 parser->type_definition_forbidden_message
11531 = G_("types may not be defined in conditions");
11532 /* Parse the type-specifier-seq. */
11533 cp_parser_decl_specifier_seq (parser,
11534 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11535 &type_specifiers,
11536 &declares_class_or_enum);
11537 /* Restore the saved message. */
11538 parser->type_definition_forbidden_message = saved_message;
11539 /* If all is well, we might be looking at a declaration. */
11540 if (!cp_parser_error_occurred (parser))
11542 tree decl;
11543 tree asm_specification;
11544 tree attributes;
11545 cp_declarator *declarator;
11546 tree initializer = NULL_TREE;
11548 /* Parse the declarator. */
11549 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11550 /*ctor_dtor_or_conv_p=*/NULL,
11551 /*parenthesized_p=*/NULL,
11552 /*member_p=*/false,
11553 /*friend_p=*/false);
11554 /* Parse the attributes. */
11555 attributes = cp_parser_attributes_opt (parser);
11556 /* Parse the asm-specification. */
11557 asm_specification = cp_parser_asm_specification_opt (parser);
11558 /* If the next token is not an `=' or '{', then we might still be
11559 looking at an expression. For example:
11561 if (A(a).x)
11563 looks like a decl-specifier-seq and a declarator -- but then
11564 there is no `=', so this is an expression. */
11565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11566 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11567 cp_parser_simulate_error (parser);
11569 /* If we did see an `=' or '{', then we are looking at a declaration
11570 for sure. */
11571 if (cp_parser_parse_definitely (parser))
11573 tree pushed_scope;
11574 bool non_constant_p;
11575 int flags = LOOKUP_ONLYCONVERTING;
11577 /* Create the declaration. */
11578 decl = start_decl (declarator, &type_specifiers,
11579 /*initialized_p=*/true,
11580 attributes, /*prefix_attributes=*/NULL_TREE,
11581 &pushed_scope);
11583 /* Parse the initializer. */
11584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11586 initializer = cp_parser_braced_list (parser, &non_constant_p);
11587 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11588 flags = 0;
11590 else
11592 /* Consume the `='. */
11593 cp_parser_require (parser, CPP_EQ, RT_EQ);
11594 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11596 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11599 /* Process the initializer. */
11600 cp_finish_decl (decl,
11601 initializer, !non_constant_p,
11602 asm_specification,
11603 flags);
11605 if (pushed_scope)
11606 pop_scope (pushed_scope);
11608 return convert_from_reference (decl);
11611 /* If we didn't even get past the declarator successfully, we are
11612 definitely not looking at a declaration. */
11613 else
11614 cp_parser_abort_tentative_parse (parser);
11616 /* Otherwise, we are looking at an expression. */
11617 return cp_parser_expression (parser);
11620 /* Parses a for-statement or range-for-statement until the closing ')',
11621 not included. */
11623 static tree
11624 cp_parser_for (cp_parser *parser, bool ivdep)
11626 tree init, scope, decl;
11627 bool is_range_for;
11629 /* Begin the for-statement. */
11630 scope = begin_for_scope (&init);
11632 /* Parse the initialization. */
11633 is_range_for = cp_parser_init_statement (parser, &decl);
11635 if (is_range_for)
11636 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11637 else
11638 return cp_parser_c_for (parser, scope, init, ivdep);
11641 static tree
11642 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11644 /* Normal for loop */
11645 tree condition = NULL_TREE;
11646 tree expression = NULL_TREE;
11647 tree stmt;
11649 stmt = begin_for_stmt (scope, init);
11650 /* The init-statement has already been parsed in
11651 cp_parser_init_statement, so no work is needed here. */
11652 finish_init_stmt (stmt);
11654 /* If there's a condition, process it. */
11655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11656 condition = cp_parser_condition (parser);
11657 else if (ivdep)
11659 cp_parser_error (parser, "missing loop condition in loop with "
11660 "%<GCC ivdep%> pragma");
11661 condition = error_mark_node;
11663 finish_for_cond (condition, stmt, ivdep);
11664 /* Look for the `;'. */
11665 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11667 /* If there's an expression, process it. */
11668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11669 expression = cp_parser_expression (parser);
11670 finish_for_expr (expression, stmt);
11672 return stmt;
11675 /* Tries to parse a range-based for-statement:
11677 range-based-for:
11678 decl-specifier-seq declarator : expression
11680 The decl-specifier-seq declarator and the `:' are already parsed by
11681 cp_parser_init_statement. If processing_template_decl it returns a
11682 newly created RANGE_FOR_STMT; if not, it is converted to a
11683 regular FOR_STMT. */
11685 static tree
11686 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11687 bool ivdep)
11689 tree stmt, range_expr;
11690 auto_vec <cxx_binding *, 16> bindings;
11691 auto_vec <tree, 16> names;
11692 tree decomp_first_name = NULL_TREE;
11693 unsigned int decomp_cnt = 0;
11695 /* Get the range declaration momentarily out of the way so that
11696 the range expression doesn't clash with it. */
11697 if (range_decl != error_mark_node)
11699 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11701 tree v = DECL_VALUE_EXPR (range_decl);
11702 /* For decomposition declaration get all of the corresponding
11703 declarations out of the way. */
11704 if (TREE_CODE (v) == ARRAY_REF
11705 && VAR_P (TREE_OPERAND (v, 0))
11706 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11708 tree d = range_decl;
11709 range_decl = TREE_OPERAND (v, 0);
11710 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11711 decomp_first_name = d;
11712 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11714 tree name = DECL_NAME (d);
11715 names.safe_push (name);
11716 bindings.safe_push (IDENTIFIER_BINDING (name));
11717 IDENTIFIER_BINDING (name)
11718 = IDENTIFIER_BINDING (name)->previous;
11722 if (names.is_empty ())
11724 tree name = DECL_NAME (range_decl);
11725 names.safe_push (name);
11726 bindings.safe_push (IDENTIFIER_BINDING (name));
11727 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11733 bool expr_non_constant_p;
11734 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11736 else
11737 range_expr = cp_parser_expression (parser);
11739 /* Put the range declaration(s) back into scope. */
11740 for (unsigned int i = 0; i < names.length (); i++)
11742 cxx_binding *binding = bindings[i];
11743 binding->previous = IDENTIFIER_BINDING (names[i]);
11744 IDENTIFIER_BINDING (names[i]) = binding;
11747 /* If in template, STMT is converted to a normal for-statement
11748 at instantiation. If not, it is done just ahead. */
11749 if (processing_template_decl)
11751 if (check_for_bare_parameter_packs (range_expr))
11752 range_expr = error_mark_node;
11753 stmt = begin_range_for_stmt (scope, init);
11754 if (ivdep)
11755 RANGE_FOR_IVDEP (stmt) = 1;
11756 finish_range_for_decl (stmt, range_decl, range_expr);
11757 if (!type_dependent_expression_p (range_expr)
11758 /* do_auto_deduction doesn't mess with template init-lists. */
11759 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11760 do_range_for_auto_deduction (range_decl, range_expr);
11762 else
11764 stmt = begin_for_stmt (scope, init);
11765 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11766 decomp_first_name, decomp_cnt, ivdep);
11768 return stmt;
11771 /* Subroutine of cp_convert_range_for: given the initializer expression,
11772 builds up the range temporary. */
11774 static tree
11775 build_range_temp (tree range_expr)
11777 tree range_type, range_temp;
11779 /* Find out the type deduced by the declaration
11780 `auto &&__range = range_expr'. */
11781 range_type = cp_build_reference_type (make_auto (), true);
11782 range_type = do_auto_deduction (range_type, range_expr,
11783 type_uses_auto (range_type));
11785 /* Create the __range variable. */
11786 range_temp = build_decl (input_location, VAR_DECL,
11787 get_identifier ("__for_range"), range_type);
11788 TREE_USED (range_temp) = 1;
11789 DECL_ARTIFICIAL (range_temp) = 1;
11791 return range_temp;
11794 /* Used by cp_parser_range_for in template context: we aren't going to
11795 do a full conversion yet, but we still need to resolve auto in the
11796 type of the for-range-declaration if present. This is basically
11797 a shortcut version of cp_convert_range_for. */
11799 static void
11800 do_range_for_auto_deduction (tree decl, tree range_expr)
11802 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11803 if (auto_node)
11805 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11806 range_temp = convert_from_reference (build_range_temp (range_expr));
11807 iter_type = (cp_parser_perform_range_for_lookup
11808 (range_temp, &begin_dummy, &end_dummy));
11809 if (iter_type)
11811 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11812 iter_type);
11813 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11814 RO_UNARY_STAR,
11815 tf_warning_or_error);
11816 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11817 iter_decl, auto_node);
11822 /* Converts a range-based for-statement into a normal
11823 for-statement, as per the definition.
11825 for (RANGE_DECL : RANGE_EXPR)
11826 BLOCK
11828 should be equivalent to:
11831 auto &&__range = RANGE_EXPR;
11832 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11833 __begin != __end;
11834 ++__begin)
11836 RANGE_DECL = *__begin;
11837 BLOCK
11841 If RANGE_EXPR is an array:
11842 BEGIN_EXPR = __range
11843 END_EXPR = __range + ARRAY_SIZE(__range)
11844 Else if RANGE_EXPR has a member 'begin' or 'end':
11845 BEGIN_EXPR = __range.begin()
11846 END_EXPR = __range.end()
11847 Else:
11848 BEGIN_EXPR = begin(__range)
11849 END_EXPR = end(__range);
11851 If __range has a member 'begin' but not 'end', or vice versa, we must
11852 still use the second alternative (it will surely fail, however).
11853 When calling begin()/end() in the third alternative we must use
11854 argument dependent lookup, but always considering 'std' as an associated
11855 namespace. */
11857 tree
11858 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11859 tree decomp_first_name, unsigned int decomp_cnt,
11860 bool ivdep)
11862 tree begin, end;
11863 tree iter_type, begin_expr, end_expr;
11864 tree condition, expression;
11866 range_expr = mark_lvalue_use (range_expr);
11868 if (range_decl == error_mark_node || range_expr == error_mark_node)
11869 /* If an error happened previously do nothing or else a lot of
11870 unhelpful errors would be issued. */
11871 begin_expr = end_expr = iter_type = error_mark_node;
11872 else
11874 tree range_temp;
11876 if (VAR_P (range_expr)
11877 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11878 /* Can't bind a reference to an array of runtime bound. */
11879 range_temp = range_expr;
11880 else
11882 range_temp = build_range_temp (range_expr);
11883 pushdecl (range_temp);
11884 cp_finish_decl (range_temp, range_expr,
11885 /*is_constant_init*/false, NULL_TREE,
11886 LOOKUP_ONLYCONVERTING);
11887 range_temp = convert_from_reference (range_temp);
11889 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11890 &begin_expr, &end_expr);
11893 /* The new for initialization statement. */
11894 begin = build_decl (input_location, VAR_DECL,
11895 get_identifier ("__for_begin"), iter_type);
11896 TREE_USED (begin) = 1;
11897 DECL_ARTIFICIAL (begin) = 1;
11898 pushdecl (begin);
11899 cp_finish_decl (begin, begin_expr,
11900 /*is_constant_init*/false, NULL_TREE,
11901 LOOKUP_ONLYCONVERTING);
11903 if (cxx_dialect >= cxx17)
11904 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11905 end = build_decl (input_location, VAR_DECL,
11906 get_identifier ("__for_end"), iter_type);
11907 TREE_USED (end) = 1;
11908 DECL_ARTIFICIAL (end) = 1;
11909 pushdecl (end);
11910 cp_finish_decl (end, end_expr,
11911 /*is_constant_init*/false, NULL_TREE,
11912 LOOKUP_ONLYCONVERTING);
11914 finish_init_stmt (statement);
11916 /* The new for condition. */
11917 condition = build_x_binary_op (input_location, NE_EXPR,
11918 begin, ERROR_MARK,
11919 end, ERROR_MARK,
11920 NULL, tf_warning_or_error);
11921 finish_for_cond (condition, statement, ivdep);
11923 /* The new increment expression. */
11924 expression = finish_unary_op_expr (input_location,
11925 PREINCREMENT_EXPR, begin,
11926 tf_warning_or_error);
11927 finish_for_expr (expression, statement);
11929 /* The declaration is initialized with *__begin inside the loop body. */
11930 cp_finish_decl (range_decl,
11931 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11932 tf_warning_or_error),
11933 /*is_constant_init*/false, NULL_TREE,
11934 LOOKUP_ONLYCONVERTING);
11935 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11936 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11938 return statement;
11941 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11942 We need to solve both at the same time because the method used
11943 depends on the existence of members begin or end.
11944 Returns the type deduced for the iterator expression. */
11946 static tree
11947 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11949 if (error_operand_p (range))
11951 *begin = *end = error_mark_node;
11952 return error_mark_node;
11955 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11957 error ("range-based %<for%> expression of type %qT "
11958 "has incomplete type", TREE_TYPE (range));
11959 *begin = *end = error_mark_node;
11960 return error_mark_node;
11962 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11964 /* If RANGE is an array, we will use pointer arithmetic. */
11965 *begin = decay_conversion (range, tf_warning_or_error);
11966 *end = build_binary_op (input_location, PLUS_EXPR,
11967 range,
11968 array_type_nelts_top (TREE_TYPE (range)),
11969 false);
11970 return TREE_TYPE (*begin);
11972 else
11974 /* If it is not an array, we must do a bit of magic. */
11975 tree id_begin, id_end;
11976 tree member_begin, member_end;
11978 *begin = *end = error_mark_node;
11980 id_begin = get_identifier ("begin");
11981 id_end = get_identifier ("end");
11982 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11983 /*protect=*/2, /*want_type=*/false,
11984 tf_warning_or_error);
11985 member_end = lookup_member (TREE_TYPE (range), id_end,
11986 /*protect=*/2, /*want_type=*/false,
11987 tf_warning_or_error);
11989 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11991 /* Use the member functions. */
11992 if (member_begin != NULL_TREE)
11993 *begin = cp_parser_range_for_member_function (range, id_begin);
11994 else
11995 error ("range-based %<for%> expression of type %qT has an "
11996 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11998 if (member_end != NULL_TREE)
11999 *end = cp_parser_range_for_member_function (range, id_end);
12000 else
12001 error ("range-based %<for%> expression of type %qT has a "
12002 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12004 else
12006 /* Use global functions with ADL. */
12007 vec<tree, va_gc> *vec;
12008 vec = make_tree_vector ();
12010 vec_safe_push (vec, range);
12012 member_begin = perform_koenig_lookup (id_begin, vec,
12013 tf_warning_or_error);
12014 *begin = finish_call_expr (member_begin, &vec, false, true,
12015 tf_warning_or_error);
12016 member_end = perform_koenig_lookup (id_end, vec,
12017 tf_warning_or_error);
12018 *end = finish_call_expr (member_end, &vec, false, true,
12019 tf_warning_or_error);
12021 release_tree_vector (vec);
12024 /* Last common checks. */
12025 if (*begin == error_mark_node || *end == error_mark_node)
12027 /* If one of the expressions is an error do no more checks. */
12028 *begin = *end = error_mark_node;
12029 return error_mark_node;
12031 else if (type_dependent_expression_p (*begin)
12032 || type_dependent_expression_p (*end))
12033 /* Can happen, when, eg, in a template context, Koenig lookup
12034 can't resolve begin/end (c++/58503). */
12035 return NULL_TREE;
12036 else
12038 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12039 /* The unqualified type of the __begin and __end temporaries should
12040 be the same, as required by the multiple auto declaration. */
12041 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12043 if (cxx_dialect >= cxx17
12044 && (build_x_binary_op (input_location, NE_EXPR,
12045 *begin, ERROR_MARK,
12046 *end, ERROR_MARK,
12047 NULL, tf_none)
12048 != error_mark_node))
12049 /* P0184R0 allows __begin and __end to have different types,
12050 but make sure they are comparable so we can give a better
12051 diagnostic. */;
12052 else
12053 error ("inconsistent begin/end types in range-based %<for%> "
12054 "statement: %qT and %qT",
12055 TREE_TYPE (*begin), TREE_TYPE (*end));
12057 return iter_type;
12062 /* Helper function for cp_parser_perform_range_for_lookup.
12063 Builds a tree for RANGE.IDENTIFIER(). */
12065 static tree
12066 cp_parser_range_for_member_function (tree range, tree identifier)
12068 tree member, res;
12069 vec<tree, va_gc> *vec;
12071 member = finish_class_member_access_expr (range, identifier,
12072 false, tf_warning_or_error);
12073 if (member == error_mark_node)
12074 return error_mark_node;
12076 vec = make_tree_vector ();
12077 res = finish_call_expr (member, &vec,
12078 /*disallow_virtual=*/false,
12079 /*koenig_p=*/false,
12080 tf_warning_or_error);
12081 release_tree_vector (vec);
12082 return res;
12085 /* Parse an iteration-statement.
12087 iteration-statement:
12088 while ( condition ) statement
12089 do statement while ( expression ) ;
12090 for ( init-statement condition [opt] ; expression [opt] )
12091 statement
12093 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12095 static tree
12096 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12098 cp_token *token;
12099 enum rid keyword;
12100 tree statement;
12101 unsigned char in_statement;
12102 token_indent_info guard_tinfo;
12104 /* Peek at the next token. */
12105 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12106 if (!token)
12107 return error_mark_node;
12109 guard_tinfo = get_token_indent_info (token);
12111 /* Remember whether or not we are already within an iteration
12112 statement. */
12113 in_statement = parser->in_statement;
12115 /* See what kind of keyword it is. */
12116 keyword = token->keyword;
12117 switch (keyword)
12119 case RID_WHILE:
12121 tree condition;
12123 /* Begin the while-statement. */
12124 statement = begin_while_stmt ();
12125 /* Look for the `('. */
12126 matching_parens parens;
12127 parens.require_open (parser);
12128 /* Parse the condition. */
12129 condition = cp_parser_condition (parser);
12130 finish_while_stmt_cond (condition, statement, ivdep);
12131 /* Look for the `)'. */
12132 parens.require_close (parser);
12133 /* Parse the dependent statement. */
12134 parser->in_statement = IN_ITERATION_STMT;
12135 bool prev = note_iteration_stmt_body_start ();
12136 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12137 note_iteration_stmt_body_end (prev);
12138 parser->in_statement = in_statement;
12139 /* We're done with the while-statement. */
12140 finish_while_stmt (statement);
12142 break;
12144 case RID_DO:
12146 tree expression;
12148 /* Begin the do-statement. */
12149 statement = begin_do_stmt ();
12150 /* Parse the body of the do-statement. */
12151 parser->in_statement = IN_ITERATION_STMT;
12152 bool prev = note_iteration_stmt_body_start ();
12153 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12154 note_iteration_stmt_body_end (prev);
12155 parser->in_statement = in_statement;
12156 finish_do_body (statement);
12157 /* Look for the `while' keyword. */
12158 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12159 /* Look for the `('. */
12160 matching_parens parens;
12161 parens.require_open (parser);
12162 /* Parse the expression. */
12163 expression = cp_parser_expression (parser);
12164 /* We're done with the do-statement. */
12165 finish_do_stmt (expression, statement, ivdep);
12166 /* Look for the `)'. */
12167 parens.require_close (parser);
12168 /* Look for the `;'. */
12169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12171 break;
12173 case RID_FOR:
12175 /* Look for the `('. */
12176 matching_parens parens;
12177 parens.require_open (parser);
12179 statement = cp_parser_for (parser, ivdep);
12181 /* Look for the `)'. */
12182 parens.require_close (parser);
12184 /* Parse the body of the for-statement. */
12185 parser->in_statement = IN_ITERATION_STMT;
12186 bool prev = note_iteration_stmt_body_start ();
12187 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12188 note_iteration_stmt_body_end (prev);
12189 parser->in_statement = in_statement;
12191 /* We're done with the for-statement. */
12192 finish_for_stmt (statement);
12194 break;
12196 default:
12197 cp_parser_error (parser, "expected iteration-statement");
12198 statement = error_mark_node;
12199 break;
12202 return statement;
12205 /* Parse a init-statement or the declarator of a range-based-for.
12206 Returns true if a range-based-for declaration is seen.
12208 init-statement:
12209 expression-statement
12210 simple-declaration */
12212 static bool
12213 cp_parser_init_statement (cp_parser* parser, tree *decl)
12215 /* If the next token is a `;', then we have an empty
12216 expression-statement. Grammatically, this is also a
12217 simple-declaration, but an invalid one, because it does not
12218 declare anything. Therefore, if we did not handle this case
12219 specially, we would issue an error message about an invalid
12220 declaration. */
12221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12223 bool is_range_for = false;
12224 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12226 /* A colon is used in range-based for. */
12227 parser->colon_corrects_to_scope_p = false;
12229 /* We're going to speculatively look for a declaration, falling back
12230 to an expression, if necessary. */
12231 cp_parser_parse_tentatively (parser);
12232 /* Parse the declaration. */
12233 cp_parser_simple_declaration (parser,
12234 /*function_definition_allowed_p=*/false,
12235 decl);
12236 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12237 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12239 /* It is a range-for, consume the ':' */
12240 cp_lexer_consume_token (parser->lexer);
12241 is_range_for = true;
12242 if (cxx_dialect < cxx11)
12244 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12245 "range-based %<for%> loops only available with "
12246 "-std=c++11 or -std=gnu++11");
12247 *decl = error_mark_node;
12250 else
12251 /* The ';' is not consumed yet because we told
12252 cp_parser_simple_declaration not to. */
12253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12255 if (cp_parser_parse_definitely (parser))
12256 return is_range_for;
12257 /* If the tentative parse failed, then we shall need to look for an
12258 expression-statement. */
12260 /* If we are here, it is an expression-statement. */
12261 cp_parser_expression_statement (parser, NULL_TREE);
12262 return false;
12265 /* Parse a jump-statement.
12267 jump-statement:
12268 break ;
12269 continue ;
12270 return expression [opt] ;
12271 return braced-init-list ;
12272 goto identifier ;
12274 GNU extension:
12276 jump-statement:
12277 goto * expression ;
12279 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12281 static tree
12282 cp_parser_jump_statement (cp_parser* parser)
12284 tree statement = error_mark_node;
12285 cp_token *token;
12286 enum rid keyword;
12287 unsigned char in_statement;
12289 /* Peek at the next token. */
12290 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12291 if (!token)
12292 return error_mark_node;
12294 /* See what kind of keyword it is. */
12295 keyword = token->keyword;
12296 switch (keyword)
12298 case RID_BREAK:
12299 in_statement = parser->in_statement & ~IN_IF_STMT;
12300 switch (in_statement)
12302 case 0:
12303 error_at (token->location, "break statement not within loop or switch");
12304 break;
12305 default:
12306 gcc_assert ((in_statement & IN_SWITCH_STMT)
12307 || in_statement == IN_ITERATION_STMT);
12308 statement = finish_break_stmt ();
12309 if (in_statement == IN_ITERATION_STMT)
12310 break_maybe_infinite_loop ();
12311 break;
12312 case IN_OMP_BLOCK:
12313 error_at (token->location, "invalid exit from OpenMP structured block");
12314 break;
12315 case IN_OMP_FOR:
12316 error_at (token->location, "break statement used with OpenMP for loop");
12317 break;
12319 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12320 break;
12322 case RID_CONTINUE:
12323 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12325 case 0:
12326 error_at (token->location, "continue statement not within a loop");
12327 break;
12328 /* Fall through. */
12329 case IN_ITERATION_STMT:
12330 case IN_OMP_FOR:
12331 statement = finish_continue_stmt ();
12332 break;
12333 case IN_OMP_BLOCK:
12334 error_at (token->location, "invalid exit from OpenMP structured block");
12335 break;
12336 default:
12337 gcc_unreachable ();
12339 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12340 break;
12342 case RID_RETURN:
12344 tree expr;
12345 bool expr_non_constant_p;
12347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12349 cp_lexer_set_source_position (parser->lexer);
12350 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12351 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12353 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12354 expr = cp_parser_expression (parser);
12355 else
12356 /* If the next token is a `;', then there is no
12357 expression. */
12358 expr = NULL_TREE;
12359 /* Build the return-statement. */
12360 if (current_function_auto_return_pattern && in_discarded_stmt)
12361 /* Don't deduce from a discarded return statement. */;
12362 else
12363 statement = finish_return_stmt (expr);
12364 /* Look for the final `;'. */
12365 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12367 break;
12369 case RID_GOTO:
12370 if (parser->in_function_body
12371 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12373 error ("%<goto%> in %<constexpr%> function");
12374 cp_function_chain->invalid_constexpr = true;
12377 /* Create the goto-statement. */
12378 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12380 /* Issue a warning about this use of a GNU extension. */
12381 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12382 /* Consume the '*' token. */
12383 cp_lexer_consume_token (parser->lexer);
12384 /* Parse the dependent expression. */
12385 finish_goto_stmt (cp_parser_expression (parser));
12387 else
12388 finish_goto_stmt (cp_parser_identifier (parser));
12389 /* Look for the final `;'. */
12390 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12391 break;
12393 default:
12394 cp_parser_error (parser, "expected jump-statement");
12395 break;
12398 return statement;
12401 /* Parse a declaration-statement.
12403 declaration-statement:
12404 block-declaration */
12406 static void
12407 cp_parser_declaration_statement (cp_parser* parser)
12409 void *p;
12411 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12412 p = obstack_alloc (&declarator_obstack, 0);
12414 /* Parse the block-declaration. */
12415 cp_parser_block_declaration (parser, /*statement_p=*/true);
12417 /* Free any declarators allocated. */
12418 obstack_free (&declarator_obstack, p);
12421 /* Some dependent statements (like `if (cond) statement'), are
12422 implicitly in their own scope. In other words, if the statement is
12423 a single statement (as opposed to a compound-statement), it is
12424 none-the-less treated as if it were enclosed in braces. Any
12425 declarations appearing in the dependent statement are out of scope
12426 after control passes that point. This function parses a statement,
12427 but ensures that is in its own scope, even if it is not a
12428 compound-statement.
12430 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12431 is a (possibly labeled) if statement which is not enclosed in
12432 braces and has an else clause. This is used to implement
12433 -Wparentheses.
12435 CHAIN is a vector of if-else-if conditions. This is used to implement
12436 -Wduplicated-cond.
12438 Returns the new statement. */
12440 static tree
12441 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12442 const token_indent_info &guard_tinfo,
12443 vec<tree> *chain)
12445 tree statement;
12446 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12447 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12448 token_indent_info body_tinfo
12449 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12451 if (if_p != NULL)
12452 *if_p = false;
12454 /* Mark if () ; with a special NOP_EXPR. */
12455 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12457 cp_lexer_consume_token (parser->lexer);
12458 statement = add_stmt (build_empty_stmt (body_loc));
12460 if (guard_tinfo.keyword == RID_IF
12461 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12462 warning_at (body_loc, OPT_Wempty_body,
12463 "suggest braces around empty body in an %<if%> statement");
12464 else if (guard_tinfo.keyword == RID_ELSE)
12465 warning_at (body_loc, OPT_Wempty_body,
12466 "suggest braces around empty body in an %<else%> statement");
12468 /* if a compound is opened, we simply parse the statement directly. */
12469 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12470 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12471 /* If the token is not a `{', then we must take special action. */
12472 else
12474 /* Create a compound-statement. */
12475 statement = begin_compound_stmt (0);
12476 /* Parse the dependent-statement. */
12477 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12478 &body_loc_after_labels);
12479 /* Finish the dummy compound-statement. */
12480 finish_compound_stmt (statement);
12483 token_indent_info next_tinfo
12484 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12485 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12487 if (body_loc_after_labels != UNKNOWN_LOCATION
12488 && next_tinfo.type != CPP_SEMICOLON)
12489 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12490 guard_tinfo.location, guard_tinfo.keyword);
12492 /* Return the statement. */
12493 return statement;
12496 /* For some dependent statements (like `while (cond) statement'), we
12497 have already created a scope. Therefore, even if the dependent
12498 statement is a compound-statement, we do not want to create another
12499 scope. */
12501 static void
12502 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12503 const token_indent_info &guard_tinfo)
12505 /* If the token is a `{', then we must take special action. */
12506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12508 token_indent_info body_tinfo
12509 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12510 location_t loc_after_labels = UNKNOWN_LOCATION;
12512 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12513 &loc_after_labels);
12514 token_indent_info next_tinfo
12515 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12516 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12518 if (loc_after_labels != UNKNOWN_LOCATION
12519 && next_tinfo.type != CPP_SEMICOLON)
12520 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12521 guard_tinfo.location,
12522 guard_tinfo.keyword);
12524 else
12526 /* Avoid calling cp_parser_compound_statement, so that we
12527 don't create a new scope. Do everything else by hand. */
12528 matching_braces braces;
12529 braces.require_open (parser);
12530 /* If the next keyword is `__label__' we have a label declaration. */
12531 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12532 cp_parser_label_declaration (parser);
12533 /* Parse an (optional) statement-seq. */
12534 cp_parser_statement_seq_opt (parser, NULL_TREE);
12535 braces.require_close (parser);
12539 /* Declarations [gram.dcl.dcl] */
12541 /* Parse an optional declaration-sequence.
12543 declaration-seq:
12544 declaration
12545 declaration-seq declaration */
12547 static void
12548 cp_parser_declaration_seq_opt (cp_parser* parser)
12550 while (true)
12552 cp_token *token;
12554 token = cp_lexer_peek_token (parser->lexer);
12556 if (token->type == CPP_CLOSE_BRACE
12557 || token->type == CPP_EOF
12558 || token->type == CPP_PRAGMA_EOL)
12559 break;
12561 if (token->type == CPP_SEMICOLON)
12563 /* A declaration consisting of a single semicolon is
12564 invalid. Allow it unless we're being pedantic. */
12565 cp_lexer_consume_token (parser->lexer);
12566 if (!in_system_header_at (input_location))
12567 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12568 continue;
12571 /* If we're entering or exiting a region that's implicitly
12572 extern "C", modify the lang context appropriately. */
12573 if (!parser->implicit_extern_c && token->implicit_extern_c)
12575 push_lang_context (lang_name_c);
12576 parser->implicit_extern_c = true;
12578 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12580 pop_lang_context ();
12581 parser->implicit_extern_c = false;
12584 if (token->type == CPP_PRAGMA)
12586 /* A top-level declaration can consist solely of a #pragma.
12587 A nested declaration cannot, so this is done here and not
12588 in cp_parser_declaration. (A #pragma at block scope is
12589 handled in cp_parser_statement.) */
12590 cp_parser_pragma (parser, pragma_external, NULL);
12591 continue;
12594 /* Parse the declaration itself. */
12595 cp_parser_declaration (parser);
12599 /* Parse a declaration.
12601 declaration:
12602 block-declaration
12603 function-definition
12604 template-declaration
12605 explicit-instantiation
12606 explicit-specialization
12607 linkage-specification
12608 namespace-definition
12610 C++17:
12611 deduction-guide
12613 GNU extension:
12615 declaration:
12616 __extension__ declaration */
12618 static void
12619 cp_parser_declaration (cp_parser* parser)
12621 cp_token token1;
12622 cp_token token2;
12623 int saved_pedantic;
12624 void *p;
12625 tree attributes = NULL_TREE;
12627 /* Check for the `__extension__' keyword. */
12628 if (cp_parser_extension_opt (parser, &saved_pedantic))
12630 /* Parse the qualified declaration. */
12631 cp_parser_declaration (parser);
12632 /* Restore the PEDANTIC flag. */
12633 pedantic = saved_pedantic;
12635 return;
12638 /* Try to figure out what kind of declaration is present. */
12639 token1 = *cp_lexer_peek_token (parser->lexer);
12641 if (token1.type != CPP_EOF)
12642 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12643 else
12645 token2.type = CPP_EOF;
12646 token2.keyword = RID_MAX;
12649 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12650 p = obstack_alloc (&declarator_obstack, 0);
12652 /* If the next token is `extern' and the following token is a string
12653 literal, then we have a linkage specification. */
12654 if (token1.keyword == RID_EXTERN
12655 && cp_parser_is_pure_string_literal (&token2))
12656 cp_parser_linkage_specification (parser);
12657 /* If the next token is `template', then we have either a template
12658 declaration, an explicit instantiation, or an explicit
12659 specialization. */
12660 else if (token1.keyword == RID_TEMPLATE)
12662 /* `template <>' indicates a template specialization. */
12663 if (token2.type == CPP_LESS
12664 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12665 cp_parser_explicit_specialization (parser);
12666 /* `template <' indicates a template declaration. */
12667 else if (token2.type == CPP_LESS)
12668 cp_parser_template_declaration (parser, /*member_p=*/false);
12669 /* Anything else must be an explicit instantiation. */
12670 else
12671 cp_parser_explicit_instantiation (parser);
12673 /* If the next token is `export', then we have a template
12674 declaration. */
12675 else if (token1.keyword == RID_EXPORT)
12676 cp_parser_template_declaration (parser, /*member_p=*/false);
12677 /* If the next token is `extern', 'static' or 'inline' and the one
12678 after that is `template', we have a GNU extended explicit
12679 instantiation directive. */
12680 else if (cp_parser_allow_gnu_extensions_p (parser)
12681 && (token1.keyword == RID_EXTERN
12682 || token1.keyword == RID_STATIC
12683 || token1.keyword == RID_INLINE)
12684 && token2.keyword == RID_TEMPLATE)
12685 cp_parser_explicit_instantiation (parser);
12686 /* If the next token is `namespace', check for a named or unnamed
12687 namespace definition. */
12688 else if (token1.keyword == RID_NAMESPACE
12689 && (/* A named namespace definition. */
12690 (token2.type == CPP_NAME
12691 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12692 != CPP_EQ))
12693 || (token2.type == CPP_OPEN_SQUARE
12694 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12695 == CPP_OPEN_SQUARE)
12696 /* An unnamed namespace definition. */
12697 || token2.type == CPP_OPEN_BRACE
12698 || token2.keyword == RID_ATTRIBUTE))
12699 cp_parser_namespace_definition (parser);
12700 /* An inline (associated) namespace definition. */
12701 else if (token1.keyword == RID_INLINE
12702 && token2.keyword == RID_NAMESPACE)
12703 cp_parser_namespace_definition (parser);
12704 /* Objective-C++ declaration/definition. */
12705 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12706 cp_parser_objc_declaration (parser, NULL_TREE);
12707 else if (c_dialect_objc ()
12708 && token1.keyword == RID_ATTRIBUTE
12709 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12710 cp_parser_objc_declaration (parser, attributes);
12711 /* At this point we may have a template declared by a concept
12712 introduction. */
12713 else if (flag_concepts
12714 && cp_parser_template_declaration_after_export (parser,
12715 /*member_p=*/false))
12716 /* We did. */;
12717 else
12718 /* Try to parse a block-declaration, or a function-definition. */
12719 cp_parser_block_declaration (parser, /*statement_p=*/false);
12721 /* Free any declarators allocated. */
12722 obstack_free (&declarator_obstack, p);
12725 /* Parse a block-declaration.
12727 block-declaration:
12728 simple-declaration
12729 asm-definition
12730 namespace-alias-definition
12731 using-declaration
12732 using-directive
12734 GNU Extension:
12736 block-declaration:
12737 __extension__ block-declaration
12739 C++0x Extension:
12741 block-declaration:
12742 static_assert-declaration
12744 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12745 part of a declaration-statement. */
12747 static void
12748 cp_parser_block_declaration (cp_parser *parser,
12749 bool statement_p)
12751 cp_token *token1;
12752 int saved_pedantic;
12754 /* Check for the `__extension__' keyword. */
12755 if (cp_parser_extension_opt (parser, &saved_pedantic))
12757 /* Parse the qualified declaration. */
12758 cp_parser_block_declaration (parser, statement_p);
12759 /* Restore the PEDANTIC flag. */
12760 pedantic = saved_pedantic;
12762 return;
12765 /* Peek at the next token to figure out which kind of declaration is
12766 present. */
12767 token1 = cp_lexer_peek_token (parser->lexer);
12769 /* If the next keyword is `asm', we have an asm-definition. */
12770 if (token1->keyword == RID_ASM)
12772 if (statement_p)
12773 cp_parser_commit_to_tentative_parse (parser);
12774 cp_parser_asm_definition (parser);
12776 /* If the next keyword is `namespace', we have a
12777 namespace-alias-definition. */
12778 else if (token1->keyword == RID_NAMESPACE)
12779 cp_parser_namespace_alias_definition (parser);
12780 /* If the next keyword is `using', we have a
12781 using-declaration, a using-directive, or an alias-declaration. */
12782 else if (token1->keyword == RID_USING)
12784 cp_token *token2;
12786 if (statement_p)
12787 cp_parser_commit_to_tentative_parse (parser);
12788 /* If the token after `using' is `namespace', then we have a
12789 using-directive. */
12790 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12791 if (token2->keyword == RID_NAMESPACE)
12792 cp_parser_using_directive (parser);
12793 /* If the second token after 'using' is '=', then we have an
12794 alias-declaration. */
12795 else if (cxx_dialect >= cxx11
12796 && token2->type == CPP_NAME
12797 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12798 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12799 cp_parser_alias_declaration (parser);
12800 /* Otherwise, it's a using-declaration. */
12801 else
12802 cp_parser_using_declaration (parser,
12803 /*access_declaration_p=*/false);
12805 /* If the next keyword is `__label__' we have a misplaced label
12806 declaration. */
12807 else if (token1->keyword == RID_LABEL)
12809 cp_lexer_consume_token (parser->lexer);
12810 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12811 cp_parser_skip_to_end_of_statement (parser);
12812 /* If the next token is now a `;', consume it. */
12813 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12814 cp_lexer_consume_token (parser->lexer);
12816 /* If the next token is `static_assert' we have a static assertion. */
12817 else if (token1->keyword == RID_STATIC_ASSERT)
12818 cp_parser_static_assert (parser, /*member_p=*/false);
12819 /* Anything else must be a simple-declaration. */
12820 else
12821 cp_parser_simple_declaration (parser, !statement_p,
12822 /*maybe_range_for_decl*/NULL);
12825 /* Parse a simple-declaration.
12827 simple-declaration:
12828 decl-specifier-seq [opt] init-declarator-list [opt] ;
12829 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12830 brace-or-equal-initializer ;
12832 init-declarator-list:
12833 init-declarator
12834 init-declarator-list , init-declarator
12836 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12837 function-definition as a simple-declaration.
12839 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12840 parsed declaration if it is an uninitialized single declarator not followed
12841 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12842 if present, will not be consumed. */
12844 static void
12845 cp_parser_simple_declaration (cp_parser* parser,
12846 bool function_definition_allowed_p,
12847 tree *maybe_range_for_decl)
12849 cp_decl_specifier_seq decl_specifiers;
12850 int declares_class_or_enum;
12851 bool saw_declarator;
12852 location_t comma_loc = UNKNOWN_LOCATION;
12853 location_t init_loc = UNKNOWN_LOCATION;
12855 if (maybe_range_for_decl)
12856 *maybe_range_for_decl = NULL_TREE;
12858 /* Defer access checks until we know what is being declared; the
12859 checks for names appearing in the decl-specifier-seq should be
12860 done as if we were in the scope of the thing being declared. */
12861 push_deferring_access_checks (dk_deferred);
12863 /* Parse the decl-specifier-seq. We have to keep track of whether
12864 or not the decl-specifier-seq declares a named class or
12865 enumeration type, since that is the only case in which the
12866 init-declarator-list is allowed to be empty.
12868 [dcl.dcl]
12870 In a simple-declaration, the optional init-declarator-list can be
12871 omitted only when declaring a class or enumeration, that is when
12872 the decl-specifier-seq contains either a class-specifier, an
12873 elaborated-type-specifier, or an enum-specifier. */
12874 cp_parser_decl_specifier_seq (parser,
12875 CP_PARSER_FLAGS_OPTIONAL,
12876 &decl_specifiers,
12877 &declares_class_or_enum);
12878 /* We no longer need to defer access checks. */
12879 stop_deferring_access_checks ();
12881 /* In a block scope, a valid declaration must always have a
12882 decl-specifier-seq. By not trying to parse declarators, we can
12883 resolve the declaration/expression ambiguity more quickly. */
12884 if (!function_definition_allowed_p
12885 && !decl_specifiers.any_specifiers_p)
12887 cp_parser_error (parser, "expected declaration");
12888 goto done;
12891 /* If the next two tokens are both identifiers, the code is
12892 erroneous. The usual cause of this situation is code like:
12894 T t;
12896 where "T" should name a type -- but does not. */
12897 if (!decl_specifiers.any_type_specifiers_p
12898 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12900 /* If parsing tentatively, we should commit; we really are
12901 looking at a declaration. */
12902 cp_parser_commit_to_tentative_parse (parser);
12903 /* Give up. */
12904 goto done;
12907 /* If we have seen at least one decl-specifier, and the next token
12908 is not a parenthesis, then we must be looking at a declaration.
12909 (After "int (" we might be looking at a functional cast.) */
12910 if (decl_specifiers.any_specifiers_p
12911 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12912 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12913 && !cp_parser_error_occurred (parser))
12914 cp_parser_commit_to_tentative_parse (parser);
12916 /* Look for C++17 decomposition declaration. */
12917 for (size_t n = 1; ; n++)
12918 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12919 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12920 continue;
12921 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12922 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12923 && decl_specifiers.any_specifiers_p)
12925 tree decl
12926 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12927 maybe_range_for_decl,
12928 &init_loc);
12930 /* The next token should be either a `,' or a `;'. */
12931 cp_token *token = cp_lexer_peek_token (parser->lexer);
12932 /* If it's a `;', we are done. */
12933 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12934 goto finish;
12935 /* Anything else is an error. */
12936 else
12938 /* If we have already issued an error message we don't need
12939 to issue another one. */
12940 if ((decl != error_mark_node
12941 && DECL_INITIAL (decl) != error_mark_node)
12942 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12943 cp_parser_error (parser, "expected %<,%> or %<;%>");
12944 /* Skip tokens until we reach the end of the statement. */
12945 cp_parser_skip_to_end_of_statement (parser);
12946 /* If the next token is now a `;', consume it. */
12947 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12948 cp_lexer_consume_token (parser->lexer);
12949 goto done;
12952 else
12953 break;
12955 tree last_type;
12956 bool auto_specifier_p;
12957 /* NULL_TREE if both variable and function declaration are allowed,
12958 error_mark_node if function declaration are not allowed and
12959 a FUNCTION_DECL that should be diagnosed if it is followed by
12960 variable declarations. */
12961 tree auto_function_declaration;
12963 last_type = NULL_TREE;
12964 auto_specifier_p
12965 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12966 auto_function_declaration = NULL_TREE;
12968 /* Keep going until we hit the `;' at the end of the simple
12969 declaration. */
12970 saw_declarator = false;
12971 while (cp_lexer_next_token_is_not (parser->lexer,
12972 CPP_SEMICOLON))
12974 cp_token *token;
12975 bool function_definition_p;
12976 tree decl;
12977 tree auto_result = NULL_TREE;
12979 if (saw_declarator)
12981 /* If we are processing next declarator, comma is expected */
12982 token = cp_lexer_peek_token (parser->lexer);
12983 gcc_assert (token->type == CPP_COMMA);
12984 cp_lexer_consume_token (parser->lexer);
12985 if (maybe_range_for_decl)
12987 *maybe_range_for_decl = error_mark_node;
12988 if (comma_loc == UNKNOWN_LOCATION)
12989 comma_loc = token->location;
12992 else
12993 saw_declarator = true;
12995 /* Parse the init-declarator. */
12996 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12997 /*checks=*/NULL,
12998 function_definition_allowed_p,
12999 /*member_p=*/false,
13000 declares_class_or_enum,
13001 &function_definition_p,
13002 maybe_range_for_decl,
13003 &init_loc,
13004 &auto_result);
13005 /* If an error occurred while parsing tentatively, exit quickly.
13006 (That usually happens when in the body of a function; each
13007 statement is treated as a declaration-statement until proven
13008 otherwise.) */
13009 if (cp_parser_error_occurred (parser))
13010 goto done;
13012 if (auto_specifier_p && cxx_dialect >= cxx14)
13014 /* If the init-declarator-list contains more than one
13015 init-declarator, they shall all form declarations of
13016 variables. */
13017 if (auto_function_declaration == NULL_TREE)
13018 auto_function_declaration
13019 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13020 else if (TREE_CODE (decl) == FUNCTION_DECL
13021 || auto_function_declaration != error_mark_node)
13023 error_at (decl_specifiers.locations[ds_type_spec],
13024 "non-variable %qD in declaration with more than one "
13025 "declarator with placeholder type",
13026 TREE_CODE (decl) == FUNCTION_DECL
13027 ? decl : auto_function_declaration);
13028 auto_function_declaration = error_mark_node;
13032 if (auto_result
13033 && (!processing_template_decl || !type_uses_auto (auto_result)))
13035 if (last_type
13036 && last_type != error_mark_node
13037 && !same_type_p (auto_result, last_type))
13039 /* If the list of declarators contains more than one declarator,
13040 the type of each declared variable is determined as described
13041 above. If the type deduced for the template parameter U is not
13042 the same in each deduction, the program is ill-formed. */
13043 error_at (decl_specifiers.locations[ds_type_spec],
13044 "inconsistent deduction for %qT: %qT and then %qT",
13045 decl_specifiers.type, last_type, auto_result);
13046 last_type = error_mark_node;
13048 else
13049 last_type = auto_result;
13052 /* Handle function definitions specially. */
13053 if (function_definition_p)
13055 /* If the next token is a `,', then we are probably
13056 processing something like:
13058 void f() {}, *p;
13060 which is erroneous. */
13061 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13063 cp_token *token = cp_lexer_peek_token (parser->lexer);
13064 error_at (token->location,
13065 "mixing"
13066 " declarations and function-definitions is forbidden");
13068 /* Otherwise, we're done with the list of declarators. */
13069 else
13071 pop_deferring_access_checks ();
13072 return;
13075 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13076 *maybe_range_for_decl = decl;
13077 /* The next token should be either a `,' or a `;'. */
13078 token = cp_lexer_peek_token (parser->lexer);
13079 /* If it's a `,', there are more declarators to come. */
13080 if (token->type == CPP_COMMA)
13081 /* will be consumed next time around */;
13082 /* If it's a `;', we are done. */
13083 else if (token->type == CPP_SEMICOLON)
13084 break;
13085 else if (maybe_range_for_decl)
13087 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13088 permerror (decl_specifiers.locations[ds_type_spec],
13089 "types may not be defined in a for-range-declaration");
13090 break;
13092 /* Anything else is an error. */
13093 else
13095 /* If we have already issued an error message we don't need
13096 to issue another one. */
13097 if ((decl != error_mark_node
13098 && DECL_INITIAL (decl) != error_mark_node)
13099 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13100 cp_parser_error (parser, "expected %<,%> or %<;%>");
13101 /* Skip tokens until we reach the end of the statement. */
13102 cp_parser_skip_to_end_of_statement (parser);
13103 /* If the next token is now a `;', consume it. */
13104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13105 cp_lexer_consume_token (parser->lexer);
13106 goto done;
13108 /* After the first time around, a function-definition is not
13109 allowed -- even if it was OK at first. For example:
13111 int i, f() {}
13113 is not valid. */
13114 function_definition_allowed_p = false;
13117 /* Issue an error message if no declarators are present, and the
13118 decl-specifier-seq does not itself declare a class or
13119 enumeration: [dcl.dcl]/3. */
13120 if (!saw_declarator)
13122 if (cp_parser_declares_only_class_p (parser))
13124 if (!declares_class_or_enum
13125 && decl_specifiers.type
13126 && OVERLOAD_TYPE_P (decl_specifiers.type))
13127 /* Ensure an error is issued anyway when finish_decltype_type,
13128 called via cp_parser_decl_specifier_seq, returns a class or
13129 an enumeration (c++/51786). */
13130 decl_specifiers.type = NULL_TREE;
13131 shadow_tag (&decl_specifiers);
13133 /* Perform any deferred access checks. */
13134 perform_deferred_access_checks (tf_warning_or_error);
13137 /* Consume the `;'. */
13138 finish:
13139 if (!maybe_range_for_decl)
13140 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13141 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13143 if (init_loc != UNKNOWN_LOCATION)
13144 error_at (init_loc, "initializer in range-based %<for%> loop");
13145 if (comma_loc != UNKNOWN_LOCATION)
13146 error_at (comma_loc,
13147 "multiple declarations in range-based %<for%> loop");
13150 done:
13151 pop_deferring_access_checks ();
13154 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13155 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13156 initializer ; */
13158 static tree
13159 cp_parser_decomposition_declaration (cp_parser *parser,
13160 cp_decl_specifier_seq *decl_specifiers,
13161 tree *maybe_range_for_decl,
13162 location_t *init_loc)
13164 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13165 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13166 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13168 /* Parse the identifier-list. */
13169 auto_vec<cp_expr, 10> v;
13170 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13171 while (true)
13173 cp_expr e = cp_parser_identifier (parser);
13174 if (e.get_value () == error_mark_node)
13175 break;
13176 v.safe_push (e);
13177 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13178 break;
13179 cp_lexer_consume_token (parser->lexer);
13182 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13183 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13185 end_loc = UNKNOWN_LOCATION;
13186 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13187 false);
13188 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13189 cp_lexer_consume_token (parser->lexer);
13190 else
13192 cp_parser_skip_to_end_of_statement (parser);
13193 return error_mark_node;
13197 if (cxx_dialect < cxx17)
13198 pedwarn (loc, 0, "structured bindings only available with "
13199 "-std=c++17 or -std=gnu++17");
13201 tree pushed_scope;
13202 cp_declarator *declarator = make_declarator (cdk_decomp);
13203 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13204 declarator->id_loc = loc;
13205 if (ref_qual != REF_QUAL_NONE)
13206 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13207 ref_qual == REF_QUAL_RVALUE,
13208 NULL_TREE);
13209 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13210 NULL_TREE, decl_specifiers->attributes,
13211 &pushed_scope);
13212 tree orig_decl = decl;
13214 unsigned int i;
13215 cp_expr e;
13216 cp_decl_specifier_seq decl_specs;
13217 clear_decl_specs (&decl_specs);
13218 decl_specs.type = make_auto ();
13219 tree prev = decl;
13220 FOR_EACH_VEC_ELT (v, i, e)
13222 if (i == 0)
13223 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13224 else
13225 declarator->u.id.unqualified_name = e.get_value ();
13226 declarator->id_loc = e.get_location ();
13227 tree elt_pushed_scope;
13228 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13229 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13230 if (decl2 == error_mark_node)
13231 decl = error_mark_node;
13232 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13234 /* Ensure we've diagnosed redeclaration if we aren't creating
13235 a new VAR_DECL. */
13236 gcc_assert (errorcount);
13237 decl = error_mark_node;
13239 else
13240 prev = decl2;
13241 if (elt_pushed_scope)
13242 pop_scope (elt_pushed_scope);
13245 if (v.is_empty ())
13247 error_at (loc, "empty structured binding declaration");
13248 decl = error_mark_node;
13251 if (maybe_range_for_decl == NULL
13252 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13254 bool non_constant_p = false, is_direct_init = false;
13255 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13256 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13257 &non_constant_p);
13258 if (initializer == NULL_TREE
13259 || (TREE_CODE (initializer) == TREE_LIST
13260 && TREE_CHAIN (initializer))
13261 || (is_direct_init
13262 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13263 && CONSTRUCTOR_NELTS (initializer) != 1))
13265 error_at (loc, "invalid initializer for structured binding "
13266 "declaration");
13267 initializer = error_mark_node;
13270 if (decl != error_mark_node)
13272 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13273 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13274 cp_finish_decomp (decl, prev, v.length ());
13277 else if (decl != error_mark_node)
13279 *maybe_range_for_decl = prev;
13280 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13281 the underlying DECL. */
13282 cp_finish_decomp (decl, prev, v.length ());
13285 if (pushed_scope)
13286 pop_scope (pushed_scope);
13288 if (decl == error_mark_node && DECL_P (orig_decl))
13290 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13291 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13294 return decl;
13297 /* Parse a decl-specifier-seq.
13299 decl-specifier-seq:
13300 decl-specifier-seq [opt] decl-specifier
13301 decl-specifier attribute-specifier-seq [opt] (C++11)
13303 decl-specifier:
13304 storage-class-specifier
13305 type-specifier
13306 function-specifier
13307 friend
13308 typedef
13310 GNU Extension:
13312 decl-specifier:
13313 attributes
13315 Concepts Extension:
13317 decl-specifier:
13318 concept
13320 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13322 The parser flags FLAGS is used to control type-specifier parsing.
13324 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13325 flags:
13327 1: one of the decl-specifiers is an elaborated-type-specifier
13328 (i.e., a type declaration)
13329 2: one of the decl-specifiers is an enum-specifier or a
13330 class-specifier (i.e., a type definition)
13334 static void
13335 cp_parser_decl_specifier_seq (cp_parser* parser,
13336 cp_parser_flags flags,
13337 cp_decl_specifier_seq *decl_specs,
13338 int* declares_class_or_enum)
13340 bool constructor_possible_p = !parser->in_declarator_p;
13341 bool found_decl_spec = false;
13342 cp_token *start_token = NULL;
13343 cp_decl_spec ds;
13345 /* Clear DECL_SPECS. */
13346 clear_decl_specs (decl_specs);
13348 /* Assume no class or enumeration type is declared. */
13349 *declares_class_or_enum = 0;
13351 /* Keep reading specifiers until there are no more to read. */
13352 while (true)
13354 bool constructor_p;
13355 cp_token *token;
13356 ds = ds_last;
13358 /* Peek at the next token. */
13359 token = cp_lexer_peek_token (parser->lexer);
13361 /* Save the first token of the decl spec list for error
13362 reporting. */
13363 if (!start_token)
13364 start_token = token;
13365 /* Handle attributes. */
13366 if (cp_next_tokens_can_be_attribute_p (parser))
13368 /* Parse the attributes. */
13369 tree attrs = cp_parser_attributes_opt (parser);
13371 /* In a sequence of declaration specifiers, c++11 attributes
13372 appertain to the type that precede them. In that case
13373 [dcl.spec]/1 says:
13375 The attribute-specifier-seq affects the type only for
13376 the declaration it appears in, not other declarations
13377 involving the same type.
13379 But for now let's force the user to position the
13380 attribute either at the beginning of the declaration or
13381 after the declarator-id, which would clearly mean that it
13382 applies to the declarator. */
13383 if (cxx11_attribute_p (attrs))
13385 if (!found_decl_spec)
13386 /* The c++11 attribute is at the beginning of the
13387 declaration. It appertains to the entity being
13388 declared. */;
13389 else
13391 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13393 /* This is an attribute following a
13394 class-specifier. */
13395 if (decl_specs->type_definition_p)
13396 warn_misplaced_attr_for_class_type (token->location,
13397 decl_specs->type);
13398 attrs = NULL_TREE;
13400 else
13402 decl_specs->std_attributes
13403 = chainon (decl_specs->std_attributes,
13404 attrs);
13405 if (decl_specs->locations[ds_std_attribute] == 0)
13406 decl_specs->locations[ds_std_attribute] = token->location;
13408 continue;
13412 decl_specs->attributes
13413 = chainon (decl_specs->attributes,
13414 attrs);
13415 if (decl_specs->locations[ds_attribute] == 0)
13416 decl_specs->locations[ds_attribute] = token->location;
13417 continue;
13419 /* Assume we will find a decl-specifier keyword. */
13420 found_decl_spec = true;
13421 /* If the next token is an appropriate keyword, we can simply
13422 add it to the list. */
13423 switch (token->keyword)
13425 /* decl-specifier:
13426 friend
13427 constexpr */
13428 case RID_FRIEND:
13429 if (!at_class_scope_p ())
13431 gcc_rich_location richloc (token->location);
13432 richloc.add_fixit_remove ();
13433 error_at (&richloc, "%<friend%> used outside of class");
13434 cp_lexer_purge_token (parser->lexer);
13436 else
13438 ds = ds_friend;
13439 /* Consume the token. */
13440 cp_lexer_consume_token (parser->lexer);
13442 break;
13444 case RID_CONSTEXPR:
13445 ds = ds_constexpr;
13446 cp_lexer_consume_token (parser->lexer);
13447 break;
13449 case RID_CONCEPT:
13450 ds = ds_concept;
13451 cp_lexer_consume_token (parser->lexer);
13452 break;
13454 /* function-specifier:
13455 inline
13456 virtual
13457 explicit */
13458 case RID_INLINE:
13459 case RID_VIRTUAL:
13460 case RID_EXPLICIT:
13461 cp_parser_function_specifier_opt (parser, decl_specs);
13462 break;
13464 /* decl-specifier:
13465 typedef */
13466 case RID_TYPEDEF:
13467 ds = ds_typedef;
13468 /* Consume the token. */
13469 cp_lexer_consume_token (parser->lexer);
13470 /* A constructor declarator cannot appear in a typedef. */
13471 constructor_possible_p = false;
13472 /* The "typedef" keyword can only occur in a declaration; we
13473 may as well commit at this point. */
13474 cp_parser_commit_to_tentative_parse (parser);
13476 if (decl_specs->storage_class != sc_none)
13477 decl_specs->conflicting_specifiers_p = true;
13478 break;
13480 /* storage-class-specifier:
13481 auto
13482 register
13483 static
13484 extern
13485 mutable
13487 GNU Extension:
13488 thread */
13489 case RID_AUTO:
13490 if (cxx_dialect == cxx98)
13492 /* Consume the token. */
13493 cp_lexer_consume_token (parser->lexer);
13495 /* Complain about `auto' as a storage specifier, if
13496 we're complaining about C++0x compatibility. */
13497 gcc_rich_location richloc (token->location);
13498 richloc.add_fixit_remove ();
13499 warning_at (&richloc, OPT_Wc__11_compat,
13500 "%<auto%> changes meaning in C++11; "
13501 "please remove it");
13503 /* Set the storage class anyway. */
13504 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13505 token);
13507 else
13508 /* C++0x auto type-specifier. */
13509 found_decl_spec = false;
13510 break;
13512 case RID_REGISTER:
13513 case RID_STATIC:
13514 case RID_EXTERN:
13515 case RID_MUTABLE:
13516 /* Consume the token. */
13517 cp_lexer_consume_token (parser->lexer);
13518 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13519 token);
13520 break;
13521 case RID_THREAD:
13522 /* Consume the token. */
13523 ds = ds_thread;
13524 cp_lexer_consume_token (parser->lexer);
13525 break;
13527 default:
13528 /* We did not yet find a decl-specifier yet. */
13529 found_decl_spec = false;
13530 break;
13533 if (found_decl_spec
13534 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13535 && token->keyword != RID_CONSTEXPR)
13536 error ("decl-specifier invalid in condition");
13538 if (found_decl_spec
13539 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13540 && token->keyword != RID_MUTABLE
13541 && token->keyword != RID_CONSTEXPR)
13542 error_at (token->location, "%qD invalid in lambda",
13543 ridpointers[token->keyword]);
13545 if (ds != ds_last)
13546 set_and_check_decl_spec_loc (decl_specs, ds, token);
13548 /* Constructors are a special case. The `S' in `S()' is not a
13549 decl-specifier; it is the beginning of the declarator. */
13550 constructor_p
13551 = (!found_decl_spec
13552 && constructor_possible_p
13553 && (cp_parser_constructor_declarator_p
13554 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13556 /* If we don't have a DECL_SPEC yet, then we must be looking at
13557 a type-specifier. */
13558 if (!found_decl_spec && !constructor_p)
13560 int decl_spec_declares_class_or_enum;
13561 bool is_cv_qualifier;
13562 tree type_spec;
13564 type_spec
13565 = cp_parser_type_specifier (parser, flags,
13566 decl_specs,
13567 /*is_declaration=*/true,
13568 &decl_spec_declares_class_or_enum,
13569 &is_cv_qualifier);
13570 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13572 /* If this type-specifier referenced a user-defined type
13573 (a typedef, class-name, etc.), then we can't allow any
13574 more such type-specifiers henceforth.
13576 [dcl.spec]
13578 The longest sequence of decl-specifiers that could
13579 possibly be a type name is taken as the
13580 decl-specifier-seq of a declaration. The sequence shall
13581 be self-consistent as described below.
13583 [dcl.type]
13585 As a general rule, at most one type-specifier is allowed
13586 in the complete decl-specifier-seq of a declaration. The
13587 only exceptions are the following:
13589 -- const or volatile can be combined with any other
13590 type-specifier.
13592 -- signed or unsigned can be combined with char, long,
13593 short, or int.
13595 -- ..
13597 Example:
13599 typedef char* Pc;
13600 void g (const int Pc);
13602 Here, Pc is *not* part of the decl-specifier seq; it's
13603 the declarator. Therefore, once we see a type-specifier
13604 (other than a cv-qualifier), we forbid any additional
13605 user-defined types. We *do* still allow things like `int
13606 int' to be considered a decl-specifier-seq, and issue the
13607 error message later. */
13608 if (type_spec && !is_cv_qualifier)
13609 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13610 /* A constructor declarator cannot follow a type-specifier. */
13611 if (type_spec)
13613 constructor_possible_p = false;
13614 found_decl_spec = true;
13615 if (!is_cv_qualifier)
13616 decl_specs->any_type_specifiers_p = true;
13620 /* If we still do not have a DECL_SPEC, then there are no more
13621 decl-specifiers. */
13622 if (!found_decl_spec)
13623 break;
13625 decl_specs->any_specifiers_p = true;
13626 /* After we see one decl-specifier, further decl-specifiers are
13627 always optional. */
13628 flags |= CP_PARSER_FLAGS_OPTIONAL;
13631 /* Don't allow a friend specifier with a class definition. */
13632 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13633 && (*declares_class_or_enum & 2))
13634 error_at (decl_specs->locations[ds_friend],
13635 "class definition may not be declared a friend");
13638 /* Parse an (optional) storage-class-specifier.
13640 storage-class-specifier:
13641 auto
13642 register
13643 static
13644 extern
13645 mutable
13647 GNU Extension:
13649 storage-class-specifier:
13650 thread
13652 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13654 static tree
13655 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13657 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13659 case RID_AUTO:
13660 if (cxx_dialect != cxx98)
13661 return NULL_TREE;
13662 /* Fall through for C++98. */
13663 gcc_fallthrough ();
13665 case RID_REGISTER:
13666 case RID_STATIC:
13667 case RID_EXTERN:
13668 case RID_MUTABLE:
13669 case RID_THREAD:
13670 /* Consume the token. */
13671 return cp_lexer_consume_token (parser->lexer)->u.value;
13673 default:
13674 return NULL_TREE;
13678 /* Parse an (optional) function-specifier.
13680 function-specifier:
13681 inline
13682 virtual
13683 explicit
13685 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13686 Updates DECL_SPECS, if it is non-NULL. */
13688 static tree
13689 cp_parser_function_specifier_opt (cp_parser* parser,
13690 cp_decl_specifier_seq *decl_specs)
13692 cp_token *token = cp_lexer_peek_token (parser->lexer);
13693 switch (token->keyword)
13695 case RID_INLINE:
13696 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13697 break;
13699 case RID_VIRTUAL:
13700 /* 14.5.2.3 [temp.mem]
13702 A member function template shall not be virtual. */
13703 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13704 && current_class_type)
13705 error_at (token->location, "templates may not be %<virtual%>");
13706 else
13707 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13708 break;
13710 case RID_EXPLICIT:
13711 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13712 break;
13714 default:
13715 return NULL_TREE;
13718 /* Consume the token. */
13719 return cp_lexer_consume_token (parser->lexer)->u.value;
13722 /* Parse a linkage-specification.
13724 linkage-specification:
13725 extern string-literal { declaration-seq [opt] }
13726 extern string-literal declaration */
13728 static void
13729 cp_parser_linkage_specification (cp_parser* parser)
13731 tree linkage;
13733 /* Look for the `extern' keyword. */
13734 cp_token *extern_token
13735 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13737 /* Look for the string-literal. */
13738 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13739 linkage = cp_parser_string_literal (parser, false, false);
13741 /* Transform the literal into an identifier. If the literal is a
13742 wide-character string, or contains embedded NULs, then we can't
13743 handle it as the user wants. */
13744 if (strlen (TREE_STRING_POINTER (linkage))
13745 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13747 cp_parser_error (parser, "invalid linkage-specification");
13748 /* Assume C++ linkage. */
13749 linkage = lang_name_cplusplus;
13751 else
13752 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13754 /* We're now using the new linkage. */
13755 push_lang_context (linkage);
13757 /* Preserve the location of the the innermost linkage specification,
13758 tracking the locations of nested specifications via a local. */
13759 location_t saved_location
13760 = parser->innermost_linkage_specification_location;
13761 /* Construct a location ranging from the start of the "extern" to
13762 the end of the string-literal, with the caret at the start, e.g.:
13763 extern "C" {
13764 ^~~~~~~~~~
13766 parser->innermost_linkage_specification_location
13767 = make_location (extern_token->location,
13768 extern_token->location,
13769 get_finish (string_token->location));
13771 /* If the next token is a `{', then we're using the first
13772 production. */
13773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13775 cp_ensure_no_omp_declare_simd (parser);
13776 cp_ensure_no_oacc_routine (parser);
13778 /* Consume the `{' token. */
13779 matching_braces braces;
13780 braces.consume_open (parser)->location;
13781 /* Parse the declarations. */
13782 cp_parser_declaration_seq_opt (parser);
13783 /* Look for the closing `}'. */
13784 braces.require_close (parser);
13786 /* Otherwise, there's just one declaration. */
13787 else
13789 bool saved_in_unbraced_linkage_specification_p;
13791 saved_in_unbraced_linkage_specification_p
13792 = parser->in_unbraced_linkage_specification_p;
13793 parser->in_unbraced_linkage_specification_p = true;
13794 cp_parser_declaration (parser);
13795 parser->in_unbraced_linkage_specification_p
13796 = saved_in_unbraced_linkage_specification_p;
13799 /* We're done with the linkage-specification. */
13800 pop_lang_context ();
13802 /* Restore location of parent linkage specification, if any. */
13803 parser->innermost_linkage_specification_location = saved_location;
13806 /* Parse a static_assert-declaration.
13808 static_assert-declaration:
13809 static_assert ( constant-expression , string-literal ) ;
13810 static_assert ( constant-expression ) ; (C++17)
13812 If MEMBER_P, this static_assert is a class member. */
13814 static void
13815 cp_parser_static_assert(cp_parser *parser, bool member_p)
13817 cp_expr condition;
13818 location_t token_loc;
13819 tree message;
13820 bool dummy;
13822 /* Peek at the `static_assert' token so we can keep track of exactly
13823 where the static assertion started. */
13824 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13826 /* Look for the `static_assert' keyword. */
13827 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13828 RT_STATIC_ASSERT))
13829 return;
13831 /* We know we are in a static assertion; commit to any tentative
13832 parse. */
13833 if (cp_parser_parsing_tentatively (parser))
13834 cp_parser_commit_to_tentative_parse (parser);
13836 /* Parse the `(' starting the static assertion condition. */
13837 matching_parens parens;
13838 parens.require_open (parser);
13840 /* Parse the constant-expression. Allow a non-constant expression
13841 here in order to give better diagnostics in finish_static_assert. */
13842 condition =
13843 cp_parser_constant_expression (parser,
13844 /*allow_non_constant_p=*/true,
13845 /*non_constant_p=*/&dummy);
13847 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13849 if (cxx_dialect < cxx17)
13850 pedwarn (input_location, OPT_Wpedantic,
13851 "static_assert without a message "
13852 "only available with -std=c++17 or -std=gnu++17");
13853 /* Eat the ')' */
13854 cp_lexer_consume_token (parser->lexer);
13855 message = build_string (1, "");
13856 TREE_TYPE (message) = char_array_type_node;
13857 fix_string_type (message);
13859 else
13861 /* Parse the separating `,'. */
13862 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13864 /* Parse the string-literal message. */
13865 message = cp_parser_string_literal (parser,
13866 /*translate=*/false,
13867 /*wide_ok=*/true);
13869 /* A `)' completes the static assertion. */
13870 if (!parens.require_close (parser))
13871 cp_parser_skip_to_closing_parenthesis (parser,
13872 /*recovering=*/true,
13873 /*or_comma=*/false,
13874 /*consume_paren=*/true);
13877 /* A semicolon terminates the declaration. */
13878 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13880 /* Get the location for the static assertion. Use that of the
13881 condition if available, otherwise, use that of the "static_assert"
13882 token. */
13883 location_t assert_loc = condition.get_location ();
13884 if (assert_loc == UNKNOWN_LOCATION)
13885 assert_loc = token_loc;
13887 /* Complete the static assertion, which may mean either processing
13888 the static assert now or saving it for template instantiation. */
13889 finish_static_assert (condition, message, assert_loc, member_p);
13892 /* Parse the expression in decltype ( expression ). */
13894 static tree
13895 cp_parser_decltype_expr (cp_parser *parser,
13896 bool &id_expression_or_member_access_p)
13898 cp_token *id_expr_start_token;
13899 tree expr;
13901 /* Since we're going to preserve any side-effects from this parse, set up a
13902 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13903 in the expression. */
13904 tentative_firewall firewall (parser);
13906 /* First, try parsing an id-expression. */
13907 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13908 cp_parser_parse_tentatively (parser);
13909 expr = cp_parser_id_expression (parser,
13910 /*template_keyword_p=*/false,
13911 /*check_dependency_p=*/true,
13912 /*template_p=*/NULL,
13913 /*declarator_p=*/false,
13914 /*optional_p=*/false);
13916 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13918 bool non_integral_constant_expression_p = false;
13919 tree id_expression = expr;
13920 cp_id_kind idk;
13921 const char *error_msg;
13923 if (identifier_p (expr))
13924 /* Lookup the name we got back from the id-expression. */
13925 expr = cp_parser_lookup_name_simple (parser, expr,
13926 id_expr_start_token->location);
13928 if (expr
13929 && expr != error_mark_node
13930 && TREE_CODE (expr) != TYPE_DECL
13931 && (TREE_CODE (expr) != BIT_NOT_EXPR
13932 || !TYPE_P (TREE_OPERAND (expr, 0)))
13933 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13935 /* Complete lookup of the id-expression. */
13936 expr = (finish_id_expression
13937 (id_expression, expr, parser->scope, &idk,
13938 /*integral_constant_expression_p=*/false,
13939 /*allow_non_integral_constant_expression_p=*/true,
13940 &non_integral_constant_expression_p,
13941 /*template_p=*/false,
13942 /*done=*/true,
13943 /*address_p=*/false,
13944 /*template_arg_p=*/false,
13945 &error_msg,
13946 id_expr_start_token->location));
13948 if (expr == error_mark_node)
13949 /* We found an id-expression, but it was something that we
13950 should not have found. This is an error, not something
13951 we can recover from, so note that we found an
13952 id-expression and we'll recover as gracefully as
13953 possible. */
13954 id_expression_or_member_access_p = true;
13957 if (expr
13958 && expr != error_mark_node
13959 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13960 /* We have an id-expression. */
13961 id_expression_or_member_access_p = true;
13964 if (!id_expression_or_member_access_p)
13966 /* Abort the id-expression parse. */
13967 cp_parser_abort_tentative_parse (parser);
13969 /* Parsing tentatively, again. */
13970 cp_parser_parse_tentatively (parser);
13972 /* Parse a class member access. */
13973 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13974 /*cast_p=*/false, /*decltype*/true,
13975 /*member_access_only_p=*/true, NULL);
13977 if (expr
13978 && expr != error_mark_node
13979 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13980 /* We have an id-expression. */
13981 id_expression_or_member_access_p = true;
13984 if (id_expression_or_member_access_p)
13985 /* We have parsed the complete id-expression or member access. */
13986 cp_parser_parse_definitely (parser);
13987 else
13989 /* Abort our attempt to parse an id-expression or member access
13990 expression. */
13991 cp_parser_abort_tentative_parse (parser);
13993 /* Parse a full expression. */
13994 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13995 /*decltype_p=*/true);
13998 return expr;
14001 /* Parse a `decltype' type. Returns the type.
14003 simple-type-specifier:
14004 decltype ( expression )
14005 C++14 proposal:
14006 decltype ( auto ) */
14008 static tree
14009 cp_parser_decltype (cp_parser *parser)
14011 tree expr;
14012 bool id_expression_or_member_access_p = false;
14013 const char *saved_message;
14014 bool saved_integral_constant_expression_p;
14015 bool saved_non_integral_constant_expression_p;
14016 bool saved_greater_than_is_operator_p;
14017 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14019 if (start_token->type == CPP_DECLTYPE)
14021 /* Already parsed. */
14022 cp_lexer_consume_token (parser->lexer);
14023 return saved_checks_value (start_token->u.tree_check_value);
14026 /* Look for the `decltype' token. */
14027 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14028 return error_mark_node;
14030 /* Parse the opening `('. */
14031 matching_parens parens;
14032 if (!parens.require_open (parser))
14033 return error_mark_node;
14035 /* decltype (auto) */
14036 if (cxx_dialect >= cxx14
14037 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14039 cp_lexer_consume_token (parser->lexer);
14040 if (!parens.require_close (parser))
14041 return error_mark_node;
14042 expr = make_decltype_auto ();
14043 AUTO_IS_DECLTYPE (expr) = true;
14044 goto rewrite;
14047 /* Types cannot be defined in a `decltype' expression. Save away the
14048 old message. */
14049 saved_message = parser->type_definition_forbidden_message;
14051 /* And create the new one. */
14052 parser->type_definition_forbidden_message
14053 = G_("types may not be defined in %<decltype%> expressions");
14055 /* The restrictions on constant-expressions do not apply inside
14056 decltype expressions. */
14057 saved_integral_constant_expression_p
14058 = parser->integral_constant_expression_p;
14059 saved_non_integral_constant_expression_p
14060 = parser->non_integral_constant_expression_p;
14061 parser->integral_constant_expression_p = false;
14063 /* Within a parenthesized expression, a `>' token is always
14064 the greater-than operator. */
14065 saved_greater_than_is_operator_p
14066 = parser->greater_than_is_operator_p;
14067 parser->greater_than_is_operator_p = true;
14069 /* Do not actually evaluate the expression. */
14070 ++cp_unevaluated_operand;
14072 /* Do not warn about problems with the expression. */
14073 ++c_inhibit_evaluation_warnings;
14075 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14077 /* Go back to evaluating expressions. */
14078 --cp_unevaluated_operand;
14079 --c_inhibit_evaluation_warnings;
14081 /* The `>' token might be the end of a template-id or
14082 template-parameter-list now. */
14083 parser->greater_than_is_operator_p
14084 = saved_greater_than_is_operator_p;
14086 /* Restore the old message and the integral constant expression
14087 flags. */
14088 parser->type_definition_forbidden_message = saved_message;
14089 parser->integral_constant_expression_p
14090 = saved_integral_constant_expression_p;
14091 parser->non_integral_constant_expression_p
14092 = saved_non_integral_constant_expression_p;
14094 /* Parse to the closing `)'. */
14095 if (!parens.require_close (parser))
14097 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14098 /*consume_paren=*/true);
14099 return error_mark_node;
14102 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14103 tf_warning_or_error);
14105 rewrite:
14106 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14107 it again. */
14108 start_token->type = CPP_DECLTYPE;
14109 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14110 start_token->u.tree_check_value->value = expr;
14111 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14112 start_token->keyword = RID_MAX;
14113 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14115 return expr;
14118 /* Special member functions [gram.special] */
14120 /* Parse a conversion-function-id.
14122 conversion-function-id:
14123 operator conversion-type-id
14125 Returns an IDENTIFIER_NODE representing the operator. */
14127 static tree
14128 cp_parser_conversion_function_id (cp_parser* parser)
14130 tree type;
14131 tree saved_scope;
14132 tree saved_qualifying_scope;
14133 tree saved_object_scope;
14134 tree pushed_scope = NULL_TREE;
14136 /* Look for the `operator' token. */
14137 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14138 return error_mark_node;
14139 /* When we parse the conversion-type-id, the current scope will be
14140 reset. However, we need that information in able to look up the
14141 conversion function later, so we save it here. */
14142 saved_scope = parser->scope;
14143 saved_qualifying_scope = parser->qualifying_scope;
14144 saved_object_scope = parser->object_scope;
14145 /* We must enter the scope of the class so that the names of
14146 entities declared within the class are available in the
14147 conversion-type-id. For example, consider:
14149 struct S {
14150 typedef int I;
14151 operator I();
14154 S::operator I() { ... }
14156 In order to see that `I' is a type-name in the definition, we
14157 must be in the scope of `S'. */
14158 if (saved_scope)
14159 pushed_scope = push_scope (saved_scope);
14160 /* Parse the conversion-type-id. */
14161 type = cp_parser_conversion_type_id (parser);
14162 /* Leave the scope of the class, if any. */
14163 if (pushed_scope)
14164 pop_scope (pushed_scope);
14165 /* Restore the saved scope. */
14166 parser->scope = saved_scope;
14167 parser->qualifying_scope = saved_qualifying_scope;
14168 parser->object_scope = saved_object_scope;
14169 /* If the TYPE is invalid, indicate failure. */
14170 if (type == error_mark_node)
14171 return error_mark_node;
14172 return make_conv_op_name (type);
14175 /* Parse a conversion-type-id:
14177 conversion-type-id:
14178 type-specifier-seq conversion-declarator [opt]
14180 Returns the TYPE specified. */
14182 static tree
14183 cp_parser_conversion_type_id (cp_parser* parser)
14185 tree attributes;
14186 cp_decl_specifier_seq type_specifiers;
14187 cp_declarator *declarator;
14188 tree type_specified;
14189 const char *saved_message;
14191 /* Parse the attributes. */
14192 attributes = cp_parser_attributes_opt (parser);
14194 saved_message = parser->type_definition_forbidden_message;
14195 parser->type_definition_forbidden_message
14196 = G_("types may not be defined in a conversion-type-id");
14198 /* Parse the type-specifiers. */
14199 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14200 /*is_trailing_return=*/false,
14201 &type_specifiers);
14203 parser->type_definition_forbidden_message = saved_message;
14205 /* If that didn't work, stop. */
14206 if (type_specifiers.type == error_mark_node)
14207 return error_mark_node;
14208 /* Parse the conversion-declarator. */
14209 declarator = cp_parser_conversion_declarator_opt (parser);
14211 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14212 /*initialized=*/0, &attributes);
14213 if (attributes)
14214 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14216 /* Don't give this error when parsing tentatively. This happens to
14217 work because we always parse this definitively once. */
14218 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14219 && type_uses_auto (type_specified))
14221 if (cxx_dialect < cxx14)
14223 error ("invalid use of %<auto%> in conversion operator");
14224 return error_mark_node;
14226 else if (template_parm_scope_p ())
14227 warning (0, "use of %<auto%> in member template "
14228 "conversion operator can never be deduced");
14231 return type_specified;
14234 /* Parse an (optional) conversion-declarator.
14236 conversion-declarator:
14237 ptr-operator conversion-declarator [opt]
14241 static cp_declarator *
14242 cp_parser_conversion_declarator_opt (cp_parser* parser)
14244 enum tree_code code;
14245 tree class_type, std_attributes = NULL_TREE;
14246 cp_cv_quals cv_quals;
14248 /* We don't know if there's a ptr-operator next, or not. */
14249 cp_parser_parse_tentatively (parser);
14250 /* Try the ptr-operator. */
14251 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14252 &std_attributes);
14253 /* If it worked, look for more conversion-declarators. */
14254 if (cp_parser_parse_definitely (parser))
14256 cp_declarator *declarator;
14258 /* Parse another optional declarator. */
14259 declarator = cp_parser_conversion_declarator_opt (parser);
14261 declarator = cp_parser_make_indirect_declarator
14262 (code, class_type, cv_quals, declarator, std_attributes);
14264 return declarator;
14267 return NULL;
14270 /* Parse an (optional) ctor-initializer.
14272 ctor-initializer:
14273 : mem-initializer-list */
14275 static void
14276 cp_parser_ctor_initializer_opt (cp_parser* parser)
14278 /* If the next token is not a `:', then there is no
14279 ctor-initializer. */
14280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14282 /* Do default initialization of any bases and members. */
14283 if (DECL_CONSTRUCTOR_P (current_function_decl))
14284 finish_mem_initializers (NULL_TREE);
14285 return;
14288 /* Consume the `:' token. */
14289 cp_lexer_consume_token (parser->lexer);
14290 /* And the mem-initializer-list. */
14291 cp_parser_mem_initializer_list (parser);
14294 /* Parse a mem-initializer-list.
14296 mem-initializer-list:
14297 mem-initializer ... [opt]
14298 mem-initializer ... [opt] , mem-initializer-list */
14300 static void
14301 cp_parser_mem_initializer_list (cp_parser* parser)
14303 tree mem_initializer_list = NULL_TREE;
14304 tree target_ctor = error_mark_node;
14305 cp_token *token = cp_lexer_peek_token (parser->lexer);
14307 /* Let the semantic analysis code know that we are starting the
14308 mem-initializer-list. */
14309 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14310 error_at (token->location,
14311 "only constructors take member initializers");
14313 /* Loop through the list. */
14314 while (true)
14316 tree mem_initializer;
14318 token = cp_lexer_peek_token (parser->lexer);
14319 /* Parse the mem-initializer. */
14320 mem_initializer = cp_parser_mem_initializer (parser);
14321 /* If the next token is a `...', we're expanding member initializers. */
14322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14324 /* Consume the `...'. */
14325 cp_lexer_consume_token (parser->lexer);
14327 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14328 can be expanded but members cannot. */
14329 if (mem_initializer != error_mark_node
14330 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14332 error_at (token->location,
14333 "cannot expand initializer for member %qD",
14334 TREE_PURPOSE (mem_initializer));
14335 mem_initializer = error_mark_node;
14338 /* Construct the pack expansion type. */
14339 if (mem_initializer != error_mark_node)
14340 mem_initializer = make_pack_expansion (mem_initializer);
14342 if (target_ctor != error_mark_node
14343 && mem_initializer != error_mark_node)
14345 error ("mem-initializer for %qD follows constructor delegation",
14346 TREE_PURPOSE (mem_initializer));
14347 mem_initializer = error_mark_node;
14349 /* Look for a target constructor. */
14350 if (mem_initializer != error_mark_node
14351 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14352 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14354 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14355 if (mem_initializer_list)
14357 error ("constructor delegation follows mem-initializer for %qD",
14358 TREE_PURPOSE (mem_initializer_list));
14359 mem_initializer = error_mark_node;
14361 target_ctor = mem_initializer;
14363 /* Add it to the list, unless it was erroneous. */
14364 if (mem_initializer != error_mark_node)
14366 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14367 mem_initializer_list = mem_initializer;
14369 /* If the next token is not a `,', we're done. */
14370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14371 break;
14372 /* Consume the `,' token. */
14373 cp_lexer_consume_token (parser->lexer);
14376 /* Perform semantic analysis. */
14377 if (DECL_CONSTRUCTOR_P (current_function_decl))
14378 finish_mem_initializers (mem_initializer_list);
14381 /* Parse a mem-initializer.
14383 mem-initializer:
14384 mem-initializer-id ( expression-list [opt] )
14385 mem-initializer-id braced-init-list
14387 GNU extension:
14389 mem-initializer:
14390 ( expression-list [opt] )
14392 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14393 class) or FIELD_DECL (for a non-static data member) to initialize;
14394 the TREE_VALUE is the expression-list. An empty initialization
14395 list is represented by void_list_node. */
14397 static tree
14398 cp_parser_mem_initializer (cp_parser* parser)
14400 tree mem_initializer_id;
14401 tree expression_list;
14402 tree member;
14403 cp_token *token = cp_lexer_peek_token (parser->lexer);
14405 /* Find out what is being initialized. */
14406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14408 permerror (token->location,
14409 "anachronistic old-style base class initializer");
14410 mem_initializer_id = NULL_TREE;
14412 else
14414 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14415 if (mem_initializer_id == error_mark_node)
14416 return mem_initializer_id;
14418 member = expand_member_init (mem_initializer_id);
14419 if (member && !DECL_P (member))
14420 in_base_initializer = 1;
14422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14424 bool expr_non_constant_p;
14425 cp_lexer_set_source_position (parser->lexer);
14426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14427 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14428 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14429 expression_list = build_tree_list (NULL_TREE, expression_list);
14431 else
14433 vec<tree, va_gc> *vec;
14434 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14435 /*cast_p=*/false,
14436 /*allow_expansion_p=*/true,
14437 /*non_constant_p=*/NULL);
14438 if (vec == NULL)
14439 return error_mark_node;
14440 expression_list = build_tree_list_vec (vec);
14441 release_tree_vector (vec);
14444 if (expression_list == error_mark_node)
14445 return error_mark_node;
14446 if (!expression_list)
14447 expression_list = void_type_node;
14449 in_base_initializer = 0;
14451 return member ? build_tree_list (member, expression_list) : error_mark_node;
14454 /* Parse a mem-initializer-id.
14456 mem-initializer-id:
14457 :: [opt] nested-name-specifier [opt] class-name
14458 decltype-specifier (C++11)
14459 identifier
14461 Returns a TYPE indicating the class to be initialized for the first
14462 production (and the second in C++11). Returns an IDENTIFIER_NODE
14463 indicating the data member to be initialized for the last production. */
14465 static tree
14466 cp_parser_mem_initializer_id (cp_parser* parser)
14468 bool global_scope_p;
14469 bool nested_name_specifier_p;
14470 bool template_p = false;
14471 tree id;
14473 cp_token *token = cp_lexer_peek_token (parser->lexer);
14475 /* `typename' is not allowed in this context ([temp.res]). */
14476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14478 error_at (token->location,
14479 "keyword %<typename%> not allowed in this context (a qualified "
14480 "member initializer is implicitly a type)");
14481 cp_lexer_consume_token (parser->lexer);
14483 /* Look for the optional `::' operator. */
14484 global_scope_p
14485 = (cp_parser_global_scope_opt (parser,
14486 /*current_scope_valid_p=*/false)
14487 != NULL_TREE);
14488 /* Look for the optional nested-name-specifier. The simplest way to
14489 implement:
14491 [temp.res]
14493 The keyword `typename' is not permitted in a base-specifier or
14494 mem-initializer; in these contexts a qualified name that
14495 depends on a template-parameter is implicitly assumed to be a
14496 type name.
14498 is to assume that we have seen the `typename' keyword at this
14499 point. */
14500 nested_name_specifier_p
14501 = (cp_parser_nested_name_specifier_opt (parser,
14502 /*typename_keyword_p=*/true,
14503 /*check_dependency_p=*/true,
14504 /*type_p=*/true,
14505 /*is_declaration=*/true)
14506 != NULL_TREE);
14507 if (nested_name_specifier_p)
14508 template_p = cp_parser_optional_template_keyword (parser);
14509 /* If there is a `::' operator or a nested-name-specifier, then we
14510 are definitely looking for a class-name. */
14511 if (global_scope_p || nested_name_specifier_p)
14512 return cp_parser_class_name (parser,
14513 /*typename_keyword_p=*/true,
14514 /*template_keyword_p=*/template_p,
14515 typename_type,
14516 /*check_dependency_p=*/true,
14517 /*class_head_p=*/false,
14518 /*is_declaration=*/true);
14519 /* Otherwise, we could also be looking for an ordinary identifier. */
14520 cp_parser_parse_tentatively (parser);
14521 if (cp_lexer_next_token_is_decltype (parser->lexer))
14522 /* Try a decltype-specifier. */
14523 id = cp_parser_decltype (parser);
14524 else
14525 /* Otherwise, try a class-name. */
14526 id = cp_parser_class_name (parser,
14527 /*typename_keyword_p=*/true,
14528 /*template_keyword_p=*/false,
14529 none_type,
14530 /*check_dependency_p=*/true,
14531 /*class_head_p=*/false,
14532 /*is_declaration=*/true);
14533 /* If we found one, we're done. */
14534 if (cp_parser_parse_definitely (parser))
14535 return id;
14536 /* Otherwise, look for an ordinary identifier. */
14537 return cp_parser_identifier (parser);
14540 /* Overloading [gram.over] */
14542 /* Parse an operator-function-id.
14544 operator-function-id:
14545 operator operator
14547 Returns an IDENTIFIER_NODE for the operator which is a
14548 human-readable spelling of the identifier, e.g., `operator +'. */
14550 static cp_expr
14551 cp_parser_operator_function_id (cp_parser* parser)
14553 /* Look for the `operator' keyword. */
14554 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14555 return error_mark_node;
14556 /* And then the name of the operator itself. */
14557 return cp_parser_operator (parser);
14560 /* Return an identifier node for a user-defined literal operator.
14561 The suffix identifier is chained to the operator name identifier. */
14563 tree
14564 cp_literal_operator_id (const char* name)
14566 tree identifier;
14567 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14568 + strlen (name) + 10);
14569 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14570 identifier = get_identifier (buffer);
14572 return identifier;
14575 /* Parse an operator.
14577 operator:
14578 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14579 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14580 || ++ -- , ->* -> () []
14582 GNU Extensions:
14584 operator:
14585 <? >? <?= >?=
14587 Returns an IDENTIFIER_NODE for the operator which is a
14588 human-readable spelling of the identifier, e.g., `operator +'. */
14590 static cp_expr
14591 cp_parser_operator (cp_parser* parser)
14593 tree id = NULL_TREE;
14594 cp_token *token;
14595 bool utf8 = false;
14597 /* Peek at the next token. */
14598 token = cp_lexer_peek_token (parser->lexer);
14600 location_t start_loc = token->location;
14602 /* Figure out which operator we have. */
14603 enum tree_code op = ERROR_MARK;
14604 bool assop = false;
14605 bool consumed = false;
14606 switch (token->type)
14608 case CPP_KEYWORD:
14610 /* The keyword should be either `new' or `delete'. */
14611 if (token->keyword == RID_NEW)
14612 op = NEW_EXPR;
14613 else if (token->keyword == RID_DELETE)
14614 op = DELETE_EXPR;
14615 else
14616 break;
14618 /* Consume the `new' or `delete' token. */
14619 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14621 /* Peek at the next token. */
14622 token = cp_lexer_peek_token (parser->lexer);
14623 /* If it's a `[' token then this is the array variant of the
14624 operator. */
14625 if (token->type == CPP_OPEN_SQUARE)
14627 /* Consume the `[' token. */
14628 cp_lexer_consume_token (parser->lexer);
14629 /* Look for the `]' token. */
14630 if (cp_token *close_token
14631 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14632 end_loc = close_token->location;
14633 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14635 start_loc = make_location (start_loc, start_loc, end_loc);
14636 consumed = true;
14637 break;
14640 case CPP_PLUS:
14641 op = PLUS_EXPR;
14642 break;
14644 case CPP_MINUS:
14645 op = MINUS_EXPR;
14646 break;
14648 case CPP_MULT:
14649 op = MULT_EXPR;
14650 break;
14652 case CPP_DIV:
14653 op = TRUNC_DIV_EXPR;
14654 break;
14656 case CPP_MOD:
14657 op = TRUNC_MOD_EXPR;
14658 break;
14660 case CPP_XOR:
14661 op = BIT_XOR_EXPR;
14662 break;
14664 case CPP_AND:
14665 op = BIT_AND_EXPR;
14666 break;
14668 case CPP_OR:
14669 op = BIT_IOR_EXPR;
14670 break;
14672 case CPP_COMPL:
14673 op = BIT_NOT_EXPR;
14674 break;
14676 case CPP_NOT:
14677 op = TRUTH_NOT_EXPR;
14678 break;
14680 case CPP_EQ:
14681 assop = true;
14682 op = NOP_EXPR;
14683 break;
14685 case CPP_LESS:
14686 op = LT_EXPR;
14687 break;
14689 case CPP_GREATER:
14690 op = GT_EXPR;
14691 break;
14693 case CPP_PLUS_EQ:
14694 assop = true;
14695 op = PLUS_EXPR;
14696 break;
14698 case CPP_MINUS_EQ:
14699 assop = true;
14700 op = MINUS_EXPR;
14701 break;
14703 case CPP_MULT_EQ:
14704 assop = true;
14705 op = MULT_EXPR;
14706 break;
14708 case CPP_DIV_EQ:
14709 assop = true;
14710 op = TRUNC_DIV_EXPR;
14711 break;
14713 case CPP_MOD_EQ:
14714 assop = true;
14715 op = TRUNC_MOD_EXPR;
14716 break;
14718 case CPP_XOR_EQ:
14719 assop = true;
14720 op = BIT_XOR_EXPR;
14721 break;
14723 case CPP_AND_EQ:
14724 assop = true;
14725 op = BIT_AND_EXPR;
14726 break;
14728 case CPP_OR_EQ:
14729 assop = true;
14730 op = BIT_IOR_EXPR;
14731 break;
14733 case CPP_LSHIFT:
14734 op = LSHIFT_EXPR;
14735 break;
14737 case CPP_RSHIFT:
14738 op = RSHIFT_EXPR;
14739 break;
14741 case CPP_LSHIFT_EQ:
14742 assop = true;
14743 op = LSHIFT_EXPR;
14744 break;
14746 case CPP_RSHIFT_EQ:
14747 assop = true;
14748 op = RSHIFT_EXPR;
14749 break;
14751 case CPP_EQ_EQ:
14752 op = EQ_EXPR;
14753 break;
14755 case CPP_NOT_EQ:
14756 op = NE_EXPR;
14757 break;
14759 case CPP_LESS_EQ:
14760 op = LE_EXPR;
14761 break;
14763 case CPP_GREATER_EQ:
14764 op = GE_EXPR;
14765 break;
14767 case CPP_AND_AND:
14768 op = TRUTH_ANDIF_EXPR;
14769 break;
14771 case CPP_OR_OR:
14772 op = TRUTH_ORIF_EXPR;
14773 break;
14775 case CPP_PLUS_PLUS:
14776 op = POSTINCREMENT_EXPR;
14777 break;
14779 case CPP_MINUS_MINUS:
14780 op = PREDECREMENT_EXPR;
14781 break;
14783 case CPP_COMMA:
14784 op = COMPOUND_EXPR;
14785 break;
14787 case CPP_DEREF_STAR:
14788 op = MEMBER_REF;
14789 break;
14791 case CPP_DEREF:
14792 op = COMPONENT_REF;
14793 break;
14795 case CPP_OPEN_PAREN:
14797 /* Consume the `('. */
14798 matching_parens parens;
14799 parens.consume_open (parser);
14800 /* Look for the matching `)'. */
14801 parens.require_close (parser);
14802 op = CALL_EXPR;
14803 consumed = true;
14804 break;
14807 case CPP_OPEN_SQUARE:
14808 /* Consume the `['. */
14809 cp_lexer_consume_token (parser->lexer);
14810 /* Look for the matching `]'. */
14811 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14812 op = ARRAY_REF;
14813 consumed = true;
14814 break;
14816 case CPP_UTF8STRING:
14817 case CPP_UTF8STRING_USERDEF:
14818 utf8 = true;
14819 /* FALLTHRU */
14820 case CPP_STRING:
14821 case CPP_WSTRING:
14822 case CPP_STRING16:
14823 case CPP_STRING32:
14824 case CPP_STRING_USERDEF:
14825 case CPP_WSTRING_USERDEF:
14826 case CPP_STRING16_USERDEF:
14827 case CPP_STRING32_USERDEF:
14829 tree str, string_tree;
14830 int sz, len;
14832 if (cxx_dialect == cxx98)
14833 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14835 /* Consume the string. */
14836 str = cp_parser_string_literal (parser, /*translate=*/true,
14837 /*wide_ok=*/true, /*lookup_udlit=*/false);
14838 if (str == error_mark_node)
14839 return error_mark_node;
14840 else if (TREE_CODE (str) == USERDEF_LITERAL)
14842 string_tree = USERDEF_LITERAL_VALUE (str);
14843 id = USERDEF_LITERAL_SUFFIX_ID (str);
14845 else
14847 string_tree = str;
14848 /* Look for the suffix identifier. */
14849 token = cp_lexer_peek_token (parser->lexer);
14850 if (token->type == CPP_NAME)
14851 id = cp_parser_identifier (parser);
14852 else if (token->type == CPP_KEYWORD)
14854 error ("unexpected keyword;"
14855 " remove space between quotes and suffix identifier");
14856 return error_mark_node;
14858 else
14860 error ("expected suffix identifier");
14861 return error_mark_node;
14864 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14865 (TREE_TYPE (TREE_TYPE (string_tree))));
14866 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14867 if (len != 0)
14869 error ("expected empty string after %<operator%> keyword");
14870 return error_mark_node;
14872 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14873 != char_type_node)
14875 error ("invalid encoding prefix in literal operator");
14876 return error_mark_node;
14878 if (id != error_mark_node)
14880 const char *name = IDENTIFIER_POINTER (id);
14881 id = cp_literal_operator_id (name);
14883 return id;
14886 default:
14887 /* Anything else is an error. */
14888 break;
14891 /* If we have selected an identifier, we need to consume the
14892 operator token. */
14893 if (op != ERROR_MARK)
14895 id = ovl_op_identifier (assop, op);
14896 if (!consumed)
14897 cp_lexer_consume_token (parser->lexer);
14899 /* Otherwise, no valid operator name was present. */
14900 else
14902 cp_parser_error (parser, "expected operator");
14903 id = error_mark_node;
14906 return cp_expr (id, start_loc);
14909 /* Parse a template-declaration.
14911 template-declaration:
14912 export [opt] template < template-parameter-list > declaration
14914 If MEMBER_P is TRUE, this template-declaration occurs within a
14915 class-specifier.
14917 The grammar rule given by the standard isn't correct. What
14918 is really meant is:
14920 template-declaration:
14921 export [opt] template-parameter-list-seq
14922 decl-specifier-seq [opt] init-declarator [opt] ;
14923 export [opt] template-parameter-list-seq
14924 function-definition
14926 template-parameter-list-seq:
14927 template-parameter-list-seq [opt]
14928 template < template-parameter-list >
14930 Concept Extensions:
14932 template-parameter-list-seq:
14933 template < template-parameter-list > requires-clause [opt]
14935 requires-clause:
14936 requires logical-or-expression */
14938 static void
14939 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14941 /* Check for `export'. */
14942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14944 /* Consume the `export' token. */
14945 cp_lexer_consume_token (parser->lexer);
14946 /* Warn that we do not support `export'. */
14947 warning (0, "keyword %<export%> not implemented, and will be ignored");
14950 cp_parser_template_declaration_after_export (parser, member_p);
14953 /* Parse a template-parameter-list.
14955 template-parameter-list:
14956 template-parameter
14957 template-parameter-list , template-parameter
14959 Returns a TREE_LIST. Each node represents a template parameter.
14960 The nodes are connected via their TREE_CHAINs. */
14962 static tree
14963 cp_parser_template_parameter_list (cp_parser* parser)
14965 tree parameter_list = NULL_TREE;
14967 begin_template_parm_list ();
14969 /* The loop below parses the template parms. We first need to know
14970 the total number of template parms to be able to compute proper
14971 canonical types of each dependent type. So after the loop, when
14972 we know the total number of template parms,
14973 end_template_parm_list computes the proper canonical types and
14974 fixes up the dependent types accordingly. */
14975 while (true)
14977 tree parameter;
14978 bool is_non_type;
14979 bool is_parameter_pack;
14980 location_t parm_loc;
14982 /* Parse the template-parameter. */
14983 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14984 parameter = cp_parser_template_parameter (parser,
14985 &is_non_type,
14986 &is_parameter_pack);
14987 /* Add it to the list. */
14988 if (parameter != error_mark_node)
14989 parameter_list = process_template_parm (parameter_list,
14990 parm_loc,
14991 parameter,
14992 is_non_type,
14993 is_parameter_pack);
14994 else
14996 tree err_parm = build_tree_list (parameter, parameter);
14997 parameter_list = chainon (parameter_list, err_parm);
15000 /* If the next token is not a `,', we're done. */
15001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15002 break;
15003 /* Otherwise, consume the `,' token. */
15004 cp_lexer_consume_token (parser->lexer);
15007 return end_template_parm_list (parameter_list);
15010 /* Parse a introduction-list.
15012 introduction-list:
15013 introduced-parameter
15014 introduction-list , introduced-parameter
15016 introduced-parameter:
15017 ...[opt] identifier
15019 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15020 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15021 WILDCARD_DECL will also have DECL_NAME set and token location in
15022 DECL_SOURCE_LOCATION. */
15024 static tree
15025 cp_parser_introduction_list (cp_parser *parser)
15027 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15029 while (true)
15031 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15032 if (is_pack)
15033 cp_lexer_consume_token (parser->lexer);
15035 /* Build placeholder. */
15036 tree parm = build_nt (WILDCARD_DECL);
15037 DECL_SOURCE_LOCATION (parm)
15038 = cp_lexer_peek_token (parser->lexer)->location;
15039 DECL_NAME (parm) = cp_parser_identifier (parser);
15040 WILDCARD_PACK_P (parm) = is_pack;
15041 vec_safe_push (introduction_vec, parm);
15043 /* If the next token is not a `,', we're done. */
15044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15045 break;
15046 /* Otherwise, consume the `,' token. */
15047 cp_lexer_consume_token (parser->lexer);
15050 /* Convert the vec into a TREE_VEC. */
15051 tree introduction_list = make_tree_vec (introduction_vec->length ());
15052 unsigned int n;
15053 tree parm;
15054 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15055 TREE_VEC_ELT (introduction_list, n) = parm;
15057 release_tree_vector (introduction_vec);
15058 return introduction_list;
15061 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15062 is an abstract declarator. */
15064 static inline cp_declarator*
15065 get_id_declarator (cp_declarator *declarator)
15067 cp_declarator *d = declarator;
15068 while (d && d->kind != cdk_id)
15069 d = d->declarator;
15070 return d;
15073 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15074 is an abstract declarator. */
15076 static inline tree
15077 get_unqualified_id (cp_declarator *declarator)
15079 declarator = get_id_declarator (declarator);
15080 if (declarator)
15081 return declarator->u.id.unqualified_name;
15082 else
15083 return NULL_TREE;
15086 /* Returns true if DECL represents a constrained-parameter. */
15088 static inline bool
15089 is_constrained_parameter (tree decl)
15091 return (decl
15092 && TREE_CODE (decl) == TYPE_DECL
15093 && CONSTRAINED_PARM_CONCEPT (decl)
15094 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15097 /* Returns true if PARM declares a constrained-parameter. */
15099 static inline bool
15100 is_constrained_parameter (cp_parameter_declarator *parm)
15102 return is_constrained_parameter (parm->decl_specifiers.type);
15105 /* Check that the type parameter is only a declarator-id, and that its
15106 type is not cv-qualified. */
15108 bool
15109 cp_parser_check_constrained_type_parm (cp_parser *parser,
15110 cp_parameter_declarator *parm)
15112 if (!parm->declarator)
15113 return true;
15115 if (parm->declarator->kind != cdk_id)
15117 cp_parser_error (parser, "invalid constrained type parameter");
15118 return false;
15121 /* Don't allow cv-qualified type parameters. */
15122 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15123 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15125 cp_parser_error (parser, "cv-qualified type parameter");
15126 return false;
15129 return true;
15132 /* Finish parsing/processing a template type parameter and checking
15133 various restrictions. */
15135 static inline tree
15136 cp_parser_constrained_type_template_parm (cp_parser *parser,
15137 tree id,
15138 cp_parameter_declarator* parmdecl)
15140 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15141 return finish_template_type_parm (class_type_node, id);
15142 else
15143 return error_mark_node;
15146 static tree
15147 finish_constrained_template_template_parm (tree proto, tree id)
15149 /* FIXME: This should probably be copied, and we may need to adjust
15150 the template parameter depths. */
15151 tree saved_parms = current_template_parms;
15152 begin_template_parm_list ();
15153 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15154 end_template_parm_list ();
15156 tree parm = finish_template_template_parm (class_type_node, id);
15157 current_template_parms = saved_parms;
15159 return parm;
15162 /* Finish parsing/processing a template template parameter by borrowing
15163 the template parameter list from the prototype parameter. */
15165 static tree
15166 cp_parser_constrained_template_template_parm (cp_parser *parser,
15167 tree proto,
15168 tree id,
15169 cp_parameter_declarator *parmdecl)
15171 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15172 return error_mark_node;
15173 return finish_constrained_template_template_parm (proto, id);
15176 /* Create a new non-type template parameter from the given PARM
15177 declarator. */
15179 static tree
15180 constrained_non_type_template_parm (bool *is_non_type,
15181 cp_parameter_declarator *parm)
15183 *is_non_type = true;
15184 cp_declarator *decl = parm->declarator;
15185 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15186 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15187 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15190 /* Build a constrained template parameter based on the PARMDECL
15191 declarator. The type of PARMDECL is the constrained type, which
15192 refers to the prototype template parameter that ultimately
15193 specifies the type of the declared parameter. */
15195 static tree
15196 finish_constrained_parameter (cp_parser *parser,
15197 cp_parameter_declarator *parmdecl,
15198 bool *is_non_type,
15199 bool *is_parameter_pack)
15201 tree decl = parmdecl->decl_specifiers.type;
15202 tree id = get_unqualified_id (parmdecl->declarator);
15203 tree def = parmdecl->default_argument;
15204 tree proto = DECL_INITIAL (decl);
15206 /* A template parameter constrained by a variadic concept shall also
15207 be declared as a template parameter pack. */
15208 bool is_variadic = template_parameter_pack_p (proto);
15209 if (is_variadic && !*is_parameter_pack)
15210 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15212 /* Build the parameter. Return an error if the declarator was invalid. */
15213 tree parm;
15214 if (TREE_CODE (proto) == TYPE_DECL)
15215 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15216 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15217 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15218 parmdecl);
15219 else
15220 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15221 if (parm == error_mark_node)
15222 return error_mark_node;
15224 /* Finish the parameter decl and create a node attaching the
15225 default argument and constraint. */
15226 parm = build_tree_list (def, parm);
15227 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15229 return parm;
15232 /* Returns true if the parsed type actually represents the declaration
15233 of a type template-parameter. */
15235 static inline bool
15236 declares_constrained_type_template_parameter (tree type)
15238 return (is_constrained_parameter (type)
15239 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15243 /* Returns true if the parsed type actually represents the declaration of
15244 a template template-parameter. */
15246 static bool
15247 declares_constrained_template_template_parameter (tree type)
15249 return (is_constrained_parameter (type)
15250 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15253 /* Parse a default argument for a type template-parameter.
15254 Note that diagnostics are handled in cp_parser_template_parameter. */
15256 static tree
15257 cp_parser_default_type_template_argument (cp_parser *parser)
15259 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15261 /* Consume the `=' token. */
15262 cp_lexer_consume_token (parser->lexer);
15264 cp_token *token = cp_lexer_peek_token (parser->lexer);
15266 /* Parse the default-argument. */
15267 push_deferring_access_checks (dk_no_deferred);
15268 tree default_argument = cp_parser_type_id (parser);
15269 pop_deferring_access_checks ();
15271 if (flag_concepts && type_uses_auto (default_argument))
15273 error_at (token->location,
15274 "invalid use of %<auto%> in default template argument");
15275 return error_mark_node;
15278 return default_argument;
15281 /* Parse a default argument for a template template-parameter. */
15283 static tree
15284 cp_parser_default_template_template_argument (cp_parser *parser)
15286 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15288 bool is_template;
15290 /* Consume the `='. */
15291 cp_lexer_consume_token (parser->lexer);
15292 /* Parse the id-expression. */
15293 push_deferring_access_checks (dk_no_deferred);
15294 /* save token before parsing the id-expression, for error
15295 reporting */
15296 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15297 tree default_argument
15298 = cp_parser_id_expression (parser,
15299 /*template_keyword_p=*/false,
15300 /*check_dependency_p=*/true,
15301 /*template_p=*/&is_template,
15302 /*declarator_p=*/false,
15303 /*optional_p=*/false);
15304 if (TREE_CODE (default_argument) == TYPE_DECL)
15305 /* If the id-expression was a template-id that refers to
15306 a template-class, we already have the declaration here,
15307 so no further lookup is needed. */
15309 else
15310 /* Look up the name. */
15311 default_argument
15312 = cp_parser_lookup_name (parser, default_argument,
15313 none_type,
15314 /*is_template=*/is_template,
15315 /*is_namespace=*/false,
15316 /*check_dependency=*/true,
15317 /*ambiguous_decls=*/NULL,
15318 token->location);
15319 /* See if the default argument is valid. */
15320 default_argument = check_template_template_default_arg (default_argument);
15321 pop_deferring_access_checks ();
15322 return default_argument;
15325 /* Parse a template-parameter.
15327 template-parameter:
15328 type-parameter
15329 parameter-declaration
15331 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15332 the parameter. The TREE_PURPOSE is the default value, if any.
15333 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15334 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15335 set to true iff this parameter is a parameter pack. */
15337 static tree
15338 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15339 bool *is_parameter_pack)
15341 cp_token *token;
15342 cp_parameter_declarator *parameter_declarator;
15343 tree parm;
15345 /* Assume it is a type parameter or a template parameter. */
15346 *is_non_type = false;
15347 /* Assume it not a parameter pack. */
15348 *is_parameter_pack = false;
15349 /* Peek at the next token. */
15350 token = cp_lexer_peek_token (parser->lexer);
15351 /* If it is `template', we have a type-parameter. */
15352 if (token->keyword == RID_TEMPLATE)
15353 return cp_parser_type_parameter (parser, is_parameter_pack);
15354 /* If it is `class' or `typename' we do not know yet whether it is a
15355 type parameter or a non-type parameter. Consider:
15357 template <typename T, typename T::X X> ...
15361 template <class C, class D*> ...
15363 Here, the first parameter is a type parameter, and the second is
15364 a non-type parameter. We can tell by looking at the token after
15365 the identifier -- if it is a `,', `=', or `>' then we have a type
15366 parameter. */
15367 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15369 /* Peek at the token after `class' or `typename'. */
15370 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15371 /* If it's an ellipsis, we have a template type parameter
15372 pack. */
15373 if (token->type == CPP_ELLIPSIS)
15374 return cp_parser_type_parameter (parser, is_parameter_pack);
15375 /* If it's an identifier, skip it. */
15376 if (token->type == CPP_NAME)
15377 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15378 /* Now, see if the token looks like the end of a template
15379 parameter. */
15380 if (token->type == CPP_COMMA
15381 || token->type == CPP_EQ
15382 || token->type == CPP_GREATER)
15383 return cp_parser_type_parameter (parser, is_parameter_pack);
15386 /* Otherwise, it is a non-type parameter or a constrained parameter.
15388 [temp.param]
15390 When parsing a default template-argument for a non-type
15391 template-parameter, the first non-nested `>' is taken as the end
15392 of the template parameter-list rather than a greater-than
15393 operator. */
15394 parameter_declarator
15395 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15396 /*parenthesized_p=*/NULL);
15398 if (!parameter_declarator)
15399 return error_mark_node;
15401 /* If the parameter declaration is marked as a parameter pack, set
15402 *IS_PARAMETER_PACK to notify the caller. */
15403 if (parameter_declarator->template_parameter_pack_p)
15404 *is_parameter_pack = true;
15406 if (parameter_declarator->default_argument)
15408 /* Can happen in some cases of erroneous input (c++/34892). */
15409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15410 /* Consume the `...' for better error recovery. */
15411 cp_lexer_consume_token (parser->lexer);
15414 // The parameter may have been constrained.
15415 if (is_constrained_parameter (parameter_declarator))
15416 return finish_constrained_parameter (parser,
15417 parameter_declarator,
15418 is_non_type,
15419 is_parameter_pack);
15421 // Now we're sure that the parameter is a non-type parameter.
15422 *is_non_type = true;
15424 parm = grokdeclarator (parameter_declarator->declarator,
15425 &parameter_declarator->decl_specifiers,
15426 TPARM, /*initialized=*/0,
15427 /*attrlist=*/NULL);
15428 if (parm == error_mark_node)
15429 return error_mark_node;
15431 return build_tree_list (parameter_declarator->default_argument, parm);
15434 /* Parse a type-parameter.
15436 type-parameter:
15437 class identifier [opt]
15438 class identifier [opt] = type-id
15439 typename identifier [opt]
15440 typename identifier [opt] = type-id
15441 template < template-parameter-list > class identifier [opt]
15442 template < template-parameter-list > class identifier [opt]
15443 = id-expression
15445 GNU Extension (variadic templates):
15447 type-parameter:
15448 class ... identifier [opt]
15449 typename ... identifier [opt]
15451 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15452 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15453 the declaration of the parameter.
15455 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15457 static tree
15458 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15460 cp_token *token;
15461 tree parameter;
15463 /* Look for a keyword to tell us what kind of parameter this is. */
15464 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15465 if (!token)
15466 return error_mark_node;
15468 switch (token->keyword)
15470 case RID_CLASS:
15471 case RID_TYPENAME:
15473 tree identifier;
15474 tree default_argument;
15476 /* If the next token is an ellipsis, we have a template
15477 argument pack. */
15478 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15480 /* Consume the `...' token. */
15481 cp_lexer_consume_token (parser->lexer);
15482 maybe_warn_variadic_templates ();
15484 *is_parameter_pack = true;
15487 /* If the next token is an identifier, then it names the
15488 parameter. */
15489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15490 identifier = cp_parser_identifier (parser);
15491 else
15492 identifier = NULL_TREE;
15494 /* Create the parameter. */
15495 parameter = finish_template_type_parm (class_type_node, identifier);
15497 /* If the next token is an `=', we have a default argument. */
15498 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15500 default_argument
15501 = cp_parser_default_type_template_argument (parser);
15503 /* Template parameter packs cannot have default
15504 arguments. */
15505 if (*is_parameter_pack)
15507 if (identifier)
15508 error_at (token->location,
15509 "template parameter pack %qD cannot have a "
15510 "default argument", identifier);
15511 else
15512 error_at (token->location,
15513 "template parameter packs cannot have "
15514 "default arguments");
15515 default_argument = NULL_TREE;
15517 else if (check_for_bare_parameter_packs (default_argument))
15518 default_argument = error_mark_node;
15520 else
15521 default_argument = NULL_TREE;
15523 /* Create the combined representation of the parameter and the
15524 default argument. */
15525 parameter = build_tree_list (default_argument, parameter);
15527 break;
15529 case RID_TEMPLATE:
15531 tree identifier;
15532 tree default_argument;
15534 /* Look for the `<'. */
15535 cp_parser_require (parser, CPP_LESS, RT_LESS);
15536 /* Parse the template-parameter-list. */
15537 cp_parser_template_parameter_list (parser);
15538 /* Look for the `>'. */
15539 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15541 // If template requirements are present, parse them.
15542 if (flag_concepts)
15544 tree reqs = get_shorthand_constraints (current_template_parms);
15545 if (tree r = cp_parser_requires_clause_opt (parser))
15546 reqs = conjoin_constraints (reqs, normalize_expression (r));
15547 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15550 /* Look for the `class' or 'typename' keywords. */
15551 cp_parser_type_parameter_key (parser);
15552 /* If the next token is an ellipsis, we have a template
15553 argument pack. */
15554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15556 /* Consume the `...' token. */
15557 cp_lexer_consume_token (parser->lexer);
15558 maybe_warn_variadic_templates ();
15560 *is_parameter_pack = true;
15562 /* If the next token is an `=', then there is a
15563 default-argument. If the next token is a `>', we are at
15564 the end of the parameter-list. If the next token is a `,',
15565 then we are at the end of this parameter. */
15566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15567 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15568 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15570 identifier = cp_parser_identifier (parser);
15571 /* Treat invalid names as if the parameter were nameless. */
15572 if (identifier == error_mark_node)
15573 identifier = NULL_TREE;
15575 else
15576 identifier = NULL_TREE;
15578 /* Create the template parameter. */
15579 parameter = finish_template_template_parm (class_type_node,
15580 identifier);
15582 /* If the next token is an `=', then there is a
15583 default-argument. */
15584 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15586 default_argument
15587 = cp_parser_default_template_template_argument (parser);
15589 /* Template parameter packs cannot have default
15590 arguments. */
15591 if (*is_parameter_pack)
15593 if (identifier)
15594 error_at (token->location,
15595 "template parameter pack %qD cannot "
15596 "have a default argument",
15597 identifier);
15598 else
15599 error_at (token->location, "template parameter packs cannot "
15600 "have default arguments");
15601 default_argument = NULL_TREE;
15604 else
15605 default_argument = NULL_TREE;
15607 /* Create the combined representation of the parameter and the
15608 default argument. */
15609 parameter = build_tree_list (default_argument, parameter);
15611 break;
15613 default:
15614 gcc_unreachable ();
15615 break;
15618 return parameter;
15621 /* Parse a template-id.
15623 template-id:
15624 template-name < template-argument-list [opt] >
15626 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15627 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15628 returned. Otherwise, if the template-name names a function, or set
15629 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15630 names a class, returns a TYPE_DECL for the specialization.
15632 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15633 uninstantiated templates. */
15635 static tree
15636 cp_parser_template_id (cp_parser *parser,
15637 bool template_keyword_p,
15638 bool check_dependency_p,
15639 enum tag_types tag_type,
15640 bool is_declaration)
15642 tree templ;
15643 tree arguments;
15644 tree template_id;
15645 cp_token_position start_of_id = 0;
15646 cp_token *next_token = NULL, *next_token_2 = NULL;
15647 bool is_identifier;
15649 /* If the next token corresponds to a template-id, there is no need
15650 to reparse it. */
15651 cp_token *token = cp_lexer_peek_token (parser->lexer);
15652 if (token->type == CPP_TEMPLATE_ID)
15654 cp_lexer_consume_token (parser->lexer);
15655 return saved_checks_value (token->u.tree_check_value);
15658 /* Avoid performing name lookup if there is no possibility of
15659 finding a template-id. */
15660 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15661 || (token->type == CPP_NAME
15662 && !cp_parser_nth_token_starts_template_argument_list_p
15663 (parser, 2)))
15665 cp_parser_error (parser, "expected template-id");
15666 return error_mark_node;
15669 /* Remember where the template-id starts. */
15670 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15671 start_of_id = cp_lexer_token_position (parser->lexer, false);
15673 push_deferring_access_checks (dk_deferred);
15675 /* Parse the template-name. */
15676 is_identifier = false;
15677 templ = cp_parser_template_name (parser, template_keyword_p,
15678 check_dependency_p,
15679 is_declaration,
15680 tag_type,
15681 &is_identifier);
15682 if (templ == error_mark_node || is_identifier)
15684 pop_deferring_access_checks ();
15685 return templ;
15688 /* Since we're going to preserve any side-effects from this parse, set up a
15689 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15690 in the template arguments. */
15691 tentative_firewall firewall (parser);
15693 /* If we find the sequence `[:' after a template-name, it's probably
15694 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15695 parse correctly the argument list. */
15696 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15697 == CPP_OPEN_SQUARE)
15698 && next_token->flags & DIGRAPH
15699 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15700 == CPP_COLON)
15701 && !(next_token_2->flags & PREV_WHITE))
15703 cp_parser_parse_tentatively (parser);
15704 /* Change `:' into `::'. */
15705 next_token_2->type = CPP_SCOPE;
15706 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15707 CPP_LESS. */
15708 cp_lexer_consume_token (parser->lexer);
15710 /* Parse the arguments. */
15711 arguments = cp_parser_enclosed_template_argument_list (parser);
15712 if (!cp_parser_parse_definitely (parser))
15714 /* If we couldn't parse an argument list, then we revert our changes
15715 and return simply an error. Maybe this is not a template-id
15716 after all. */
15717 next_token_2->type = CPP_COLON;
15718 cp_parser_error (parser, "expected %<<%>");
15719 pop_deferring_access_checks ();
15720 return error_mark_node;
15722 /* Otherwise, emit an error about the invalid digraph, but continue
15723 parsing because we got our argument list. */
15724 if (permerror (next_token->location,
15725 "%<<::%> cannot begin a template-argument list"))
15727 static bool hint = false;
15728 inform (next_token->location,
15729 "%<<:%> is an alternate spelling for %<[%>."
15730 " Insert whitespace between %<<%> and %<::%>");
15731 if (!hint && !flag_permissive)
15733 inform (next_token->location, "(if you use %<-fpermissive%> "
15734 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15735 "accept your code)");
15736 hint = true;
15740 else
15742 /* Look for the `<' that starts the template-argument-list. */
15743 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15745 pop_deferring_access_checks ();
15746 return error_mark_node;
15748 /* Parse the arguments. */
15749 arguments = cp_parser_enclosed_template_argument_list (parser);
15752 /* Set the location to be of the form:
15753 template-name < template-argument-list [opt] >
15754 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15755 with caret == start at the start of the template-name,
15756 ranging until the closing '>'. */
15757 location_t finish_loc
15758 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15759 location_t combined_loc
15760 = make_location (token->location, token->location, finish_loc);
15762 /* Build a representation of the specialization. */
15763 if (identifier_p (templ))
15764 template_id = build_min_nt_loc (combined_loc,
15765 TEMPLATE_ID_EXPR,
15766 templ, arguments);
15767 else if (DECL_TYPE_TEMPLATE_P (templ)
15768 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15770 bool entering_scope;
15771 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15772 template (rather than some instantiation thereof) only if
15773 is not nested within some other construct. For example, in
15774 "template <typename T> void f(T) { A<T>::", A<T> is just an
15775 instantiation of A. */
15776 entering_scope = (template_parm_scope_p ()
15777 && cp_lexer_next_token_is (parser->lexer,
15778 CPP_SCOPE));
15779 template_id
15780 = finish_template_type (templ, arguments, entering_scope);
15782 /* A template-like identifier may be a partial concept id. */
15783 else if (flag_concepts
15784 && (template_id = (cp_parser_maybe_partial_concept_id
15785 (parser, templ, arguments))))
15786 return template_id;
15787 else if (variable_template_p (templ))
15789 template_id = lookup_template_variable (templ, arguments);
15790 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15791 SET_EXPR_LOCATION (template_id, combined_loc);
15793 else
15795 /* If it's not a class-template or a template-template, it should be
15796 a function-template. */
15797 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15798 || TREE_CODE (templ) == OVERLOAD
15799 || BASELINK_P (templ)));
15801 template_id = lookup_template_function (templ, arguments);
15802 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15803 SET_EXPR_LOCATION (template_id, combined_loc);
15806 /* If parsing tentatively, replace the sequence of tokens that makes
15807 up the template-id with a CPP_TEMPLATE_ID token. That way,
15808 should we re-parse the token stream, we will not have to repeat
15809 the effort required to do the parse, nor will we issue duplicate
15810 error messages about problems during instantiation of the
15811 template. */
15812 if (start_of_id
15813 /* Don't do this if we had a parse error in a declarator; re-parsing
15814 might succeed if a name changes meaning (60361). */
15815 && !(cp_parser_error_occurred (parser)
15816 && cp_parser_parsing_tentatively (parser)
15817 && parser->in_declarator_p))
15819 /* Reset the contents of the START_OF_ID token. */
15820 token->type = CPP_TEMPLATE_ID;
15821 token->location = combined_loc;
15823 /* We must mark the lookup as kept, so we don't throw it away on
15824 the first parse. */
15825 if (is_overloaded_fn (template_id))
15826 lookup_keep (get_fns (template_id), true);
15828 /* Retrieve any deferred checks. Do not pop this access checks yet
15829 so the memory will not be reclaimed during token replacing below. */
15830 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15831 token->u.tree_check_value->value = template_id;
15832 token->u.tree_check_value->checks = get_deferred_access_checks ();
15833 token->keyword = RID_MAX;
15835 /* Purge all subsequent tokens. */
15836 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15838 /* ??? Can we actually assume that, if template_id ==
15839 error_mark_node, we will have issued a diagnostic to the
15840 user, as opposed to simply marking the tentative parse as
15841 failed? */
15842 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15843 error_at (token->location, "parse error in template argument list");
15846 pop_to_parent_deferring_access_checks ();
15847 return template_id;
15850 /* Parse a template-name.
15852 template-name:
15853 identifier
15855 The standard should actually say:
15857 template-name:
15858 identifier
15859 operator-function-id
15861 A defect report has been filed about this issue.
15863 A conversion-function-id cannot be a template name because they cannot
15864 be part of a template-id. In fact, looking at this code:
15866 a.operator K<int>()
15868 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15869 It is impossible to call a templated conversion-function-id with an
15870 explicit argument list, since the only allowed template parameter is
15871 the type to which it is converting.
15873 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15874 `template' keyword, in a construction like:
15876 T::template f<3>()
15878 In that case `f' is taken to be a template-name, even though there
15879 is no way of knowing for sure.
15881 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15882 name refers to a set of overloaded functions, at least one of which
15883 is a template, or an IDENTIFIER_NODE with the name of the template,
15884 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15885 names are looked up inside uninstantiated templates. */
15887 static tree
15888 cp_parser_template_name (cp_parser* parser,
15889 bool template_keyword_p,
15890 bool check_dependency_p,
15891 bool is_declaration,
15892 enum tag_types tag_type,
15893 bool *is_identifier)
15895 tree identifier;
15896 tree decl;
15897 cp_token *token = cp_lexer_peek_token (parser->lexer);
15899 /* If the next token is `operator', then we have either an
15900 operator-function-id or a conversion-function-id. */
15901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15903 /* We don't know whether we're looking at an
15904 operator-function-id or a conversion-function-id. */
15905 cp_parser_parse_tentatively (parser);
15906 /* Try an operator-function-id. */
15907 identifier = cp_parser_operator_function_id (parser);
15908 /* If that didn't work, try a conversion-function-id. */
15909 if (!cp_parser_parse_definitely (parser))
15911 cp_parser_error (parser, "expected template-name");
15912 return error_mark_node;
15915 /* Look for the identifier. */
15916 else
15917 identifier = cp_parser_identifier (parser);
15919 /* If we didn't find an identifier, we don't have a template-id. */
15920 if (identifier == error_mark_node)
15921 return error_mark_node;
15923 /* If the name immediately followed the `template' keyword, then it
15924 is a template-name. However, if the next token is not `<', then
15925 we do not treat it as a template-name, since it is not being used
15926 as part of a template-id. This enables us to handle constructs
15927 like:
15929 template <typename T> struct S { S(); };
15930 template <typename T> S<T>::S();
15932 correctly. We would treat `S' as a template -- if it were `S<T>'
15933 -- but we do not if there is no `<'. */
15935 if (processing_template_decl
15936 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15938 /* In a declaration, in a dependent context, we pretend that the
15939 "template" keyword was present in order to improve error
15940 recovery. For example, given:
15942 template <typename T> void f(T::X<int>);
15944 we want to treat "X<int>" as a template-id. */
15945 if (is_declaration
15946 && !template_keyword_p
15947 && parser->scope && TYPE_P (parser->scope)
15948 && check_dependency_p
15949 && dependent_scope_p (parser->scope)
15950 /* Do not do this for dtors (or ctors), since they never
15951 need the template keyword before their name. */
15952 && !constructor_name_p (identifier, parser->scope))
15954 cp_token_position start = 0;
15956 /* Explain what went wrong. */
15957 error_at (token->location, "non-template %qD used as template",
15958 identifier);
15959 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15960 parser->scope, identifier);
15961 /* If parsing tentatively, find the location of the "<" token. */
15962 if (cp_parser_simulate_error (parser))
15963 start = cp_lexer_token_position (parser->lexer, true);
15964 /* Parse the template arguments so that we can issue error
15965 messages about them. */
15966 cp_lexer_consume_token (parser->lexer);
15967 cp_parser_enclosed_template_argument_list (parser);
15968 /* Skip tokens until we find a good place from which to
15969 continue parsing. */
15970 cp_parser_skip_to_closing_parenthesis (parser,
15971 /*recovering=*/true,
15972 /*or_comma=*/true,
15973 /*consume_paren=*/false);
15974 /* If parsing tentatively, permanently remove the
15975 template argument list. That will prevent duplicate
15976 error messages from being issued about the missing
15977 "template" keyword. */
15978 if (start)
15979 cp_lexer_purge_tokens_after (parser->lexer, start);
15980 if (is_identifier)
15981 *is_identifier = true;
15982 parser->context->object_type = NULL_TREE;
15983 return identifier;
15986 /* If the "template" keyword is present, then there is generally
15987 no point in doing name-lookup, so we just return IDENTIFIER.
15988 But, if the qualifying scope is non-dependent then we can
15989 (and must) do name-lookup normally. */
15990 if (template_keyword_p)
15992 tree scope = (parser->scope ? parser->scope
15993 : parser->context->object_type);
15994 if (scope && TYPE_P (scope)
15995 && (!CLASS_TYPE_P (scope)
15996 || (check_dependency_p && dependent_type_p (scope))))
15998 /* We're optimizing away the call to cp_parser_lookup_name, but
15999 we still need to do this. */
16000 parser->context->object_type = NULL_TREE;
16001 return identifier;
16006 /* Look up the name. */
16007 decl = cp_parser_lookup_name (parser, identifier,
16008 tag_type,
16009 /*is_template=*/true,
16010 /*is_namespace=*/false,
16011 check_dependency_p,
16012 /*ambiguous_decls=*/NULL,
16013 token->location);
16015 decl = strip_using_decl (decl);
16017 /* If DECL is a template, then the name was a template-name. */
16018 if (TREE_CODE (decl) == TEMPLATE_DECL)
16020 if (TREE_DEPRECATED (decl)
16021 && deprecated_state != DEPRECATED_SUPPRESS)
16022 warn_deprecated_use (decl, NULL_TREE);
16024 else
16026 /* The standard does not explicitly indicate whether a name that
16027 names a set of overloaded declarations, some of which are
16028 templates, is a template-name. However, such a name should
16029 be a template-name; otherwise, there is no way to form a
16030 template-id for the overloaded templates. */
16031 bool found = false;
16033 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16034 !found && iter; ++iter)
16035 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16036 found = true;
16038 if (!found)
16040 /* The name does not name a template. */
16041 cp_parser_error (parser, "expected template-name");
16042 return error_mark_node;
16046 /* If DECL is dependent, and refers to a function, then just return
16047 its name; we will look it up again during template instantiation. */
16048 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16050 tree scope = ovl_scope (decl);
16051 if (TYPE_P (scope) && dependent_type_p (scope))
16052 return identifier;
16055 return decl;
16058 /* Parse a template-argument-list.
16060 template-argument-list:
16061 template-argument ... [opt]
16062 template-argument-list , template-argument ... [opt]
16064 Returns a TREE_VEC containing the arguments. */
16066 static tree
16067 cp_parser_template_argument_list (cp_parser* parser)
16069 tree fixed_args[10];
16070 unsigned n_args = 0;
16071 unsigned alloced = 10;
16072 tree *arg_ary = fixed_args;
16073 tree vec;
16074 bool saved_in_template_argument_list_p;
16075 bool saved_ice_p;
16076 bool saved_non_ice_p;
16078 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16079 parser->in_template_argument_list_p = true;
16080 /* Even if the template-id appears in an integral
16081 constant-expression, the contents of the argument list do
16082 not. */
16083 saved_ice_p = parser->integral_constant_expression_p;
16084 parser->integral_constant_expression_p = false;
16085 saved_non_ice_p = parser->non_integral_constant_expression_p;
16086 parser->non_integral_constant_expression_p = false;
16088 /* Parse the arguments. */
16091 tree argument;
16093 if (n_args)
16094 /* Consume the comma. */
16095 cp_lexer_consume_token (parser->lexer);
16097 /* Parse the template-argument. */
16098 argument = cp_parser_template_argument (parser);
16100 /* If the next token is an ellipsis, we're expanding a template
16101 argument pack. */
16102 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16104 if (argument == error_mark_node)
16106 cp_token *token = cp_lexer_peek_token (parser->lexer);
16107 error_at (token->location,
16108 "expected parameter pack before %<...%>");
16110 /* Consume the `...' token. */
16111 cp_lexer_consume_token (parser->lexer);
16113 /* Make the argument into a TYPE_PACK_EXPANSION or
16114 EXPR_PACK_EXPANSION. */
16115 argument = make_pack_expansion (argument);
16118 if (n_args == alloced)
16120 alloced *= 2;
16122 if (arg_ary == fixed_args)
16124 arg_ary = XNEWVEC (tree, alloced);
16125 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16127 else
16128 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16130 arg_ary[n_args++] = argument;
16132 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16134 vec = make_tree_vec (n_args);
16136 while (n_args--)
16137 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16139 if (arg_ary != fixed_args)
16140 free (arg_ary);
16141 parser->non_integral_constant_expression_p = saved_non_ice_p;
16142 parser->integral_constant_expression_p = saved_ice_p;
16143 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16144 if (CHECKING_P)
16145 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16146 return vec;
16149 /* Parse a template-argument.
16151 template-argument:
16152 assignment-expression
16153 type-id
16154 id-expression
16156 The representation is that of an assignment-expression, type-id, or
16157 id-expression -- except that the qualified id-expression is
16158 evaluated, so that the value returned is either a DECL or an
16159 OVERLOAD.
16161 Although the standard says "assignment-expression", it forbids
16162 throw-expressions or assignments in the template argument.
16163 Therefore, we use "conditional-expression" instead. */
16165 static tree
16166 cp_parser_template_argument (cp_parser* parser)
16168 tree argument;
16169 bool template_p;
16170 bool address_p;
16171 bool maybe_type_id = false;
16172 cp_token *token = NULL, *argument_start_token = NULL;
16173 location_t loc = 0;
16174 cp_id_kind idk;
16176 /* There's really no way to know what we're looking at, so we just
16177 try each alternative in order.
16179 [temp.arg]
16181 In a template-argument, an ambiguity between a type-id and an
16182 expression is resolved to a type-id, regardless of the form of
16183 the corresponding template-parameter.
16185 Therefore, we try a type-id first. */
16186 cp_parser_parse_tentatively (parser);
16187 argument = cp_parser_template_type_arg (parser);
16188 /* If there was no error parsing the type-id but the next token is a
16189 '>>', our behavior depends on which dialect of C++ we're
16190 parsing. In C++98, we probably found a typo for '> >'. But there
16191 are type-id which are also valid expressions. For instance:
16193 struct X { int operator >> (int); };
16194 template <int V> struct Foo {};
16195 Foo<X () >> 5> r;
16197 Here 'X()' is a valid type-id of a function type, but the user just
16198 wanted to write the expression "X() >> 5". Thus, we remember that we
16199 found a valid type-id, but we still try to parse the argument as an
16200 expression to see what happens.
16202 In C++0x, the '>>' will be considered two separate '>'
16203 tokens. */
16204 if (!cp_parser_error_occurred (parser)
16205 && cxx_dialect == cxx98
16206 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16208 maybe_type_id = true;
16209 cp_parser_abort_tentative_parse (parser);
16211 else
16213 /* If the next token isn't a `,' or a `>', then this argument wasn't
16214 really finished. This means that the argument is not a valid
16215 type-id. */
16216 if (!cp_parser_next_token_ends_template_argument_p (parser))
16217 cp_parser_error (parser, "expected template-argument");
16218 /* If that worked, we're done. */
16219 if (cp_parser_parse_definitely (parser))
16220 return argument;
16222 /* We're still not sure what the argument will be. */
16223 cp_parser_parse_tentatively (parser);
16224 /* Try a template. */
16225 argument_start_token = cp_lexer_peek_token (parser->lexer);
16226 argument = cp_parser_id_expression (parser,
16227 /*template_keyword_p=*/false,
16228 /*check_dependency_p=*/true,
16229 &template_p,
16230 /*declarator_p=*/false,
16231 /*optional_p=*/false);
16232 /* If the next token isn't a `,' or a `>', then this argument wasn't
16233 really finished. */
16234 if (!cp_parser_next_token_ends_template_argument_p (parser))
16235 cp_parser_error (parser, "expected template-argument");
16236 if (!cp_parser_error_occurred (parser))
16238 /* Figure out what is being referred to. If the id-expression
16239 was for a class template specialization, then we will have a
16240 TYPE_DECL at this point. There is no need to do name lookup
16241 at this point in that case. */
16242 if (TREE_CODE (argument) != TYPE_DECL)
16243 argument = cp_parser_lookup_name (parser, argument,
16244 none_type,
16245 /*is_template=*/template_p,
16246 /*is_namespace=*/false,
16247 /*check_dependency=*/true,
16248 /*ambiguous_decls=*/NULL,
16249 argument_start_token->location);
16250 /* Handle a constrained-type-specifier for a non-type template
16251 parameter. */
16252 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16253 argument = decl;
16254 else if (TREE_CODE (argument) != TEMPLATE_DECL
16255 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16256 cp_parser_error (parser, "expected template-name");
16258 if (cp_parser_parse_definitely (parser))
16260 if (TREE_DEPRECATED (argument))
16261 warn_deprecated_use (argument, NULL_TREE);
16262 return argument;
16264 /* It must be a non-type argument. In C++17 any constant-expression is
16265 allowed. */
16266 if (cxx_dialect > cxx14)
16267 goto general_expr;
16269 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16271 -- an integral constant-expression of integral or enumeration
16272 type; or
16274 -- the name of a non-type template-parameter; or
16276 -- the name of an object or function with external linkage...
16278 -- the address of an object or function with external linkage...
16280 -- a pointer to member... */
16281 /* Look for a non-type template parameter. */
16282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16284 cp_parser_parse_tentatively (parser);
16285 argument = cp_parser_primary_expression (parser,
16286 /*address_p=*/false,
16287 /*cast_p=*/false,
16288 /*template_arg_p=*/true,
16289 &idk);
16290 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16291 || !cp_parser_next_token_ends_template_argument_p (parser))
16292 cp_parser_simulate_error (parser);
16293 if (cp_parser_parse_definitely (parser))
16294 return argument;
16297 /* If the next token is "&", the argument must be the address of an
16298 object or function with external linkage. */
16299 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16300 if (address_p)
16302 loc = cp_lexer_peek_token (parser->lexer)->location;
16303 cp_lexer_consume_token (parser->lexer);
16305 /* See if we might have an id-expression. */
16306 token = cp_lexer_peek_token (parser->lexer);
16307 if (token->type == CPP_NAME
16308 || token->keyword == RID_OPERATOR
16309 || token->type == CPP_SCOPE
16310 || token->type == CPP_TEMPLATE_ID
16311 || token->type == CPP_NESTED_NAME_SPECIFIER)
16313 cp_parser_parse_tentatively (parser);
16314 argument = cp_parser_primary_expression (parser,
16315 address_p,
16316 /*cast_p=*/false,
16317 /*template_arg_p=*/true,
16318 &idk);
16319 if (cp_parser_error_occurred (parser)
16320 || !cp_parser_next_token_ends_template_argument_p (parser))
16321 cp_parser_abort_tentative_parse (parser);
16322 else
16324 tree probe;
16326 if (INDIRECT_REF_P (argument))
16328 /* Strip the dereference temporarily. */
16329 gcc_assert (REFERENCE_REF_P (argument));
16330 argument = TREE_OPERAND (argument, 0);
16333 /* If we're in a template, we represent a qualified-id referring
16334 to a static data member as a SCOPE_REF even if the scope isn't
16335 dependent so that we can check access control later. */
16336 probe = argument;
16337 if (TREE_CODE (probe) == SCOPE_REF)
16338 probe = TREE_OPERAND (probe, 1);
16339 if (VAR_P (probe))
16341 /* A variable without external linkage might still be a
16342 valid constant-expression, so no error is issued here
16343 if the external-linkage check fails. */
16344 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16345 cp_parser_simulate_error (parser);
16347 else if (is_overloaded_fn (argument))
16348 /* All overloaded functions are allowed; if the external
16349 linkage test does not pass, an error will be issued
16350 later. */
16352 else if (address_p
16353 && (TREE_CODE (argument) == OFFSET_REF
16354 || TREE_CODE (argument) == SCOPE_REF))
16355 /* A pointer-to-member. */
16357 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16359 else
16360 cp_parser_simulate_error (parser);
16362 if (cp_parser_parse_definitely (parser))
16364 if (address_p)
16365 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16366 tf_warning_or_error);
16367 else
16368 argument = convert_from_reference (argument);
16369 return argument;
16373 /* If the argument started with "&", there are no other valid
16374 alternatives at this point. */
16375 if (address_p)
16377 cp_parser_error (parser, "invalid non-type template argument");
16378 return error_mark_node;
16381 general_expr:
16382 /* If the argument wasn't successfully parsed as a type-id followed
16383 by '>>', the argument can only be a constant expression now.
16384 Otherwise, we try parsing the constant-expression tentatively,
16385 because the argument could really be a type-id. */
16386 if (maybe_type_id)
16387 cp_parser_parse_tentatively (parser);
16389 if (cxx_dialect <= cxx14)
16390 argument = cp_parser_constant_expression (parser);
16391 else
16393 /* With C++17 generalized non-type template arguments we need to handle
16394 lvalue constant expressions, too. */
16395 argument = cp_parser_assignment_expression (parser);
16396 require_potential_constant_expression (argument);
16399 if (!maybe_type_id)
16400 return argument;
16401 if (!cp_parser_next_token_ends_template_argument_p (parser))
16402 cp_parser_error (parser, "expected template-argument");
16403 if (cp_parser_parse_definitely (parser))
16404 return argument;
16405 /* We did our best to parse the argument as a non type-id, but that
16406 was the only alternative that matched (albeit with a '>' after
16407 it). We can assume it's just a typo from the user, and a
16408 diagnostic will then be issued. */
16409 return cp_parser_template_type_arg (parser);
16412 /* Parse an explicit-instantiation.
16414 explicit-instantiation:
16415 template declaration
16417 Although the standard says `declaration', what it really means is:
16419 explicit-instantiation:
16420 template decl-specifier-seq [opt] declarator [opt] ;
16422 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16423 supposed to be allowed. A defect report has been filed about this
16424 issue.
16426 GNU Extension:
16428 explicit-instantiation:
16429 storage-class-specifier template
16430 decl-specifier-seq [opt] declarator [opt] ;
16431 function-specifier template
16432 decl-specifier-seq [opt] declarator [opt] ; */
16434 static void
16435 cp_parser_explicit_instantiation (cp_parser* parser)
16437 int declares_class_or_enum;
16438 cp_decl_specifier_seq decl_specifiers;
16439 tree extension_specifier = NULL_TREE;
16441 timevar_push (TV_TEMPLATE_INST);
16443 /* Look for an (optional) storage-class-specifier or
16444 function-specifier. */
16445 if (cp_parser_allow_gnu_extensions_p (parser))
16447 extension_specifier
16448 = cp_parser_storage_class_specifier_opt (parser);
16449 if (!extension_specifier)
16450 extension_specifier
16451 = cp_parser_function_specifier_opt (parser,
16452 /*decl_specs=*/NULL);
16455 /* Look for the `template' keyword. */
16456 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16457 /* Let the front end know that we are processing an explicit
16458 instantiation. */
16459 begin_explicit_instantiation ();
16460 /* [temp.explicit] says that we are supposed to ignore access
16461 control while processing explicit instantiation directives. */
16462 push_deferring_access_checks (dk_no_check);
16463 /* Parse a decl-specifier-seq. */
16464 cp_parser_decl_specifier_seq (parser,
16465 CP_PARSER_FLAGS_OPTIONAL,
16466 &decl_specifiers,
16467 &declares_class_or_enum);
16468 /* If there was exactly one decl-specifier, and it declared a class,
16469 and there's no declarator, then we have an explicit type
16470 instantiation. */
16471 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16473 tree type;
16475 type = check_tag_decl (&decl_specifiers,
16476 /*explicit_type_instantiation_p=*/true);
16477 /* Turn access control back on for names used during
16478 template instantiation. */
16479 pop_deferring_access_checks ();
16480 if (type)
16481 do_type_instantiation (type, extension_specifier,
16482 /*complain=*/tf_error);
16484 else
16486 cp_declarator *declarator;
16487 tree decl;
16489 /* Parse the declarator. */
16490 declarator
16491 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16492 /*ctor_dtor_or_conv_p=*/NULL,
16493 /*parenthesized_p=*/NULL,
16494 /*member_p=*/false,
16495 /*friend_p=*/false);
16496 if (declares_class_or_enum & 2)
16497 cp_parser_check_for_definition_in_return_type (declarator,
16498 decl_specifiers.type,
16499 decl_specifiers.locations[ds_type_spec]);
16500 if (declarator != cp_error_declarator)
16502 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16503 permerror (decl_specifiers.locations[ds_inline],
16504 "explicit instantiation shall not use"
16505 " %<inline%> specifier");
16506 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16507 permerror (decl_specifiers.locations[ds_constexpr],
16508 "explicit instantiation shall not use"
16509 " %<constexpr%> specifier");
16511 decl = grokdeclarator (declarator, &decl_specifiers,
16512 NORMAL, 0, &decl_specifiers.attributes);
16513 /* Turn access control back on for names used during
16514 template instantiation. */
16515 pop_deferring_access_checks ();
16516 /* Do the explicit instantiation. */
16517 do_decl_instantiation (decl, extension_specifier);
16519 else
16521 pop_deferring_access_checks ();
16522 /* Skip the body of the explicit instantiation. */
16523 cp_parser_skip_to_end_of_statement (parser);
16526 /* We're done with the instantiation. */
16527 end_explicit_instantiation ();
16529 cp_parser_consume_semicolon_at_end_of_statement (parser);
16531 timevar_pop (TV_TEMPLATE_INST);
16534 /* Parse an explicit-specialization.
16536 explicit-specialization:
16537 template < > declaration
16539 Although the standard says `declaration', what it really means is:
16541 explicit-specialization:
16542 template <> decl-specifier [opt] init-declarator [opt] ;
16543 template <> function-definition
16544 template <> explicit-specialization
16545 template <> template-declaration */
16547 static void
16548 cp_parser_explicit_specialization (cp_parser* parser)
16550 bool need_lang_pop;
16551 cp_token *token = cp_lexer_peek_token (parser->lexer);
16553 /* Look for the `template' keyword. */
16554 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16555 /* Look for the `<'. */
16556 cp_parser_require (parser, CPP_LESS, RT_LESS);
16557 /* Look for the `>'. */
16558 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16559 /* We have processed another parameter list. */
16560 ++parser->num_template_parameter_lists;
16561 /* [temp]
16563 A template ... explicit specialization ... shall not have C
16564 linkage. */
16565 if (current_lang_name == lang_name_c)
16567 error_at (token->location, "template specialization with C linkage");
16568 maybe_show_extern_c_location ();
16569 /* Give it C++ linkage to avoid confusing other parts of the
16570 front end. */
16571 push_lang_context (lang_name_cplusplus);
16572 need_lang_pop = true;
16574 else
16575 need_lang_pop = false;
16576 /* Let the front end know that we are beginning a specialization. */
16577 if (!begin_specialization ())
16579 end_specialization ();
16580 return;
16583 /* If the next keyword is `template', we need to figure out whether
16584 or not we're looking a template-declaration. */
16585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16587 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16589 cp_parser_template_declaration_after_export (parser,
16590 /*member_p=*/false);
16591 else
16592 cp_parser_explicit_specialization (parser);
16594 else
16595 /* Parse the dependent declaration. */
16596 cp_parser_single_declaration (parser,
16597 /*checks=*/NULL,
16598 /*member_p=*/false,
16599 /*explicit_specialization_p=*/true,
16600 /*friend_p=*/NULL);
16601 /* We're done with the specialization. */
16602 end_specialization ();
16603 /* For the erroneous case of a template with C linkage, we pushed an
16604 implicit C++ linkage scope; exit that scope now. */
16605 if (need_lang_pop)
16606 pop_lang_context ();
16607 /* We're done with this parameter list. */
16608 --parser->num_template_parameter_lists;
16611 /* Parse a type-specifier.
16613 type-specifier:
16614 simple-type-specifier
16615 class-specifier
16616 enum-specifier
16617 elaborated-type-specifier
16618 cv-qualifier
16620 GNU Extension:
16622 type-specifier:
16623 __complex__
16625 Returns a representation of the type-specifier. For a
16626 class-specifier, enum-specifier, or elaborated-type-specifier, a
16627 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16629 The parser flags FLAGS is used to control type-specifier parsing.
16631 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16632 in a decl-specifier-seq.
16634 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16635 class-specifier, enum-specifier, or elaborated-type-specifier, then
16636 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16637 if a type is declared; 2 if it is defined. Otherwise, it is set to
16638 zero.
16640 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16641 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16642 is set to FALSE. */
16644 static tree
16645 cp_parser_type_specifier (cp_parser* parser,
16646 cp_parser_flags flags,
16647 cp_decl_specifier_seq *decl_specs,
16648 bool is_declaration,
16649 int* declares_class_or_enum,
16650 bool* is_cv_qualifier)
16652 tree type_spec = NULL_TREE;
16653 cp_token *token;
16654 enum rid keyword;
16655 cp_decl_spec ds = ds_last;
16657 /* Assume this type-specifier does not declare a new type. */
16658 if (declares_class_or_enum)
16659 *declares_class_or_enum = 0;
16660 /* And that it does not specify a cv-qualifier. */
16661 if (is_cv_qualifier)
16662 *is_cv_qualifier = false;
16663 /* Peek at the next token. */
16664 token = cp_lexer_peek_token (parser->lexer);
16666 /* If we're looking at a keyword, we can use that to guide the
16667 production we choose. */
16668 keyword = token->keyword;
16669 switch (keyword)
16671 case RID_ENUM:
16672 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16673 goto elaborated_type_specifier;
16675 /* Look for the enum-specifier. */
16676 type_spec = cp_parser_enum_specifier (parser);
16677 /* If that worked, we're done. */
16678 if (type_spec)
16680 if (declares_class_or_enum)
16681 *declares_class_or_enum = 2;
16682 if (decl_specs)
16683 cp_parser_set_decl_spec_type (decl_specs,
16684 type_spec,
16685 token,
16686 /*type_definition_p=*/true);
16687 return type_spec;
16689 else
16690 goto elaborated_type_specifier;
16692 /* Any of these indicate either a class-specifier, or an
16693 elaborated-type-specifier. */
16694 case RID_CLASS:
16695 case RID_STRUCT:
16696 case RID_UNION:
16697 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16698 goto elaborated_type_specifier;
16700 /* Parse tentatively so that we can back up if we don't find a
16701 class-specifier. */
16702 cp_parser_parse_tentatively (parser);
16703 /* Look for the class-specifier. */
16704 type_spec = cp_parser_class_specifier (parser);
16705 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16706 /* If that worked, we're done. */
16707 if (cp_parser_parse_definitely (parser))
16709 if (declares_class_or_enum)
16710 *declares_class_or_enum = 2;
16711 if (decl_specs)
16712 cp_parser_set_decl_spec_type (decl_specs,
16713 type_spec,
16714 token,
16715 /*type_definition_p=*/true);
16716 return type_spec;
16719 /* Fall through. */
16720 elaborated_type_specifier:
16721 /* We're declaring (not defining) a class or enum. */
16722 if (declares_class_or_enum)
16723 *declares_class_or_enum = 1;
16725 /* Fall through. */
16726 case RID_TYPENAME:
16727 /* Look for an elaborated-type-specifier. */
16728 type_spec
16729 = (cp_parser_elaborated_type_specifier
16730 (parser,
16731 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16732 is_declaration));
16733 if (decl_specs)
16734 cp_parser_set_decl_spec_type (decl_specs,
16735 type_spec,
16736 token,
16737 /*type_definition_p=*/false);
16738 return type_spec;
16740 case RID_CONST:
16741 ds = ds_const;
16742 if (is_cv_qualifier)
16743 *is_cv_qualifier = true;
16744 break;
16746 case RID_VOLATILE:
16747 ds = ds_volatile;
16748 if (is_cv_qualifier)
16749 *is_cv_qualifier = true;
16750 break;
16752 case RID_RESTRICT:
16753 ds = ds_restrict;
16754 if (is_cv_qualifier)
16755 *is_cv_qualifier = true;
16756 break;
16758 case RID_COMPLEX:
16759 /* The `__complex__' keyword is a GNU extension. */
16760 ds = ds_complex;
16761 break;
16763 default:
16764 break;
16767 /* Handle simple keywords. */
16768 if (ds != ds_last)
16770 if (decl_specs)
16772 set_and_check_decl_spec_loc (decl_specs, ds, token);
16773 decl_specs->any_specifiers_p = true;
16775 return cp_lexer_consume_token (parser->lexer)->u.value;
16778 /* If we do not already have a type-specifier, assume we are looking
16779 at a simple-type-specifier. */
16780 type_spec = cp_parser_simple_type_specifier (parser,
16781 decl_specs,
16782 flags);
16784 /* If we didn't find a type-specifier, and a type-specifier was not
16785 optional in this context, issue an error message. */
16786 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16788 cp_parser_error (parser, "expected type specifier");
16789 return error_mark_node;
16792 return type_spec;
16795 /* Parse a simple-type-specifier.
16797 simple-type-specifier:
16798 :: [opt] nested-name-specifier [opt] type-name
16799 :: [opt] nested-name-specifier template template-id
16800 char
16801 wchar_t
16802 bool
16803 short
16805 long
16806 signed
16807 unsigned
16808 float
16809 double
16810 void
16812 C++11 Extension:
16814 simple-type-specifier:
16815 auto
16816 decltype ( expression )
16817 char16_t
16818 char32_t
16819 __underlying_type ( type-id )
16821 C++17 extension:
16823 nested-name-specifier(opt) template-name
16825 GNU Extension:
16827 simple-type-specifier:
16828 __int128
16829 __typeof__ unary-expression
16830 __typeof__ ( type-id )
16831 __typeof__ ( type-id ) { initializer-list , [opt] }
16833 Concepts Extension:
16835 simple-type-specifier:
16836 constrained-type-specifier
16838 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16839 appropriately updated. */
16841 static tree
16842 cp_parser_simple_type_specifier (cp_parser* parser,
16843 cp_decl_specifier_seq *decl_specs,
16844 cp_parser_flags flags)
16846 tree type = NULL_TREE;
16847 cp_token *token;
16848 int idx;
16850 /* Peek at the next token. */
16851 token = cp_lexer_peek_token (parser->lexer);
16853 /* If we're looking at a keyword, things are easy. */
16854 switch (token->keyword)
16856 case RID_CHAR:
16857 if (decl_specs)
16858 decl_specs->explicit_char_p = true;
16859 type = char_type_node;
16860 break;
16861 case RID_CHAR16:
16862 type = char16_type_node;
16863 break;
16864 case RID_CHAR32:
16865 type = char32_type_node;
16866 break;
16867 case RID_WCHAR:
16868 type = wchar_type_node;
16869 break;
16870 case RID_BOOL:
16871 type = boolean_type_node;
16872 break;
16873 case RID_SHORT:
16874 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16875 type = short_integer_type_node;
16876 break;
16877 case RID_INT:
16878 if (decl_specs)
16879 decl_specs->explicit_int_p = true;
16880 type = integer_type_node;
16881 break;
16882 case RID_INT_N_0:
16883 case RID_INT_N_1:
16884 case RID_INT_N_2:
16885 case RID_INT_N_3:
16886 idx = token->keyword - RID_INT_N_0;
16887 if (! int_n_enabled_p [idx])
16888 break;
16889 if (decl_specs)
16891 decl_specs->explicit_intN_p = true;
16892 decl_specs->int_n_idx = idx;
16894 type = int_n_trees [idx].signed_type;
16895 break;
16896 case RID_LONG:
16897 if (decl_specs)
16898 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16899 type = long_integer_type_node;
16900 break;
16901 case RID_SIGNED:
16902 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16903 type = integer_type_node;
16904 break;
16905 case RID_UNSIGNED:
16906 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16907 type = unsigned_type_node;
16908 break;
16909 case RID_FLOAT:
16910 type = float_type_node;
16911 break;
16912 case RID_DOUBLE:
16913 type = double_type_node;
16914 break;
16915 case RID_VOID:
16916 type = void_type_node;
16917 break;
16919 case RID_AUTO:
16920 maybe_warn_cpp0x (CPP0X_AUTO);
16921 if (parser->auto_is_implicit_function_template_parm_p)
16923 /* The 'auto' might be the placeholder return type for a function decl
16924 with trailing return type. */
16925 bool have_trailing_return_fn_decl = false;
16927 cp_parser_parse_tentatively (parser);
16928 cp_lexer_consume_token (parser->lexer);
16929 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16930 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16931 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16932 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16934 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16936 cp_lexer_consume_token (parser->lexer);
16937 cp_parser_skip_to_closing_parenthesis (parser,
16938 /*recovering*/false,
16939 /*or_comma*/false,
16940 /*consume_paren*/true);
16941 continue;
16944 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16946 have_trailing_return_fn_decl = true;
16947 break;
16950 cp_lexer_consume_token (parser->lexer);
16952 cp_parser_abort_tentative_parse (parser);
16954 if (have_trailing_return_fn_decl)
16956 type = make_auto ();
16957 break;
16960 if (cxx_dialect >= cxx14)
16962 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16963 type = TREE_TYPE (type);
16965 else
16966 type = error_mark_node;
16968 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16970 if (cxx_dialect < cxx14)
16971 error_at (token->location,
16972 "use of %<auto%> in lambda parameter declaration "
16973 "only available with "
16974 "-std=c++14 or -std=gnu++14");
16976 else if (cxx_dialect < cxx14)
16977 error_at (token->location,
16978 "use of %<auto%> in parameter declaration "
16979 "only available with "
16980 "-std=c++14 or -std=gnu++14");
16981 else if (!flag_concepts)
16982 pedwarn (token->location, OPT_Wpedantic,
16983 "ISO C++ forbids use of %<auto%> in parameter "
16984 "declaration");
16986 else
16987 type = make_auto ();
16988 break;
16990 case RID_DECLTYPE:
16991 /* Since DR 743, decltype can either be a simple-type-specifier by
16992 itself or begin a nested-name-specifier. Parsing it will replace
16993 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16994 handling below decide what to do. */
16995 cp_parser_decltype (parser);
16996 cp_lexer_set_token_position (parser->lexer, token);
16997 break;
16999 case RID_TYPEOF:
17000 /* Consume the `typeof' token. */
17001 cp_lexer_consume_token (parser->lexer);
17002 /* Parse the operand to `typeof'. */
17003 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17004 /* If it is not already a TYPE, take its type. */
17005 if (!TYPE_P (type))
17006 type = finish_typeof (type);
17008 if (decl_specs)
17009 cp_parser_set_decl_spec_type (decl_specs, type,
17010 token,
17011 /*type_definition_p=*/false);
17013 return type;
17015 case RID_UNDERLYING_TYPE:
17016 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17017 if (decl_specs)
17018 cp_parser_set_decl_spec_type (decl_specs, type,
17019 token,
17020 /*type_definition_p=*/false);
17022 return type;
17024 case RID_BASES:
17025 case RID_DIRECT_BASES:
17026 type = cp_parser_trait_expr (parser, token->keyword);
17027 if (decl_specs)
17028 cp_parser_set_decl_spec_type (decl_specs, type,
17029 token,
17030 /*type_definition_p=*/false);
17031 return type;
17032 default:
17033 break;
17036 /* If token is an already-parsed decltype not followed by ::,
17037 it's a simple-type-specifier. */
17038 if (token->type == CPP_DECLTYPE
17039 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17041 type = saved_checks_value (token->u.tree_check_value);
17042 if (decl_specs)
17044 cp_parser_set_decl_spec_type (decl_specs, type,
17045 token,
17046 /*type_definition_p=*/false);
17047 /* Remember that we are handling a decltype in order to
17048 implement the resolution of DR 1510 when the argument
17049 isn't instantiation dependent. */
17050 decl_specs->decltype_p = true;
17052 cp_lexer_consume_token (parser->lexer);
17053 return type;
17056 /* If the type-specifier was for a built-in type, we're done. */
17057 if (type)
17059 /* Record the type. */
17060 if (decl_specs
17061 && (token->keyword != RID_SIGNED
17062 && token->keyword != RID_UNSIGNED
17063 && token->keyword != RID_SHORT
17064 && token->keyword != RID_LONG))
17065 cp_parser_set_decl_spec_type (decl_specs,
17066 type,
17067 token,
17068 /*type_definition_p=*/false);
17069 if (decl_specs)
17070 decl_specs->any_specifiers_p = true;
17072 /* Consume the token. */
17073 cp_lexer_consume_token (parser->lexer);
17075 if (type == error_mark_node)
17076 return error_mark_node;
17078 /* There is no valid C++ program where a non-template type is
17079 followed by a "<". That usually indicates that the user thought
17080 that the type was a template. */
17081 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17082 token->location);
17084 return TYPE_NAME (type);
17087 /* The type-specifier must be a user-defined type. */
17088 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17090 bool qualified_p;
17091 bool global_p;
17093 /* Don't gobble tokens or issue error messages if this is an
17094 optional type-specifier. */
17095 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17096 cp_parser_parse_tentatively (parser);
17098 token = cp_lexer_peek_token (parser->lexer);
17100 /* Look for the optional `::' operator. */
17101 global_p
17102 = (cp_parser_global_scope_opt (parser,
17103 /*current_scope_valid_p=*/false)
17104 != NULL_TREE);
17105 /* Look for the nested-name specifier. */
17106 qualified_p
17107 = (cp_parser_nested_name_specifier_opt (parser,
17108 /*typename_keyword_p=*/false,
17109 /*check_dependency_p=*/true,
17110 /*type_p=*/false,
17111 /*is_declaration=*/false)
17112 != NULL_TREE);
17113 /* If we have seen a nested-name-specifier, and the next token
17114 is `template', then we are using the template-id production. */
17115 if (parser->scope
17116 && cp_parser_optional_template_keyword (parser))
17118 /* Look for the template-id. */
17119 type = cp_parser_template_id (parser,
17120 /*template_keyword_p=*/true,
17121 /*check_dependency_p=*/true,
17122 none_type,
17123 /*is_declaration=*/false);
17124 /* If the template-id did not name a type, we are out of
17125 luck. */
17126 if (TREE_CODE (type) != TYPE_DECL)
17128 cp_parser_error (parser, "expected template-id for type");
17129 type = NULL_TREE;
17132 /* Otherwise, look for a type-name. */
17133 else
17134 type = cp_parser_type_name (parser);
17135 /* Keep track of all name-lookups performed in class scopes. */
17136 if (type
17137 && !global_p
17138 && !qualified_p
17139 && TREE_CODE (type) == TYPE_DECL
17140 && identifier_p (DECL_NAME (type)))
17141 maybe_note_name_used_in_class (DECL_NAME (type), type);
17142 /* If it didn't work out, we don't have a TYPE. */
17143 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17144 && !cp_parser_parse_definitely (parser))
17145 type = NULL_TREE;
17146 if (!type && cxx_dialect >= cxx17)
17148 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17149 cp_parser_parse_tentatively (parser);
17151 cp_parser_global_scope_opt (parser,
17152 /*current_scope_valid_p=*/false);
17153 cp_parser_nested_name_specifier_opt (parser,
17154 /*typename_keyword_p=*/false,
17155 /*check_dependency_p=*/true,
17156 /*type_p=*/false,
17157 /*is_declaration=*/false);
17158 tree name = cp_parser_identifier (parser);
17159 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17160 && parser->scope != error_mark_node)
17162 tree tmpl = cp_parser_lookup_name (parser, name,
17163 none_type,
17164 /*is_template=*/false,
17165 /*is_namespace=*/false,
17166 /*check_dependency=*/true,
17167 /*ambiguous_decls=*/NULL,
17168 token->location);
17169 if (tmpl && tmpl != error_mark_node
17170 && (DECL_CLASS_TEMPLATE_P (tmpl)
17171 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17172 type = make_template_placeholder (tmpl);
17173 else
17175 type = error_mark_node;
17176 if (!cp_parser_simulate_error (parser))
17177 cp_parser_name_lookup_error (parser, name, tmpl,
17178 NLE_TYPE, token->location);
17181 else
17182 type = error_mark_node;
17184 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17185 && !cp_parser_parse_definitely (parser))
17186 type = NULL_TREE;
17188 if (type && decl_specs)
17189 cp_parser_set_decl_spec_type (decl_specs, type,
17190 token,
17191 /*type_definition_p=*/false);
17194 /* If we didn't get a type-name, issue an error message. */
17195 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17197 cp_parser_error (parser, "expected type-name");
17198 return error_mark_node;
17201 if (type && type != error_mark_node)
17203 /* See if TYPE is an Objective-C type, and if so, parse and
17204 accept any protocol references following it. Do this before
17205 the cp_parser_check_for_invalid_template_id() call, because
17206 Objective-C types can be followed by '<...>' which would
17207 enclose protocol names rather than template arguments, and so
17208 everything is fine. */
17209 if (c_dialect_objc () && !parser->scope
17210 && (objc_is_id (type) || objc_is_class_name (type)))
17212 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17213 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17215 /* Clobber the "unqualified" type previously entered into
17216 DECL_SPECS with the new, improved protocol-qualified version. */
17217 if (decl_specs)
17218 decl_specs->type = qual_type;
17220 return qual_type;
17223 /* There is no valid C++ program where a non-template type is
17224 followed by a "<". That usually indicates that the user
17225 thought that the type was a template. */
17226 cp_parser_check_for_invalid_template_id (parser, type,
17227 none_type,
17228 token->location);
17231 return type;
17234 /* Parse a type-name.
17236 type-name:
17237 class-name
17238 enum-name
17239 typedef-name
17240 simple-template-id [in c++0x]
17242 enum-name:
17243 identifier
17245 typedef-name:
17246 identifier
17248 Concepts:
17250 type-name:
17251 concept-name
17252 partial-concept-id
17254 concept-name:
17255 identifier
17257 Returns a TYPE_DECL for the type. */
17259 static tree
17260 cp_parser_type_name (cp_parser* parser)
17262 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17265 /* See above. */
17266 static tree
17267 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17269 tree type_decl;
17271 /* We can't know yet whether it is a class-name or not. */
17272 cp_parser_parse_tentatively (parser);
17273 /* Try a class-name. */
17274 type_decl = cp_parser_class_name (parser,
17275 typename_keyword_p,
17276 /*template_keyword_p=*/false,
17277 none_type,
17278 /*check_dependency_p=*/true,
17279 /*class_head_p=*/false,
17280 /*is_declaration=*/false);
17281 /* If it's not a class-name, keep looking. */
17282 if (!cp_parser_parse_definitely (parser))
17284 if (cxx_dialect < cxx11)
17285 /* It must be a typedef-name or an enum-name. */
17286 return cp_parser_nonclass_name (parser);
17288 cp_parser_parse_tentatively (parser);
17289 /* It is either a simple-template-id representing an
17290 instantiation of an alias template... */
17291 type_decl = cp_parser_template_id (parser,
17292 /*template_keyword_p=*/false,
17293 /*check_dependency_p=*/true,
17294 none_type,
17295 /*is_declaration=*/false);
17296 /* Note that this must be an instantiation of an alias template
17297 because [temp.names]/6 says:
17299 A template-id that names an alias template specialization
17300 is a type-name.
17302 Whereas [temp.names]/7 says:
17304 A simple-template-id that names a class template
17305 specialization is a class-name.
17307 With concepts, this could also be a partial-concept-id that
17308 declares a non-type template parameter. */
17309 if (type_decl != NULL_TREE
17310 && TREE_CODE (type_decl) == TYPE_DECL
17311 && TYPE_DECL_ALIAS_P (type_decl))
17312 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17313 else if (is_constrained_parameter (type_decl))
17314 /* Don't do anything. */ ;
17315 else
17316 cp_parser_simulate_error (parser);
17318 if (!cp_parser_parse_definitely (parser))
17319 /* ... Or a typedef-name or an enum-name. */
17320 return cp_parser_nonclass_name (parser);
17323 return type_decl;
17326 /* Check if DECL and ARGS can form a constrained-type-specifier.
17327 If ARGS is non-null, we try to form a concept check of the
17328 form DECL<?, ARGS> where ? is a wildcard that matches any
17329 kind of template argument. If ARGS is NULL, then we try to
17330 form a concept check of the form DECL<?>. */
17332 static tree
17333 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17334 tree decl, tree args)
17336 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17338 /* If we a constrained-type-specifier cannot be deduced. */
17339 if (parser->prevent_constrained_type_specifiers)
17340 return NULL_TREE;
17342 /* A constrained type specifier can only be found in an
17343 overload set or as a reference to a template declaration.
17345 FIXME: This might be masking a bug. It's possible that
17346 that the deduction below is causing template specializations
17347 to be formed with the wildcard as an argument. */
17348 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17349 return NULL_TREE;
17351 /* Try to build a call expression that evaluates the
17352 concept. This can fail if the overload set refers
17353 only to non-templates. */
17354 tree placeholder = build_nt (WILDCARD_DECL);
17355 tree check = build_concept_check (decl, placeholder, args);
17356 if (check == error_mark_node)
17357 return NULL_TREE;
17359 /* Deduce the checked constraint and the prototype parameter.
17361 FIXME: In certain cases, failure to deduce should be a
17362 diagnosable error. */
17363 tree conc;
17364 tree proto;
17365 if (!deduce_constrained_parameter (check, conc, proto))
17366 return NULL_TREE;
17368 /* In template parameter scope, this results in a constrained
17369 parameter. Return a descriptor of that parm. */
17370 if (processing_template_parmlist)
17371 return build_constrained_parameter (conc, proto, args);
17373 /* In a parameter-declaration-clause, constrained-type
17374 specifiers result in invented template parameters. */
17375 if (parser->auto_is_implicit_function_template_parm_p)
17377 tree x = build_constrained_parameter (conc, proto, args);
17378 return synthesize_implicit_template_parm (parser, x);
17380 else
17382 /* Otherwise, we're in a context where the constrained
17383 type name is deduced and the constraint applies
17384 after deduction. */
17385 return make_constrained_auto (conc, args);
17388 return NULL_TREE;
17391 /* If DECL refers to a concept, return a TYPE_DECL representing
17392 the result of using the constrained type specifier in the
17393 current context. DECL refers to a concept if
17395 - it is an overload set containing a function concept taking a single
17396 type argument, or
17398 - it is a variable concept taking a single type argument. */
17400 static tree
17401 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17403 if (flag_concepts
17404 && (TREE_CODE (decl) == OVERLOAD
17405 || BASELINK_P (decl)
17406 || variable_concept_p (decl)))
17407 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17408 else
17409 return NULL_TREE;
17412 /* Check if DECL and ARGS form a partial-concept-id. If so,
17413 assign ID to the resulting constrained placeholder.
17415 Returns true if the partial-concept-id designates a placeholder
17416 and false otherwise. Note that *id is set to NULL_TREE in
17417 this case. */
17419 static tree
17420 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17422 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17425 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17426 or a concept-name.
17428 enum-name:
17429 identifier
17431 typedef-name:
17432 identifier
17434 concept-name:
17435 identifier
17437 Returns a TYPE_DECL for the type. */
17439 static tree
17440 cp_parser_nonclass_name (cp_parser* parser)
17442 tree type_decl;
17443 tree identifier;
17445 cp_token *token = cp_lexer_peek_token (parser->lexer);
17446 identifier = cp_parser_identifier (parser);
17447 if (identifier == error_mark_node)
17448 return error_mark_node;
17450 /* Look up the type-name. */
17451 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17453 type_decl = strip_using_decl (type_decl);
17455 /* If we found an overload set, then it may refer to a concept-name. */
17456 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17457 type_decl = decl;
17459 if (TREE_CODE (type_decl) != TYPE_DECL
17460 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17462 /* See if this is an Objective-C type. */
17463 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17464 tree type = objc_get_protocol_qualified_type (identifier, protos);
17465 if (type)
17466 type_decl = TYPE_NAME (type);
17469 /* Issue an error if we did not find a type-name. */
17470 if (TREE_CODE (type_decl) != TYPE_DECL
17471 /* In Objective-C, we have the complication that class names are
17472 normally type names and start declarations (eg, the
17473 "NSObject" in "NSObject *object;"), but can be used in an
17474 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17475 is an expression. So, a classname followed by a dot is not a
17476 valid type-name. */
17477 || (objc_is_class_name (TREE_TYPE (type_decl))
17478 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17480 if (!cp_parser_simulate_error (parser))
17481 cp_parser_name_lookup_error (parser, identifier, type_decl,
17482 NLE_TYPE, token->location);
17483 return error_mark_node;
17485 /* Remember that the name was used in the definition of the
17486 current class so that we can check later to see if the
17487 meaning would have been different after the class was
17488 entirely defined. */
17489 else if (type_decl != error_mark_node
17490 && !parser->scope)
17491 maybe_note_name_used_in_class (identifier, type_decl);
17493 return type_decl;
17496 /* Parse an elaborated-type-specifier. Note that the grammar given
17497 here incorporates the resolution to DR68.
17499 elaborated-type-specifier:
17500 class-key :: [opt] nested-name-specifier [opt] identifier
17501 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17502 enum-key :: [opt] nested-name-specifier [opt] identifier
17503 typename :: [opt] nested-name-specifier identifier
17504 typename :: [opt] nested-name-specifier template [opt]
17505 template-id
17507 GNU extension:
17509 elaborated-type-specifier:
17510 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17511 class-key attributes :: [opt] nested-name-specifier [opt]
17512 template [opt] template-id
17513 enum attributes :: [opt] nested-name-specifier [opt] identifier
17515 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17516 declared `friend'. If IS_DECLARATION is TRUE, then this
17517 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17518 something is being declared.
17520 Returns the TYPE specified. */
17522 static tree
17523 cp_parser_elaborated_type_specifier (cp_parser* parser,
17524 bool is_friend,
17525 bool is_declaration)
17527 enum tag_types tag_type;
17528 tree identifier;
17529 tree type = NULL_TREE;
17530 tree attributes = NULL_TREE;
17531 tree globalscope;
17532 cp_token *token = NULL;
17534 /* See if we're looking at the `enum' keyword. */
17535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17537 /* Consume the `enum' token. */
17538 cp_lexer_consume_token (parser->lexer);
17539 /* Remember that it's an enumeration type. */
17540 tag_type = enum_type;
17541 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17542 enums) is used here. */
17543 cp_token *token = cp_lexer_peek_token (parser->lexer);
17544 if (cp_parser_is_keyword (token, RID_CLASS)
17545 || cp_parser_is_keyword (token, RID_STRUCT))
17547 gcc_rich_location richloc (token->location);
17548 richloc.add_range (input_location, false);
17549 richloc.add_fixit_remove ();
17550 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17551 "a scoped enum must not use the %qD keyword",
17552 token->u.value);
17553 /* Consume the `struct' or `class' and parse it anyway. */
17554 cp_lexer_consume_token (parser->lexer);
17556 /* Parse the attributes. */
17557 attributes = cp_parser_attributes_opt (parser);
17559 /* Or, it might be `typename'. */
17560 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17561 RID_TYPENAME))
17563 /* Consume the `typename' token. */
17564 cp_lexer_consume_token (parser->lexer);
17565 /* Remember that it's a `typename' type. */
17566 tag_type = typename_type;
17568 /* Otherwise it must be a class-key. */
17569 else
17571 tag_type = cp_parser_class_key (parser);
17572 if (tag_type == none_type)
17573 return error_mark_node;
17574 /* Parse the attributes. */
17575 attributes = cp_parser_attributes_opt (parser);
17578 /* Look for the `::' operator. */
17579 globalscope = cp_parser_global_scope_opt (parser,
17580 /*current_scope_valid_p=*/false);
17581 /* Look for the nested-name-specifier. */
17582 tree nested_name_specifier;
17583 if (tag_type == typename_type && !globalscope)
17585 nested_name_specifier
17586 = cp_parser_nested_name_specifier (parser,
17587 /*typename_keyword_p=*/true,
17588 /*check_dependency_p=*/true,
17589 /*type_p=*/true,
17590 is_declaration);
17591 if (!nested_name_specifier)
17592 return error_mark_node;
17594 else
17595 /* Even though `typename' is not present, the proposed resolution
17596 to Core Issue 180 says that in `class A<T>::B', `B' should be
17597 considered a type-name, even if `A<T>' is dependent. */
17598 nested_name_specifier
17599 = cp_parser_nested_name_specifier_opt (parser,
17600 /*typename_keyword_p=*/true,
17601 /*check_dependency_p=*/true,
17602 /*type_p=*/true,
17603 is_declaration);
17604 /* For everything but enumeration types, consider a template-id.
17605 For an enumeration type, consider only a plain identifier. */
17606 if (tag_type != enum_type)
17608 bool template_p = false;
17609 tree decl;
17611 /* Allow the `template' keyword. */
17612 template_p = cp_parser_optional_template_keyword (parser);
17613 /* If we didn't see `template', we don't know if there's a
17614 template-id or not. */
17615 if (!template_p)
17616 cp_parser_parse_tentatively (parser);
17617 /* Parse the template-id. */
17618 token = cp_lexer_peek_token (parser->lexer);
17619 decl = cp_parser_template_id (parser, template_p,
17620 /*check_dependency_p=*/true,
17621 tag_type,
17622 is_declaration);
17623 /* If we didn't find a template-id, look for an ordinary
17624 identifier. */
17625 if (!template_p && !cp_parser_parse_definitely (parser))
17627 /* We can get here when cp_parser_template_id, called by
17628 cp_parser_class_name with tag_type == none_type, succeeds
17629 and caches a BASELINK. Then, when called again here,
17630 instead of failing and returning an error_mark_node
17631 returns it (see template/typename17.C in C++11).
17632 ??? Could we diagnose this earlier? */
17633 else if (tag_type == typename_type && BASELINK_P (decl))
17635 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17636 type = error_mark_node;
17638 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17639 in effect, then we must assume that, upon instantiation, the
17640 template will correspond to a class. */
17641 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17642 && tag_type == typename_type)
17643 type = make_typename_type (parser->scope, decl,
17644 typename_type,
17645 /*complain=*/tf_error);
17646 /* If the `typename' keyword is in effect and DECL is not a type
17647 decl, then type is non existent. */
17648 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17650 else if (TREE_CODE (decl) == TYPE_DECL)
17652 type = check_elaborated_type_specifier (tag_type, decl,
17653 /*allow_template_p=*/true);
17655 /* If the next token is a semicolon, this must be a specialization,
17656 instantiation, or friend declaration. Check the scope while we
17657 still know whether or not we had a nested-name-specifier. */
17658 if (type != error_mark_node
17659 && !nested_name_specifier && !is_friend
17660 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17661 check_unqualified_spec_or_inst (type, token->location);
17663 else if (decl == error_mark_node)
17664 type = error_mark_node;
17667 if (!type)
17669 token = cp_lexer_peek_token (parser->lexer);
17670 identifier = cp_parser_identifier (parser);
17672 if (identifier == error_mark_node)
17674 parser->scope = NULL_TREE;
17675 return error_mark_node;
17678 /* For a `typename', we needn't call xref_tag. */
17679 if (tag_type == typename_type
17680 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17681 return cp_parser_make_typename_type (parser, identifier,
17682 token->location);
17684 /* Template parameter lists apply only if we are not within a
17685 function parameter list. */
17686 bool template_parm_lists_apply
17687 = parser->num_template_parameter_lists;
17688 if (template_parm_lists_apply)
17689 for (cp_binding_level *s = current_binding_level;
17690 s && s->kind != sk_template_parms;
17691 s = s->level_chain)
17692 if (s->kind == sk_function_parms)
17693 template_parm_lists_apply = false;
17695 /* Look up a qualified name in the usual way. */
17696 if (parser->scope)
17698 tree decl;
17699 tree ambiguous_decls;
17701 decl = cp_parser_lookup_name (parser, identifier,
17702 tag_type,
17703 /*is_template=*/false,
17704 /*is_namespace=*/false,
17705 /*check_dependency=*/true,
17706 &ambiguous_decls,
17707 token->location);
17709 /* If the lookup was ambiguous, an error will already have been
17710 issued. */
17711 if (ambiguous_decls)
17712 return error_mark_node;
17714 /* If we are parsing friend declaration, DECL may be a
17715 TEMPLATE_DECL tree node here. However, we need to check
17716 whether this TEMPLATE_DECL results in valid code. Consider
17717 the following example:
17719 namespace N {
17720 template <class T> class C {};
17722 class X {
17723 template <class T> friend class N::C; // #1, valid code
17725 template <class T> class Y {
17726 friend class N::C; // #2, invalid code
17729 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17730 name lookup of `N::C'. We see that friend declaration must
17731 be template for the code to be valid. Note that
17732 processing_template_decl does not work here since it is
17733 always 1 for the above two cases. */
17735 decl = (cp_parser_maybe_treat_template_as_class
17736 (decl, /*tag_name_p=*/is_friend
17737 && template_parm_lists_apply));
17739 if (TREE_CODE (decl) != TYPE_DECL)
17741 cp_parser_diagnose_invalid_type_name (parser,
17742 identifier,
17743 token->location);
17744 return error_mark_node;
17747 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17749 bool allow_template = (template_parm_lists_apply
17750 || DECL_SELF_REFERENCE_P (decl));
17751 type = check_elaborated_type_specifier (tag_type, decl,
17752 allow_template);
17754 if (type == error_mark_node)
17755 return error_mark_node;
17758 /* Forward declarations of nested types, such as
17760 class C1::C2;
17761 class C1::C2::C3;
17763 are invalid unless all components preceding the final '::'
17764 are complete. If all enclosing types are complete, these
17765 declarations become merely pointless.
17767 Invalid forward declarations of nested types are errors
17768 caught elsewhere in parsing. Those that are pointless arrive
17769 here. */
17771 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17772 && !is_friend && !processing_explicit_instantiation)
17773 warning (0, "declaration %qD does not declare anything", decl);
17775 type = TREE_TYPE (decl);
17777 else
17779 /* An elaborated-type-specifier sometimes introduces a new type and
17780 sometimes names an existing type. Normally, the rule is that it
17781 introduces a new type only if there is not an existing type of
17782 the same name already in scope. For example, given:
17784 struct S {};
17785 void f() { struct S s; }
17787 the `struct S' in the body of `f' is the same `struct S' as in
17788 the global scope; the existing definition is used. However, if
17789 there were no global declaration, this would introduce a new
17790 local class named `S'.
17792 An exception to this rule applies to the following code:
17794 namespace N { struct S; }
17796 Here, the elaborated-type-specifier names a new type
17797 unconditionally; even if there is already an `S' in the
17798 containing scope this declaration names a new type.
17799 This exception only applies if the elaborated-type-specifier
17800 forms the complete declaration:
17802 [class.name]
17804 A declaration consisting solely of `class-key identifier ;' is
17805 either a redeclaration of the name in the current scope or a
17806 forward declaration of the identifier as a class name. It
17807 introduces the name into the current scope.
17809 We are in this situation precisely when the next token is a `;'.
17811 An exception to the exception is that a `friend' declaration does
17812 *not* name a new type; i.e., given:
17814 struct S { friend struct T; };
17816 `T' is not a new type in the scope of `S'.
17818 Also, `new struct S' or `sizeof (struct S)' never results in the
17819 definition of a new type; a new type can only be declared in a
17820 declaration context. */
17822 tag_scope ts;
17823 bool template_p;
17825 if (is_friend)
17826 /* Friends have special name lookup rules. */
17827 ts = ts_within_enclosing_non_class;
17828 else if (is_declaration
17829 && cp_lexer_next_token_is (parser->lexer,
17830 CPP_SEMICOLON))
17831 /* This is a `class-key identifier ;' */
17832 ts = ts_current;
17833 else
17834 ts = ts_global;
17836 template_p =
17837 (template_parm_lists_apply
17838 && (cp_parser_next_token_starts_class_definition_p (parser)
17839 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17840 /* An unqualified name was used to reference this type, so
17841 there were no qualifying templates. */
17842 if (template_parm_lists_apply
17843 && !cp_parser_check_template_parameters (parser,
17844 /*num_templates=*/0,
17845 token->location,
17846 /*declarator=*/NULL))
17847 return error_mark_node;
17848 type = xref_tag (tag_type, identifier, ts, template_p);
17852 if (type == error_mark_node)
17853 return error_mark_node;
17855 /* Allow attributes on forward declarations of classes. */
17856 if (attributes)
17858 if (TREE_CODE (type) == TYPENAME_TYPE)
17859 warning (OPT_Wattributes,
17860 "attributes ignored on uninstantiated type");
17861 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17862 && ! processing_explicit_instantiation)
17863 warning (OPT_Wattributes,
17864 "attributes ignored on template instantiation");
17865 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17866 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17867 else
17868 warning (OPT_Wattributes,
17869 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17872 if (tag_type != enum_type)
17874 /* Indicate whether this class was declared as a `class' or as a
17875 `struct'. */
17876 if (CLASS_TYPE_P (type))
17877 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17878 cp_parser_check_class_key (tag_type, type);
17881 /* A "<" cannot follow an elaborated type specifier. If that
17882 happens, the user was probably trying to form a template-id. */
17883 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17884 token->location);
17886 return type;
17889 /* Parse an enum-specifier.
17891 enum-specifier:
17892 enum-head { enumerator-list [opt] }
17893 enum-head { enumerator-list , } [C++0x]
17895 enum-head:
17896 enum-key identifier [opt] enum-base [opt]
17897 enum-key nested-name-specifier identifier enum-base [opt]
17899 enum-key:
17900 enum
17901 enum class [C++0x]
17902 enum struct [C++0x]
17904 enum-base: [C++0x]
17905 : type-specifier-seq
17907 opaque-enum-specifier:
17908 enum-key identifier enum-base [opt] ;
17910 GNU Extensions:
17911 enum-key attributes[opt] identifier [opt] enum-base [opt]
17912 { enumerator-list [opt] }attributes[opt]
17913 enum-key attributes[opt] identifier [opt] enum-base [opt]
17914 { enumerator-list, }attributes[opt] [C++0x]
17916 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17917 if the token stream isn't an enum-specifier after all. */
17919 static tree
17920 cp_parser_enum_specifier (cp_parser* parser)
17922 tree identifier;
17923 tree type = NULL_TREE;
17924 tree prev_scope;
17925 tree nested_name_specifier = NULL_TREE;
17926 tree attributes;
17927 bool scoped_enum_p = false;
17928 bool has_underlying_type = false;
17929 bool nested_being_defined = false;
17930 bool new_value_list = false;
17931 bool is_new_type = false;
17932 bool is_unnamed = false;
17933 tree underlying_type = NULL_TREE;
17934 cp_token *type_start_token = NULL;
17935 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17937 parser->colon_corrects_to_scope_p = false;
17939 /* Parse tentatively so that we can back up if we don't find a
17940 enum-specifier. */
17941 cp_parser_parse_tentatively (parser);
17943 /* Caller guarantees that the current token is 'enum', an identifier
17944 possibly follows, and the token after that is an opening brace.
17945 If we don't have an identifier, fabricate an anonymous name for
17946 the enumeration being defined. */
17947 cp_lexer_consume_token (parser->lexer);
17949 /* Parse the "class" or "struct", which indicates a scoped
17950 enumeration type in C++0x. */
17951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17952 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17954 if (cxx_dialect < cxx11)
17955 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17957 /* Consume the `struct' or `class' token. */
17958 cp_lexer_consume_token (parser->lexer);
17960 scoped_enum_p = true;
17963 attributes = cp_parser_attributes_opt (parser);
17965 /* Clear the qualification. */
17966 parser->scope = NULL_TREE;
17967 parser->qualifying_scope = NULL_TREE;
17968 parser->object_scope = NULL_TREE;
17970 /* Figure out in what scope the declaration is being placed. */
17971 prev_scope = current_scope ();
17973 type_start_token = cp_lexer_peek_token (parser->lexer);
17975 push_deferring_access_checks (dk_no_check);
17976 nested_name_specifier
17977 = cp_parser_nested_name_specifier_opt (parser,
17978 /*typename_keyword_p=*/true,
17979 /*check_dependency_p=*/false,
17980 /*type_p=*/false,
17981 /*is_declaration=*/false);
17983 if (nested_name_specifier)
17985 tree name;
17987 identifier = cp_parser_identifier (parser);
17988 name = cp_parser_lookup_name (parser, identifier,
17989 enum_type,
17990 /*is_template=*/false,
17991 /*is_namespace=*/false,
17992 /*check_dependency=*/true,
17993 /*ambiguous_decls=*/NULL,
17994 input_location);
17995 if (name && name != error_mark_node)
17997 type = TREE_TYPE (name);
17998 if (TREE_CODE (type) == TYPENAME_TYPE)
18000 /* Are template enums allowed in ISO? */
18001 if (template_parm_scope_p ())
18002 pedwarn (type_start_token->location, OPT_Wpedantic,
18003 "%qD is an enumeration template", name);
18004 /* ignore a typename reference, for it will be solved by name
18005 in start_enum. */
18006 type = NULL_TREE;
18009 else if (nested_name_specifier == error_mark_node)
18010 /* We already issued an error. */;
18011 else
18013 error_at (type_start_token->location,
18014 "%qD does not name an enumeration in %qT",
18015 identifier, nested_name_specifier);
18016 nested_name_specifier = error_mark_node;
18019 else
18021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18022 identifier = cp_parser_identifier (parser);
18023 else
18025 identifier = make_anon_name ();
18026 is_unnamed = true;
18027 if (scoped_enum_p)
18028 error_at (type_start_token->location,
18029 "unnamed scoped enum is not allowed");
18032 pop_deferring_access_checks ();
18034 /* Check for the `:' that denotes a specified underlying type in C++0x.
18035 Note that a ':' could also indicate a bitfield width, however. */
18036 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18038 cp_decl_specifier_seq type_specifiers;
18040 /* Consume the `:'. */
18041 cp_lexer_consume_token (parser->lexer);
18043 /* Parse the type-specifier-seq. */
18044 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18045 /*is_trailing_return=*/false,
18046 &type_specifiers);
18048 /* At this point this is surely not elaborated type specifier. */
18049 if (!cp_parser_parse_definitely (parser))
18050 return NULL_TREE;
18052 if (cxx_dialect < cxx11)
18053 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18055 has_underlying_type = true;
18057 /* If that didn't work, stop. */
18058 if (type_specifiers.type != error_mark_node)
18060 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18061 /*initialized=*/0, NULL);
18062 if (underlying_type == error_mark_node
18063 || check_for_bare_parameter_packs (underlying_type))
18064 underlying_type = NULL_TREE;
18068 /* Look for the `{' but don't consume it yet. */
18069 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18071 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18073 cp_parser_error (parser, "expected %<{%>");
18074 if (has_underlying_type)
18076 type = NULL_TREE;
18077 goto out;
18080 /* An opaque-enum-specifier must have a ';' here. */
18081 if ((scoped_enum_p || underlying_type)
18082 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18084 cp_parser_error (parser, "expected %<;%> or %<{%>");
18085 if (has_underlying_type)
18087 type = NULL_TREE;
18088 goto out;
18093 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18094 return NULL_TREE;
18096 if (nested_name_specifier)
18098 if (CLASS_TYPE_P (nested_name_specifier))
18100 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18101 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18102 push_scope (nested_name_specifier);
18104 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18106 push_nested_namespace (nested_name_specifier);
18110 /* Issue an error message if type-definitions are forbidden here. */
18111 if (!cp_parser_check_type_definition (parser))
18112 type = error_mark_node;
18113 else
18114 /* Create the new type. We do this before consuming the opening
18115 brace so the enum will be recorded as being on the line of its
18116 tag (or the 'enum' keyword, if there is no tag). */
18117 type = start_enum (identifier, type, underlying_type,
18118 attributes, scoped_enum_p, &is_new_type);
18120 /* If the next token is not '{' it is an opaque-enum-specifier or an
18121 elaborated-type-specifier. */
18122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18124 timevar_push (TV_PARSE_ENUM);
18125 if (nested_name_specifier
18126 && nested_name_specifier != error_mark_node)
18128 /* The following catches invalid code such as:
18129 enum class S<int>::E { A, B, C }; */
18130 if (!processing_specialization
18131 && CLASS_TYPE_P (nested_name_specifier)
18132 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18133 error_at (type_start_token->location, "cannot add an enumerator "
18134 "list to a template instantiation");
18136 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18138 error_at (type_start_token->location,
18139 "%<%T::%E%> has not been declared",
18140 TYPE_CONTEXT (nested_name_specifier),
18141 nested_name_specifier);
18142 type = error_mark_node;
18144 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18145 && !CLASS_TYPE_P (nested_name_specifier))
18147 error_at (type_start_token->location, "nested name specifier "
18148 "%qT for enum declaration does not name a class "
18149 "or namespace", nested_name_specifier);
18150 type = error_mark_node;
18152 /* If that scope does not contain the scope in which the
18153 class was originally declared, the program is invalid. */
18154 else if (prev_scope && !is_ancestor (prev_scope,
18155 nested_name_specifier))
18157 if (at_namespace_scope_p ())
18158 error_at (type_start_token->location,
18159 "declaration of %qD in namespace %qD which does not "
18160 "enclose %qD",
18161 type, prev_scope, nested_name_specifier);
18162 else
18163 error_at (type_start_token->location,
18164 "declaration of %qD in %qD which does not "
18165 "enclose %qD",
18166 type, prev_scope, nested_name_specifier);
18167 type = error_mark_node;
18169 /* If that scope is the scope where the declaration is being placed
18170 the program is invalid. */
18171 else if (CLASS_TYPE_P (nested_name_specifier)
18172 && CLASS_TYPE_P (prev_scope)
18173 && same_type_p (nested_name_specifier, prev_scope))
18175 permerror (type_start_token->location,
18176 "extra qualification not allowed");
18177 nested_name_specifier = NULL_TREE;
18181 if (scoped_enum_p)
18182 begin_scope (sk_scoped_enum, type);
18184 /* Consume the opening brace. */
18185 matching_braces braces;
18186 braces.consume_open (parser);
18188 if (type == error_mark_node)
18189 ; /* Nothing to add */
18190 else if (OPAQUE_ENUM_P (type)
18191 || (cxx_dialect > cxx98 && processing_specialization))
18193 new_value_list = true;
18194 SET_OPAQUE_ENUM_P (type, false);
18195 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18197 else
18199 error_at (type_start_token->location,
18200 "multiple definition of %q#T", type);
18201 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18202 "previous definition here");
18203 type = error_mark_node;
18206 if (type == error_mark_node)
18207 cp_parser_skip_to_end_of_block_or_statement (parser);
18208 /* If the next token is not '}', then there are some enumerators. */
18209 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18211 if (is_unnamed && !scoped_enum_p)
18212 pedwarn (type_start_token->location, OPT_Wpedantic,
18213 "ISO C++ forbids empty unnamed enum");
18215 else
18216 cp_parser_enumerator_list (parser, type);
18218 /* Consume the final '}'. */
18219 braces.require_close (parser);
18221 if (scoped_enum_p)
18222 finish_scope ();
18223 timevar_pop (TV_PARSE_ENUM);
18225 else
18227 /* If a ';' follows, then it is an opaque-enum-specifier
18228 and additional restrictions apply. */
18229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18231 if (is_unnamed)
18232 error_at (type_start_token->location,
18233 "opaque-enum-specifier without name");
18234 else if (nested_name_specifier)
18235 error_at (type_start_token->location,
18236 "opaque-enum-specifier must use a simple identifier");
18240 /* Look for trailing attributes to apply to this enumeration, and
18241 apply them if appropriate. */
18242 if (cp_parser_allow_gnu_extensions_p (parser))
18244 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18245 cplus_decl_attributes (&type,
18246 trailing_attr,
18247 (int) ATTR_FLAG_TYPE_IN_PLACE);
18250 /* Finish up the enumeration. */
18251 if (type != error_mark_node)
18253 if (new_value_list)
18254 finish_enum_value_list (type);
18255 if (is_new_type)
18256 finish_enum (type);
18259 if (nested_name_specifier)
18261 if (CLASS_TYPE_P (nested_name_specifier))
18263 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18264 pop_scope (nested_name_specifier);
18266 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18268 pop_nested_namespace (nested_name_specifier);
18271 out:
18272 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18273 return type;
18276 /* Parse an enumerator-list. The enumerators all have the indicated
18277 TYPE.
18279 enumerator-list:
18280 enumerator-definition
18281 enumerator-list , enumerator-definition */
18283 static void
18284 cp_parser_enumerator_list (cp_parser* parser, tree type)
18286 while (true)
18288 /* Parse an enumerator-definition. */
18289 cp_parser_enumerator_definition (parser, type);
18291 /* If the next token is not a ',', we've reached the end of
18292 the list. */
18293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18294 break;
18295 /* Otherwise, consume the `,' and keep going. */
18296 cp_lexer_consume_token (parser->lexer);
18297 /* If the next token is a `}', there is a trailing comma. */
18298 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18300 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18301 pedwarn (input_location, OPT_Wpedantic,
18302 "comma at end of enumerator list");
18303 break;
18308 /* Parse an enumerator-definition. The enumerator has the indicated
18309 TYPE.
18311 enumerator-definition:
18312 enumerator
18313 enumerator = constant-expression
18315 enumerator:
18316 identifier
18318 GNU Extensions:
18320 enumerator-definition:
18321 enumerator attributes [opt]
18322 enumerator attributes [opt] = constant-expression */
18324 static void
18325 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18327 tree identifier;
18328 tree value;
18329 location_t loc;
18331 /* Save the input location because we are interested in the location
18332 of the identifier and not the location of the explicit value. */
18333 loc = cp_lexer_peek_token (parser->lexer)->location;
18335 /* Look for the identifier. */
18336 identifier = cp_parser_identifier (parser);
18337 if (identifier == error_mark_node)
18338 return;
18340 /* Parse any specified attributes. */
18341 tree attrs = cp_parser_attributes_opt (parser);
18343 /* If the next token is an '=', then there is an explicit value. */
18344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18346 /* Consume the `=' token. */
18347 cp_lexer_consume_token (parser->lexer);
18348 /* Parse the value. */
18349 value = cp_parser_constant_expression (parser);
18351 else
18352 value = NULL_TREE;
18354 /* If we are processing a template, make sure the initializer of the
18355 enumerator doesn't contain any bare template parameter pack. */
18356 if (check_for_bare_parameter_packs (value))
18357 value = error_mark_node;
18359 /* Create the enumerator. */
18360 build_enumerator (identifier, value, type, attrs, loc);
18363 /* Parse a namespace-name.
18365 namespace-name:
18366 original-namespace-name
18367 namespace-alias
18369 Returns the NAMESPACE_DECL for the namespace. */
18371 static tree
18372 cp_parser_namespace_name (cp_parser* parser)
18374 tree identifier;
18375 tree namespace_decl;
18377 cp_token *token = cp_lexer_peek_token (parser->lexer);
18379 /* Get the name of the namespace. */
18380 identifier = cp_parser_identifier (parser);
18381 if (identifier == error_mark_node)
18382 return error_mark_node;
18384 /* Look up the identifier in the currently active scope. Look only
18385 for namespaces, due to:
18387 [basic.lookup.udir]
18389 When looking up a namespace-name in a using-directive or alias
18390 definition, only namespace names are considered.
18392 And:
18394 [basic.lookup.qual]
18396 During the lookup of a name preceding the :: scope resolution
18397 operator, object, function, and enumerator names are ignored.
18399 (Note that cp_parser_qualifying_entity only calls this
18400 function if the token after the name is the scope resolution
18401 operator.) */
18402 namespace_decl = cp_parser_lookup_name (parser, identifier,
18403 none_type,
18404 /*is_template=*/false,
18405 /*is_namespace=*/true,
18406 /*check_dependency=*/true,
18407 /*ambiguous_decls=*/NULL,
18408 token->location);
18409 /* If it's not a namespace, issue an error. */
18410 if (namespace_decl == error_mark_node
18411 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18413 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18415 error_at (token->location, "%qD is not a namespace-name", identifier);
18416 if (namespace_decl == error_mark_node
18417 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18418 suggest_alternative_in_explicit_scope (token->location, identifier,
18419 parser->scope);
18421 cp_parser_error (parser, "expected namespace-name");
18422 namespace_decl = error_mark_node;
18425 return namespace_decl;
18428 /* Parse a namespace-definition.
18430 namespace-definition:
18431 named-namespace-definition
18432 unnamed-namespace-definition
18434 named-namespace-definition:
18435 original-namespace-definition
18436 extension-namespace-definition
18438 original-namespace-definition:
18439 namespace identifier { namespace-body }
18441 extension-namespace-definition:
18442 namespace original-namespace-name { namespace-body }
18444 unnamed-namespace-definition:
18445 namespace { namespace-body } */
18447 static void
18448 cp_parser_namespace_definition (cp_parser* parser)
18450 tree identifier;
18451 int nested_definition_count = 0;
18453 cp_ensure_no_omp_declare_simd (parser);
18454 cp_ensure_no_oacc_routine (parser);
18456 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18458 if (is_inline)
18460 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18461 cp_lexer_consume_token (parser->lexer);
18464 /* Look for the `namespace' keyword. */
18465 cp_token* token
18466 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18468 /* Parse any specified attributes before the identifier. */
18469 tree attribs = cp_parser_attributes_opt (parser);
18471 for (;;)
18473 identifier = NULL_TREE;
18475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18477 identifier = cp_parser_identifier (parser);
18479 /* Parse any attributes specified after the identifier. */
18480 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18484 break;
18486 if (!nested_definition_count && cxx_dialect < cxx17)
18487 pedwarn (input_location, OPT_Wpedantic,
18488 "nested namespace definitions only available with "
18489 "-std=c++17 or -std=gnu++17");
18491 /* Nested namespace names can create new namespaces (unlike
18492 other qualified-ids). */
18493 if (int count = identifier ? push_namespace (identifier) : 0)
18494 nested_definition_count += count;
18495 else
18496 cp_parser_error (parser, "nested namespace name required");
18497 cp_lexer_consume_token (parser->lexer);
18500 if (nested_definition_count && !identifier)
18501 cp_parser_error (parser, "namespace name required");
18503 if (nested_definition_count && attribs)
18504 error_at (token->location,
18505 "a nested namespace definition cannot have attributes");
18506 if (nested_definition_count && is_inline)
18507 error_at (token->location,
18508 "a nested namespace definition cannot be inline");
18510 /* Start the namespace. */
18511 nested_definition_count += push_namespace (identifier, is_inline);
18513 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18515 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18517 /* Look for the `{' to validate starting the namespace. */
18518 matching_braces braces;
18519 if (braces.require_open (parser))
18521 /* Parse the body of the namespace. */
18522 cp_parser_namespace_body (parser);
18524 /* Look for the final `}'. */
18525 braces.require_close (parser);
18528 if (has_visibility)
18529 pop_visibility (1);
18531 /* Pop the nested namespace definitions. */
18532 while (nested_definition_count--)
18533 pop_namespace ();
18536 /* Parse a namespace-body.
18538 namespace-body:
18539 declaration-seq [opt] */
18541 static void
18542 cp_parser_namespace_body (cp_parser* parser)
18544 cp_parser_declaration_seq_opt (parser);
18547 /* Parse a namespace-alias-definition.
18549 namespace-alias-definition:
18550 namespace identifier = qualified-namespace-specifier ; */
18552 static void
18553 cp_parser_namespace_alias_definition (cp_parser* parser)
18555 tree identifier;
18556 tree namespace_specifier;
18558 cp_token *token = cp_lexer_peek_token (parser->lexer);
18560 /* Look for the `namespace' keyword. */
18561 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18562 /* Look for the identifier. */
18563 identifier = cp_parser_identifier (parser);
18564 if (identifier == error_mark_node)
18565 return;
18566 /* Look for the `=' token. */
18567 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18568 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18570 error_at (token->location, "%<namespace%> definition is not allowed here");
18571 /* Skip the definition. */
18572 cp_lexer_consume_token (parser->lexer);
18573 if (cp_parser_skip_to_closing_brace (parser))
18574 cp_lexer_consume_token (parser->lexer);
18575 return;
18577 cp_parser_require (parser, CPP_EQ, RT_EQ);
18578 /* Look for the qualified-namespace-specifier. */
18579 namespace_specifier
18580 = cp_parser_qualified_namespace_specifier (parser);
18581 /* Look for the `;' token. */
18582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18584 /* Register the alias in the symbol table. */
18585 do_namespace_alias (identifier, namespace_specifier);
18588 /* Parse a qualified-namespace-specifier.
18590 qualified-namespace-specifier:
18591 :: [opt] nested-name-specifier [opt] namespace-name
18593 Returns a NAMESPACE_DECL corresponding to the specified
18594 namespace. */
18596 static tree
18597 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18599 /* Look for the optional `::'. */
18600 cp_parser_global_scope_opt (parser,
18601 /*current_scope_valid_p=*/false);
18603 /* Look for the optional nested-name-specifier. */
18604 cp_parser_nested_name_specifier_opt (parser,
18605 /*typename_keyword_p=*/false,
18606 /*check_dependency_p=*/true,
18607 /*type_p=*/false,
18608 /*is_declaration=*/true);
18610 return cp_parser_namespace_name (parser);
18613 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18614 access declaration.
18616 using-declaration:
18617 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18618 using :: unqualified-id ;
18620 access-declaration:
18621 qualified-id ;
18625 static bool
18626 cp_parser_using_declaration (cp_parser* parser,
18627 bool access_declaration_p)
18629 cp_token *token;
18630 bool typename_p = false;
18631 bool global_scope_p;
18632 tree decl;
18633 tree identifier;
18634 tree qscope;
18635 int oldcount = errorcount;
18636 cp_token *diag_token = NULL;
18638 if (access_declaration_p)
18640 diag_token = cp_lexer_peek_token (parser->lexer);
18641 cp_parser_parse_tentatively (parser);
18643 else
18645 /* Look for the `using' keyword. */
18646 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18648 again:
18649 /* Peek at the next token. */
18650 token = cp_lexer_peek_token (parser->lexer);
18651 /* See if it's `typename'. */
18652 if (token->keyword == RID_TYPENAME)
18654 /* Remember that we've seen it. */
18655 typename_p = true;
18656 /* Consume the `typename' token. */
18657 cp_lexer_consume_token (parser->lexer);
18661 /* Look for the optional global scope qualification. */
18662 global_scope_p
18663 = (cp_parser_global_scope_opt (parser,
18664 /*current_scope_valid_p=*/false)
18665 != NULL_TREE);
18667 /* If we saw `typename', or didn't see `::', then there must be a
18668 nested-name-specifier present. */
18669 if (typename_p || !global_scope_p)
18671 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18672 /*check_dependency_p=*/true,
18673 /*type_p=*/false,
18674 /*is_declaration=*/true);
18675 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18677 cp_parser_skip_to_end_of_block_or_statement (parser);
18678 return false;
18681 /* Otherwise, we could be in either of the two productions. In that
18682 case, treat the nested-name-specifier as optional. */
18683 else
18684 qscope = cp_parser_nested_name_specifier_opt (parser,
18685 /*typename_keyword_p=*/false,
18686 /*check_dependency_p=*/true,
18687 /*type_p=*/false,
18688 /*is_declaration=*/true);
18689 if (!qscope)
18690 qscope = global_namespace;
18691 else if (UNSCOPED_ENUM_P (qscope))
18692 qscope = CP_TYPE_CONTEXT (qscope);
18694 if (access_declaration_p && cp_parser_error_occurred (parser))
18695 /* Something has already gone wrong; there's no need to parse
18696 further. Since an error has occurred, the return value of
18697 cp_parser_parse_definitely will be false, as required. */
18698 return cp_parser_parse_definitely (parser);
18700 token = cp_lexer_peek_token (parser->lexer);
18701 /* Parse the unqualified-id. */
18702 identifier = cp_parser_unqualified_id (parser,
18703 /*template_keyword_p=*/false,
18704 /*check_dependency_p=*/true,
18705 /*declarator_p=*/true,
18706 /*optional_p=*/false);
18708 if (access_declaration_p)
18710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18711 cp_parser_simulate_error (parser);
18712 if (!cp_parser_parse_definitely (parser))
18713 return false;
18715 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18717 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18718 if (cxx_dialect < cxx17
18719 && !in_system_header_at (ell->location))
18720 pedwarn (ell->location, 0,
18721 "pack expansion in using-declaration only available "
18722 "with -std=c++17 or -std=gnu++17");
18723 qscope = make_pack_expansion (qscope);
18726 /* The function we call to handle a using-declaration is different
18727 depending on what scope we are in. */
18728 if (qscope == error_mark_node || identifier == error_mark_node)
18730 else if (!identifier_p (identifier)
18731 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18732 /* [namespace.udecl]
18734 A using declaration shall not name a template-id. */
18735 error_at (token->location,
18736 "a template-id may not appear in a using-declaration");
18737 else
18739 if (at_class_scope_p ())
18741 /* Create the USING_DECL. */
18742 decl = do_class_using_decl (qscope, identifier);
18744 if (decl && typename_p)
18745 USING_DECL_TYPENAME_P (decl) = 1;
18747 if (check_for_bare_parameter_packs (decl))
18749 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18750 return false;
18752 else
18753 /* Add it to the list of members in this class. */
18754 finish_member_declaration (decl);
18756 else
18758 decl = cp_parser_lookup_name_simple (parser,
18759 identifier,
18760 token->location);
18761 if (decl == error_mark_node)
18762 cp_parser_name_lookup_error (parser, identifier,
18763 decl, NLE_NULL,
18764 token->location);
18765 else if (check_for_bare_parameter_packs (decl))
18767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18768 return false;
18770 else if (!at_namespace_scope_p ())
18771 finish_local_using_decl (decl, qscope, identifier);
18772 else
18773 finish_namespace_using_decl (decl, qscope, identifier);
18777 if (!access_declaration_p
18778 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18780 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18781 if (cxx_dialect < cxx17)
18782 pedwarn (comma->location, 0,
18783 "comma-separated list in using-declaration only available "
18784 "with -std=c++17 or -std=gnu++17");
18785 goto again;
18788 /* Look for the final `;'. */
18789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18791 if (access_declaration_p && errorcount == oldcount)
18792 warning_at (diag_token->location, OPT_Wdeprecated,
18793 "access declarations are deprecated "
18794 "in favour of using-declarations; "
18795 "suggestion: add the %<using%> keyword");
18797 return true;
18800 /* Parse an alias-declaration.
18802 alias-declaration:
18803 using identifier attribute-specifier-seq [opt] = type-id */
18805 static tree
18806 cp_parser_alias_declaration (cp_parser* parser)
18808 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18809 location_t id_location;
18810 cp_declarator *declarator;
18811 cp_decl_specifier_seq decl_specs;
18812 bool member_p;
18813 const char *saved_message = NULL;
18815 /* Look for the `using' keyword. */
18816 cp_token *using_token
18817 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18818 if (using_token == NULL)
18819 return error_mark_node;
18821 id_location = cp_lexer_peek_token (parser->lexer)->location;
18822 id = cp_parser_identifier (parser);
18823 if (id == error_mark_node)
18824 return error_mark_node;
18826 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18827 attributes = cp_parser_attributes_opt (parser);
18828 if (attributes == error_mark_node)
18829 return error_mark_node;
18831 cp_parser_require (parser, CPP_EQ, RT_EQ);
18833 if (cp_parser_error_occurred (parser))
18834 return error_mark_node;
18836 cp_parser_commit_to_tentative_parse (parser);
18838 /* Now we are going to parse the type-id of the declaration. */
18841 [dcl.type]/3 says:
18843 "A type-specifier-seq shall not define a class or enumeration
18844 unless it appears in the type-id of an alias-declaration (7.1.3) that
18845 is not the declaration of a template-declaration."
18847 In other words, if we currently are in an alias template, the
18848 type-id should not define a type.
18850 So let's set parser->type_definition_forbidden_message in that
18851 case; cp_parser_check_type_definition (called by
18852 cp_parser_class_specifier) will then emit an error if a type is
18853 defined in the type-id. */
18854 if (parser->num_template_parameter_lists)
18856 saved_message = parser->type_definition_forbidden_message;
18857 parser->type_definition_forbidden_message =
18858 G_("types may not be defined in alias template declarations");
18861 type = cp_parser_type_id (parser);
18863 /* Restore the error message if need be. */
18864 if (parser->num_template_parameter_lists)
18865 parser->type_definition_forbidden_message = saved_message;
18867 if (type == error_mark_node
18868 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18870 cp_parser_skip_to_end_of_block_or_statement (parser);
18871 return error_mark_node;
18874 /* A typedef-name can also be introduced by an alias-declaration. The
18875 identifier following the using keyword becomes a typedef-name. It has
18876 the same semantics as if it were introduced by the typedef
18877 specifier. In particular, it does not define a new type and it shall
18878 not appear in the type-id. */
18880 clear_decl_specs (&decl_specs);
18881 decl_specs.type = type;
18882 if (attributes != NULL_TREE)
18884 decl_specs.attributes = attributes;
18885 set_and_check_decl_spec_loc (&decl_specs,
18886 ds_attribute,
18887 attrs_token);
18889 set_and_check_decl_spec_loc (&decl_specs,
18890 ds_typedef,
18891 using_token);
18892 set_and_check_decl_spec_loc (&decl_specs,
18893 ds_alias,
18894 using_token);
18896 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18897 declarator->id_loc = id_location;
18899 member_p = at_class_scope_p ();
18900 if (member_p)
18901 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18902 NULL_TREE, attributes);
18903 else
18904 decl = start_decl (declarator, &decl_specs, 0,
18905 attributes, NULL_TREE, &pushed_scope);
18906 if (decl == error_mark_node)
18907 return decl;
18909 // Attach constraints to the alias declaration.
18910 if (flag_concepts && current_template_parms)
18912 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18913 tree constr = build_constraints (reqs, NULL_TREE);
18914 set_constraints (decl, constr);
18917 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18919 if (pushed_scope)
18920 pop_scope (pushed_scope);
18922 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18923 added into the symbol table; otherwise, return the TYPE_DECL. */
18924 if (DECL_LANG_SPECIFIC (decl)
18925 && DECL_TEMPLATE_INFO (decl)
18926 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18928 decl = DECL_TI_TEMPLATE (decl);
18929 if (member_p)
18930 check_member_template (decl);
18933 return decl;
18936 /* Parse a using-directive.
18938 using-directive:
18939 using namespace :: [opt] nested-name-specifier [opt]
18940 namespace-name ; */
18942 static void
18943 cp_parser_using_directive (cp_parser* parser)
18945 tree namespace_decl;
18946 tree attribs;
18948 /* Look for the `using' keyword. */
18949 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18950 /* And the `namespace' keyword. */
18951 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18952 /* Look for the optional `::' operator. */
18953 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18954 /* And the optional nested-name-specifier. */
18955 cp_parser_nested_name_specifier_opt (parser,
18956 /*typename_keyword_p=*/false,
18957 /*check_dependency_p=*/true,
18958 /*type_p=*/false,
18959 /*is_declaration=*/true);
18960 /* Get the namespace being used. */
18961 namespace_decl = cp_parser_namespace_name (parser);
18962 /* And any specified attributes. */
18963 attribs = cp_parser_attributes_opt (parser);
18965 /* Update the symbol table. */
18966 if (namespace_bindings_p ())
18967 finish_namespace_using_directive (namespace_decl, attribs);
18968 else
18969 finish_local_using_directive (namespace_decl, attribs);
18971 /* Look for the final `;'. */
18972 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18975 /* Parse an asm-definition.
18977 asm-definition:
18978 asm ( string-literal ) ;
18980 GNU Extension:
18982 asm-definition:
18983 asm volatile [opt] ( string-literal ) ;
18984 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18985 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18986 : asm-operand-list [opt] ) ;
18987 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18988 : asm-operand-list [opt]
18989 : asm-clobber-list [opt] ) ;
18990 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18991 : asm-clobber-list [opt]
18992 : asm-goto-list ) ; */
18994 static void
18995 cp_parser_asm_definition (cp_parser* parser)
18997 tree string;
18998 tree outputs = NULL_TREE;
18999 tree inputs = NULL_TREE;
19000 tree clobbers = NULL_TREE;
19001 tree labels = NULL_TREE;
19002 tree asm_stmt;
19003 bool volatile_p = false;
19004 bool extended_p = false;
19005 bool invalid_inputs_p = false;
19006 bool invalid_outputs_p = false;
19007 bool goto_p = false;
19008 required_token missing = RT_NONE;
19010 /* Look for the `asm' keyword. */
19011 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19013 if (parser->in_function_body
19014 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19016 error ("%<asm%> in %<constexpr%> function");
19017 cp_function_chain->invalid_constexpr = true;
19020 /* See if the next token is `volatile'. */
19021 if (cp_parser_allow_gnu_extensions_p (parser)
19022 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19024 /* Remember that we saw the `volatile' keyword. */
19025 volatile_p = true;
19026 /* Consume the token. */
19027 cp_lexer_consume_token (parser->lexer);
19029 if (cp_parser_allow_gnu_extensions_p (parser)
19030 && parser->in_function_body
19031 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19033 /* Remember that we saw the `goto' keyword. */
19034 goto_p = true;
19035 /* Consume the token. */
19036 cp_lexer_consume_token (parser->lexer);
19038 /* Look for the opening `('. */
19039 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19040 return;
19041 /* Look for the string. */
19042 string = cp_parser_string_literal (parser, false, false);
19043 if (string == error_mark_node)
19045 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19046 /*consume_paren=*/true);
19047 return;
19050 /* If we're allowing GNU extensions, check for the extended assembly
19051 syntax. Unfortunately, the `:' tokens need not be separated by
19052 a space in C, and so, for compatibility, we tolerate that here
19053 too. Doing that means that we have to treat the `::' operator as
19054 two `:' tokens. */
19055 if (cp_parser_allow_gnu_extensions_p (parser)
19056 && parser->in_function_body
19057 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19058 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19060 bool inputs_p = false;
19061 bool clobbers_p = false;
19062 bool labels_p = false;
19064 /* The extended syntax was used. */
19065 extended_p = true;
19067 /* Look for outputs. */
19068 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19070 /* Consume the `:'. */
19071 cp_lexer_consume_token (parser->lexer);
19072 /* Parse the output-operands. */
19073 if (cp_lexer_next_token_is_not (parser->lexer,
19074 CPP_COLON)
19075 && cp_lexer_next_token_is_not (parser->lexer,
19076 CPP_SCOPE)
19077 && cp_lexer_next_token_is_not (parser->lexer,
19078 CPP_CLOSE_PAREN)
19079 && !goto_p)
19081 outputs = cp_parser_asm_operand_list (parser);
19082 if (outputs == error_mark_node)
19083 invalid_outputs_p = true;
19086 /* If the next token is `::', there are no outputs, and the
19087 next token is the beginning of the inputs. */
19088 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19089 /* The inputs are coming next. */
19090 inputs_p = true;
19092 /* Look for inputs. */
19093 if (inputs_p
19094 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19096 /* Consume the `:' or `::'. */
19097 cp_lexer_consume_token (parser->lexer);
19098 /* Parse the output-operands. */
19099 if (cp_lexer_next_token_is_not (parser->lexer,
19100 CPP_COLON)
19101 && cp_lexer_next_token_is_not (parser->lexer,
19102 CPP_SCOPE)
19103 && cp_lexer_next_token_is_not (parser->lexer,
19104 CPP_CLOSE_PAREN))
19106 inputs = cp_parser_asm_operand_list (parser);
19107 if (inputs == error_mark_node)
19108 invalid_inputs_p = true;
19111 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19112 /* The clobbers are coming next. */
19113 clobbers_p = true;
19115 /* Look for clobbers. */
19116 if (clobbers_p
19117 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19119 clobbers_p = true;
19120 /* Consume the `:' or `::'. */
19121 cp_lexer_consume_token (parser->lexer);
19122 /* Parse the clobbers. */
19123 if (cp_lexer_next_token_is_not (parser->lexer,
19124 CPP_COLON)
19125 && cp_lexer_next_token_is_not (parser->lexer,
19126 CPP_CLOSE_PAREN))
19127 clobbers = cp_parser_asm_clobber_list (parser);
19129 else if (goto_p
19130 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19131 /* The labels are coming next. */
19132 labels_p = true;
19134 /* Look for labels. */
19135 if (labels_p
19136 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19138 labels_p = true;
19139 /* Consume the `:' or `::'. */
19140 cp_lexer_consume_token (parser->lexer);
19141 /* Parse the labels. */
19142 labels = cp_parser_asm_label_list (parser);
19145 if (goto_p && !labels_p)
19146 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19148 else if (goto_p)
19149 missing = RT_COLON_SCOPE;
19151 /* Look for the closing `)'. */
19152 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19153 missing ? missing : RT_CLOSE_PAREN))
19154 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19155 /*consume_paren=*/true);
19156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19158 if (!invalid_inputs_p && !invalid_outputs_p)
19160 /* Create the ASM_EXPR. */
19161 if (parser->in_function_body)
19163 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19164 inputs, clobbers, labels);
19165 /* If the extended syntax was not used, mark the ASM_EXPR. */
19166 if (!extended_p)
19168 tree temp = asm_stmt;
19169 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19170 temp = TREE_OPERAND (temp, 0);
19172 ASM_INPUT_P (temp) = 1;
19175 else
19176 symtab->finalize_toplevel_asm (string);
19180 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19181 type that comes from the decl-specifier-seq. */
19183 static tree
19184 strip_declarator_types (tree type, cp_declarator *declarator)
19186 for (cp_declarator *d = declarator; d;)
19187 switch (d->kind)
19189 case cdk_id:
19190 case cdk_decomp:
19191 case cdk_error:
19192 d = NULL;
19193 break;
19195 default:
19196 if (TYPE_PTRMEMFUNC_P (type))
19197 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19198 type = TREE_TYPE (type);
19199 d = d->declarator;
19200 break;
19203 return type;
19206 /* Declarators [gram.dcl.decl] */
19208 /* Parse an init-declarator.
19210 init-declarator:
19211 declarator initializer [opt]
19213 GNU Extension:
19215 init-declarator:
19216 declarator asm-specification [opt] attributes [opt] initializer [opt]
19218 function-definition:
19219 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19220 function-body
19221 decl-specifier-seq [opt] declarator function-try-block
19223 GNU Extension:
19225 function-definition:
19226 __extension__ function-definition
19228 TM Extension:
19230 function-definition:
19231 decl-specifier-seq [opt] declarator function-transaction-block
19233 The DECL_SPECIFIERS apply to this declarator. Returns a
19234 representation of the entity declared. If MEMBER_P is TRUE, then
19235 this declarator appears in a class scope. The new DECL created by
19236 this declarator is returned.
19238 The CHECKS are access checks that should be performed once we know
19239 what entity is being declared (and, therefore, what classes have
19240 befriended it).
19242 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19243 for a function-definition here as well. If the declarator is a
19244 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19245 be TRUE upon return. By that point, the function-definition will
19246 have been completely parsed.
19248 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19249 is FALSE.
19251 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19252 parsed declaration if it is an uninitialized single declarator not followed
19253 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19254 if present, will not be consumed. If returned, this declarator will be
19255 created with SD_INITIALIZED but will not call cp_finish_decl.
19257 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19258 and there is an initializer, the pointed location_t is set to the
19259 location of the '=' or `(', or '{' in C++11 token introducing the
19260 initializer. */
19262 static tree
19263 cp_parser_init_declarator (cp_parser* parser,
19264 cp_decl_specifier_seq *decl_specifiers,
19265 vec<deferred_access_check, va_gc> *checks,
19266 bool function_definition_allowed_p,
19267 bool member_p,
19268 int declares_class_or_enum,
19269 bool* function_definition_p,
19270 tree* maybe_range_for_decl,
19271 location_t* init_loc,
19272 tree* auto_result)
19274 cp_token *token = NULL, *asm_spec_start_token = NULL,
19275 *attributes_start_token = NULL;
19276 cp_declarator *declarator;
19277 tree prefix_attributes;
19278 tree attributes = NULL;
19279 tree asm_specification;
19280 tree initializer;
19281 tree decl = NULL_TREE;
19282 tree scope;
19283 int is_initialized;
19284 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19285 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19286 "(...)". */
19287 enum cpp_ttype initialization_kind;
19288 bool is_direct_init = false;
19289 bool is_non_constant_init;
19290 int ctor_dtor_or_conv_p;
19291 bool friend_p = cp_parser_friend_p (decl_specifiers);
19292 tree pushed_scope = NULL_TREE;
19293 bool range_for_decl_p = false;
19294 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19295 location_t tmp_init_loc = UNKNOWN_LOCATION;
19297 /* Gather the attributes that were provided with the
19298 decl-specifiers. */
19299 prefix_attributes = decl_specifiers->attributes;
19301 /* Assume that this is not the declarator for a function
19302 definition. */
19303 if (function_definition_p)
19304 *function_definition_p = false;
19306 /* Default arguments are only permitted for function parameters. */
19307 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19308 parser->default_arg_ok_p = false;
19310 /* Defer access checks while parsing the declarator; we cannot know
19311 what names are accessible until we know what is being
19312 declared. */
19313 resume_deferring_access_checks ();
19315 token = cp_lexer_peek_token (parser->lexer);
19317 /* Parse the declarator. */
19318 declarator
19319 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19320 &ctor_dtor_or_conv_p,
19321 /*parenthesized_p=*/NULL,
19322 member_p, friend_p);
19323 /* Gather up the deferred checks. */
19324 stop_deferring_access_checks ();
19326 parser->default_arg_ok_p = saved_default_arg_ok_p;
19328 /* If the DECLARATOR was erroneous, there's no need to go
19329 further. */
19330 if (declarator == cp_error_declarator)
19331 return error_mark_node;
19333 /* Check that the number of template-parameter-lists is OK. */
19334 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19335 token->location))
19336 return error_mark_node;
19338 if (declares_class_or_enum & 2)
19339 cp_parser_check_for_definition_in_return_type (declarator,
19340 decl_specifiers->type,
19341 decl_specifiers->locations[ds_type_spec]);
19343 /* Figure out what scope the entity declared by the DECLARATOR is
19344 located in. `grokdeclarator' sometimes changes the scope, so
19345 we compute it now. */
19346 scope = get_scope_of_declarator (declarator);
19348 /* Perform any lookups in the declared type which were thought to be
19349 dependent, but are not in the scope of the declarator. */
19350 decl_specifiers->type
19351 = maybe_update_decl_type (decl_specifiers->type, scope);
19353 /* If we're allowing GNU extensions, look for an
19354 asm-specification. */
19355 if (cp_parser_allow_gnu_extensions_p (parser))
19357 /* Look for an asm-specification. */
19358 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19359 asm_specification = cp_parser_asm_specification_opt (parser);
19361 else
19362 asm_specification = NULL_TREE;
19364 /* Look for attributes. */
19365 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19366 attributes = cp_parser_attributes_opt (parser);
19368 /* Peek at the next token. */
19369 token = cp_lexer_peek_token (parser->lexer);
19371 bool bogus_implicit_tmpl = false;
19373 if (function_declarator_p (declarator))
19375 /* Handle C++17 deduction guides. */
19376 if (!decl_specifiers->type
19377 && ctor_dtor_or_conv_p <= 0
19378 && cxx_dialect >= cxx17)
19380 cp_declarator *id = get_id_declarator (declarator);
19381 tree name = id->u.id.unqualified_name;
19382 parser->scope = id->u.id.qualifying_scope;
19383 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19384 if (tmpl
19385 && (DECL_CLASS_TEMPLATE_P (tmpl)
19386 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19388 id->u.id.unqualified_name = dguide_name (tmpl);
19389 id->u.id.sfk = sfk_deduction_guide;
19390 ctor_dtor_or_conv_p = 1;
19394 /* Check to see if the token indicates the start of a
19395 function-definition. */
19396 if (cp_parser_token_starts_function_definition_p (token))
19398 if (!function_definition_allowed_p)
19400 /* If a function-definition should not appear here, issue an
19401 error message. */
19402 cp_parser_error (parser,
19403 "a function-definition is not allowed here");
19404 return error_mark_node;
19407 location_t func_brace_location
19408 = cp_lexer_peek_token (parser->lexer)->location;
19410 /* Neither attributes nor an asm-specification are allowed
19411 on a function-definition. */
19412 if (asm_specification)
19413 error_at (asm_spec_start_token->location,
19414 "an asm-specification is not allowed "
19415 "on a function-definition");
19416 if (attributes)
19417 error_at (attributes_start_token->location,
19418 "attributes are not allowed "
19419 "on a function-definition");
19420 /* This is a function-definition. */
19421 *function_definition_p = true;
19423 /* Parse the function definition. */
19424 if (member_p)
19425 decl = cp_parser_save_member_function_body (parser,
19426 decl_specifiers,
19427 declarator,
19428 prefix_attributes);
19429 else
19430 decl =
19431 (cp_parser_function_definition_from_specifiers_and_declarator
19432 (parser, decl_specifiers, prefix_attributes, declarator));
19434 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19436 /* This is where the prologue starts... */
19437 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19438 = func_brace_location;
19441 return decl;
19444 else if (parser->fully_implicit_function_template_p)
19446 /* A non-template declaration involving a function parameter list
19447 containing an implicit template parameter will be made into a
19448 template. If the resulting declaration is not going to be an
19449 actual function then finish the template scope here to prevent it.
19450 An error message will be issued once we have a decl to talk about.
19452 FIXME probably we should do type deduction rather than create an
19453 implicit template, but the standard currently doesn't allow it. */
19454 bogus_implicit_tmpl = true;
19455 finish_fully_implicit_template (parser, NULL_TREE);
19458 /* [dcl.dcl]
19460 Only in function declarations for constructors, destructors, type
19461 conversions, and deduction guides can the decl-specifier-seq be omitted.
19463 We explicitly postpone this check past the point where we handle
19464 function-definitions because we tolerate function-definitions
19465 that are missing their return types in some modes. */
19466 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19468 cp_parser_error (parser,
19469 "expected constructor, destructor, or type conversion");
19470 return error_mark_node;
19473 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19474 if (token->type == CPP_EQ
19475 || token->type == CPP_OPEN_PAREN
19476 || token->type == CPP_OPEN_BRACE)
19478 is_initialized = SD_INITIALIZED;
19479 initialization_kind = token->type;
19480 if (maybe_range_for_decl)
19481 *maybe_range_for_decl = error_mark_node;
19482 tmp_init_loc = token->location;
19483 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19484 *init_loc = tmp_init_loc;
19486 if (token->type == CPP_EQ
19487 && function_declarator_p (declarator))
19489 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19490 if (t2->keyword == RID_DEFAULT)
19491 is_initialized = SD_DEFAULTED;
19492 else if (t2->keyword == RID_DELETE)
19493 is_initialized = SD_DELETED;
19496 else
19498 /* If the init-declarator isn't initialized and isn't followed by a
19499 `,' or `;', it's not a valid init-declarator. */
19500 if (token->type != CPP_COMMA
19501 && token->type != CPP_SEMICOLON)
19503 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19504 range_for_decl_p = true;
19505 else
19507 if (!maybe_range_for_decl)
19508 cp_parser_error (parser, "expected initializer");
19509 return error_mark_node;
19512 is_initialized = SD_UNINITIALIZED;
19513 initialization_kind = CPP_EOF;
19516 /* Because start_decl has side-effects, we should only call it if we
19517 know we're going ahead. By this point, we know that we cannot
19518 possibly be looking at any other construct. */
19519 cp_parser_commit_to_tentative_parse (parser);
19521 /* Enter the newly declared entry in the symbol table. If we're
19522 processing a declaration in a class-specifier, we wait until
19523 after processing the initializer. */
19524 if (!member_p)
19526 if (parser->in_unbraced_linkage_specification_p)
19527 decl_specifiers->storage_class = sc_extern;
19528 decl = start_decl (declarator, decl_specifiers,
19529 range_for_decl_p? SD_INITIALIZED : is_initialized,
19530 attributes, prefix_attributes, &pushed_scope);
19531 cp_finalize_omp_declare_simd (parser, decl);
19532 cp_finalize_oacc_routine (parser, decl, false);
19533 /* Adjust location of decl if declarator->id_loc is more appropriate:
19534 set, and decl wasn't merged with another decl, in which case its
19535 location would be different from input_location, and more accurate. */
19536 if (DECL_P (decl)
19537 && declarator->id_loc != UNKNOWN_LOCATION
19538 && DECL_SOURCE_LOCATION (decl) == input_location)
19539 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19541 else if (scope)
19542 /* Enter the SCOPE. That way unqualified names appearing in the
19543 initializer will be looked up in SCOPE. */
19544 pushed_scope = push_scope (scope);
19546 /* Perform deferred access control checks, now that we know in which
19547 SCOPE the declared entity resides. */
19548 if (!member_p && decl)
19550 tree saved_current_function_decl = NULL_TREE;
19552 /* If the entity being declared is a function, pretend that we
19553 are in its scope. If it is a `friend', it may have access to
19554 things that would not otherwise be accessible. */
19555 if (TREE_CODE (decl) == FUNCTION_DECL)
19557 saved_current_function_decl = current_function_decl;
19558 current_function_decl = decl;
19561 /* Perform access checks for template parameters. */
19562 cp_parser_perform_template_parameter_access_checks (checks);
19564 /* Perform the access control checks for the declarator and the
19565 decl-specifiers. */
19566 perform_deferred_access_checks (tf_warning_or_error);
19568 /* Restore the saved value. */
19569 if (TREE_CODE (decl) == FUNCTION_DECL)
19570 current_function_decl = saved_current_function_decl;
19573 /* Parse the initializer. */
19574 initializer = NULL_TREE;
19575 is_direct_init = false;
19576 is_non_constant_init = true;
19577 if (is_initialized)
19579 if (function_declarator_p (declarator))
19581 if (initialization_kind == CPP_EQ)
19582 initializer = cp_parser_pure_specifier (parser);
19583 else
19585 /* If the declaration was erroneous, we don't really
19586 know what the user intended, so just silently
19587 consume the initializer. */
19588 if (decl != error_mark_node)
19589 error_at (tmp_init_loc, "initializer provided for function");
19590 cp_parser_skip_to_closing_parenthesis (parser,
19591 /*recovering=*/true,
19592 /*or_comma=*/false,
19593 /*consume_paren=*/true);
19596 else
19598 /* We want to record the extra mangling scope for in-class
19599 initializers of class members and initializers of static data
19600 member templates. The former involves deferring
19601 parsing of the initializer until end of class as with default
19602 arguments. So right here we only handle the latter. */
19603 if (!member_p && processing_template_decl)
19604 start_lambda_scope (decl);
19605 initializer = cp_parser_initializer (parser,
19606 &is_direct_init,
19607 &is_non_constant_init);
19608 if (!member_p && processing_template_decl)
19609 finish_lambda_scope ();
19610 if (initializer == error_mark_node)
19611 cp_parser_skip_to_end_of_statement (parser);
19615 /* The old parser allows attributes to appear after a parenthesized
19616 initializer. Mark Mitchell proposed removing this functionality
19617 on the GCC mailing lists on 2002-08-13. This parser accepts the
19618 attributes -- but ignores them. */
19619 if (cp_parser_allow_gnu_extensions_p (parser)
19620 && initialization_kind == CPP_OPEN_PAREN)
19621 if (cp_parser_attributes_opt (parser))
19622 warning (OPT_Wattributes,
19623 "attributes after parenthesized initializer ignored");
19625 /* And now complain about a non-function implicit template. */
19626 if (bogus_implicit_tmpl && decl != error_mark_node)
19627 error_at (DECL_SOURCE_LOCATION (decl),
19628 "non-function %qD declared as implicit template", decl);
19630 /* For an in-class declaration, use `grokfield' to create the
19631 declaration. */
19632 if (member_p)
19634 if (pushed_scope)
19636 pop_scope (pushed_scope);
19637 pushed_scope = NULL_TREE;
19639 decl = grokfield (declarator, decl_specifiers,
19640 initializer, !is_non_constant_init,
19641 /*asmspec=*/NULL_TREE,
19642 chainon (attributes, prefix_attributes));
19643 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19644 cp_parser_save_default_args (parser, decl);
19645 cp_finalize_omp_declare_simd (parser, decl);
19646 cp_finalize_oacc_routine (parser, decl, false);
19649 /* Finish processing the declaration. But, skip member
19650 declarations. */
19651 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19653 cp_finish_decl (decl,
19654 initializer, !is_non_constant_init,
19655 asm_specification,
19656 /* If the initializer is in parentheses, then this is
19657 a direct-initialization, which means that an
19658 `explicit' constructor is OK. Otherwise, an
19659 `explicit' constructor cannot be used. */
19660 ((is_direct_init || !is_initialized)
19661 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19663 else if ((cxx_dialect != cxx98) && friend_p
19664 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19665 /* Core issue #226 (C++0x only): A default template-argument
19666 shall not be specified in a friend class template
19667 declaration. */
19668 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19669 /*is_partial=*/false, /*is_friend_decl=*/1);
19671 if (!friend_p && pushed_scope)
19672 pop_scope (pushed_scope);
19674 if (function_declarator_p (declarator)
19675 && parser->fully_implicit_function_template_p)
19677 if (member_p)
19678 decl = finish_fully_implicit_template (parser, decl);
19679 else
19680 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19683 if (auto_result && is_initialized && decl_specifiers->type
19684 && type_uses_auto (decl_specifiers->type))
19685 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19687 return decl;
19690 /* Parse a declarator.
19692 declarator:
19693 direct-declarator
19694 ptr-operator declarator
19696 abstract-declarator:
19697 ptr-operator abstract-declarator [opt]
19698 direct-abstract-declarator
19700 GNU Extensions:
19702 declarator:
19703 attributes [opt] direct-declarator
19704 attributes [opt] ptr-operator declarator
19706 abstract-declarator:
19707 attributes [opt] ptr-operator abstract-declarator [opt]
19708 attributes [opt] direct-abstract-declarator
19710 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19711 detect constructors, destructors, deduction guides, or conversion operators.
19712 It is set to -1 if the declarator is a name, and +1 if it is a
19713 function. Otherwise it is set to zero. Usually you just want to
19714 test for >0, but internally the negative value is used.
19716 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19717 a decl-specifier-seq unless it declares a constructor, destructor,
19718 or conversion. It might seem that we could check this condition in
19719 semantic analysis, rather than parsing, but that makes it difficult
19720 to handle something like `f()'. We want to notice that there are
19721 no decl-specifiers, and therefore realize that this is an
19722 expression, not a declaration.)
19724 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19725 the declarator is a direct-declarator of the form "(...)".
19727 MEMBER_P is true iff this declarator is a member-declarator.
19729 FRIEND_P is true iff this declarator is a friend. */
19731 static cp_declarator *
19732 cp_parser_declarator (cp_parser* parser,
19733 cp_parser_declarator_kind dcl_kind,
19734 int* ctor_dtor_or_conv_p,
19735 bool* parenthesized_p,
19736 bool member_p, bool friend_p)
19738 cp_declarator *declarator;
19739 enum tree_code code;
19740 cp_cv_quals cv_quals;
19741 tree class_type;
19742 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19744 /* Assume this is not a constructor, destructor, or type-conversion
19745 operator. */
19746 if (ctor_dtor_or_conv_p)
19747 *ctor_dtor_or_conv_p = 0;
19749 if (cp_parser_allow_gnu_extensions_p (parser))
19750 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19752 /* Check for the ptr-operator production. */
19753 cp_parser_parse_tentatively (parser);
19754 /* Parse the ptr-operator. */
19755 code = cp_parser_ptr_operator (parser,
19756 &class_type,
19757 &cv_quals,
19758 &std_attributes);
19760 /* If that worked, then we have a ptr-operator. */
19761 if (cp_parser_parse_definitely (parser))
19763 /* If a ptr-operator was found, then this declarator was not
19764 parenthesized. */
19765 if (parenthesized_p)
19766 *parenthesized_p = true;
19767 /* The dependent declarator is optional if we are parsing an
19768 abstract-declarator. */
19769 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19770 cp_parser_parse_tentatively (parser);
19772 /* Parse the dependent declarator. */
19773 declarator = cp_parser_declarator (parser, dcl_kind,
19774 /*ctor_dtor_or_conv_p=*/NULL,
19775 /*parenthesized_p=*/NULL,
19776 /*member_p=*/false,
19777 friend_p);
19779 /* If we are parsing an abstract-declarator, we must handle the
19780 case where the dependent declarator is absent. */
19781 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19782 && !cp_parser_parse_definitely (parser))
19783 declarator = NULL;
19785 declarator = cp_parser_make_indirect_declarator
19786 (code, class_type, cv_quals, declarator, std_attributes);
19788 /* Everything else is a direct-declarator. */
19789 else
19791 if (parenthesized_p)
19792 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19793 CPP_OPEN_PAREN);
19794 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19795 ctor_dtor_or_conv_p,
19796 member_p, friend_p);
19799 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19800 declarator->attributes = gnu_attributes;
19801 return declarator;
19804 /* Parse a direct-declarator or direct-abstract-declarator.
19806 direct-declarator:
19807 declarator-id
19808 direct-declarator ( parameter-declaration-clause )
19809 cv-qualifier-seq [opt]
19810 ref-qualifier [opt]
19811 exception-specification [opt]
19812 direct-declarator [ constant-expression [opt] ]
19813 ( declarator )
19815 direct-abstract-declarator:
19816 direct-abstract-declarator [opt]
19817 ( parameter-declaration-clause )
19818 cv-qualifier-seq [opt]
19819 ref-qualifier [opt]
19820 exception-specification [opt]
19821 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19822 ( abstract-declarator )
19824 Returns a representation of the declarator. DCL_KIND is
19825 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19826 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19827 we are parsing a direct-declarator. It is
19828 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19829 of ambiguity we prefer an abstract declarator, as per
19830 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19831 as for cp_parser_declarator. */
19833 static cp_declarator *
19834 cp_parser_direct_declarator (cp_parser* parser,
19835 cp_parser_declarator_kind dcl_kind,
19836 int* ctor_dtor_or_conv_p,
19837 bool member_p, bool friend_p)
19839 cp_token *token;
19840 cp_declarator *declarator = NULL;
19841 tree scope = NULL_TREE;
19842 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19843 bool saved_in_declarator_p = parser->in_declarator_p;
19844 bool first = true;
19845 tree pushed_scope = NULL_TREE;
19846 cp_token *open_paren = NULL, *close_paren = NULL;
19848 while (true)
19850 /* Peek at the next token. */
19851 token = cp_lexer_peek_token (parser->lexer);
19852 if (token->type == CPP_OPEN_PAREN)
19854 /* This is either a parameter-declaration-clause, or a
19855 parenthesized declarator. When we know we are parsing a
19856 named declarator, it must be a parenthesized declarator
19857 if FIRST is true. For instance, `(int)' is a
19858 parameter-declaration-clause, with an omitted
19859 direct-abstract-declarator. But `((*))', is a
19860 parenthesized abstract declarator. Finally, when T is a
19861 template parameter `(T)' is a
19862 parameter-declaration-clause, and not a parenthesized
19863 named declarator.
19865 We first try and parse a parameter-declaration-clause,
19866 and then try a nested declarator (if FIRST is true).
19868 It is not an error for it not to be a
19869 parameter-declaration-clause, even when FIRST is
19870 false. Consider,
19872 int i (int);
19873 int i (3);
19875 The first is the declaration of a function while the
19876 second is the definition of a variable, including its
19877 initializer.
19879 Having seen only the parenthesis, we cannot know which of
19880 these two alternatives should be selected. Even more
19881 complex are examples like:
19883 int i (int (a));
19884 int i (int (3));
19886 The former is a function-declaration; the latter is a
19887 variable initialization.
19889 Thus again, we try a parameter-declaration-clause, and if
19890 that fails, we back out and return. */
19892 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19894 tree params;
19895 bool is_declarator = false;
19897 open_paren = NULL;
19899 /* In a member-declarator, the only valid interpretation
19900 of a parenthesis is the start of a
19901 parameter-declaration-clause. (It is invalid to
19902 initialize a static data member with a parenthesized
19903 initializer; only the "=" form of initialization is
19904 permitted.) */
19905 if (!member_p)
19906 cp_parser_parse_tentatively (parser);
19908 /* Consume the `('. */
19909 matching_parens parens;
19910 parens.consume_open (parser);
19911 if (first)
19913 /* If this is going to be an abstract declarator, we're
19914 in a declarator and we can't have default args. */
19915 parser->default_arg_ok_p = false;
19916 parser->in_declarator_p = true;
19919 begin_scope (sk_function_parms, NULL_TREE);
19921 /* Parse the parameter-declaration-clause. */
19922 params = cp_parser_parameter_declaration_clause (parser);
19924 /* Consume the `)'. */
19925 parens.require_close (parser);
19927 /* If all went well, parse the cv-qualifier-seq,
19928 ref-qualifier and the exception-specification. */
19929 if (member_p || cp_parser_parse_definitely (parser))
19931 cp_cv_quals cv_quals;
19932 cp_virt_specifiers virt_specifiers;
19933 cp_ref_qualifier ref_qual;
19934 tree exception_specification;
19935 tree late_return;
19936 tree attrs;
19937 bool memfn = (member_p || (pushed_scope
19938 && CLASS_TYPE_P (pushed_scope)));
19940 is_declarator = true;
19942 if (ctor_dtor_or_conv_p)
19943 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19944 first = false;
19946 /* Parse the cv-qualifier-seq. */
19947 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19948 /* Parse the ref-qualifier. */
19949 ref_qual = cp_parser_ref_qualifier_opt (parser);
19950 /* Parse the tx-qualifier. */
19951 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19952 /* And the exception-specification. */
19953 exception_specification
19954 = cp_parser_exception_specification_opt (parser);
19956 attrs = cp_parser_std_attribute_spec_seq (parser);
19958 /* In here, we handle cases where attribute is used after
19959 the function declaration. For example:
19960 void func (int x) __attribute__((vector(..))); */
19961 tree gnu_attrs = NULL_TREE;
19962 tree requires_clause = NULL_TREE;
19963 late_return = (cp_parser_late_return_type_opt
19964 (parser, declarator, requires_clause,
19965 memfn ? cv_quals : -1));
19967 /* Parse the virt-specifier-seq. */
19968 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19970 /* Create the function-declarator. */
19971 declarator = make_call_declarator (declarator,
19972 params,
19973 cv_quals,
19974 virt_specifiers,
19975 ref_qual,
19976 tx_qual,
19977 exception_specification,
19978 late_return,
19979 requires_clause);
19980 declarator->std_attributes = attrs;
19981 declarator->attributes = gnu_attrs;
19982 /* Any subsequent parameter lists are to do with
19983 return type, so are not those of the declared
19984 function. */
19985 parser->default_arg_ok_p = false;
19988 /* Remove the function parms from scope. */
19989 pop_bindings_and_leave_scope ();
19991 if (is_declarator)
19992 /* Repeat the main loop. */
19993 continue;
19996 /* If this is the first, we can try a parenthesized
19997 declarator. */
19998 if (first)
20000 bool saved_in_type_id_in_expr_p;
20002 parser->default_arg_ok_p = saved_default_arg_ok_p;
20003 parser->in_declarator_p = saved_in_declarator_p;
20005 open_paren = token;
20006 /* Consume the `('. */
20007 matching_parens parens;
20008 parens.consume_open (parser);
20009 /* Parse the nested declarator. */
20010 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20011 parser->in_type_id_in_expr_p = true;
20012 declarator
20013 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20014 /*parenthesized_p=*/NULL,
20015 member_p, friend_p);
20016 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20017 first = false;
20018 /* Expect a `)'. */
20019 close_paren = cp_lexer_peek_token (parser->lexer);
20020 if (!parens.require_close (parser))
20021 declarator = cp_error_declarator;
20022 if (declarator == cp_error_declarator)
20023 break;
20025 goto handle_declarator;
20027 /* Otherwise, we must be done. */
20028 else
20029 break;
20031 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20032 && token->type == CPP_OPEN_SQUARE
20033 && !cp_next_tokens_can_be_attribute_p (parser))
20035 /* Parse an array-declarator. */
20036 tree bounds, attrs;
20038 if (ctor_dtor_or_conv_p)
20039 *ctor_dtor_or_conv_p = 0;
20041 open_paren = NULL;
20042 first = false;
20043 parser->default_arg_ok_p = false;
20044 parser->in_declarator_p = true;
20045 /* Consume the `['. */
20046 cp_lexer_consume_token (parser->lexer);
20047 /* Peek at the next token. */
20048 token = cp_lexer_peek_token (parser->lexer);
20049 /* If the next token is `]', then there is no
20050 constant-expression. */
20051 if (token->type != CPP_CLOSE_SQUARE)
20053 bool non_constant_p;
20054 bounds
20055 = cp_parser_constant_expression (parser,
20056 /*allow_non_constant=*/true,
20057 &non_constant_p);
20058 if (!non_constant_p)
20059 /* OK */;
20060 else if (error_operand_p (bounds))
20061 /* Already gave an error. */;
20062 else if (!parser->in_function_body
20063 || current_binding_level->kind == sk_function_parms)
20065 /* Normally, the array bound must be an integral constant
20066 expression. However, as an extension, we allow VLAs
20067 in function scopes as long as they aren't part of a
20068 parameter declaration. */
20069 cp_parser_error (parser,
20070 "array bound is not an integer constant");
20071 bounds = error_mark_node;
20073 else if (processing_template_decl
20074 && !type_dependent_expression_p (bounds))
20076 /* Remember this wasn't a constant-expression. */
20077 bounds = build_nop (TREE_TYPE (bounds), bounds);
20078 TREE_SIDE_EFFECTS (bounds) = 1;
20081 else
20082 bounds = NULL_TREE;
20083 /* Look for the closing `]'. */
20084 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20086 declarator = cp_error_declarator;
20087 break;
20090 attrs = cp_parser_std_attribute_spec_seq (parser);
20091 declarator = make_array_declarator (declarator, bounds);
20092 declarator->std_attributes = attrs;
20094 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20097 tree qualifying_scope;
20098 tree unqualified_name;
20099 tree attrs;
20100 special_function_kind sfk;
20101 bool abstract_ok;
20102 bool pack_expansion_p = false;
20103 cp_token *declarator_id_start_token;
20105 /* Parse a declarator-id */
20106 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20107 if (abstract_ok)
20109 cp_parser_parse_tentatively (parser);
20111 /* If we see an ellipsis, we should be looking at a
20112 parameter pack. */
20113 if (token->type == CPP_ELLIPSIS)
20115 /* Consume the `...' */
20116 cp_lexer_consume_token (parser->lexer);
20118 pack_expansion_p = true;
20122 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20123 unqualified_name
20124 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20125 qualifying_scope = parser->scope;
20126 if (abstract_ok)
20128 bool okay = false;
20130 if (!unqualified_name && pack_expansion_p)
20132 /* Check whether an error occurred. */
20133 okay = !cp_parser_error_occurred (parser);
20135 /* We already consumed the ellipsis to mark a
20136 parameter pack, but we have no way to report it,
20137 so abort the tentative parse. We will be exiting
20138 immediately anyway. */
20139 cp_parser_abort_tentative_parse (parser);
20141 else
20142 okay = cp_parser_parse_definitely (parser);
20144 if (!okay)
20145 unqualified_name = error_mark_node;
20146 else if (unqualified_name
20147 && (qualifying_scope
20148 || (!identifier_p (unqualified_name))))
20150 cp_parser_error (parser, "expected unqualified-id");
20151 unqualified_name = error_mark_node;
20155 if (!unqualified_name)
20156 return NULL;
20157 if (unqualified_name == error_mark_node)
20159 declarator = cp_error_declarator;
20160 pack_expansion_p = false;
20161 declarator->parameter_pack_p = false;
20162 break;
20165 attrs = cp_parser_std_attribute_spec_seq (parser);
20167 if (qualifying_scope && at_namespace_scope_p ()
20168 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20170 /* In the declaration of a member of a template class
20171 outside of the class itself, the SCOPE will sometimes
20172 be a TYPENAME_TYPE. For example, given:
20174 template <typename T>
20175 int S<T>::R::i = 3;
20177 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20178 this context, we must resolve S<T>::R to an ordinary
20179 type, rather than a typename type.
20181 The reason we normally avoid resolving TYPENAME_TYPEs
20182 is that a specialization of `S' might render
20183 `S<T>::R' not a type. However, if `S' is
20184 specialized, then this `i' will not be used, so there
20185 is no harm in resolving the types here. */
20186 tree type;
20188 /* Resolve the TYPENAME_TYPE. */
20189 type = resolve_typename_type (qualifying_scope,
20190 /*only_current_p=*/false);
20191 /* If that failed, the declarator is invalid. */
20192 if (TREE_CODE (type) == TYPENAME_TYPE)
20194 if (typedef_variant_p (type))
20195 error_at (declarator_id_start_token->location,
20196 "cannot define member of dependent typedef "
20197 "%qT", type);
20198 else
20199 error_at (declarator_id_start_token->location,
20200 "%<%T::%E%> is not a type",
20201 TYPE_CONTEXT (qualifying_scope),
20202 TYPE_IDENTIFIER (qualifying_scope));
20204 qualifying_scope = type;
20207 sfk = sfk_none;
20209 if (unqualified_name)
20211 tree class_type;
20213 if (qualifying_scope
20214 && CLASS_TYPE_P (qualifying_scope))
20215 class_type = qualifying_scope;
20216 else
20217 class_type = current_class_type;
20219 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20221 tree name_type = TREE_TYPE (unqualified_name);
20223 if (!class_type || !same_type_p (name_type, class_type))
20225 /* We do not attempt to print the declarator
20226 here because we do not have enough
20227 information about its original syntactic
20228 form. */
20229 cp_parser_error (parser, "invalid declarator");
20230 declarator = cp_error_declarator;
20231 break;
20233 else if (qualifying_scope
20234 && CLASSTYPE_USE_TEMPLATE (name_type))
20236 error_at (declarator_id_start_token->location,
20237 "invalid use of constructor as a template");
20238 inform (declarator_id_start_token->location,
20239 "use %<%T::%D%> instead of %<%T::%D%> to "
20240 "name the constructor in a qualified name",
20241 class_type,
20242 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20243 class_type, name_type);
20244 declarator = cp_error_declarator;
20245 break;
20247 unqualified_name = constructor_name (class_type);
20250 if (class_type)
20252 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20253 sfk = sfk_destructor;
20254 else if (identifier_p (unqualified_name)
20255 && IDENTIFIER_CONV_OP_P (unqualified_name))
20256 sfk = sfk_conversion;
20257 else if (/* There's no way to declare a constructor
20258 for an unnamed type, even if the type
20259 got a name for linkage purposes. */
20260 !TYPE_WAS_UNNAMED (class_type)
20261 /* Handle correctly (c++/19200):
20263 struct S {
20264 struct T{};
20265 friend void S(T);
20268 and also:
20270 namespace N {
20271 void S();
20274 struct S {
20275 friend void N::S();
20276 }; */
20277 && (!friend_p || class_type == qualifying_scope)
20278 && constructor_name_p (unqualified_name,
20279 class_type))
20280 sfk = sfk_constructor;
20281 else if (is_overloaded_fn (unqualified_name)
20282 && DECL_CONSTRUCTOR_P (get_first_fn
20283 (unqualified_name)))
20284 sfk = sfk_constructor;
20286 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20287 *ctor_dtor_or_conv_p = -1;
20290 declarator = make_id_declarator (qualifying_scope,
20291 unqualified_name,
20292 sfk);
20293 declarator->std_attributes = attrs;
20294 declarator->id_loc = token->location;
20295 declarator->parameter_pack_p = pack_expansion_p;
20297 if (pack_expansion_p)
20298 maybe_warn_variadic_templates ();
20301 handle_declarator:;
20302 scope = get_scope_of_declarator (declarator);
20303 if (scope)
20305 /* Any names that appear after the declarator-id for a
20306 member are looked up in the containing scope. */
20307 if (at_function_scope_p ())
20309 /* But declarations with qualified-ids can't appear in a
20310 function. */
20311 cp_parser_error (parser, "qualified-id in declaration");
20312 declarator = cp_error_declarator;
20313 break;
20315 pushed_scope = push_scope (scope);
20317 parser->in_declarator_p = true;
20318 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20319 || (declarator && declarator->kind == cdk_id))
20320 /* Default args are only allowed on function
20321 declarations. */
20322 parser->default_arg_ok_p = saved_default_arg_ok_p;
20323 else
20324 parser->default_arg_ok_p = false;
20326 first = false;
20328 /* We're done. */
20329 else
20330 break;
20333 /* For an abstract declarator, we might wind up with nothing at this
20334 point. That's an error; the declarator is not optional. */
20335 if (!declarator)
20336 cp_parser_error (parser, "expected declarator");
20337 else if (open_paren)
20339 /* Record overly parenthesized declarator so we can give a
20340 diagnostic about confusing decl/expr disambiguation. */
20341 if (declarator->kind == cdk_array)
20343 /* If the open and close parens are on different lines, this
20344 is probably a formatting thing, so ignore. */
20345 expanded_location open = expand_location (open_paren->location);
20346 expanded_location close = expand_location (close_paren->location);
20347 if (open.line != close.line || open.file != close.file)
20348 open_paren = NULL;
20350 if (open_paren)
20351 declarator->parenthesized = open_paren->location;
20354 /* If we entered a scope, we must exit it now. */
20355 if (pushed_scope)
20356 pop_scope (pushed_scope);
20358 parser->default_arg_ok_p = saved_default_arg_ok_p;
20359 parser->in_declarator_p = saved_in_declarator_p;
20361 return declarator;
20364 /* Parse a ptr-operator.
20366 ptr-operator:
20367 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20368 * cv-qualifier-seq [opt]
20370 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20371 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20373 GNU Extension:
20375 ptr-operator:
20376 & cv-qualifier-seq [opt]
20378 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20379 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20380 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20381 filled in with the TYPE containing the member. *CV_QUALS is
20382 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20383 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20384 Note that the tree codes returned by this function have nothing
20385 to do with the types of trees that will be eventually be created
20386 to represent the pointer or reference type being parsed. They are
20387 just constants with suggestive names. */
20388 static enum tree_code
20389 cp_parser_ptr_operator (cp_parser* parser,
20390 tree* type,
20391 cp_cv_quals *cv_quals,
20392 tree *attributes)
20394 enum tree_code code = ERROR_MARK;
20395 cp_token *token;
20396 tree attrs = NULL_TREE;
20398 /* Assume that it's not a pointer-to-member. */
20399 *type = NULL_TREE;
20400 /* And that there are no cv-qualifiers. */
20401 *cv_quals = TYPE_UNQUALIFIED;
20403 /* Peek at the next token. */
20404 token = cp_lexer_peek_token (parser->lexer);
20406 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20407 if (token->type == CPP_MULT)
20408 code = INDIRECT_REF;
20409 else if (token->type == CPP_AND)
20410 code = ADDR_EXPR;
20411 else if ((cxx_dialect != cxx98) &&
20412 token->type == CPP_AND_AND) /* C++0x only */
20413 code = NON_LVALUE_EXPR;
20415 if (code != ERROR_MARK)
20417 /* Consume the `*', `&' or `&&'. */
20418 cp_lexer_consume_token (parser->lexer);
20420 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20421 `&', if we are allowing GNU extensions. (The only qualifier
20422 that can legally appear after `&' is `restrict', but that is
20423 enforced during semantic analysis. */
20424 if (code == INDIRECT_REF
20425 || cp_parser_allow_gnu_extensions_p (parser))
20426 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20428 attrs = cp_parser_std_attribute_spec_seq (parser);
20429 if (attributes != NULL)
20430 *attributes = attrs;
20432 else
20434 /* Try the pointer-to-member case. */
20435 cp_parser_parse_tentatively (parser);
20436 /* Look for the optional `::' operator. */
20437 cp_parser_global_scope_opt (parser,
20438 /*current_scope_valid_p=*/false);
20439 /* Look for the nested-name specifier. */
20440 token = cp_lexer_peek_token (parser->lexer);
20441 cp_parser_nested_name_specifier (parser,
20442 /*typename_keyword_p=*/false,
20443 /*check_dependency_p=*/true,
20444 /*type_p=*/false,
20445 /*is_declaration=*/false);
20446 /* If we found it, and the next token is a `*', then we are
20447 indeed looking at a pointer-to-member operator. */
20448 if (!cp_parser_error_occurred (parser)
20449 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20451 /* Indicate that the `*' operator was used. */
20452 code = INDIRECT_REF;
20454 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20455 error_at (token->location, "%qD is a namespace", parser->scope);
20456 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20457 error_at (token->location, "cannot form pointer to member of "
20458 "non-class %q#T", parser->scope);
20459 else
20461 /* The type of which the member is a member is given by the
20462 current SCOPE. */
20463 *type = parser->scope;
20464 /* The next name will not be qualified. */
20465 parser->scope = NULL_TREE;
20466 parser->qualifying_scope = NULL_TREE;
20467 parser->object_scope = NULL_TREE;
20468 /* Look for optional c++11 attributes. */
20469 attrs = cp_parser_std_attribute_spec_seq (parser);
20470 if (attributes != NULL)
20471 *attributes = attrs;
20472 /* Look for the optional cv-qualifier-seq. */
20473 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20476 /* If that didn't work we don't have a ptr-operator. */
20477 if (!cp_parser_parse_definitely (parser))
20478 cp_parser_error (parser, "expected ptr-operator");
20481 return code;
20484 /* Parse an (optional) cv-qualifier-seq.
20486 cv-qualifier-seq:
20487 cv-qualifier cv-qualifier-seq [opt]
20489 cv-qualifier:
20490 const
20491 volatile
20493 GNU Extension:
20495 cv-qualifier:
20496 __restrict__
20498 Returns a bitmask representing the cv-qualifiers. */
20500 static cp_cv_quals
20501 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20503 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20505 while (true)
20507 cp_token *token;
20508 cp_cv_quals cv_qualifier;
20510 /* Peek at the next token. */
20511 token = cp_lexer_peek_token (parser->lexer);
20512 /* See if it's a cv-qualifier. */
20513 switch (token->keyword)
20515 case RID_CONST:
20516 cv_qualifier = TYPE_QUAL_CONST;
20517 break;
20519 case RID_VOLATILE:
20520 cv_qualifier = TYPE_QUAL_VOLATILE;
20521 break;
20523 case RID_RESTRICT:
20524 cv_qualifier = TYPE_QUAL_RESTRICT;
20525 break;
20527 default:
20528 cv_qualifier = TYPE_UNQUALIFIED;
20529 break;
20532 if (!cv_qualifier)
20533 break;
20535 if (cv_quals & cv_qualifier)
20537 gcc_rich_location richloc (token->location);
20538 richloc.add_fixit_remove ();
20539 error_at (&richloc, "duplicate cv-qualifier");
20540 cp_lexer_purge_token (parser->lexer);
20542 else
20544 cp_lexer_consume_token (parser->lexer);
20545 cv_quals |= cv_qualifier;
20549 return cv_quals;
20552 /* Parse an (optional) ref-qualifier
20554 ref-qualifier:
20558 Returns cp_ref_qualifier representing ref-qualifier. */
20560 static cp_ref_qualifier
20561 cp_parser_ref_qualifier_opt (cp_parser* parser)
20563 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20565 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20566 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20567 return ref_qual;
20569 while (true)
20571 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20572 cp_token *token = cp_lexer_peek_token (parser->lexer);
20574 switch (token->type)
20576 case CPP_AND:
20577 curr_ref_qual = REF_QUAL_LVALUE;
20578 break;
20580 case CPP_AND_AND:
20581 curr_ref_qual = REF_QUAL_RVALUE;
20582 break;
20584 default:
20585 curr_ref_qual = REF_QUAL_NONE;
20586 break;
20589 if (!curr_ref_qual)
20590 break;
20591 else if (ref_qual)
20593 error_at (token->location, "multiple ref-qualifiers");
20594 cp_lexer_purge_token (parser->lexer);
20596 else
20598 ref_qual = curr_ref_qual;
20599 cp_lexer_consume_token (parser->lexer);
20603 return ref_qual;
20606 /* Parse an optional tx-qualifier.
20608 tx-qualifier:
20609 transaction_safe
20610 transaction_safe_dynamic */
20612 static tree
20613 cp_parser_tx_qualifier_opt (cp_parser *parser)
20615 cp_token *token = cp_lexer_peek_token (parser->lexer);
20616 if (token->type == CPP_NAME)
20618 tree name = token->u.value;
20619 const char *p = IDENTIFIER_POINTER (name);
20620 const int len = strlen ("transaction_safe");
20621 if (!strncmp (p, "transaction_safe", len))
20623 p += len;
20624 if (*p == '\0'
20625 || !strcmp (p, "_dynamic"))
20627 cp_lexer_consume_token (parser->lexer);
20628 if (!flag_tm)
20630 error ("%qE requires %<-fgnu-tm%>", name);
20631 return NULL_TREE;
20633 else
20634 return name;
20638 return NULL_TREE;
20641 /* Parse an (optional) virt-specifier-seq.
20643 virt-specifier-seq:
20644 virt-specifier virt-specifier-seq [opt]
20646 virt-specifier:
20647 override
20648 final
20650 Returns a bitmask representing the virt-specifiers. */
20652 static cp_virt_specifiers
20653 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20655 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20657 while (true)
20659 cp_token *token;
20660 cp_virt_specifiers virt_specifier;
20662 /* Peek at the next token. */
20663 token = cp_lexer_peek_token (parser->lexer);
20664 /* See if it's a virt-specifier-qualifier. */
20665 if (token->type != CPP_NAME)
20666 break;
20667 if (id_equal (token->u.value, "override"))
20669 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20670 virt_specifier = VIRT_SPEC_OVERRIDE;
20672 else if (id_equal (token->u.value, "final"))
20674 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20675 virt_specifier = VIRT_SPEC_FINAL;
20677 else if (id_equal (token->u.value, "__final"))
20679 virt_specifier = VIRT_SPEC_FINAL;
20681 else
20682 break;
20684 if (virt_specifiers & virt_specifier)
20686 gcc_rich_location richloc (token->location);
20687 richloc.add_fixit_remove ();
20688 error_at (&richloc, "duplicate virt-specifier");
20689 cp_lexer_purge_token (parser->lexer);
20691 else
20693 cp_lexer_consume_token (parser->lexer);
20694 virt_specifiers |= virt_specifier;
20697 return virt_specifiers;
20700 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20701 is in scope even though it isn't real. */
20703 void
20704 inject_this_parameter (tree ctype, cp_cv_quals quals)
20706 tree this_parm;
20708 if (current_class_ptr)
20710 /* We don't clear this between NSDMIs. Is it already what we want? */
20711 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20712 if (DECL_P (current_class_ptr)
20713 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20714 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20715 && cp_type_quals (type) == quals)
20716 return;
20719 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20720 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20721 current_class_ptr = NULL_TREE;
20722 current_class_ref
20723 = cp_build_fold_indirect_ref (this_parm);
20724 current_class_ptr = this_parm;
20727 /* Return true iff our current scope is a non-static data member
20728 initializer. */
20730 bool
20731 parsing_nsdmi (void)
20733 /* We recognize NSDMI context by the context-less 'this' pointer set up
20734 by the function above. */
20735 if (current_class_ptr
20736 && TREE_CODE (current_class_ptr) == PARM_DECL
20737 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20738 return true;
20739 return false;
20742 /* Parse a late-specified return type, if any. This is not a separate
20743 non-terminal, but part of a function declarator, which looks like
20745 -> trailing-type-specifier-seq abstract-declarator(opt)
20747 Returns the type indicated by the type-id.
20749 In addition to this, parse any queued up #pragma omp declare simd
20750 clauses, and #pragma acc routine clauses.
20752 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20753 function. */
20755 static tree
20756 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20757 tree& requires_clause, cp_cv_quals quals)
20759 cp_token *token;
20760 tree type = NULL_TREE;
20761 bool declare_simd_p = (parser->omp_declare_simd
20762 && declarator
20763 && declarator->kind == cdk_id);
20765 bool oacc_routine_p = (parser->oacc_routine
20766 && declarator
20767 && declarator->kind == cdk_id);
20769 /* Peek at the next token. */
20770 token = cp_lexer_peek_token (parser->lexer);
20771 /* A late-specified return type is indicated by an initial '->'. */
20772 if (token->type != CPP_DEREF
20773 && token->keyword != RID_REQUIRES
20774 && !(token->type == CPP_NAME
20775 && token->u.value == ridpointers[RID_REQUIRES])
20776 && !(declare_simd_p || oacc_routine_p))
20777 return NULL_TREE;
20779 tree save_ccp = current_class_ptr;
20780 tree save_ccr = current_class_ref;
20781 if (quals >= 0)
20783 /* DR 1207: 'this' is in scope in the trailing return type. */
20784 inject_this_parameter (current_class_type, quals);
20787 if (token->type == CPP_DEREF)
20789 /* Consume the ->. */
20790 cp_lexer_consume_token (parser->lexer);
20792 type = cp_parser_trailing_type_id (parser);
20795 /* Function declarations may be followed by a trailing
20796 requires-clause. */
20797 requires_clause = cp_parser_requires_clause_opt (parser);
20799 if (declare_simd_p)
20800 declarator->attributes
20801 = cp_parser_late_parsing_omp_declare_simd (parser,
20802 declarator->attributes);
20803 if (oacc_routine_p)
20804 declarator->attributes
20805 = cp_parser_late_parsing_oacc_routine (parser,
20806 declarator->attributes);
20808 if (quals >= 0)
20810 current_class_ptr = save_ccp;
20811 current_class_ref = save_ccr;
20814 return type;
20817 /* Parse a declarator-id.
20819 declarator-id:
20820 id-expression
20821 :: [opt] nested-name-specifier [opt] type-name
20823 In the `id-expression' case, the value returned is as for
20824 cp_parser_id_expression if the id-expression was an unqualified-id.
20825 If the id-expression was a qualified-id, then a SCOPE_REF is
20826 returned. The first operand is the scope (either a NAMESPACE_DECL
20827 or TREE_TYPE), but the second is still just a representation of an
20828 unqualified-id. */
20830 static tree
20831 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20833 tree id;
20834 /* The expression must be an id-expression. Assume that qualified
20835 names are the names of types so that:
20837 template <class T>
20838 int S<T>::R::i = 3;
20840 will work; we must treat `S<T>::R' as the name of a type.
20841 Similarly, assume that qualified names are templates, where
20842 required, so that:
20844 template <class T>
20845 int S<T>::R<T>::i = 3;
20847 will work, too. */
20848 id = cp_parser_id_expression (parser,
20849 /*template_keyword_p=*/false,
20850 /*check_dependency_p=*/false,
20851 /*template_p=*/NULL,
20852 /*declarator_p=*/true,
20853 optional_p);
20854 if (id && BASELINK_P (id))
20855 id = BASELINK_FUNCTIONS (id);
20856 return id;
20859 /* Parse a type-id.
20861 type-id:
20862 type-specifier-seq abstract-declarator [opt]
20864 Returns the TYPE specified. */
20866 static tree
20867 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20868 bool is_trailing_return)
20870 cp_decl_specifier_seq type_specifier_seq;
20871 cp_declarator *abstract_declarator;
20873 /* Parse the type-specifier-seq. */
20874 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20875 is_trailing_return,
20876 &type_specifier_seq);
20877 if (is_template_arg && type_specifier_seq.type
20878 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20879 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20880 /* A bare template name as a template argument is a template template
20881 argument, not a placeholder, so fail parsing it as a type argument. */
20883 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20884 cp_parser_simulate_error (parser);
20885 return error_mark_node;
20887 if (type_specifier_seq.type == error_mark_node)
20888 return error_mark_node;
20890 /* There might or might not be an abstract declarator. */
20891 cp_parser_parse_tentatively (parser);
20892 /* Look for the declarator. */
20893 abstract_declarator
20894 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20895 /*parenthesized_p=*/NULL,
20896 /*member_p=*/false,
20897 /*friend_p=*/false);
20898 /* Check to see if there really was a declarator. */
20899 if (!cp_parser_parse_definitely (parser))
20900 abstract_declarator = NULL;
20902 if (type_specifier_seq.type
20903 /* The concepts TS allows 'auto' as a type-id. */
20904 && (!flag_concepts || parser->in_type_id_in_expr_p)
20905 /* None of the valid uses of 'auto' in C++14 involve the type-id
20906 nonterminal, but it is valid in a trailing-return-type. */
20907 && !(cxx_dialect >= cxx14 && is_trailing_return))
20908 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20910 /* A type-id with type 'auto' is only ok if the abstract declarator
20911 is a function declarator with a late-specified return type.
20913 A type-id with 'auto' is also valid in a trailing-return-type
20914 in a compound-requirement. */
20915 if (abstract_declarator
20916 && abstract_declarator->kind == cdk_function
20917 && abstract_declarator->u.function.late_return_type)
20918 /* OK */;
20919 else if (parser->in_result_type_constraint_p)
20920 /* OK */;
20921 else
20923 location_t loc = type_specifier_seq.locations[ds_type_spec];
20924 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20926 error_at (loc, "missing template arguments after %qT",
20927 auto_node);
20928 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20929 tmpl);
20931 else
20932 error_at (loc, "invalid use of %qT", auto_node);
20933 return error_mark_node;
20937 return groktypename (&type_specifier_seq, abstract_declarator,
20938 is_template_arg);
20941 static tree
20942 cp_parser_type_id (cp_parser *parser)
20944 return cp_parser_type_id_1 (parser, false, false);
20947 static tree
20948 cp_parser_template_type_arg (cp_parser *parser)
20950 tree r;
20951 const char *saved_message = parser->type_definition_forbidden_message;
20952 parser->type_definition_forbidden_message
20953 = G_("types may not be defined in template arguments");
20954 r = cp_parser_type_id_1 (parser, true, false);
20955 parser->type_definition_forbidden_message = saved_message;
20956 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20958 error ("invalid use of %<auto%> in template argument");
20959 r = error_mark_node;
20961 return r;
20964 static tree
20965 cp_parser_trailing_type_id (cp_parser *parser)
20967 return cp_parser_type_id_1 (parser, false, true);
20970 /* Parse a type-specifier-seq.
20972 type-specifier-seq:
20973 type-specifier type-specifier-seq [opt]
20975 GNU extension:
20977 type-specifier-seq:
20978 attributes type-specifier-seq [opt]
20980 If IS_DECLARATION is true, we are at the start of a "condition" or
20981 exception-declaration, so we might be followed by a declarator-id.
20983 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20984 i.e. we've just seen "->".
20986 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20988 static void
20989 cp_parser_type_specifier_seq (cp_parser* parser,
20990 bool is_declaration,
20991 bool is_trailing_return,
20992 cp_decl_specifier_seq *type_specifier_seq)
20994 bool seen_type_specifier = false;
20995 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20996 cp_token *start_token = NULL;
20998 /* Clear the TYPE_SPECIFIER_SEQ. */
20999 clear_decl_specs (type_specifier_seq);
21001 /* In the context of a trailing return type, enum E { } is an
21002 elaborated-type-specifier followed by a function-body, not an
21003 enum-specifier. */
21004 if (is_trailing_return)
21005 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21007 /* Parse the type-specifiers and attributes. */
21008 while (true)
21010 tree type_specifier;
21011 bool is_cv_qualifier;
21013 /* Check for attributes first. */
21014 if (cp_next_tokens_can_be_attribute_p (parser))
21016 type_specifier_seq->attributes =
21017 chainon (type_specifier_seq->attributes,
21018 cp_parser_attributes_opt (parser));
21019 continue;
21022 /* record the token of the beginning of the type specifier seq,
21023 for error reporting purposes*/
21024 if (!start_token)
21025 start_token = cp_lexer_peek_token (parser->lexer);
21027 /* Look for the type-specifier. */
21028 type_specifier = cp_parser_type_specifier (parser,
21029 flags,
21030 type_specifier_seq,
21031 /*is_declaration=*/false,
21032 NULL,
21033 &is_cv_qualifier);
21034 if (!type_specifier)
21036 /* If the first type-specifier could not be found, this is not a
21037 type-specifier-seq at all. */
21038 if (!seen_type_specifier)
21040 /* Set in_declarator_p to avoid skipping to the semicolon. */
21041 int in_decl = parser->in_declarator_p;
21042 parser->in_declarator_p = true;
21044 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21045 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21046 cp_parser_error (parser, "expected type-specifier");
21048 parser->in_declarator_p = in_decl;
21050 type_specifier_seq->type = error_mark_node;
21051 return;
21053 /* If subsequent type-specifiers could not be found, the
21054 type-specifier-seq is complete. */
21055 break;
21058 seen_type_specifier = true;
21059 /* The standard says that a condition can be:
21061 type-specifier-seq declarator = assignment-expression
21063 However, given:
21065 struct S {};
21066 if (int S = ...)
21068 we should treat the "S" as a declarator, not as a
21069 type-specifier. The standard doesn't say that explicitly for
21070 type-specifier-seq, but it does say that for
21071 decl-specifier-seq in an ordinary declaration. Perhaps it
21072 would be clearer just to allow a decl-specifier-seq here, and
21073 then add a semantic restriction that if any decl-specifiers
21074 that are not type-specifiers appear, the program is invalid. */
21075 if (is_declaration && !is_cv_qualifier)
21076 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21080 /* Return whether the function currently being declared has an associated
21081 template parameter list. */
21083 static bool
21084 function_being_declared_is_template_p (cp_parser* parser)
21086 if (!current_template_parms || processing_template_parmlist)
21087 return false;
21089 if (parser->implicit_template_scope)
21090 return true;
21092 if (at_class_scope_p ()
21093 && TYPE_BEING_DEFINED (current_class_type))
21094 return parser->num_template_parameter_lists != 0;
21096 return ((int) parser->num_template_parameter_lists > template_class_depth
21097 (current_class_type));
21100 /* Parse a parameter-declaration-clause.
21102 parameter-declaration-clause:
21103 parameter-declaration-list [opt] ... [opt]
21104 parameter-declaration-list , ...
21106 Returns a representation for the parameter declarations. A return
21107 value of NULL indicates a parameter-declaration-clause consisting
21108 only of an ellipsis. */
21110 static tree
21111 cp_parser_parameter_declaration_clause (cp_parser* parser)
21113 tree parameters;
21114 cp_token *token;
21115 bool ellipsis_p;
21116 bool is_error;
21118 struct cleanup {
21119 cp_parser* parser;
21120 int auto_is_implicit_function_template_parm_p;
21121 ~cleanup() {
21122 parser->auto_is_implicit_function_template_parm_p
21123 = auto_is_implicit_function_template_parm_p;
21125 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21127 (void) cleanup;
21129 if (!processing_specialization
21130 && !processing_template_parmlist
21131 && !processing_explicit_instantiation)
21132 if (!current_function_decl
21133 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21134 parser->auto_is_implicit_function_template_parm_p = true;
21136 /* Peek at the next token. */
21137 token = cp_lexer_peek_token (parser->lexer);
21138 /* Check for trivial parameter-declaration-clauses. */
21139 if (token->type == CPP_ELLIPSIS)
21141 /* Consume the `...' token. */
21142 cp_lexer_consume_token (parser->lexer);
21143 return NULL_TREE;
21145 else if (token->type == CPP_CLOSE_PAREN)
21146 /* There are no parameters. */
21148 #ifndef NO_IMPLICIT_EXTERN_C
21149 if (in_system_header_at (input_location)
21150 && current_class_type == NULL
21151 && current_lang_name == lang_name_c)
21152 return NULL_TREE;
21153 else
21154 #endif
21155 return void_list_node;
21157 /* Check for `(void)', too, which is a special case. */
21158 else if (token->keyword == RID_VOID
21159 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21160 == CPP_CLOSE_PAREN))
21162 /* Consume the `void' token. */
21163 cp_lexer_consume_token (parser->lexer);
21164 /* There are no parameters. */
21165 return void_list_node;
21168 /* Parse the parameter-declaration-list. */
21169 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21170 /* If a parse error occurred while parsing the
21171 parameter-declaration-list, then the entire
21172 parameter-declaration-clause is erroneous. */
21173 if (is_error)
21174 return NULL;
21176 /* Peek at the next token. */
21177 token = cp_lexer_peek_token (parser->lexer);
21178 /* If it's a `,', the clause should terminate with an ellipsis. */
21179 if (token->type == CPP_COMMA)
21181 /* Consume the `,'. */
21182 cp_lexer_consume_token (parser->lexer);
21183 /* Expect an ellipsis. */
21184 ellipsis_p
21185 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21187 /* It might also be `...' if the optional trailing `,' was
21188 omitted. */
21189 else if (token->type == CPP_ELLIPSIS)
21191 /* Consume the `...' token. */
21192 cp_lexer_consume_token (parser->lexer);
21193 /* And remember that we saw it. */
21194 ellipsis_p = true;
21196 else
21197 ellipsis_p = false;
21199 /* Finish the parameter list. */
21200 if (!ellipsis_p)
21201 parameters = chainon (parameters, void_list_node);
21203 return parameters;
21206 /* Parse a parameter-declaration-list.
21208 parameter-declaration-list:
21209 parameter-declaration
21210 parameter-declaration-list , parameter-declaration
21212 Returns a representation of the parameter-declaration-list, as for
21213 cp_parser_parameter_declaration_clause. However, the
21214 `void_list_node' is never appended to the list. Upon return,
21215 *IS_ERROR will be true iff an error occurred. */
21217 static tree
21218 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21220 tree parameters = NULL_TREE;
21221 tree *tail = &parameters;
21222 bool saved_in_unbraced_linkage_specification_p;
21223 int index = 0;
21225 /* Assume all will go well. */
21226 *is_error = false;
21227 /* The special considerations that apply to a function within an
21228 unbraced linkage specifications do not apply to the parameters
21229 to the function. */
21230 saved_in_unbraced_linkage_specification_p
21231 = parser->in_unbraced_linkage_specification_p;
21232 parser->in_unbraced_linkage_specification_p = false;
21234 /* Look for more parameters. */
21235 while (true)
21237 cp_parameter_declarator *parameter;
21238 tree decl = error_mark_node;
21239 bool parenthesized_p = false;
21240 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21241 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21242 (current_template_parms)) : 0);
21244 /* Parse the parameter. */
21245 parameter
21246 = cp_parser_parameter_declaration (parser,
21247 /*template_parm_p=*/false,
21248 &parenthesized_p);
21250 /* We don't know yet if the enclosing context is deprecated, so wait
21251 and warn in grokparms if appropriate. */
21252 deprecated_state = DEPRECATED_SUPPRESS;
21254 if (parameter)
21256 /* If a function parameter pack was specified and an implicit template
21257 parameter was introduced during cp_parser_parameter_declaration,
21258 change any implicit parameters introduced into packs. */
21259 if (parser->implicit_template_parms
21260 && parameter->declarator
21261 && parameter->declarator->parameter_pack_p)
21263 int latest_template_parm_idx = TREE_VEC_LENGTH
21264 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21266 if (latest_template_parm_idx != template_parm_idx)
21267 parameter->decl_specifiers.type = convert_generic_types_to_packs
21268 (parameter->decl_specifiers.type,
21269 template_parm_idx, latest_template_parm_idx);
21272 decl = grokdeclarator (parameter->declarator,
21273 &parameter->decl_specifiers,
21274 PARM,
21275 parameter->default_argument != NULL_TREE,
21276 &parameter->decl_specifiers.attributes);
21277 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21278 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21281 deprecated_state = DEPRECATED_NORMAL;
21283 /* If a parse error occurred parsing the parameter declaration,
21284 then the entire parameter-declaration-list is erroneous. */
21285 if (decl == error_mark_node)
21287 *is_error = true;
21288 parameters = error_mark_node;
21289 break;
21292 if (parameter->decl_specifiers.attributes)
21293 cplus_decl_attributes (&decl,
21294 parameter->decl_specifiers.attributes,
21296 if (DECL_NAME (decl))
21297 decl = pushdecl (decl);
21299 if (decl != error_mark_node)
21301 retrofit_lang_decl (decl);
21302 DECL_PARM_INDEX (decl) = ++index;
21303 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21306 /* Add the new parameter to the list. */
21307 *tail = build_tree_list (parameter->default_argument, decl);
21308 tail = &TREE_CHAIN (*tail);
21310 /* Peek at the next token. */
21311 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21312 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21313 /* These are for Objective-C++ */
21314 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21315 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21316 /* The parameter-declaration-list is complete. */
21317 break;
21318 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21320 cp_token *token;
21322 /* Peek at the next token. */
21323 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21324 /* If it's an ellipsis, then the list is complete. */
21325 if (token->type == CPP_ELLIPSIS)
21326 break;
21327 /* Otherwise, there must be more parameters. Consume the
21328 `,'. */
21329 cp_lexer_consume_token (parser->lexer);
21330 /* When parsing something like:
21332 int i(float f, double d)
21334 we can tell after seeing the declaration for "f" that we
21335 are not looking at an initialization of a variable "i",
21336 but rather at the declaration of a function "i".
21338 Due to the fact that the parsing of template arguments
21339 (as specified to a template-id) requires backtracking we
21340 cannot use this technique when inside a template argument
21341 list. */
21342 if (!parser->in_template_argument_list_p
21343 && !parser->in_type_id_in_expr_p
21344 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21345 /* However, a parameter-declaration of the form
21346 "float(f)" (which is a valid declaration of a
21347 parameter "f") can also be interpreted as an
21348 expression (the conversion of "f" to "float"). */
21349 && !parenthesized_p)
21350 cp_parser_commit_to_tentative_parse (parser);
21352 else
21354 cp_parser_error (parser, "expected %<,%> or %<...%>");
21355 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21356 cp_parser_skip_to_closing_parenthesis (parser,
21357 /*recovering=*/true,
21358 /*or_comma=*/false,
21359 /*consume_paren=*/false);
21360 break;
21364 parser->in_unbraced_linkage_specification_p
21365 = saved_in_unbraced_linkage_specification_p;
21367 /* Reset implicit_template_scope if we are about to leave the function
21368 parameter list that introduced it. Note that for out-of-line member
21369 definitions, there will be one or more class scopes before we get to
21370 the template parameter scope. */
21372 if (cp_binding_level *its = parser->implicit_template_scope)
21373 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21375 while (maybe_its->kind == sk_class)
21376 maybe_its = maybe_its->level_chain;
21377 if (maybe_its == its)
21379 parser->implicit_template_parms = 0;
21380 parser->implicit_template_scope = 0;
21384 return parameters;
21387 /* Parse a parameter declaration.
21389 parameter-declaration:
21390 decl-specifier-seq ... [opt] declarator
21391 decl-specifier-seq declarator = assignment-expression
21392 decl-specifier-seq ... [opt] abstract-declarator [opt]
21393 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21395 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21396 declares a template parameter. (In that case, a non-nested `>'
21397 token encountered during the parsing of the assignment-expression
21398 is not interpreted as a greater-than operator.)
21400 Returns a representation of the parameter, or NULL if an error
21401 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21402 true iff the declarator is of the form "(p)". */
21404 static cp_parameter_declarator *
21405 cp_parser_parameter_declaration (cp_parser *parser,
21406 bool template_parm_p,
21407 bool *parenthesized_p)
21409 int declares_class_or_enum;
21410 cp_decl_specifier_seq decl_specifiers;
21411 cp_declarator *declarator;
21412 tree default_argument;
21413 cp_token *token = NULL, *declarator_token_start = NULL;
21414 const char *saved_message;
21415 bool template_parameter_pack_p = false;
21417 /* In a template parameter, `>' is not an operator.
21419 [temp.param]
21421 When parsing a default template-argument for a non-type
21422 template-parameter, the first non-nested `>' is taken as the end
21423 of the template parameter-list rather than a greater-than
21424 operator. */
21426 /* Type definitions may not appear in parameter types. */
21427 saved_message = parser->type_definition_forbidden_message;
21428 parser->type_definition_forbidden_message
21429 = G_("types may not be defined in parameter types");
21431 /* Parse the declaration-specifiers. */
21432 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21433 cp_parser_decl_specifier_seq (parser,
21434 CP_PARSER_FLAGS_NONE,
21435 &decl_specifiers,
21436 &declares_class_or_enum);
21438 /* Complain about missing 'typename' or other invalid type names. */
21439 if (!decl_specifiers.any_type_specifiers_p
21440 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21441 decl_specifiers.type = error_mark_node;
21443 /* If an error occurred, there's no reason to attempt to parse the
21444 rest of the declaration. */
21445 if (cp_parser_error_occurred (parser))
21447 parser->type_definition_forbidden_message = saved_message;
21448 return NULL;
21451 /* Peek at the next token. */
21452 token = cp_lexer_peek_token (parser->lexer);
21454 /* If the next token is a `)', `,', `=', `>', or `...', then there
21455 is no declarator. However, when variadic templates are enabled,
21456 there may be a declarator following `...'. */
21457 if (token->type == CPP_CLOSE_PAREN
21458 || token->type == CPP_COMMA
21459 || token->type == CPP_EQ
21460 || token->type == CPP_GREATER)
21462 declarator = NULL;
21463 if (parenthesized_p)
21464 *parenthesized_p = false;
21466 /* Otherwise, there should be a declarator. */
21467 else
21469 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21470 parser->default_arg_ok_p = false;
21472 /* After seeing a decl-specifier-seq, if the next token is not a
21473 "(", there is no possibility that the code is a valid
21474 expression. Therefore, if parsing tentatively, we commit at
21475 this point. */
21476 if (!parser->in_template_argument_list_p
21477 /* In an expression context, having seen:
21479 (int((char ...
21481 we cannot be sure whether we are looking at a
21482 function-type (taking a "char" as a parameter) or a cast
21483 of some object of type "char" to "int". */
21484 && !parser->in_type_id_in_expr_p
21485 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21486 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21487 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21488 cp_parser_commit_to_tentative_parse (parser);
21489 /* Parse the declarator. */
21490 declarator_token_start = token;
21491 declarator = cp_parser_declarator (parser,
21492 CP_PARSER_DECLARATOR_EITHER,
21493 /*ctor_dtor_or_conv_p=*/NULL,
21494 parenthesized_p,
21495 /*member_p=*/false,
21496 /*friend_p=*/false);
21497 parser->default_arg_ok_p = saved_default_arg_ok_p;
21498 /* After the declarator, allow more attributes. */
21499 decl_specifiers.attributes
21500 = chainon (decl_specifiers.attributes,
21501 cp_parser_attributes_opt (parser));
21503 /* If the declarator is a template parameter pack, remember that and
21504 clear the flag in the declarator itself so we don't get errors
21505 from grokdeclarator. */
21506 if (template_parm_p && declarator && declarator->parameter_pack_p)
21508 declarator->parameter_pack_p = false;
21509 template_parameter_pack_p = true;
21513 /* If the next token is an ellipsis, and we have not seen a declarator
21514 name, and if either the type of the declarator contains parameter
21515 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21516 for, eg, abbreviated integral type names), then we actually have a
21517 parameter pack expansion expression. Otherwise, leave the ellipsis
21518 for a C-style variadic function. */
21519 token = cp_lexer_peek_token (parser->lexer);
21520 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21522 tree type = decl_specifiers.type;
21524 if (type && DECL_P (type))
21525 type = TREE_TYPE (type);
21527 if (((type
21528 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21529 && (template_parm_p || uses_parameter_packs (type)))
21530 || (!type && template_parm_p))
21531 && declarator_can_be_parameter_pack (declarator))
21533 /* Consume the `...'. */
21534 cp_lexer_consume_token (parser->lexer);
21535 maybe_warn_variadic_templates ();
21537 /* Build a pack expansion type */
21538 if (template_parm_p)
21539 template_parameter_pack_p = true;
21540 else if (declarator)
21541 declarator->parameter_pack_p = true;
21542 else
21543 decl_specifiers.type = make_pack_expansion (type);
21547 /* The restriction on defining new types applies only to the type
21548 of the parameter, not to the default argument. */
21549 parser->type_definition_forbidden_message = saved_message;
21551 /* If the next token is `=', then process a default argument. */
21552 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21554 tree type = decl_specifiers.type;
21555 token = cp_lexer_peek_token (parser->lexer);
21556 /* If we are defining a class, then the tokens that make up the
21557 default argument must be saved and processed later. */
21558 if (!template_parm_p && at_class_scope_p ()
21559 && TYPE_BEING_DEFINED (current_class_type)
21560 && !LAMBDA_TYPE_P (current_class_type))
21561 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21563 // A constrained-type-specifier may declare a type template-parameter.
21564 else if (declares_constrained_type_template_parameter (type))
21565 default_argument
21566 = cp_parser_default_type_template_argument (parser);
21568 // A constrained-type-specifier may declare a template-template-parameter.
21569 else if (declares_constrained_template_template_parameter (type))
21570 default_argument
21571 = cp_parser_default_template_template_argument (parser);
21573 /* Outside of a class definition, we can just parse the
21574 assignment-expression. */
21575 else
21576 default_argument
21577 = cp_parser_default_argument (parser, template_parm_p);
21579 if (!parser->default_arg_ok_p)
21581 permerror (token->location,
21582 "default arguments are only "
21583 "permitted for function parameters");
21585 else if ((declarator && declarator->parameter_pack_p)
21586 || template_parameter_pack_p
21587 || (decl_specifiers.type
21588 && PACK_EXPANSION_P (decl_specifiers.type)))
21590 /* Find the name of the parameter pack. */
21591 cp_declarator *id_declarator = declarator;
21592 while (id_declarator && id_declarator->kind != cdk_id)
21593 id_declarator = id_declarator->declarator;
21595 if (id_declarator && id_declarator->kind == cdk_id)
21596 error_at (declarator_token_start->location,
21597 template_parm_p
21598 ? G_("template parameter pack %qD "
21599 "cannot have a default argument")
21600 : G_("parameter pack %qD cannot have "
21601 "a default argument"),
21602 id_declarator->u.id.unqualified_name);
21603 else
21604 error_at (declarator_token_start->location,
21605 template_parm_p
21606 ? G_("template parameter pack cannot have "
21607 "a default argument")
21608 : G_("parameter pack cannot have a "
21609 "default argument"));
21611 default_argument = NULL_TREE;
21614 else
21615 default_argument = NULL_TREE;
21617 /* Generate a location for the parameter, ranging from the start of the
21618 initial token to the end of the final token (using input_location for
21619 the latter, set up by cp_lexer_set_source_position_from_token when
21620 consuming tokens).
21622 If we have a identifier, then use it for the caret location, e.g.
21624 extern int callee (int one, int (*two)(int, int), float three);
21625 ~~~~~~^~~~~~~~~~~~~~
21627 otherwise, reuse the start location for the caret location e.g.:
21629 extern int callee (int one, int (*)(int, int), float three);
21630 ^~~~~~~~~~~~~~~~~
21633 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21634 ? declarator->id_loc
21635 : decl_spec_token_start->location);
21636 location_t param_loc = make_location (caret_loc,
21637 decl_spec_token_start->location,
21638 input_location);
21640 return make_parameter_declarator (&decl_specifiers,
21641 declarator,
21642 default_argument,
21643 param_loc,
21644 template_parameter_pack_p);
21647 /* Parse a default argument and return it.
21649 TEMPLATE_PARM_P is true if this is a default argument for a
21650 non-type template parameter. */
21651 static tree
21652 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21654 tree default_argument = NULL_TREE;
21655 bool saved_greater_than_is_operator_p;
21656 bool saved_local_variables_forbidden_p;
21657 bool non_constant_p, is_direct_init;
21659 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21660 set correctly. */
21661 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21662 parser->greater_than_is_operator_p = !template_parm_p;
21663 /* Local variable names (and the `this' keyword) may not
21664 appear in a default argument. */
21665 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21666 parser->local_variables_forbidden_p = true;
21667 /* Parse the assignment-expression. */
21668 if (template_parm_p)
21669 push_deferring_access_checks (dk_no_deferred);
21670 tree saved_class_ptr = NULL_TREE;
21671 tree saved_class_ref = NULL_TREE;
21672 /* The "this" pointer is not valid in a default argument. */
21673 if (cfun)
21675 saved_class_ptr = current_class_ptr;
21676 cp_function_chain->x_current_class_ptr = NULL_TREE;
21677 saved_class_ref = current_class_ref;
21678 cp_function_chain->x_current_class_ref = NULL_TREE;
21680 default_argument
21681 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21682 /* Restore the "this" pointer. */
21683 if (cfun)
21685 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21686 cp_function_chain->x_current_class_ref = saved_class_ref;
21688 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21689 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21690 if (template_parm_p)
21691 pop_deferring_access_checks ();
21692 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21693 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21695 return default_argument;
21698 /* Parse a function-body.
21700 function-body:
21701 compound_statement */
21703 static void
21704 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21706 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21707 ? BCS_TRY_BLOCK : BCS_NORMAL),
21708 true);
21711 /* Parse a ctor-initializer-opt followed by a function-body. Return
21712 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21713 is true we are parsing a function-try-block. */
21715 static void
21716 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21717 bool in_function_try_block)
21719 tree body, list;
21720 const bool check_body_p =
21721 DECL_CONSTRUCTOR_P (current_function_decl)
21722 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21723 tree last = NULL;
21725 /* Begin the function body. */
21726 body = begin_function_body ();
21727 /* Parse the optional ctor-initializer. */
21728 cp_parser_ctor_initializer_opt (parser);
21730 /* If we're parsing a constexpr constructor definition, we need
21731 to check that the constructor body is indeed empty. However,
21732 before we get to cp_parser_function_body lot of junk has been
21733 generated, so we can't just check that we have an empty block.
21734 Rather we take a snapshot of the outermost block, and check whether
21735 cp_parser_function_body changed its state. */
21736 if (check_body_p)
21738 list = cur_stmt_list;
21739 if (STATEMENT_LIST_TAIL (list))
21740 last = STATEMENT_LIST_TAIL (list)->stmt;
21742 /* Parse the function-body. */
21743 cp_parser_function_body (parser, in_function_try_block);
21744 if (check_body_p)
21745 check_constexpr_ctor_body (last, list, /*complain=*/true);
21746 /* Finish the function body. */
21747 finish_function_body (body);
21750 /* Parse an initializer.
21752 initializer:
21753 = initializer-clause
21754 ( expression-list )
21756 Returns an expression representing the initializer. If no
21757 initializer is present, NULL_TREE is returned.
21759 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21760 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21761 set to TRUE if there is no initializer present. If there is an
21762 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21763 is set to true; otherwise it is set to false. */
21765 static tree
21766 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21767 bool* non_constant_p)
21769 cp_token *token;
21770 tree init;
21772 /* Peek at the next token. */
21773 token = cp_lexer_peek_token (parser->lexer);
21775 /* Let our caller know whether or not this initializer was
21776 parenthesized. */
21777 *is_direct_init = (token->type != CPP_EQ);
21778 /* Assume that the initializer is constant. */
21779 *non_constant_p = false;
21781 if (token->type == CPP_EQ)
21783 /* Consume the `='. */
21784 cp_lexer_consume_token (parser->lexer);
21785 /* Parse the initializer-clause. */
21786 init = cp_parser_initializer_clause (parser, non_constant_p);
21788 else if (token->type == CPP_OPEN_PAREN)
21790 vec<tree, va_gc> *vec;
21791 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21792 /*cast_p=*/false,
21793 /*allow_expansion_p=*/true,
21794 non_constant_p);
21795 if (vec == NULL)
21796 return error_mark_node;
21797 init = build_tree_list_vec (vec);
21798 release_tree_vector (vec);
21800 else if (token->type == CPP_OPEN_BRACE)
21802 cp_lexer_set_source_position (parser->lexer);
21803 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21804 init = cp_parser_braced_list (parser, non_constant_p);
21805 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21807 else
21809 /* Anything else is an error. */
21810 cp_parser_error (parser, "expected initializer");
21811 init = error_mark_node;
21814 if (check_for_bare_parameter_packs (init))
21815 init = error_mark_node;
21817 return init;
21820 /* Parse an initializer-clause.
21822 initializer-clause:
21823 assignment-expression
21824 braced-init-list
21826 Returns an expression representing the initializer.
21828 If the `assignment-expression' production is used the value
21829 returned is simply a representation for the expression.
21831 Otherwise, calls cp_parser_braced_list. */
21833 static cp_expr
21834 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21836 cp_expr initializer;
21838 /* Assume the expression is constant. */
21839 *non_constant_p = false;
21841 /* If it is not a `{', then we are looking at an
21842 assignment-expression. */
21843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21845 initializer
21846 = cp_parser_constant_expression (parser,
21847 /*allow_non_constant_p=*/true,
21848 non_constant_p);
21850 else
21851 initializer = cp_parser_braced_list (parser, non_constant_p);
21853 return initializer;
21856 /* Parse a brace-enclosed initializer list.
21858 braced-init-list:
21859 { initializer-list , [opt] }
21860 { designated-initializer-list , [opt] }
21863 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21864 the elements of the initializer-list (or NULL, if the last
21865 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21866 NULL_TREE. There is no way to detect whether or not the optional
21867 trailing `,' was provided. NON_CONSTANT_P is as for
21868 cp_parser_initializer. */
21870 static cp_expr
21871 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21873 tree initializer;
21874 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21876 /* Consume the `{' token. */
21877 matching_braces braces;
21878 braces.consume_open (parser);
21879 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21880 initializer = make_node (CONSTRUCTOR);
21881 /* If it's not a `}', then there is a non-trivial initializer. */
21882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21884 /* Parse the initializer list. */
21885 CONSTRUCTOR_ELTS (initializer)
21886 = cp_parser_initializer_list (parser, non_constant_p);
21887 /* A trailing `,' token is allowed. */
21888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21889 cp_lexer_consume_token (parser->lexer);
21891 else
21892 *non_constant_p = false;
21893 /* Now, there should be a trailing `}'. */
21894 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21895 braces.require_close (parser);
21896 TREE_TYPE (initializer) = init_list_type_node;
21898 cp_expr result (initializer);
21899 /* Build a location of the form:
21900 { ... }
21901 ^~~~~~~
21902 with caret==start at the open brace, finish at the close brace. */
21903 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21904 result.set_location (combined_loc);
21905 return result;
21908 /* Consume tokens up to, and including, the next non-nested closing `]'.
21909 Returns true iff we found a closing `]'. */
21911 static bool
21912 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21914 unsigned square_depth = 0;
21916 while (true)
21918 cp_token * token = cp_lexer_peek_token (parser->lexer);
21920 switch (token->type)
21922 case CPP_EOF:
21923 case CPP_PRAGMA_EOL:
21924 /* If we've run out of tokens, then there is no closing `]'. */
21925 return false;
21927 case CPP_OPEN_SQUARE:
21928 ++square_depth;
21929 break;
21931 case CPP_CLOSE_SQUARE:
21932 if (!square_depth--)
21934 cp_lexer_consume_token (parser->lexer);
21935 return true;
21937 break;
21939 default:
21940 break;
21943 /* Consume the token. */
21944 cp_lexer_consume_token (parser->lexer);
21948 /* Return true if we are looking at an array-designator, false otherwise. */
21950 static bool
21951 cp_parser_array_designator_p (cp_parser *parser)
21953 /* Consume the `['. */
21954 cp_lexer_consume_token (parser->lexer);
21956 cp_lexer_save_tokens (parser->lexer);
21958 /* Skip tokens until the next token is a closing square bracket.
21959 If we find the closing `]', and the next token is a `=', then
21960 we are looking at an array designator. */
21961 bool array_designator_p
21962 = (cp_parser_skip_to_closing_square_bracket (parser)
21963 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21965 /* Roll back the tokens we skipped. */
21966 cp_lexer_rollback_tokens (parser->lexer);
21968 return array_designator_p;
21971 /* Parse an initializer-list.
21973 initializer-list:
21974 initializer-clause ... [opt]
21975 initializer-list , initializer-clause ... [opt]
21977 C++2A Extension:
21979 designated-initializer-list:
21980 designated-initializer-clause
21981 designated-initializer-list , designated-initializer-clause
21983 designated-initializer-clause:
21984 designator brace-or-equal-initializer
21986 designator:
21987 . identifier
21989 GNU Extension:
21991 initializer-list:
21992 designation initializer-clause ...[opt]
21993 initializer-list , designation initializer-clause ...[opt]
21995 designation:
21996 . identifier =
21997 identifier :
21998 [ constant-expression ] =
22000 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22001 for the initializer. If the INDEX of the elt is non-NULL, it is the
22002 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22003 as for cp_parser_initializer. */
22005 static vec<constructor_elt, va_gc> *
22006 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22008 vec<constructor_elt, va_gc> *v = NULL;
22009 bool first_p = true;
22010 tree first_designator = NULL_TREE;
22012 /* Assume all of the expressions are constant. */
22013 *non_constant_p = false;
22015 /* Parse the rest of the list. */
22016 while (true)
22018 cp_token *token;
22019 tree designator;
22020 tree initializer;
22021 bool clause_non_constant_p;
22022 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22024 /* Handle the C++2A syntax, '. id ='. */
22025 if ((cxx_dialect >= cxx2a
22026 || cp_parser_allow_gnu_extensions_p (parser))
22027 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22028 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22029 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22030 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22031 == CPP_OPEN_BRACE)))
22033 if (cxx_dialect < cxx2a)
22034 pedwarn (loc, OPT_Wpedantic,
22035 "C++ designated initializers only available with "
22036 "-std=c++2a or -std=gnu++2a");
22037 /* Consume the `.'. */
22038 cp_lexer_consume_token (parser->lexer);
22039 /* Consume the identifier. */
22040 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22042 /* Consume the `='. */
22043 cp_lexer_consume_token (parser->lexer);
22045 /* Also, if the next token is an identifier and the following one is a
22046 colon, we are looking at the GNU designated-initializer
22047 syntax. */
22048 else if (cp_parser_allow_gnu_extensions_p (parser)
22049 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22051 == CPP_COLON))
22053 /* Warn the user that they are using an extension. */
22054 pedwarn (loc, OPT_Wpedantic,
22055 "ISO C++ does not allow GNU designated initializers");
22056 /* Consume the identifier. */
22057 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22058 /* Consume the `:'. */
22059 cp_lexer_consume_token (parser->lexer);
22061 /* Also handle C99 array designators, '[ const ] ='. */
22062 else if (cp_parser_allow_gnu_extensions_p (parser)
22063 && !c_dialect_objc ()
22064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22066 /* In C++11, [ could start a lambda-introducer. */
22067 bool non_const = false;
22069 cp_parser_parse_tentatively (parser);
22071 if (!cp_parser_array_designator_p (parser))
22073 cp_parser_simulate_error (parser);
22074 designator = NULL_TREE;
22076 else
22078 designator = cp_parser_constant_expression (parser, true,
22079 &non_const);
22080 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22081 cp_parser_require (parser, CPP_EQ, RT_EQ);
22084 if (!cp_parser_parse_definitely (parser))
22085 designator = NULL_TREE;
22086 else if (non_const)
22087 require_potential_rvalue_constant_expression (designator);
22088 if (designator)
22089 /* Warn the user that they are using an extension. */
22090 pedwarn (loc, OPT_Wpedantic,
22091 "ISO C++ does not allow C99 designated initializers");
22093 else
22094 designator = NULL_TREE;
22096 if (first_p)
22098 first_designator = designator;
22099 first_p = false;
22101 else if (cxx_dialect >= cxx2a
22102 && first_designator != error_mark_node
22103 && (!first_designator != !designator))
22105 error_at (loc, "either all initializer clauses should be designated "
22106 "or none of them should be");
22107 first_designator = error_mark_node;
22109 else if (cxx_dialect < cxx2a && !first_designator)
22110 first_designator = designator;
22112 /* Parse the initializer. */
22113 initializer = cp_parser_initializer_clause (parser,
22114 &clause_non_constant_p);
22115 /* If any clause is non-constant, so is the entire initializer. */
22116 if (clause_non_constant_p)
22117 *non_constant_p = true;
22119 /* If we have an ellipsis, this is an initializer pack
22120 expansion. */
22121 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22125 /* Consume the `...'. */
22126 cp_lexer_consume_token (parser->lexer);
22128 if (designator && cxx_dialect >= cxx2a)
22129 error_at (loc,
22130 "%<...%> not allowed in designated initializer list");
22132 /* Turn the initializer into an initializer expansion. */
22133 initializer = make_pack_expansion (initializer);
22136 /* Add it to the vector. */
22137 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22139 /* If the next token is not a comma, we have reached the end of
22140 the list. */
22141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22142 break;
22144 /* Peek at the next token. */
22145 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22146 /* If the next token is a `}', then we're still done. An
22147 initializer-clause can have a trailing `,' after the
22148 initializer-list and before the closing `}'. */
22149 if (token->type == CPP_CLOSE_BRACE)
22150 break;
22152 /* Consume the `,' token. */
22153 cp_lexer_consume_token (parser->lexer);
22156 /* The same identifier shall not appear in multiple designators
22157 of a designated-initializer-list. */
22158 if (first_designator)
22160 unsigned int i;
22161 tree designator, val;
22162 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22163 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22165 if (IDENTIFIER_MARKED (designator))
22167 error_at (EXPR_LOC_OR_LOC (val, input_location),
22168 "%<.%s%> designator used multiple times in "
22169 "the same initializer list",
22170 IDENTIFIER_POINTER (designator));
22171 (*v)[i].index = NULL_TREE;
22173 else
22174 IDENTIFIER_MARKED (designator) = 1;
22176 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22177 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22178 IDENTIFIER_MARKED (designator) = 0;
22181 return v;
22184 /* Classes [gram.class] */
22186 /* Parse a class-name.
22188 class-name:
22189 identifier
22190 template-id
22192 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22193 to indicate that names looked up in dependent types should be
22194 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22195 keyword has been used to indicate that the name that appears next
22196 is a template. TAG_TYPE indicates the explicit tag given before
22197 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22198 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22199 is the class being defined in a class-head. If ENUM_OK is TRUE,
22200 enum-names are also accepted.
22202 Returns the TYPE_DECL representing the class. */
22204 static tree
22205 cp_parser_class_name (cp_parser *parser,
22206 bool typename_keyword_p,
22207 bool template_keyword_p,
22208 enum tag_types tag_type,
22209 bool check_dependency_p,
22210 bool class_head_p,
22211 bool is_declaration,
22212 bool enum_ok)
22214 tree decl;
22215 tree scope;
22216 bool typename_p;
22217 cp_token *token;
22218 tree identifier = NULL_TREE;
22220 /* All class-names start with an identifier. */
22221 token = cp_lexer_peek_token (parser->lexer);
22222 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22224 cp_parser_error (parser, "expected class-name");
22225 return error_mark_node;
22228 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22229 to a template-id, so we save it here. */
22230 scope = parser->scope;
22231 if (scope == error_mark_node)
22232 return error_mark_node;
22234 /* Any name names a type if we're following the `typename' keyword
22235 in a qualified name where the enclosing scope is type-dependent. */
22236 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22237 && dependent_type_p (scope));
22238 /* Handle the common case (an identifier, but not a template-id)
22239 efficiently. */
22240 if (token->type == CPP_NAME
22241 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22243 cp_token *identifier_token;
22244 bool ambiguous_p;
22246 /* Look for the identifier. */
22247 identifier_token = cp_lexer_peek_token (parser->lexer);
22248 ambiguous_p = identifier_token->error_reported;
22249 identifier = cp_parser_identifier (parser);
22250 /* If the next token isn't an identifier, we are certainly not
22251 looking at a class-name. */
22252 if (identifier == error_mark_node)
22253 decl = error_mark_node;
22254 /* If we know this is a type-name, there's no need to look it
22255 up. */
22256 else if (typename_p)
22257 decl = identifier;
22258 else
22260 tree ambiguous_decls;
22261 /* If we already know that this lookup is ambiguous, then
22262 we've already issued an error message; there's no reason
22263 to check again. */
22264 if (ambiguous_p)
22266 cp_parser_simulate_error (parser);
22267 return error_mark_node;
22269 /* If the next token is a `::', then the name must be a type
22270 name.
22272 [basic.lookup.qual]
22274 During the lookup for a name preceding the :: scope
22275 resolution operator, object, function, and enumerator
22276 names are ignored. */
22277 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22278 tag_type = scope_type;
22279 /* Look up the name. */
22280 decl = cp_parser_lookup_name (parser, identifier,
22281 tag_type,
22282 /*is_template=*/false,
22283 /*is_namespace=*/false,
22284 check_dependency_p,
22285 &ambiguous_decls,
22286 identifier_token->location);
22287 if (ambiguous_decls)
22289 if (cp_parser_parsing_tentatively (parser))
22290 cp_parser_simulate_error (parser);
22291 return error_mark_node;
22295 else
22297 /* Try a template-id. */
22298 decl = cp_parser_template_id (parser, template_keyword_p,
22299 check_dependency_p,
22300 tag_type,
22301 is_declaration);
22302 if (decl == error_mark_node)
22303 return error_mark_node;
22306 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22308 /* If this is a typename, create a TYPENAME_TYPE. */
22309 if (typename_p && decl != error_mark_node)
22311 decl = make_typename_type (scope, decl, typename_type,
22312 /*complain=*/tf_error);
22313 if (decl != error_mark_node)
22314 decl = TYPE_NAME (decl);
22317 decl = strip_using_decl (decl);
22319 /* Check to see that it is really the name of a class. */
22320 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22321 && identifier_p (TREE_OPERAND (decl, 0))
22322 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22323 /* Situations like this:
22325 template <typename T> struct A {
22326 typename T::template X<int>::I i;
22329 are problematic. Is `T::template X<int>' a class-name? The
22330 standard does not seem to be definitive, but there is no other
22331 valid interpretation of the following `::'. Therefore, those
22332 names are considered class-names. */
22334 decl = make_typename_type (scope, decl, tag_type, tf_error);
22335 if (decl != error_mark_node)
22336 decl = TYPE_NAME (decl);
22338 else if (TREE_CODE (decl) != TYPE_DECL
22339 || TREE_TYPE (decl) == error_mark_node
22340 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22341 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22342 /* In Objective-C 2.0, a classname followed by '.' starts a
22343 dot-syntax expression, and it's not a type-name. */
22344 || (c_dialect_objc ()
22345 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22346 && objc_is_class_name (decl)))
22347 decl = error_mark_node;
22349 if (decl == error_mark_node)
22350 cp_parser_error (parser, "expected class-name");
22351 else if (identifier && !parser->scope)
22352 maybe_note_name_used_in_class (identifier, decl);
22354 return decl;
22357 /* Parse a class-specifier.
22359 class-specifier:
22360 class-head { member-specification [opt] }
22362 Returns the TREE_TYPE representing the class. */
22364 static tree
22365 cp_parser_class_specifier_1 (cp_parser* parser)
22367 tree type;
22368 tree attributes = NULL_TREE;
22369 bool nested_name_specifier_p;
22370 unsigned saved_num_template_parameter_lists;
22371 bool saved_in_function_body;
22372 unsigned char in_statement;
22373 bool in_switch_statement_p;
22374 bool saved_in_unbraced_linkage_specification_p;
22375 tree old_scope = NULL_TREE;
22376 tree scope = NULL_TREE;
22377 cp_token *closing_brace;
22379 push_deferring_access_checks (dk_no_deferred);
22381 /* Parse the class-head. */
22382 type = cp_parser_class_head (parser,
22383 &nested_name_specifier_p);
22384 /* If the class-head was a semantic disaster, skip the entire body
22385 of the class. */
22386 if (!type)
22388 cp_parser_skip_to_end_of_block_or_statement (parser);
22389 pop_deferring_access_checks ();
22390 return error_mark_node;
22393 /* Look for the `{'. */
22394 matching_braces braces;
22395 if (!braces.require_open (parser))
22397 pop_deferring_access_checks ();
22398 return error_mark_node;
22401 cp_ensure_no_omp_declare_simd (parser);
22402 cp_ensure_no_oacc_routine (parser);
22404 /* Issue an error message if type-definitions are forbidden here. */
22405 cp_parser_check_type_definition (parser);
22406 /* Remember that we are defining one more class. */
22407 ++parser->num_classes_being_defined;
22408 /* Inside the class, surrounding template-parameter-lists do not
22409 apply. */
22410 saved_num_template_parameter_lists
22411 = parser->num_template_parameter_lists;
22412 parser->num_template_parameter_lists = 0;
22413 /* We are not in a function body. */
22414 saved_in_function_body = parser->in_function_body;
22415 parser->in_function_body = false;
22416 /* Or in a loop. */
22417 in_statement = parser->in_statement;
22418 parser->in_statement = 0;
22419 /* Or in a switch. */
22420 in_switch_statement_p = parser->in_switch_statement_p;
22421 parser->in_switch_statement_p = false;
22422 /* We are not immediately inside an extern "lang" block. */
22423 saved_in_unbraced_linkage_specification_p
22424 = parser->in_unbraced_linkage_specification_p;
22425 parser->in_unbraced_linkage_specification_p = false;
22427 // Associate constraints with the type.
22428 if (flag_concepts)
22429 type = associate_classtype_constraints (type);
22431 /* Start the class. */
22432 if (nested_name_specifier_p)
22434 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22435 old_scope = push_inner_scope (scope);
22437 type = begin_class_definition (type);
22439 if (type == error_mark_node)
22440 /* If the type is erroneous, skip the entire body of the class. */
22441 cp_parser_skip_to_closing_brace (parser);
22442 else
22443 /* Parse the member-specification. */
22444 cp_parser_member_specification_opt (parser);
22446 /* Look for the trailing `}'. */
22447 closing_brace = braces.require_close (parser);
22448 /* Look for trailing attributes to apply to this class. */
22449 if (cp_parser_allow_gnu_extensions_p (parser))
22450 attributes = cp_parser_gnu_attributes_opt (parser);
22451 if (type != error_mark_node)
22452 type = finish_struct (type, attributes);
22453 if (nested_name_specifier_p)
22454 pop_inner_scope (old_scope, scope);
22456 /* We've finished a type definition. Check for the common syntax
22457 error of forgetting a semicolon after the definition. We need to
22458 be careful, as we can't just check for not-a-semicolon and be done
22459 with it; the user might have typed:
22461 class X { } c = ...;
22462 class X { } *p = ...;
22464 and so forth. Instead, enumerate all the possible tokens that
22465 might follow this production; if we don't see one of them, then
22466 complain and silently insert the semicolon. */
22468 cp_token *token = cp_lexer_peek_token (parser->lexer);
22469 bool want_semicolon = true;
22471 if (cp_next_tokens_can_be_std_attribute_p (parser))
22472 /* Don't try to parse c++11 attributes here. As per the
22473 grammar, that should be a task for
22474 cp_parser_decl_specifier_seq. */
22475 want_semicolon = false;
22477 switch (token->type)
22479 case CPP_NAME:
22480 case CPP_SEMICOLON:
22481 case CPP_MULT:
22482 case CPP_AND:
22483 case CPP_OPEN_PAREN:
22484 case CPP_CLOSE_PAREN:
22485 case CPP_COMMA:
22486 want_semicolon = false;
22487 break;
22489 /* While it's legal for type qualifiers and storage class
22490 specifiers to follow type definitions in the grammar, only
22491 compiler testsuites contain code like that. Assume that if
22492 we see such code, then what we're really seeing is a case
22493 like:
22495 class X { }
22496 const <type> var = ...;
22500 class Y { }
22501 static <type> func (...) ...
22503 i.e. the qualifier or specifier applies to the next
22504 declaration. To do so, however, we need to look ahead one
22505 more token to see if *that* token is a type specifier.
22507 This code could be improved to handle:
22509 class Z { }
22510 static const <type> var = ...; */
22511 case CPP_KEYWORD:
22512 if (keyword_is_decl_specifier (token->keyword))
22514 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22516 /* Handling user-defined types here would be nice, but very
22517 tricky. */
22518 want_semicolon
22519 = (lookahead->type == CPP_KEYWORD
22520 && keyword_begins_type_specifier (lookahead->keyword));
22522 break;
22523 default:
22524 break;
22527 /* If we don't have a type, then something is very wrong and we
22528 shouldn't try to do anything clever. Likewise for not seeing the
22529 closing brace. */
22530 if (closing_brace && TYPE_P (type) && want_semicolon)
22532 /* Locate the closing brace. */
22533 cp_token_position prev
22534 = cp_lexer_previous_token_position (parser->lexer);
22535 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22536 location_t loc = prev_token->location;
22538 /* We want to suggest insertion of a ';' immediately *after* the
22539 closing brace, so, if we can, offset the location by 1 column. */
22540 location_t next_loc = loc;
22541 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22542 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22544 rich_location richloc (line_table, next_loc);
22546 /* If we successfully offset the location, suggest the fix-it. */
22547 if (next_loc != loc)
22548 richloc.add_fixit_insert_before (next_loc, ";");
22550 if (CLASSTYPE_DECLARED_CLASS (type))
22551 error_at (&richloc,
22552 "expected %<;%> after class definition");
22553 else if (TREE_CODE (type) == RECORD_TYPE)
22554 error_at (&richloc,
22555 "expected %<;%> after struct definition");
22556 else if (TREE_CODE (type) == UNION_TYPE)
22557 error_at (&richloc,
22558 "expected %<;%> after union definition");
22559 else
22560 gcc_unreachable ();
22562 /* Unget one token and smash it to look as though we encountered
22563 a semicolon in the input stream. */
22564 cp_lexer_set_token_position (parser->lexer, prev);
22565 token = cp_lexer_peek_token (parser->lexer);
22566 token->type = CPP_SEMICOLON;
22567 token->keyword = RID_MAX;
22571 /* If this class is not itself within the scope of another class,
22572 then we need to parse the bodies of all of the queued function
22573 definitions. Note that the queued functions defined in a class
22574 are not always processed immediately following the
22575 class-specifier for that class. Consider:
22577 struct A {
22578 struct B { void f() { sizeof (A); } };
22581 If `f' were processed before the processing of `A' were
22582 completed, there would be no way to compute the size of `A'.
22583 Note that the nesting we are interested in here is lexical --
22584 not the semantic nesting given by TYPE_CONTEXT. In particular,
22585 for:
22587 struct A { struct B; };
22588 struct A::B { void f() { } };
22590 there is no need to delay the parsing of `A::B::f'. */
22591 if (--parser->num_classes_being_defined == 0)
22593 tree decl;
22594 tree class_type = NULL_TREE;
22595 tree pushed_scope = NULL_TREE;
22596 unsigned ix;
22597 cp_default_arg_entry *e;
22598 tree save_ccp, save_ccr;
22600 /* In a first pass, parse default arguments to the functions.
22601 Then, in a second pass, parse the bodies of the functions.
22602 This two-phased approach handles cases like:
22604 struct S {
22605 void f() { g(); }
22606 void g(int i = 3);
22610 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22612 decl = e->decl;
22613 /* If there are default arguments that have not yet been processed,
22614 take care of them now. */
22615 if (class_type != e->class_type)
22617 if (pushed_scope)
22618 pop_scope (pushed_scope);
22619 class_type = e->class_type;
22620 pushed_scope = push_scope (class_type);
22622 /* Make sure that any template parameters are in scope. */
22623 maybe_begin_member_template_processing (decl);
22624 /* Parse the default argument expressions. */
22625 cp_parser_late_parsing_default_args (parser, decl);
22626 /* Remove any template parameters from the symbol table. */
22627 maybe_end_member_template_processing ();
22629 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22630 /* Now parse any NSDMIs. */
22631 save_ccp = current_class_ptr;
22632 save_ccr = current_class_ref;
22633 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22635 if (class_type != DECL_CONTEXT (decl))
22637 if (pushed_scope)
22638 pop_scope (pushed_scope);
22639 class_type = DECL_CONTEXT (decl);
22640 pushed_scope = push_scope (class_type);
22642 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22643 cp_parser_late_parsing_nsdmi (parser, decl);
22645 vec_safe_truncate (unparsed_nsdmis, 0);
22646 current_class_ptr = save_ccp;
22647 current_class_ref = save_ccr;
22648 if (pushed_scope)
22649 pop_scope (pushed_scope);
22651 /* Now do some post-NSDMI bookkeeping. */
22652 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22653 after_nsdmi_defaulted_late_checks (class_type);
22654 vec_safe_truncate (unparsed_classes, 0);
22655 after_nsdmi_defaulted_late_checks (type);
22657 /* Now parse the body of the functions. */
22658 if (flag_openmp)
22660 /* OpenMP UDRs need to be parsed before all other functions. */
22661 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22662 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22663 cp_parser_late_parsing_for_member (parser, decl);
22664 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22665 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22666 cp_parser_late_parsing_for_member (parser, decl);
22668 else
22669 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22670 cp_parser_late_parsing_for_member (parser, decl);
22671 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22673 else
22674 vec_safe_push (unparsed_classes, type);
22676 /* Put back any saved access checks. */
22677 pop_deferring_access_checks ();
22679 /* Restore saved state. */
22680 parser->in_switch_statement_p = in_switch_statement_p;
22681 parser->in_statement = in_statement;
22682 parser->in_function_body = saved_in_function_body;
22683 parser->num_template_parameter_lists
22684 = saved_num_template_parameter_lists;
22685 parser->in_unbraced_linkage_specification_p
22686 = saved_in_unbraced_linkage_specification_p;
22688 return type;
22691 static tree
22692 cp_parser_class_specifier (cp_parser* parser)
22694 tree ret;
22695 timevar_push (TV_PARSE_STRUCT);
22696 ret = cp_parser_class_specifier_1 (parser);
22697 timevar_pop (TV_PARSE_STRUCT);
22698 return ret;
22701 /* Parse a class-head.
22703 class-head:
22704 class-key identifier [opt] base-clause [opt]
22705 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22706 class-key nested-name-specifier [opt] template-id
22707 base-clause [opt]
22709 class-virt-specifier:
22710 final
22712 GNU Extensions:
22713 class-key attributes identifier [opt] base-clause [opt]
22714 class-key attributes nested-name-specifier identifier base-clause [opt]
22715 class-key attributes nested-name-specifier [opt] template-id
22716 base-clause [opt]
22718 Upon return BASES is initialized to the list of base classes (or
22719 NULL, if there are none) in the same form returned by
22720 cp_parser_base_clause.
22722 Returns the TYPE of the indicated class. Sets
22723 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22724 involving a nested-name-specifier was used, and FALSE otherwise.
22726 Returns error_mark_node if this is not a class-head.
22728 Returns NULL_TREE if the class-head is syntactically valid, but
22729 semantically invalid in a way that means we should skip the entire
22730 body of the class. */
22732 static tree
22733 cp_parser_class_head (cp_parser* parser,
22734 bool* nested_name_specifier_p)
22736 tree nested_name_specifier;
22737 enum tag_types class_key;
22738 tree id = NULL_TREE;
22739 tree type = NULL_TREE;
22740 tree attributes;
22741 tree bases;
22742 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22743 bool template_id_p = false;
22744 bool qualified_p = false;
22745 bool invalid_nested_name_p = false;
22746 bool invalid_explicit_specialization_p = false;
22747 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22748 tree pushed_scope = NULL_TREE;
22749 unsigned num_templates;
22750 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22751 /* Assume no nested-name-specifier will be present. */
22752 *nested_name_specifier_p = false;
22753 /* Assume no template parameter lists will be used in defining the
22754 type. */
22755 num_templates = 0;
22756 parser->colon_corrects_to_scope_p = false;
22758 /* Look for the class-key. */
22759 class_key = cp_parser_class_key (parser);
22760 if (class_key == none_type)
22761 return error_mark_node;
22763 location_t class_head_start_location = input_location;
22765 /* Parse the attributes. */
22766 attributes = cp_parser_attributes_opt (parser);
22768 /* If the next token is `::', that is invalid -- but sometimes
22769 people do try to write:
22771 struct ::S {};
22773 Handle this gracefully by accepting the extra qualifier, and then
22774 issuing an error about it later if this really is a
22775 class-head. If it turns out just to be an elaborated type
22776 specifier, remain silent. */
22777 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22778 qualified_p = true;
22780 push_deferring_access_checks (dk_no_check);
22782 /* Determine the name of the class. Begin by looking for an
22783 optional nested-name-specifier. */
22784 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22785 nested_name_specifier
22786 = cp_parser_nested_name_specifier_opt (parser,
22787 /*typename_keyword_p=*/false,
22788 /*check_dependency_p=*/false,
22789 /*type_p=*/true,
22790 /*is_declaration=*/false);
22791 /* If there was a nested-name-specifier, then there *must* be an
22792 identifier. */
22794 cp_token *bad_template_keyword = NULL;
22796 if (nested_name_specifier)
22798 type_start_token = cp_lexer_peek_token (parser->lexer);
22799 /* Although the grammar says `identifier', it really means
22800 `class-name' or `template-name'. You are only allowed to
22801 define a class that has already been declared with this
22802 syntax.
22804 The proposed resolution for Core Issue 180 says that wherever
22805 you see `class T::X' you should treat `X' as a type-name.
22807 It is OK to define an inaccessible class; for example:
22809 class A { class B; };
22810 class A::B {};
22812 We do not know if we will see a class-name, or a
22813 template-name. We look for a class-name first, in case the
22814 class-name is a template-id; if we looked for the
22815 template-name first we would stop after the template-name. */
22816 cp_parser_parse_tentatively (parser);
22817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22818 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22819 type = cp_parser_class_name (parser,
22820 /*typename_keyword_p=*/false,
22821 /*template_keyword_p=*/false,
22822 class_type,
22823 /*check_dependency_p=*/false,
22824 /*class_head_p=*/true,
22825 /*is_declaration=*/false);
22826 /* If that didn't work, ignore the nested-name-specifier. */
22827 if (!cp_parser_parse_definitely (parser))
22829 invalid_nested_name_p = true;
22830 type_start_token = cp_lexer_peek_token (parser->lexer);
22831 id = cp_parser_identifier (parser);
22832 if (id == error_mark_node)
22833 id = NULL_TREE;
22835 /* If we could not find a corresponding TYPE, treat this
22836 declaration like an unqualified declaration. */
22837 if (type == error_mark_node)
22838 nested_name_specifier = NULL_TREE;
22839 /* Otherwise, count the number of templates used in TYPE and its
22840 containing scopes. */
22841 else
22843 tree scope;
22845 for (scope = TREE_TYPE (type);
22846 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22847 scope = get_containing_scope (scope))
22848 if (TYPE_P (scope)
22849 && CLASS_TYPE_P (scope)
22850 && CLASSTYPE_TEMPLATE_INFO (scope)
22851 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22852 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22853 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22854 ++num_templates;
22857 /* Otherwise, the identifier is optional. */
22858 else
22860 /* We don't know whether what comes next is a template-id,
22861 an identifier, or nothing at all. */
22862 cp_parser_parse_tentatively (parser);
22863 /* Check for a template-id. */
22864 type_start_token = cp_lexer_peek_token (parser->lexer);
22865 id = cp_parser_template_id (parser,
22866 /*template_keyword_p=*/false,
22867 /*check_dependency_p=*/true,
22868 class_key,
22869 /*is_declaration=*/true);
22870 /* If that didn't work, it could still be an identifier. */
22871 if (!cp_parser_parse_definitely (parser))
22873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22875 type_start_token = cp_lexer_peek_token (parser->lexer);
22876 id = cp_parser_identifier (parser);
22878 else
22879 id = NULL_TREE;
22881 else
22883 template_id_p = true;
22884 ++num_templates;
22888 pop_deferring_access_checks ();
22890 if (id)
22892 cp_parser_check_for_invalid_template_id (parser, id,
22893 class_key,
22894 type_start_token->location);
22896 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22898 /* If it's not a `:' or a `{' then we can't really be looking at a
22899 class-head, since a class-head only appears as part of a
22900 class-specifier. We have to detect this situation before calling
22901 xref_tag, since that has irreversible side-effects. */
22902 if (!cp_parser_next_token_starts_class_definition_p (parser))
22904 cp_parser_error (parser, "expected %<{%> or %<:%>");
22905 type = error_mark_node;
22906 goto out;
22909 /* At this point, we're going ahead with the class-specifier, even
22910 if some other problem occurs. */
22911 cp_parser_commit_to_tentative_parse (parser);
22912 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22914 cp_parser_error (parser,
22915 "cannot specify %<override%> for a class");
22916 type = error_mark_node;
22917 goto out;
22919 /* Issue the error about the overly-qualified name now. */
22920 if (qualified_p)
22922 cp_parser_error (parser,
22923 "global qualification of class name is invalid");
22924 type = error_mark_node;
22925 goto out;
22927 else if (invalid_nested_name_p)
22929 cp_parser_error (parser,
22930 "qualified name does not name a class");
22931 type = error_mark_node;
22932 goto out;
22934 else if (nested_name_specifier)
22936 tree scope;
22938 if (bad_template_keyword)
22939 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22940 keyword template shall not appear at the top level. */
22941 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22942 "keyword %<template%> not allowed in class-head-name");
22944 /* Reject typedef-names in class heads. */
22945 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22947 error_at (type_start_token->location,
22948 "invalid class name in declaration of %qD",
22949 type);
22950 type = NULL_TREE;
22951 goto done;
22954 /* Figure out in what scope the declaration is being placed. */
22955 scope = current_scope ();
22956 /* If that scope does not contain the scope in which the
22957 class was originally declared, the program is invalid. */
22958 if (scope && !is_ancestor (scope, nested_name_specifier))
22960 if (at_namespace_scope_p ())
22961 error_at (type_start_token->location,
22962 "declaration of %qD in namespace %qD which does not "
22963 "enclose %qD",
22964 type, scope, nested_name_specifier);
22965 else
22966 error_at (type_start_token->location,
22967 "declaration of %qD in %qD which does not enclose %qD",
22968 type, scope, nested_name_specifier);
22969 type = NULL_TREE;
22970 goto done;
22972 /* [dcl.meaning]
22974 A declarator-id shall not be qualified except for the
22975 definition of a ... nested class outside of its class
22976 ... [or] the definition or explicit instantiation of a
22977 class member of a namespace outside of its namespace. */
22978 if (scope == nested_name_specifier)
22980 permerror (nested_name_specifier_token_start->location,
22981 "extra qualification not allowed");
22982 nested_name_specifier = NULL_TREE;
22983 num_templates = 0;
22986 /* An explicit-specialization must be preceded by "template <>". If
22987 it is not, try to recover gracefully. */
22988 if (at_namespace_scope_p ()
22989 && parser->num_template_parameter_lists == 0
22990 && !processing_template_parmlist
22991 && template_id_p)
22993 /* Build a location of this form:
22994 struct typename <ARGS>
22995 ^~~~~~~~~~~~~~~~~~~~~~
22996 with caret==start at the start token, and
22997 finishing at the end of the type. */
22998 location_t reported_loc
22999 = make_location (class_head_start_location,
23000 class_head_start_location,
23001 get_finish (type_start_token->location));
23002 rich_location richloc (line_table, reported_loc);
23003 richloc.add_fixit_insert_before (class_head_start_location,
23004 "template <> ");
23005 error_at (&richloc,
23006 "an explicit specialization must be preceded by"
23007 " %<template <>%>");
23008 invalid_explicit_specialization_p = true;
23009 /* Take the same action that would have been taken by
23010 cp_parser_explicit_specialization. */
23011 ++parser->num_template_parameter_lists;
23012 begin_specialization ();
23014 /* There must be no "return" statements between this point and the
23015 end of this function; set "type "to the correct return value and
23016 use "goto done;" to return. */
23017 /* Make sure that the right number of template parameters were
23018 present. */
23019 if (!cp_parser_check_template_parameters (parser, num_templates,
23020 type_start_token->location,
23021 /*declarator=*/NULL))
23023 /* If something went wrong, there is no point in even trying to
23024 process the class-definition. */
23025 type = NULL_TREE;
23026 goto done;
23029 /* Look up the type. */
23030 if (template_id_p)
23032 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23033 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23034 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23036 error_at (type_start_token->location,
23037 "function template %qD redeclared as a class template", id);
23038 type = error_mark_node;
23040 else
23042 type = TREE_TYPE (id);
23043 type = maybe_process_partial_specialization (type);
23045 /* Check the scope while we still know whether or not we had a
23046 nested-name-specifier. */
23047 if (type != error_mark_node)
23048 check_unqualified_spec_or_inst (type, type_start_token->location);
23050 if (nested_name_specifier)
23051 pushed_scope = push_scope (nested_name_specifier);
23053 else if (nested_name_specifier)
23055 tree class_type;
23057 /* Given:
23059 template <typename T> struct S { struct T };
23060 template <typename T> struct S<T>::T { };
23062 we will get a TYPENAME_TYPE when processing the definition of
23063 `S::T'. We need to resolve it to the actual type before we
23064 try to define it. */
23065 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23067 class_type = resolve_typename_type (TREE_TYPE (type),
23068 /*only_current_p=*/false);
23069 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23070 type = TYPE_NAME (class_type);
23071 else
23073 cp_parser_error (parser, "could not resolve typename type");
23074 type = error_mark_node;
23078 if (maybe_process_partial_specialization (TREE_TYPE (type))
23079 == error_mark_node)
23081 type = NULL_TREE;
23082 goto done;
23085 class_type = current_class_type;
23086 /* Enter the scope indicated by the nested-name-specifier. */
23087 pushed_scope = push_scope (nested_name_specifier);
23088 /* Get the canonical version of this type. */
23089 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23090 /* Call push_template_decl if it seems like we should be defining a
23091 template either from the template headers or the type we're
23092 defining, so that we diagnose both extra and missing headers. */
23093 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23094 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23095 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23097 type = push_template_decl (type);
23098 if (type == error_mark_node)
23100 type = NULL_TREE;
23101 goto done;
23105 type = TREE_TYPE (type);
23106 *nested_name_specifier_p = true;
23108 else /* The name is not a nested name. */
23110 /* If the class was unnamed, create a dummy name. */
23111 if (!id)
23112 id = make_anon_name ();
23113 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23114 ? ts_within_enclosing_non_class
23115 : ts_current);
23116 type = xref_tag (class_key, id, tag_scope,
23117 parser->num_template_parameter_lists);
23120 /* Indicate whether this class was declared as a `class' or as a
23121 `struct'. */
23122 if (TREE_CODE (type) == RECORD_TYPE)
23123 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23124 cp_parser_check_class_key (class_key, type);
23126 /* If this type was already complete, and we see another definition,
23127 that's an error. */
23128 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23130 error_at (type_start_token->location, "redefinition of %q#T",
23131 type);
23132 inform (location_of (type), "previous definition of %q#T",
23133 type);
23134 type = NULL_TREE;
23135 goto done;
23137 else if (type == error_mark_node)
23138 type = NULL_TREE;
23140 if (type)
23142 /* Apply attributes now, before any use of the class as a template
23143 argument in its base list. */
23144 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23145 fixup_attribute_variants (type);
23148 /* We will have entered the scope containing the class; the names of
23149 base classes should be looked up in that context. For example:
23151 struct A { struct B {}; struct C; };
23152 struct A::C : B {};
23154 is valid. */
23156 /* Get the list of base-classes, if there is one. */
23157 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23159 /* PR59482: enter the class scope so that base-specifiers are looked
23160 up correctly. */
23161 if (type)
23162 pushclass (type);
23163 bases = cp_parser_base_clause (parser);
23164 /* PR59482: get out of the previously pushed class scope so that the
23165 subsequent pops pop the right thing. */
23166 if (type)
23167 popclass ();
23169 else
23170 bases = NULL_TREE;
23172 /* If we're really defining a class, process the base classes.
23173 If they're invalid, fail. */
23174 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23175 xref_basetypes (type, bases);
23177 done:
23178 /* Leave the scope given by the nested-name-specifier. We will
23179 enter the class scope itself while processing the members. */
23180 if (pushed_scope)
23181 pop_scope (pushed_scope);
23183 if (invalid_explicit_specialization_p)
23185 end_specialization ();
23186 --parser->num_template_parameter_lists;
23189 if (type)
23190 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23191 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23192 CLASSTYPE_FINAL (type) = 1;
23193 out:
23194 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23195 return type;
23198 /* Parse a class-key.
23200 class-key:
23201 class
23202 struct
23203 union
23205 Returns the kind of class-key specified, or none_type to indicate
23206 error. */
23208 static enum tag_types
23209 cp_parser_class_key (cp_parser* parser)
23211 cp_token *token;
23212 enum tag_types tag_type;
23214 /* Look for the class-key. */
23215 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23216 if (!token)
23217 return none_type;
23219 /* Check to see if the TOKEN is a class-key. */
23220 tag_type = cp_parser_token_is_class_key (token);
23221 if (!tag_type)
23222 cp_parser_error (parser, "expected class-key");
23223 return tag_type;
23226 /* Parse a type-parameter-key.
23228 type-parameter-key:
23229 class
23230 typename
23233 static void
23234 cp_parser_type_parameter_key (cp_parser* parser)
23236 /* Look for the type-parameter-key. */
23237 enum tag_types tag_type = none_type;
23238 cp_token *token = cp_lexer_peek_token (parser->lexer);
23239 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23241 cp_lexer_consume_token (parser->lexer);
23242 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23243 /* typename is not allowed in a template template parameter
23244 by the standard until C++17. */
23245 pedwarn (token->location, OPT_Wpedantic,
23246 "ISO C++ forbids typename key in template template parameter;"
23247 " use -std=c++17 or -std=gnu++17");
23249 else
23250 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23252 return;
23255 /* Parse an (optional) member-specification.
23257 member-specification:
23258 member-declaration member-specification [opt]
23259 access-specifier : member-specification [opt] */
23261 static void
23262 cp_parser_member_specification_opt (cp_parser* parser)
23264 while (true)
23266 cp_token *token;
23267 enum rid keyword;
23269 /* Peek at the next token. */
23270 token = cp_lexer_peek_token (parser->lexer);
23271 /* If it's a `}', or EOF then we've seen all the members. */
23272 if (token->type == CPP_CLOSE_BRACE
23273 || token->type == CPP_EOF
23274 || token->type == CPP_PRAGMA_EOL)
23275 break;
23277 /* See if this token is a keyword. */
23278 keyword = token->keyword;
23279 switch (keyword)
23281 case RID_PUBLIC:
23282 case RID_PROTECTED:
23283 case RID_PRIVATE:
23284 /* Consume the access-specifier. */
23285 cp_lexer_consume_token (parser->lexer);
23286 /* Remember which access-specifier is active. */
23287 current_access_specifier = token->u.value;
23288 /* Look for the `:'. */
23289 cp_parser_require (parser, CPP_COLON, RT_COLON);
23290 break;
23292 default:
23293 /* Accept #pragmas at class scope. */
23294 if (token->type == CPP_PRAGMA)
23296 cp_parser_pragma (parser, pragma_member, NULL);
23297 break;
23300 /* Otherwise, the next construction must be a
23301 member-declaration. */
23302 cp_parser_member_declaration (parser);
23307 /* Parse a member-declaration.
23309 member-declaration:
23310 decl-specifier-seq [opt] member-declarator-list [opt] ;
23311 function-definition ; [opt]
23312 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23313 using-declaration
23314 template-declaration
23315 alias-declaration
23317 member-declarator-list:
23318 member-declarator
23319 member-declarator-list , member-declarator
23321 member-declarator:
23322 declarator pure-specifier [opt]
23323 declarator constant-initializer [opt]
23324 identifier [opt] : constant-expression
23326 GNU Extensions:
23328 member-declaration:
23329 __extension__ member-declaration
23331 member-declarator:
23332 declarator attributes [opt] pure-specifier [opt]
23333 declarator attributes [opt] constant-initializer [opt]
23334 identifier [opt] attributes [opt] : constant-expression
23336 C++0x Extensions:
23338 member-declaration:
23339 static_assert-declaration */
23341 static void
23342 cp_parser_member_declaration (cp_parser* parser)
23344 cp_decl_specifier_seq decl_specifiers;
23345 tree prefix_attributes;
23346 tree decl;
23347 int declares_class_or_enum;
23348 bool friend_p;
23349 cp_token *token = NULL;
23350 cp_token *decl_spec_token_start = NULL;
23351 cp_token *initializer_token_start = NULL;
23352 int saved_pedantic;
23353 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23355 /* Check for the `__extension__' keyword. */
23356 if (cp_parser_extension_opt (parser, &saved_pedantic))
23358 /* Recurse. */
23359 cp_parser_member_declaration (parser);
23360 /* Restore the old value of the PEDANTIC flag. */
23361 pedantic = saved_pedantic;
23363 return;
23366 /* Check for a template-declaration. */
23367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23369 /* An explicit specialization here is an error condition, and we
23370 expect the specialization handler to detect and report this. */
23371 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23372 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23373 cp_parser_explicit_specialization (parser);
23374 else
23375 cp_parser_template_declaration (parser, /*member_p=*/true);
23377 return;
23379 /* Check for a template introduction. */
23380 else if (cp_parser_template_declaration_after_export (parser, true))
23381 return;
23383 /* Check for a using-declaration. */
23384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23386 if (cxx_dialect < cxx11)
23388 /* Parse the using-declaration. */
23389 cp_parser_using_declaration (parser,
23390 /*access_declaration_p=*/false);
23391 return;
23393 else
23395 tree decl;
23396 bool alias_decl_expected;
23397 cp_parser_parse_tentatively (parser);
23398 decl = cp_parser_alias_declaration (parser);
23399 /* Note that if we actually see the '=' token after the
23400 identifier, cp_parser_alias_declaration commits the
23401 tentative parse. In that case, we really expect an
23402 alias-declaration. Otherwise, we expect a using
23403 declaration. */
23404 alias_decl_expected =
23405 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23406 cp_parser_parse_definitely (parser);
23408 if (alias_decl_expected)
23409 finish_member_declaration (decl);
23410 else
23411 cp_parser_using_declaration (parser,
23412 /*access_declaration_p=*/false);
23413 return;
23417 /* Check for @defs. */
23418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23420 tree ivar, member;
23421 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23422 ivar = ivar_chains;
23423 while (ivar)
23425 member = ivar;
23426 ivar = TREE_CHAIN (member);
23427 TREE_CHAIN (member) = NULL_TREE;
23428 finish_member_declaration (member);
23430 return;
23433 /* If the next token is `static_assert' we have a static assertion. */
23434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23436 cp_parser_static_assert (parser, /*member_p=*/true);
23437 return;
23440 parser->colon_corrects_to_scope_p = false;
23442 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23443 goto out;
23445 /* Parse the decl-specifier-seq. */
23446 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23447 cp_parser_decl_specifier_seq (parser,
23448 CP_PARSER_FLAGS_OPTIONAL,
23449 &decl_specifiers,
23450 &declares_class_or_enum);
23451 /* Check for an invalid type-name. */
23452 if (!decl_specifiers.any_type_specifiers_p
23453 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23454 goto out;
23455 /* If there is no declarator, then the decl-specifier-seq should
23456 specify a type. */
23457 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23459 /* If there was no decl-specifier-seq, and the next token is a
23460 `;', then we have something like:
23462 struct S { ; };
23464 [class.mem]
23466 Each member-declaration shall declare at least one member
23467 name of the class. */
23468 if (!decl_specifiers.any_specifiers_p)
23470 cp_token *token = cp_lexer_peek_token (parser->lexer);
23471 if (!in_system_header_at (token->location))
23473 gcc_rich_location richloc (token->location);
23474 richloc.add_fixit_remove ();
23475 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23478 else
23480 tree type;
23482 /* See if this declaration is a friend. */
23483 friend_p = cp_parser_friend_p (&decl_specifiers);
23484 /* If there were decl-specifiers, check to see if there was
23485 a class-declaration. */
23486 type = check_tag_decl (&decl_specifiers,
23487 /*explicit_type_instantiation_p=*/false);
23488 /* Nested classes have already been added to the class, but
23489 a `friend' needs to be explicitly registered. */
23490 if (friend_p)
23492 /* If the `friend' keyword was present, the friend must
23493 be introduced with a class-key. */
23494 if (!declares_class_or_enum && cxx_dialect < cxx11)
23495 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23496 "in C++03 a class-key must be used "
23497 "when declaring a friend");
23498 /* In this case:
23500 template <typename T> struct A {
23501 friend struct A<T>::B;
23504 A<T>::B will be represented by a TYPENAME_TYPE, and
23505 therefore not recognized by check_tag_decl. */
23506 if (!type)
23508 type = decl_specifiers.type;
23509 if (type && TREE_CODE (type) == TYPE_DECL)
23510 type = TREE_TYPE (type);
23512 if (!type || !TYPE_P (type))
23513 error_at (decl_spec_token_start->location,
23514 "friend declaration does not name a class or "
23515 "function");
23516 else
23517 make_friend_class (current_class_type, type,
23518 /*complain=*/true);
23520 /* If there is no TYPE, an error message will already have
23521 been issued. */
23522 else if (!type || type == error_mark_node)
23524 /* An anonymous aggregate has to be handled specially; such
23525 a declaration really declares a data member (with a
23526 particular type), as opposed to a nested class. */
23527 else if (ANON_AGGR_TYPE_P (type))
23529 /* C++11 9.5/6. */
23530 if (decl_specifiers.storage_class != sc_none)
23531 error_at (decl_spec_token_start->location,
23532 "a storage class on an anonymous aggregate "
23533 "in class scope is not allowed");
23535 /* Remove constructors and such from TYPE, now that we
23536 know it is an anonymous aggregate. */
23537 fixup_anonymous_aggr (type);
23538 /* And make the corresponding data member. */
23539 decl = build_decl (decl_spec_token_start->location,
23540 FIELD_DECL, NULL_TREE, type);
23541 /* Add it to the class. */
23542 finish_member_declaration (decl);
23544 else
23545 cp_parser_check_access_in_redeclaration
23546 (TYPE_NAME (type),
23547 decl_spec_token_start->location);
23550 else
23552 bool assume_semicolon = false;
23554 /* Clear attributes from the decl_specifiers but keep them
23555 around as prefix attributes that apply them to the entity
23556 being declared. */
23557 prefix_attributes = decl_specifiers.attributes;
23558 decl_specifiers.attributes = NULL_TREE;
23560 /* See if these declarations will be friends. */
23561 friend_p = cp_parser_friend_p (&decl_specifiers);
23563 /* Keep going until we hit the `;' at the end of the
23564 declaration. */
23565 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23567 tree attributes = NULL_TREE;
23568 tree first_attribute;
23569 tree initializer;
23570 bool is_bitfld = false;
23571 bool named_bitfld = false;
23573 /* Peek at the next token. */
23574 token = cp_lexer_peek_token (parser->lexer);
23576 /* The following code wants to know early if it is a bit-field
23577 or some other declaration. Attributes can appear before
23578 the `:' token, but are hopefully rare enough that the
23579 simplicity of the tentative lookup pays off. */
23580 if (cp_next_tokens_can_be_attribute_p (parser)
23581 || (token->type == CPP_NAME
23582 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23583 && (named_bitfld = true)))
23585 cp_parser_parse_tentatively (parser);
23586 if (named_bitfld)
23587 cp_lexer_consume_token (parser->lexer);
23588 cp_parser_attributes_opt (parser);
23589 token = cp_lexer_peek_token (parser->lexer);
23590 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23591 cp_parser_abort_tentative_parse (parser);
23594 /* Check for a bitfield declaration. */
23595 if (is_bitfld
23596 || token->type == CPP_COLON
23597 || (token->type == CPP_NAME
23598 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23599 && (named_bitfld = true)))
23601 tree identifier;
23602 tree width;
23603 tree late_attributes = NULL_TREE;
23605 if (named_bitfld)
23606 identifier = cp_parser_identifier (parser);
23607 else
23608 identifier = NULL_TREE;
23610 /* Look for attributes that apply to the bitfield. */
23611 attributes = cp_parser_attributes_opt (parser);
23613 /* Consume the `:' token. */
23614 cp_lexer_consume_token (parser->lexer);
23616 /* Get the width of the bitfield. */
23617 width = cp_parser_constant_expression (parser, false, NULL,
23618 cxx_dialect >= cxx11);
23620 /* In C++2A and as extension for C++11 and above we allow
23621 default member initializers for bit-fields. */
23622 initializer = NULL_TREE;
23623 if (cxx_dialect >= cxx11
23624 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23625 || cp_lexer_next_token_is (parser->lexer,
23626 CPP_OPEN_BRACE)))
23628 location_t loc
23629 = cp_lexer_peek_token (parser->lexer)->location;
23630 if (cxx_dialect < cxx2a
23631 && !in_system_header_at (loc)
23632 && identifier != NULL_TREE)
23633 pedwarn (loc, 0,
23634 "default member initializers for bit-fields "
23635 "only available with -std=c++2a or "
23636 "-std=gnu++2a");
23638 initializer = cp_parser_save_nsdmi (parser);
23639 if (identifier == NULL_TREE)
23641 error_at (loc, "default member initializer for "
23642 "unnamed bit-field");
23643 initializer = NULL_TREE;
23646 else
23648 /* Look for attributes that apply to the bitfield after
23649 the `:' token and width. This is where GCC used to
23650 parse attributes in the past, pedwarn if there is
23651 a std attribute. */
23652 if (cp_next_tokens_can_be_std_attribute_p (parser))
23653 pedwarn (input_location, OPT_Wpedantic,
23654 "ISO C++ allows bit-field attributes only "
23655 "before the %<:%> token");
23657 late_attributes = cp_parser_attributes_opt (parser);
23660 attributes = chainon (attributes, late_attributes);
23662 /* Remember which attributes are prefix attributes and
23663 which are not. */
23664 first_attribute = attributes;
23665 /* Combine the attributes. */
23666 attributes = chainon (prefix_attributes, attributes);
23668 /* Create the bitfield declaration. */
23669 decl = grokbitfield (identifier
23670 ? make_id_declarator (NULL_TREE,
23671 identifier,
23672 sfk_none)
23673 : NULL,
23674 &decl_specifiers,
23675 width, initializer,
23676 attributes);
23678 else
23680 cp_declarator *declarator;
23681 tree asm_specification;
23682 int ctor_dtor_or_conv_p;
23684 /* Parse the declarator. */
23685 declarator
23686 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23687 &ctor_dtor_or_conv_p,
23688 /*parenthesized_p=*/NULL,
23689 /*member_p=*/true,
23690 friend_p);
23692 /* If something went wrong parsing the declarator, make sure
23693 that we at least consume some tokens. */
23694 if (declarator == cp_error_declarator)
23696 /* Skip to the end of the statement. */
23697 cp_parser_skip_to_end_of_statement (parser);
23698 /* If the next token is not a semicolon, that is
23699 probably because we just skipped over the body of
23700 a function. So, we consume a semicolon if
23701 present, but do not issue an error message if it
23702 is not present. */
23703 if (cp_lexer_next_token_is (parser->lexer,
23704 CPP_SEMICOLON))
23705 cp_lexer_consume_token (parser->lexer);
23706 goto out;
23709 if (declares_class_or_enum & 2)
23710 cp_parser_check_for_definition_in_return_type
23711 (declarator, decl_specifiers.type,
23712 decl_specifiers.locations[ds_type_spec]);
23714 /* Look for an asm-specification. */
23715 asm_specification = cp_parser_asm_specification_opt (parser);
23716 /* Look for attributes that apply to the declaration. */
23717 attributes = cp_parser_attributes_opt (parser);
23718 /* Remember which attributes are prefix attributes and
23719 which are not. */
23720 first_attribute = attributes;
23721 /* Combine the attributes. */
23722 attributes = chainon (prefix_attributes, attributes);
23724 /* If it's an `=', then we have a constant-initializer or a
23725 pure-specifier. It is not correct to parse the
23726 initializer before registering the member declaration
23727 since the member declaration should be in scope while
23728 its initializer is processed. However, the rest of the
23729 front end does not yet provide an interface that allows
23730 us to handle this correctly. */
23731 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23733 /* In [class.mem]:
23735 A pure-specifier shall be used only in the declaration of
23736 a virtual function.
23738 A member-declarator can contain a constant-initializer
23739 only if it declares a static member of integral or
23740 enumeration type.
23742 Therefore, if the DECLARATOR is for a function, we look
23743 for a pure-specifier; otherwise, we look for a
23744 constant-initializer. When we call `grokfield', it will
23745 perform more stringent semantics checks. */
23746 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23747 if (function_declarator_p (declarator)
23748 || (decl_specifiers.type
23749 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23750 && declarator->kind == cdk_id
23751 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23752 == FUNCTION_TYPE)))
23753 initializer = cp_parser_pure_specifier (parser);
23754 else if (decl_specifiers.storage_class != sc_static)
23755 initializer = cp_parser_save_nsdmi (parser);
23756 else if (cxx_dialect >= cxx11)
23758 bool nonconst;
23759 /* Don't require a constant rvalue in C++11, since we
23760 might want a reference constant. We'll enforce
23761 constancy later. */
23762 cp_lexer_consume_token (parser->lexer);
23763 /* Parse the initializer. */
23764 initializer = cp_parser_initializer_clause (parser,
23765 &nonconst);
23767 else
23768 /* Parse the initializer. */
23769 initializer = cp_parser_constant_initializer (parser);
23771 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23772 && !function_declarator_p (declarator))
23774 bool x;
23775 if (decl_specifiers.storage_class != sc_static)
23776 initializer = cp_parser_save_nsdmi (parser);
23777 else
23778 initializer = cp_parser_initializer (parser, &x, &x);
23780 /* Otherwise, there is no initializer. */
23781 else
23782 initializer = NULL_TREE;
23784 /* See if we are probably looking at a function
23785 definition. We are certainly not looking at a
23786 member-declarator. Calling `grokfield' has
23787 side-effects, so we must not do it unless we are sure
23788 that we are looking at a member-declarator. */
23789 if (cp_parser_token_starts_function_definition_p
23790 (cp_lexer_peek_token (parser->lexer)))
23792 /* The grammar does not allow a pure-specifier to be
23793 used when a member function is defined. (It is
23794 possible that this fact is an oversight in the
23795 standard, since a pure function may be defined
23796 outside of the class-specifier. */
23797 if (initializer && initializer_token_start)
23798 error_at (initializer_token_start->location,
23799 "pure-specifier on function-definition");
23800 decl = cp_parser_save_member_function_body (parser,
23801 &decl_specifiers,
23802 declarator,
23803 attributes);
23804 if (parser->fully_implicit_function_template_p)
23805 decl = finish_fully_implicit_template (parser, decl);
23806 /* If the member was not a friend, declare it here. */
23807 if (!friend_p)
23808 finish_member_declaration (decl);
23809 /* Peek at the next token. */
23810 token = cp_lexer_peek_token (parser->lexer);
23811 /* If the next token is a semicolon, consume it. */
23812 if (token->type == CPP_SEMICOLON)
23814 location_t semicolon_loc
23815 = cp_lexer_consume_token (parser->lexer)->location;
23816 gcc_rich_location richloc (semicolon_loc);
23817 richloc.add_fixit_remove ();
23818 warning_at (&richloc, OPT_Wextra_semi,
23819 "extra %<;%> after in-class "
23820 "function definition");
23822 goto out;
23824 else
23825 if (declarator->kind == cdk_function)
23826 declarator->id_loc = token->location;
23827 /* Create the declaration. */
23828 decl = grokfield (declarator, &decl_specifiers,
23829 initializer, /*init_const_expr_p=*/true,
23830 asm_specification, attributes);
23831 if (parser->fully_implicit_function_template_p)
23833 if (friend_p)
23834 finish_fully_implicit_template (parser, 0);
23835 else
23836 decl = finish_fully_implicit_template (parser, decl);
23840 cp_finalize_omp_declare_simd (parser, decl);
23841 cp_finalize_oacc_routine (parser, decl, false);
23843 /* Reset PREFIX_ATTRIBUTES. */
23844 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23845 attributes = TREE_CHAIN (attributes);
23846 if (attributes)
23847 TREE_CHAIN (attributes) = NULL_TREE;
23849 /* If there is any qualification still in effect, clear it
23850 now; we will be starting fresh with the next declarator. */
23851 parser->scope = NULL_TREE;
23852 parser->qualifying_scope = NULL_TREE;
23853 parser->object_scope = NULL_TREE;
23854 /* If it's a `,', then there are more declarators. */
23855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23857 cp_lexer_consume_token (parser->lexer);
23858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23860 cp_token *token = cp_lexer_previous_token (parser->lexer);
23861 gcc_rich_location richloc (token->location);
23862 richloc.add_fixit_remove ();
23863 error_at (&richloc, "stray %<,%> at end of "
23864 "member declaration");
23867 /* If the next token isn't a `;', then we have a parse error. */
23868 else if (cp_lexer_next_token_is_not (parser->lexer,
23869 CPP_SEMICOLON))
23871 /* The next token might be a ways away from where the
23872 actual semicolon is missing. Find the previous token
23873 and use that for our error position. */
23874 cp_token *token = cp_lexer_previous_token (parser->lexer);
23875 gcc_rich_location richloc (token->location);
23876 richloc.add_fixit_insert_after (";");
23877 error_at (&richloc, "expected %<;%> at end of "
23878 "member declaration");
23880 /* Assume that the user meant to provide a semicolon. If
23881 we were to cp_parser_skip_to_end_of_statement, we might
23882 skip to a semicolon inside a member function definition
23883 and issue nonsensical error messages. */
23884 assume_semicolon = true;
23887 if (decl)
23889 /* Add DECL to the list of members. */
23890 if (!friend_p
23891 /* Explicitly include, eg, NSDMIs, for better error
23892 recovery (c++/58650). */
23893 || !DECL_DECLARES_FUNCTION_P (decl))
23894 finish_member_declaration (decl);
23896 if (TREE_CODE (decl) == FUNCTION_DECL)
23897 cp_parser_save_default_args (parser, decl);
23898 else if (TREE_CODE (decl) == FIELD_DECL
23899 && DECL_INITIAL (decl))
23900 /* Add DECL to the queue of NSDMI to be parsed later. */
23901 vec_safe_push (unparsed_nsdmis, decl);
23904 if (assume_semicolon)
23905 goto out;
23909 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23910 out:
23911 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23914 /* Parse a pure-specifier.
23916 pure-specifier:
23919 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23920 Otherwise, ERROR_MARK_NODE is returned. */
23922 static tree
23923 cp_parser_pure_specifier (cp_parser* parser)
23925 cp_token *token;
23927 /* Look for the `=' token. */
23928 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23929 return error_mark_node;
23930 /* Look for the `0' token. */
23931 token = cp_lexer_peek_token (parser->lexer);
23933 if (token->type == CPP_EOF
23934 || token->type == CPP_PRAGMA_EOL)
23935 return error_mark_node;
23937 cp_lexer_consume_token (parser->lexer);
23939 /* Accept = default or = delete in c++0x mode. */
23940 if (token->keyword == RID_DEFAULT
23941 || token->keyword == RID_DELETE)
23943 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23944 return token->u.value;
23947 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23948 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23950 cp_parser_error (parser,
23951 "invalid pure specifier (only %<= 0%> is allowed)");
23952 cp_parser_skip_to_end_of_statement (parser);
23953 return error_mark_node;
23955 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23957 error_at (token->location, "templates may not be %<virtual%>");
23958 return error_mark_node;
23961 return integer_zero_node;
23964 /* Parse a constant-initializer.
23966 constant-initializer:
23967 = constant-expression
23969 Returns a representation of the constant-expression. */
23971 static tree
23972 cp_parser_constant_initializer (cp_parser* parser)
23974 /* Look for the `=' token. */
23975 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23976 return error_mark_node;
23978 /* It is invalid to write:
23980 struct S { static const int i = { 7 }; };
23983 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23985 cp_parser_error (parser,
23986 "a brace-enclosed initializer is not allowed here");
23987 /* Consume the opening brace. */
23988 matching_braces braces;
23989 braces.consume_open (parser);
23990 /* Skip the initializer. */
23991 cp_parser_skip_to_closing_brace (parser);
23992 /* Look for the trailing `}'. */
23993 braces.require_close (parser);
23995 return error_mark_node;
23998 return cp_parser_constant_expression (parser);
24001 /* Derived classes [gram.class.derived] */
24003 /* Parse a base-clause.
24005 base-clause:
24006 : base-specifier-list
24008 base-specifier-list:
24009 base-specifier ... [opt]
24010 base-specifier-list , base-specifier ... [opt]
24012 Returns a TREE_LIST representing the base-classes, in the order in
24013 which they were declared. The representation of each node is as
24014 described by cp_parser_base_specifier.
24016 In the case that no bases are specified, this function will return
24017 NULL_TREE, not ERROR_MARK_NODE. */
24019 static tree
24020 cp_parser_base_clause (cp_parser* parser)
24022 tree bases = NULL_TREE;
24024 /* Look for the `:' that begins the list. */
24025 cp_parser_require (parser, CPP_COLON, RT_COLON);
24027 /* Scan the base-specifier-list. */
24028 while (true)
24030 cp_token *token;
24031 tree base;
24032 bool pack_expansion_p = false;
24034 /* Look for the base-specifier. */
24035 base = cp_parser_base_specifier (parser);
24036 /* Look for the (optional) ellipsis. */
24037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24039 /* Consume the `...'. */
24040 cp_lexer_consume_token (parser->lexer);
24042 pack_expansion_p = true;
24045 /* Add BASE to the front of the list. */
24046 if (base && base != error_mark_node)
24048 if (pack_expansion_p)
24049 /* Make this a pack expansion type. */
24050 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24052 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24054 TREE_CHAIN (base) = bases;
24055 bases = base;
24058 /* Peek at the next token. */
24059 token = cp_lexer_peek_token (parser->lexer);
24060 /* If it's not a comma, then the list is complete. */
24061 if (token->type != CPP_COMMA)
24062 break;
24063 /* Consume the `,'. */
24064 cp_lexer_consume_token (parser->lexer);
24067 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24068 base class had a qualified name. However, the next name that
24069 appears is certainly not qualified. */
24070 parser->scope = NULL_TREE;
24071 parser->qualifying_scope = NULL_TREE;
24072 parser->object_scope = NULL_TREE;
24074 return nreverse (bases);
24077 /* Parse a base-specifier.
24079 base-specifier:
24080 :: [opt] nested-name-specifier [opt] class-name
24081 virtual access-specifier [opt] :: [opt] nested-name-specifier
24082 [opt] class-name
24083 access-specifier virtual [opt] :: [opt] nested-name-specifier
24084 [opt] class-name
24086 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24087 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24088 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24089 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24091 static tree
24092 cp_parser_base_specifier (cp_parser* parser)
24094 cp_token *token;
24095 bool done = false;
24096 bool virtual_p = false;
24097 bool duplicate_virtual_error_issued_p = false;
24098 bool duplicate_access_error_issued_p = false;
24099 bool class_scope_p, template_p;
24100 tree access = access_default_node;
24101 tree type;
24103 /* Process the optional `virtual' and `access-specifier'. */
24104 while (!done)
24106 /* Peek at the next token. */
24107 token = cp_lexer_peek_token (parser->lexer);
24108 /* Process `virtual'. */
24109 switch (token->keyword)
24111 case RID_VIRTUAL:
24112 /* If `virtual' appears more than once, issue an error. */
24113 if (virtual_p && !duplicate_virtual_error_issued_p)
24115 cp_parser_error (parser,
24116 "%<virtual%> specified more than once in base-specifier");
24117 duplicate_virtual_error_issued_p = true;
24120 virtual_p = true;
24122 /* Consume the `virtual' token. */
24123 cp_lexer_consume_token (parser->lexer);
24125 break;
24127 case RID_PUBLIC:
24128 case RID_PROTECTED:
24129 case RID_PRIVATE:
24130 /* If more than one access specifier appears, issue an
24131 error. */
24132 if (access != access_default_node
24133 && !duplicate_access_error_issued_p)
24135 cp_parser_error (parser,
24136 "more than one access specifier in base-specifier");
24137 duplicate_access_error_issued_p = true;
24140 access = ridpointers[(int) token->keyword];
24142 /* Consume the access-specifier. */
24143 cp_lexer_consume_token (parser->lexer);
24145 break;
24147 default:
24148 done = true;
24149 break;
24152 /* It is not uncommon to see programs mechanically, erroneously, use
24153 the 'typename' keyword to denote (dependent) qualified types
24154 as base classes. */
24155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24157 token = cp_lexer_peek_token (parser->lexer);
24158 if (!processing_template_decl)
24159 error_at (token->location,
24160 "keyword %<typename%> not allowed outside of templates");
24161 else
24162 error_at (token->location,
24163 "keyword %<typename%> not allowed in this context "
24164 "(the base class is implicitly a type)");
24165 cp_lexer_consume_token (parser->lexer);
24168 /* Look for the optional `::' operator. */
24169 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24170 /* Look for the nested-name-specifier. The simplest way to
24171 implement:
24173 [temp.res]
24175 The keyword `typename' is not permitted in a base-specifier or
24176 mem-initializer; in these contexts a qualified name that
24177 depends on a template-parameter is implicitly assumed to be a
24178 type name.
24180 is to pretend that we have seen the `typename' keyword at this
24181 point. */
24182 cp_parser_nested_name_specifier_opt (parser,
24183 /*typename_keyword_p=*/true,
24184 /*check_dependency_p=*/true,
24185 /*type_p=*/true,
24186 /*is_declaration=*/true);
24187 /* If the base class is given by a qualified name, assume that names
24188 we see are type names or templates, as appropriate. */
24189 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24190 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24192 if (!parser->scope
24193 && cp_lexer_next_token_is_decltype (parser->lexer))
24194 /* DR 950 allows decltype as a base-specifier. */
24195 type = cp_parser_decltype (parser);
24196 else
24198 /* Otherwise, look for the class-name. */
24199 type = cp_parser_class_name (parser,
24200 class_scope_p,
24201 template_p,
24202 typename_type,
24203 /*check_dependency_p=*/true,
24204 /*class_head_p=*/false,
24205 /*is_declaration=*/true);
24206 type = TREE_TYPE (type);
24209 if (type == error_mark_node)
24210 return error_mark_node;
24212 return finish_base_specifier (type, access, virtual_p);
24215 /* Exception handling [gram.exception] */
24217 /* Parse an (optional) noexcept-specification.
24219 noexcept-specification:
24220 noexcept ( constant-expression ) [opt]
24222 If no noexcept-specification is present, returns NULL_TREE.
24223 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24224 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24225 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24226 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24227 in which case a boolean condition is returned instead. */
24229 static tree
24230 cp_parser_noexcept_specification_opt (cp_parser* parser,
24231 bool require_constexpr,
24232 bool* consumed_expr,
24233 bool return_cond)
24235 cp_token *token;
24236 const char *saved_message;
24238 /* Peek at the next token. */
24239 token = cp_lexer_peek_token (parser->lexer);
24241 /* Is it a noexcept-specification? */
24242 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24244 tree expr;
24245 cp_lexer_consume_token (parser->lexer);
24247 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24249 matching_parens parens;
24250 parens.consume_open (parser);
24252 if (require_constexpr)
24254 /* Types may not be defined in an exception-specification. */
24255 saved_message = parser->type_definition_forbidden_message;
24256 parser->type_definition_forbidden_message
24257 = G_("types may not be defined in an exception-specification");
24259 expr = cp_parser_constant_expression (parser);
24261 /* Restore the saved message. */
24262 parser->type_definition_forbidden_message = saved_message;
24264 else
24266 expr = cp_parser_expression (parser);
24267 *consumed_expr = true;
24270 parens.require_close (parser);
24272 else
24274 expr = boolean_true_node;
24275 if (!require_constexpr)
24276 *consumed_expr = false;
24279 /* We cannot build a noexcept-spec right away because this will check
24280 that expr is a constexpr. */
24281 if (!return_cond)
24282 return build_noexcept_spec (expr, tf_warning_or_error);
24283 else
24284 return expr;
24286 else
24287 return NULL_TREE;
24290 /* Parse an (optional) exception-specification.
24292 exception-specification:
24293 throw ( type-id-list [opt] )
24295 Returns a TREE_LIST representing the exception-specification. The
24296 TREE_VALUE of each node is a type. */
24298 static tree
24299 cp_parser_exception_specification_opt (cp_parser* parser)
24301 cp_token *token;
24302 tree type_id_list;
24303 const char *saved_message;
24305 /* Peek at the next token. */
24306 token = cp_lexer_peek_token (parser->lexer);
24308 /* Is it a noexcept-specification? */
24309 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24310 false);
24311 if (type_id_list != NULL_TREE)
24312 return type_id_list;
24314 /* If it's not `throw', then there's no exception-specification. */
24315 if (!cp_parser_is_keyword (token, RID_THROW))
24316 return NULL_TREE;
24318 location_t loc = token->location;
24320 /* Consume the `throw'. */
24321 cp_lexer_consume_token (parser->lexer);
24323 /* Look for the `('. */
24324 matching_parens parens;
24325 parens.require_open (parser);
24327 /* Peek at the next token. */
24328 token = cp_lexer_peek_token (parser->lexer);
24329 /* If it's not a `)', then there is a type-id-list. */
24330 if (token->type != CPP_CLOSE_PAREN)
24332 /* Types may not be defined in an exception-specification. */
24333 saved_message = parser->type_definition_forbidden_message;
24334 parser->type_definition_forbidden_message
24335 = G_("types may not be defined in an exception-specification");
24336 /* Parse the type-id-list. */
24337 type_id_list = cp_parser_type_id_list (parser);
24338 /* Restore the saved message. */
24339 parser->type_definition_forbidden_message = saved_message;
24341 if (cxx_dialect >= cxx17)
24343 error_at (loc, "ISO C++17 does not allow dynamic exception "
24344 "specifications");
24345 type_id_list = NULL_TREE;
24347 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24348 warning_at (loc, OPT_Wdeprecated,
24349 "dynamic exception specifications are deprecated in "
24350 "C++11");
24352 /* In C++17, throw() is equivalent to noexcept (true). throw()
24353 is deprecated in C++11 and above as well, but is still widely used,
24354 so don't warn about it yet. */
24355 else if (cxx_dialect >= cxx17)
24356 type_id_list = noexcept_true_spec;
24357 else
24358 type_id_list = empty_except_spec;
24360 /* Look for the `)'. */
24361 parens.require_close (parser);
24363 return type_id_list;
24366 /* Parse an (optional) type-id-list.
24368 type-id-list:
24369 type-id ... [opt]
24370 type-id-list , type-id ... [opt]
24372 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24373 in the order that the types were presented. */
24375 static tree
24376 cp_parser_type_id_list (cp_parser* parser)
24378 tree types = NULL_TREE;
24380 while (true)
24382 cp_token *token;
24383 tree type;
24385 token = cp_lexer_peek_token (parser->lexer);
24387 /* Get the next type-id. */
24388 type = cp_parser_type_id (parser);
24389 /* Check for invalid 'auto'. */
24390 if (flag_concepts && type_uses_auto (type))
24392 error_at (token->location,
24393 "invalid use of %<auto%> in exception-specification");
24394 type = error_mark_node;
24396 /* Parse the optional ellipsis. */
24397 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24399 /* Consume the `...'. */
24400 cp_lexer_consume_token (parser->lexer);
24402 /* Turn the type into a pack expansion expression. */
24403 type = make_pack_expansion (type);
24405 /* Add it to the list. */
24406 types = add_exception_specifier (types, type, /*complain=*/1);
24407 /* Peek at the next token. */
24408 token = cp_lexer_peek_token (parser->lexer);
24409 /* If it is not a `,', we are done. */
24410 if (token->type != CPP_COMMA)
24411 break;
24412 /* Consume the `,'. */
24413 cp_lexer_consume_token (parser->lexer);
24416 return nreverse (types);
24419 /* Parse a try-block.
24421 try-block:
24422 try compound-statement handler-seq */
24424 static tree
24425 cp_parser_try_block (cp_parser* parser)
24427 tree try_block;
24429 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24430 if (parser->in_function_body
24431 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24432 error ("%<try%> in %<constexpr%> function");
24434 try_block = begin_try_block ();
24435 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24436 finish_try_block (try_block);
24437 cp_parser_handler_seq (parser);
24438 finish_handler_sequence (try_block);
24440 return try_block;
24443 /* Parse a function-try-block.
24445 function-try-block:
24446 try ctor-initializer [opt] function-body handler-seq */
24448 static void
24449 cp_parser_function_try_block (cp_parser* parser)
24451 tree compound_stmt;
24452 tree try_block;
24454 /* Look for the `try' keyword. */
24455 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24456 return;
24457 /* Let the rest of the front end know where we are. */
24458 try_block = begin_function_try_block (&compound_stmt);
24459 /* Parse the function-body. */
24460 cp_parser_ctor_initializer_opt_and_function_body
24461 (parser, /*in_function_try_block=*/true);
24462 /* We're done with the `try' part. */
24463 finish_function_try_block (try_block);
24464 /* Parse the handlers. */
24465 cp_parser_handler_seq (parser);
24466 /* We're done with the handlers. */
24467 finish_function_handler_sequence (try_block, compound_stmt);
24470 /* Parse a handler-seq.
24472 handler-seq:
24473 handler handler-seq [opt] */
24475 static void
24476 cp_parser_handler_seq (cp_parser* parser)
24478 while (true)
24480 cp_token *token;
24482 /* Parse the handler. */
24483 cp_parser_handler (parser);
24484 /* Peek at the next token. */
24485 token = cp_lexer_peek_token (parser->lexer);
24486 /* If it's not `catch' then there are no more handlers. */
24487 if (!cp_parser_is_keyword (token, RID_CATCH))
24488 break;
24492 /* Parse a handler.
24494 handler:
24495 catch ( exception-declaration ) compound-statement */
24497 static void
24498 cp_parser_handler (cp_parser* parser)
24500 tree handler;
24501 tree declaration;
24503 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24504 handler = begin_handler ();
24505 matching_parens parens;
24506 parens.require_open (parser);
24507 declaration = cp_parser_exception_declaration (parser);
24508 finish_handler_parms (declaration, handler);
24509 parens.require_close (parser);
24510 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24511 finish_handler (handler);
24514 /* Parse an exception-declaration.
24516 exception-declaration:
24517 type-specifier-seq declarator
24518 type-specifier-seq abstract-declarator
24519 type-specifier-seq
24522 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24523 ellipsis variant is used. */
24525 static tree
24526 cp_parser_exception_declaration (cp_parser* parser)
24528 cp_decl_specifier_seq type_specifiers;
24529 cp_declarator *declarator;
24530 const char *saved_message;
24532 /* If it's an ellipsis, it's easy to handle. */
24533 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24535 /* Consume the `...' token. */
24536 cp_lexer_consume_token (parser->lexer);
24537 return NULL_TREE;
24540 /* Types may not be defined in exception-declarations. */
24541 saved_message = parser->type_definition_forbidden_message;
24542 parser->type_definition_forbidden_message
24543 = G_("types may not be defined in exception-declarations");
24545 /* Parse the type-specifier-seq. */
24546 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24547 /*is_trailing_return=*/false,
24548 &type_specifiers);
24549 /* If it's a `)', then there is no declarator. */
24550 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24551 declarator = NULL;
24552 else
24553 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24554 /*ctor_dtor_or_conv_p=*/NULL,
24555 /*parenthesized_p=*/NULL,
24556 /*member_p=*/false,
24557 /*friend_p=*/false);
24559 /* Restore the saved message. */
24560 parser->type_definition_forbidden_message = saved_message;
24562 if (!type_specifiers.any_specifiers_p)
24563 return error_mark_node;
24565 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24568 /* Parse a throw-expression.
24570 throw-expression:
24571 throw assignment-expression [opt]
24573 Returns a THROW_EXPR representing the throw-expression. */
24575 static tree
24576 cp_parser_throw_expression (cp_parser* parser)
24578 tree expression;
24579 cp_token* token;
24581 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24582 token = cp_lexer_peek_token (parser->lexer);
24583 /* Figure out whether or not there is an assignment-expression
24584 following the "throw" keyword. */
24585 if (token->type == CPP_COMMA
24586 || token->type == CPP_SEMICOLON
24587 || token->type == CPP_CLOSE_PAREN
24588 || token->type == CPP_CLOSE_SQUARE
24589 || token->type == CPP_CLOSE_BRACE
24590 || token->type == CPP_COLON)
24591 expression = NULL_TREE;
24592 else
24593 expression = cp_parser_assignment_expression (parser);
24595 return build_throw (expression);
24598 /* GNU Extensions */
24600 /* Parse an (optional) asm-specification.
24602 asm-specification:
24603 asm ( string-literal )
24605 If the asm-specification is present, returns a STRING_CST
24606 corresponding to the string-literal. Otherwise, returns
24607 NULL_TREE. */
24609 static tree
24610 cp_parser_asm_specification_opt (cp_parser* parser)
24612 cp_token *token;
24613 tree asm_specification;
24615 /* Peek at the next token. */
24616 token = cp_lexer_peek_token (parser->lexer);
24617 /* If the next token isn't the `asm' keyword, then there's no
24618 asm-specification. */
24619 if (!cp_parser_is_keyword (token, RID_ASM))
24620 return NULL_TREE;
24622 /* Consume the `asm' token. */
24623 cp_lexer_consume_token (parser->lexer);
24624 /* Look for the `('. */
24625 matching_parens parens;
24626 parens.require_open (parser);
24628 /* Look for the string-literal. */
24629 asm_specification = cp_parser_string_literal (parser, false, false);
24631 /* Look for the `)'. */
24632 parens.require_close (parser);
24634 return asm_specification;
24637 /* Parse an asm-operand-list.
24639 asm-operand-list:
24640 asm-operand
24641 asm-operand-list , asm-operand
24643 asm-operand:
24644 string-literal ( expression )
24645 [ string-literal ] string-literal ( expression )
24647 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24648 each node is the expression. The TREE_PURPOSE is itself a
24649 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24650 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24651 is a STRING_CST for the string literal before the parenthesis. Returns
24652 ERROR_MARK_NODE if any of the operands are invalid. */
24654 static tree
24655 cp_parser_asm_operand_list (cp_parser* parser)
24657 tree asm_operands = NULL_TREE;
24658 bool invalid_operands = false;
24660 while (true)
24662 tree string_literal;
24663 tree expression;
24664 tree name;
24666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24668 /* Consume the `[' token. */
24669 cp_lexer_consume_token (parser->lexer);
24670 /* Read the operand name. */
24671 name = cp_parser_identifier (parser);
24672 if (name != error_mark_node)
24673 name = build_string (IDENTIFIER_LENGTH (name),
24674 IDENTIFIER_POINTER (name));
24675 /* Look for the closing `]'. */
24676 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24678 else
24679 name = NULL_TREE;
24680 /* Look for the string-literal. */
24681 string_literal = cp_parser_string_literal (parser, false, false);
24683 /* Look for the `('. */
24684 matching_parens parens;
24685 parens.require_open (parser);
24686 /* Parse the expression. */
24687 expression = cp_parser_expression (parser);
24688 /* Look for the `)'. */
24689 parens.require_close (parser);
24691 if (name == error_mark_node
24692 || string_literal == error_mark_node
24693 || expression == error_mark_node)
24694 invalid_operands = true;
24696 /* Add this operand to the list. */
24697 asm_operands = tree_cons (build_tree_list (name, string_literal),
24698 expression,
24699 asm_operands);
24700 /* If the next token is not a `,', there are no more
24701 operands. */
24702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24703 break;
24704 /* Consume the `,'. */
24705 cp_lexer_consume_token (parser->lexer);
24708 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24711 /* Parse an asm-clobber-list.
24713 asm-clobber-list:
24714 string-literal
24715 asm-clobber-list , string-literal
24717 Returns a TREE_LIST, indicating the clobbers in the order that they
24718 appeared. The TREE_VALUE of each node is a STRING_CST. */
24720 static tree
24721 cp_parser_asm_clobber_list (cp_parser* parser)
24723 tree clobbers = NULL_TREE;
24725 while (true)
24727 tree string_literal;
24729 /* Look for the string literal. */
24730 string_literal = cp_parser_string_literal (parser, false, false);
24731 /* Add it to the list. */
24732 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24733 /* If the next token is not a `,', then the list is
24734 complete. */
24735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24736 break;
24737 /* Consume the `,' token. */
24738 cp_lexer_consume_token (parser->lexer);
24741 return clobbers;
24744 /* Parse an asm-label-list.
24746 asm-label-list:
24747 identifier
24748 asm-label-list , identifier
24750 Returns a TREE_LIST, indicating the labels in the order that they
24751 appeared. The TREE_VALUE of each node is a label. */
24753 static tree
24754 cp_parser_asm_label_list (cp_parser* parser)
24756 tree labels = NULL_TREE;
24758 while (true)
24760 tree identifier, label, name;
24762 /* Look for the identifier. */
24763 identifier = cp_parser_identifier (parser);
24764 if (!error_operand_p (identifier))
24766 label = lookup_label (identifier);
24767 if (TREE_CODE (label) == LABEL_DECL)
24769 TREE_USED (label) = 1;
24770 check_goto (label);
24771 name = build_string (IDENTIFIER_LENGTH (identifier),
24772 IDENTIFIER_POINTER (identifier));
24773 labels = tree_cons (name, label, labels);
24776 /* If the next token is not a `,', then the list is
24777 complete. */
24778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24779 break;
24780 /* Consume the `,' token. */
24781 cp_lexer_consume_token (parser->lexer);
24784 return nreverse (labels);
24787 /* Return TRUE iff the next tokens in the stream are possibly the
24788 beginning of a GNU extension attribute. */
24790 static bool
24791 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24793 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24796 /* Return TRUE iff the next tokens in the stream are possibly the
24797 beginning of a standard C++-11 attribute specifier. */
24799 static bool
24800 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24802 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24805 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24806 beginning of a standard C++-11 attribute specifier. */
24808 static bool
24809 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24811 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24813 return (cxx_dialect >= cxx11
24814 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24815 || (token->type == CPP_OPEN_SQUARE
24816 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24817 && token->type == CPP_OPEN_SQUARE)));
24820 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24821 beginning of a GNU extension attribute. */
24823 static bool
24824 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24826 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24828 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24831 /* Return true iff the next tokens can be the beginning of either a
24832 GNU attribute list, or a standard C++11 attribute sequence. */
24834 static bool
24835 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24837 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24838 || cp_next_tokens_can_be_std_attribute_p (parser));
24841 /* Return true iff the next Nth tokens can be the beginning of either
24842 a GNU attribute list, or a standard C++11 attribute sequence. */
24844 static bool
24845 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24847 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24848 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24851 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24852 of GNU attributes, or return NULL. */
24854 static tree
24855 cp_parser_attributes_opt (cp_parser *parser)
24857 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24858 return cp_parser_gnu_attributes_opt (parser);
24859 return cp_parser_std_attribute_spec_seq (parser);
24862 /* Parse an (optional) series of attributes.
24864 attributes:
24865 attributes attribute
24867 attribute:
24868 __attribute__ (( attribute-list [opt] ))
24870 The return value is as for cp_parser_gnu_attribute_list. */
24872 static tree
24873 cp_parser_gnu_attributes_opt (cp_parser* parser)
24875 tree attributes = NULL_TREE;
24877 while (true)
24879 cp_token *token;
24880 tree attribute_list;
24881 bool ok = true;
24883 /* Peek at the next token. */
24884 token = cp_lexer_peek_token (parser->lexer);
24885 /* If it's not `__attribute__', then we're done. */
24886 if (token->keyword != RID_ATTRIBUTE)
24887 break;
24889 /* Consume the `__attribute__' keyword. */
24890 cp_lexer_consume_token (parser->lexer);
24891 /* Look for the two `(' tokens. */
24892 matching_parens outer_parens;
24893 outer_parens.require_open (parser);
24894 matching_parens inner_parens;
24895 inner_parens.require_open (parser);
24897 /* Peek at the next token. */
24898 token = cp_lexer_peek_token (parser->lexer);
24899 if (token->type != CPP_CLOSE_PAREN)
24900 /* Parse the attribute-list. */
24901 attribute_list = cp_parser_gnu_attribute_list (parser);
24902 else
24903 /* If the next token is a `)', then there is no attribute
24904 list. */
24905 attribute_list = NULL;
24907 /* Look for the two `)' tokens. */
24908 if (!inner_parens.require_close (parser))
24909 ok = false;
24910 if (!outer_parens.require_close (parser))
24911 ok = false;
24912 if (!ok)
24913 cp_parser_skip_to_end_of_statement (parser);
24915 /* Add these new attributes to the list. */
24916 attributes = chainon (attributes, attribute_list);
24919 return attributes;
24922 /* Parse a GNU attribute-list.
24924 attribute-list:
24925 attribute
24926 attribute-list , attribute
24928 attribute:
24929 identifier
24930 identifier ( identifier )
24931 identifier ( identifier , expression-list )
24932 identifier ( expression-list )
24934 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24935 to an attribute. The TREE_PURPOSE of each node is the identifier
24936 indicating which attribute is in use. The TREE_VALUE represents
24937 the arguments, if any. */
24939 static tree
24940 cp_parser_gnu_attribute_list (cp_parser* parser)
24942 tree attribute_list = NULL_TREE;
24943 bool save_translate_strings_p = parser->translate_strings_p;
24945 parser->translate_strings_p = false;
24946 while (true)
24948 cp_token *token;
24949 tree identifier;
24950 tree attribute;
24952 /* Look for the identifier. We also allow keywords here; for
24953 example `__attribute__ ((const))' is legal. */
24954 token = cp_lexer_peek_token (parser->lexer);
24955 if (token->type == CPP_NAME
24956 || token->type == CPP_KEYWORD)
24958 tree arguments = NULL_TREE;
24960 /* Consume the token, but save it since we need it for the
24961 SIMD enabled function parsing. */
24962 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24964 /* Save away the identifier that indicates which attribute
24965 this is. */
24966 identifier = (token->type == CPP_KEYWORD)
24967 /* For keywords, use the canonical spelling, not the
24968 parsed identifier. */
24969 ? ridpointers[(int) token->keyword]
24970 : id_token->u.value;
24972 identifier = canonicalize_attr_name (identifier);
24973 attribute = build_tree_list (identifier, NULL_TREE);
24975 /* Peek at the next token. */
24976 token = cp_lexer_peek_token (parser->lexer);
24977 /* If it's an `(', then parse the attribute arguments. */
24978 if (token->type == CPP_OPEN_PAREN)
24980 vec<tree, va_gc> *vec;
24981 int attr_flag = (attribute_takes_identifier_p (identifier)
24982 ? id_attr : normal_attr);
24983 vec = cp_parser_parenthesized_expression_list
24984 (parser, attr_flag, /*cast_p=*/false,
24985 /*allow_expansion_p=*/false,
24986 /*non_constant_p=*/NULL);
24987 if (vec == NULL)
24988 arguments = error_mark_node;
24989 else
24991 arguments = build_tree_list_vec (vec);
24992 release_tree_vector (vec);
24994 /* Save the arguments away. */
24995 TREE_VALUE (attribute) = arguments;
24998 if (arguments != error_mark_node)
25000 /* Add this attribute to the list. */
25001 TREE_CHAIN (attribute) = attribute_list;
25002 attribute_list = attribute;
25005 token = cp_lexer_peek_token (parser->lexer);
25007 /* Now, look for more attributes. If the next token isn't a
25008 `,', we're done. */
25009 if (token->type != CPP_COMMA)
25010 break;
25012 /* Consume the comma and keep going. */
25013 cp_lexer_consume_token (parser->lexer);
25015 parser->translate_strings_p = save_translate_strings_p;
25017 /* We built up the list in reverse order. */
25018 return nreverse (attribute_list);
25021 /* Parse a standard C++11 attribute.
25023 The returned representation is a TREE_LIST which TREE_PURPOSE is
25024 the scoped name of the attribute, and the TREE_VALUE is its
25025 arguments list.
25027 Note that the scoped name of the attribute is itself a TREE_LIST
25028 which TREE_PURPOSE is the namespace of the attribute, and
25029 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25030 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25031 and which TREE_PURPOSE is directly the attribute name.
25033 Clients of the attribute code should use get_attribute_namespace
25034 and get_attribute_name to get the actual namespace and name of
25035 attributes, regardless of their being GNU or C++11 attributes.
25037 attribute:
25038 attribute-token attribute-argument-clause [opt]
25040 attribute-token:
25041 identifier
25042 attribute-scoped-token
25044 attribute-scoped-token:
25045 attribute-namespace :: identifier
25047 attribute-namespace:
25048 identifier
25050 attribute-argument-clause:
25051 ( balanced-token-seq )
25053 balanced-token-seq:
25054 balanced-token [opt]
25055 balanced-token-seq balanced-token
25057 balanced-token:
25058 ( balanced-token-seq )
25059 [ balanced-token-seq ]
25060 { balanced-token-seq }. */
25062 static tree
25063 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25065 tree attribute, attr_id = NULL_TREE, arguments;
25066 cp_token *token;
25068 /* First, parse name of the attribute, a.k.a attribute-token. */
25070 token = cp_lexer_peek_token (parser->lexer);
25071 if (token->type == CPP_NAME)
25072 attr_id = token->u.value;
25073 else if (token->type == CPP_KEYWORD)
25074 attr_id = ridpointers[(int) token->keyword];
25075 else if (token->flags & NAMED_OP)
25076 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25078 if (attr_id == NULL_TREE)
25079 return NULL_TREE;
25081 cp_lexer_consume_token (parser->lexer);
25083 token = cp_lexer_peek_token (parser->lexer);
25084 if (token->type == CPP_SCOPE)
25086 /* We are seeing a scoped attribute token. */
25088 cp_lexer_consume_token (parser->lexer);
25089 if (attr_ns)
25090 error_at (token->location, "attribute using prefix used together "
25091 "with scoped attribute token");
25092 attr_ns = attr_id;
25094 token = cp_lexer_consume_token (parser->lexer);
25095 if (token->type == CPP_NAME)
25096 attr_id = token->u.value;
25097 else if (token->type == CPP_KEYWORD)
25098 attr_id = ridpointers[(int) token->keyword];
25099 else if (token->flags & NAMED_OP)
25100 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25101 else
25103 error_at (token->location,
25104 "expected an identifier for the attribute name");
25105 return error_mark_node;
25108 attr_id = canonicalize_attr_name (attr_id);
25109 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25110 NULL_TREE);
25111 token = cp_lexer_peek_token (parser->lexer);
25113 else if (attr_ns)
25114 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25115 NULL_TREE);
25116 else
25118 attr_id = canonicalize_attr_name (attr_id);
25119 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25120 NULL_TREE);
25121 /* C++11 noreturn attribute is equivalent to GNU's. */
25122 if (is_attribute_p ("noreturn", attr_id))
25123 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25124 /* C++14 deprecated attribute is equivalent to GNU's. */
25125 else if (is_attribute_p ("deprecated", attr_id))
25126 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25127 /* C++17 fallthrough attribute is equivalent to GNU's. */
25128 else if (is_attribute_p ("fallthrough", attr_id))
25129 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25130 /* Transactional Memory TS optimize_for_synchronized attribute is
25131 equivalent to GNU transaction_callable. */
25132 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25133 TREE_PURPOSE (attribute)
25134 = get_identifier ("transaction_callable");
25135 /* Transactional Memory attributes are GNU attributes. */
25136 else if (tm_attr_to_mask (attr_id))
25137 TREE_PURPOSE (attribute) = attr_id;
25140 /* Now parse the optional argument clause of the attribute. */
25142 if (token->type != CPP_OPEN_PAREN)
25143 return attribute;
25146 vec<tree, va_gc> *vec;
25147 int attr_flag = normal_attr;
25149 if (attr_ns == get_identifier ("gnu")
25150 && attribute_takes_identifier_p (attr_id))
25151 /* A GNU attribute that takes an identifier in parameter. */
25152 attr_flag = id_attr;
25154 vec = cp_parser_parenthesized_expression_list
25155 (parser, attr_flag, /*cast_p=*/false,
25156 /*allow_expansion_p=*/true,
25157 /*non_constant_p=*/NULL);
25158 if (vec == NULL)
25159 arguments = error_mark_node;
25160 else
25162 arguments = build_tree_list_vec (vec);
25163 release_tree_vector (vec);
25166 if (arguments == error_mark_node)
25167 attribute = error_mark_node;
25168 else
25169 TREE_VALUE (attribute) = arguments;
25172 return attribute;
25175 /* Check that the attribute ATTRIBUTE appears at most once in the
25176 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25177 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25178 isn't implemented yet in GCC. */
25180 static void
25181 cp_parser_check_std_attribute (tree attributes, tree attribute)
25183 if (attributes)
25185 tree name = get_attribute_name (attribute);
25186 if (is_attribute_p ("noreturn", name)
25187 && lookup_attribute ("noreturn", attributes))
25188 error ("attribute %<noreturn%> can appear at most once "
25189 "in an attribute-list");
25190 else if (is_attribute_p ("deprecated", name)
25191 && lookup_attribute ("deprecated", attributes))
25192 error ("attribute %<deprecated%> can appear at most once "
25193 "in an attribute-list");
25197 /* Parse a list of standard C++-11 attributes.
25199 attribute-list:
25200 attribute [opt]
25201 attribute-list , attribute[opt]
25202 attribute ...
25203 attribute-list , attribute ...
25206 static tree
25207 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25209 tree attributes = NULL_TREE, attribute = NULL_TREE;
25210 cp_token *token = NULL;
25212 while (true)
25214 attribute = cp_parser_std_attribute (parser, attr_ns);
25215 if (attribute == error_mark_node)
25216 break;
25217 if (attribute != NULL_TREE)
25219 cp_parser_check_std_attribute (attributes, attribute);
25220 TREE_CHAIN (attribute) = attributes;
25221 attributes = attribute;
25223 token = cp_lexer_peek_token (parser->lexer);
25224 if (token->type == CPP_ELLIPSIS)
25226 cp_lexer_consume_token (parser->lexer);
25227 if (attribute == NULL_TREE)
25228 error_at (token->location,
25229 "expected attribute before %<...%>");
25230 else
25232 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25233 if (pack == error_mark_node)
25234 return error_mark_node;
25235 TREE_VALUE (attribute) = pack;
25237 token = cp_lexer_peek_token (parser->lexer);
25239 if (token->type != CPP_COMMA)
25240 break;
25241 cp_lexer_consume_token (parser->lexer);
25243 attributes = nreverse (attributes);
25244 return attributes;
25247 /* Parse a standard C++-11 attribute specifier.
25249 attribute-specifier:
25250 [ [ attribute-using-prefix [opt] attribute-list ] ]
25251 alignment-specifier
25253 attribute-using-prefix:
25254 using attribute-namespace :
25256 alignment-specifier:
25257 alignas ( type-id ... [opt] )
25258 alignas ( alignment-expression ... [opt] ). */
25260 static tree
25261 cp_parser_std_attribute_spec (cp_parser *parser)
25263 tree attributes = NULL_TREE;
25264 cp_token *token = cp_lexer_peek_token (parser->lexer);
25266 if (token->type == CPP_OPEN_SQUARE
25267 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25269 tree attr_ns = NULL_TREE;
25271 cp_lexer_consume_token (parser->lexer);
25272 cp_lexer_consume_token (parser->lexer);
25274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25276 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25277 if (token->type == CPP_NAME)
25278 attr_ns = token->u.value;
25279 else if (token->type == CPP_KEYWORD)
25280 attr_ns = ridpointers[(int) token->keyword];
25281 else if (token->flags & NAMED_OP)
25282 attr_ns = get_identifier (cpp_type2name (token->type,
25283 token->flags));
25284 if (attr_ns
25285 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25287 if (cxx_dialect < cxx17
25288 && !in_system_header_at (input_location))
25289 pedwarn (input_location, 0,
25290 "attribute using prefix only available "
25291 "with -std=c++17 or -std=gnu++17");
25293 cp_lexer_consume_token (parser->lexer);
25294 cp_lexer_consume_token (parser->lexer);
25295 cp_lexer_consume_token (parser->lexer);
25297 else
25298 attr_ns = NULL_TREE;
25301 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25303 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25304 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25305 cp_parser_skip_to_end_of_statement (parser);
25306 else
25307 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25308 when we are sure that we have actually parsed them. */
25309 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25311 else
25313 tree alignas_expr;
25315 /* Look for an alignment-specifier. */
25317 token = cp_lexer_peek_token (parser->lexer);
25319 if (token->type != CPP_KEYWORD
25320 || token->keyword != RID_ALIGNAS)
25321 return NULL_TREE;
25323 cp_lexer_consume_token (parser->lexer);
25324 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25326 matching_parens parens;
25327 if (!parens.require_open (parser))
25329 cp_parser_error (parser, "expected %<(%>");
25330 return error_mark_node;
25333 cp_parser_parse_tentatively (parser);
25334 alignas_expr = cp_parser_type_id (parser);
25336 if (!cp_parser_parse_definitely (parser))
25338 alignas_expr = cp_parser_assignment_expression (parser);
25339 if (alignas_expr == error_mark_node)
25340 cp_parser_skip_to_end_of_statement (parser);
25341 if (alignas_expr == NULL_TREE
25342 || alignas_expr == error_mark_node)
25343 return alignas_expr;
25346 alignas_expr = cxx_alignas_expr (alignas_expr);
25347 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25349 /* Handle alignas (pack...). */
25350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25352 cp_lexer_consume_token (parser->lexer);
25353 alignas_expr = make_pack_expansion (alignas_expr);
25356 /* Something went wrong, so don't build the attribute. */
25357 if (alignas_expr == error_mark_node)
25358 return error_mark_node;
25360 if (!parens.require_close (parser))
25362 cp_parser_error (parser, "expected %<)%>");
25363 return error_mark_node;
25366 /* Build the C++-11 representation of an 'aligned'
25367 attribute. */
25368 attributes =
25369 build_tree_list (build_tree_list (get_identifier ("gnu"),
25370 get_identifier ("aligned")),
25371 alignas_expr);
25374 return attributes;
25377 /* Parse a standard C++-11 attribute-specifier-seq.
25379 attribute-specifier-seq:
25380 attribute-specifier-seq [opt] attribute-specifier
25383 static tree
25384 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25386 tree attr_specs = NULL_TREE;
25387 tree attr_last = NULL_TREE;
25389 while (true)
25391 tree attr_spec = cp_parser_std_attribute_spec (parser);
25392 if (attr_spec == NULL_TREE)
25393 break;
25394 if (attr_spec == error_mark_node)
25395 return error_mark_node;
25397 if (attr_last)
25398 TREE_CHAIN (attr_last) = attr_spec;
25399 else
25400 attr_specs = attr_last = attr_spec;
25401 attr_last = tree_last (attr_last);
25404 return attr_specs;
25407 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25408 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25409 current value of the PEDANTIC flag, regardless of whether or not
25410 the `__extension__' keyword is present. The caller is responsible
25411 for restoring the value of the PEDANTIC flag. */
25413 static bool
25414 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25416 /* Save the old value of the PEDANTIC flag. */
25417 *saved_pedantic = pedantic;
25419 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25421 /* Consume the `__extension__' token. */
25422 cp_lexer_consume_token (parser->lexer);
25423 /* We're not being pedantic while the `__extension__' keyword is
25424 in effect. */
25425 pedantic = 0;
25427 return true;
25430 return false;
25433 /* Parse a label declaration.
25435 label-declaration:
25436 __label__ label-declarator-seq ;
25438 label-declarator-seq:
25439 identifier , label-declarator-seq
25440 identifier */
25442 static void
25443 cp_parser_label_declaration (cp_parser* parser)
25445 /* Look for the `__label__' keyword. */
25446 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25448 while (true)
25450 tree identifier;
25452 /* Look for an identifier. */
25453 identifier = cp_parser_identifier (parser);
25454 /* If we failed, stop. */
25455 if (identifier == error_mark_node)
25456 break;
25457 /* Declare it as a label. */
25458 finish_label_decl (identifier);
25459 /* If the next token is a `;', stop. */
25460 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25461 break;
25462 /* Look for the `,' separating the label declarations. */
25463 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25466 /* Look for the final `;'. */
25467 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25470 // -------------------------------------------------------------------------- //
25471 // Requires Clause
25473 // Parse a requires clause.
25475 // requires-clause:
25476 // 'requires' logical-or-expression
25478 // The required logical-or-expression must be a constant expression. Note
25479 // that we don't check that the expression is constepxr here. We defer until
25480 // we analyze constraints and then, we only check atomic constraints.
25481 static tree
25482 cp_parser_requires_clause (cp_parser *parser)
25484 // Parse the requires clause so that it is not automatically folded.
25485 ++processing_template_decl;
25486 tree expr = cp_parser_binary_expression (parser, false, false,
25487 PREC_NOT_OPERATOR, NULL);
25488 if (check_for_bare_parameter_packs (expr))
25489 expr = error_mark_node;
25490 --processing_template_decl;
25491 return expr;
25494 // Optionally parse a requires clause:
25495 static tree
25496 cp_parser_requires_clause_opt (cp_parser *parser)
25498 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25499 if (tok->keyword != RID_REQUIRES)
25501 if (!flag_concepts && tok->type == CPP_NAME
25502 && tok->u.value == ridpointers[RID_REQUIRES])
25504 error_at (cp_lexer_peek_token (parser->lexer)->location,
25505 "%<requires%> only available with -fconcepts");
25506 /* Parse and discard the requires-clause. */
25507 cp_lexer_consume_token (parser->lexer);
25508 cp_parser_requires_clause (parser);
25510 return NULL_TREE;
25512 cp_lexer_consume_token (parser->lexer);
25513 return cp_parser_requires_clause (parser);
25517 /*---------------------------------------------------------------------------
25518 Requires expressions
25519 ---------------------------------------------------------------------------*/
25521 /* Parse a requires expression
25523 requirement-expression:
25524 'requires' requirement-parameter-list [opt] requirement-body */
25525 static tree
25526 cp_parser_requires_expression (cp_parser *parser)
25528 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25529 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25531 /* A requires-expression shall appear only within a concept
25532 definition or a requires-clause.
25534 TODO: Implement this diagnostic correctly. */
25535 if (!processing_template_decl)
25537 error_at (loc, "a requires expression cannot appear outside a template");
25538 cp_parser_skip_to_end_of_statement (parser);
25539 return error_mark_node;
25542 tree parms, reqs;
25544 /* Local parameters are delared as variables within the scope
25545 of the expression. They are not visible past the end of
25546 the expression. Expressions within the requires-expression
25547 are unevaluated. */
25548 struct scope_sentinel
25550 scope_sentinel ()
25552 ++cp_unevaluated_operand;
25553 begin_scope (sk_block, NULL_TREE);
25556 ~scope_sentinel ()
25558 pop_bindings_and_leave_scope ();
25559 --cp_unevaluated_operand;
25561 } s;
25563 /* Parse the optional parameter list. */
25564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25566 parms = cp_parser_requirement_parameter_list (parser);
25567 if (parms == error_mark_node)
25568 return error_mark_node;
25570 else
25571 parms = NULL_TREE;
25573 /* Parse the requirement body. */
25574 reqs = cp_parser_requirement_body (parser);
25575 if (reqs == error_mark_node)
25576 return error_mark_node;
25579 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25580 the parm chain. */
25581 grokparms (parms, &parms);
25582 return finish_requires_expr (parms, reqs);
25585 /* Parse a parameterized requirement.
25587 requirement-parameter-list:
25588 '(' parameter-declaration-clause ')' */
25589 static tree
25590 cp_parser_requirement_parameter_list (cp_parser *parser)
25592 matching_parens parens;
25593 if (!parens.require_open (parser))
25594 return error_mark_node;
25596 tree parms = cp_parser_parameter_declaration_clause (parser);
25598 if (!parens.require_close (parser))
25599 return error_mark_node;
25601 return parms;
25604 /* Parse the body of a requirement.
25606 requirement-body:
25607 '{' requirement-list '}' */
25608 static tree
25609 cp_parser_requirement_body (cp_parser *parser)
25611 matching_braces braces;
25612 if (!braces.require_open (parser))
25613 return error_mark_node;
25615 tree reqs = cp_parser_requirement_list (parser);
25617 if (!braces.require_close (parser))
25618 return error_mark_node;
25620 return reqs;
25623 /* Parse a list of requirements.
25625 requirement-list:
25626 requirement
25627 requirement-list ';' requirement[opt] */
25628 static tree
25629 cp_parser_requirement_list (cp_parser *parser)
25631 tree result = NULL_TREE;
25632 while (true)
25634 tree req = cp_parser_requirement (parser);
25635 if (req == error_mark_node)
25636 return error_mark_node;
25638 result = tree_cons (NULL_TREE, req, result);
25640 /* If we see a semi-colon, consume it. */
25641 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25642 cp_lexer_consume_token (parser->lexer);
25644 /* Stop processing at the end of the list. */
25645 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25646 break;
25649 /* Reverse the order of requirements so they are analyzed in
25650 declaration order. */
25651 return nreverse (result);
25654 /* Parse a syntactic requirement or type requirement.
25656 requirement:
25657 simple-requirement
25658 compound-requirement
25659 type-requirement
25660 nested-requirement */
25661 static tree
25662 cp_parser_requirement (cp_parser *parser)
25664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25665 return cp_parser_compound_requirement (parser);
25666 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25667 return cp_parser_type_requirement (parser);
25668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25669 return cp_parser_nested_requirement (parser);
25670 else
25671 return cp_parser_simple_requirement (parser);
25674 /* Parse a simple requirement.
25676 simple-requirement:
25677 expression ';' */
25678 static tree
25679 cp_parser_simple_requirement (cp_parser *parser)
25681 tree expr = cp_parser_expression (parser, NULL, false, false);
25682 if (!expr || expr == error_mark_node)
25683 return error_mark_node;
25685 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25686 return error_mark_node;
25688 return finish_simple_requirement (expr);
25691 /* Parse a type requirement
25693 type-requirement
25694 nested-name-specifier [opt] required-type-name ';'
25696 required-type-name:
25697 type-name
25698 'template' [opt] simple-template-id */
25699 static tree
25700 cp_parser_type_requirement (cp_parser *parser)
25702 cp_lexer_consume_token (parser->lexer);
25704 // Save the scope before parsing name specifiers.
25705 tree saved_scope = parser->scope;
25706 tree saved_object_scope = parser->object_scope;
25707 tree saved_qualifying_scope = parser->qualifying_scope;
25708 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25709 cp_parser_nested_name_specifier_opt (parser,
25710 /*typename_keyword_p=*/true,
25711 /*check_dependency_p=*/false,
25712 /*type_p=*/true,
25713 /*is_declaration=*/false);
25715 tree type;
25716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25718 cp_lexer_consume_token (parser->lexer);
25719 type = cp_parser_template_id (parser,
25720 /*template_keyword_p=*/true,
25721 /*check_dependency=*/false,
25722 /*tag_type=*/none_type,
25723 /*is_declaration=*/false);
25724 type = make_typename_type (parser->scope, type, typename_type,
25725 /*complain=*/tf_error);
25727 else
25728 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25730 if (TREE_CODE (type) == TYPE_DECL)
25731 type = TREE_TYPE (type);
25733 parser->scope = saved_scope;
25734 parser->object_scope = saved_object_scope;
25735 parser->qualifying_scope = saved_qualifying_scope;
25737 if (type == error_mark_node)
25738 cp_parser_skip_to_end_of_statement (parser);
25740 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25741 return error_mark_node;
25742 if (type == error_mark_node)
25743 return error_mark_node;
25745 return finish_type_requirement (type);
25748 /* Parse a compound requirement
25750 compound-requirement:
25751 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25752 static tree
25753 cp_parser_compound_requirement (cp_parser *parser)
25755 /* Parse an expression enclosed in '{ }'s. */
25756 matching_braces braces;
25757 if (!braces.require_open (parser))
25758 return error_mark_node;
25760 tree expr = cp_parser_expression (parser, NULL, false, false);
25761 if (!expr || expr == error_mark_node)
25762 return error_mark_node;
25764 if (!braces.require_close (parser))
25765 return error_mark_node;
25767 /* Parse the optional noexcept. */
25768 bool noexcept_p = false;
25769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25771 cp_lexer_consume_token (parser->lexer);
25772 noexcept_p = true;
25775 /* Parse the optional trailing return type. */
25776 tree type = NULL_TREE;
25777 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25779 cp_lexer_consume_token (parser->lexer);
25780 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25781 parser->in_result_type_constraint_p = true;
25782 type = cp_parser_trailing_type_id (parser);
25783 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25784 if (type == error_mark_node)
25785 return error_mark_node;
25788 return finish_compound_requirement (expr, type, noexcept_p);
25791 /* Parse a nested requirement. This is the same as a requires clause.
25793 nested-requirement:
25794 requires-clause */
25795 static tree
25796 cp_parser_nested_requirement (cp_parser *parser)
25798 cp_lexer_consume_token (parser->lexer);
25799 tree req = cp_parser_requires_clause (parser);
25800 if (req == error_mark_node)
25801 return error_mark_node;
25802 return finish_nested_requirement (req);
25805 /* Support Functions */
25807 /* Return the appropriate prefer_type argument for lookup_name_real based on
25808 tag_type and template_mem_access. */
25810 static inline int
25811 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25813 /* DR 141: When looking in the current enclosing context for a template-name
25814 after -> or ., only consider class templates. */
25815 if (template_mem_access)
25816 return 2;
25817 switch (tag_type)
25819 case none_type: return 0; // No preference.
25820 case scope_type: return 1; // Type or namespace.
25821 default: return 2; // Type only.
25825 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25826 NAME should have one of the representations used for an
25827 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25828 is returned. If PARSER->SCOPE is a dependent type, then a
25829 SCOPE_REF is returned.
25831 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25832 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25833 was formed. Abstractly, such entities should not be passed to this
25834 function, because they do not need to be looked up, but it is
25835 simpler to check for this special case here, rather than at the
25836 call-sites.
25838 In cases not explicitly covered above, this function returns a
25839 DECL, OVERLOAD, or baselink representing the result of the lookup.
25840 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25841 is returned.
25843 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25844 (e.g., "struct") that was used. In that case bindings that do not
25845 refer to types are ignored.
25847 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25848 ignored.
25850 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25851 are ignored.
25853 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25854 types.
25856 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25857 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25858 NULL_TREE otherwise. */
25860 static cp_expr
25861 cp_parser_lookup_name (cp_parser *parser, tree name,
25862 enum tag_types tag_type,
25863 bool is_template,
25864 bool is_namespace,
25865 bool check_dependency,
25866 tree *ambiguous_decls,
25867 location_t name_location)
25869 tree decl;
25870 tree object_type = parser->context->object_type;
25872 /* Assume that the lookup will be unambiguous. */
25873 if (ambiguous_decls)
25874 *ambiguous_decls = NULL_TREE;
25876 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25877 no longer valid. Note that if we are parsing tentatively, and
25878 the parse fails, OBJECT_TYPE will be automatically restored. */
25879 parser->context->object_type = NULL_TREE;
25881 if (name == error_mark_node)
25882 return error_mark_node;
25884 /* A template-id has already been resolved; there is no lookup to
25885 do. */
25886 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25887 return name;
25888 if (BASELINK_P (name))
25890 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25891 == TEMPLATE_ID_EXPR);
25892 return name;
25895 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25896 it should already have been checked to make sure that the name
25897 used matches the type being destroyed. */
25898 if (TREE_CODE (name) == BIT_NOT_EXPR)
25900 tree type;
25902 /* Figure out to which type this destructor applies. */
25903 if (parser->scope)
25904 type = parser->scope;
25905 else if (object_type)
25906 type = object_type;
25907 else
25908 type = current_class_type;
25909 /* If that's not a class type, there is no destructor. */
25910 if (!type || !CLASS_TYPE_P (type))
25911 return error_mark_node;
25913 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25914 lazily_declare_fn (sfk_destructor, type);
25916 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25917 return dtor;
25919 return error_mark_node;
25922 /* By this point, the NAME should be an ordinary identifier. If
25923 the id-expression was a qualified name, the qualifying scope is
25924 stored in PARSER->SCOPE at this point. */
25925 gcc_assert (identifier_p (name));
25927 /* Perform the lookup. */
25928 if (parser->scope)
25930 bool dependent_p;
25932 if (parser->scope == error_mark_node)
25933 return error_mark_node;
25935 /* If the SCOPE is dependent, the lookup must be deferred until
25936 the template is instantiated -- unless we are explicitly
25937 looking up names in uninstantiated templates. Even then, we
25938 cannot look up the name if the scope is not a class type; it
25939 might, for example, be a template type parameter. */
25940 dependent_p = (TYPE_P (parser->scope)
25941 && dependent_scope_p (parser->scope));
25942 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25943 && dependent_p)
25944 /* Defer lookup. */
25945 decl = error_mark_node;
25946 else
25948 tree pushed_scope = NULL_TREE;
25950 /* If PARSER->SCOPE is a dependent type, then it must be a
25951 class type, and we must not be checking dependencies;
25952 otherwise, we would have processed this lookup above. So
25953 that PARSER->SCOPE is not considered a dependent base by
25954 lookup_member, we must enter the scope here. */
25955 if (dependent_p)
25956 pushed_scope = push_scope (parser->scope);
25958 /* If the PARSER->SCOPE is a template specialization, it
25959 may be instantiated during name lookup. In that case,
25960 errors may be issued. Even if we rollback the current
25961 tentative parse, those errors are valid. */
25962 decl = lookup_qualified_name (parser->scope, name,
25963 prefer_type_arg (tag_type),
25964 /*complain=*/true);
25966 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25967 lookup result and the nested-name-specifier nominates a class C:
25968 * if the name specified after the nested-name-specifier, when
25969 looked up in C, is the injected-class-name of C (Clause 9), or
25970 * if the name specified after the nested-name-specifier is the
25971 same as the identifier or the simple-template-id's template-
25972 name in the last component of the nested-name-specifier,
25973 the name is instead considered to name the constructor of
25974 class C. [ Note: for example, the constructor is not an
25975 acceptable lookup result in an elaborated-type-specifier so
25976 the constructor would not be used in place of the
25977 injected-class-name. --end note ] Such a constructor name
25978 shall be used only in the declarator-id of a declaration that
25979 names a constructor or in a using-declaration. */
25980 if (tag_type == none_type
25981 && DECL_SELF_REFERENCE_P (decl)
25982 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25983 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25984 prefer_type_arg (tag_type),
25985 /*complain=*/true);
25987 /* If we have a single function from a using decl, pull it out. */
25988 if (TREE_CODE (decl) == OVERLOAD
25989 && !really_overloaded_fn (decl))
25990 decl = OVL_FUNCTION (decl);
25992 if (pushed_scope)
25993 pop_scope (pushed_scope);
25996 /* If the scope is a dependent type and either we deferred lookup or
25997 we did lookup but didn't find the name, rememeber the name. */
25998 if (decl == error_mark_node && TYPE_P (parser->scope)
25999 && dependent_type_p (parser->scope))
26001 if (tag_type)
26003 tree type;
26005 /* The resolution to Core Issue 180 says that `struct
26006 A::B' should be considered a type-name, even if `A'
26007 is dependent. */
26008 type = make_typename_type (parser->scope, name, tag_type,
26009 /*complain=*/tf_error);
26010 if (type != error_mark_node)
26011 decl = TYPE_NAME (type);
26013 else if (is_template
26014 && (cp_parser_next_token_ends_template_argument_p (parser)
26015 || cp_lexer_next_token_is (parser->lexer,
26016 CPP_CLOSE_PAREN)))
26017 decl = make_unbound_class_template (parser->scope,
26018 name, NULL_TREE,
26019 /*complain=*/tf_error);
26020 else
26021 decl = build_qualified_name (/*type=*/NULL_TREE,
26022 parser->scope, name,
26023 is_template);
26025 parser->qualifying_scope = parser->scope;
26026 parser->object_scope = NULL_TREE;
26028 else if (object_type)
26030 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26031 OBJECT_TYPE is not a class. */
26032 if (CLASS_TYPE_P (object_type))
26033 /* If the OBJECT_TYPE is a template specialization, it may
26034 be instantiated during name lookup. In that case, errors
26035 may be issued. Even if we rollback the current tentative
26036 parse, those errors are valid. */
26037 decl = lookup_member (object_type,
26038 name,
26039 /*protect=*/0,
26040 prefer_type_arg (tag_type),
26041 tf_warning_or_error);
26042 else
26043 decl = NULL_TREE;
26045 if (!decl)
26046 /* Look it up in the enclosing context. DR 141: When looking for a
26047 template-name after -> or ., only consider class templates. */
26048 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26049 /*nonclass=*/0,
26050 /*block_p=*/true, is_namespace, 0);
26051 if (object_type == unknown_type_node)
26052 /* The object is type-dependent, so we can't look anything up; we used
26053 this to get the DR 141 behavior. */
26054 object_type = NULL_TREE;
26055 parser->object_scope = object_type;
26056 parser->qualifying_scope = NULL_TREE;
26058 else
26060 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26061 /*nonclass=*/0,
26062 /*block_p=*/true, is_namespace, 0);
26063 parser->qualifying_scope = NULL_TREE;
26064 parser->object_scope = NULL_TREE;
26067 /* If the lookup failed, let our caller know. */
26068 if (!decl || decl == error_mark_node)
26069 return error_mark_node;
26071 /* Pull out the template from an injected-class-name (or multiple). */
26072 if (is_template)
26073 decl = maybe_get_template_decl_from_type_decl (decl);
26075 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26076 if (TREE_CODE (decl) == TREE_LIST)
26078 if (ambiguous_decls)
26079 *ambiguous_decls = decl;
26080 /* The error message we have to print is too complicated for
26081 cp_parser_error, so we incorporate its actions directly. */
26082 if (!cp_parser_simulate_error (parser))
26084 error_at (name_location, "reference to %qD is ambiguous",
26085 name);
26086 print_candidates (decl);
26088 return error_mark_node;
26091 gcc_assert (DECL_P (decl)
26092 || TREE_CODE (decl) == OVERLOAD
26093 || TREE_CODE (decl) == SCOPE_REF
26094 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26095 || BASELINK_P (decl));
26097 /* If we have resolved the name of a member declaration, check to
26098 see if the declaration is accessible. When the name resolves to
26099 set of overloaded functions, accessibility is checked when
26100 overload resolution is done.
26102 During an explicit instantiation, access is not checked at all,
26103 as per [temp.explicit]. */
26104 if (DECL_P (decl))
26105 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26107 maybe_record_typedef_use (decl);
26109 return cp_expr (decl, name_location);
26112 /* Like cp_parser_lookup_name, but for use in the typical case where
26113 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26114 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26116 static tree
26117 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26119 return cp_parser_lookup_name (parser, name,
26120 none_type,
26121 /*is_template=*/false,
26122 /*is_namespace=*/false,
26123 /*check_dependency=*/true,
26124 /*ambiguous_decls=*/NULL,
26125 location);
26128 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26129 the current context, return the TYPE_DECL. If TAG_NAME_P is
26130 true, the DECL indicates the class being defined in a class-head,
26131 or declared in an elaborated-type-specifier.
26133 Otherwise, return DECL. */
26135 static tree
26136 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26138 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26139 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26141 struct A {
26142 template <typename T> struct B;
26145 template <typename T> struct A::B {};
26147 Similarly, in an elaborated-type-specifier:
26149 namespace N { struct X{}; }
26151 struct A {
26152 template <typename T> friend struct N::X;
26155 However, if the DECL refers to a class type, and we are in
26156 the scope of the class, then the name lookup automatically
26157 finds the TYPE_DECL created by build_self_reference rather
26158 than a TEMPLATE_DECL. For example, in:
26160 template <class T> struct S {
26161 S s;
26164 there is no need to handle such case. */
26166 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26167 return DECL_TEMPLATE_RESULT (decl);
26169 return decl;
26172 /* If too many, or too few, template-parameter lists apply to the
26173 declarator, issue an error message. Returns TRUE if all went well,
26174 and FALSE otherwise. */
26176 static bool
26177 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26178 cp_declarator *declarator,
26179 location_t declarator_location)
26181 switch (declarator->kind)
26183 case cdk_id:
26185 unsigned num_templates = 0;
26186 tree scope = declarator->u.id.qualifying_scope;
26188 if (scope)
26189 num_templates = num_template_headers_for_class (scope);
26190 else if (TREE_CODE (declarator->u.id.unqualified_name)
26191 == TEMPLATE_ID_EXPR)
26192 /* If the DECLARATOR has the form `X<y>' then it uses one
26193 additional level of template parameters. */
26194 ++num_templates;
26196 return cp_parser_check_template_parameters
26197 (parser, num_templates, declarator_location, declarator);
26200 case cdk_function:
26201 case cdk_array:
26202 case cdk_pointer:
26203 case cdk_reference:
26204 case cdk_ptrmem:
26205 return (cp_parser_check_declarator_template_parameters
26206 (parser, declarator->declarator, declarator_location));
26208 case cdk_decomp:
26209 case cdk_error:
26210 return true;
26212 default:
26213 gcc_unreachable ();
26215 return false;
26218 /* NUM_TEMPLATES were used in the current declaration. If that is
26219 invalid, return FALSE and issue an error messages. Otherwise,
26220 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26221 declarator and we can print more accurate diagnostics. */
26223 static bool
26224 cp_parser_check_template_parameters (cp_parser* parser,
26225 unsigned num_templates,
26226 location_t location,
26227 cp_declarator *declarator)
26229 /* If there are the same number of template classes and parameter
26230 lists, that's OK. */
26231 if (parser->num_template_parameter_lists == num_templates)
26232 return true;
26233 /* If there are more, but only one more, then we are referring to a
26234 member template. That's OK too. */
26235 if (parser->num_template_parameter_lists == num_templates + 1)
26236 return true;
26237 /* If there are more template classes than parameter lists, we have
26238 something like:
26240 template <class T> void S<T>::R<T>::f (); */
26241 if (parser->num_template_parameter_lists < num_templates)
26243 if (declarator && !current_function_decl)
26244 error_at (location, "specializing member %<%T::%E%> "
26245 "requires %<template<>%> syntax",
26246 declarator->u.id.qualifying_scope,
26247 declarator->u.id.unqualified_name);
26248 else if (declarator)
26249 error_at (location, "invalid declaration of %<%T::%E%>",
26250 declarator->u.id.qualifying_scope,
26251 declarator->u.id.unqualified_name);
26252 else
26253 error_at (location, "too few template-parameter-lists");
26254 return false;
26256 /* Otherwise, there are too many template parameter lists. We have
26257 something like:
26259 template <class T> template <class U> void S::f(); */
26260 error_at (location, "too many template-parameter-lists");
26261 return false;
26264 /* Parse an optional `::' token indicating that the following name is
26265 from the global namespace. If so, PARSER->SCOPE is set to the
26266 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26267 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26268 Returns the new value of PARSER->SCOPE, if the `::' token is
26269 present, and NULL_TREE otherwise. */
26271 static tree
26272 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26274 cp_token *token;
26276 /* Peek at the next token. */
26277 token = cp_lexer_peek_token (parser->lexer);
26278 /* If we're looking at a `::' token then we're starting from the
26279 global namespace, not our current location. */
26280 if (token->type == CPP_SCOPE)
26282 /* Consume the `::' token. */
26283 cp_lexer_consume_token (parser->lexer);
26284 /* Set the SCOPE so that we know where to start the lookup. */
26285 parser->scope = global_namespace;
26286 parser->qualifying_scope = global_namespace;
26287 parser->object_scope = NULL_TREE;
26289 return parser->scope;
26291 else if (!current_scope_valid_p)
26293 parser->scope = NULL_TREE;
26294 parser->qualifying_scope = NULL_TREE;
26295 parser->object_scope = NULL_TREE;
26298 return NULL_TREE;
26301 /* Returns TRUE if the upcoming token sequence is the start of a
26302 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26303 declarator is preceded by the `friend' specifier. */
26305 static bool
26306 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26308 bool constructor_p;
26309 bool outside_class_specifier_p;
26310 tree nested_name_specifier;
26311 cp_token *next_token;
26313 /* The common case is that this is not a constructor declarator, so
26314 try to avoid doing lots of work if at all possible. It's not
26315 valid declare a constructor at function scope. */
26316 if (parser->in_function_body)
26317 return false;
26318 /* And only certain tokens can begin a constructor declarator. */
26319 next_token = cp_lexer_peek_token (parser->lexer);
26320 if (next_token->type != CPP_NAME
26321 && next_token->type != CPP_SCOPE
26322 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26323 && next_token->type != CPP_TEMPLATE_ID)
26324 return false;
26326 /* Parse tentatively; we are going to roll back all of the tokens
26327 consumed here. */
26328 cp_parser_parse_tentatively (parser);
26329 /* Assume that we are looking at a constructor declarator. */
26330 constructor_p = true;
26332 /* Look for the optional `::' operator. */
26333 cp_parser_global_scope_opt (parser,
26334 /*current_scope_valid_p=*/false);
26335 /* Look for the nested-name-specifier. */
26336 nested_name_specifier
26337 = (cp_parser_nested_name_specifier_opt (parser,
26338 /*typename_keyword_p=*/false,
26339 /*check_dependency_p=*/false,
26340 /*type_p=*/false,
26341 /*is_declaration=*/false));
26343 outside_class_specifier_p = (!at_class_scope_p ()
26344 || !TYPE_BEING_DEFINED (current_class_type)
26345 || friend_p);
26347 /* Outside of a class-specifier, there must be a
26348 nested-name-specifier. Except in C++17 mode, where we
26349 might be declaring a guiding declaration. */
26350 if (!nested_name_specifier && outside_class_specifier_p
26351 && cxx_dialect < cxx17)
26352 constructor_p = false;
26353 else if (nested_name_specifier == error_mark_node)
26354 constructor_p = false;
26356 /* If we have a class scope, this is easy; DR 147 says that S::S always
26357 names the constructor, and no other qualified name could. */
26358 if (constructor_p && nested_name_specifier
26359 && CLASS_TYPE_P (nested_name_specifier))
26361 tree id = cp_parser_unqualified_id (parser,
26362 /*template_keyword_p=*/false,
26363 /*check_dependency_p=*/false,
26364 /*declarator_p=*/true,
26365 /*optional_p=*/false);
26366 if (is_overloaded_fn (id))
26367 id = DECL_NAME (get_first_fn (id));
26368 if (!constructor_name_p (id, nested_name_specifier))
26369 constructor_p = false;
26371 /* If we still think that this might be a constructor-declarator,
26372 look for a class-name. */
26373 else if (constructor_p)
26375 /* If we have:
26377 template <typename T> struct S {
26378 S();
26381 we must recognize that the nested `S' names a class. */
26382 if (cxx_dialect >= cxx17)
26383 cp_parser_parse_tentatively (parser);
26385 tree type_decl;
26386 type_decl = cp_parser_class_name (parser,
26387 /*typename_keyword_p=*/false,
26388 /*template_keyword_p=*/false,
26389 none_type,
26390 /*check_dependency_p=*/false,
26391 /*class_head_p=*/false,
26392 /*is_declaration=*/false);
26394 if (cxx_dialect >= cxx17
26395 && !cp_parser_parse_definitely (parser))
26397 type_decl = NULL_TREE;
26398 tree tmpl = cp_parser_template_name (parser,
26399 /*template_keyword*/false,
26400 /*check_dependency_p*/false,
26401 /*is_declaration*/false,
26402 none_type,
26403 /*is_identifier*/NULL);
26404 if (DECL_CLASS_TEMPLATE_P (tmpl)
26405 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26406 /* It's a deduction guide, return true. */;
26407 else
26408 cp_parser_simulate_error (parser);
26411 /* If there was no class-name, then this is not a constructor.
26412 Otherwise, if we are in a class-specifier and we aren't
26413 handling a friend declaration, check that its type matches
26414 current_class_type (c++/38313). Note: error_mark_node
26415 is left alone for error recovery purposes. */
26416 constructor_p = (!cp_parser_error_occurred (parser)
26417 && (outside_class_specifier_p
26418 || type_decl == NULL_TREE
26419 || type_decl == error_mark_node
26420 || same_type_p (current_class_type,
26421 TREE_TYPE (type_decl))));
26423 /* If we're still considering a constructor, we have to see a `(',
26424 to begin the parameter-declaration-clause, followed by either a
26425 `)', an `...', or a decl-specifier. We need to check for a
26426 type-specifier to avoid being fooled into thinking that:
26428 S (f) (int);
26430 is a constructor. (It is actually a function named `f' that
26431 takes one parameter (of type `int') and returns a value of type
26432 `S'. */
26433 if (constructor_p
26434 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26435 constructor_p = false;
26437 if (constructor_p
26438 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26439 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26440 /* A parameter declaration begins with a decl-specifier,
26441 which is either the "attribute" keyword, a storage class
26442 specifier, or (usually) a type-specifier. */
26443 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26445 tree type;
26446 tree pushed_scope = NULL_TREE;
26447 unsigned saved_num_template_parameter_lists;
26449 /* Names appearing in the type-specifier should be looked up
26450 in the scope of the class. */
26451 if (current_class_type)
26452 type = NULL_TREE;
26453 else if (type_decl)
26455 type = TREE_TYPE (type_decl);
26456 if (TREE_CODE (type) == TYPENAME_TYPE)
26458 type = resolve_typename_type (type,
26459 /*only_current_p=*/false);
26460 if (TREE_CODE (type) == TYPENAME_TYPE)
26462 cp_parser_abort_tentative_parse (parser);
26463 return false;
26466 pushed_scope = push_scope (type);
26469 /* Inside the constructor parameter list, surrounding
26470 template-parameter-lists do not apply. */
26471 saved_num_template_parameter_lists
26472 = parser->num_template_parameter_lists;
26473 parser->num_template_parameter_lists = 0;
26475 /* Look for the type-specifier. */
26476 cp_parser_type_specifier (parser,
26477 CP_PARSER_FLAGS_NONE,
26478 /*decl_specs=*/NULL,
26479 /*is_declarator=*/true,
26480 /*declares_class_or_enum=*/NULL,
26481 /*is_cv_qualifier=*/NULL);
26483 parser->num_template_parameter_lists
26484 = saved_num_template_parameter_lists;
26486 /* Leave the scope of the class. */
26487 if (pushed_scope)
26488 pop_scope (pushed_scope);
26490 constructor_p = !cp_parser_error_occurred (parser);
26494 /* We did not really want to consume any tokens. */
26495 cp_parser_abort_tentative_parse (parser);
26497 return constructor_p;
26500 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26501 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26502 they must be performed once we are in the scope of the function.
26504 Returns the function defined. */
26506 static tree
26507 cp_parser_function_definition_from_specifiers_and_declarator
26508 (cp_parser* parser,
26509 cp_decl_specifier_seq *decl_specifiers,
26510 tree attributes,
26511 const cp_declarator *declarator)
26513 tree fn;
26514 bool success_p;
26516 /* Begin the function-definition. */
26517 success_p = start_function (decl_specifiers, declarator, attributes);
26519 /* The things we're about to see are not directly qualified by any
26520 template headers we've seen thus far. */
26521 reset_specialization ();
26523 /* If there were names looked up in the decl-specifier-seq that we
26524 did not check, check them now. We must wait until we are in the
26525 scope of the function to perform the checks, since the function
26526 might be a friend. */
26527 perform_deferred_access_checks (tf_warning_or_error);
26529 if (success_p)
26531 cp_finalize_omp_declare_simd (parser, current_function_decl);
26532 parser->omp_declare_simd = NULL;
26533 cp_finalize_oacc_routine (parser, current_function_decl, true);
26534 parser->oacc_routine = NULL;
26537 if (!success_p)
26539 /* Skip the entire function. */
26540 cp_parser_skip_to_end_of_block_or_statement (parser);
26541 fn = error_mark_node;
26543 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26545 /* Seen already, skip it. An error message has already been output. */
26546 cp_parser_skip_to_end_of_block_or_statement (parser);
26547 fn = current_function_decl;
26548 current_function_decl = NULL_TREE;
26549 /* If this is a function from a class, pop the nested class. */
26550 if (current_class_name)
26551 pop_nested_class ();
26553 else
26555 timevar_id_t tv;
26556 if (DECL_DECLARED_INLINE_P (current_function_decl))
26557 tv = TV_PARSE_INLINE;
26558 else
26559 tv = TV_PARSE_FUNC;
26560 timevar_push (tv);
26561 fn = cp_parser_function_definition_after_declarator (parser,
26562 /*inline_p=*/false);
26563 timevar_pop (tv);
26566 return fn;
26569 /* Parse the part of a function-definition that follows the
26570 declarator. INLINE_P is TRUE iff this function is an inline
26571 function defined within a class-specifier.
26573 Returns the function defined. */
26575 static tree
26576 cp_parser_function_definition_after_declarator (cp_parser* parser,
26577 bool inline_p)
26579 tree fn;
26580 bool saved_in_unbraced_linkage_specification_p;
26581 bool saved_in_function_body;
26582 unsigned saved_num_template_parameter_lists;
26583 cp_token *token;
26584 bool fully_implicit_function_template_p
26585 = parser->fully_implicit_function_template_p;
26586 parser->fully_implicit_function_template_p = false;
26587 tree implicit_template_parms
26588 = parser->implicit_template_parms;
26589 parser->implicit_template_parms = 0;
26590 cp_binding_level* implicit_template_scope
26591 = parser->implicit_template_scope;
26592 parser->implicit_template_scope = 0;
26594 saved_in_function_body = parser->in_function_body;
26595 parser->in_function_body = true;
26596 /* If the next token is `return', then the code may be trying to
26597 make use of the "named return value" extension that G++ used to
26598 support. */
26599 token = cp_lexer_peek_token (parser->lexer);
26600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26602 /* Consume the `return' keyword. */
26603 cp_lexer_consume_token (parser->lexer);
26604 /* Look for the identifier that indicates what value is to be
26605 returned. */
26606 cp_parser_identifier (parser);
26607 /* Issue an error message. */
26608 error_at (token->location,
26609 "named return values are no longer supported");
26610 /* Skip tokens until we reach the start of the function body. */
26611 while (true)
26613 cp_token *token = cp_lexer_peek_token (parser->lexer);
26614 if (token->type == CPP_OPEN_BRACE
26615 || token->type == CPP_EOF
26616 || token->type == CPP_PRAGMA_EOL)
26617 break;
26618 cp_lexer_consume_token (parser->lexer);
26621 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26622 anything declared inside `f'. */
26623 saved_in_unbraced_linkage_specification_p
26624 = parser->in_unbraced_linkage_specification_p;
26625 parser->in_unbraced_linkage_specification_p = false;
26626 /* Inside the function, surrounding template-parameter-lists do not
26627 apply. */
26628 saved_num_template_parameter_lists
26629 = parser->num_template_parameter_lists;
26630 parser->num_template_parameter_lists = 0;
26632 /* If the next token is `try', `__transaction_atomic', or
26633 `__transaction_relaxed`, then we are looking at either function-try-block
26634 or function-transaction-block. Note that all of these include the
26635 function-body. */
26636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26637 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26638 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26639 RID_TRANSACTION_RELAXED))
26640 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26641 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26642 cp_parser_function_try_block (parser);
26643 else
26644 cp_parser_ctor_initializer_opt_and_function_body
26645 (parser, /*in_function_try_block=*/false);
26647 /* Finish the function. */
26648 fn = finish_function (inline_p);
26649 /* Generate code for it, if necessary. */
26650 expand_or_defer_fn (fn);
26651 /* Restore the saved values. */
26652 parser->in_unbraced_linkage_specification_p
26653 = saved_in_unbraced_linkage_specification_p;
26654 parser->num_template_parameter_lists
26655 = saved_num_template_parameter_lists;
26656 parser->in_function_body = saved_in_function_body;
26658 parser->fully_implicit_function_template_p
26659 = fully_implicit_function_template_p;
26660 parser->implicit_template_parms
26661 = implicit_template_parms;
26662 parser->implicit_template_scope
26663 = implicit_template_scope;
26665 if (parser->fully_implicit_function_template_p)
26666 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26668 return fn;
26671 /* Parse a template-declaration body (following argument list). */
26673 static void
26674 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26675 tree parameter_list,
26676 bool member_p)
26678 tree decl = NULL_TREE;
26679 bool friend_p = false;
26681 /* We just processed one more parameter list. */
26682 ++parser->num_template_parameter_lists;
26684 /* Get the deferred access checks from the parameter list. These
26685 will be checked once we know what is being declared, as for a
26686 member template the checks must be performed in the scope of the
26687 class containing the member. */
26688 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26690 /* Tentatively parse for a new template parameter list, which can either be
26691 the template keyword or a template introduction. */
26692 if (cp_parser_template_declaration_after_export (parser, member_p))
26693 /* OK */;
26694 else if (cxx_dialect >= cxx11
26695 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26696 decl = cp_parser_alias_declaration (parser);
26697 else
26699 /* There are no access checks when parsing a template, as we do not
26700 know if a specialization will be a friend. */
26701 push_deferring_access_checks (dk_no_check);
26702 cp_token *token = cp_lexer_peek_token (parser->lexer);
26703 decl = cp_parser_single_declaration (parser,
26704 checks,
26705 member_p,
26706 /*explicit_specialization_p=*/false,
26707 &friend_p);
26708 pop_deferring_access_checks ();
26710 /* If this is a member template declaration, let the front
26711 end know. */
26712 if (member_p && !friend_p && decl)
26714 if (TREE_CODE (decl) == TYPE_DECL)
26715 cp_parser_check_access_in_redeclaration (decl, token->location);
26717 decl = finish_member_template_decl (decl);
26719 else if (friend_p && decl
26720 && DECL_DECLARES_TYPE_P (decl))
26721 make_friend_class (current_class_type, TREE_TYPE (decl),
26722 /*complain=*/true);
26724 /* We are done with the current parameter list. */
26725 --parser->num_template_parameter_lists;
26727 pop_deferring_access_checks ();
26729 /* Finish up. */
26730 finish_template_decl (parameter_list);
26732 /* Check the template arguments for a literal operator template. */
26733 if (decl
26734 && DECL_DECLARES_FUNCTION_P (decl)
26735 && UDLIT_OPER_P (DECL_NAME (decl)))
26737 bool ok = true;
26738 if (parameter_list == NULL_TREE)
26739 ok = false;
26740 else
26742 int num_parms = TREE_VEC_LENGTH (parameter_list);
26743 if (num_parms == 1)
26745 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26746 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26747 if (TREE_TYPE (parm) != char_type_node
26748 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26749 ok = false;
26751 else if (num_parms == 2 && cxx_dialect >= cxx14)
26753 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26754 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26755 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26756 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26757 if (parm == error_mark_node
26758 || TREE_TYPE (parm) != TREE_TYPE (type)
26759 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26760 ok = false;
26762 else
26763 ok = false;
26765 if (!ok)
26767 if (cxx_dialect >= cxx14)
26768 error ("literal operator template %qD has invalid parameter list."
26769 " Expected non-type template argument pack <char...>"
26770 " or <typename CharT, CharT...>",
26771 decl);
26772 else
26773 error ("literal operator template %qD has invalid parameter list."
26774 " Expected non-type template argument pack <char...>",
26775 decl);
26779 /* Register member declarations. */
26780 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26781 finish_member_declaration (decl);
26782 /* If DECL is a function template, we must return to parse it later.
26783 (Even though there is no definition, there might be default
26784 arguments that need handling.) */
26785 if (member_p && decl
26786 && DECL_DECLARES_FUNCTION_P (decl))
26787 vec_safe_push (unparsed_funs_with_definitions, decl);
26790 /* Parse a template introduction header for a template-declaration. Returns
26791 false if tentative parse fails. */
26793 static bool
26794 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26796 cp_parser_parse_tentatively (parser);
26798 tree saved_scope = parser->scope;
26799 tree saved_object_scope = parser->object_scope;
26800 tree saved_qualifying_scope = parser->qualifying_scope;
26802 /* Look for the optional `::' operator. */
26803 cp_parser_global_scope_opt (parser,
26804 /*current_scope_valid_p=*/false);
26805 /* Look for the nested-name-specifier. */
26806 cp_parser_nested_name_specifier_opt (parser,
26807 /*typename_keyword_p=*/false,
26808 /*check_dependency_p=*/true,
26809 /*type_p=*/false,
26810 /*is_declaration=*/false);
26812 cp_token *token = cp_lexer_peek_token (parser->lexer);
26813 tree concept_name = cp_parser_identifier (parser);
26815 /* Look up the concept for which we will be matching
26816 template parameters. */
26817 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26818 token->location);
26819 parser->scope = saved_scope;
26820 parser->object_scope = saved_object_scope;
26821 parser->qualifying_scope = saved_qualifying_scope;
26823 if (concept_name == error_mark_node)
26824 cp_parser_simulate_error (parser);
26826 /* Look for opening brace for introduction. */
26827 matching_braces braces;
26828 braces.require_open (parser);
26830 if (!cp_parser_parse_definitely (parser))
26831 return false;
26833 push_deferring_access_checks (dk_deferred);
26835 /* Build vector of placeholder parameters and grab
26836 matching identifiers. */
26837 tree introduction_list = cp_parser_introduction_list (parser);
26839 /* The introduction-list shall not be empty. */
26840 int nargs = TREE_VEC_LENGTH (introduction_list);
26841 if (nargs == 0)
26843 error ("empty introduction-list");
26844 return true;
26847 /* Look for closing brace for introduction. */
26848 if (!braces.require_close (parser))
26849 return true;
26851 if (tmpl_decl == error_mark_node)
26853 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26854 token->location);
26855 return true;
26858 /* Build and associate the constraint. */
26859 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26860 if (parms && parms != error_mark_node)
26862 cp_parser_template_declaration_after_parameters (parser, parms,
26863 member_p);
26864 return true;
26867 error_at (token->location, "no matching concept for template-introduction");
26868 return true;
26871 /* Parse a normal template-declaration following the template keyword. */
26873 static void
26874 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26876 tree parameter_list;
26877 bool need_lang_pop;
26878 location_t location = input_location;
26880 /* Look for the `<' token. */
26881 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26882 return;
26883 if (at_class_scope_p () && current_function_decl)
26885 /* 14.5.2.2 [temp.mem]
26887 A local class shall not have member templates. */
26888 error_at (location,
26889 "invalid declaration of member template in local class");
26890 cp_parser_skip_to_end_of_block_or_statement (parser);
26891 return;
26893 /* [temp]
26895 A template ... shall not have C linkage. */
26896 if (current_lang_name == lang_name_c)
26898 error_at (location, "template with C linkage");
26899 maybe_show_extern_c_location ();
26900 /* Give it C++ linkage to avoid confusing other parts of the
26901 front end. */
26902 push_lang_context (lang_name_cplusplus);
26903 need_lang_pop = true;
26905 else
26906 need_lang_pop = false;
26908 /* We cannot perform access checks on the template parameter
26909 declarations until we know what is being declared, just as we
26910 cannot check the decl-specifier list. */
26911 push_deferring_access_checks (dk_deferred);
26913 /* If the next token is `>', then we have an invalid
26914 specialization. Rather than complain about an invalid template
26915 parameter, issue an error message here. */
26916 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26918 cp_parser_error (parser, "invalid explicit specialization");
26919 begin_specialization ();
26920 parameter_list = NULL_TREE;
26922 else
26924 /* Parse the template parameters. */
26925 parameter_list = cp_parser_template_parameter_list (parser);
26928 /* Look for the `>'. */
26929 cp_parser_skip_to_end_of_template_parameter_list (parser);
26931 /* Manage template requirements */
26932 if (flag_concepts)
26934 tree reqs = get_shorthand_constraints (current_template_parms);
26935 if (tree r = cp_parser_requires_clause_opt (parser))
26936 reqs = conjoin_constraints (reqs, normalize_expression (r));
26937 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26940 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26941 member_p);
26943 /* For the erroneous case of a template with C linkage, we pushed an
26944 implicit C++ linkage scope; exit that scope now. */
26945 if (need_lang_pop)
26946 pop_lang_context ();
26949 /* Parse a template-declaration, assuming that the `export' (and
26950 `extern') keywords, if present, has already been scanned. MEMBER_P
26951 is as for cp_parser_template_declaration. */
26953 static bool
26954 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26956 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26958 cp_lexer_consume_token (parser->lexer);
26959 cp_parser_explicit_template_declaration (parser, member_p);
26960 return true;
26962 else if (flag_concepts)
26963 return cp_parser_template_introduction (parser, member_p);
26965 return false;
26968 /* Perform the deferred access checks from a template-parameter-list.
26969 CHECKS is a TREE_LIST of access checks, as returned by
26970 get_deferred_access_checks. */
26972 static void
26973 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26975 ++processing_template_parmlist;
26976 perform_access_checks (checks, tf_warning_or_error);
26977 --processing_template_parmlist;
26980 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26981 `function-definition' sequence that follows a template header.
26982 If MEMBER_P is true, this declaration appears in a class scope.
26984 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26985 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26987 static tree
26988 cp_parser_single_declaration (cp_parser* parser,
26989 vec<deferred_access_check, va_gc> *checks,
26990 bool member_p,
26991 bool explicit_specialization_p,
26992 bool* friend_p)
26994 int declares_class_or_enum;
26995 tree decl = NULL_TREE;
26996 cp_decl_specifier_seq decl_specifiers;
26997 bool function_definition_p = false;
26998 cp_token *decl_spec_token_start;
27000 /* This function is only used when processing a template
27001 declaration. */
27002 gcc_assert (innermost_scope_kind () == sk_template_parms
27003 || innermost_scope_kind () == sk_template_spec);
27005 /* Defer access checks until we know what is being declared. */
27006 push_deferring_access_checks (dk_deferred);
27008 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27009 alternative. */
27010 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27011 cp_parser_decl_specifier_seq (parser,
27012 CP_PARSER_FLAGS_OPTIONAL,
27013 &decl_specifiers,
27014 &declares_class_or_enum);
27015 if (friend_p)
27016 *friend_p = cp_parser_friend_p (&decl_specifiers);
27018 /* There are no template typedefs. */
27019 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27021 error_at (decl_spec_token_start->location,
27022 "template declaration of %<typedef%>");
27023 decl = error_mark_node;
27026 /* Gather up the access checks that occurred the
27027 decl-specifier-seq. */
27028 stop_deferring_access_checks ();
27030 /* Check for the declaration of a template class. */
27031 if (declares_class_or_enum)
27033 if (cp_parser_declares_only_class_p (parser)
27034 || (declares_class_or_enum & 2))
27036 // If this is a declaration, but not a definition, associate
27037 // any constraints with the type declaration. Constraints
27038 // are associated with definitions in cp_parser_class_specifier.
27039 if (declares_class_or_enum == 1)
27040 associate_classtype_constraints (decl_specifiers.type);
27042 decl = shadow_tag (&decl_specifiers);
27044 /* In this case:
27046 struct C {
27047 friend template <typename T> struct A<T>::B;
27050 A<T>::B will be represented by a TYPENAME_TYPE, and
27051 therefore not recognized by shadow_tag. */
27052 if (friend_p && *friend_p
27053 && !decl
27054 && decl_specifiers.type
27055 && TYPE_P (decl_specifiers.type))
27056 decl = decl_specifiers.type;
27058 if (decl && decl != error_mark_node)
27059 decl = TYPE_NAME (decl);
27060 else
27061 decl = error_mark_node;
27063 /* Perform access checks for template parameters. */
27064 cp_parser_perform_template_parameter_access_checks (checks);
27066 /* Give a helpful diagnostic for
27067 template <class T> struct A { } a;
27068 if we aren't already recovering from an error. */
27069 if (!cp_parser_declares_only_class_p (parser)
27070 && !seen_error ())
27072 error_at (cp_lexer_peek_token (parser->lexer)->location,
27073 "a class template declaration must not declare "
27074 "anything else");
27075 cp_parser_skip_to_end_of_block_or_statement (parser);
27076 goto out;
27081 /* Complain about missing 'typename' or other invalid type names. */
27082 if (!decl_specifiers.any_type_specifiers_p
27083 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27085 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27086 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27087 the rest of this declaration. */
27088 decl = error_mark_node;
27089 goto out;
27092 /* If it's not a template class, try for a template function. If
27093 the next token is a `;', then this declaration does not declare
27094 anything. But, if there were errors in the decl-specifiers, then
27095 the error might well have come from an attempted class-specifier.
27096 In that case, there's no need to warn about a missing declarator. */
27097 if (!decl
27098 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27099 || decl_specifiers.type != error_mark_node))
27101 decl = cp_parser_init_declarator (parser,
27102 &decl_specifiers,
27103 checks,
27104 /*function_definition_allowed_p=*/true,
27105 member_p,
27106 declares_class_or_enum,
27107 &function_definition_p,
27108 NULL, NULL, NULL);
27110 /* 7.1.1-1 [dcl.stc]
27112 A storage-class-specifier shall not be specified in an explicit
27113 specialization... */
27114 if (decl
27115 && explicit_specialization_p
27116 && decl_specifiers.storage_class != sc_none)
27118 error_at (decl_spec_token_start->location,
27119 "explicit template specialization cannot have a storage class");
27120 decl = error_mark_node;
27123 if (decl && VAR_P (decl))
27124 check_template_variable (decl);
27127 /* Look for a trailing `;' after the declaration. */
27128 if (!function_definition_p
27129 && (decl == error_mark_node
27130 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27131 cp_parser_skip_to_end_of_block_or_statement (parser);
27133 out:
27134 pop_deferring_access_checks ();
27136 /* Clear any current qualification; whatever comes next is the start
27137 of something new. */
27138 parser->scope = NULL_TREE;
27139 parser->qualifying_scope = NULL_TREE;
27140 parser->object_scope = NULL_TREE;
27142 return decl;
27145 /* Parse a cast-expression that is not the operand of a unary "&". */
27147 static cp_expr
27148 cp_parser_simple_cast_expression (cp_parser *parser)
27150 return cp_parser_cast_expression (parser, /*address_p=*/false,
27151 /*cast_p=*/false, /*decltype*/false, NULL);
27154 /* Parse a functional cast to TYPE. Returns an expression
27155 representing the cast. */
27157 static cp_expr
27158 cp_parser_functional_cast (cp_parser* parser, tree type)
27160 vec<tree, va_gc> *vec;
27161 tree expression_list;
27162 cp_expr cast;
27163 bool nonconst_p;
27165 location_t start_loc = input_location;
27167 if (!type)
27168 type = error_mark_node;
27170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27172 cp_lexer_set_source_position (parser->lexer);
27173 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27174 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27175 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27176 if (TREE_CODE (type) == TYPE_DECL)
27177 type = TREE_TYPE (type);
27179 cast = finish_compound_literal (type, expression_list,
27180 tf_warning_or_error, fcl_functional);
27181 /* Create a location of the form:
27182 type_name{i, f}
27183 ^~~~~~~~~~~~~~~
27184 with caret == start at the start of the type name,
27185 finishing at the closing brace. */
27186 location_t finish_loc
27187 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27188 location_t combined_loc = make_location (start_loc, start_loc,
27189 finish_loc);
27190 cast.set_location (combined_loc);
27191 return cast;
27195 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27196 /*cast_p=*/true,
27197 /*allow_expansion_p=*/true,
27198 /*non_constant_p=*/NULL);
27199 if (vec == NULL)
27200 expression_list = error_mark_node;
27201 else
27203 expression_list = build_tree_list_vec (vec);
27204 release_tree_vector (vec);
27207 cast = build_functional_cast (type, expression_list,
27208 tf_warning_or_error);
27209 /* [expr.const]/1: In an integral constant expression "only type
27210 conversions to integral or enumeration type can be used". */
27211 if (TREE_CODE (type) == TYPE_DECL)
27212 type = TREE_TYPE (type);
27213 if (cast != error_mark_node
27214 && !cast_valid_in_integral_constant_expression_p (type)
27215 && cp_parser_non_integral_constant_expression (parser,
27216 NIC_CONSTRUCTOR))
27217 return error_mark_node;
27219 /* Create a location of the form:
27220 float(i)
27221 ^~~~~~~~
27222 with caret == start at the start of the type name,
27223 finishing at the closing paren. */
27224 location_t finish_loc
27225 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27226 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27227 cast.set_location (combined_loc);
27228 return cast;
27231 /* Save the tokens that make up the body of a member function defined
27232 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27233 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27234 specifiers applied to the declaration. Returns the FUNCTION_DECL
27235 for the member function. */
27237 static tree
27238 cp_parser_save_member_function_body (cp_parser* parser,
27239 cp_decl_specifier_seq *decl_specifiers,
27240 cp_declarator *declarator,
27241 tree attributes)
27243 cp_token *first;
27244 cp_token *last;
27245 tree fn;
27246 bool function_try_block = false;
27248 /* Create the FUNCTION_DECL. */
27249 fn = grokmethod (decl_specifiers, declarator, attributes);
27250 cp_finalize_omp_declare_simd (parser, fn);
27251 cp_finalize_oacc_routine (parser, fn, true);
27252 /* If something went badly wrong, bail out now. */
27253 if (fn == error_mark_node)
27255 /* If there's a function-body, skip it. */
27256 if (cp_parser_token_starts_function_definition_p
27257 (cp_lexer_peek_token (parser->lexer)))
27258 cp_parser_skip_to_end_of_block_or_statement (parser);
27259 return error_mark_node;
27262 /* Remember it, if there default args to post process. */
27263 cp_parser_save_default_args (parser, fn);
27265 /* Save away the tokens that make up the body of the
27266 function. */
27267 first = parser->lexer->next_token;
27269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27270 cp_lexer_consume_token (parser->lexer);
27271 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27272 RID_TRANSACTION_ATOMIC))
27274 cp_lexer_consume_token (parser->lexer);
27275 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27277 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27278 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27279 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27280 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27281 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27283 cp_lexer_consume_token (parser->lexer);
27284 cp_lexer_consume_token (parser->lexer);
27285 cp_lexer_consume_token (parser->lexer);
27286 cp_lexer_consume_token (parser->lexer);
27287 cp_lexer_consume_token (parser->lexer);
27289 else
27290 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27291 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27293 cp_lexer_consume_token (parser->lexer);
27294 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27295 break;
27299 /* Handle function try blocks. */
27300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27302 cp_lexer_consume_token (parser->lexer);
27303 function_try_block = true;
27305 /* We can have braced-init-list mem-initializers before the fn body. */
27306 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27308 cp_lexer_consume_token (parser->lexer);
27309 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27311 /* cache_group will stop after an un-nested { } pair, too. */
27312 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27313 break;
27315 /* variadic mem-inits have ... after the ')'. */
27316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27317 cp_lexer_consume_token (parser->lexer);
27320 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27321 /* Handle function try blocks. */
27322 if (function_try_block)
27323 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27324 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27325 last = parser->lexer->next_token;
27327 /* Save away the inline definition; we will process it when the
27328 class is complete. */
27329 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27330 DECL_PENDING_INLINE_P (fn) = 1;
27332 /* We need to know that this was defined in the class, so that
27333 friend templates are handled correctly. */
27334 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27336 /* Add FN to the queue of functions to be parsed later. */
27337 vec_safe_push (unparsed_funs_with_definitions, fn);
27339 return fn;
27342 /* Save the tokens that make up the in-class initializer for a non-static
27343 data member. Returns a DEFAULT_ARG. */
27345 static tree
27346 cp_parser_save_nsdmi (cp_parser* parser)
27348 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27351 /* Parse a template-argument-list, as well as the trailing ">" (but
27352 not the opening "<"). See cp_parser_template_argument_list for the
27353 return value. */
27355 static tree
27356 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27358 tree arguments;
27359 tree saved_scope;
27360 tree saved_qualifying_scope;
27361 tree saved_object_scope;
27362 bool saved_greater_than_is_operator_p;
27363 int saved_unevaluated_operand;
27364 int saved_inhibit_evaluation_warnings;
27366 /* [temp.names]
27368 When parsing a template-id, the first non-nested `>' is taken as
27369 the end of the template-argument-list rather than a greater-than
27370 operator. */
27371 saved_greater_than_is_operator_p
27372 = parser->greater_than_is_operator_p;
27373 parser->greater_than_is_operator_p = false;
27374 /* Parsing the argument list may modify SCOPE, so we save it
27375 here. */
27376 saved_scope = parser->scope;
27377 saved_qualifying_scope = parser->qualifying_scope;
27378 saved_object_scope = parser->object_scope;
27379 /* We need to evaluate the template arguments, even though this
27380 template-id may be nested within a "sizeof". */
27381 saved_unevaluated_operand = cp_unevaluated_operand;
27382 cp_unevaluated_operand = 0;
27383 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27384 c_inhibit_evaluation_warnings = 0;
27385 /* Parse the template-argument-list itself. */
27386 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27387 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27388 arguments = NULL_TREE;
27389 else
27390 arguments = cp_parser_template_argument_list (parser);
27391 /* Look for the `>' that ends the template-argument-list. If we find
27392 a '>>' instead, it's probably just a typo. */
27393 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27395 if (cxx_dialect != cxx98)
27397 /* In C++0x, a `>>' in a template argument list or cast
27398 expression is considered to be two separate `>'
27399 tokens. So, change the current token to a `>', but don't
27400 consume it: it will be consumed later when the outer
27401 template argument list (or cast expression) is parsed.
27402 Note that this replacement of `>' for `>>' is necessary
27403 even if we are parsing tentatively: in the tentative
27404 case, after calling
27405 cp_parser_enclosed_template_argument_list we will always
27406 throw away all of the template arguments and the first
27407 closing `>', either because the template argument list
27408 was erroneous or because we are replacing those tokens
27409 with a CPP_TEMPLATE_ID token. The second `>' (which will
27410 not have been thrown away) is needed either to close an
27411 outer template argument list or to complete a new-style
27412 cast. */
27413 cp_token *token = cp_lexer_peek_token (parser->lexer);
27414 token->type = CPP_GREATER;
27416 else if (!saved_greater_than_is_operator_p)
27418 /* If we're in a nested template argument list, the '>>' has
27419 to be a typo for '> >'. We emit the error message, but we
27420 continue parsing and we push a '>' as next token, so that
27421 the argument list will be parsed correctly. Note that the
27422 global source location is still on the token before the
27423 '>>', so we need to say explicitly where we want it. */
27424 cp_token *token = cp_lexer_peek_token (parser->lexer);
27425 gcc_rich_location richloc (token->location);
27426 richloc.add_fixit_replace ("> >");
27427 error_at (&richloc, "%<>>%> should be %<> >%> "
27428 "within a nested template argument list");
27430 token->type = CPP_GREATER;
27432 else
27434 /* If this is not a nested template argument list, the '>>'
27435 is a typo for '>'. Emit an error message and continue.
27436 Same deal about the token location, but here we can get it
27437 right by consuming the '>>' before issuing the diagnostic. */
27438 cp_token *token = cp_lexer_consume_token (parser->lexer);
27439 error_at (token->location,
27440 "spurious %<>>%>, use %<>%> to terminate "
27441 "a template argument list");
27444 else
27445 cp_parser_skip_to_end_of_template_parameter_list (parser);
27446 /* The `>' token might be a greater-than operator again now. */
27447 parser->greater_than_is_operator_p
27448 = saved_greater_than_is_operator_p;
27449 /* Restore the SAVED_SCOPE. */
27450 parser->scope = saved_scope;
27451 parser->qualifying_scope = saved_qualifying_scope;
27452 parser->object_scope = saved_object_scope;
27453 cp_unevaluated_operand = saved_unevaluated_operand;
27454 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27456 return arguments;
27459 /* MEMBER_FUNCTION is a member function, or a friend. If default
27460 arguments, or the body of the function have not yet been parsed,
27461 parse them now. */
27463 static void
27464 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27466 timevar_push (TV_PARSE_INMETH);
27467 /* If this member is a template, get the underlying
27468 FUNCTION_DECL. */
27469 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27470 member_function = DECL_TEMPLATE_RESULT (member_function);
27472 /* There should not be any class definitions in progress at this
27473 point; the bodies of members are only parsed outside of all class
27474 definitions. */
27475 gcc_assert (parser->num_classes_being_defined == 0);
27476 /* While we're parsing the member functions we might encounter more
27477 classes. We want to handle them right away, but we don't want
27478 them getting mixed up with functions that are currently in the
27479 queue. */
27480 push_unparsed_function_queues (parser);
27482 /* Make sure that any template parameters are in scope. */
27483 maybe_begin_member_template_processing (member_function);
27485 /* If the body of the function has not yet been parsed, parse it
27486 now. */
27487 if (DECL_PENDING_INLINE_P (member_function))
27489 tree function_scope;
27490 cp_token_cache *tokens;
27492 /* The function is no longer pending; we are processing it. */
27493 tokens = DECL_PENDING_INLINE_INFO (member_function);
27494 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27495 DECL_PENDING_INLINE_P (member_function) = 0;
27497 /* If this is a local class, enter the scope of the containing
27498 function. */
27499 function_scope = current_function_decl;
27500 if (function_scope)
27501 push_function_context ();
27503 /* Push the body of the function onto the lexer stack. */
27504 cp_parser_push_lexer_for_tokens (parser, tokens);
27506 /* Let the front end know that we going to be defining this
27507 function. */
27508 start_preparsed_function (member_function, NULL_TREE,
27509 SF_PRE_PARSED | SF_INCLASS_INLINE);
27511 /* Don't do access checking if it is a templated function. */
27512 if (processing_template_decl)
27513 push_deferring_access_checks (dk_no_check);
27515 /* #pragma omp declare reduction needs special parsing. */
27516 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27518 parser->lexer->in_pragma = true;
27519 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27520 finish_function (/*inline_p=*/true);
27521 cp_check_omp_declare_reduction (member_function);
27523 else
27524 /* Now, parse the body of the function. */
27525 cp_parser_function_definition_after_declarator (parser,
27526 /*inline_p=*/true);
27528 if (processing_template_decl)
27529 pop_deferring_access_checks ();
27531 /* Leave the scope of the containing function. */
27532 if (function_scope)
27533 pop_function_context ();
27534 cp_parser_pop_lexer (parser);
27537 /* Remove any template parameters from the symbol table. */
27538 maybe_end_member_template_processing ();
27540 /* Restore the queue. */
27541 pop_unparsed_function_queues (parser);
27542 timevar_pop (TV_PARSE_INMETH);
27545 /* If DECL contains any default args, remember it on the unparsed
27546 functions queue. */
27548 static void
27549 cp_parser_save_default_args (cp_parser* parser, tree decl)
27551 tree probe;
27553 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27554 probe;
27555 probe = TREE_CHAIN (probe))
27556 if (TREE_PURPOSE (probe))
27558 cp_default_arg_entry entry = {current_class_type, decl};
27559 vec_safe_push (unparsed_funs_with_default_args, entry);
27560 break;
27564 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27565 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27566 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27567 from the parameter-type-list. */
27569 static tree
27570 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27571 tree default_arg, tree parmtype)
27573 cp_token_cache *tokens;
27574 tree parsed_arg;
27575 bool dummy;
27577 if (default_arg == error_mark_node)
27578 return error_mark_node;
27580 /* Push the saved tokens for the default argument onto the parser's
27581 lexer stack. */
27582 tokens = DEFARG_TOKENS (default_arg);
27583 cp_parser_push_lexer_for_tokens (parser, tokens);
27585 start_lambda_scope (decl);
27587 /* Parse the default argument. */
27588 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27589 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27590 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27592 finish_lambda_scope ();
27594 if (parsed_arg == error_mark_node)
27595 cp_parser_skip_to_end_of_statement (parser);
27597 if (!processing_template_decl)
27599 /* In a non-template class, check conversions now. In a template,
27600 we'll wait and instantiate these as needed. */
27601 if (TREE_CODE (decl) == PARM_DECL)
27602 parsed_arg = check_default_argument (parmtype, parsed_arg,
27603 tf_warning_or_error);
27604 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27605 parsed_arg = error_mark_node;
27606 else
27607 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27610 /* If the token stream has not been completely used up, then
27611 there was extra junk after the end of the default
27612 argument. */
27613 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27615 if (TREE_CODE (decl) == PARM_DECL)
27616 cp_parser_error (parser, "expected %<,%>");
27617 else
27618 cp_parser_error (parser, "expected %<;%>");
27621 /* Revert to the main lexer. */
27622 cp_parser_pop_lexer (parser);
27624 return parsed_arg;
27627 /* FIELD is a non-static data member with an initializer which we saved for
27628 later; parse it now. */
27630 static void
27631 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27633 tree def;
27635 maybe_begin_member_template_processing (field);
27637 push_unparsed_function_queues (parser);
27638 def = cp_parser_late_parse_one_default_arg (parser, field,
27639 DECL_INITIAL (field),
27640 NULL_TREE);
27641 pop_unparsed_function_queues (parser);
27643 maybe_end_member_template_processing ();
27645 DECL_INITIAL (field) = def;
27648 /* FN is a FUNCTION_DECL which may contains a parameter with an
27649 unparsed DEFAULT_ARG. Parse the default args now. This function
27650 assumes that the current scope is the scope in which the default
27651 argument should be processed. */
27653 static void
27654 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27656 bool saved_local_variables_forbidden_p;
27657 tree parm, parmdecl;
27659 /* While we're parsing the default args, we might (due to the
27660 statement expression extension) encounter more classes. We want
27661 to handle them right away, but we don't want them getting mixed
27662 up with default args that are currently in the queue. */
27663 push_unparsed_function_queues (parser);
27665 /* Local variable names (and the `this' keyword) may not appear
27666 in a default argument. */
27667 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27668 parser->local_variables_forbidden_p = true;
27670 push_defarg_context (fn);
27672 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27673 parmdecl = DECL_ARGUMENTS (fn);
27674 parm && parm != void_list_node;
27675 parm = TREE_CHAIN (parm),
27676 parmdecl = DECL_CHAIN (parmdecl))
27678 tree default_arg = TREE_PURPOSE (parm);
27679 tree parsed_arg;
27680 vec<tree, va_gc> *insts;
27681 tree copy;
27682 unsigned ix;
27684 if (!default_arg)
27685 continue;
27687 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27688 /* This can happen for a friend declaration for a function
27689 already declared with default arguments. */
27690 continue;
27692 parsed_arg
27693 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27694 default_arg,
27695 TREE_VALUE (parm));
27696 TREE_PURPOSE (parm) = parsed_arg;
27698 /* Update any instantiations we've already created. */
27699 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27700 vec_safe_iterate (insts, ix, &copy); ix++)
27701 TREE_PURPOSE (copy) = parsed_arg;
27704 pop_defarg_context ();
27706 /* Make sure no default arg is missing. */
27707 check_default_args (fn);
27709 /* Restore the state of local_variables_forbidden_p. */
27710 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27712 /* Restore the queue. */
27713 pop_unparsed_function_queues (parser);
27716 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27718 sizeof ... ( identifier )
27720 where the 'sizeof' token has already been consumed. */
27722 static tree
27723 cp_parser_sizeof_pack (cp_parser *parser)
27725 /* Consume the `...'. */
27726 cp_lexer_consume_token (parser->lexer);
27727 maybe_warn_variadic_templates ();
27729 matching_parens parens;
27730 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27731 if (paren)
27732 parens.consume_open (parser);
27733 else
27734 permerror (cp_lexer_peek_token (parser->lexer)->location,
27735 "%<sizeof...%> argument must be surrounded by parentheses");
27737 cp_token *token = cp_lexer_peek_token (parser->lexer);
27738 tree name = cp_parser_identifier (parser);
27739 if (name == error_mark_node)
27740 return error_mark_node;
27741 /* The name is not qualified. */
27742 parser->scope = NULL_TREE;
27743 parser->qualifying_scope = NULL_TREE;
27744 parser->object_scope = NULL_TREE;
27745 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27746 if (expr == error_mark_node)
27747 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27748 token->location);
27749 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27750 expr = TREE_TYPE (expr);
27751 else if (TREE_CODE (expr) == CONST_DECL)
27752 expr = DECL_INITIAL (expr);
27753 expr = make_pack_expansion (expr);
27754 PACK_EXPANSION_SIZEOF_P (expr) = true;
27756 if (paren)
27757 parens.require_close (parser);
27759 return expr;
27762 /* Parse the operand of `sizeof' (or a similar operator). Returns
27763 either a TYPE or an expression, depending on the form of the
27764 input. The KEYWORD indicates which kind of expression we have
27765 encountered. */
27767 static tree
27768 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27770 tree expr = NULL_TREE;
27771 const char *saved_message;
27772 char *tmp;
27773 bool saved_integral_constant_expression_p;
27774 bool saved_non_integral_constant_expression_p;
27776 /* If it's a `...', then we are computing the length of a parameter
27777 pack. */
27778 if (keyword == RID_SIZEOF
27779 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27780 return cp_parser_sizeof_pack (parser);
27782 /* Types cannot be defined in a `sizeof' expression. Save away the
27783 old message. */
27784 saved_message = parser->type_definition_forbidden_message;
27785 /* And create the new one. */
27786 tmp = concat ("types may not be defined in %<",
27787 IDENTIFIER_POINTER (ridpointers[keyword]),
27788 "%> expressions", NULL);
27789 parser->type_definition_forbidden_message = tmp;
27791 /* The restrictions on constant-expressions do not apply inside
27792 sizeof expressions. */
27793 saved_integral_constant_expression_p
27794 = parser->integral_constant_expression_p;
27795 saved_non_integral_constant_expression_p
27796 = parser->non_integral_constant_expression_p;
27797 parser->integral_constant_expression_p = false;
27799 /* Do not actually evaluate the expression. */
27800 ++cp_unevaluated_operand;
27801 ++c_inhibit_evaluation_warnings;
27802 /* If it's a `(', then we might be looking at the type-id
27803 construction. */
27804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27806 tree type = NULL_TREE;
27808 /* We can't be sure yet whether we're looking at a type-id or an
27809 expression. */
27810 cp_parser_parse_tentatively (parser);
27812 matching_parens parens;
27813 parens.consume_open (parser);
27815 /* Note: as a GNU Extension, compound literals are considered
27816 postfix-expressions as they are in C99, so they are valid
27817 arguments to sizeof. See comment in cp_parser_cast_expression
27818 for details. */
27819 if (cp_parser_compound_literal_p (parser))
27820 cp_parser_simulate_error (parser);
27821 else
27823 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27824 parser->in_type_id_in_expr_p = true;
27825 /* Look for the type-id. */
27826 type = cp_parser_type_id (parser);
27827 /* Look for the closing `)'. */
27828 parens.require_close (parser);
27829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27832 /* If all went well, then we're done. */
27833 if (cp_parser_parse_definitely (parser))
27835 cp_decl_specifier_seq decl_specs;
27837 /* Build a trivial decl-specifier-seq. */
27838 clear_decl_specs (&decl_specs);
27839 decl_specs.type = type;
27841 /* Call grokdeclarator to figure out what type this is. */
27842 expr = grokdeclarator (NULL,
27843 &decl_specs,
27844 TYPENAME,
27845 /*initialized=*/0,
27846 /*attrlist=*/NULL);
27850 /* If the type-id production did not work out, then we must be
27851 looking at the unary-expression production. */
27852 if (!expr)
27853 expr = cp_parser_unary_expression (parser);
27855 /* Go back to evaluating expressions. */
27856 --cp_unevaluated_operand;
27857 --c_inhibit_evaluation_warnings;
27859 /* Free the message we created. */
27860 free (tmp);
27861 /* And restore the old one. */
27862 parser->type_definition_forbidden_message = saved_message;
27863 parser->integral_constant_expression_p
27864 = saved_integral_constant_expression_p;
27865 parser->non_integral_constant_expression_p
27866 = saved_non_integral_constant_expression_p;
27868 return expr;
27871 /* If the current declaration has no declarator, return true. */
27873 static bool
27874 cp_parser_declares_only_class_p (cp_parser *parser)
27876 /* If the next token is a `;' or a `,' then there is no
27877 declarator. */
27878 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27879 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27882 /* Update the DECL_SPECS to reflect the storage class indicated by
27883 KEYWORD. */
27885 static void
27886 cp_parser_set_storage_class (cp_parser *parser,
27887 cp_decl_specifier_seq *decl_specs,
27888 enum rid keyword,
27889 cp_token *token)
27891 cp_storage_class storage_class;
27893 if (parser->in_unbraced_linkage_specification_p)
27895 error_at (token->location, "invalid use of %qD in linkage specification",
27896 ridpointers[keyword]);
27897 return;
27899 else if (decl_specs->storage_class != sc_none)
27901 decl_specs->conflicting_specifiers_p = true;
27902 return;
27905 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27906 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27907 && decl_specs->gnu_thread_keyword_p)
27909 pedwarn (decl_specs->locations[ds_thread], 0,
27910 "%<__thread%> before %qD", ridpointers[keyword]);
27913 switch (keyword)
27915 case RID_AUTO:
27916 storage_class = sc_auto;
27917 break;
27918 case RID_REGISTER:
27919 storage_class = sc_register;
27920 break;
27921 case RID_STATIC:
27922 storage_class = sc_static;
27923 break;
27924 case RID_EXTERN:
27925 storage_class = sc_extern;
27926 break;
27927 case RID_MUTABLE:
27928 storage_class = sc_mutable;
27929 break;
27930 default:
27931 gcc_unreachable ();
27933 decl_specs->storage_class = storage_class;
27934 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27936 /* A storage class specifier cannot be applied alongside a typedef
27937 specifier. If there is a typedef specifier present then set
27938 conflicting_specifiers_p which will trigger an error later
27939 on in grokdeclarator. */
27940 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27941 decl_specs->conflicting_specifiers_p = true;
27944 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27945 is true, the type is a class or enum definition. */
27947 static void
27948 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27949 tree type_spec,
27950 cp_token *token,
27951 bool type_definition_p)
27953 decl_specs->any_specifiers_p = true;
27955 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27956 (with, for example, in "typedef int wchar_t;") we remember that
27957 this is what happened. In system headers, we ignore these
27958 declarations so that G++ can work with system headers that are not
27959 C++-safe. */
27960 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27961 && !type_definition_p
27962 && (type_spec == boolean_type_node
27963 || type_spec == char16_type_node
27964 || type_spec == char32_type_node
27965 || type_spec == wchar_type_node)
27966 && (decl_specs->type
27967 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27968 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27969 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27970 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27972 decl_specs->redefined_builtin_type = type_spec;
27973 set_and_check_decl_spec_loc (decl_specs,
27974 ds_redefined_builtin_type_spec,
27975 token);
27976 if (!decl_specs->type)
27978 decl_specs->type = type_spec;
27979 decl_specs->type_definition_p = false;
27980 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27983 else if (decl_specs->type)
27984 decl_specs->multiple_types_p = true;
27985 else
27987 decl_specs->type = type_spec;
27988 decl_specs->type_definition_p = type_definition_p;
27989 decl_specs->redefined_builtin_type = NULL_TREE;
27990 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27994 /* True iff TOKEN is the GNU keyword __thread. */
27996 static bool
27997 token_is__thread (cp_token *token)
27999 gcc_assert (token->keyword == RID_THREAD);
28000 return id_equal (token->u.value, "__thread");
28003 /* Set the location for a declarator specifier and check if it is
28004 duplicated.
28006 DECL_SPECS is the sequence of declarator specifiers onto which to
28007 set the location.
28009 DS is the single declarator specifier to set which location is to
28010 be set onto the existing sequence of declarators.
28012 LOCATION is the location for the declarator specifier to
28013 consider. */
28015 static void
28016 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28017 cp_decl_spec ds, cp_token *token)
28019 gcc_assert (ds < ds_last);
28021 if (decl_specs == NULL)
28022 return;
28024 source_location location = token->location;
28026 if (decl_specs->locations[ds] == 0)
28028 decl_specs->locations[ds] = location;
28029 if (ds == ds_thread)
28030 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28032 else
28034 if (ds == ds_long)
28036 if (decl_specs->locations[ds_long_long] != 0)
28037 error_at (location,
28038 "%<long long long%> is too long for GCC");
28039 else
28041 decl_specs->locations[ds_long_long] = location;
28042 pedwarn_cxx98 (location,
28043 OPT_Wlong_long,
28044 "ISO C++ 1998 does not support %<long long%>");
28047 else if (ds == ds_thread)
28049 bool gnu = token_is__thread (token);
28050 if (gnu != decl_specs->gnu_thread_keyword_p)
28051 error_at (location,
28052 "both %<__thread%> and %<thread_local%> specified");
28053 else
28055 gcc_rich_location richloc (location);
28056 richloc.add_fixit_remove ();
28057 error_at (&richloc, "duplicate %qD", token->u.value);
28060 else
28062 static const char *const decl_spec_names[] = {
28063 "signed",
28064 "unsigned",
28065 "short",
28066 "long",
28067 "const",
28068 "volatile",
28069 "restrict",
28070 "inline",
28071 "virtual",
28072 "explicit",
28073 "friend",
28074 "typedef",
28075 "using",
28076 "constexpr",
28077 "__complex"
28079 gcc_rich_location richloc (location);
28080 richloc.add_fixit_remove ();
28081 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28086 /* Return true iff the declarator specifier DS is present in the
28087 sequence of declarator specifiers DECL_SPECS. */
28089 bool
28090 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28091 cp_decl_spec ds)
28093 gcc_assert (ds < ds_last);
28095 if (decl_specs == NULL)
28096 return false;
28098 return decl_specs->locations[ds] != 0;
28101 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28102 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28104 static bool
28105 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28107 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28110 /* Issue an error message indicating that TOKEN_DESC was expected.
28111 If KEYWORD is true, it indicated this function is called by
28112 cp_parser_require_keword and the required token can only be
28113 a indicated keyword.
28115 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28116 within any error as the location of an "opening" token matching
28117 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28118 RT_CLOSE_PAREN). */
28120 static void
28121 cp_parser_required_error (cp_parser *parser,
28122 required_token token_desc,
28123 bool keyword,
28124 location_t matching_location)
28126 if (cp_parser_simulate_error (parser))
28127 return;
28129 const char *gmsgid = NULL;
28130 switch (token_desc)
28132 case RT_NEW:
28133 gmsgid = G_("expected %<new%>");
28134 break;
28135 case RT_DELETE:
28136 gmsgid = G_("expected %<delete%>");
28137 break;
28138 case RT_RETURN:
28139 gmsgid = G_("expected %<return%>");
28140 break;
28141 case RT_WHILE:
28142 gmsgid = G_("expected %<while%>");
28143 break;
28144 case RT_EXTERN:
28145 gmsgid = G_("expected %<extern%>");
28146 break;
28147 case RT_STATIC_ASSERT:
28148 gmsgid = G_("expected %<static_assert%>");
28149 break;
28150 case RT_DECLTYPE:
28151 gmsgid = G_("expected %<decltype%>");
28152 break;
28153 case RT_OPERATOR:
28154 gmsgid = G_("expected %<operator%>");
28155 break;
28156 case RT_CLASS:
28157 gmsgid = G_("expected %<class%>");
28158 break;
28159 case RT_TEMPLATE:
28160 gmsgid = G_("expected %<template%>");
28161 break;
28162 case RT_NAMESPACE:
28163 gmsgid = G_("expected %<namespace%>");
28164 break;
28165 case RT_USING:
28166 gmsgid = G_("expected %<using%>");
28167 break;
28168 case RT_ASM:
28169 gmsgid = G_("expected %<asm%>");
28170 break;
28171 case RT_TRY:
28172 gmsgid = G_("expected %<try%>");
28173 break;
28174 case RT_CATCH:
28175 gmsgid = G_("expected %<catch%>");
28176 break;
28177 case RT_THROW:
28178 gmsgid = G_("expected %<throw%>");
28179 break;
28180 case RT_LABEL:
28181 gmsgid = G_("expected %<__label__%>");
28182 break;
28183 case RT_AT_TRY:
28184 gmsgid = G_("expected %<@try%>");
28185 break;
28186 case RT_AT_SYNCHRONIZED:
28187 gmsgid = G_("expected %<@synchronized%>");
28188 break;
28189 case RT_AT_THROW:
28190 gmsgid = G_("expected %<@throw%>");
28191 break;
28192 case RT_TRANSACTION_ATOMIC:
28193 gmsgid = G_("expected %<__transaction_atomic%>");
28194 break;
28195 case RT_TRANSACTION_RELAXED:
28196 gmsgid = G_("expected %<__transaction_relaxed%>");
28197 break;
28198 default:
28199 break;
28202 if (!gmsgid && !keyword)
28204 switch (token_desc)
28206 case RT_SEMICOLON:
28207 gmsgid = G_("expected %<;%>");
28208 break;
28209 case RT_OPEN_PAREN:
28210 gmsgid = G_("expected %<(%>");
28211 break;
28212 case RT_CLOSE_BRACE:
28213 gmsgid = G_("expected %<}%>");
28214 break;
28215 case RT_OPEN_BRACE:
28216 gmsgid = G_("expected %<{%>");
28217 break;
28218 case RT_CLOSE_SQUARE:
28219 gmsgid = G_("expected %<]%>");
28220 break;
28221 case RT_OPEN_SQUARE:
28222 gmsgid = G_("expected %<[%>");
28223 break;
28224 case RT_COMMA:
28225 gmsgid = G_("expected %<,%>");
28226 break;
28227 case RT_SCOPE:
28228 gmsgid = G_("expected %<::%>");
28229 break;
28230 case RT_LESS:
28231 gmsgid = G_("expected %<<%>");
28232 break;
28233 case RT_GREATER:
28234 gmsgid = G_("expected %<>%>");
28235 break;
28236 case RT_EQ:
28237 gmsgid = G_("expected %<=%>");
28238 break;
28239 case RT_ELLIPSIS:
28240 gmsgid = G_("expected %<...%>");
28241 break;
28242 case RT_MULT:
28243 gmsgid = G_("expected %<*%>");
28244 break;
28245 case RT_COMPL:
28246 gmsgid = G_("expected %<~%>");
28247 break;
28248 case RT_COLON:
28249 gmsgid = G_("expected %<:%>");
28250 break;
28251 case RT_COLON_SCOPE:
28252 gmsgid = G_("expected %<:%> or %<::%>");
28253 break;
28254 case RT_CLOSE_PAREN:
28255 gmsgid = G_("expected %<)%>");
28256 break;
28257 case RT_COMMA_CLOSE_PAREN:
28258 gmsgid = G_("expected %<,%> or %<)%>");
28259 break;
28260 case RT_PRAGMA_EOL:
28261 gmsgid = G_("expected end of line");
28262 break;
28263 case RT_NAME:
28264 gmsgid = G_("expected identifier");
28265 break;
28266 case RT_SELECT:
28267 gmsgid = G_("expected selection-statement");
28268 break;
28269 case RT_ITERATION:
28270 gmsgid = G_("expected iteration-statement");
28271 break;
28272 case RT_JUMP:
28273 gmsgid = G_("expected jump-statement");
28274 break;
28275 case RT_CLASS_KEY:
28276 gmsgid = G_("expected class-key");
28277 break;
28278 case RT_CLASS_TYPENAME_TEMPLATE:
28279 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28280 break;
28281 default:
28282 gcc_unreachable ();
28286 if (gmsgid)
28287 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28291 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28292 issue an error message indicating that TOKEN_DESC was expected.
28294 Returns the token consumed, if the token had the appropriate type.
28295 Otherwise, returns NULL.
28297 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28298 within any error as the location of an "opening" token matching
28299 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28300 RT_CLOSE_PAREN). */
28302 static cp_token *
28303 cp_parser_require (cp_parser* parser,
28304 enum cpp_ttype type,
28305 required_token token_desc,
28306 location_t matching_location)
28308 if (cp_lexer_next_token_is (parser->lexer, type))
28309 return cp_lexer_consume_token (parser->lexer);
28310 else
28312 /* Output the MESSAGE -- unless we're parsing tentatively. */
28313 if (!cp_parser_simulate_error (parser))
28314 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28315 matching_location);
28316 return NULL;
28320 /* An error message is produced if the next token is not '>'.
28321 All further tokens are skipped until the desired token is
28322 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28324 static void
28325 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28327 /* Current level of '< ... >'. */
28328 unsigned level = 0;
28329 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28330 unsigned nesting_depth = 0;
28332 /* Are we ready, yet? If not, issue error message. */
28333 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28334 return;
28336 /* Skip tokens until the desired token is found. */
28337 while (true)
28339 /* Peek at the next token. */
28340 switch (cp_lexer_peek_token (parser->lexer)->type)
28342 case CPP_LESS:
28343 if (!nesting_depth)
28344 ++level;
28345 break;
28347 case CPP_RSHIFT:
28348 if (cxx_dialect == cxx98)
28349 /* C++0x views the `>>' operator as two `>' tokens, but
28350 C++98 does not. */
28351 break;
28352 else if (!nesting_depth && level-- == 0)
28354 /* We've hit a `>>' where the first `>' closes the
28355 template argument list, and the second `>' is
28356 spurious. Just consume the `>>' and stop; we've
28357 already produced at least one error. */
28358 cp_lexer_consume_token (parser->lexer);
28359 return;
28361 /* Fall through for C++0x, so we handle the second `>' in
28362 the `>>'. */
28363 gcc_fallthrough ();
28365 case CPP_GREATER:
28366 if (!nesting_depth && level-- == 0)
28368 /* We've reached the token we want, consume it and stop. */
28369 cp_lexer_consume_token (parser->lexer);
28370 return;
28372 break;
28374 case CPP_OPEN_PAREN:
28375 case CPP_OPEN_SQUARE:
28376 ++nesting_depth;
28377 break;
28379 case CPP_CLOSE_PAREN:
28380 case CPP_CLOSE_SQUARE:
28381 if (nesting_depth-- == 0)
28382 return;
28383 break;
28385 case CPP_EOF:
28386 case CPP_PRAGMA_EOL:
28387 case CPP_SEMICOLON:
28388 case CPP_OPEN_BRACE:
28389 case CPP_CLOSE_BRACE:
28390 /* The '>' was probably forgotten, don't look further. */
28391 return;
28393 default:
28394 break;
28397 /* Consume this token. */
28398 cp_lexer_consume_token (parser->lexer);
28402 /* If the next token is the indicated keyword, consume it. Otherwise,
28403 issue an error message indicating that TOKEN_DESC was expected.
28405 Returns the token consumed, if the token had the appropriate type.
28406 Otherwise, returns NULL. */
28408 static cp_token *
28409 cp_parser_require_keyword (cp_parser* parser,
28410 enum rid keyword,
28411 required_token token_desc)
28413 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28415 if (token && token->keyword != keyword)
28417 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28418 UNKNOWN_LOCATION);
28419 return NULL;
28422 return token;
28425 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28426 function-definition. */
28428 static bool
28429 cp_parser_token_starts_function_definition_p (cp_token* token)
28431 return (/* An ordinary function-body begins with an `{'. */
28432 token->type == CPP_OPEN_BRACE
28433 /* A ctor-initializer begins with a `:'. */
28434 || token->type == CPP_COLON
28435 /* A function-try-block begins with `try'. */
28436 || token->keyword == RID_TRY
28437 /* A function-transaction-block begins with `__transaction_atomic'
28438 or `__transaction_relaxed'. */
28439 || token->keyword == RID_TRANSACTION_ATOMIC
28440 || token->keyword == RID_TRANSACTION_RELAXED
28441 /* The named return value extension begins with `return'. */
28442 || token->keyword == RID_RETURN);
28445 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28446 definition. */
28448 static bool
28449 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28451 cp_token *token;
28453 token = cp_lexer_peek_token (parser->lexer);
28454 return (token->type == CPP_OPEN_BRACE
28455 || (token->type == CPP_COLON
28456 && !parser->colon_doesnt_start_class_def_p));
28459 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28460 C++0x) ending a template-argument. */
28462 static bool
28463 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28465 cp_token *token;
28467 token = cp_lexer_peek_token (parser->lexer);
28468 return (token->type == CPP_COMMA
28469 || token->type == CPP_GREATER
28470 || token->type == CPP_ELLIPSIS
28471 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28474 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28475 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28477 static bool
28478 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28479 size_t n)
28481 cp_token *token;
28483 token = cp_lexer_peek_nth_token (parser->lexer, n);
28484 if (token->type == CPP_LESS)
28485 return true;
28486 /* Check for the sequence `<::' in the original code. It would be lexed as
28487 `[:', where `[' is a digraph, and there is no whitespace before
28488 `:'. */
28489 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28491 cp_token *token2;
28492 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28493 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28494 return true;
28496 return false;
28499 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28500 or none_type otherwise. */
28502 static enum tag_types
28503 cp_parser_token_is_class_key (cp_token* token)
28505 switch (token->keyword)
28507 case RID_CLASS:
28508 return class_type;
28509 case RID_STRUCT:
28510 return record_type;
28511 case RID_UNION:
28512 return union_type;
28514 default:
28515 return none_type;
28519 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28520 or none_type otherwise or if the token is null. */
28522 static enum tag_types
28523 cp_parser_token_is_type_parameter_key (cp_token* token)
28525 if (!token)
28526 return none_type;
28528 switch (token->keyword)
28530 case RID_CLASS:
28531 return class_type;
28532 case RID_TYPENAME:
28533 return typename_type;
28535 default:
28536 return none_type;
28540 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28542 static void
28543 cp_parser_check_class_key (enum tag_types class_key, tree type)
28545 if (type == error_mark_node)
28546 return;
28547 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28549 if (permerror (input_location, "%qs tag used in naming %q#T",
28550 class_key == union_type ? "union"
28551 : class_key == record_type ? "struct" : "class",
28552 type))
28553 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28554 "%q#T was previously declared here", type);
28558 /* Issue an error message if DECL is redeclared with different
28559 access than its original declaration [class.access.spec/3].
28560 This applies to nested classes, nested class templates and
28561 enumerations [class.mem/1]. */
28563 static void
28564 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28566 if (!decl
28567 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28568 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28569 return;
28571 if ((TREE_PRIVATE (decl)
28572 != (current_access_specifier == access_private_node))
28573 || (TREE_PROTECTED (decl)
28574 != (current_access_specifier == access_protected_node)))
28575 error_at (location, "%qD redeclared with different access", decl);
28578 /* Look for the `template' keyword, as a syntactic disambiguator.
28579 Return TRUE iff it is present, in which case it will be
28580 consumed. */
28582 static bool
28583 cp_parser_optional_template_keyword (cp_parser *parser)
28585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28587 /* In C++98 the `template' keyword can only be used within templates;
28588 outside templates the parser can always figure out what is a
28589 template and what is not. In C++11, per the resolution of DR 468,
28590 `template' is allowed in cases where it is not strictly necessary. */
28591 if (!processing_template_decl
28592 && pedantic && cxx_dialect == cxx98)
28594 cp_token *token = cp_lexer_peek_token (parser->lexer);
28595 pedwarn (token->location, OPT_Wpedantic,
28596 "in C++98 %<template%> (as a disambiguator) is only "
28597 "allowed within templates");
28598 /* If this part of the token stream is rescanned, the same
28599 error message would be generated. So, we purge the token
28600 from the stream. */
28601 cp_lexer_purge_token (parser->lexer);
28602 return false;
28604 else
28606 /* Consume the `template' keyword. */
28607 cp_lexer_consume_token (parser->lexer);
28608 return true;
28611 return false;
28614 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28615 set PARSER->SCOPE, and perform other related actions. */
28617 static void
28618 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28620 struct tree_check *check_value;
28622 /* Get the stored value. */
28623 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28624 /* Set the scope from the stored value. */
28625 parser->scope = saved_checks_value (check_value);
28626 parser->qualifying_scope = check_value->qualifying_scope;
28627 parser->object_scope = NULL_TREE;
28630 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28631 encounter the end of a block before what we were looking for. */
28633 static bool
28634 cp_parser_cache_group (cp_parser *parser,
28635 enum cpp_ttype end,
28636 unsigned depth)
28638 while (true)
28640 cp_token *token = cp_lexer_peek_token (parser->lexer);
28642 /* Abort a parenthesized expression if we encounter a semicolon. */
28643 if ((end == CPP_CLOSE_PAREN || depth == 0)
28644 && token->type == CPP_SEMICOLON)
28645 return true;
28646 /* If we've reached the end of the file, stop. */
28647 if (token->type == CPP_EOF
28648 || (end != CPP_PRAGMA_EOL
28649 && token->type == CPP_PRAGMA_EOL))
28650 return true;
28651 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28652 /* We've hit the end of an enclosing block, so there's been some
28653 kind of syntax error. */
28654 return true;
28656 /* Consume the token. */
28657 cp_lexer_consume_token (parser->lexer);
28658 /* See if it starts a new group. */
28659 if (token->type == CPP_OPEN_BRACE)
28661 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28662 /* In theory this should probably check end == '}', but
28663 cp_parser_save_member_function_body needs it to exit
28664 after either '}' or ')' when called with ')'. */
28665 if (depth == 0)
28666 return false;
28668 else if (token->type == CPP_OPEN_PAREN)
28670 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28671 if (depth == 0 && end == CPP_CLOSE_PAREN)
28672 return false;
28674 else if (token->type == CPP_PRAGMA)
28675 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28676 else if (token->type == end)
28677 return false;
28681 /* Like above, for caching a default argument or NSDMI. Both of these are
28682 terminated by a non-nested comma, but it can be unclear whether or not a
28683 comma is nested in a template argument list unless we do more parsing.
28684 In order to handle this ambiguity, when we encounter a ',' after a '<'
28685 we try to parse what follows as a parameter-declaration-list (in the
28686 case of a default argument) or a member-declarator (in the case of an
28687 NSDMI). If that succeeds, then we stop caching. */
28689 static tree
28690 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28692 unsigned depth = 0;
28693 int maybe_template_id = 0;
28694 cp_token *first_token;
28695 cp_token *token;
28696 tree default_argument;
28698 /* Add tokens until we have processed the entire default
28699 argument. We add the range [first_token, token). */
28700 first_token = cp_lexer_peek_token (parser->lexer);
28701 if (first_token->type == CPP_OPEN_BRACE)
28703 /* For list-initialization, this is straightforward. */
28704 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28705 token = cp_lexer_peek_token (parser->lexer);
28707 else while (true)
28709 bool done = false;
28711 /* Peek at the next token. */
28712 token = cp_lexer_peek_token (parser->lexer);
28713 /* What we do depends on what token we have. */
28714 switch (token->type)
28716 /* In valid code, a default argument must be
28717 immediately followed by a `,' `)', or `...'. */
28718 case CPP_COMMA:
28719 if (depth == 0 && maybe_template_id)
28721 /* If we've seen a '<', we might be in a
28722 template-argument-list. Until Core issue 325 is
28723 resolved, we don't know how this situation ought
28724 to be handled, so try to DTRT. We check whether
28725 what comes after the comma is a valid parameter
28726 declaration list. If it is, then the comma ends
28727 the default argument; otherwise the default
28728 argument continues. */
28729 bool error = false;
28730 cp_token *peek;
28732 /* Set ITALP so cp_parser_parameter_declaration_list
28733 doesn't decide to commit to this parse. */
28734 bool saved_italp = parser->in_template_argument_list_p;
28735 parser->in_template_argument_list_p = true;
28737 cp_parser_parse_tentatively (parser);
28739 if (nsdmi)
28741 /* Parse declarators until we reach a non-comma or
28742 somthing that cannot be an initializer.
28743 Just checking whether we're looking at a single
28744 declarator is insufficient. Consider:
28745 int var = tuple<T,U>::x;
28746 The template parameter 'U' looks exactly like a
28747 declarator. */
28750 int ctor_dtor_or_conv_p;
28751 cp_lexer_consume_token (parser->lexer);
28752 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28753 &ctor_dtor_or_conv_p,
28754 /*parenthesized_p=*/NULL,
28755 /*member_p=*/true,
28756 /*friend_p=*/false);
28757 peek = cp_lexer_peek_token (parser->lexer);
28758 if (cp_parser_error_occurred (parser))
28759 break;
28761 while (peek->type == CPP_COMMA);
28762 /* If we met an '=' or ';' then the original comma
28763 was the end of the NSDMI. Otherwise assume
28764 we're still in the NSDMI. */
28765 error = (peek->type != CPP_EQ
28766 && peek->type != CPP_SEMICOLON);
28768 else
28770 cp_lexer_consume_token (parser->lexer);
28771 begin_scope (sk_function_parms, NULL_TREE);
28772 cp_parser_parameter_declaration_list (parser, &error);
28773 pop_bindings_and_leave_scope ();
28775 if (!cp_parser_error_occurred (parser) && !error)
28776 done = true;
28777 cp_parser_abort_tentative_parse (parser);
28779 parser->in_template_argument_list_p = saved_italp;
28780 break;
28782 /* FALLTHRU */
28783 case CPP_CLOSE_PAREN:
28784 case CPP_ELLIPSIS:
28785 /* If we run into a non-nested `;', `}', or `]',
28786 then the code is invalid -- but the default
28787 argument is certainly over. */
28788 case CPP_SEMICOLON:
28789 case CPP_CLOSE_BRACE:
28790 case CPP_CLOSE_SQUARE:
28791 if (depth == 0
28792 /* Handle correctly int n = sizeof ... ( p ); */
28793 && token->type != CPP_ELLIPSIS)
28794 done = true;
28795 /* Update DEPTH, if necessary. */
28796 else if (token->type == CPP_CLOSE_PAREN
28797 || token->type == CPP_CLOSE_BRACE
28798 || token->type == CPP_CLOSE_SQUARE)
28799 --depth;
28800 break;
28802 case CPP_OPEN_PAREN:
28803 case CPP_OPEN_SQUARE:
28804 case CPP_OPEN_BRACE:
28805 ++depth;
28806 break;
28808 case CPP_LESS:
28809 if (depth == 0)
28810 /* This might be the comparison operator, or it might
28811 start a template argument list. */
28812 ++maybe_template_id;
28813 break;
28815 case CPP_RSHIFT:
28816 if (cxx_dialect == cxx98)
28817 break;
28818 /* Fall through for C++0x, which treats the `>>'
28819 operator like two `>' tokens in certain
28820 cases. */
28821 gcc_fallthrough ();
28823 case CPP_GREATER:
28824 if (depth == 0)
28826 /* This might be an operator, or it might close a
28827 template argument list. But if a previous '<'
28828 started a template argument list, this will have
28829 closed it, so we can't be in one anymore. */
28830 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28831 if (maybe_template_id < 0)
28832 maybe_template_id = 0;
28834 break;
28836 /* If we run out of tokens, issue an error message. */
28837 case CPP_EOF:
28838 case CPP_PRAGMA_EOL:
28839 error_at (token->location, "file ends in default argument");
28840 return error_mark_node;
28842 case CPP_NAME:
28843 case CPP_SCOPE:
28844 /* In these cases, we should look for template-ids.
28845 For example, if the default argument is
28846 `X<int, double>()', we need to do name lookup to
28847 figure out whether or not `X' is a template; if
28848 so, the `,' does not end the default argument.
28850 That is not yet done. */
28851 break;
28853 default:
28854 break;
28857 /* If we've reached the end, stop. */
28858 if (done)
28859 break;
28861 /* Add the token to the token block. */
28862 token = cp_lexer_consume_token (parser->lexer);
28865 /* Create a DEFAULT_ARG to represent the unparsed default
28866 argument. */
28867 default_argument = make_node (DEFAULT_ARG);
28868 DEFARG_TOKENS (default_argument)
28869 = cp_token_cache_new (first_token, token);
28870 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28872 return default_argument;
28875 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28877 location_t
28878 defarg_location (tree default_argument)
28880 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28881 location_t start = tokens->first->location;
28882 location_t end = tokens->last->location;
28883 return make_location (start, start, end);
28886 /* Begin parsing tentatively. We always save tokens while parsing
28887 tentatively so that if the tentative parsing fails we can restore the
28888 tokens. */
28890 static void
28891 cp_parser_parse_tentatively (cp_parser* parser)
28893 /* Enter a new parsing context. */
28894 parser->context = cp_parser_context_new (parser->context);
28895 /* Begin saving tokens. */
28896 cp_lexer_save_tokens (parser->lexer);
28897 /* In order to avoid repetitive access control error messages,
28898 access checks are queued up until we are no longer parsing
28899 tentatively. */
28900 push_deferring_access_checks (dk_deferred);
28903 /* Commit to the currently active tentative parse. */
28905 static void
28906 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28908 cp_parser_context *context;
28909 cp_lexer *lexer;
28911 /* Mark all of the levels as committed. */
28912 lexer = parser->lexer;
28913 for (context = parser->context; context->next; context = context->next)
28915 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28916 break;
28917 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28918 while (!cp_lexer_saving_tokens (lexer))
28919 lexer = lexer->next;
28920 cp_lexer_commit_tokens (lexer);
28924 /* Commit to the topmost currently active tentative parse.
28926 Note that this function shouldn't be called when there are
28927 irreversible side-effects while in a tentative state. For
28928 example, we shouldn't create a permanent entry in the symbol
28929 table, or issue an error message that might not apply if the
28930 tentative parse is aborted. */
28932 static void
28933 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28935 cp_parser_context *context = parser->context;
28936 cp_lexer *lexer = parser->lexer;
28938 if (context)
28940 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28941 return;
28942 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28944 while (!cp_lexer_saving_tokens (lexer))
28945 lexer = lexer->next;
28946 cp_lexer_commit_tokens (lexer);
28950 /* Abort the currently active tentative parse. All consumed tokens
28951 will be rolled back, and no diagnostics will be issued. */
28953 static void
28954 cp_parser_abort_tentative_parse (cp_parser* parser)
28956 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28957 || errorcount > 0);
28958 cp_parser_simulate_error (parser);
28959 /* Now, pretend that we want to see if the construct was
28960 successfully parsed. */
28961 cp_parser_parse_definitely (parser);
28964 /* Stop parsing tentatively. If a parse error has occurred, restore the
28965 token stream. Otherwise, commit to the tokens we have consumed.
28966 Returns true if no error occurred; false otherwise. */
28968 static bool
28969 cp_parser_parse_definitely (cp_parser* parser)
28971 bool error_occurred;
28972 cp_parser_context *context;
28974 /* Remember whether or not an error occurred, since we are about to
28975 destroy that information. */
28976 error_occurred = cp_parser_error_occurred (parser);
28977 /* Remove the topmost context from the stack. */
28978 context = parser->context;
28979 parser->context = context->next;
28980 /* If no parse errors occurred, commit to the tentative parse. */
28981 if (!error_occurred)
28983 /* Commit to the tokens read tentatively, unless that was
28984 already done. */
28985 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28986 cp_lexer_commit_tokens (parser->lexer);
28988 pop_to_parent_deferring_access_checks ();
28990 /* Otherwise, if errors occurred, roll back our state so that things
28991 are just as they were before we began the tentative parse. */
28992 else
28994 cp_lexer_rollback_tokens (parser->lexer);
28995 pop_deferring_access_checks ();
28997 /* Add the context to the front of the free list. */
28998 context->next = cp_parser_context_free_list;
28999 cp_parser_context_free_list = context;
29001 return !error_occurred;
29004 /* Returns true if we are parsing tentatively and are not committed to
29005 this tentative parse. */
29007 static bool
29008 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29010 return (cp_parser_parsing_tentatively (parser)
29011 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29014 /* Returns nonzero iff an error has occurred during the most recent
29015 tentative parse. */
29017 static bool
29018 cp_parser_error_occurred (cp_parser* parser)
29020 return (cp_parser_parsing_tentatively (parser)
29021 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29024 /* Returns nonzero if GNU extensions are allowed. */
29026 static bool
29027 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29029 return parser->allow_gnu_extensions_p;
29032 /* Objective-C++ Productions */
29035 /* Parse an Objective-C expression, which feeds into a primary-expression
29036 above.
29038 objc-expression:
29039 objc-message-expression
29040 objc-string-literal
29041 objc-encode-expression
29042 objc-protocol-expression
29043 objc-selector-expression
29045 Returns a tree representation of the expression. */
29047 static cp_expr
29048 cp_parser_objc_expression (cp_parser* parser)
29050 /* Try to figure out what kind of declaration is present. */
29051 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29053 switch (kwd->type)
29055 case CPP_OPEN_SQUARE:
29056 return cp_parser_objc_message_expression (parser);
29058 case CPP_OBJC_STRING:
29059 kwd = cp_lexer_consume_token (parser->lexer);
29060 return objc_build_string_object (kwd->u.value);
29062 case CPP_KEYWORD:
29063 switch (kwd->keyword)
29065 case RID_AT_ENCODE:
29066 return cp_parser_objc_encode_expression (parser);
29068 case RID_AT_PROTOCOL:
29069 return cp_parser_objc_protocol_expression (parser);
29071 case RID_AT_SELECTOR:
29072 return cp_parser_objc_selector_expression (parser);
29074 default:
29075 break;
29077 /* FALLTHRU */
29078 default:
29079 error_at (kwd->location,
29080 "misplaced %<@%D%> Objective-C++ construct",
29081 kwd->u.value);
29082 cp_parser_skip_to_end_of_block_or_statement (parser);
29085 return error_mark_node;
29088 /* Parse an Objective-C message expression.
29090 objc-message-expression:
29091 [ objc-message-receiver objc-message-args ]
29093 Returns a representation of an Objective-C message. */
29095 static tree
29096 cp_parser_objc_message_expression (cp_parser* parser)
29098 tree receiver, messageargs;
29100 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29101 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29102 receiver = cp_parser_objc_message_receiver (parser);
29103 messageargs = cp_parser_objc_message_args (parser);
29104 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29105 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29107 tree result = objc_build_message_expr (receiver, messageargs);
29109 /* Construct a location e.g.
29110 [self func1:5]
29111 ^~~~~~~~~~~~~~
29112 ranging from the '[' to the ']', with the caret at the start. */
29113 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29114 protected_set_expr_location (result, combined_loc);
29116 return result;
29119 /* Parse an objc-message-receiver.
29121 objc-message-receiver:
29122 expression
29123 simple-type-specifier
29125 Returns a representation of the type or expression. */
29127 static tree
29128 cp_parser_objc_message_receiver (cp_parser* parser)
29130 tree rcv;
29132 /* An Objective-C message receiver may be either (1) a type
29133 or (2) an expression. */
29134 cp_parser_parse_tentatively (parser);
29135 rcv = cp_parser_expression (parser);
29137 /* If that worked out, fine. */
29138 if (cp_parser_parse_definitely (parser))
29139 return rcv;
29141 cp_parser_parse_tentatively (parser);
29142 rcv = cp_parser_simple_type_specifier (parser,
29143 /*decl_specs=*/NULL,
29144 CP_PARSER_FLAGS_NONE);
29146 if (cp_parser_parse_definitely (parser))
29147 return objc_get_class_reference (rcv);
29149 cp_parser_error (parser, "objective-c++ message receiver expected");
29150 return error_mark_node;
29153 /* Parse the arguments and selectors comprising an Objective-C message.
29155 objc-message-args:
29156 objc-selector
29157 objc-selector-args
29158 objc-selector-args , objc-comma-args
29160 objc-selector-args:
29161 objc-selector [opt] : assignment-expression
29162 objc-selector-args objc-selector [opt] : assignment-expression
29164 objc-comma-args:
29165 assignment-expression
29166 objc-comma-args , assignment-expression
29168 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29169 selector arguments and TREE_VALUE containing a list of comma
29170 arguments. */
29172 static tree
29173 cp_parser_objc_message_args (cp_parser* parser)
29175 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29176 bool maybe_unary_selector_p = true;
29177 cp_token *token = cp_lexer_peek_token (parser->lexer);
29179 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29181 tree selector = NULL_TREE, arg;
29183 if (token->type != CPP_COLON)
29184 selector = cp_parser_objc_selector (parser);
29186 /* Detect if we have a unary selector. */
29187 if (maybe_unary_selector_p
29188 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29189 return build_tree_list (selector, NULL_TREE);
29191 maybe_unary_selector_p = false;
29192 cp_parser_require (parser, CPP_COLON, RT_COLON);
29193 arg = cp_parser_assignment_expression (parser);
29195 sel_args
29196 = chainon (sel_args,
29197 build_tree_list (selector, arg));
29199 token = cp_lexer_peek_token (parser->lexer);
29202 /* Handle non-selector arguments, if any. */
29203 while (token->type == CPP_COMMA)
29205 tree arg;
29207 cp_lexer_consume_token (parser->lexer);
29208 arg = cp_parser_assignment_expression (parser);
29210 addl_args
29211 = chainon (addl_args,
29212 build_tree_list (NULL_TREE, arg));
29214 token = cp_lexer_peek_token (parser->lexer);
29217 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29219 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29220 return build_tree_list (error_mark_node, error_mark_node);
29223 return build_tree_list (sel_args, addl_args);
29226 /* Parse an Objective-C encode expression.
29228 objc-encode-expression:
29229 @encode objc-typename
29231 Returns an encoded representation of the type argument. */
29233 static cp_expr
29234 cp_parser_objc_encode_expression (cp_parser* parser)
29236 tree type;
29237 cp_token *token;
29238 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29240 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29241 matching_parens parens;
29242 parens.require_open (parser);
29243 token = cp_lexer_peek_token (parser->lexer);
29244 type = complete_type (cp_parser_type_id (parser));
29245 parens.require_close (parser);
29247 if (!type)
29249 error_at (token->location,
29250 "%<@encode%> must specify a type as an argument");
29251 return error_mark_node;
29254 /* This happens if we find @encode(T) (where T is a template
29255 typename or something dependent on a template typename) when
29256 parsing a template. In that case, we can't compile it
29257 immediately, but we rather create an AT_ENCODE_EXPR which will
29258 need to be instantiated when the template is used.
29260 if (dependent_type_p (type))
29262 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29263 TREE_READONLY (value) = 1;
29264 return value;
29268 /* Build a location of the form:
29269 @encode(int)
29270 ^~~~~~~~~~~~
29271 with caret==start at the @ token, finishing at the close paren. */
29272 location_t combined_loc
29273 = make_location (start_loc, start_loc,
29274 cp_lexer_previous_token (parser->lexer)->location);
29276 return cp_expr (objc_build_encode_expr (type), combined_loc);
29279 /* Parse an Objective-C @defs expression. */
29281 static tree
29282 cp_parser_objc_defs_expression (cp_parser *parser)
29284 tree name;
29286 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29287 matching_parens parens;
29288 parens.require_open (parser);
29289 name = cp_parser_identifier (parser);
29290 parens.require_close (parser);
29292 return objc_get_class_ivars (name);
29295 /* Parse an Objective-C protocol expression.
29297 objc-protocol-expression:
29298 @protocol ( identifier )
29300 Returns a representation of the protocol expression. */
29302 static tree
29303 cp_parser_objc_protocol_expression (cp_parser* parser)
29305 tree proto;
29306 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29308 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29309 matching_parens parens;
29310 parens.require_open (parser);
29311 proto = cp_parser_identifier (parser);
29312 parens.require_close (parser);
29314 /* Build a location of the form:
29315 @protocol(prot)
29316 ^~~~~~~~~~~~~~~
29317 with caret==start at the @ token, finishing at the close paren. */
29318 location_t combined_loc
29319 = make_location (start_loc, start_loc,
29320 cp_lexer_previous_token (parser->lexer)->location);
29321 tree result = objc_build_protocol_expr (proto);
29322 protected_set_expr_location (result, combined_loc);
29323 return result;
29326 /* Parse an Objective-C selector expression.
29328 objc-selector-expression:
29329 @selector ( objc-method-signature )
29331 objc-method-signature:
29332 objc-selector
29333 objc-selector-seq
29335 objc-selector-seq:
29336 objc-selector :
29337 objc-selector-seq objc-selector :
29339 Returns a representation of the method selector. */
29341 static tree
29342 cp_parser_objc_selector_expression (cp_parser* parser)
29344 tree sel_seq = NULL_TREE;
29345 bool maybe_unary_selector_p = true;
29346 cp_token *token;
29347 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29349 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29350 matching_parens parens;
29351 parens.require_open (parser);
29352 token = cp_lexer_peek_token (parser->lexer);
29354 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29355 || token->type == CPP_SCOPE)
29357 tree selector = NULL_TREE;
29359 if (token->type != CPP_COLON
29360 || token->type == CPP_SCOPE)
29361 selector = cp_parser_objc_selector (parser);
29363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29364 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29366 /* Detect if we have a unary selector. */
29367 if (maybe_unary_selector_p)
29369 sel_seq = selector;
29370 goto finish_selector;
29372 else
29374 cp_parser_error (parser, "expected %<:%>");
29377 maybe_unary_selector_p = false;
29378 token = cp_lexer_consume_token (parser->lexer);
29380 if (token->type == CPP_SCOPE)
29382 sel_seq
29383 = chainon (sel_seq,
29384 build_tree_list (selector, NULL_TREE));
29385 sel_seq
29386 = chainon (sel_seq,
29387 build_tree_list (NULL_TREE, NULL_TREE));
29389 else
29390 sel_seq
29391 = chainon (sel_seq,
29392 build_tree_list (selector, NULL_TREE));
29394 token = cp_lexer_peek_token (parser->lexer);
29397 finish_selector:
29398 parens.require_close (parser);
29401 /* Build a location of the form:
29402 @selector(func)
29403 ^~~~~~~~~~~~~~~
29404 with caret==start at the @ token, finishing at the close paren. */
29405 location_t combined_loc
29406 = make_location (loc, loc,
29407 cp_lexer_previous_token (parser->lexer)->location);
29408 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29409 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29410 protected_set_expr_location (result, combined_loc);
29411 return result;
29414 /* Parse a list of identifiers.
29416 objc-identifier-list:
29417 identifier
29418 objc-identifier-list , identifier
29420 Returns a TREE_LIST of identifier nodes. */
29422 static tree
29423 cp_parser_objc_identifier_list (cp_parser* parser)
29425 tree identifier;
29426 tree list;
29427 cp_token *sep;
29429 identifier = cp_parser_identifier (parser);
29430 if (identifier == error_mark_node)
29431 return error_mark_node;
29433 list = build_tree_list (NULL_TREE, identifier);
29434 sep = cp_lexer_peek_token (parser->lexer);
29436 while (sep->type == CPP_COMMA)
29438 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29439 identifier = cp_parser_identifier (parser);
29440 if (identifier == error_mark_node)
29441 return list;
29443 list = chainon (list, build_tree_list (NULL_TREE,
29444 identifier));
29445 sep = cp_lexer_peek_token (parser->lexer);
29448 return list;
29451 /* Parse an Objective-C alias declaration.
29453 objc-alias-declaration:
29454 @compatibility_alias identifier identifier ;
29456 This function registers the alias mapping with the Objective-C front end.
29457 It returns nothing. */
29459 static void
29460 cp_parser_objc_alias_declaration (cp_parser* parser)
29462 tree alias, orig;
29464 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29465 alias = cp_parser_identifier (parser);
29466 orig = cp_parser_identifier (parser);
29467 objc_declare_alias (alias, orig);
29468 cp_parser_consume_semicolon_at_end_of_statement (parser);
29471 /* Parse an Objective-C class forward-declaration.
29473 objc-class-declaration:
29474 @class objc-identifier-list ;
29476 The function registers the forward declarations with the Objective-C
29477 front end. It returns nothing. */
29479 static void
29480 cp_parser_objc_class_declaration (cp_parser* parser)
29482 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29483 while (true)
29485 tree id;
29487 id = cp_parser_identifier (parser);
29488 if (id == error_mark_node)
29489 break;
29491 objc_declare_class (id);
29493 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29494 cp_lexer_consume_token (parser->lexer);
29495 else
29496 break;
29498 cp_parser_consume_semicolon_at_end_of_statement (parser);
29501 /* Parse a list of Objective-C protocol references.
29503 objc-protocol-refs-opt:
29504 objc-protocol-refs [opt]
29506 objc-protocol-refs:
29507 < objc-identifier-list >
29509 Returns a TREE_LIST of identifiers, if any. */
29511 static tree
29512 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29514 tree protorefs = NULL_TREE;
29516 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29518 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29519 protorefs = cp_parser_objc_identifier_list (parser);
29520 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29523 return protorefs;
29526 /* Parse a Objective-C visibility specification. */
29528 static void
29529 cp_parser_objc_visibility_spec (cp_parser* parser)
29531 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29533 switch (vis->keyword)
29535 case RID_AT_PRIVATE:
29536 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29537 break;
29538 case RID_AT_PROTECTED:
29539 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29540 break;
29541 case RID_AT_PUBLIC:
29542 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29543 break;
29544 case RID_AT_PACKAGE:
29545 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29546 break;
29547 default:
29548 return;
29551 /* Eat '@private'/'@protected'/'@public'. */
29552 cp_lexer_consume_token (parser->lexer);
29555 /* Parse an Objective-C method type. Return 'true' if it is a class
29556 (+) method, and 'false' if it is an instance (-) method. */
29558 static inline bool
29559 cp_parser_objc_method_type (cp_parser* parser)
29561 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29562 return true;
29563 else
29564 return false;
29567 /* Parse an Objective-C protocol qualifier. */
29569 static tree
29570 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29572 tree quals = NULL_TREE, node;
29573 cp_token *token = cp_lexer_peek_token (parser->lexer);
29575 node = token->u.value;
29577 while (node && identifier_p (node)
29578 && (node == ridpointers [(int) RID_IN]
29579 || node == ridpointers [(int) RID_OUT]
29580 || node == ridpointers [(int) RID_INOUT]
29581 || node == ridpointers [(int) RID_BYCOPY]
29582 || node == ridpointers [(int) RID_BYREF]
29583 || node == ridpointers [(int) RID_ONEWAY]))
29585 quals = tree_cons (NULL_TREE, node, quals);
29586 cp_lexer_consume_token (parser->lexer);
29587 token = cp_lexer_peek_token (parser->lexer);
29588 node = token->u.value;
29591 return quals;
29594 /* Parse an Objective-C typename. */
29596 static tree
29597 cp_parser_objc_typename (cp_parser* parser)
29599 tree type_name = NULL_TREE;
29601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29603 tree proto_quals, cp_type = NULL_TREE;
29605 matching_parens parens;
29606 parens.consume_open (parser); /* Eat '('. */
29607 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29609 /* An ObjC type name may consist of just protocol qualifiers, in which
29610 case the type shall default to 'id'. */
29611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29613 cp_type = cp_parser_type_id (parser);
29615 /* If the type could not be parsed, an error has already
29616 been produced. For error recovery, behave as if it had
29617 not been specified, which will use the default type
29618 'id'. */
29619 if (cp_type == error_mark_node)
29621 cp_type = NULL_TREE;
29622 /* We need to skip to the closing parenthesis as
29623 cp_parser_type_id() does not seem to do it for
29624 us. */
29625 cp_parser_skip_to_closing_parenthesis (parser,
29626 /*recovering=*/true,
29627 /*or_comma=*/false,
29628 /*consume_paren=*/false);
29632 parens.require_close (parser);
29633 type_name = build_tree_list (proto_quals, cp_type);
29636 return type_name;
29639 /* Check to see if TYPE refers to an Objective-C selector name. */
29641 static bool
29642 cp_parser_objc_selector_p (enum cpp_ttype type)
29644 return (type == CPP_NAME || type == CPP_KEYWORD
29645 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29646 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29647 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29648 || type == CPP_XOR || type == CPP_XOR_EQ);
29651 /* Parse an Objective-C selector. */
29653 static tree
29654 cp_parser_objc_selector (cp_parser* parser)
29656 cp_token *token = cp_lexer_consume_token (parser->lexer);
29658 if (!cp_parser_objc_selector_p (token->type))
29660 error_at (token->location, "invalid Objective-C++ selector name");
29661 return error_mark_node;
29664 /* C++ operator names are allowed to appear in ObjC selectors. */
29665 switch (token->type)
29667 case CPP_AND_AND: return get_identifier ("and");
29668 case CPP_AND_EQ: return get_identifier ("and_eq");
29669 case CPP_AND: return get_identifier ("bitand");
29670 case CPP_OR: return get_identifier ("bitor");
29671 case CPP_COMPL: return get_identifier ("compl");
29672 case CPP_NOT: return get_identifier ("not");
29673 case CPP_NOT_EQ: return get_identifier ("not_eq");
29674 case CPP_OR_OR: return get_identifier ("or");
29675 case CPP_OR_EQ: return get_identifier ("or_eq");
29676 case CPP_XOR: return get_identifier ("xor");
29677 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29678 default: return token->u.value;
29682 /* Parse an Objective-C params list. */
29684 static tree
29685 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29687 tree params = NULL_TREE;
29688 bool maybe_unary_selector_p = true;
29689 cp_token *token = cp_lexer_peek_token (parser->lexer);
29691 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29693 tree selector = NULL_TREE, type_name, identifier;
29694 tree parm_attr = NULL_TREE;
29696 if (token->keyword == RID_ATTRIBUTE)
29697 break;
29699 if (token->type != CPP_COLON)
29700 selector = cp_parser_objc_selector (parser);
29702 /* Detect if we have a unary selector. */
29703 if (maybe_unary_selector_p
29704 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29706 params = selector; /* Might be followed by attributes. */
29707 break;
29710 maybe_unary_selector_p = false;
29711 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29713 /* Something went quite wrong. There should be a colon
29714 here, but there is not. Stop parsing parameters. */
29715 break;
29717 type_name = cp_parser_objc_typename (parser);
29718 /* New ObjC allows attributes on parameters too. */
29719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29720 parm_attr = cp_parser_attributes_opt (parser);
29721 identifier = cp_parser_identifier (parser);
29723 params
29724 = chainon (params,
29725 objc_build_keyword_decl (selector,
29726 type_name,
29727 identifier,
29728 parm_attr));
29730 token = cp_lexer_peek_token (parser->lexer);
29733 if (params == NULL_TREE)
29735 cp_parser_error (parser, "objective-c++ method declaration is expected");
29736 return error_mark_node;
29739 /* We allow tail attributes for the method. */
29740 if (token->keyword == RID_ATTRIBUTE)
29742 *attributes = cp_parser_attributes_opt (parser);
29743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29744 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29745 return params;
29746 cp_parser_error (parser,
29747 "method attributes must be specified at the end");
29748 return error_mark_node;
29751 if (params == NULL_TREE)
29753 cp_parser_error (parser, "objective-c++ method declaration is expected");
29754 return error_mark_node;
29756 return params;
29759 /* Parse the non-keyword Objective-C params. */
29761 static tree
29762 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29763 tree* attributes)
29765 tree params = make_node (TREE_LIST);
29766 cp_token *token = cp_lexer_peek_token (parser->lexer);
29767 *ellipsisp = false; /* Initially, assume no ellipsis. */
29769 while (token->type == CPP_COMMA)
29771 cp_parameter_declarator *parmdecl;
29772 tree parm;
29774 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29775 token = cp_lexer_peek_token (parser->lexer);
29777 if (token->type == CPP_ELLIPSIS)
29779 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29780 *ellipsisp = true;
29781 token = cp_lexer_peek_token (parser->lexer);
29782 break;
29785 /* TODO: parse attributes for tail parameters. */
29786 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29787 parm = grokdeclarator (parmdecl->declarator,
29788 &parmdecl->decl_specifiers,
29789 PARM, /*initialized=*/0,
29790 /*attrlist=*/NULL);
29792 chainon (params, build_tree_list (NULL_TREE, parm));
29793 token = cp_lexer_peek_token (parser->lexer);
29796 /* We allow tail attributes for the method. */
29797 if (token->keyword == RID_ATTRIBUTE)
29799 if (*attributes == NULL_TREE)
29801 *attributes = cp_parser_attributes_opt (parser);
29802 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29803 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29804 return params;
29806 else
29807 /* We have an error, but parse the attributes, so that we can
29808 carry on. */
29809 *attributes = cp_parser_attributes_opt (parser);
29811 cp_parser_error (parser,
29812 "method attributes must be specified at the end");
29813 return error_mark_node;
29816 return params;
29819 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29821 static void
29822 cp_parser_objc_interstitial_code (cp_parser* parser)
29824 cp_token *token = cp_lexer_peek_token (parser->lexer);
29826 /* If the next token is `extern' and the following token is a string
29827 literal, then we have a linkage specification. */
29828 if (token->keyword == RID_EXTERN
29829 && cp_parser_is_pure_string_literal
29830 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29831 cp_parser_linkage_specification (parser);
29832 /* Handle #pragma, if any. */
29833 else if (token->type == CPP_PRAGMA)
29834 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29835 /* Allow stray semicolons. */
29836 else if (token->type == CPP_SEMICOLON)
29837 cp_lexer_consume_token (parser->lexer);
29838 /* Mark methods as optional or required, when building protocols. */
29839 else if (token->keyword == RID_AT_OPTIONAL)
29841 cp_lexer_consume_token (parser->lexer);
29842 objc_set_method_opt (true);
29844 else if (token->keyword == RID_AT_REQUIRED)
29846 cp_lexer_consume_token (parser->lexer);
29847 objc_set_method_opt (false);
29849 else if (token->keyword == RID_NAMESPACE)
29850 cp_parser_namespace_definition (parser);
29851 /* Other stray characters must generate errors. */
29852 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29854 cp_lexer_consume_token (parser->lexer);
29855 error ("stray %qs between Objective-C++ methods",
29856 token->type == CPP_OPEN_BRACE ? "{" : "}");
29858 /* Finally, try to parse a block-declaration, or a function-definition. */
29859 else
29860 cp_parser_block_declaration (parser, /*statement_p=*/false);
29863 /* Parse a method signature. */
29865 static tree
29866 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29868 tree rettype, kwdparms, optparms;
29869 bool ellipsis = false;
29870 bool is_class_method;
29872 is_class_method = cp_parser_objc_method_type (parser);
29873 rettype = cp_parser_objc_typename (parser);
29874 *attributes = NULL_TREE;
29875 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29876 if (kwdparms == error_mark_node)
29877 return error_mark_node;
29878 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29879 if (optparms == error_mark_node)
29880 return error_mark_node;
29882 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29885 static bool
29886 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29888 tree tattr;
29889 cp_lexer_save_tokens (parser->lexer);
29890 tattr = cp_parser_attributes_opt (parser);
29891 gcc_assert (tattr) ;
29893 /* If the attributes are followed by a method introducer, this is not allowed.
29894 Dump the attributes and flag the situation. */
29895 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29896 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29897 return true;
29899 /* Otherwise, the attributes introduce some interstitial code, possibly so
29900 rewind to allow that check. */
29901 cp_lexer_rollback_tokens (parser->lexer);
29902 return false;
29905 /* Parse an Objective-C method prototype list. */
29907 static void
29908 cp_parser_objc_method_prototype_list (cp_parser* parser)
29910 cp_token *token = cp_lexer_peek_token (parser->lexer);
29912 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29914 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29916 tree attributes, sig;
29917 bool is_class_method;
29918 if (token->type == CPP_PLUS)
29919 is_class_method = true;
29920 else
29921 is_class_method = false;
29922 sig = cp_parser_objc_method_signature (parser, &attributes);
29923 if (sig == error_mark_node)
29925 cp_parser_skip_to_end_of_block_or_statement (parser);
29926 token = cp_lexer_peek_token (parser->lexer);
29927 continue;
29929 objc_add_method_declaration (is_class_method, sig, attributes);
29930 cp_parser_consume_semicolon_at_end_of_statement (parser);
29932 else if (token->keyword == RID_AT_PROPERTY)
29933 cp_parser_objc_at_property_declaration (parser);
29934 else if (token->keyword == RID_ATTRIBUTE
29935 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29936 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29937 OPT_Wattributes,
29938 "prefix attributes are ignored for methods");
29939 else
29940 /* Allow for interspersed non-ObjC++ code. */
29941 cp_parser_objc_interstitial_code (parser);
29943 token = cp_lexer_peek_token (parser->lexer);
29946 if (token->type != CPP_EOF)
29947 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29948 else
29949 cp_parser_error (parser, "expected %<@end%>");
29951 objc_finish_interface ();
29954 /* Parse an Objective-C method definition list. */
29956 static void
29957 cp_parser_objc_method_definition_list (cp_parser* parser)
29959 cp_token *token = cp_lexer_peek_token (parser->lexer);
29961 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29963 tree meth;
29965 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29967 cp_token *ptk;
29968 tree sig, attribute;
29969 bool is_class_method;
29970 if (token->type == CPP_PLUS)
29971 is_class_method = true;
29972 else
29973 is_class_method = false;
29974 push_deferring_access_checks (dk_deferred);
29975 sig = cp_parser_objc_method_signature (parser, &attribute);
29976 if (sig == error_mark_node)
29978 cp_parser_skip_to_end_of_block_or_statement (parser);
29979 token = cp_lexer_peek_token (parser->lexer);
29980 continue;
29982 objc_start_method_definition (is_class_method, sig, attribute,
29983 NULL_TREE);
29985 /* For historical reasons, we accept an optional semicolon. */
29986 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29987 cp_lexer_consume_token (parser->lexer);
29989 ptk = cp_lexer_peek_token (parser->lexer);
29990 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29991 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29993 perform_deferred_access_checks (tf_warning_or_error);
29994 stop_deferring_access_checks ();
29995 meth = cp_parser_function_definition_after_declarator (parser,
29996 false);
29997 pop_deferring_access_checks ();
29998 objc_finish_method_definition (meth);
30001 /* The following case will be removed once @synthesize is
30002 completely implemented. */
30003 else if (token->keyword == RID_AT_PROPERTY)
30004 cp_parser_objc_at_property_declaration (parser);
30005 else if (token->keyword == RID_AT_SYNTHESIZE)
30006 cp_parser_objc_at_synthesize_declaration (parser);
30007 else if (token->keyword == RID_AT_DYNAMIC)
30008 cp_parser_objc_at_dynamic_declaration (parser);
30009 else if (token->keyword == RID_ATTRIBUTE
30010 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30011 warning_at (token->location, OPT_Wattributes,
30012 "prefix attributes are ignored for methods");
30013 else
30014 /* Allow for interspersed non-ObjC++ code. */
30015 cp_parser_objc_interstitial_code (parser);
30017 token = cp_lexer_peek_token (parser->lexer);
30020 if (token->type != CPP_EOF)
30021 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30022 else
30023 cp_parser_error (parser, "expected %<@end%>");
30025 objc_finish_implementation ();
30028 /* Parse Objective-C ivars. */
30030 static void
30031 cp_parser_objc_class_ivars (cp_parser* parser)
30033 cp_token *token = cp_lexer_peek_token (parser->lexer);
30035 if (token->type != CPP_OPEN_BRACE)
30036 return; /* No ivars specified. */
30038 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30039 token = cp_lexer_peek_token (parser->lexer);
30041 while (token->type != CPP_CLOSE_BRACE
30042 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30044 cp_decl_specifier_seq declspecs;
30045 int decl_class_or_enum_p;
30046 tree prefix_attributes;
30048 cp_parser_objc_visibility_spec (parser);
30050 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30051 break;
30053 cp_parser_decl_specifier_seq (parser,
30054 CP_PARSER_FLAGS_OPTIONAL,
30055 &declspecs,
30056 &decl_class_or_enum_p);
30058 /* auto, register, static, extern, mutable. */
30059 if (declspecs.storage_class != sc_none)
30061 cp_parser_error (parser, "invalid type for instance variable");
30062 declspecs.storage_class = sc_none;
30065 /* thread_local. */
30066 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30068 cp_parser_error (parser, "invalid type for instance variable");
30069 declspecs.locations[ds_thread] = 0;
30072 /* typedef. */
30073 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30075 cp_parser_error (parser, "invalid type for instance variable");
30076 declspecs.locations[ds_typedef] = 0;
30079 prefix_attributes = declspecs.attributes;
30080 declspecs.attributes = NULL_TREE;
30082 /* Keep going until we hit the `;' at the end of the
30083 declaration. */
30084 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30086 tree width = NULL_TREE, attributes, first_attribute, decl;
30087 cp_declarator *declarator = NULL;
30088 int ctor_dtor_or_conv_p;
30090 /* Check for a (possibly unnamed) bitfield declaration. */
30091 token = cp_lexer_peek_token (parser->lexer);
30092 if (token->type == CPP_COLON)
30093 goto eat_colon;
30095 if (token->type == CPP_NAME
30096 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30097 == CPP_COLON))
30099 /* Get the name of the bitfield. */
30100 declarator = make_id_declarator (NULL_TREE,
30101 cp_parser_identifier (parser),
30102 sfk_none);
30104 eat_colon:
30105 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30106 /* Get the width of the bitfield. */
30107 width
30108 = cp_parser_constant_expression (parser);
30110 else
30112 /* Parse the declarator. */
30113 declarator
30114 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30115 &ctor_dtor_or_conv_p,
30116 /*parenthesized_p=*/NULL,
30117 /*member_p=*/false,
30118 /*friend_p=*/false);
30121 /* Look for attributes that apply to the ivar. */
30122 attributes = cp_parser_attributes_opt (parser);
30123 /* Remember which attributes are prefix attributes and
30124 which are not. */
30125 first_attribute = attributes;
30126 /* Combine the attributes. */
30127 attributes = chainon (prefix_attributes, attributes);
30129 if (width)
30130 /* Create the bitfield declaration. */
30131 decl = grokbitfield (declarator, &declspecs,
30132 width, NULL_TREE, attributes);
30133 else
30134 decl = grokfield (declarator, &declspecs,
30135 NULL_TREE, /*init_const_expr_p=*/false,
30136 NULL_TREE, attributes);
30138 /* Add the instance variable. */
30139 if (decl != error_mark_node && decl != NULL_TREE)
30140 objc_add_instance_variable (decl);
30142 /* Reset PREFIX_ATTRIBUTES. */
30143 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30144 attributes = TREE_CHAIN (attributes);
30145 if (attributes)
30146 TREE_CHAIN (attributes) = NULL_TREE;
30148 token = cp_lexer_peek_token (parser->lexer);
30150 if (token->type == CPP_COMMA)
30152 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30153 continue;
30155 break;
30158 cp_parser_consume_semicolon_at_end_of_statement (parser);
30159 token = cp_lexer_peek_token (parser->lexer);
30162 if (token->keyword == RID_AT_END)
30163 cp_parser_error (parser, "expected %<}%>");
30165 /* Do not consume the RID_AT_END, so it will be read again as terminating
30166 the @interface of @implementation. */
30167 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30168 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30170 /* For historical reasons, we accept an optional semicolon. */
30171 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30172 cp_lexer_consume_token (parser->lexer);
30175 /* Parse an Objective-C protocol declaration. */
30177 static void
30178 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30180 tree proto, protorefs;
30181 cp_token *tok;
30183 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30186 tok = cp_lexer_peek_token (parser->lexer);
30187 error_at (tok->location, "identifier expected after %<@protocol%>");
30188 cp_parser_consume_semicolon_at_end_of_statement (parser);
30189 return;
30192 /* See if we have a forward declaration or a definition. */
30193 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30195 /* Try a forward declaration first. */
30196 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30198 while (true)
30200 tree id;
30202 id = cp_parser_identifier (parser);
30203 if (id == error_mark_node)
30204 break;
30206 objc_declare_protocol (id, attributes);
30208 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30209 cp_lexer_consume_token (parser->lexer);
30210 else
30211 break;
30213 cp_parser_consume_semicolon_at_end_of_statement (parser);
30216 /* Ok, we got a full-fledged definition (or at least should). */
30217 else
30219 proto = cp_parser_identifier (parser);
30220 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30221 objc_start_protocol (proto, protorefs, attributes);
30222 cp_parser_objc_method_prototype_list (parser);
30226 /* Parse an Objective-C superclass or category. */
30228 static void
30229 cp_parser_objc_superclass_or_category (cp_parser *parser,
30230 bool iface_p,
30231 tree *super,
30232 tree *categ, bool *is_class_extension)
30234 cp_token *next = cp_lexer_peek_token (parser->lexer);
30236 *super = *categ = NULL_TREE;
30237 *is_class_extension = false;
30238 if (next->type == CPP_COLON)
30240 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30241 *super = cp_parser_identifier (parser);
30243 else if (next->type == CPP_OPEN_PAREN)
30245 matching_parens parens;
30246 parens.consume_open (parser); /* Eat '('. */
30248 /* If there is no category name, and this is an @interface, we
30249 have a class extension. */
30250 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30252 *categ = NULL_TREE;
30253 *is_class_extension = true;
30255 else
30256 *categ = cp_parser_identifier (parser);
30258 parens.require_close (parser);
30262 /* Parse an Objective-C class interface. */
30264 static void
30265 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30267 tree name, super, categ, protos;
30268 bool is_class_extension;
30270 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30271 name = cp_parser_identifier (parser);
30272 if (name == error_mark_node)
30274 /* It's hard to recover because even if valid @interface stuff
30275 is to follow, we can't compile it (or validate it) if we
30276 don't even know which class it refers to. Let's assume this
30277 was a stray '@interface' token in the stream and skip it.
30279 return;
30281 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30282 &is_class_extension);
30283 protos = cp_parser_objc_protocol_refs_opt (parser);
30285 /* We have either a class or a category on our hands. */
30286 if (categ || is_class_extension)
30287 objc_start_category_interface (name, categ, protos, attributes);
30288 else
30290 objc_start_class_interface (name, super, protos, attributes);
30291 /* Handle instance variable declarations, if any. */
30292 cp_parser_objc_class_ivars (parser);
30293 objc_continue_interface ();
30296 cp_parser_objc_method_prototype_list (parser);
30299 /* Parse an Objective-C class implementation. */
30301 static void
30302 cp_parser_objc_class_implementation (cp_parser* parser)
30304 tree name, super, categ;
30305 bool is_class_extension;
30307 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30308 name = cp_parser_identifier (parser);
30309 if (name == error_mark_node)
30311 /* It's hard to recover because even if valid @implementation
30312 stuff is to follow, we can't compile it (or validate it) if
30313 we don't even know which class it refers to. Let's assume
30314 this was a stray '@implementation' token in the stream and
30315 skip it.
30317 return;
30319 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30320 &is_class_extension);
30322 /* We have either a class or a category on our hands. */
30323 if (categ)
30324 objc_start_category_implementation (name, categ);
30325 else
30327 objc_start_class_implementation (name, super);
30328 /* Handle instance variable declarations, if any. */
30329 cp_parser_objc_class_ivars (parser);
30330 objc_continue_implementation ();
30333 cp_parser_objc_method_definition_list (parser);
30336 /* Consume the @end token and finish off the implementation. */
30338 static void
30339 cp_parser_objc_end_implementation (cp_parser* parser)
30341 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30342 objc_finish_implementation ();
30345 /* Parse an Objective-C declaration. */
30347 static void
30348 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30350 /* Try to figure out what kind of declaration is present. */
30351 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30353 if (attributes)
30354 switch (kwd->keyword)
30356 case RID_AT_ALIAS:
30357 case RID_AT_CLASS:
30358 case RID_AT_END:
30359 error_at (kwd->location, "attributes may not be specified before"
30360 " the %<@%D%> Objective-C++ keyword",
30361 kwd->u.value);
30362 attributes = NULL;
30363 break;
30364 case RID_AT_IMPLEMENTATION:
30365 warning_at (kwd->location, OPT_Wattributes,
30366 "prefix attributes are ignored before %<@%D%>",
30367 kwd->u.value);
30368 attributes = NULL;
30369 default:
30370 break;
30373 switch (kwd->keyword)
30375 case RID_AT_ALIAS:
30376 cp_parser_objc_alias_declaration (parser);
30377 break;
30378 case RID_AT_CLASS:
30379 cp_parser_objc_class_declaration (parser);
30380 break;
30381 case RID_AT_PROTOCOL:
30382 cp_parser_objc_protocol_declaration (parser, attributes);
30383 break;
30384 case RID_AT_INTERFACE:
30385 cp_parser_objc_class_interface (parser, attributes);
30386 break;
30387 case RID_AT_IMPLEMENTATION:
30388 cp_parser_objc_class_implementation (parser);
30389 break;
30390 case RID_AT_END:
30391 cp_parser_objc_end_implementation (parser);
30392 break;
30393 default:
30394 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30395 kwd->u.value);
30396 cp_parser_skip_to_end_of_block_or_statement (parser);
30400 /* Parse an Objective-C try-catch-finally statement.
30402 objc-try-catch-finally-stmt:
30403 @try compound-statement objc-catch-clause-seq [opt]
30404 objc-finally-clause [opt]
30406 objc-catch-clause-seq:
30407 objc-catch-clause objc-catch-clause-seq [opt]
30409 objc-catch-clause:
30410 @catch ( objc-exception-declaration ) compound-statement
30412 objc-finally-clause:
30413 @finally compound-statement
30415 objc-exception-declaration:
30416 parameter-declaration
30417 '...'
30419 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30421 Returns NULL_TREE.
30423 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30424 for C. Keep them in sync. */
30426 static tree
30427 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30429 location_t location;
30430 tree stmt;
30432 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30433 location = cp_lexer_peek_token (parser->lexer)->location;
30434 objc_maybe_warn_exceptions (location);
30435 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30436 node, lest it get absorbed into the surrounding block. */
30437 stmt = push_stmt_list ();
30438 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30439 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30441 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30443 cp_parameter_declarator *parm;
30444 tree parameter_declaration = error_mark_node;
30445 bool seen_open_paren = false;
30446 matching_parens parens;
30448 cp_lexer_consume_token (parser->lexer);
30449 if (parens.require_open (parser))
30450 seen_open_paren = true;
30451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30453 /* We have "@catch (...)" (where the '...' are literally
30454 what is in the code). Skip the '...'.
30455 parameter_declaration is set to NULL_TREE, and
30456 objc_being_catch_clauses() knows that that means
30457 '...'. */
30458 cp_lexer_consume_token (parser->lexer);
30459 parameter_declaration = NULL_TREE;
30461 else
30463 /* We have "@catch (NSException *exception)" or something
30464 like that. Parse the parameter declaration. */
30465 parm = cp_parser_parameter_declaration (parser, false, NULL);
30466 if (parm == NULL)
30467 parameter_declaration = error_mark_node;
30468 else
30469 parameter_declaration = grokdeclarator (parm->declarator,
30470 &parm->decl_specifiers,
30471 PARM, /*initialized=*/0,
30472 /*attrlist=*/NULL);
30474 if (seen_open_paren)
30475 parens.require_close (parser);
30476 else
30478 /* If there was no open parenthesis, we are recovering from
30479 an error, and we are trying to figure out what mistake
30480 the user has made. */
30482 /* If there is an immediate closing parenthesis, the user
30483 probably forgot the opening one (ie, they typed "@catch
30484 NSException *e)". Parse the closing parenthesis and keep
30485 going. */
30486 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30487 cp_lexer_consume_token (parser->lexer);
30489 /* If these is no immediate closing parenthesis, the user
30490 probably doesn't know that parenthesis are required at
30491 all (ie, they typed "@catch NSException *e"). So, just
30492 forget about the closing parenthesis and keep going. */
30494 objc_begin_catch_clause (parameter_declaration);
30495 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30496 objc_finish_catch_clause ();
30498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30500 cp_lexer_consume_token (parser->lexer);
30501 location = cp_lexer_peek_token (parser->lexer)->location;
30502 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30503 node, lest it get absorbed into the surrounding block. */
30504 stmt = push_stmt_list ();
30505 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30506 objc_build_finally_clause (location, pop_stmt_list (stmt));
30509 return objc_finish_try_stmt ();
30512 /* Parse an Objective-C synchronized statement.
30514 objc-synchronized-stmt:
30515 @synchronized ( expression ) compound-statement
30517 Returns NULL_TREE. */
30519 static tree
30520 cp_parser_objc_synchronized_statement (cp_parser *parser)
30522 location_t location;
30523 tree lock, stmt;
30525 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30527 location = cp_lexer_peek_token (parser->lexer)->location;
30528 objc_maybe_warn_exceptions (location);
30529 matching_parens parens;
30530 parens.require_open (parser);
30531 lock = cp_parser_expression (parser);
30532 parens.require_close (parser);
30534 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30535 node, lest it get absorbed into the surrounding block. */
30536 stmt = push_stmt_list ();
30537 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30539 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30542 /* Parse an Objective-C throw statement.
30544 objc-throw-stmt:
30545 @throw assignment-expression [opt] ;
30547 Returns a constructed '@throw' statement. */
30549 static tree
30550 cp_parser_objc_throw_statement (cp_parser *parser)
30552 tree expr = NULL_TREE;
30553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30555 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30558 expr = cp_parser_expression (parser);
30560 cp_parser_consume_semicolon_at_end_of_statement (parser);
30562 return objc_build_throw_stmt (loc, expr);
30565 /* Parse an Objective-C statement. */
30567 static tree
30568 cp_parser_objc_statement (cp_parser * parser)
30570 /* Try to figure out what kind of declaration is present. */
30571 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30573 switch (kwd->keyword)
30575 case RID_AT_TRY:
30576 return cp_parser_objc_try_catch_finally_statement (parser);
30577 case RID_AT_SYNCHRONIZED:
30578 return cp_parser_objc_synchronized_statement (parser);
30579 case RID_AT_THROW:
30580 return cp_parser_objc_throw_statement (parser);
30581 default:
30582 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30583 kwd->u.value);
30584 cp_parser_skip_to_end_of_block_or_statement (parser);
30587 return error_mark_node;
30590 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30591 look ahead to see if an objc keyword follows the attributes. This
30592 is to detect the use of prefix attributes on ObjC @interface and
30593 @protocol. */
30595 static bool
30596 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30598 cp_lexer_save_tokens (parser->lexer);
30599 *attrib = cp_parser_attributes_opt (parser);
30600 gcc_assert (*attrib);
30601 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30603 cp_lexer_commit_tokens (parser->lexer);
30604 return true;
30606 cp_lexer_rollback_tokens (parser->lexer);
30607 return false;
30610 /* This routine is a minimal replacement for
30611 c_parser_struct_declaration () used when parsing the list of
30612 types/names or ObjC++ properties. For example, when parsing the
30613 code
30615 @property (readonly) int a, b, c;
30617 this function is responsible for parsing "int a, int b, int c" and
30618 returning the declarations as CHAIN of DECLs.
30620 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30621 similar parsing. */
30622 static tree
30623 cp_parser_objc_struct_declaration (cp_parser *parser)
30625 tree decls = NULL_TREE;
30626 cp_decl_specifier_seq declspecs;
30627 int decl_class_or_enum_p;
30628 tree prefix_attributes;
30630 cp_parser_decl_specifier_seq (parser,
30631 CP_PARSER_FLAGS_NONE,
30632 &declspecs,
30633 &decl_class_or_enum_p);
30635 if (declspecs.type == error_mark_node)
30636 return error_mark_node;
30638 /* auto, register, static, extern, mutable. */
30639 if (declspecs.storage_class != sc_none)
30641 cp_parser_error (parser, "invalid type for property");
30642 declspecs.storage_class = sc_none;
30645 /* thread_local. */
30646 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30648 cp_parser_error (parser, "invalid type for property");
30649 declspecs.locations[ds_thread] = 0;
30652 /* typedef. */
30653 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30655 cp_parser_error (parser, "invalid type for property");
30656 declspecs.locations[ds_typedef] = 0;
30659 prefix_attributes = declspecs.attributes;
30660 declspecs.attributes = NULL_TREE;
30662 /* Keep going until we hit the `;' at the end of the declaration. */
30663 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30665 tree attributes, first_attribute, decl;
30666 cp_declarator *declarator;
30667 cp_token *token;
30669 /* Parse the declarator. */
30670 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30671 NULL, NULL, false, false);
30673 /* Look for attributes that apply to the ivar. */
30674 attributes = cp_parser_attributes_opt (parser);
30675 /* Remember which attributes are prefix attributes and
30676 which are not. */
30677 first_attribute = attributes;
30678 /* Combine the attributes. */
30679 attributes = chainon (prefix_attributes, attributes);
30681 decl = grokfield (declarator, &declspecs,
30682 NULL_TREE, /*init_const_expr_p=*/false,
30683 NULL_TREE, attributes);
30685 if (decl == error_mark_node || decl == NULL_TREE)
30686 return error_mark_node;
30688 /* Reset PREFIX_ATTRIBUTES. */
30689 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30690 attributes = TREE_CHAIN (attributes);
30691 if (attributes)
30692 TREE_CHAIN (attributes) = NULL_TREE;
30694 DECL_CHAIN (decl) = decls;
30695 decls = decl;
30697 token = cp_lexer_peek_token (parser->lexer);
30698 if (token->type == CPP_COMMA)
30700 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30701 continue;
30703 else
30704 break;
30706 return decls;
30709 /* Parse an Objective-C @property declaration. The syntax is:
30711 objc-property-declaration:
30712 '@property' objc-property-attributes[opt] struct-declaration ;
30714 objc-property-attributes:
30715 '(' objc-property-attribute-list ')'
30717 objc-property-attribute-list:
30718 objc-property-attribute
30719 objc-property-attribute-list, objc-property-attribute
30721 objc-property-attribute
30722 'getter' = identifier
30723 'setter' = identifier
30724 'readonly'
30725 'readwrite'
30726 'assign'
30727 'retain'
30728 'copy'
30729 'nonatomic'
30731 For example:
30732 @property NSString *name;
30733 @property (readonly) id object;
30734 @property (retain, nonatomic, getter=getTheName) id name;
30735 @property int a, b, c;
30737 PS: This function is identical to
30738 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30739 static void
30740 cp_parser_objc_at_property_declaration (cp_parser *parser)
30742 /* The following variables hold the attributes of the properties as
30743 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30744 seen. When we see an attribute, we set them to 'true' (if they
30745 are boolean properties) or to the identifier (if they have an
30746 argument, ie, for getter and setter). Note that here we only
30747 parse the list of attributes, check the syntax and accumulate the
30748 attributes that we find. objc_add_property_declaration() will
30749 then process the information. */
30750 bool property_assign = false;
30751 bool property_copy = false;
30752 tree property_getter_ident = NULL_TREE;
30753 bool property_nonatomic = false;
30754 bool property_readonly = false;
30755 bool property_readwrite = false;
30756 bool property_retain = false;
30757 tree property_setter_ident = NULL_TREE;
30759 /* 'properties' is the list of properties that we read. Usually a
30760 single one, but maybe more (eg, in "@property int a, b, c;" there
30761 are three). */
30762 tree properties;
30763 location_t loc;
30765 loc = cp_lexer_peek_token (parser->lexer)->location;
30767 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30769 /* Parse the optional attribute list... */
30770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30772 /* Eat the '('. */
30773 matching_parens parens;
30774 parens.consume_open (parser);
30776 while (true)
30778 bool syntax_error = false;
30779 cp_token *token = cp_lexer_peek_token (parser->lexer);
30780 enum rid keyword;
30782 if (token->type != CPP_NAME)
30784 cp_parser_error (parser, "expected identifier");
30785 break;
30787 keyword = C_RID_CODE (token->u.value);
30788 cp_lexer_consume_token (parser->lexer);
30789 switch (keyword)
30791 case RID_ASSIGN: property_assign = true; break;
30792 case RID_COPY: property_copy = true; break;
30793 case RID_NONATOMIC: property_nonatomic = true; break;
30794 case RID_READONLY: property_readonly = true; break;
30795 case RID_READWRITE: property_readwrite = true; break;
30796 case RID_RETAIN: property_retain = true; break;
30798 case RID_GETTER:
30799 case RID_SETTER:
30800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30802 if (keyword == RID_GETTER)
30803 cp_parser_error (parser,
30804 "missing %<=%> (after %<getter%> attribute)");
30805 else
30806 cp_parser_error (parser,
30807 "missing %<=%> (after %<setter%> attribute)");
30808 syntax_error = true;
30809 break;
30811 cp_lexer_consume_token (parser->lexer); /* eat the = */
30812 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30814 cp_parser_error (parser, "expected identifier");
30815 syntax_error = true;
30816 break;
30818 if (keyword == RID_SETTER)
30820 if (property_setter_ident != NULL_TREE)
30822 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30823 cp_lexer_consume_token (parser->lexer);
30825 else
30826 property_setter_ident = cp_parser_objc_selector (parser);
30827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30828 cp_parser_error (parser, "setter name must terminate with %<:%>");
30829 else
30830 cp_lexer_consume_token (parser->lexer);
30832 else
30834 if (property_getter_ident != NULL_TREE)
30836 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30837 cp_lexer_consume_token (parser->lexer);
30839 else
30840 property_getter_ident = cp_parser_objc_selector (parser);
30842 break;
30843 default:
30844 cp_parser_error (parser, "unknown property attribute");
30845 syntax_error = true;
30846 break;
30849 if (syntax_error)
30850 break;
30852 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30853 cp_lexer_consume_token (parser->lexer);
30854 else
30855 break;
30858 /* FIXME: "@property (setter, assign);" will generate a spurious
30859 "error: expected ‘)’ before ‘,’ token". This is because
30860 cp_parser_require, unlike the C counterpart, will produce an
30861 error even if we are in error recovery. */
30862 if (!parens.require_close (parser))
30864 cp_parser_skip_to_closing_parenthesis (parser,
30865 /*recovering=*/true,
30866 /*or_comma=*/false,
30867 /*consume_paren=*/true);
30871 /* ... and the property declaration(s). */
30872 properties = cp_parser_objc_struct_declaration (parser);
30874 if (properties == error_mark_node)
30876 cp_parser_skip_to_end_of_statement (parser);
30877 /* If the next token is now a `;', consume it. */
30878 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30879 cp_lexer_consume_token (parser->lexer);
30880 return;
30883 if (properties == NULL_TREE)
30884 cp_parser_error (parser, "expected identifier");
30885 else
30887 /* Comma-separated properties are chained together in
30888 reverse order; add them one by one. */
30889 properties = nreverse (properties);
30891 for (; properties; properties = TREE_CHAIN (properties))
30892 objc_add_property_declaration (loc, copy_node (properties),
30893 property_readonly, property_readwrite,
30894 property_assign, property_retain,
30895 property_copy, property_nonatomic,
30896 property_getter_ident, property_setter_ident);
30899 cp_parser_consume_semicolon_at_end_of_statement (parser);
30902 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30904 objc-synthesize-declaration:
30905 @synthesize objc-synthesize-identifier-list ;
30907 objc-synthesize-identifier-list:
30908 objc-synthesize-identifier
30909 objc-synthesize-identifier-list, objc-synthesize-identifier
30911 objc-synthesize-identifier
30912 identifier
30913 identifier = identifier
30915 For example:
30916 @synthesize MyProperty;
30917 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30919 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30920 for C. Keep them in sync.
30922 static void
30923 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30925 tree list = NULL_TREE;
30926 location_t loc;
30927 loc = cp_lexer_peek_token (parser->lexer)->location;
30929 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30930 while (true)
30932 tree property, ivar;
30933 property = cp_parser_identifier (parser);
30934 if (property == error_mark_node)
30936 cp_parser_consume_semicolon_at_end_of_statement (parser);
30937 return;
30939 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30941 cp_lexer_consume_token (parser->lexer);
30942 ivar = cp_parser_identifier (parser);
30943 if (ivar == error_mark_node)
30945 cp_parser_consume_semicolon_at_end_of_statement (parser);
30946 return;
30949 else
30950 ivar = NULL_TREE;
30951 list = chainon (list, build_tree_list (ivar, property));
30952 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30953 cp_lexer_consume_token (parser->lexer);
30954 else
30955 break;
30957 cp_parser_consume_semicolon_at_end_of_statement (parser);
30958 objc_add_synthesize_declaration (loc, list);
30961 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30963 objc-dynamic-declaration:
30964 @dynamic identifier-list ;
30966 For example:
30967 @dynamic MyProperty;
30968 @dynamic MyProperty, AnotherProperty;
30970 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30971 for C. Keep them in sync.
30973 static void
30974 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30976 tree list = NULL_TREE;
30977 location_t loc;
30978 loc = cp_lexer_peek_token (parser->lexer)->location;
30980 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30981 while (true)
30983 tree property;
30984 property = cp_parser_identifier (parser);
30985 if (property == error_mark_node)
30987 cp_parser_consume_semicolon_at_end_of_statement (parser);
30988 return;
30990 list = chainon (list, build_tree_list (NULL, property));
30991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30992 cp_lexer_consume_token (parser->lexer);
30993 else
30994 break;
30996 cp_parser_consume_semicolon_at_end_of_statement (parser);
30997 objc_add_dynamic_declaration (loc, list);
31001 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31003 /* Returns name of the next clause.
31004 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31005 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31006 returned and the token is consumed. */
31008 static pragma_omp_clause
31009 cp_parser_omp_clause_name (cp_parser *parser)
31011 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31014 result = PRAGMA_OACC_CLAUSE_AUTO;
31015 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31016 result = PRAGMA_OMP_CLAUSE_IF;
31017 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31018 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31019 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31020 result = PRAGMA_OACC_CLAUSE_DELETE;
31021 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31022 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31023 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31024 result = PRAGMA_OMP_CLAUSE_FOR;
31025 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31027 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31028 const char *p = IDENTIFIER_POINTER (id);
31030 switch (p[0])
31032 case 'a':
31033 if (!strcmp ("aligned", p))
31034 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31035 else if (!strcmp ("async", p))
31036 result = PRAGMA_OACC_CLAUSE_ASYNC;
31037 break;
31038 case 'c':
31039 if (!strcmp ("collapse", p))
31040 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31041 else if (!strcmp ("copy", p))
31042 result = PRAGMA_OACC_CLAUSE_COPY;
31043 else if (!strcmp ("copyin", p))
31044 result = PRAGMA_OMP_CLAUSE_COPYIN;
31045 else if (!strcmp ("copyout", p))
31046 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31047 else if (!strcmp ("copyprivate", p))
31048 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31049 else if (!strcmp ("create", p))
31050 result = PRAGMA_OACC_CLAUSE_CREATE;
31051 break;
31052 case 'd':
31053 if (!strcmp ("defaultmap", p))
31054 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31055 else if (!strcmp ("depend", p))
31056 result = PRAGMA_OMP_CLAUSE_DEPEND;
31057 else if (!strcmp ("device", p))
31058 result = PRAGMA_OMP_CLAUSE_DEVICE;
31059 else if (!strcmp ("deviceptr", p))
31060 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31061 else if (!strcmp ("device_resident", p))
31062 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31063 else if (!strcmp ("dist_schedule", p))
31064 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31065 break;
31066 case 'f':
31067 if (!strcmp ("final", p))
31068 result = PRAGMA_OMP_CLAUSE_FINAL;
31069 else if (!strcmp ("firstprivate", p))
31070 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31071 else if (!strcmp ("from", p))
31072 result = PRAGMA_OMP_CLAUSE_FROM;
31073 break;
31074 case 'g':
31075 if (!strcmp ("gang", p))
31076 result = PRAGMA_OACC_CLAUSE_GANG;
31077 else if (!strcmp ("grainsize", p))
31078 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31079 break;
31080 case 'h':
31081 if (!strcmp ("hint", p))
31082 result = PRAGMA_OMP_CLAUSE_HINT;
31083 else if (!strcmp ("host", p))
31084 result = PRAGMA_OACC_CLAUSE_HOST;
31085 break;
31086 case 'i':
31087 if (!strcmp ("inbranch", p))
31088 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31089 else if (!strcmp ("independent", p))
31090 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31091 else if (!strcmp ("is_device_ptr", p))
31092 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31093 break;
31094 case 'l':
31095 if (!strcmp ("lastprivate", p))
31096 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31097 else if (!strcmp ("linear", p))
31098 result = PRAGMA_OMP_CLAUSE_LINEAR;
31099 else if (!strcmp ("link", p))
31100 result = PRAGMA_OMP_CLAUSE_LINK;
31101 break;
31102 case 'm':
31103 if (!strcmp ("map", p))
31104 result = PRAGMA_OMP_CLAUSE_MAP;
31105 else if (!strcmp ("mergeable", p))
31106 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31107 break;
31108 case 'n':
31109 if (!strcmp ("nogroup", p))
31110 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31111 else if (!strcmp ("notinbranch", p))
31112 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31113 else if (!strcmp ("nowait", p))
31114 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31115 else if (!strcmp ("num_gangs", p))
31116 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31117 else if (!strcmp ("num_tasks", p))
31118 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31119 else if (!strcmp ("num_teams", p))
31120 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31121 else if (!strcmp ("num_threads", p))
31122 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31123 else if (!strcmp ("num_workers", p))
31124 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31125 break;
31126 case 'o':
31127 if (!strcmp ("ordered", p))
31128 result = PRAGMA_OMP_CLAUSE_ORDERED;
31129 break;
31130 case 'p':
31131 if (!strcmp ("parallel", p))
31132 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31133 else if (!strcmp ("present", p))
31134 result = PRAGMA_OACC_CLAUSE_PRESENT;
31135 else if (!strcmp ("present_or_copy", p)
31136 || !strcmp ("pcopy", p))
31137 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31138 else if (!strcmp ("present_or_copyin", p)
31139 || !strcmp ("pcopyin", p))
31140 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31141 else if (!strcmp ("present_or_copyout", p)
31142 || !strcmp ("pcopyout", p))
31143 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31144 else if (!strcmp ("present_or_create", p)
31145 || !strcmp ("pcreate", p))
31146 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31147 else if (!strcmp ("priority", p))
31148 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31149 else if (!strcmp ("proc_bind", p))
31150 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31151 break;
31152 case 'r':
31153 if (!strcmp ("reduction", p))
31154 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31155 break;
31156 case 's':
31157 if (!strcmp ("safelen", p))
31158 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31159 else if (!strcmp ("schedule", p))
31160 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31161 else if (!strcmp ("sections", p))
31162 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31163 else if (!strcmp ("self", p))
31164 result = PRAGMA_OACC_CLAUSE_SELF;
31165 else if (!strcmp ("seq", p))
31166 result = PRAGMA_OACC_CLAUSE_SEQ;
31167 else if (!strcmp ("shared", p))
31168 result = PRAGMA_OMP_CLAUSE_SHARED;
31169 else if (!strcmp ("simd", p))
31170 result = PRAGMA_OMP_CLAUSE_SIMD;
31171 else if (!strcmp ("simdlen", p))
31172 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31173 break;
31174 case 't':
31175 if (!strcmp ("taskgroup", p))
31176 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31177 else if (!strcmp ("thread_limit", p))
31178 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31179 else if (!strcmp ("threads", p))
31180 result = PRAGMA_OMP_CLAUSE_THREADS;
31181 else if (!strcmp ("tile", p))
31182 result = PRAGMA_OACC_CLAUSE_TILE;
31183 else if (!strcmp ("to", p))
31184 result = PRAGMA_OMP_CLAUSE_TO;
31185 break;
31186 case 'u':
31187 if (!strcmp ("uniform", p))
31188 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31189 else if (!strcmp ("untied", p))
31190 result = PRAGMA_OMP_CLAUSE_UNTIED;
31191 else if (!strcmp ("use_device", p))
31192 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31193 else if (!strcmp ("use_device_ptr", p))
31194 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31195 break;
31196 case 'v':
31197 if (!strcmp ("vector", p))
31198 result = PRAGMA_OACC_CLAUSE_VECTOR;
31199 else if (!strcmp ("vector_length", p))
31200 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31201 break;
31202 case 'w':
31203 if (!strcmp ("wait", p))
31204 result = PRAGMA_OACC_CLAUSE_WAIT;
31205 else if (!strcmp ("worker", p))
31206 result = PRAGMA_OACC_CLAUSE_WORKER;
31207 break;
31211 if (result != PRAGMA_OMP_CLAUSE_NONE)
31212 cp_lexer_consume_token (parser->lexer);
31214 return result;
31217 /* Validate that a clause of the given type does not already exist. */
31219 static void
31220 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31221 const char *name, location_t location)
31223 tree c;
31225 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31226 if (OMP_CLAUSE_CODE (c) == code)
31228 error_at (location, "too many %qs clauses", name);
31229 break;
31233 /* OpenMP 2.5:
31234 variable-list:
31235 identifier
31236 variable-list , identifier
31238 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31239 colon). An opening parenthesis will have been consumed by the caller.
31241 If KIND is nonzero, create the appropriate node and install the decl
31242 in OMP_CLAUSE_DECL and add the node to the head of the list.
31244 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31245 return the list created.
31247 COLON can be NULL if only closing parenthesis should end the list,
31248 or pointer to bool which will receive false if the list is terminated
31249 by closing parenthesis or true if the list is terminated by colon. */
31251 static tree
31252 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31253 tree list, bool *colon)
31255 cp_token *token;
31256 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31257 if (colon)
31259 parser->colon_corrects_to_scope_p = false;
31260 *colon = false;
31262 while (1)
31264 tree name, decl;
31266 token = cp_lexer_peek_token (parser->lexer);
31267 if (kind != 0
31268 && current_class_ptr
31269 && cp_parser_is_keyword (token, RID_THIS))
31271 decl = finish_this_expr ();
31272 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31273 || CONVERT_EXPR_P (decl))
31274 decl = TREE_OPERAND (decl, 0);
31275 cp_lexer_consume_token (parser->lexer);
31277 else
31279 name = cp_parser_id_expression (parser, /*template_p=*/false,
31280 /*check_dependency_p=*/true,
31281 /*template_p=*/NULL,
31282 /*declarator_p=*/false,
31283 /*optional_p=*/false);
31284 if (name == error_mark_node)
31285 goto skip_comma;
31287 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31288 if (decl == error_mark_node)
31289 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31290 token->location);
31292 if (decl == error_mark_node)
31294 else if (kind != 0)
31296 switch (kind)
31298 case OMP_CLAUSE__CACHE_:
31299 /* The OpenACC cache directive explicitly only allows "array
31300 elements or subarrays". */
31301 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31303 error_at (token->location, "expected %<[%>");
31304 decl = error_mark_node;
31305 break;
31307 /* FALLTHROUGH. */
31308 case OMP_CLAUSE_MAP:
31309 case OMP_CLAUSE_FROM:
31310 case OMP_CLAUSE_TO:
31311 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31313 location_t loc
31314 = cp_lexer_peek_token (parser->lexer)->location;
31315 cp_id_kind idk = CP_ID_KIND_NONE;
31316 cp_lexer_consume_token (parser->lexer);
31317 decl = convert_from_reference (decl);
31318 decl
31319 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31320 decl, false,
31321 &idk, loc);
31323 /* FALLTHROUGH. */
31324 case OMP_CLAUSE_DEPEND:
31325 case OMP_CLAUSE_REDUCTION:
31326 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31328 tree low_bound = NULL_TREE, length = NULL_TREE;
31330 parser->colon_corrects_to_scope_p = false;
31331 cp_lexer_consume_token (parser->lexer);
31332 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31333 low_bound = cp_parser_expression (parser);
31334 if (!colon)
31335 parser->colon_corrects_to_scope_p
31336 = saved_colon_corrects_to_scope_p;
31337 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31338 length = integer_one_node;
31339 else
31341 /* Look for `:'. */
31342 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31343 goto skip_comma;
31344 if (!cp_lexer_next_token_is (parser->lexer,
31345 CPP_CLOSE_SQUARE))
31346 length = cp_parser_expression (parser);
31348 /* Look for the closing `]'. */
31349 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31350 RT_CLOSE_SQUARE))
31351 goto skip_comma;
31353 decl = tree_cons (low_bound, length, decl);
31355 break;
31356 default:
31357 break;
31360 tree u = build_omp_clause (token->location, kind);
31361 OMP_CLAUSE_DECL (u) = decl;
31362 OMP_CLAUSE_CHAIN (u) = list;
31363 list = u;
31365 else
31366 list = tree_cons (decl, NULL_TREE, list);
31368 get_comma:
31369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31370 break;
31371 cp_lexer_consume_token (parser->lexer);
31374 if (colon)
31375 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31377 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31379 *colon = true;
31380 cp_parser_require (parser, CPP_COLON, RT_COLON);
31381 return list;
31384 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31386 int ending;
31388 /* Try to resync to an unnested comma. Copied from
31389 cp_parser_parenthesized_expression_list. */
31390 skip_comma:
31391 if (colon)
31392 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31393 ending = cp_parser_skip_to_closing_parenthesis (parser,
31394 /*recovering=*/true,
31395 /*or_comma=*/true,
31396 /*consume_paren=*/true);
31397 if (ending < 0)
31398 goto get_comma;
31401 return list;
31404 /* Similarly, but expect leading and trailing parenthesis. This is a very
31405 common case for omp clauses. */
31407 static tree
31408 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31410 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31411 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31412 return list;
31415 /* OpenACC 2.0:
31416 copy ( variable-list )
31417 copyin ( variable-list )
31418 copyout ( variable-list )
31419 create ( variable-list )
31420 delete ( variable-list )
31421 present ( variable-list )
31422 present_or_copy ( variable-list )
31423 pcopy ( variable-list )
31424 present_or_copyin ( variable-list )
31425 pcopyin ( variable-list )
31426 present_or_copyout ( variable-list )
31427 pcopyout ( variable-list )
31428 present_or_create ( variable-list )
31429 pcreate ( variable-list ) */
31431 static tree
31432 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31433 tree list)
31435 enum gomp_map_kind kind;
31436 switch (c_kind)
31438 case PRAGMA_OACC_CLAUSE_COPY:
31439 kind = GOMP_MAP_FORCE_TOFROM;
31440 break;
31441 case PRAGMA_OACC_CLAUSE_COPYIN:
31442 kind = GOMP_MAP_FORCE_TO;
31443 break;
31444 case PRAGMA_OACC_CLAUSE_COPYOUT:
31445 kind = GOMP_MAP_FORCE_FROM;
31446 break;
31447 case PRAGMA_OACC_CLAUSE_CREATE:
31448 kind = GOMP_MAP_FORCE_ALLOC;
31449 break;
31450 case PRAGMA_OACC_CLAUSE_DELETE:
31451 kind = GOMP_MAP_DELETE;
31452 break;
31453 case PRAGMA_OACC_CLAUSE_DEVICE:
31454 kind = GOMP_MAP_FORCE_TO;
31455 break;
31456 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31457 kind = GOMP_MAP_DEVICE_RESIDENT;
31458 break;
31459 case PRAGMA_OACC_CLAUSE_HOST:
31460 case PRAGMA_OACC_CLAUSE_SELF:
31461 kind = GOMP_MAP_FORCE_FROM;
31462 break;
31463 case PRAGMA_OACC_CLAUSE_LINK:
31464 kind = GOMP_MAP_LINK;
31465 break;
31466 case PRAGMA_OACC_CLAUSE_PRESENT:
31467 kind = GOMP_MAP_FORCE_PRESENT;
31468 break;
31469 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31470 kind = GOMP_MAP_TOFROM;
31471 break;
31472 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31473 kind = GOMP_MAP_TO;
31474 break;
31475 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31476 kind = GOMP_MAP_FROM;
31477 break;
31478 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31479 kind = GOMP_MAP_ALLOC;
31480 break;
31481 default:
31482 gcc_unreachable ();
31484 tree nl, c;
31485 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31487 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31488 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31490 return nl;
31493 /* OpenACC 2.0:
31494 deviceptr ( variable-list ) */
31496 static tree
31497 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31500 tree vars, t;
31502 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31503 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31504 variable-list must only allow for pointer variables. */
31505 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31506 for (t = vars; t; t = TREE_CHAIN (t))
31508 tree v = TREE_PURPOSE (t);
31509 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31510 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31511 OMP_CLAUSE_DECL (u) = v;
31512 OMP_CLAUSE_CHAIN (u) = list;
31513 list = u;
31516 return list;
31519 /* OpenACC 2.0:
31520 auto
31521 independent
31522 nohost
31523 seq */
31525 static tree
31526 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31527 enum omp_clause_code code,
31528 tree list, location_t location)
31530 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31531 tree c = build_omp_clause (location, code);
31532 OMP_CLAUSE_CHAIN (c) = list;
31533 return c;
31536 /* OpenACC:
31537 num_gangs ( expression )
31538 num_workers ( expression )
31539 vector_length ( expression ) */
31541 static tree
31542 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31543 const char *str, tree list)
31545 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31547 matching_parens parens;
31548 if (!parens.require_open (parser))
31549 return list;
31551 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31553 if (t == error_mark_node
31554 || !parens.require_close (parser))
31556 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31557 /*or_comma=*/false,
31558 /*consume_paren=*/true);
31559 return list;
31562 check_no_duplicate_clause (list, code, str, loc);
31564 tree c = build_omp_clause (loc, code);
31565 OMP_CLAUSE_OPERAND (c, 0) = t;
31566 OMP_CLAUSE_CHAIN (c) = list;
31567 return c;
31570 /* OpenACC:
31572 gang [( gang-arg-list )]
31573 worker [( [num:] int-expr )]
31574 vector [( [length:] int-expr )]
31576 where gang-arg is one of:
31578 [num:] int-expr
31579 static: size-expr
31581 and size-expr may be:
31584 int-expr
31587 static tree
31588 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31589 const char *str, tree list)
31591 const char *id = "num";
31592 cp_lexer *lexer = parser->lexer;
31593 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31594 location_t loc = cp_lexer_peek_token (lexer)->location;
31596 if (kind == OMP_CLAUSE_VECTOR)
31597 id = "length";
31599 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31601 matching_parens parens;
31602 parens.consume_open (parser);
31606 cp_token *next = cp_lexer_peek_token (lexer);
31607 int idx = 0;
31609 /* Gang static argument. */
31610 if (kind == OMP_CLAUSE_GANG
31611 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31613 cp_lexer_consume_token (lexer);
31615 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31616 goto cleanup_error;
31618 idx = 1;
31619 if (ops[idx] != NULL)
31621 cp_parser_error (parser, "too many %<static%> arguments");
31622 goto cleanup_error;
31625 /* Check for the '*' argument. */
31626 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31627 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31628 || cp_lexer_nth_token_is (parser->lexer, 2,
31629 CPP_CLOSE_PAREN)))
31631 cp_lexer_consume_token (lexer);
31632 ops[idx] = integer_minus_one_node;
31634 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31636 cp_lexer_consume_token (lexer);
31637 continue;
31639 else break;
31642 /* Worker num: argument and vector length: arguments. */
31643 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31644 && id_equal (next->u.value, id)
31645 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31647 cp_lexer_consume_token (lexer); /* id */
31648 cp_lexer_consume_token (lexer); /* ':' */
31651 /* Now collect the actual argument. */
31652 if (ops[idx] != NULL_TREE)
31654 cp_parser_error (parser, "unexpected argument");
31655 goto cleanup_error;
31658 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31659 false);
31660 if (expr == error_mark_node)
31661 goto cleanup_error;
31663 mark_exp_read (expr);
31664 ops[idx] = expr;
31666 if (kind == OMP_CLAUSE_GANG
31667 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31669 cp_lexer_consume_token (lexer);
31670 continue;
31672 break;
31674 while (1);
31676 if (!parens.require_close (parser))
31677 goto cleanup_error;
31680 check_no_duplicate_clause (list, kind, str, loc);
31682 c = build_omp_clause (loc, kind);
31684 if (ops[1])
31685 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31687 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31688 OMP_CLAUSE_CHAIN (c) = list;
31690 return c;
31692 cleanup_error:
31693 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31694 return list;
31697 /* OpenACC 2.0:
31698 tile ( size-expr-list ) */
31700 static tree
31701 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31703 tree c, expr = error_mark_node;
31704 tree tile = NULL_TREE;
31706 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31707 so, but the spec authors never considered such a case and have
31708 differing opinions on what it might mean, including 'not
31709 allowed'.) */
31710 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31711 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31712 clause_loc);
31714 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31715 return list;
31719 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31720 return list;
31722 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31723 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31724 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31726 cp_lexer_consume_token (parser->lexer);
31727 expr = integer_zero_node;
31729 else
31730 expr = cp_parser_constant_expression (parser);
31732 tile = tree_cons (NULL_TREE, expr, tile);
31734 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31736 /* Consume the trailing ')'. */
31737 cp_lexer_consume_token (parser->lexer);
31739 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31740 tile = nreverse (tile);
31741 OMP_CLAUSE_TILE_LIST (c) = tile;
31742 OMP_CLAUSE_CHAIN (c) = list;
31743 return c;
31746 /* OpenACC 2.0
31747 Parse wait clause or directive parameters. */
31749 static tree
31750 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31752 vec<tree, va_gc> *args;
31753 tree t, args_tree;
31755 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31756 /*cast_p=*/false,
31757 /*allow_expansion_p=*/true,
31758 /*non_constant_p=*/NULL);
31760 if (args == NULL || args->length () == 0)
31762 cp_parser_error (parser, "expected integer expression before ')'");
31763 if (args != NULL)
31764 release_tree_vector (args);
31765 return list;
31768 args_tree = build_tree_list_vec (args);
31770 release_tree_vector (args);
31772 for (t = args_tree; t; t = TREE_CHAIN (t))
31774 tree targ = TREE_VALUE (t);
31776 if (targ != error_mark_node)
31778 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31779 error ("%<wait%> expression must be integral");
31780 else
31782 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31784 targ = mark_rvalue_use (targ);
31785 OMP_CLAUSE_DECL (c) = targ;
31786 OMP_CLAUSE_CHAIN (c) = list;
31787 list = c;
31792 return list;
31795 /* OpenACC:
31796 wait ( int-expr-list ) */
31798 static tree
31799 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31801 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31803 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31804 return list;
31806 list = cp_parser_oacc_wait_list (parser, location, list);
31808 return list;
31811 /* OpenMP 3.0:
31812 collapse ( constant-expression ) */
31814 static tree
31815 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31817 tree c, num;
31818 location_t loc;
31819 HOST_WIDE_INT n;
31821 loc = cp_lexer_peek_token (parser->lexer)->location;
31822 matching_parens parens;
31823 if (!parens.require_open (parser))
31824 return list;
31826 num = cp_parser_constant_expression (parser);
31828 if (!parens.require_close (parser))
31829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31830 /*or_comma=*/false,
31831 /*consume_paren=*/true);
31833 if (num == error_mark_node)
31834 return list;
31835 num = fold_non_dependent_expr (num);
31836 if (!tree_fits_shwi_p (num)
31837 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31838 || (n = tree_to_shwi (num)) <= 0
31839 || (int) n != n)
31841 error_at (loc, "collapse argument needs positive constant integer expression");
31842 return list;
31845 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31846 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31847 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31848 OMP_CLAUSE_CHAIN (c) = list;
31849 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31851 return c;
31854 /* OpenMP 2.5:
31855 default ( none | shared )
31857 OpenACC:
31858 default ( none | present ) */
31860 static tree
31861 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31862 location_t location, bool is_oacc)
31864 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31865 tree c;
31867 matching_parens parens;
31868 if (!parens.require_open (parser))
31869 return list;
31870 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31872 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31873 const char *p = IDENTIFIER_POINTER (id);
31875 switch (p[0])
31877 case 'n':
31878 if (strcmp ("none", p) != 0)
31879 goto invalid_kind;
31880 kind = OMP_CLAUSE_DEFAULT_NONE;
31881 break;
31883 case 'p':
31884 if (strcmp ("present", p) != 0 || !is_oacc)
31885 goto invalid_kind;
31886 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31887 break;
31889 case 's':
31890 if (strcmp ("shared", p) != 0 || is_oacc)
31891 goto invalid_kind;
31892 kind = OMP_CLAUSE_DEFAULT_SHARED;
31893 break;
31895 default:
31896 goto invalid_kind;
31899 cp_lexer_consume_token (parser->lexer);
31901 else
31903 invalid_kind:
31904 if (is_oacc)
31905 cp_parser_error (parser, "expected %<none%> or %<present%>");
31906 else
31907 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31910 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31911 || !parens.require_close (parser))
31912 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31913 /*or_comma=*/false,
31914 /*consume_paren=*/true);
31916 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31917 return list;
31919 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31920 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31921 OMP_CLAUSE_CHAIN (c) = list;
31922 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31924 return c;
31927 /* OpenMP 3.1:
31928 final ( expression ) */
31930 static tree
31931 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31933 tree t, c;
31935 matching_parens parens;
31936 if (!parens.require_open (parser))
31937 return list;
31939 t = cp_parser_condition (parser);
31941 if (t == error_mark_node
31942 || !parens.require_close (parser))
31943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31944 /*or_comma=*/false,
31945 /*consume_paren=*/true);
31947 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31949 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31950 OMP_CLAUSE_FINAL_EXPR (c) = t;
31951 OMP_CLAUSE_CHAIN (c) = list;
31953 return c;
31956 /* OpenMP 2.5:
31957 if ( expression )
31959 OpenMP 4.5:
31960 if ( directive-name-modifier : expression )
31962 directive-name-modifier:
31963 parallel | task | taskloop | target data | target | target update
31964 | target enter data | target exit data */
31966 static tree
31967 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31968 bool is_omp)
31970 tree t, c;
31971 enum tree_code if_modifier = ERROR_MARK;
31973 matching_parens parens;
31974 if (!parens.require_open (parser))
31975 return list;
31977 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31980 const char *p = IDENTIFIER_POINTER (id);
31981 int n = 2;
31983 if (strcmp ("parallel", p) == 0)
31984 if_modifier = OMP_PARALLEL;
31985 else if (strcmp ("task", p) == 0)
31986 if_modifier = OMP_TASK;
31987 else if (strcmp ("taskloop", p) == 0)
31988 if_modifier = OMP_TASKLOOP;
31989 else if (strcmp ("target", p) == 0)
31991 if_modifier = OMP_TARGET;
31992 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31994 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31995 p = IDENTIFIER_POINTER (id);
31996 if (strcmp ("data", p) == 0)
31997 if_modifier = OMP_TARGET_DATA;
31998 else if (strcmp ("update", p) == 0)
31999 if_modifier = OMP_TARGET_UPDATE;
32000 else if (strcmp ("enter", p) == 0)
32001 if_modifier = OMP_TARGET_ENTER_DATA;
32002 else if (strcmp ("exit", p) == 0)
32003 if_modifier = OMP_TARGET_EXIT_DATA;
32004 if (if_modifier != OMP_TARGET)
32005 n = 3;
32006 else
32008 location_t loc
32009 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32010 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32011 "or %<exit%>");
32012 if_modifier = ERROR_MARK;
32014 if (if_modifier == OMP_TARGET_ENTER_DATA
32015 || if_modifier == OMP_TARGET_EXIT_DATA)
32017 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32019 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32020 p = IDENTIFIER_POINTER (id);
32021 if (strcmp ("data", p) == 0)
32022 n = 4;
32024 if (n != 4)
32026 location_t loc
32027 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32028 error_at (loc, "expected %<data%>");
32029 if_modifier = ERROR_MARK;
32034 if (if_modifier != ERROR_MARK)
32036 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32038 while (n-- > 0)
32039 cp_lexer_consume_token (parser->lexer);
32041 else
32043 if (n > 2)
32045 location_t loc
32046 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32047 error_at (loc, "expected %<:%>");
32049 if_modifier = ERROR_MARK;
32054 t = cp_parser_condition (parser);
32056 if (t == error_mark_node
32057 || !parens.require_close (parser))
32058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32059 /*or_comma=*/false,
32060 /*consume_paren=*/true);
32062 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32063 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32065 if (if_modifier != ERROR_MARK
32066 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32068 const char *p = NULL;
32069 switch (if_modifier)
32071 case OMP_PARALLEL: p = "parallel"; break;
32072 case OMP_TASK: p = "task"; break;
32073 case OMP_TASKLOOP: p = "taskloop"; break;
32074 case OMP_TARGET_DATA: p = "target data"; break;
32075 case OMP_TARGET: p = "target"; break;
32076 case OMP_TARGET_UPDATE: p = "target update"; break;
32077 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32078 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32079 default: gcc_unreachable ();
32081 error_at (location, "too many %<if%> clauses with %qs modifier",
32083 return list;
32085 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32087 if (!is_omp)
32088 error_at (location, "too many %<if%> clauses");
32089 else
32090 error_at (location, "too many %<if%> clauses without modifier");
32091 return list;
32093 else if (if_modifier == ERROR_MARK
32094 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32096 error_at (location, "if any %<if%> clause has modifier, then all "
32097 "%<if%> clauses have to use modifier");
32098 return list;
32102 c = build_omp_clause (location, OMP_CLAUSE_IF);
32103 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32104 OMP_CLAUSE_IF_EXPR (c) = t;
32105 OMP_CLAUSE_CHAIN (c) = list;
32107 return c;
32110 /* OpenMP 3.1:
32111 mergeable */
32113 static tree
32114 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32115 tree list, location_t location)
32117 tree c;
32119 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32120 location);
32122 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32123 OMP_CLAUSE_CHAIN (c) = list;
32124 return c;
32127 /* OpenMP 2.5:
32128 nowait */
32130 static tree
32131 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32132 tree list, location_t location)
32134 tree c;
32136 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32138 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32139 OMP_CLAUSE_CHAIN (c) = list;
32140 return c;
32143 /* OpenMP 2.5:
32144 num_threads ( expression ) */
32146 static tree
32147 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32148 location_t location)
32150 tree t, c;
32152 matching_parens parens;
32153 if (!parens.require_open (parser))
32154 return list;
32156 t = cp_parser_expression (parser);
32158 if (t == error_mark_node
32159 || !parens.require_close (parser))
32160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32161 /*or_comma=*/false,
32162 /*consume_paren=*/true);
32164 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32165 "num_threads", location);
32167 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32168 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32169 OMP_CLAUSE_CHAIN (c) = list;
32171 return c;
32174 /* OpenMP 4.5:
32175 num_tasks ( expression ) */
32177 static tree
32178 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32179 location_t location)
32181 tree t, c;
32183 matching_parens parens;
32184 if (!parens.require_open (parser))
32185 return list;
32187 t = cp_parser_expression (parser);
32189 if (t == error_mark_node
32190 || !parens.require_close (parser))
32191 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32192 /*or_comma=*/false,
32193 /*consume_paren=*/true);
32195 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32196 "num_tasks", location);
32198 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32199 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32200 OMP_CLAUSE_CHAIN (c) = list;
32202 return c;
32205 /* OpenMP 4.5:
32206 grainsize ( expression ) */
32208 static tree
32209 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32210 location_t location)
32212 tree t, c;
32214 matching_parens parens;
32215 if (!parens.require_open (parser))
32216 return list;
32218 t = cp_parser_expression (parser);
32220 if (t == error_mark_node
32221 || !parens.require_close (parser))
32222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32223 /*or_comma=*/false,
32224 /*consume_paren=*/true);
32226 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32227 "grainsize", location);
32229 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32230 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32231 OMP_CLAUSE_CHAIN (c) = list;
32233 return c;
32236 /* OpenMP 4.5:
32237 priority ( expression ) */
32239 static tree
32240 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32241 location_t location)
32243 tree t, c;
32245 matching_parens parens;
32246 if (!parens.require_open (parser))
32247 return list;
32249 t = cp_parser_expression (parser);
32251 if (t == error_mark_node
32252 || !parens.require_close (parser))
32253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32254 /*or_comma=*/false,
32255 /*consume_paren=*/true);
32257 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32258 "priority", location);
32260 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32261 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32262 OMP_CLAUSE_CHAIN (c) = list;
32264 return c;
32267 /* OpenMP 4.5:
32268 hint ( expression ) */
32270 static tree
32271 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32272 location_t location)
32274 tree t, c;
32276 matching_parens parens;
32277 if (!parens.require_open (parser))
32278 return list;
32280 t = cp_parser_expression (parser);
32282 if (t == error_mark_node
32283 || !parens.require_close (parser))
32284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32285 /*or_comma=*/false,
32286 /*consume_paren=*/true);
32288 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32290 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32291 OMP_CLAUSE_HINT_EXPR (c) = t;
32292 OMP_CLAUSE_CHAIN (c) = list;
32294 return c;
32297 /* OpenMP 4.5:
32298 defaultmap ( tofrom : scalar ) */
32300 static tree
32301 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32302 location_t location)
32304 tree c, id;
32305 const char *p;
32307 matching_parens parens;
32308 if (!parens.require_open (parser))
32309 return list;
32311 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32313 cp_parser_error (parser, "expected %<tofrom%>");
32314 goto out_err;
32316 id = cp_lexer_peek_token (parser->lexer)->u.value;
32317 p = IDENTIFIER_POINTER (id);
32318 if (strcmp (p, "tofrom") != 0)
32320 cp_parser_error (parser, "expected %<tofrom%>");
32321 goto out_err;
32323 cp_lexer_consume_token (parser->lexer);
32324 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32325 goto out_err;
32327 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32329 cp_parser_error (parser, "expected %<scalar%>");
32330 goto out_err;
32332 id = cp_lexer_peek_token (parser->lexer)->u.value;
32333 p = IDENTIFIER_POINTER (id);
32334 if (strcmp (p, "scalar") != 0)
32336 cp_parser_error (parser, "expected %<scalar%>");
32337 goto out_err;
32339 cp_lexer_consume_token (parser->lexer);
32340 if (!parens.require_close (parser))
32341 goto out_err;
32343 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32344 location);
32346 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32347 OMP_CLAUSE_CHAIN (c) = list;
32348 return c;
32350 out_err:
32351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32352 /*or_comma=*/false,
32353 /*consume_paren=*/true);
32354 return list;
32357 /* OpenMP 2.5:
32358 ordered
32360 OpenMP 4.5:
32361 ordered ( constant-expression ) */
32363 static tree
32364 cp_parser_omp_clause_ordered (cp_parser *parser,
32365 tree list, location_t location)
32367 tree c, num = NULL_TREE;
32368 HOST_WIDE_INT n;
32370 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32371 "ordered", location);
32373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32375 matching_parens parens;
32376 parens.consume_open (parser);
32378 num = cp_parser_constant_expression (parser);
32380 if (!parens.require_close (parser))
32381 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32382 /*or_comma=*/false,
32383 /*consume_paren=*/true);
32385 if (num == error_mark_node)
32386 return list;
32387 num = fold_non_dependent_expr (num);
32388 if (!tree_fits_shwi_p (num)
32389 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32390 || (n = tree_to_shwi (num)) <= 0
32391 || (int) n != n)
32393 error_at (location,
32394 "ordered argument needs positive constant integer "
32395 "expression");
32396 return list;
32400 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32401 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32402 OMP_CLAUSE_CHAIN (c) = list;
32403 return c;
32406 /* OpenMP 2.5:
32407 reduction ( reduction-operator : variable-list )
32409 reduction-operator:
32410 One of: + * - & ^ | && ||
32412 OpenMP 3.1:
32414 reduction-operator:
32415 One of: + * - & ^ | && || min max
32417 OpenMP 4.0:
32419 reduction-operator:
32420 One of: + * - & ^ | && ||
32421 id-expression */
32423 static tree
32424 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32426 enum tree_code code = ERROR_MARK;
32427 tree nlist, c, id = NULL_TREE;
32429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32430 return list;
32432 switch (cp_lexer_peek_token (parser->lexer)->type)
32434 case CPP_PLUS: code = PLUS_EXPR; break;
32435 case CPP_MULT: code = MULT_EXPR; break;
32436 case CPP_MINUS: code = MINUS_EXPR; break;
32437 case CPP_AND: code = BIT_AND_EXPR; break;
32438 case CPP_XOR: code = BIT_XOR_EXPR; break;
32439 case CPP_OR: code = BIT_IOR_EXPR; break;
32440 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32441 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32442 default: break;
32445 if (code != ERROR_MARK)
32446 cp_lexer_consume_token (parser->lexer);
32447 else
32449 bool saved_colon_corrects_to_scope_p;
32450 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32451 parser->colon_corrects_to_scope_p = false;
32452 id = cp_parser_id_expression (parser, /*template_p=*/false,
32453 /*check_dependency_p=*/true,
32454 /*template_p=*/NULL,
32455 /*declarator_p=*/false,
32456 /*optional_p=*/false);
32457 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32458 if (identifier_p (id))
32460 const char *p = IDENTIFIER_POINTER (id);
32462 if (strcmp (p, "min") == 0)
32463 code = MIN_EXPR;
32464 else if (strcmp (p, "max") == 0)
32465 code = MAX_EXPR;
32466 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32467 code = PLUS_EXPR;
32468 else if (id == ovl_op_identifier (false, MULT_EXPR))
32469 code = MULT_EXPR;
32470 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32471 code = MINUS_EXPR;
32472 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32473 code = BIT_AND_EXPR;
32474 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32475 code = BIT_IOR_EXPR;
32476 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32477 code = BIT_XOR_EXPR;
32478 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32479 code = TRUTH_ANDIF_EXPR;
32480 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32481 code = TRUTH_ORIF_EXPR;
32482 id = omp_reduction_id (code, id, NULL_TREE);
32483 tree scope = parser->scope;
32484 if (scope)
32485 id = build_qualified_name (NULL_TREE, scope, id, false);
32486 parser->scope = NULL_TREE;
32487 parser->qualifying_scope = NULL_TREE;
32488 parser->object_scope = NULL_TREE;
32490 else
32492 error ("invalid reduction-identifier");
32493 resync_fail:
32494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32495 /*or_comma=*/false,
32496 /*consume_paren=*/true);
32497 return list;
32501 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32502 goto resync_fail;
32504 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32505 NULL);
32506 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32508 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32509 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32512 return nlist;
32515 /* OpenMP 2.5:
32516 schedule ( schedule-kind )
32517 schedule ( schedule-kind , expression )
32519 schedule-kind:
32520 static | dynamic | guided | runtime | auto
32522 OpenMP 4.5:
32523 schedule ( schedule-modifier : schedule-kind )
32524 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32526 schedule-modifier:
32527 simd
32528 monotonic
32529 nonmonotonic */
32531 static tree
32532 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32534 tree c, t;
32535 int modifiers = 0, nmodifiers = 0;
32537 matching_parens parens;
32538 if (!parens.require_open (parser))
32539 return list;
32541 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32543 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32546 const char *p = IDENTIFIER_POINTER (id);
32547 if (strcmp ("simd", p) == 0)
32548 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32549 else if (strcmp ("monotonic", p) == 0)
32550 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32551 else if (strcmp ("nonmonotonic", p) == 0)
32552 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32553 else
32554 break;
32555 cp_lexer_consume_token (parser->lexer);
32556 if (nmodifiers++ == 0
32557 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32558 cp_lexer_consume_token (parser->lexer);
32559 else
32561 cp_parser_require (parser, CPP_COLON, RT_COLON);
32562 break;
32566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32569 const char *p = IDENTIFIER_POINTER (id);
32571 switch (p[0])
32573 case 'd':
32574 if (strcmp ("dynamic", p) != 0)
32575 goto invalid_kind;
32576 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32577 break;
32579 case 'g':
32580 if (strcmp ("guided", p) != 0)
32581 goto invalid_kind;
32582 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32583 break;
32585 case 'r':
32586 if (strcmp ("runtime", p) != 0)
32587 goto invalid_kind;
32588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32589 break;
32591 default:
32592 goto invalid_kind;
32595 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32596 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32597 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32598 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32599 else
32600 goto invalid_kind;
32601 cp_lexer_consume_token (parser->lexer);
32603 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32604 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32605 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32606 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32608 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32609 "specified");
32610 modifiers = 0;
32613 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32615 cp_token *token;
32616 cp_lexer_consume_token (parser->lexer);
32618 token = cp_lexer_peek_token (parser->lexer);
32619 t = cp_parser_assignment_expression (parser);
32621 if (t == error_mark_node)
32622 goto resync_fail;
32623 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32624 error_at (token->location, "schedule %<runtime%> does not take "
32625 "a %<chunk_size%> parameter");
32626 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32627 error_at (token->location, "schedule %<auto%> does not take "
32628 "a %<chunk_size%> parameter");
32629 else
32630 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32632 if (!parens.require_close (parser))
32633 goto resync_fail;
32635 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32636 goto resync_fail;
32638 OMP_CLAUSE_SCHEDULE_KIND (c)
32639 = (enum omp_clause_schedule_kind)
32640 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32642 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32643 OMP_CLAUSE_CHAIN (c) = list;
32644 return c;
32646 invalid_kind:
32647 cp_parser_error (parser, "invalid schedule kind");
32648 resync_fail:
32649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32650 /*or_comma=*/false,
32651 /*consume_paren=*/true);
32652 return list;
32655 /* OpenMP 3.0:
32656 untied */
32658 static tree
32659 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32660 tree list, location_t location)
32662 tree c;
32664 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32666 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32667 OMP_CLAUSE_CHAIN (c) = list;
32668 return c;
32671 /* OpenMP 4.0:
32672 inbranch
32673 notinbranch */
32675 static tree
32676 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32677 tree list, location_t location)
32679 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32680 tree c = build_omp_clause (location, code);
32681 OMP_CLAUSE_CHAIN (c) = list;
32682 return c;
32685 /* OpenMP 4.0:
32686 parallel
32688 sections
32689 taskgroup */
32691 static tree
32692 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32693 enum omp_clause_code code,
32694 tree list, location_t location)
32696 tree c = build_omp_clause (location, code);
32697 OMP_CLAUSE_CHAIN (c) = list;
32698 return c;
32701 /* OpenMP 4.5:
32702 nogroup */
32704 static tree
32705 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32706 tree list, location_t location)
32708 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32709 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32710 OMP_CLAUSE_CHAIN (c) = list;
32711 return c;
32714 /* OpenMP 4.5:
32715 simd
32716 threads */
32718 static tree
32719 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32720 enum omp_clause_code code,
32721 tree list, location_t location)
32723 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32724 tree c = build_omp_clause (location, code);
32725 OMP_CLAUSE_CHAIN (c) = list;
32726 return c;
32729 /* OpenMP 4.0:
32730 num_teams ( expression ) */
32732 static tree
32733 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32734 location_t location)
32736 tree t, c;
32738 matching_parens parens;
32739 if (!parens.require_open (parser))
32740 return list;
32742 t = cp_parser_expression (parser);
32744 if (t == error_mark_node
32745 || !parens.require_close (parser))
32746 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32747 /*or_comma=*/false,
32748 /*consume_paren=*/true);
32750 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32751 "num_teams", location);
32753 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32754 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32755 OMP_CLAUSE_CHAIN (c) = list;
32757 return c;
32760 /* OpenMP 4.0:
32761 thread_limit ( expression ) */
32763 static tree
32764 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32765 location_t location)
32767 tree t, c;
32769 matching_parens parens;
32770 if (!parens.require_open (parser))
32771 return list;
32773 t = cp_parser_expression (parser);
32775 if (t == error_mark_node
32776 || !parens.require_close (parser))
32777 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32778 /*or_comma=*/false,
32779 /*consume_paren=*/true);
32781 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32782 "thread_limit", location);
32784 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32785 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32786 OMP_CLAUSE_CHAIN (c) = list;
32788 return c;
32791 /* OpenMP 4.0:
32792 aligned ( variable-list )
32793 aligned ( variable-list : constant-expression ) */
32795 static tree
32796 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32798 tree nlist, c, alignment = NULL_TREE;
32799 bool colon;
32801 matching_parens parens;
32802 if (!parens.require_open (parser))
32803 return list;
32805 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32806 &colon);
32808 if (colon)
32810 alignment = cp_parser_constant_expression (parser);
32812 if (!parens.require_close (parser))
32813 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32814 /*or_comma=*/false,
32815 /*consume_paren=*/true);
32817 if (alignment == error_mark_node)
32818 alignment = NULL_TREE;
32821 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32822 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32824 return nlist;
32827 /* OpenMP 4.0:
32828 linear ( variable-list )
32829 linear ( variable-list : expression )
32831 OpenMP 4.5:
32832 linear ( modifier ( variable-list ) )
32833 linear ( modifier ( variable-list ) : expression ) */
32835 static tree
32836 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32837 bool declare_simd)
32839 tree nlist, c, step = integer_one_node;
32840 bool colon;
32841 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32843 matching_parens parens;
32844 if (!parens.require_open (parser))
32845 return list;
32847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32850 const char *p = IDENTIFIER_POINTER (id);
32852 if (strcmp ("ref", p) == 0)
32853 kind = OMP_CLAUSE_LINEAR_REF;
32854 else if (strcmp ("val", p) == 0)
32855 kind = OMP_CLAUSE_LINEAR_VAL;
32856 else if (strcmp ("uval", p) == 0)
32857 kind = OMP_CLAUSE_LINEAR_UVAL;
32858 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32859 cp_lexer_consume_token (parser->lexer);
32860 else
32861 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32864 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32865 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32866 &colon);
32867 else
32869 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32870 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32871 if (colon)
32872 cp_parser_require (parser, CPP_COLON, RT_COLON);
32873 else if (!parens.require_close (parser))
32874 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32875 /*or_comma=*/false,
32876 /*consume_paren=*/true);
32879 if (colon)
32881 step = NULL_TREE;
32882 if (declare_simd
32883 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32884 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32886 cp_token *token = cp_lexer_peek_token (parser->lexer);
32887 cp_parser_parse_tentatively (parser);
32888 step = cp_parser_id_expression (parser, /*template_p=*/false,
32889 /*check_dependency_p=*/true,
32890 /*template_p=*/NULL,
32891 /*declarator_p=*/false,
32892 /*optional_p=*/false);
32893 if (step != error_mark_node)
32894 step = cp_parser_lookup_name_simple (parser, step, token->location);
32895 if (step == error_mark_node)
32897 step = NULL_TREE;
32898 cp_parser_abort_tentative_parse (parser);
32900 else if (!cp_parser_parse_definitely (parser))
32901 step = NULL_TREE;
32903 if (!step)
32904 step = cp_parser_expression (parser);
32906 if (!parens.require_close (parser))
32907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32908 /*or_comma=*/false,
32909 /*consume_paren=*/true);
32911 if (step == error_mark_node)
32912 return list;
32915 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32917 OMP_CLAUSE_LINEAR_STEP (c) = step;
32918 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32921 return nlist;
32924 /* OpenMP 4.0:
32925 safelen ( constant-expression ) */
32927 static tree
32928 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32929 location_t location)
32931 tree t, c;
32933 matching_parens parens;
32934 if (!parens.require_open (parser))
32935 return list;
32937 t = cp_parser_constant_expression (parser);
32939 if (t == error_mark_node
32940 || !parens.require_close (parser))
32941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32942 /*or_comma=*/false,
32943 /*consume_paren=*/true);
32945 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32947 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32948 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32949 OMP_CLAUSE_CHAIN (c) = list;
32951 return c;
32954 /* OpenMP 4.0:
32955 simdlen ( constant-expression ) */
32957 static tree
32958 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32959 location_t location)
32961 tree t, c;
32963 matching_parens parens;
32964 if (!parens.require_open (parser))
32965 return list;
32967 t = cp_parser_constant_expression (parser);
32969 if (t == error_mark_node
32970 || !parens.require_close (parser))
32971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32972 /*or_comma=*/false,
32973 /*consume_paren=*/true);
32975 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32977 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32978 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32979 OMP_CLAUSE_CHAIN (c) = list;
32981 return c;
32984 /* OpenMP 4.5:
32985 vec:
32986 identifier [+/- integer]
32987 vec , identifier [+/- integer]
32990 static tree
32991 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32992 tree list)
32994 tree vec = NULL;
32996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32998 cp_parser_error (parser, "expected identifier");
32999 return list;
33002 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33004 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33005 tree t, identifier = cp_parser_identifier (parser);
33006 tree addend = NULL;
33008 if (identifier == error_mark_node)
33009 t = error_mark_node;
33010 else
33012 t = cp_parser_lookup_name_simple
33013 (parser, identifier,
33014 cp_lexer_peek_token (parser->lexer)->location);
33015 if (t == error_mark_node)
33016 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33017 id_loc);
33020 bool neg = false;
33021 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33022 neg = true;
33023 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33025 addend = integer_zero_node;
33026 goto add_to_vector;
33028 cp_lexer_consume_token (parser->lexer);
33030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33032 cp_parser_error (parser, "expected integer");
33033 return list;
33036 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33037 if (TREE_CODE (addend) != INTEGER_CST)
33039 cp_parser_error (parser, "expected integer");
33040 return list;
33042 cp_lexer_consume_token (parser->lexer);
33044 add_to_vector:
33045 if (t != error_mark_node)
33047 vec = tree_cons (addend, t, vec);
33048 if (neg)
33049 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33053 break;
33055 cp_lexer_consume_token (parser->lexer);
33058 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33060 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33061 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33062 OMP_CLAUSE_DECL (u) = nreverse (vec);
33063 OMP_CLAUSE_CHAIN (u) = list;
33064 return u;
33066 return list;
33069 /* OpenMP 4.0:
33070 depend ( depend-kind : variable-list )
33072 depend-kind:
33073 in | out | inout
33075 OpenMP 4.5:
33076 depend ( source )
33078 depend ( sink : vec ) */
33080 static tree
33081 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33083 tree nlist, c;
33084 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33086 matching_parens parens;
33087 if (!parens.require_open (parser))
33088 return list;
33090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33093 const char *p = IDENTIFIER_POINTER (id);
33095 if (strcmp ("in", p) == 0)
33096 kind = OMP_CLAUSE_DEPEND_IN;
33097 else if (strcmp ("inout", p) == 0)
33098 kind = OMP_CLAUSE_DEPEND_INOUT;
33099 else if (strcmp ("out", p) == 0)
33100 kind = OMP_CLAUSE_DEPEND_OUT;
33101 else if (strcmp ("source", p) == 0)
33102 kind = OMP_CLAUSE_DEPEND_SOURCE;
33103 else if (strcmp ("sink", p) == 0)
33104 kind = OMP_CLAUSE_DEPEND_SINK;
33105 else
33106 goto invalid_kind;
33108 else
33109 goto invalid_kind;
33111 cp_lexer_consume_token (parser->lexer);
33113 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33115 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33116 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33117 OMP_CLAUSE_DECL (c) = NULL_TREE;
33118 OMP_CLAUSE_CHAIN (c) = list;
33119 if (!parens.require_close (parser))
33120 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33121 /*or_comma=*/false,
33122 /*consume_paren=*/true);
33123 return c;
33126 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33127 goto resync_fail;
33129 if (kind == OMP_CLAUSE_DEPEND_SINK)
33130 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33131 else
33133 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33134 list, NULL);
33136 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33137 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33139 return nlist;
33141 invalid_kind:
33142 cp_parser_error (parser, "invalid depend kind");
33143 resync_fail:
33144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33145 /*or_comma=*/false,
33146 /*consume_paren=*/true);
33147 return list;
33150 /* OpenMP 4.0:
33151 map ( map-kind : variable-list )
33152 map ( variable-list )
33154 map-kind:
33155 alloc | to | from | tofrom
33157 OpenMP 4.5:
33158 map-kind:
33159 alloc | to | from | tofrom | release | delete
33161 map ( always [,] map-kind: variable-list ) */
33163 static tree
33164 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33166 tree nlist, c;
33167 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33168 bool always = false;
33170 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33171 return list;
33173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33175 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33176 const char *p = IDENTIFIER_POINTER (id);
33178 if (strcmp ("always", p) == 0)
33180 int nth = 2;
33181 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33182 nth++;
33183 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33184 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33185 == RID_DELETE))
33186 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33187 == CPP_COLON))
33189 always = true;
33190 cp_lexer_consume_token (parser->lexer);
33191 if (nth == 3)
33192 cp_lexer_consume_token (parser->lexer);
33197 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33198 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33200 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33201 const char *p = IDENTIFIER_POINTER (id);
33203 if (strcmp ("alloc", p) == 0)
33204 kind = GOMP_MAP_ALLOC;
33205 else if (strcmp ("to", p) == 0)
33206 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33207 else if (strcmp ("from", p) == 0)
33208 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33209 else if (strcmp ("tofrom", p) == 0)
33210 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33211 else if (strcmp ("release", p) == 0)
33212 kind = GOMP_MAP_RELEASE;
33213 else
33215 cp_parser_error (parser, "invalid map kind");
33216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33217 /*or_comma=*/false,
33218 /*consume_paren=*/true);
33219 return list;
33221 cp_lexer_consume_token (parser->lexer);
33222 cp_lexer_consume_token (parser->lexer);
33224 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33225 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33227 kind = GOMP_MAP_DELETE;
33228 cp_lexer_consume_token (parser->lexer);
33229 cp_lexer_consume_token (parser->lexer);
33232 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33233 NULL);
33235 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33236 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33238 return nlist;
33241 /* OpenMP 4.0:
33242 device ( expression ) */
33244 static tree
33245 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33246 location_t location)
33248 tree t, c;
33250 matching_parens parens;
33251 if (!parens.require_open (parser))
33252 return list;
33254 t = cp_parser_expression (parser);
33256 if (t == error_mark_node
33257 || !parens.require_close (parser))
33258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33259 /*or_comma=*/false,
33260 /*consume_paren=*/true);
33262 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33263 "device", location);
33265 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33266 OMP_CLAUSE_DEVICE_ID (c) = t;
33267 OMP_CLAUSE_CHAIN (c) = list;
33269 return c;
33272 /* OpenMP 4.0:
33273 dist_schedule ( static )
33274 dist_schedule ( static , expression ) */
33276 static tree
33277 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33278 location_t location)
33280 tree c, t;
33282 matching_parens parens;
33283 if (!parens.require_open (parser))
33284 return list;
33286 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33288 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33289 goto invalid_kind;
33290 cp_lexer_consume_token (parser->lexer);
33292 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33294 cp_lexer_consume_token (parser->lexer);
33296 t = cp_parser_assignment_expression (parser);
33298 if (t == error_mark_node)
33299 goto resync_fail;
33300 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33302 if (!parens.require_close (parser))
33303 goto resync_fail;
33305 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33306 goto resync_fail;
33308 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33309 location);
33310 OMP_CLAUSE_CHAIN (c) = list;
33311 return c;
33313 invalid_kind:
33314 cp_parser_error (parser, "invalid dist_schedule kind");
33315 resync_fail:
33316 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33317 /*or_comma=*/false,
33318 /*consume_paren=*/true);
33319 return list;
33322 /* OpenMP 4.0:
33323 proc_bind ( proc-bind-kind )
33325 proc-bind-kind:
33326 master | close | spread */
33328 static tree
33329 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33330 location_t location)
33332 tree c;
33333 enum omp_clause_proc_bind_kind kind;
33335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33336 return list;
33338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33340 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33341 const char *p = IDENTIFIER_POINTER (id);
33343 if (strcmp ("master", p) == 0)
33344 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33345 else if (strcmp ("close", p) == 0)
33346 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33347 else if (strcmp ("spread", p) == 0)
33348 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33349 else
33350 goto invalid_kind;
33352 else
33353 goto invalid_kind;
33355 cp_lexer_consume_token (parser->lexer);
33356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33357 goto resync_fail;
33359 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33360 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33361 location);
33362 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33363 OMP_CLAUSE_CHAIN (c) = list;
33364 return c;
33366 invalid_kind:
33367 cp_parser_error (parser, "invalid depend kind");
33368 resync_fail:
33369 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33370 /*or_comma=*/false,
33371 /*consume_paren=*/true);
33372 return list;
33375 /* OpenACC:
33376 async [( int-expr )] */
33378 static tree
33379 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33381 tree c, t;
33382 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33384 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33386 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33388 matching_parens parens;
33389 parens.consume_open (parser);
33391 t = cp_parser_expression (parser);
33392 if (t == error_mark_node
33393 || !parens.require_close (parser))
33394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33395 /*or_comma=*/false,
33396 /*consume_paren=*/true);
33399 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33401 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33402 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33403 OMP_CLAUSE_CHAIN (c) = list;
33404 list = c;
33406 return list;
33409 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33410 is a bitmask in MASK. Return the list of clauses found. */
33412 static tree
33413 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33414 const char *where, cp_token *pragma_tok,
33415 bool finish_p = true)
33417 tree clauses = NULL;
33418 bool first = true;
33420 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33422 location_t here;
33423 pragma_omp_clause c_kind;
33424 omp_clause_code code;
33425 const char *c_name;
33426 tree prev = clauses;
33428 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33429 cp_lexer_consume_token (parser->lexer);
33431 here = cp_lexer_peek_token (parser->lexer)->location;
33432 c_kind = cp_parser_omp_clause_name (parser);
33434 switch (c_kind)
33436 case PRAGMA_OACC_CLAUSE_ASYNC:
33437 clauses = cp_parser_oacc_clause_async (parser, clauses);
33438 c_name = "async";
33439 break;
33440 case PRAGMA_OACC_CLAUSE_AUTO:
33441 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33442 clauses, here);
33443 c_name = "auto";
33444 break;
33445 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33446 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33447 c_name = "collapse";
33448 break;
33449 case PRAGMA_OACC_CLAUSE_COPY:
33450 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33451 c_name = "copy";
33452 break;
33453 case PRAGMA_OACC_CLAUSE_COPYIN:
33454 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33455 c_name = "copyin";
33456 break;
33457 case PRAGMA_OACC_CLAUSE_COPYOUT:
33458 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33459 c_name = "copyout";
33460 break;
33461 case PRAGMA_OACC_CLAUSE_CREATE:
33462 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33463 c_name = "create";
33464 break;
33465 case PRAGMA_OACC_CLAUSE_DELETE:
33466 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33467 c_name = "delete";
33468 break;
33469 case PRAGMA_OMP_CLAUSE_DEFAULT:
33470 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33471 c_name = "default";
33472 break;
33473 case PRAGMA_OACC_CLAUSE_DEVICE:
33474 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33475 c_name = "device";
33476 break;
33477 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33478 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33479 c_name = "deviceptr";
33480 break;
33481 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33482 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33483 c_name = "device_resident";
33484 break;
33485 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33486 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33487 clauses);
33488 c_name = "firstprivate";
33489 break;
33490 case PRAGMA_OACC_CLAUSE_GANG:
33491 c_name = "gang";
33492 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33493 c_name, clauses);
33494 break;
33495 case PRAGMA_OACC_CLAUSE_HOST:
33496 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33497 c_name = "host";
33498 break;
33499 case PRAGMA_OACC_CLAUSE_IF:
33500 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33501 c_name = "if";
33502 break;
33503 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33504 clauses = cp_parser_oacc_simple_clause (parser,
33505 OMP_CLAUSE_INDEPENDENT,
33506 clauses, here);
33507 c_name = "independent";
33508 break;
33509 case PRAGMA_OACC_CLAUSE_LINK:
33510 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33511 c_name = "link";
33512 break;
33513 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33514 code = OMP_CLAUSE_NUM_GANGS;
33515 c_name = "num_gangs";
33516 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33517 clauses);
33518 break;
33519 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33520 c_name = "num_workers";
33521 code = OMP_CLAUSE_NUM_WORKERS;
33522 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33523 clauses);
33524 break;
33525 case PRAGMA_OACC_CLAUSE_PRESENT:
33526 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33527 c_name = "present";
33528 break;
33529 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33530 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33531 c_name = "present_or_copy";
33532 break;
33533 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33534 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33535 c_name = "present_or_copyin";
33536 break;
33537 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33538 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33539 c_name = "present_or_copyout";
33540 break;
33541 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33542 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33543 c_name = "present_or_create";
33544 break;
33545 case PRAGMA_OACC_CLAUSE_PRIVATE:
33546 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33547 clauses);
33548 c_name = "private";
33549 break;
33550 case PRAGMA_OACC_CLAUSE_REDUCTION:
33551 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33552 c_name = "reduction";
33553 break;
33554 case PRAGMA_OACC_CLAUSE_SELF:
33555 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33556 c_name = "self";
33557 break;
33558 case PRAGMA_OACC_CLAUSE_SEQ:
33559 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33560 clauses, here);
33561 c_name = "seq";
33562 break;
33563 case PRAGMA_OACC_CLAUSE_TILE:
33564 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33565 c_name = "tile";
33566 break;
33567 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33568 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33569 clauses);
33570 c_name = "use_device";
33571 break;
33572 case PRAGMA_OACC_CLAUSE_VECTOR:
33573 c_name = "vector";
33574 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33575 c_name, clauses);
33576 break;
33577 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33578 c_name = "vector_length";
33579 code = OMP_CLAUSE_VECTOR_LENGTH;
33580 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33581 clauses);
33582 break;
33583 case PRAGMA_OACC_CLAUSE_WAIT:
33584 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33585 c_name = "wait";
33586 break;
33587 case PRAGMA_OACC_CLAUSE_WORKER:
33588 c_name = "worker";
33589 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33590 c_name, clauses);
33591 break;
33592 default:
33593 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33594 goto saw_error;
33597 first = false;
33599 if (((mask >> c_kind) & 1) == 0)
33601 /* Remove the invalid clause(s) from the list to avoid
33602 confusing the rest of the compiler. */
33603 clauses = prev;
33604 error_at (here, "%qs is not valid for %qs", c_name, where);
33608 saw_error:
33609 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33611 if (finish_p)
33612 return finish_omp_clauses (clauses, C_ORT_ACC);
33614 return clauses;
33617 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33618 is a bitmask in MASK. Return the list of clauses found; the result
33619 of clause default goes in *pdefault. */
33621 static tree
33622 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33623 const char *where, cp_token *pragma_tok,
33624 bool finish_p = true)
33626 tree clauses = NULL;
33627 bool first = true;
33628 cp_token *token = NULL;
33630 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33632 pragma_omp_clause c_kind;
33633 const char *c_name;
33634 tree prev = clauses;
33636 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33637 cp_lexer_consume_token (parser->lexer);
33639 token = cp_lexer_peek_token (parser->lexer);
33640 c_kind = cp_parser_omp_clause_name (parser);
33642 switch (c_kind)
33644 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33645 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33646 token->location);
33647 c_name = "collapse";
33648 break;
33649 case PRAGMA_OMP_CLAUSE_COPYIN:
33650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33651 c_name = "copyin";
33652 break;
33653 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33655 clauses);
33656 c_name = "copyprivate";
33657 break;
33658 case PRAGMA_OMP_CLAUSE_DEFAULT:
33659 clauses = cp_parser_omp_clause_default (parser, clauses,
33660 token->location, false);
33661 c_name = "default";
33662 break;
33663 case PRAGMA_OMP_CLAUSE_FINAL:
33664 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33665 c_name = "final";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33668 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33669 clauses);
33670 c_name = "firstprivate";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33673 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33674 token->location);
33675 c_name = "grainsize";
33676 break;
33677 case PRAGMA_OMP_CLAUSE_HINT:
33678 clauses = cp_parser_omp_clause_hint (parser, clauses,
33679 token->location);
33680 c_name = "hint";
33681 break;
33682 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33683 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33684 token->location);
33685 c_name = "defaultmap";
33686 break;
33687 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33689 clauses);
33690 c_name = "use_device_ptr";
33691 break;
33692 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33693 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33694 clauses);
33695 c_name = "is_device_ptr";
33696 break;
33697 case PRAGMA_OMP_CLAUSE_IF:
33698 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33699 true);
33700 c_name = "if";
33701 break;
33702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33704 clauses);
33705 c_name = "lastprivate";
33706 break;
33707 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33708 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33709 token->location);
33710 c_name = "mergeable";
33711 break;
33712 case PRAGMA_OMP_CLAUSE_NOWAIT:
33713 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33714 c_name = "nowait";
33715 break;
33716 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33717 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33718 token->location);
33719 c_name = "num_tasks";
33720 break;
33721 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33722 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33723 token->location);
33724 c_name = "num_threads";
33725 break;
33726 case PRAGMA_OMP_CLAUSE_ORDERED:
33727 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33728 token->location);
33729 c_name = "ordered";
33730 break;
33731 case PRAGMA_OMP_CLAUSE_PRIORITY:
33732 clauses = cp_parser_omp_clause_priority (parser, clauses,
33733 token->location);
33734 c_name = "priority";
33735 break;
33736 case PRAGMA_OMP_CLAUSE_PRIVATE:
33737 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33738 clauses);
33739 c_name = "private";
33740 break;
33741 case PRAGMA_OMP_CLAUSE_REDUCTION:
33742 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33743 c_name = "reduction";
33744 break;
33745 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33746 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33747 token->location);
33748 c_name = "schedule";
33749 break;
33750 case PRAGMA_OMP_CLAUSE_SHARED:
33751 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33752 clauses);
33753 c_name = "shared";
33754 break;
33755 case PRAGMA_OMP_CLAUSE_UNTIED:
33756 clauses = cp_parser_omp_clause_untied (parser, clauses,
33757 token->location);
33758 c_name = "untied";
33759 break;
33760 case PRAGMA_OMP_CLAUSE_INBRANCH:
33761 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33762 clauses, token->location);
33763 c_name = "inbranch";
33764 break;
33765 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33766 clauses = cp_parser_omp_clause_branch (parser,
33767 OMP_CLAUSE_NOTINBRANCH,
33768 clauses, token->location);
33769 c_name = "notinbranch";
33770 break;
33771 case PRAGMA_OMP_CLAUSE_PARALLEL:
33772 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33773 clauses, token->location);
33774 c_name = "parallel";
33775 if (!first)
33777 clause_not_first:
33778 error_at (token->location, "%qs must be the first clause of %qs",
33779 c_name, where);
33780 clauses = prev;
33782 break;
33783 case PRAGMA_OMP_CLAUSE_FOR:
33784 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33785 clauses, token->location);
33786 c_name = "for";
33787 if (!first)
33788 goto clause_not_first;
33789 break;
33790 case PRAGMA_OMP_CLAUSE_SECTIONS:
33791 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33792 clauses, token->location);
33793 c_name = "sections";
33794 if (!first)
33795 goto clause_not_first;
33796 break;
33797 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33798 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33799 clauses, token->location);
33800 c_name = "taskgroup";
33801 if (!first)
33802 goto clause_not_first;
33803 break;
33804 case PRAGMA_OMP_CLAUSE_LINK:
33805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33806 c_name = "to";
33807 break;
33808 case PRAGMA_OMP_CLAUSE_TO:
33809 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33810 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33811 clauses);
33812 else
33813 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33814 c_name = "to";
33815 break;
33816 case PRAGMA_OMP_CLAUSE_FROM:
33817 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33818 c_name = "from";
33819 break;
33820 case PRAGMA_OMP_CLAUSE_UNIFORM:
33821 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33822 clauses);
33823 c_name = "uniform";
33824 break;
33825 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33826 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33827 token->location);
33828 c_name = "num_teams";
33829 break;
33830 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33831 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33832 token->location);
33833 c_name = "thread_limit";
33834 break;
33835 case PRAGMA_OMP_CLAUSE_ALIGNED:
33836 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33837 c_name = "aligned";
33838 break;
33839 case PRAGMA_OMP_CLAUSE_LINEAR:
33841 bool declare_simd = false;
33842 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33843 declare_simd = true;
33844 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33846 c_name = "linear";
33847 break;
33848 case PRAGMA_OMP_CLAUSE_DEPEND:
33849 clauses = cp_parser_omp_clause_depend (parser, clauses,
33850 token->location);
33851 c_name = "depend";
33852 break;
33853 case PRAGMA_OMP_CLAUSE_MAP:
33854 clauses = cp_parser_omp_clause_map (parser, clauses);
33855 c_name = "map";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_DEVICE:
33858 clauses = cp_parser_omp_clause_device (parser, clauses,
33859 token->location);
33860 c_name = "device";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33863 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33864 token->location);
33865 c_name = "dist_schedule";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33868 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33869 token->location);
33870 c_name = "proc_bind";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_SAFELEN:
33873 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33874 token->location);
33875 c_name = "safelen";
33876 break;
33877 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33878 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33879 token->location);
33880 c_name = "simdlen";
33881 break;
33882 case PRAGMA_OMP_CLAUSE_NOGROUP:
33883 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33884 token->location);
33885 c_name = "nogroup";
33886 break;
33887 case PRAGMA_OMP_CLAUSE_THREADS:
33888 clauses
33889 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33890 clauses, token->location);
33891 c_name = "threads";
33892 break;
33893 case PRAGMA_OMP_CLAUSE_SIMD:
33894 clauses
33895 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33896 clauses, token->location);
33897 c_name = "simd";
33898 break;
33899 default:
33900 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33901 goto saw_error;
33904 first = false;
33906 if (((mask >> c_kind) & 1) == 0)
33908 /* Remove the invalid clause(s) from the list to avoid
33909 confusing the rest of the compiler. */
33910 clauses = prev;
33911 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33914 saw_error:
33915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33916 if (finish_p)
33918 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33919 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33920 else
33921 return finish_omp_clauses (clauses, C_ORT_OMP);
33923 return clauses;
33926 /* OpenMP 2.5:
33927 structured-block:
33928 statement
33930 In practice, we're also interested in adding the statement to an
33931 outer node. So it is convenient if we work around the fact that
33932 cp_parser_statement calls add_stmt. */
33934 static unsigned
33935 cp_parser_begin_omp_structured_block (cp_parser *parser)
33937 unsigned save = parser->in_statement;
33939 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33940 This preserves the "not within loop or switch" style error messages
33941 for nonsense cases like
33942 void foo() {
33943 #pragma omp single
33944 break;
33947 if (parser->in_statement)
33948 parser->in_statement = IN_OMP_BLOCK;
33950 return save;
33953 static void
33954 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33956 parser->in_statement = save;
33959 static tree
33960 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33962 tree stmt = begin_omp_structured_block ();
33963 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33965 cp_parser_statement (parser, NULL_TREE, false, if_p);
33967 cp_parser_end_omp_structured_block (parser, save);
33968 return finish_omp_structured_block (stmt);
33971 /* OpenMP 2.5:
33972 # pragma omp atomic new-line
33973 expression-stmt
33975 expression-stmt:
33976 x binop= expr | x++ | ++x | x-- | --x
33977 binop:
33978 +, *, -, /, &, ^, |, <<, >>
33980 where x is an lvalue expression with scalar type.
33982 OpenMP 3.1:
33983 # pragma omp atomic new-line
33984 update-stmt
33986 # pragma omp atomic read new-line
33987 read-stmt
33989 # pragma omp atomic write new-line
33990 write-stmt
33992 # pragma omp atomic update new-line
33993 update-stmt
33995 # pragma omp atomic capture new-line
33996 capture-stmt
33998 # pragma omp atomic capture new-line
33999 capture-block
34001 read-stmt:
34002 v = x
34003 write-stmt:
34004 x = expr
34005 update-stmt:
34006 expression-stmt | x = x binop expr
34007 capture-stmt:
34008 v = expression-stmt
34009 capture-block:
34010 { v = x; update-stmt; } | { update-stmt; v = x; }
34012 OpenMP 4.0:
34013 update-stmt:
34014 expression-stmt | x = x binop expr | x = expr binop x
34015 capture-stmt:
34016 v = update-stmt
34017 capture-block:
34018 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34020 where x and v are lvalue expressions with scalar type. */
34022 static void
34023 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34025 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34026 tree rhs1 = NULL_TREE, orig_lhs;
34027 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34028 bool structured_block = false;
34029 bool seq_cst = false;
34031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34034 const char *p = IDENTIFIER_POINTER (id);
34036 if (!strcmp (p, "seq_cst"))
34038 seq_cst = true;
34039 cp_lexer_consume_token (parser->lexer);
34040 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34042 cp_lexer_consume_token (parser->lexer);
34045 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34047 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34048 const char *p = IDENTIFIER_POINTER (id);
34050 if (!strcmp (p, "read"))
34051 code = OMP_ATOMIC_READ;
34052 else if (!strcmp (p, "write"))
34053 code = NOP_EXPR;
34054 else if (!strcmp (p, "update"))
34055 code = OMP_ATOMIC;
34056 else if (!strcmp (p, "capture"))
34057 code = OMP_ATOMIC_CAPTURE_NEW;
34058 else
34059 p = NULL;
34060 if (p)
34061 cp_lexer_consume_token (parser->lexer);
34063 if (!seq_cst)
34065 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34066 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34067 cp_lexer_consume_token (parser->lexer);
34069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34072 const char *p = IDENTIFIER_POINTER (id);
34074 if (!strcmp (p, "seq_cst"))
34076 seq_cst = true;
34077 cp_lexer_consume_token (parser->lexer);
34081 cp_parser_require_pragma_eol (parser, pragma_tok);
34083 switch (code)
34085 case OMP_ATOMIC_READ:
34086 case NOP_EXPR: /* atomic write */
34087 v = cp_parser_unary_expression (parser);
34088 if (v == error_mark_node)
34089 goto saw_error;
34090 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34091 goto saw_error;
34092 if (code == NOP_EXPR)
34093 lhs = cp_parser_expression (parser);
34094 else
34095 lhs = cp_parser_unary_expression (parser);
34096 if (lhs == error_mark_node)
34097 goto saw_error;
34098 if (code == NOP_EXPR)
34100 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34101 opcode. */
34102 code = OMP_ATOMIC;
34103 rhs = lhs;
34104 lhs = v;
34105 v = NULL_TREE;
34107 goto done;
34108 case OMP_ATOMIC_CAPTURE_NEW:
34109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34111 cp_lexer_consume_token (parser->lexer);
34112 structured_block = true;
34114 else
34116 v = cp_parser_unary_expression (parser);
34117 if (v == error_mark_node)
34118 goto saw_error;
34119 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34120 goto saw_error;
34122 default:
34123 break;
34126 restart:
34127 lhs = cp_parser_unary_expression (parser);
34128 orig_lhs = lhs;
34129 switch (TREE_CODE (lhs))
34131 case ERROR_MARK:
34132 goto saw_error;
34134 case POSTINCREMENT_EXPR:
34135 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34136 code = OMP_ATOMIC_CAPTURE_OLD;
34137 /* FALLTHROUGH */
34138 case PREINCREMENT_EXPR:
34139 lhs = TREE_OPERAND (lhs, 0);
34140 opcode = PLUS_EXPR;
34141 rhs = integer_one_node;
34142 break;
34144 case POSTDECREMENT_EXPR:
34145 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34146 code = OMP_ATOMIC_CAPTURE_OLD;
34147 /* FALLTHROUGH */
34148 case PREDECREMENT_EXPR:
34149 lhs = TREE_OPERAND (lhs, 0);
34150 opcode = MINUS_EXPR;
34151 rhs = integer_one_node;
34152 break;
34154 case COMPOUND_EXPR:
34155 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34156 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34157 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34158 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34159 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34160 (TREE_OPERAND (lhs, 1), 0), 0)))
34161 == BOOLEAN_TYPE)
34162 /* Undo effects of boolean_increment for post {in,de}crement. */
34163 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34164 /* FALLTHRU */
34165 case MODIFY_EXPR:
34166 if (TREE_CODE (lhs) == MODIFY_EXPR
34167 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34169 /* Undo effects of boolean_increment. */
34170 if (integer_onep (TREE_OPERAND (lhs, 1)))
34172 /* This is pre or post increment. */
34173 rhs = TREE_OPERAND (lhs, 1);
34174 lhs = TREE_OPERAND (lhs, 0);
34175 opcode = NOP_EXPR;
34176 if (code == OMP_ATOMIC_CAPTURE_NEW
34177 && !structured_block
34178 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34179 code = OMP_ATOMIC_CAPTURE_OLD;
34180 break;
34183 /* FALLTHRU */
34184 default:
34185 switch (cp_lexer_peek_token (parser->lexer)->type)
34187 case CPP_MULT_EQ:
34188 opcode = MULT_EXPR;
34189 break;
34190 case CPP_DIV_EQ:
34191 opcode = TRUNC_DIV_EXPR;
34192 break;
34193 case CPP_PLUS_EQ:
34194 opcode = PLUS_EXPR;
34195 break;
34196 case CPP_MINUS_EQ:
34197 opcode = MINUS_EXPR;
34198 break;
34199 case CPP_LSHIFT_EQ:
34200 opcode = LSHIFT_EXPR;
34201 break;
34202 case CPP_RSHIFT_EQ:
34203 opcode = RSHIFT_EXPR;
34204 break;
34205 case CPP_AND_EQ:
34206 opcode = BIT_AND_EXPR;
34207 break;
34208 case CPP_OR_EQ:
34209 opcode = BIT_IOR_EXPR;
34210 break;
34211 case CPP_XOR_EQ:
34212 opcode = BIT_XOR_EXPR;
34213 break;
34214 case CPP_EQ:
34215 enum cp_parser_prec oprec;
34216 cp_token *token;
34217 cp_lexer_consume_token (parser->lexer);
34218 cp_parser_parse_tentatively (parser);
34219 rhs1 = cp_parser_simple_cast_expression (parser);
34220 if (rhs1 == error_mark_node)
34222 cp_parser_abort_tentative_parse (parser);
34223 cp_parser_simple_cast_expression (parser);
34224 goto saw_error;
34226 token = cp_lexer_peek_token (parser->lexer);
34227 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34229 cp_parser_abort_tentative_parse (parser);
34230 cp_parser_parse_tentatively (parser);
34231 rhs = cp_parser_binary_expression (parser, false, true,
34232 PREC_NOT_OPERATOR, NULL);
34233 if (rhs == error_mark_node)
34235 cp_parser_abort_tentative_parse (parser);
34236 cp_parser_binary_expression (parser, false, true,
34237 PREC_NOT_OPERATOR, NULL);
34238 goto saw_error;
34240 switch (TREE_CODE (rhs))
34242 case MULT_EXPR:
34243 case TRUNC_DIV_EXPR:
34244 case RDIV_EXPR:
34245 case PLUS_EXPR:
34246 case MINUS_EXPR:
34247 case LSHIFT_EXPR:
34248 case RSHIFT_EXPR:
34249 case BIT_AND_EXPR:
34250 case BIT_IOR_EXPR:
34251 case BIT_XOR_EXPR:
34252 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34254 if (cp_parser_parse_definitely (parser))
34256 opcode = TREE_CODE (rhs);
34257 rhs1 = TREE_OPERAND (rhs, 0);
34258 rhs = TREE_OPERAND (rhs, 1);
34259 goto stmt_done;
34261 else
34262 goto saw_error;
34264 break;
34265 default:
34266 break;
34268 cp_parser_abort_tentative_parse (parser);
34269 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34271 rhs = cp_parser_expression (parser);
34272 if (rhs == error_mark_node)
34273 goto saw_error;
34274 opcode = NOP_EXPR;
34275 rhs1 = NULL_TREE;
34276 goto stmt_done;
34278 cp_parser_error (parser,
34279 "invalid form of %<#pragma omp atomic%>");
34280 goto saw_error;
34282 if (!cp_parser_parse_definitely (parser))
34283 goto saw_error;
34284 switch (token->type)
34286 case CPP_SEMICOLON:
34287 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34289 code = OMP_ATOMIC_CAPTURE_OLD;
34290 v = lhs;
34291 lhs = NULL_TREE;
34292 lhs1 = rhs1;
34293 rhs1 = NULL_TREE;
34294 cp_lexer_consume_token (parser->lexer);
34295 goto restart;
34297 else if (structured_block)
34299 opcode = NOP_EXPR;
34300 rhs = rhs1;
34301 rhs1 = NULL_TREE;
34302 goto stmt_done;
34304 cp_parser_error (parser,
34305 "invalid form of %<#pragma omp atomic%>");
34306 goto saw_error;
34307 case CPP_MULT:
34308 opcode = MULT_EXPR;
34309 break;
34310 case CPP_DIV:
34311 opcode = TRUNC_DIV_EXPR;
34312 break;
34313 case CPP_PLUS:
34314 opcode = PLUS_EXPR;
34315 break;
34316 case CPP_MINUS:
34317 opcode = MINUS_EXPR;
34318 break;
34319 case CPP_LSHIFT:
34320 opcode = LSHIFT_EXPR;
34321 break;
34322 case CPP_RSHIFT:
34323 opcode = RSHIFT_EXPR;
34324 break;
34325 case CPP_AND:
34326 opcode = BIT_AND_EXPR;
34327 break;
34328 case CPP_OR:
34329 opcode = BIT_IOR_EXPR;
34330 break;
34331 case CPP_XOR:
34332 opcode = BIT_XOR_EXPR;
34333 break;
34334 default:
34335 cp_parser_error (parser,
34336 "invalid operator for %<#pragma omp atomic%>");
34337 goto saw_error;
34339 oprec = TOKEN_PRECEDENCE (token);
34340 gcc_assert (oprec != PREC_NOT_OPERATOR);
34341 if (commutative_tree_code (opcode))
34342 oprec = (enum cp_parser_prec) (oprec - 1);
34343 cp_lexer_consume_token (parser->lexer);
34344 rhs = cp_parser_binary_expression (parser, false, false,
34345 oprec, NULL);
34346 if (rhs == error_mark_node)
34347 goto saw_error;
34348 goto stmt_done;
34349 /* FALLTHROUGH */
34350 default:
34351 cp_parser_error (parser,
34352 "invalid operator for %<#pragma omp atomic%>");
34353 goto saw_error;
34355 cp_lexer_consume_token (parser->lexer);
34357 rhs = cp_parser_expression (parser);
34358 if (rhs == error_mark_node)
34359 goto saw_error;
34360 break;
34362 stmt_done:
34363 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34365 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34366 goto saw_error;
34367 v = cp_parser_unary_expression (parser);
34368 if (v == error_mark_node)
34369 goto saw_error;
34370 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34371 goto saw_error;
34372 lhs1 = cp_parser_unary_expression (parser);
34373 if (lhs1 == error_mark_node)
34374 goto saw_error;
34376 if (structured_block)
34378 cp_parser_consume_semicolon_at_end_of_statement (parser);
34379 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34381 done:
34382 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34383 if (!structured_block)
34384 cp_parser_consume_semicolon_at_end_of_statement (parser);
34385 return;
34387 saw_error:
34388 cp_parser_skip_to_end_of_block_or_statement (parser);
34389 if (structured_block)
34391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34392 cp_lexer_consume_token (parser->lexer);
34393 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34395 cp_parser_skip_to_end_of_block_or_statement (parser);
34396 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34397 cp_lexer_consume_token (parser->lexer);
34403 /* OpenMP 2.5:
34404 # pragma omp barrier new-line */
34406 static void
34407 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34409 cp_parser_require_pragma_eol (parser, pragma_tok);
34410 finish_omp_barrier ();
34413 /* OpenMP 2.5:
34414 # pragma omp critical [(name)] new-line
34415 structured-block
34417 OpenMP 4.5:
34418 # pragma omp critical [(name) [hint(expression)]] new-line
34419 structured-block */
34421 #define OMP_CRITICAL_CLAUSE_MASK \
34422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34424 static tree
34425 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34427 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34431 matching_parens parens;
34432 parens.consume_open (parser);
34434 name = cp_parser_identifier (parser);
34436 if (name == error_mark_node
34437 || !parens.require_close (parser))
34438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34439 /*or_comma=*/false,
34440 /*consume_paren=*/true);
34441 if (name == error_mark_node)
34442 name = NULL;
34444 clauses = cp_parser_omp_all_clauses (parser,
34445 OMP_CRITICAL_CLAUSE_MASK,
34446 "#pragma omp critical", pragma_tok);
34448 else
34449 cp_parser_require_pragma_eol (parser, pragma_tok);
34451 stmt = cp_parser_omp_structured_block (parser, if_p);
34452 return c_finish_omp_critical (input_location, stmt, name, clauses);
34455 /* OpenMP 2.5:
34456 # pragma omp flush flush-vars[opt] new-line
34458 flush-vars:
34459 ( variable-list ) */
34461 static void
34462 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34465 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34466 cp_parser_require_pragma_eol (parser, pragma_tok);
34468 finish_omp_flush ();
34471 /* Helper function, to parse omp for increment expression. */
34473 static tree
34474 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34476 tree cond = cp_parser_binary_expression (parser, false, true,
34477 PREC_NOT_OPERATOR, NULL);
34478 if (cond == error_mark_node
34479 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34481 cp_parser_skip_to_end_of_statement (parser);
34482 return error_mark_node;
34485 switch (TREE_CODE (cond))
34487 case GT_EXPR:
34488 case GE_EXPR:
34489 case LT_EXPR:
34490 case LE_EXPR:
34491 break;
34492 case NE_EXPR:
34493 /* Fall through: OpenMP disallows NE_EXPR. */
34494 gcc_fallthrough ();
34495 default:
34496 return error_mark_node;
34499 /* If decl is an iterator, preserve LHS and RHS of the relational
34500 expr until finish_omp_for. */
34501 if (decl
34502 && (type_dependent_expression_p (decl)
34503 || CLASS_TYPE_P (TREE_TYPE (decl))))
34504 return cond;
34506 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34507 TREE_CODE (cond),
34508 TREE_OPERAND (cond, 0), ERROR_MARK,
34509 TREE_OPERAND (cond, 1), ERROR_MARK,
34510 /*overload=*/NULL, tf_warning_or_error);
34513 /* Helper function, to parse omp for increment expression. */
34515 static tree
34516 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34518 cp_token *token = cp_lexer_peek_token (parser->lexer);
34519 enum tree_code op;
34520 tree lhs, rhs;
34521 cp_id_kind idk;
34522 bool decl_first;
34524 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34526 op = (token->type == CPP_PLUS_PLUS
34527 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34528 cp_lexer_consume_token (parser->lexer);
34529 lhs = cp_parser_simple_cast_expression (parser);
34530 if (lhs != decl
34531 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34532 return error_mark_node;
34533 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34536 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34537 if (lhs != decl
34538 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34539 return error_mark_node;
34541 token = cp_lexer_peek_token (parser->lexer);
34542 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34544 op = (token->type == CPP_PLUS_PLUS
34545 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34546 cp_lexer_consume_token (parser->lexer);
34547 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34550 op = cp_parser_assignment_operator_opt (parser);
34551 if (op == ERROR_MARK)
34552 return error_mark_node;
34554 if (op != NOP_EXPR)
34556 rhs = cp_parser_assignment_expression (parser);
34557 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34558 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34561 lhs = cp_parser_binary_expression (parser, false, false,
34562 PREC_ADDITIVE_EXPRESSION, NULL);
34563 token = cp_lexer_peek_token (parser->lexer);
34564 decl_first = (lhs == decl
34565 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34566 if (decl_first)
34567 lhs = NULL_TREE;
34568 if (token->type != CPP_PLUS
34569 && token->type != CPP_MINUS)
34570 return error_mark_node;
34574 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34575 cp_lexer_consume_token (parser->lexer);
34576 rhs = cp_parser_binary_expression (parser, false, false,
34577 PREC_ADDITIVE_EXPRESSION, NULL);
34578 token = cp_lexer_peek_token (parser->lexer);
34579 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34581 if (lhs == NULL_TREE)
34583 if (op == PLUS_EXPR)
34584 lhs = rhs;
34585 else
34586 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34587 tf_warning_or_error);
34589 else
34590 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34591 ERROR_MARK, NULL, tf_warning_or_error);
34594 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34596 if (!decl_first)
34598 if ((rhs != decl
34599 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34600 || op == MINUS_EXPR)
34601 return error_mark_node;
34602 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34604 else
34605 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34607 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34610 /* Parse the initialization statement of an OpenMP for loop.
34612 Return true if the resulting construct should have an
34613 OMP_CLAUSE_PRIVATE added to it. */
34615 static tree
34616 cp_parser_omp_for_loop_init (cp_parser *parser,
34617 tree &this_pre_body,
34618 vec<tree, va_gc> *for_block,
34619 tree &init,
34620 tree &orig_init,
34621 tree &decl,
34622 tree &real_decl)
34624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34625 return NULL_TREE;
34627 tree add_private_clause = NULL_TREE;
34629 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34631 init-expr:
34632 var = lb
34633 integer-type var = lb
34634 random-access-iterator-type var = lb
34635 pointer-type var = lb
34637 cp_decl_specifier_seq type_specifiers;
34639 /* First, try to parse as an initialized declaration. See
34640 cp_parser_condition, from whence the bulk of this is copied. */
34642 cp_parser_parse_tentatively (parser);
34643 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34644 /*is_trailing_return=*/false,
34645 &type_specifiers);
34646 if (cp_parser_parse_definitely (parser))
34648 /* If parsing a type specifier seq succeeded, then this
34649 MUST be a initialized declaration. */
34650 tree asm_specification, attributes;
34651 cp_declarator *declarator;
34653 declarator = cp_parser_declarator (parser,
34654 CP_PARSER_DECLARATOR_NAMED,
34655 /*ctor_dtor_or_conv_p=*/NULL,
34656 /*parenthesized_p=*/NULL,
34657 /*member_p=*/false,
34658 /*friend_p=*/false);
34659 attributes = cp_parser_attributes_opt (parser);
34660 asm_specification = cp_parser_asm_specification_opt (parser);
34662 if (declarator == cp_error_declarator)
34663 cp_parser_skip_to_end_of_statement (parser);
34665 else
34667 tree pushed_scope, auto_node;
34669 decl = start_decl (declarator, &type_specifiers,
34670 SD_INITIALIZED, attributes,
34671 /*prefix_attributes=*/NULL_TREE,
34672 &pushed_scope);
34674 auto_node = type_uses_auto (TREE_TYPE (decl));
34675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34677 if (cp_lexer_next_token_is (parser->lexer,
34678 CPP_OPEN_PAREN))
34679 error ("parenthesized initialization is not allowed in "
34680 "OpenMP %<for%> loop");
34681 else
34682 /* Trigger an error. */
34683 cp_parser_require (parser, CPP_EQ, RT_EQ);
34685 init = error_mark_node;
34686 cp_parser_skip_to_end_of_statement (parser);
34688 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34689 || type_dependent_expression_p (decl)
34690 || auto_node)
34692 bool is_direct_init, is_non_constant_init;
34694 init = cp_parser_initializer (parser,
34695 &is_direct_init,
34696 &is_non_constant_init);
34698 if (auto_node)
34700 TREE_TYPE (decl)
34701 = do_auto_deduction (TREE_TYPE (decl), init,
34702 auto_node);
34704 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34705 && !type_dependent_expression_p (decl))
34706 goto non_class;
34709 cp_finish_decl (decl, init, !is_non_constant_init,
34710 asm_specification,
34711 LOOKUP_ONLYCONVERTING);
34712 orig_init = init;
34713 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34715 vec_safe_push (for_block, this_pre_body);
34716 init = NULL_TREE;
34718 else
34720 init = pop_stmt_list (this_pre_body);
34721 if (init && TREE_CODE (init) == STATEMENT_LIST)
34723 tree_stmt_iterator i = tsi_start (init);
34724 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34725 while (!tsi_end_p (i))
34727 tree t = tsi_stmt (i);
34728 if (TREE_CODE (t) == DECL_EXPR
34729 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34731 tsi_delink (&i);
34732 vec_safe_push (for_block, t);
34733 continue;
34735 break;
34737 if (tsi_one_before_end_p (i))
34739 tree t = tsi_stmt (i);
34740 tsi_delink (&i);
34741 free_stmt_list (init);
34742 init = t;
34746 this_pre_body = NULL_TREE;
34748 else
34750 /* Consume '='. */
34751 cp_lexer_consume_token (parser->lexer);
34752 init = cp_parser_assignment_expression (parser);
34754 non_class:
34755 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34756 init = error_mark_node;
34757 else
34758 cp_finish_decl (decl, NULL_TREE,
34759 /*init_const_expr_p=*/false,
34760 asm_specification,
34761 LOOKUP_ONLYCONVERTING);
34764 if (pushed_scope)
34765 pop_scope (pushed_scope);
34768 else
34770 cp_id_kind idk;
34771 /* If parsing a type specifier sequence failed, then
34772 this MUST be a simple expression. */
34773 cp_parser_parse_tentatively (parser);
34774 decl = cp_parser_primary_expression (parser, false, false,
34775 false, &idk);
34776 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34777 if (!cp_parser_error_occurred (parser)
34778 && decl
34779 && (TREE_CODE (decl) == COMPONENT_REF
34780 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34782 cp_parser_abort_tentative_parse (parser);
34783 cp_parser_parse_tentatively (parser);
34784 cp_token *token = cp_lexer_peek_token (parser->lexer);
34785 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34786 /*check_dependency_p=*/true,
34787 /*template_p=*/NULL,
34788 /*declarator_p=*/false,
34789 /*optional_p=*/false);
34790 if (name != error_mark_node
34791 && last_tok == cp_lexer_peek_token (parser->lexer))
34793 decl = cp_parser_lookup_name_simple (parser, name,
34794 token->location);
34795 if (TREE_CODE (decl) == FIELD_DECL)
34796 add_private_clause = omp_privatize_field (decl, false);
34798 cp_parser_abort_tentative_parse (parser);
34799 cp_parser_parse_tentatively (parser);
34800 decl = cp_parser_primary_expression (parser, false, false,
34801 false, &idk);
34803 if (!cp_parser_error_occurred (parser)
34804 && decl
34805 && DECL_P (decl)
34806 && CLASS_TYPE_P (TREE_TYPE (decl)))
34808 tree rhs;
34810 cp_parser_parse_definitely (parser);
34811 cp_parser_require (parser, CPP_EQ, RT_EQ);
34812 rhs = cp_parser_assignment_expression (parser);
34813 orig_init = rhs;
34814 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34815 decl, NOP_EXPR,
34816 rhs,
34817 tf_warning_or_error));
34818 if (!add_private_clause)
34819 add_private_clause = decl;
34821 else
34823 decl = NULL;
34824 cp_parser_abort_tentative_parse (parser);
34825 init = cp_parser_expression (parser);
34826 if (init)
34828 if (TREE_CODE (init) == MODIFY_EXPR
34829 || TREE_CODE (init) == MODOP_EXPR)
34830 real_decl = TREE_OPERAND (init, 0);
34834 return add_private_clause;
34837 /* Parse the restricted form of the for statement allowed by OpenMP. */
34839 static tree
34840 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34841 tree *cclauses, bool *if_p)
34843 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34844 tree real_decl, initv, condv, incrv, declv;
34845 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34846 location_t loc_first;
34847 bool collapse_err = false;
34848 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34849 vec<tree, va_gc> *for_block = make_tree_vector ();
34850 auto_vec<tree, 4> orig_inits;
34851 bool tiling = false;
34853 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34854 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34855 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34856 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34858 tiling = true;
34859 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34861 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34862 && OMP_CLAUSE_ORDERED_EXPR (cl))
34864 ordered_cl = cl;
34865 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34868 if (ordered && ordered < collapse)
34870 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34871 "%<ordered%> clause parameter is less than %<collapse%>");
34872 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34873 = build_int_cst (NULL_TREE, collapse);
34874 ordered = collapse;
34876 if (ordered)
34878 for (tree *pc = &clauses; *pc; )
34879 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34881 error_at (OMP_CLAUSE_LOCATION (*pc),
34882 "%<linear%> clause may not be specified together "
34883 "with %<ordered%> clause with a parameter");
34884 *pc = OMP_CLAUSE_CHAIN (*pc);
34886 else
34887 pc = &OMP_CLAUSE_CHAIN (*pc);
34890 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34891 count = ordered ? ordered : collapse;
34893 declv = make_tree_vec (count);
34894 initv = make_tree_vec (count);
34895 condv = make_tree_vec (count);
34896 incrv = make_tree_vec (count);
34898 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34900 for (i = 0; i < count; i++)
34902 int bracecount = 0;
34903 tree add_private_clause = NULL_TREE;
34904 location_t loc;
34906 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34908 if (!collapse_err)
34909 cp_parser_error (parser, "for statement expected");
34910 return NULL;
34912 loc = cp_lexer_consume_token (parser->lexer)->location;
34914 matching_parens parens;
34915 if (!parens.require_open (parser))
34916 return NULL;
34918 init = orig_init = decl = real_decl = NULL;
34919 this_pre_body = push_stmt_list ();
34921 add_private_clause
34922 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34923 init, orig_init, decl, real_decl);
34925 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34926 if (this_pre_body)
34928 this_pre_body = pop_stmt_list (this_pre_body);
34929 if (pre_body)
34931 tree t = pre_body;
34932 pre_body = push_stmt_list ();
34933 add_stmt (t);
34934 add_stmt (this_pre_body);
34935 pre_body = pop_stmt_list (pre_body);
34937 else
34938 pre_body = this_pre_body;
34941 if (decl)
34942 real_decl = decl;
34943 if (cclauses != NULL
34944 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34945 && real_decl != NULL_TREE)
34947 tree *c;
34948 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34949 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34950 && OMP_CLAUSE_DECL (*c) == real_decl)
34952 error_at (loc, "iteration variable %qD"
34953 " should not be firstprivate", real_decl);
34954 *c = OMP_CLAUSE_CHAIN (*c);
34956 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34957 && OMP_CLAUSE_DECL (*c) == real_decl)
34959 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34960 tree l = *c;
34961 *c = OMP_CLAUSE_CHAIN (*c);
34962 if (code == OMP_SIMD)
34964 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34965 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34967 else
34969 OMP_CLAUSE_CHAIN (l) = clauses;
34970 clauses = l;
34972 add_private_clause = NULL_TREE;
34974 else
34976 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34977 && OMP_CLAUSE_DECL (*c) == real_decl)
34978 add_private_clause = NULL_TREE;
34979 c = &OMP_CLAUSE_CHAIN (*c);
34983 if (add_private_clause)
34985 tree c;
34986 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34988 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34989 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34990 && OMP_CLAUSE_DECL (c) == decl)
34991 break;
34992 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34993 && OMP_CLAUSE_DECL (c) == decl)
34994 error_at (loc, "iteration variable %qD "
34995 "should not be firstprivate",
34996 decl);
34997 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34998 && OMP_CLAUSE_DECL (c) == decl)
34999 error_at (loc, "iteration variable %qD should not be reduction",
35000 decl);
35002 if (c == NULL)
35004 if (code != OMP_SIMD)
35005 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35006 else if (collapse == 1)
35007 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35008 else
35009 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35010 OMP_CLAUSE_DECL (c) = add_private_clause;
35011 c = finish_omp_clauses (c, C_ORT_OMP);
35012 if (c)
35014 OMP_CLAUSE_CHAIN (c) = clauses;
35015 clauses = c;
35016 /* For linear, signal that we need to fill up
35017 the so far unknown linear step. */
35018 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35019 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35024 cond = NULL;
35025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35026 cond = cp_parser_omp_for_cond (parser, decl);
35027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35029 incr = NULL;
35030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35032 /* If decl is an iterator, preserve the operator on decl
35033 until finish_omp_for. */
35034 if (real_decl
35035 && ((processing_template_decl
35036 && (TREE_TYPE (real_decl) == NULL_TREE
35037 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35038 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35039 incr = cp_parser_omp_for_incr (parser, real_decl);
35040 else
35041 incr = cp_parser_expression (parser);
35042 if (!EXPR_HAS_LOCATION (incr))
35043 protected_set_expr_location (incr, input_location);
35046 if (!parens.require_close (parser))
35047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35048 /*or_comma=*/false,
35049 /*consume_paren=*/true);
35051 TREE_VEC_ELT (declv, i) = decl;
35052 TREE_VEC_ELT (initv, i) = init;
35053 TREE_VEC_ELT (condv, i) = cond;
35054 TREE_VEC_ELT (incrv, i) = incr;
35055 if (orig_init)
35057 orig_inits.safe_grow_cleared (i + 1);
35058 orig_inits[i] = orig_init;
35061 if (i == count - 1)
35062 break;
35064 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35065 in between the collapsed for loops to be still considered perfectly
35066 nested. Hopefully the final version clarifies this.
35067 For now handle (multiple) {'s and empty statements. */
35068 cp_parser_parse_tentatively (parser);
35069 for (;;)
35071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35072 break;
35073 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35075 cp_lexer_consume_token (parser->lexer);
35076 bracecount++;
35078 else if (bracecount
35079 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35080 cp_lexer_consume_token (parser->lexer);
35081 else
35083 loc = cp_lexer_peek_token (parser->lexer)->location;
35084 error_at (loc, "not enough for loops to collapse");
35085 collapse_err = true;
35086 cp_parser_abort_tentative_parse (parser);
35087 declv = NULL_TREE;
35088 break;
35092 if (declv)
35094 cp_parser_parse_definitely (parser);
35095 nbraces += bracecount;
35099 if (nbraces)
35100 if_p = NULL;
35102 /* Note that we saved the original contents of this flag when we entered
35103 the structured block, and so we don't need to re-save it here. */
35104 parser->in_statement = IN_OMP_FOR;
35106 /* Note that the grammar doesn't call for a structured block here,
35107 though the loop as a whole is a structured block. */
35108 body = push_stmt_list ();
35109 cp_parser_statement (parser, NULL_TREE, false, if_p);
35110 body = pop_stmt_list (body);
35112 if (declv == NULL_TREE)
35113 ret = NULL_TREE;
35114 else
35115 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35116 body, pre_body, &orig_inits, clauses);
35118 while (nbraces)
35120 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35122 cp_lexer_consume_token (parser->lexer);
35123 nbraces--;
35125 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35126 cp_lexer_consume_token (parser->lexer);
35127 else
35129 if (!collapse_err)
35131 error_at (cp_lexer_peek_token (parser->lexer)->location,
35132 "collapsed loops not perfectly nested");
35134 collapse_err = true;
35135 cp_parser_statement_seq_opt (parser, NULL);
35136 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35137 break;
35141 while (!for_block->is_empty ())
35143 tree t = for_block->pop ();
35144 if (TREE_CODE (t) == STATEMENT_LIST)
35145 add_stmt (pop_stmt_list (t));
35146 else
35147 add_stmt (t);
35149 release_tree_vector (for_block);
35151 return ret;
35154 /* Helper function for OpenMP parsing, split clauses and call
35155 finish_omp_clauses on each of the set of clauses afterwards. */
35157 static void
35158 cp_omp_split_clauses (location_t loc, enum tree_code code,
35159 omp_clause_mask mask, tree clauses, tree *cclauses)
35161 int i;
35162 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35163 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35164 if (cclauses[i])
35165 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35168 /* OpenMP 4.0:
35169 #pragma omp simd simd-clause[optseq] new-line
35170 for-loop */
35172 #define OMP_SIMD_CLAUSE_MASK \
35173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35182 static tree
35183 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35184 char *p_name, omp_clause_mask mask, tree *cclauses,
35185 bool *if_p)
35187 tree clauses, sb, ret;
35188 unsigned int save;
35189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35191 strcat (p_name, " simd");
35192 mask |= OMP_SIMD_CLAUSE_MASK;
35194 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35195 cclauses == NULL);
35196 if (cclauses)
35198 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35199 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35200 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35201 OMP_CLAUSE_ORDERED);
35202 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35204 error_at (OMP_CLAUSE_LOCATION (c),
35205 "%<ordered%> clause with parameter may not be specified "
35206 "on %qs construct", p_name);
35207 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35211 sb = begin_omp_structured_block ();
35212 save = cp_parser_begin_omp_structured_block (parser);
35214 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35216 cp_parser_end_omp_structured_block (parser, save);
35217 add_stmt (finish_omp_structured_block (sb));
35219 return ret;
35222 /* OpenMP 2.5:
35223 #pragma omp for for-clause[optseq] new-line
35224 for-loop
35226 OpenMP 4.0:
35227 #pragma omp for simd for-simd-clause[optseq] new-line
35228 for-loop */
35230 #define OMP_FOR_CLAUSE_MASK \
35231 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35241 static tree
35242 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35243 char *p_name, omp_clause_mask mask, tree *cclauses,
35244 bool *if_p)
35246 tree clauses, sb, ret;
35247 unsigned int save;
35248 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35250 strcat (p_name, " for");
35251 mask |= OMP_FOR_CLAUSE_MASK;
35252 /* parallel for{, simd} disallows nowait clause, but for
35253 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35254 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35255 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35256 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35257 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35258 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35263 const char *p = IDENTIFIER_POINTER (id);
35265 if (strcmp (p, "simd") == 0)
35267 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35268 if (cclauses == NULL)
35269 cclauses = cclauses_buf;
35271 cp_lexer_consume_token (parser->lexer);
35272 if (!flag_openmp) /* flag_openmp_simd */
35273 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35274 cclauses, if_p);
35275 sb = begin_omp_structured_block ();
35276 save = cp_parser_begin_omp_structured_block (parser);
35277 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35278 cclauses, if_p);
35279 cp_parser_end_omp_structured_block (parser, save);
35280 tree body = finish_omp_structured_block (sb);
35281 if (ret == NULL)
35282 return ret;
35283 ret = make_node (OMP_FOR);
35284 TREE_TYPE (ret) = void_type_node;
35285 OMP_FOR_BODY (ret) = body;
35286 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35287 SET_EXPR_LOCATION (ret, loc);
35288 add_stmt (ret);
35289 return ret;
35292 if (!flag_openmp) /* flag_openmp_simd */
35294 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35295 return NULL_TREE;
35298 /* Composite distribute parallel for disallows linear clause. */
35299 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35300 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35302 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35303 cclauses == NULL);
35304 if (cclauses)
35306 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35307 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35310 sb = begin_omp_structured_block ();
35311 save = cp_parser_begin_omp_structured_block (parser);
35313 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35315 cp_parser_end_omp_structured_block (parser, save);
35316 add_stmt (finish_omp_structured_block (sb));
35318 return ret;
35321 /* OpenMP 2.5:
35322 # pragma omp master new-line
35323 structured-block */
35325 static tree
35326 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35328 cp_parser_require_pragma_eol (parser, pragma_tok);
35329 return c_finish_omp_master (input_location,
35330 cp_parser_omp_structured_block (parser, if_p));
35333 /* OpenMP 2.5:
35334 # pragma omp ordered new-line
35335 structured-block
35337 OpenMP 4.5:
35338 # pragma omp ordered ordered-clauses new-line
35339 structured-block */
35341 #define OMP_ORDERED_CLAUSE_MASK \
35342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35345 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35346 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35348 static bool
35349 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35350 enum pragma_context context, bool *if_p)
35352 location_t loc = pragma_tok->location;
35354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35356 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35357 const char *p = IDENTIFIER_POINTER (id);
35359 if (strcmp (p, "depend") == 0)
35361 if (!flag_openmp) /* flag_openmp_simd */
35363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35364 return false;
35366 if (context == pragma_stmt)
35368 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35369 "%<depend%> clause may only be used in compound "
35370 "statements");
35371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35372 return false;
35374 tree clauses
35375 = cp_parser_omp_all_clauses (parser,
35376 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35377 "#pragma omp ordered", pragma_tok);
35378 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35379 return false;
35383 tree clauses
35384 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35385 "#pragma omp ordered", pragma_tok);
35387 if (!flag_openmp /* flag_openmp_simd */
35388 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35389 return false;
35391 c_finish_omp_ordered (loc, clauses,
35392 cp_parser_omp_structured_block (parser, if_p));
35393 return true;
35396 /* OpenMP 2.5:
35398 section-scope:
35399 { section-sequence }
35401 section-sequence:
35402 section-directive[opt] structured-block
35403 section-sequence section-directive structured-block */
35405 static tree
35406 cp_parser_omp_sections_scope (cp_parser *parser)
35408 tree stmt, substmt;
35409 bool error_suppress = false;
35410 cp_token *tok;
35412 matching_braces braces;
35413 if (!braces.require_open (parser))
35414 return NULL_TREE;
35416 stmt = push_stmt_list ();
35418 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35419 != PRAGMA_OMP_SECTION)
35421 substmt = cp_parser_omp_structured_block (parser, NULL);
35422 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35423 add_stmt (substmt);
35426 while (1)
35428 tok = cp_lexer_peek_token (parser->lexer);
35429 if (tok->type == CPP_CLOSE_BRACE)
35430 break;
35431 if (tok->type == CPP_EOF)
35432 break;
35434 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35436 cp_lexer_consume_token (parser->lexer);
35437 cp_parser_require_pragma_eol (parser, tok);
35438 error_suppress = false;
35440 else if (!error_suppress)
35442 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35443 error_suppress = true;
35446 substmt = cp_parser_omp_structured_block (parser, NULL);
35447 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35448 add_stmt (substmt);
35450 braces.require_close (parser);
35452 substmt = pop_stmt_list (stmt);
35454 stmt = make_node (OMP_SECTIONS);
35455 TREE_TYPE (stmt) = void_type_node;
35456 OMP_SECTIONS_BODY (stmt) = substmt;
35458 add_stmt (stmt);
35459 return stmt;
35462 /* OpenMP 2.5:
35463 # pragma omp sections sections-clause[optseq] newline
35464 sections-scope */
35466 #define OMP_SECTIONS_CLAUSE_MASK \
35467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35473 static tree
35474 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35475 char *p_name, omp_clause_mask mask, tree *cclauses)
35477 tree clauses, ret;
35478 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35480 strcat (p_name, " sections");
35481 mask |= OMP_SECTIONS_CLAUSE_MASK;
35482 if (cclauses)
35483 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35485 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35486 cclauses == NULL);
35487 if (cclauses)
35489 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35490 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35493 ret = cp_parser_omp_sections_scope (parser);
35494 if (ret)
35495 OMP_SECTIONS_CLAUSES (ret) = clauses;
35497 return ret;
35500 /* OpenMP 2.5:
35501 # pragma omp parallel parallel-clause[optseq] new-line
35502 structured-block
35503 # pragma omp parallel for parallel-for-clause[optseq] new-line
35504 structured-block
35505 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35506 structured-block
35508 OpenMP 4.0:
35509 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35510 structured-block */
35512 #define OMP_PARALLEL_CLAUSE_MASK \
35513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35523 static tree
35524 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35525 char *p_name, omp_clause_mask mask, tree *cclauses,
35526 bool *if_p)
35528 tree stmt, clauses, block;
35529 unsigned int save;
35530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35532 strcat (p_name, " parallel");
35533 mask |= OMP_PARALLEL_CLAUSE_MASK;
35534 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35535 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35536 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35537 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35541 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35542 if (cclauses == NULL)
35543 cclauses = cclauses_buf;
35545 cp_lexer_consume_token (parser->lexer);
35546 if (!flag_openmp) /* flag_openmp_simd */
35547 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35548 if_p);
35549 block = begin_omp_parallel ();
35550 save = cp_parser_begin_omp_structured_block (parser);
35551 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35552 if_p);
35553 cp_parser_end_omp_structured_block (parser, save);
35554 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35555 block);
35556 if (ret == NULL_TREE)
35557 return ret;
35558 OMP_PARALLEL_COMBINED (stmt) = 1;
35559 return stmt;
35561 /* When combined with distribute, parallel has to be followed by for.
35562 #pragma omp target parallel is allowed though. */
35563 else if (cclauses
35564 && (mask & (OMP_CLAUSE_MASK_1
35565 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35567 error_at (loc, "expected %<for%> after %qs", p_name);
35568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35569 return NULL_TREE;
35571 else if (!flag_openmp) /* flag_openmp_simd */
35573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35574 return NULL_TREE;
35576 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35578 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35579 const char *p = IDENTIFIER_POINTER (id);
35580 if (strcmp (p, "sections") == 0)
35582 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35583 cclauses = cclauses_buf;
35585 cp_lexer_consume_token (parser->lexer);
35586 block = begin_omp_parallel ();
35587 save = cp_parser_begin_omp_structured_block (parser);
35588 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35589 cp_parser_end_omp_structured_block (parser, save);
35590 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35591 block);
35592 OMP_PARALLEL_COMBINED (stmt) = 1;
35593 return stmt;
35597 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35598 cclauses == NULL);
35599 if (cclauses)
35601 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35602 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35605 block = begin_omp_parallel ();
35606 save = cp_parser_begin_omp_structured_block (parser);
35607 cp_parser_statement (parser, NULL_TREE, false, if_p);
35608 cp_parser_end_omp_structured_block (parser, save);
35609 stmt = finish_omp_parallel (clauses, block);
35610 return stmt;
35613 /* OpenMP 2.5:
35614 # pragma omp single single-clause[optseq] new-line
35615 structured-block */
35617 #define OMP_SINGLE_CLAUSE_MASK \
35618 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35623 static tree
35624 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35626 tree stmt = make_node (OMP_SINGLE);
35627 TREE_TYPE (stmt) = void_type_node;
35629 OMP_SINGLE_CLAUSES (stmt)
35630 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35631 "#pragma omp single", pragma_tok);
35632 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35634 return add_stmt (stmt);
35637 /* OpenMP 3.0:
35638 # pragma omp task task-clause[optseq] new-line
35639 structured-block */
35641 #define OMP_TASK_CLAUSE_MASK \
35642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35653 static tree
35654 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35656 tree clauses, block;
35657 unsigned int save;
35659 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35660 "#pragma omp task", pragma_tok);
35661 block = begin_omp_task ();
35662 save = cp_parser_begin_omp_structured_block (parser);
35663 cp_parser_statement (parser, NULL_TREE, false, if_p);
35664 cp_parser_end_omp_structured_block (parser, save);
35665 return finish_omp_task (clauses, block);
35668 /* OpenMP 3.0:
35669 # pragma omp taskwait new-line */
35671 static void
35672 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35674 cp_parser_require_pragma_eol (parser, pragma_tok);
35675 finish_omp_taskwait ();
35678 /* OpenMP 3.1:
35679 # pragma omp taskyield new-line */
35681 static void
35682 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35684 cp_parser_require_pragma_eol (parser, pragma_tok);
35685 finish_omp_taskyield ();
35688 /* OpenMP 4.0:
35689 # pragma omp taskgroup new-line
35690 structured-block */
35692 static tree
35693 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35695 cp_parser_require_pragma_eol (parser, pragma_tok);
35696 return c_finish_omp_taskgroup (input_location,
35697 cp_parser_omp_structured_block (parser,
35698 if_p));
35702 /* OpenMP 2.5:
35703 # pragma omp threadprivate (variable-list) */
35705 static void
35706 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35708 tree vars;
35710 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35711 cp_parser_require_pragma_eol (parser, pragma_tok);
35713 finish_omp_threadprivate (vars);
35716 /* OpenMP 4.0:
35717 # pragma omp cancel cancel-clause[optseq] new-line */
35719 #define OMP_CANCEL_CLAUSE_MASK \
35720 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35726 static void
35727 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35729 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35730 "#pragma omp cancel", pragma_tok);
35731 finish_omp_cancel (clauses);
35734 /* OpenMP 4.0:
35735 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35737 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35743 static void
35744 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35745 enum pragma_context context)
35747 tree clauses;
35748 bool point_seen = false;
35750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35753 const char *p = IDENTIFIER_POINTER (id);
35755 if (strcmp (p, "point") == 0)
35757 cp_lexer_consume_token (parser->lexer);
35758 point_seen = true;
35761 if (!point_seen)
35763 cp_parser_error (parser, "expected %<point%>");
35764 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35765 return;
35768 if (context != pragma_compound)
35770 if (context == pragma_stmt)
35771 error_at (pragma_tok->location,
35772 "%<#pragma %s%> may only be used in compound statements",
35773 "omp cancellation point");
35774 else
35775 cp_parser_error (parser, "expected declaration specifiers");
35776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35777 return;
35780 clauses = cp_parser_omp_all_clauses (parser,
35781 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35782 "#pragma omp cancellation point",
35783 pragma_tok);
35784 finish_omp_cancellation_point (clauses);
35787 /* OpenMP 4.0:
35788 #pragma omp distribute distribute-clause[optseq] new-line
35789 for-loop */
35791 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35798 static tree
35799 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35800 char *p_name, omp_clause_mask mask, tree *cclauses,
35801 bool *if_p)
35803 tree clauses, sb, ret;
35804 unsigned int save;
35805 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35807 strcat (p_name, " distribute");
35808 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35812 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35813 const char *p = IDENTIFIER_POINTER (id);
35814 bool simd = false;
35815 bool parallel = false;
35817 if (strcmp (p, "simd") == 0)
35818 simd = true;
35819 else
35820 parallel = strcmp (p, "parallel") == 0;
35821 if (parallel || simd)
35823 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35824 if (cclauses == NULL)
35825 cclauses = cclauses_buf;
35826 cp_lexer_consume_token (parser->lexer);
35827 if (!flag_openmp) /* flag_openmp_simd */
35829 if (simd)
35830 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35831 cclauses, if_p);
35832 else
35833 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35834 cclauses, if_p);
35836 sb = begin_omp_structured_block ();
35837 save = cp_parser_begin_omp_structured_block (parser);
35838 if (simd)
35839 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35840 cclauses, if_p);
35841 else
35842 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35843 cclauses, if_p);
35844 cp_parser_end_omp_structured_block (parser, save);
35845 tree body = finish_omp_structured_block (sb);
35846 if (ret == NULL)
35847 return ret;
35848 ret = make_node (OMP_DISTRIBUTE);
35849 TREE_TYPE (ret) = void_type_node;
35850 OMP_FOR_BODY (ret) = body;
35851 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35852 SET_EXPR_LOCATION (ret, loc);
35853 add_stmt (ret);
35854 return ret;
35857 if (!flag_openmp) /* flag_openmp_simd */
35859 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35860 return NULL_TREE;
35863 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35864 cclauses == NULL);
35865 if (cclauses)
35867 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35868 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35871 sb = begin_omp_structured_block ();
35872 save = cp_parser_begin_omp_structured_block (parser);
35874 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35876 cp_parser_end_omp_structured_block (parser, save);
35877 add_stmt (finish_omp_structured_block (sb));
35879 return ret;
35882 /* OpenMP 4.0:
35883 # pragma omp teams teams-clause[optseq] new-line
35884 structured-block */
35886 #define OMP_TEAMS_CLAUSE_MASK \
35887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35895 static tree
35896 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35897 char *p_name, omp_clause_mask mask, tree *cclauses,
35898 bool *if_p)
35900 tree clauses, sb, ret;
35901 unsigned int save;
35902 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35904 strcat (p_name, " teams");
35905 mask |= OMP_TEAMS_CLAUSE_MASK;
35907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35910 const char *p = IDENTIFIER_POINTER (id);
35911 if (strcmp (p, "distribute") == 0)
35913 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35914 if (cclauses == NULL)
35915 cclauses = cclauses_buf;
35917 cp_lexer_consume_token (parser->lexer);
35918 if (!flag_openmp) /* flag_openmp_simd */
35919 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35920 cclauses, if_p);
35921 sb = begin_omp_structured_block ();
35922 save = cp_parser_begin_omp_structured_block (parser);
35923 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35924 cclauses, if_p);
35925 cp_parser_end_omp_structured_block (parser, save);
35926 tree body = finish_omp_structured_block (sb);
35927 if (ret == NULL)
35928 return ret;
35929 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35930 ret = make_node (OMP_TEAMS);
35931 TREE_TYPE (ret) = void_type_node;
35932 OMP_TEAMS_CLAUSES (ret) = clauses;
35933 OMP_TEAMS_BODY (ret) = body;
35934 OMP_TEAMS_COMBINED (ret) = 1;
35935 SET_EXPR_LOCATION (ret, loc);
35936 return add_stmt (ret);
35939 if (!flag_openmp) /* flag_openmp_simd */
35941 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35942 return NULL_TREE;
35945 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35946 cclauses == NULL);
35947 if (cclauses)
35949 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35950 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35953 tree stmt = make_node (OMP_TEAMS);
35954 TREE_TYPE (stmt) = void_type_node;
35955 OMP_TEAMS_CLAUSES (stmt) = clauses;
35956 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35957 SET_EXPR_LOCATION (stmt, loc);
35959 return add_stmt (stmt);
35962 /* OpenMP 4.0:
35963 # pragma omp target data target-data-clause[optseq] new-line
35964 structured-block */
35966 #define OMP_TARGET_DATA_CLAUSE_MASK \
35967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35972 static tree
35973 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35975 tree clauses
35976 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35977 "#pragma omp target data", pragma_tok);
35978 int map_seen = 0;
35979 for (tree *pc = &clauses; *pc;)
35981 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35982 switch (OMP_CLAUSE_MAP_KIND (*pc))
35984 case GOMP_MAP_TO:
35985 case GOMP_MAP_ALWAYS_TO:
35986 case GOMP_MAP_FROM:
35987 case GOMP_MAP_ALWAYS_FROM:
35988 case GOMP_MAP_TOFROM:
35989 case GOMP_MAP_ALWAYS_TOFROM:
35990 case GOMP_MAP_ALLOC:
35991 map_seen = 3;
35992 break;
35993 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35994 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35995 case GOMP_MAP_ALWAYS_POINTER:
35996 break;
35997 default:
35998 map_seen |= 1;
35999 error_at (OMP_CLAUSE_LOCATION (*pc),
36000 "%<#pragma omp target data%> with map-type other "
36001 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36002 "on %<map%> clause");
36003 *pc = OMP_CLAUSE_CHAIN (*pc);
36004 continue;
36006 pc = &OMP_CLAUSE_CHAIN (*pc);
36009 if (map_seen != 3)
36011 if (map_seen == 0)
36012 error_at (pragma_tok->location,
36013 "%<#pragma omp target data%> must contain at least "
36014 "one %<map%> clause");
36015 return NULL_TREE;
36018 tree stmt = make_node (OMP_TARGET_DATA);
36019 TREE_TYPE (stmt) = void_type_node;
36020 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36022 keep_next_level (true);
36023 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36025 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36026 return add_stmt (stmt);
36029 /* OpenMP 4.5:
36030 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36031 structured-block */
36033 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36040 static tree
36041 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36042 enum pragma_context context)
36044 bool data_seen = false;
36045 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36047 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36048 const char *p = IDENTIFIER_POINTER (id);
36050 if (strcmp (p, "data") == 0)
36052 cp_lexer_consume_token (parser->lexer);
36053 data_seen = true;
36056 if (!data_seen)
36058 cp_parser_error (parser, "expected %<data%>");
36059 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36060 return NULL_TREE;
36063 if (context == pragma_stmt)
36065 error_at (pragma_tok->location,
36066 "%<#pragma %s%> may only be used in compound statements",
36067 "omp target enter data");
36068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36069 return NULL_TREE;
36072 tree clauses
36073 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36074 "#pragma omp target enter data", pragma_tok);
36075 int map_seen = 0;
36076 for (tree *pc = &clauses; *pc;)
36078 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36079 switch (OMP_CLAUSE_MAP_KIND (*pc))
36081 case GOMP_MAP_TO:
36082 case GOMP_MAP_ALWAYS_TO:
36083 case GOMP_MAP_ALLOC:
36084 map_seen = 3;
36085 break;
36086 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36087 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36088 case GOMP_MAP_ALWAYS_POINTER:
36089 break;
36090 default:
36091 map_seen |= 1;
36092 error_at (OMP_CLAUSE_LOCATION (*pc),
36093 "%<#pragma omp target enter data%> with map-type other "
36094 "than %<to%> or %<alloc%> on %<map%> clause");
36095 *pc = OMP_CLAUSE_CHAIN (*pc);
36096 continue;
36098 pc = &OMP_CLAUSE_CHAIN (*pc);
36101 if (map_seen != 3)
36103 if (map_seen == 0)
36104 error_at (pragma_tok->location,
36105 "%<#pragma omp target enter data%> must contain at least "
36106 "one %<map%> clause");
36107 return NULL_TREE;
36110 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36111 TREE_TYPE (stmt) = void_type_node;
36112 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36113 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36114 return add_stmt (stmt);
36117 /* OpenMP 4.5:
36118 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36119 structured-block */
36121 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36128 static tree
36129 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36130 enum pragma_context context)
36132 bool data_seen = false;
36133 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36135 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36136 const char *p = IDENTIFIER_POINTER (id);
36138 if (strcmp (p, "data") == 0)
36140 cp_lexer_consume_token (parser->lexer);
36141 data_seen = true;
36144 if (!data_seen)
36146 cp_parser_error (parser, "expected %<data%>");
36147 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36148 return NULL_TREE;
36151 if (context == pragma_stmt)
36153 error_at (pragma_tok->location,
36154 "%<#pragma %s%> may only be used in compound statements",
36155 "omp target exit data");
36156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36157 return NULL_TREE;
36160 tree clauses
36161 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36162 "#pragma omp target exit data", pragma_tok);
36163 int map_seen = 0;
36164 for (tree *pc = &clauses; *pc;)
36166 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36167 switch (OMP_CLAUSE_MAP_KIND (*pc))
36169 case GOMP_MAP_FROM:
36170 case GOMP_MAP_ALWAYS_FROM:
36171 case GOMP_MAP_RELEASE:
36172 case GOMP_MAP_DELETE:
36173 map_seen = 3;
36174 break;
36175 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36176 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36177 case GOMP_MAP_ALWAYS_POINTER:
36178 break;
36179 default:
36180 map_seen |= 1;
36181 error_at (OMP_CLAUSE_LOCATION (*pc),
36182 "%<#pragma omp target exit data%> with map-type other "
36183 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36184 " clause");
36185 *pc = OMP_CLAUSE_CHAIN (*pc);
36186 continue;
36188 pc = &OMP_CLAUSE_CHAIN (*pc);
36191 if (map_seen != 3)
36193 if (map_seen == 0)
36194 error_at (pragma_tok->location,
36195 "%<#pragma omp target exit data%> must contain at least "
36196 "one %<map%> clause");
36197 return NULL_TREE;
36200 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36201 TREE_TYPE (stmt) = void_type_node;
36202 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36203 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36204 return add_stmt (stmt);
36207 /* OpenMP 4.0:
36208 # pragma omp target update target-update-clause[optseq] new-line */
36210 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36218 static bool
36219 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36220 enum pragma_context context)
36222 if (context == pragma_stmt)
36224 error_at (pragma_tok->location,
36225 "%<#pragma %s%> may only be used in compound statements",
36226 "omp target update");
36227 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36228 return false;
36231 tree clauses
36232 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36233 "#pragma omp target update", pragma_tok);
36234 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36235 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36237 error_at (pragma_tok->location,
36238 "%<#pragma omp target update%> must contain at least one "
36239 "%<from%> or %<to%> clauses");
36240 return false;
36243 tree stmt = make_node (OMP_TARGET_UPDATE);
36244 TREE_TYPE (stmt) = void_type_node;
36245 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36246 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36247 add_stmt (stmt);
36248 return false;
36251 /* OpenMP 4.0:
36252 # pragma omp target target-clause[optseq] new-line
36253 structured-block */
36255 #define OMP_TARGET_CLAUSE_MASK \
36256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36266 static bool
36267 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36268 enum pragma_context context, bool *if_p)
36270 tree *pc = NULL, stmt;
36272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36274 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36275 const char *p = IDENTIFIER_POINTER (id);
36276 enum tree_code ccode = ERROR_MARK;
36278 if (strcmp (p, "teams") == 0)
36279 ccode = OMP_TEAMS;
36280 else if (strcmp (p, "parallel") == 0)
36281 ccode = OMP_PARALLEL;
36282 else if (strcmp (p, "simd") == 0)
36283 ccode = OMP_SIMD;
36284 if (ccode != ERROR_MARK)
36286 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36287 char p_name[sizeof ("#pragma omp target teams distribute "
36288 "parallel for simd")];
36290 cp_lexer_consume_token (parser->lexer);
36291 strcpy (p_name, "#pragma omp target");
36292 if (!flag_openmp) /* flag_openmp_simd */
36294 tree stmt;
36295 switch (ccode)
36297 case OMP_TEAMS:
36298 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36299 OMP_TARGET_CLAUSE_MASK,
36300 cclauses, if_p);
36301 break;
36302 case OMP_PARALLEL:
36303 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36304 OMP_TARGET_CLAUSE_MASK,
36305 cclauses, if_p);
36306 break;
36307 case OMP_SIMD:
36308 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36309 OMP_TARGET_CLAUSE_MASK,
36310 cclauses, if_p);
36311 break;
36312 default:
36313 gcc_unreachable ();
36315 return stmt != NULL_TREE;
36317 keep_next_level (true);
36318 tree sb = begin_omp_structured_block (), ret;
36319 unsigned save = cp_parser_begin_omp_structured_block (parser);
36320 switch (ccode)
36322 case OMP_TEAMS:
36323 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36324 OMP_TARGET_CLAUSE_MASK, cclauses,
36325 if_p);
36326 break;
36327 case OMP_PARALLEL:
36328 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36329 OMP_TARGET_CLAUSE_MASK, cclauses,
36330 if_p);
36331 break;
36332 case OMP_SIMD:
36333 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36334 OMP_TARGET_CLAUSE_MASK, cclauses,
36335 if_p);
36336 break;
36337 default:
36338 gcc_unreachable ();
36340 cp_parser_end_omp_structured_block (parser, save);
36341 tree body = finish_omp_structured_block (sb);
36342 if (ret == NULL_TREE)
36343 return false;
36344 if (ccode == OMP_TEAMS && !processing_template_decl)
36346 /* For combined target teams, ensure the num_teams and
36347 thread_limit clause expressions are evaluated on the host,
36348 before entering the target construct. */
36349 tree c;
36350 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36351 c; c = OMP_CLAUSE_CHAIN (c))
36352 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36353 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36354 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36356 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36357 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36358 if (expr == error_mark_node)
36359 continue;
36360 tree tmp = TARGET_EXPR_SLOT (expr);
36361 add_stmt (expr);
36362 OMP_CLAUSE_OPERAND (c, 0) = expr;
36363 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36364 OMP_CLAUSE_FIRSTPRIVATE);
36365 OMP_CLAUSE_DECL (tc) = tmp;
36366 OMP_CLAUSE_CHAIN (tc)
36367 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36368 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36371 tree stmt = make_node (OMP_TARGET);
36372 TREE_TYPE (stmt) = void_type_node;
36373 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36374 OMP_TARGET_BODY (stmt) = body;
36375 OMP_TARGET_COMBINED (stmt) = 1;
36376 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36377 add_stmt (stmt);
36378 pc = &OMP_TARGET_CLAUSES (stmt);
36379 goto check_clauses;
36381 else if (!flag_openmp) /* flag_openmp_simd */
36383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36384 return false;
36386 else if (strcmp (p, "data") == 0)
36388 cp_lexer_consume_token (parser->lexer);
36389 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36390 return true;
36392 else if (strcmp (p, "enter") == 0)
36394 cp_lexer_consume_token (parser->lexer);
36395 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36396 return false;
36398 else if (strcmp (p, "exit") == 0)
36400 cp_lexer_consume_token (parser->lexer);
36401 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36402 return false;
36404 else if (strcmp (p, "update") == 0)
36406 cp_lexer_consume_token (parser->lexer);
36407 return cp_parser_omp_target_update (parser, pragma_tok, context);
36410 if (!flag_openmp) /* flag_openmp_simd */
36412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36413 return false;
36416 stmt = make_node (OMP_TARGET);
36417 TREE_TYPE (stmt) = void_type_node;
36419 OMP_TARGET_CLAUSES (stmt)
36420 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36421 "#pragma omp target", pragma_tok);
36422 pc = &OMP_TARGET_CLAUSES (stmt);
36423 keep_next_level (true);
36424 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36426 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36427 add_stmt (stmt);
36429 check_clauses:
36430 while (*pc)
36432 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36433 switch (OMP_CLAUSE_MAP_KIND (*pc))
36435 case GOMP_MAP_TO:
36436 case GOMP_MAP_ALWAYS_TO:
36437 case GOMP_MAP_FROM:
36438 case GOMP_MAP_ALWAYS_FROM:
36439 case GOMP_MAP_TOFROM:
36440 case GOMP_MAP_ALWAYS_TOFROM:
36441 case GOMP_MAP_ALLOC:
36442 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36443 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36444 case GOMP_MAP_ALWAYS_POINTER:
36445 break;
36446 default:
36447 error_at (OMP_CLAUSE_LOCATION (*pc),
36448 "%<#pragma omp target%> with map-type other "
36449 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36450 "on %<map%> clause");
36451 *pc = OMP_CLAUSE_CHAIN (*pc);
36452 continue;
36454 pc = &OMP_CLAUSE_CHAIN (*pc);
36456 return true;
36459 /* OpenACC 2.0:
36460 # pragma acc cache (variable-list) new-line
36463 static tree
36464 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36466 tree stmt, clauses;
36468 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36469 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36471 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36473 stmt = make_node (OACC_CACHE);
36474 TREE_TYPE (stmt) = void_type_node;
36475 OACC_CACHE_CLAUSES (stmt) = clauses;
36476 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36477 add_stmt (stmt);
36479 return stmt;
36482 /* OpenACC 2.0:
36483 # pragma acc data oacc-data-clause[optseq] new-line
36484 structured-block */
36486 #define OACC_DATA_CLAUSE_MASK \
36487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36499 static tree
36500 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36502 tree stmt, clauses, block;
36503 unsigned int save;
36505 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36506 "#pragma acc data", pragma_tok);
36508 block = begin_omp_parallel ();
36509 save = cp_parser_begin_omp_structured_block (parser);
36510 cp_parser_statement (parser, NULL_TREE, false, if_p);
36511 cp_parser_end_omp_structured_block (parser, save);
36512 stmt = finish_oacc_data (clauses, block);
36513 return stmt;
36516 /* OpenACC 2.0:
36517 # pragma acc host_data <clauses> new-line
36518 structured-block */
36520 #define OACC_HOST_DATA_CLAUSE_MASK \
36521 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36523 static tree
36524 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36526 tree stmt, clauses, block;
36527 unsigned int save;
36529 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36530 "#pragma acc host_data", pragma_tok);
36532 block = begin_omp_parallel ();
36533 save = cp_parser_begin_omp_structured_block (parser);
36534 cp_parser_statement (parser, NULL_TREE, false, if_p);
36535 cp_parser_end_omp_structured_block (parser, save);
36536 stmt = finish_oacc_host_data (clauses, block);
36537 return stmt;
36540 /* OpenACC 2.0:
36541 # pragma acc declare oacc-data-clause[optseq] new-line
36544 #define OACC_DECLARE_CLAUSE_MASK \
36545 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36558 static tree
36559 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36561 tree clauses, stmt;
36562 bool error = false;
36564 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36565 "#pragma acc declare", pragma_tok, true);
36568 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36570 error_at (pragma_tok->location,
36571 "no valid clauses specified in %<#pragma acc declare%>");
36572 return NULL_TREE;
36575 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36577 location_t loc = OMP_CLAUSE_LOCATION (t);
36578 tree decl = OMP_CLAUSE_DECL (t);
36579 if (!DECL_P (decl))
36581 error_at (loc, "array section in %<#pragma acc declare%>");
36582 error = true;
36583 continue;
36585 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36586 switch (OMP_CLAUSE_MAP_KIND (t))
36588 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36589 case GOMP_MAP_FORCE_ALLOC:
36590 case GOMP_MAP_FORCE_TO:
36591 case GOMP_MAP_FORCE_DEVICEPTR:
36592 case GOMP_MAP_DEVICE_RESIDENT:
36593 break;
36595 case GOMP_MAP_LINK:
36596 if (!global_bindings_p ()
36597 && (TREE_STATIC (decl)
36598 || !DECL_EXTERNAL (decl)))
36600 error_at (loc,
36601 "%qD must be a global variable in "
36602 "%<#pragma acc declare link%>",
36603 decl);
36604 error = true;
36605 continue;
36607 break;
36609 default:
36610 if (global_bindings_p ())
36612 error_at (loc, "invalid OpenACC clause at file scope");
36613 error = true;
36614 continue;
36616 if (DECL_EXTERNAL (decl))
36618 error_at (loc,
36619 "invalid use of %<extern%> variable %qD "
36620 "in %<#pragma acc declare%>", decl);
36621 error = true;
36622 continue;
36624 else if (TREE_PUBLIC (decl))
36626 error_at (loc,
36627 "invalid use of %<global%> variable %qD "
36628 "in %<#pragma acc declare%>", decl);
36629 error = true;
36630 continue;
36632 break;
36635 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36636 || lookup_attribute ("omp declare target link",
36637 DECL_ATTRIBUTES (decl)))
36639 error_at (loc, "variable %qD used more than once with "
36640 "%<#pragma acc declare%>", decl);
36641 error = true;
36642 continue;
36645 if (!error)
36647 tree id;
36649 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36650 id = get_identifier ("omp declare target link");
36651 else
36652 id = get_identifier ("omp declare target");
36654 DECL_ATTRIBUTES (decl)
36655 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36656 if (global_bindings_p ())
36658 symtab_node *node = symtab_node::get (decl);
36659 if (node != NULL)
36661 node->offloadable = 1;
36662 if (ENABLE_OFFLOADING)
36664 g->have_offload = true;
36665 if (is_a <varpool_node *> (node))
36666 vec_safe_push (offload_vars, decl);
36673 if (error || global_bindings_p ())
36674 return NULL_TREE;
36676 stmt = make_node (OACC_DECLARE);
36677 TREE_TYPE (stmt) = void_type_node;
36678 OACC_DECLARE_CLAUSES (stmt) = clauses;
36679 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36681 add_stmt (stmt);
36683 return NULL_TREE;
36686 /* OpenACC 2.0:
36687 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36691 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36693 LOC is the location of the #pragma token.
36696 #define OACC_ENTER_DATA_CLAUSE_MASK \
36697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36705 #define OACC_EXIT_DATA_CLAUSE_MASK \
36706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36712 static tree
36713 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36714 bool enter)
36716 location_t loc = pragma_tok->location;
36717 tree stmt, clauses;
36718 const char *p = "";
36720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36721 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36723 if (strcmp (p, "data") != 0)
36725 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36726 enter ? "enter" : "exit");
36727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36728 return NULL_TREE;
36731 cp_lexer_consume_token (parser->lexer);
36733 if (enter)
36734 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36735 "#pragma acc enter data", pragma_tok);
36736 else
36737 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36738 "#pragma acc exit data", pragma_tok);
36740 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36742 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36743 enter ? "enter" : "exit");
36744 return NULL_TREE;
36747 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36748 TREE_TYPE (stmt) = void_type_node;
36749 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36750 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36751 add_stmt (stmt);
36752 return stmt;
36755 /* OpenACC 2.0:
36756 # pragma acc loop oacc-loop-clause[optseq] new-line
36757 structured-block */
36759 #define OACC_LOOP_CLAUSE_MASK \
36760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36771 static tree
36772 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36773 omp_clause_mask mask, tree *cclauses, bool *if_p)
36775 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36777 strcat (p_name, " loop");
36778 mask |= OACC_LOOP_CLAUSE_MASK;
36780 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36781 cclauses == NULL);
36782 if (cclauses)
36784 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36785 if (*cclauses)
36786 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36787 if (clauses)
36788 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36791 tree block = begin_omp_structured_block ();
36792 int save = cp_parser_begin_omp_structured_block (parser);
36793 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36794 cp_parser_end_omp_structured_block (parser, save);
36795 add_stmt (finish_omp_structured_block (block));
36797 return stmt;
36800 /* OpenACC 2.0:
36801 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36802 structured-block
36806 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36807 structured-block
36810 #define OACC_KERNELS_CLAUSE_MASK \
36811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36829 #define OACC_PARALLEL_CLAUSE_MASK \
36830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36851 static tree
36852 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36853 char *p_name, bool *if_p)
36855 omp_clause_mask mask;
36856 enum tree_code code;
36857 switch (cp_parser_pragma_kind (pragma_tok))
36859 case PRAGMA_OACC_KERNELS:
36860 strcat (p_name, " kernels");
36861 mask = OACC_KERNELS_CLAUSE_MASK;
36862 code = OACC_KERNELS;
36863 break;
36864 case PRAGMA_OACC_PARALLEL:
36865 strcat (p_name, " parallel");
36866 mask = OACC_PARALLEL_CLAUSE_MASK;
36867 code = OACC_PARALLEL;
36868 break;
36869 default:
36870 gcc_unreachable ();
36873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36875 const char *p
36876 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36877 if (strcmp (p, "loop") == 0)
36879 cp_lexer_consume_token (parser->lexer);
36880 tree block = begin_omp_parallel ();
36881 tree clauses;
36882 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36883 if_p);
36884 return finish_omp_construct (code, block, clauses);
36888 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36890 tree block = begin_omp_parallel ();
36891 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36892 cp_parser_statement (parser, NULL_TREE, false, if_p);
36893 cp_parser_end_omp_structured_block (parser, save);
36894 return finish_omp_construct (code, block, clauses);
36897 /* OpenACC 2.0:
36898 # pragma acc update oacc-update-clause[optseq] new-line
36901 #define OACC_UPDATE_CLAUSE_MASK \
36902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36909 static tree
36910 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36912 tree stmt, clauses;
36914 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36915 "#pragma acc update", pragma_tok);
36917 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36919 error_at (pragma_tok->location,
36920 "%<#pragma acc update%> must contain at least one "
36921 "%<device%> or %<host%> or %<self%> clause");
36922 return NULL_TREE;
36925 stmt = make_node (OACC_UPDATE);
36926 TREE_TYPE (stmt) = void_type_node;
36927 OACC_UPDATE_CLAUSES (stmt) = clauses;
36928 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36929 add_stmt (stmt);
36930 return stmt;
36933 /* OpenACC 2.0:
36934 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36936 LOC is the location of the #pragma token.
36939 #define OACC_WAIT_CLAUSE_MASK \
36940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36942 static tree
36943 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36945 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36946 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36948 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36949 list = cp_parser_oacc_wait_list (parser, loc, list);
36951 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36952 "#pragma acc wait", pragma_tok);
36954 stmt = c_finish_oacc_wait (loc, list, clauses);
36955 stmt = finish_expr_stmt (stmt);
36957 return stmt;
36960 /* OpenMP 4.0:
36961 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36963 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36971 static void
36972 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36973 enum pragma_context context)
36975 bool first_p = parser->omp_declare_simd == NULL;
36976 cp_omp_declare_simd_data data;
36977 if (first_p)
36979 data.error_seen = false;
36980 data.fndecl_seen = false;
36981 data.tokens = vNULL;
36982 data.clauses = NULL_TREE;
36983 /* It is safe to take the address of a local variable; it will only be
36984 used while this scope is live. */
36985 parser->omp_declare_simd = &data;
36988 /* Store away all pragma tokens. */
36989 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36990 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36991 cp_lexer_consume_token (parser->lexer);
36992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36993 parser->omp_declare_simd->error_seen = true;
36994 cp_parser_require_pragma_eol (parser, pragma_tok);
36995 struct cp_token_cache *cp
36996 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36997 parser->omp_declare_simd->tokens.safe_push (cp);
36999 if (first_p)
37001 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37002 cp_parser_pragma (parser, context, NULL);
37003 switch (context)
37005 case pragma_external:
37006 cp_parser_declaration (parser);
37007 break;
37008 case pragma_member:
37009 cp_parser_member_declaration (parser);
37010 break;
37011 case pragma_objc_icode:
37012 cp_parser_block_declaration (parser, /*statement_p=*/false);
37013 break;
37014 default:
37015 cp_parser_declaration_statement (parser);
37016 break;
37018 if (parser->omp_declare_simd
37019 && !parser->omp_declare_simd->error_seen
37020 && !parser->omp_declare_simd->fndecl_seen)
37021 error_at (pragma_tok->location,
37022 "%<#pragma omp declare simd%> not immediately followed by "
37023 "function declaration or definition");
37024 data.tokens.release ();
37025 parser->omp_declare_simd = NULL;
37029 /* Finalize #pragma omp declare simd clauses after direct declarator has
37030 been parsed, and put that into "omp declare simd" attribute. */
37032 static tree
37033 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37035 struct cp_token_cache *ce;
37036 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37037 int i;
37039 if (!data->error_seen && data->fndecl_seen)
37041 error ("%<#pragma omp declare simd%> not immediately followed by "
37042 "a single function declaration or definition");
37043 data->error_seen = true;
37045 if (data->error_seen)
37046 return attrs;
37048 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37050 tree c, cl;
37052 cp_parser_push_lexer_for_tokens (parser, ce);
37053 parser->lexer->in_pragma = true;
37054 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37055 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37056 cp_lexer_consume_token (parser->lexer);
37057 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37058 "#pragma omp declare simd", pragma_tok);
37059 cp_parser_pop_lexer (parser);
37060 if (cl)
37061 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37062 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37063 TREE_CHAIN (c) = attrs;
37064 if (processing_template_decl)
37065 ATTR_IS_DEPENDENT (c) = 1;
37066 attrs = c;
37069 data->fndecl_seen = true;
37070 return attrs;
37074 /* OpenMP 4.0:
37075 # pragma omp declare target new-line
37076 declarations and definitions
37077 # pragma omp end declare target new-line
37079 OpenMP 4.5:
37080 # pragma omp declare target ( extended-list ) new-line
37082 # pragma omp declare target declare-target-clauses[seq] new-line */
37084 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37088 static void
37089 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37091 tree clauses = NULL_TREE;
37092 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37093 clauses
37094 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37095 "#pragma omp declare target", pragma_tok);
37096 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37098 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37099 clauses);
37100 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37101 cp_parser_require_pragma_eol (parser, pragma_tok);
37103 else
37105 cp_parser_require_pragma_eol (parser, pragma_tok);
37106 scope_chain->omp_declare_target_attribute++;
37107 return;
37109 if (scope_chain->omp_declare_target_attribute)
37110 error_at (pragma_tok->location,
37111 "%<#pragma omp declare target%> with clauses in between "
37112 "%<#pragma omp declare target%> without clauses and "
37113 "%<#pragma omp end declare target%>");
37114 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37116 tree t = OMP_CLAUSE_DECL (c), id;
37117 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37118 tree at2 = lookup_attribute ("omp declare target link",
37119 DECL_ATTRIBUTES (t));
37120 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37122 id = get_identifier ("omp declare target link");
37123 std::swap (at1, at2);
37125 else
37126 id = get_identifier ("omp declare target");
37127 if (at2)
37129 error_at (OMP_CLAUSE_LOCATION (c),
37130 "%qD specified both in declare target %<link%> and %<to%>"
37131 " clauses", t);
37132 continue;
37134 if (!at1)
37136 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37137 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37138 continue;
37140 symtab_node *node = symtab_node::get (t);
37141 if (node != NULL)
37143 node->offloadable = 1;
37144 if (ENABLE_OFFLOADING)
37146 g->have_offload = true;
37147 if (is_a <varpool_node *> (node))
37148 vec_safe_push (offload_vars, t);
37155 static void
37156 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37158 const char *p = "";
37159 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37161 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37162 p = IDENTIFIER_POINTER (id);
37164 if (strcmp (p, "declare") == 0)
37166 cp_lexer_consume_token (parser->lexer);
37167 p = "";
37168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37170 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37171 p = IDENTIFIER_POINTER (id);
37173 if (strcmp (p, "target") == 0)
37174 cp_lexer_consume_token (parser->lexer);
37175 else
37177 cp_parser_error (parser, "expected %<target%>");
37178 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37179 return;
37182 else
37184 cp_parser_error (parser, "expected %<declare%>");
37185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37186 return;
37188 cp_parser_require_pragma_eol (parser, pragma_tok);
37189 if (!scope_chain->omp_declare_target_attribute)
37190 error_at (pragma_tok->location,
37191 "%<#pragma omp end declare target%> without corresponding "
37192 "%<#pragma omp declare target%>");
37193 else
37194 scope_chain->omp_declare_target_attribute--;
37197 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37198 expression and optional initializer clause of
37199 #pragma omp declare reduction. We store the expression(s) as
37200 either 3, 6 or 7 special statements inside of the artificial function's
37201 body. The first two statements are DECL_EXPRs for the artificial
37202 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37203 expression that uses those variables.
37204 If there was any INITIALIZER clause, this is followed by further statements,
37205 the fourth and fifth statements are DECL_EXPRs for the artificial
37206 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37207 constructor variant (first token after open paren is not omp_priv),
37208 then the sixth statement is a statement with the function call expression
37209 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37210 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37211 to initialize the OMP_PRIV artificial variable and there is seventh
37212 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37214 static bool
37215 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37217 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37218 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37219 type = TREE_TYPE (type);
37220 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37221 DECL_ARTIFICIAL (omp_out) = 1;
37222 pushdecl (omp_out);
37223 add_decl_expr (omp_out);
37224 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37225 DECL_ARTIFICIAL (omp_in) = 1;
37226 pushdecl (omp_in);
37227 add_decl_expr (omp_in);
37228 tree combiner;
37229 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37231 keep_next_level (true);
37232 tree block = begin_omp_structured_block ();
37233 combiner = cp_parser_expression (parser);
37234 finish_expr_stmt (combiner);
37235 block = finish_omp_structured_block (block);
37236 add_stmt (block);
37238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37239 return false;
37241 const char *p = "";
37242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37245 p = IDENTIFIER_POINTER (id);
37248 if (strcmp (p, "initializer") == 0)
37250 cp_lexer_consume_token (parser->lexer);
37251 matching_parens parens;
37252 if (!parens.require_open (parser))
37253 return false;
37255 p = "";
37256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37258 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37259 p = IDENTIFIER_POINTER (id);
37262 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37263 DECL_ARTIFICIAL (omp_priv) = 1;
37264 pushdecl (omp_priv);
37265 add_decl_expr (omp_priv);
37266 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37267 DECL_ARTIFICIAL (omp_orig) = 1;
37268 pushdecl (omp_orig);
37269 add_decl_expr (omp_orig);
37271 keep_next_level (true);
37272 block = begin_omp_structured_block ();
37274 bool ctor = false;
37275 if (strcmp (p, "omp_priv") == 0)
37277 bool is_direct_init, is_non_constant_init;
37278 ctor = true;
37279 cp_lexer_consume_token (parser->lexer);
37280 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37281 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37282 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37283 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37284 == CPP_CLOSE_PAREN
37285 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37286 == CPP_CLOSE_PAREN))
37288 finish_omp_structured_block (block);
37289 error ("invalid initializer clause");
37290 return false;
37292 initializer = cp_parser_initializer (parser, &is_direct_init,
37293 &is_non_constant_init);
37294 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37295 NULL_TREE, LOOKUP_ONLYCONVERTING);
37297 else
37299 cp_parser_parse_tentatively (parser);
37300 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37301 /*check_dependency_p=*/true,
37302 /*template_p=*/NULL,
37303 /*declarator_p=*/false,
37304 /*optional_p=*/false);
37305 vec<tree, va_gc> *args;
37306 if (fn_name == error_mark_node
37307 || cp_parser_error_occurred (parser)
37308 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37309 || ((args = cp_parser_parenthesized_expression_list
37310 (parser, non_attr, /*cast_p=*/false,
37311 /*allow_expansion_p=*/true,
37312 /*non_constant_p=*/NULL)),
37313 cp_parser_error_occurred (parser)))
37315 finish_omp_structured_block (block);
37316 cp_parser_abort_tentative_parse (parser);
37317 cp_parser_error (parser, "expected id-expression (arguments)");
37318 return false;
37320 unsigned int i;
37321 tree arg;
37322 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37323 if (arg == omp_priv
37324 || (TREE_CODE (arg) == ADDR_EXPR
37325 && TREE_OPERAND (arg, 0) == omp_priv))
37326 break;
37327 cp_parser_abort_tentative_parse (parser);
37328 if (arg == NULL_TREE)
37329 error ("one of the initializer call arguments should be %<omp_priv%>"
37330 " or %<&omp_priv%>");
37331 initializer = cp_parser_postfix_expression (parser, false, false, false,
37332 false, NULL);
37333 finish_expr_stmt (initializer);
37336 block = finish_omp_structured_block (block);
37337 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37338 add_stmt (block);
37340 if (ctor)
37341 add_decl_expr (omp_orig);
37343 if (!parens.require_close (parser))
37344 return false;
37347 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37348 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37349 UNKNOWN_LOCATION);
37351 return true;
37354 /* OpenMP 4.0
37355 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37356 initializer-clause[opt] new-line
37358 initializer-clause:
37359 initializer (omp_priv initializer)
37360 initializer (function-name (argument-list)) */
37362 static void
37363 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37364 enum pragma_context)
37366 auto_vec<tree> types;
37367 enum tree_code reduc_code = ERROR_MARK;
37368 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37369 unsigned int i;
37370 cp_token *first_token;
37371 cp_token_cache *cp;
37372 int errs;
37373 void *p;
37375 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37376 p = obstack_alloc (&declarator_obstack, 0);
37378 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37379 goto fail;
37381 switch (cp_lexer_peek_token (parser->lexer)->type)
37383 case CPP_PLUS:
37384 reduc_code = PLUS_EXPR;
37385 break;
37386 case CPP_MULT:
37387 reduc_code = MULT_EXPR;
37388 break;
37389 case CPP_MINUS:
37390 reduc_code = MINUS_EXPR;
37391 break;
37392 case CPP_AND:
37393 reduc_code = BIT_AND_EXPR;
37394 break;
37395 case CPP_XOR:
37396 reduc_code = BIT_XOR_EXPR;
37397 break;
37398 case CPP_OR:
37399 reduc_code = BIT_IOR_EXPR;
37400 break;
37401 case CPP_AND_AND:
37402 reduc_code = TRUTH_ANDIF_EXPR;
37403 break;
37404 case CPP_OR_OR:
37405 reduc_code = TRUTH_ORIF_EXPR;
37406 break;
37407 case CPP_NAME:
37408 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37409 break;
37410 default:
37411 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37412 "%<|%>, %<&&%>, %<||%> or identifier");
37413 goto fail;
37416 if (reduc_code != ERROR_MARK)
37417 cp_lexer_consume_token (parser->lexer);
37419 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37420 if (reduc_id == error_mark_node)
37421 goto fail;
37423 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37424 goto fail;
37426 /* Types may not be defined in declare reduction type list. */
37427 const char *saved_message;
37428 saved_message = parser->type_definition_forbidden_message;
37429 parser->type_definition_forbidden_message
37430 = G_("types may not be defined in declare reduction type list");
37431 bool saved_colon_corrects_to_scope_p;
37432 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37433 parser->colon_corrects_to_scope_p = false;
37434 bool saved_colon_doesnt_start_class_def_p;
37435 saved_colon_doesnt_start_class_def_p
37436 = parser->colon_doesnt_start_class_def_p;
37437 parser->colon_doesnt_start_class_def_p = true;
37439 while (true)
37441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37442 type = cp_parser_type_id (parser);
37443 if (type == error_mark_node)
37445 else if (ARITHMETIC_TYPE_P (type)
37446 && (orig_reduc_id == NULL_TREE
37447 || (TREE_CODE (type) != COMPLEX_TYPE
37448 && (id_equal (orig_reduc_id, "min")
37449 || id_equal (orig_reduc_id, "max")))))
37450 error_at (loc, "predeclared arithmetic type %qT in "
37451 "%<#pragma omp declare reduction%>", type);
37452 else if (TREE_CODE (type) == FUNCTION_TYPE
37453 || TREE_CODE (type) == METHOD_TYPE
37454 || TREE_CODE (type) == ARRAY_TYPE)
37455 error_at (loc, "function or array type %qT in "
37456 "%<#pragma omp declare reduction%>", type);
37457 else if (TREE_CODE (type) == REFERENCE_TYPE)
37458 error_at (loc, "reference type %qT in "
37459 "%<#pragma omp declare reduction%>", type);
37460 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37461 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37462 "%<#pragma omp declare reduction%>", type);
37463 else
37464 types.safe_push (type);
37466 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37467 cp_lexer_consume_token (parser->lexer);
37468 else
37469 break;
37472 /* Restore the saved message. */
37473 parser->type_definition_forbidden_message = saved_message;
37474 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37475 parser->colon_doesnt_start_class_def_p
37476 = saved_colon_doesnt_start_class_def_p;
37478 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37479 || types.is_empty ())
37481 fail:
37482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37483 goto done;
37486 first_token = cp_lexer_peek_token (parser->lexer);
37487 cp = NULL;
37488 errs = errorcount;
37489 FOR_EACH_VEC_ELT (types, i, type)
37491 tree fntype
37492 = build_function_type_list (void_type_node,
37493 cp_build_reference_type (type, false),
37494 NULL_TREE);
37495 tree this_reduc_id = reduc_id;
37496 if (!dependent_type_p (type))
37497 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37498 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37499 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37500 DECL_ARTIFICIAL (fndecl) = 1;
37501 DECL_EXTERNAL (fndecl) = 1;
37502 DECL_DECLARED_INLINE_P (fndecl) = 1;
37503 DECL_IGNORED_P (fndecl) = 1;
37504 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37505 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37506 DECL_ATTRIBUTES (fndecl)
37507 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37508 DECL_ATTRIBUTES (fndecl));
37509 if (processing_template_decl)
37510 fndecl = push_template_decl (fndecl);
37511 bool block_scope = false;
37512 tree block = NULL_TREE;
37513 if (current_function_decl)
37515 block_scope = true;
37516 DECL_CONTEXT (fndecl) = global_namespace;
37517 if (!processing_template_decl)
37518 pushdecl (fndecl);
37520 else if (current_class_type)
37522 if (cp == NULL)
37524 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37525 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37526 cp_lexer_consume_token (parser->lexer);
37527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37528 goto fail;
37529 cp = cp_token_cache_new (first_token,
37530 cp_lexer_peek_nth_token (parser->lexer,
37531 2));
37533 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37534 finish_member_declaration (fndecl);
37535 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37536 DECL_PENDING_INLINE_P (fndecl) = 1;
37537 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37538 continue;
37540 else
37542 DECL_CONTEXT (fndecl) = current_namespace;
37543 pushdecl (fndecl);
37545 if (!block_scope)
37546 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37547 else
37548 block = begin_omp_structured_block ();
37549 if (cp)
37551 cp_parser_push_lexer_for_tokens (parser, cp);
37552 parser->lexer->in_pragma = true;
37554 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37556 if (!block_scope)
37557 finish_function (/*inline_p=*/false);
37558 else
37559 DECL_CONTEXT (fndecl) = current_function_decl;
37560 if (cp)
37561 cp_parser_pop_lexer (parser);
37562 goto fail;
37564 if (cp)
37565 cp_parser_pop_lexer (parser);
37566 if (!block_scope)
37567 finish_function (/*inline_p=*/false);
37568 else
37570 DECL_CONTEXT (fndecl) = current_function_decl;
37571 block = finish_omp_structured_block (block);
37572 if (TREE_CODE (block) == BIND_EXPR)
37573 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37574 else if (TREE_CODE (block) == STATEMENT_LIST)
37575 DECL_SAVED_TREE (fndecl) = block;
37576 if (processing_template_decl)
37577 add_decl_expr (fndecl);
37579 cp_check_omp_declare_reduction (fndecl);
37580 if (cp == NULL && types.length () > 1)
37581 cp = cp_token_cache_new (first_token,
37582 cp_lexer_peek_nth_token (parser->lexer, 2));
37583 if (errs != errorcount)
37584 break;
37587 cp_parser_require_pragma_eol (parser, pragma_tok);
37589 done:
37590 /* Free any declarators allocated. */
37591 obstack_free (&declarator_obstack, p);
37594 /* OpenMP 4.0
37595 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37596 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37597 initializer-clause[opt] new-line
37598 #pragma omp declare target new-line */
37600 static bool
37601 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37602 enum pragma_context context)
37604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37607 const char *p = IDENTIFIER_POINTER (id);
37609 if (strcmp (p, "simd") == 0)
37611 cp_lexer_consume_token (parser->lexer);
37612 cp_parser_omp_declare_simd (parser, pragma_tok,
37613 context);
37614 return true;
37616 cp_ensure_no_omp_declare_simd (parser);
37617 if (strcmp (p, "reduction") == 0)
37619 cp_lexer_consume_token (parser->lexer);
37620 cp_parser_omp_declare_reduction (parser, pragma_tok,
37621 context);
37622 return false;
37624 if (!flag_openmp) /* flag_openmp_simd */
37626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37627 return false;
37629 if (strcmp (p, "target") == 0)
37631 cp_lexer_consume_token (parser->lexer);
37632 cp_parser_omp_declare_target (parser, pragma_tok);
37633 return false;
37636 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37637 "or %<target%>");
37638 cp_parser_require_pragma_eol (parser, pragma_tok);
37639 return false;
37642 /* OpenMP 4.5:
37643 #pragma omp taskloop taskloop-clause[optseq] new-line
37644 for-loop
37646 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37647 for-loop */
37649 #define OMP_TASKLOOP_CLAUSE_MASK \
37650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37665 static tree
37666 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37667 char *p_name, omp_clause_mask mask, tree *cclauses,
37668 bool *if_p)
37670 tree clauses, sb, ret;
37671 unsigned int save;
37672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37674 strcat (p_name, " taskloop");
37675 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37680 const char *p = IDENTIFIER_POINTER (id);
37682 if (strcmp (p, "simd") == 0)
37684 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37685 if (cclauses == NULL)
37686 cclauses = cclauses_buf;
37688 cp_lexer_consume_token (parser->lexer);
37689 if (!flag_openmp) /* flag_openmp_simd */
37690 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37691 cclauses, if_p);
37692 sb = begin_omp_structured_block ();
37693 save = cp_parser_begin_omp_structured_block (parser);
37694 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37695 cclauses, if_p);
37696 cp_parser_end_omp_structured_block (parser, save);
37697 tree body = finish_omp_structured_block (sb);
37698 if (ret == NULL)
37699 return ret;
37700 ret = make_node (OMP_TASKLOOP);
37701 TREE_TYPE (ret) = void_type_node;
37702 OMP_FOR_BODY (ret) = body;
37703 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37704 SET_EXPR_LOCATION (ret, loc);
37705 add_stmt (ret);
37706 return ret;
37709 if (!flag_openmp) /* flag_openmp_simd */
37711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37712 return NULL_TREE;
37715 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37716 cclauses == NULL);
37717 if (cclauses)
37719 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37720 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37723 sb = begin_omp_structured_block ();
37724 save = cp_parser_begin_omp_structured_block (parser);
37726 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37727 if_p);
37729 cp_parser_end_omp_structured_block (parser, save);
37730 add_stmt (finish_omp_structured_block (sb));
37732 return ret;
37736 /* OpenACC 2.0:
37737 # pragma acc routine oacc-routine-clause[optseq] new-line
37738 function-definition
37740 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37743 #define OACC_ROUTINE_CLAUSE_MASK \
37744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37750 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37751 component, which must resolve to a declared namespace-scope
37752 function. The clauses are either processed directly (for a named
37753 function), or defered until the immediatley following declaration
37754 is parsed. */
37756 static void
37757 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37758 enum pragma_context context)
37760 gcc_checking_assert (context == pragma_external);
37761 /* The checking for "another pragma following this one" in the "no optional
37762 '( name )'" case makes sure that we dont re-enter. */
37763 gcc_checking_assert (parser->oacc_routine == NULL);
37765 cp_oacc_routine_data data;
37766 data.error_seen = false;
37767 data.fndecl_seen = false;
37768 data.tokens = vNULL;
37769 data.clauses = NULL_TREE;
37770 data.loc = pragma_tok->location;
37771 /* It is safe to take the address of a local variable; it will only be
37772 used while this scope is live. */
37773 parser->oacc_routine = &data;
37775 /* Look for optional '( name )'. */
37776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37778 matching_parens parens;
37779 parens.consume_open (parser); /* '(' */
37781 /* We parse the name as an id-expression. If it resolves to
37782 anything other than a non-overloaded function at namespace
37783 scope, it's an error. */
37784 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37785 tree name = cp_parser_id_expression (parser,
37786 /*template_keyword_p=*/false,
37787 /*check_dependency_p=*/false,
37788 /*template_p=*/NULL,
37789 /*declarator_p=*/false,
37790 /*optional_p=*/false);
37791 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37792 if (name != error_mark_node && decl == error_mark_node)
37793 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37795 if (decl == error_mark_node
37796 || !parens.require_close (parser))
37798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37799 parser->oacc_routine = NULL;
37800 return;
37803 data.clauses
37804 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37805 "#pragma acc routine",
37806 cp_lexer_peek_token (parser->lexer));
37808 if (decl && is_overloaded_fn (decl)
37809 && (TREE_CODE (decl) != FUNCTION_DECL
37810 || DECL_FUNCTION_TEMPLATE_P (decl)))
37812 error_at (name_loc,
37813 "%<#pragma acc routine%> names a set of overloads");
37814 parser->oacc_routine = NULL;
37815 return;
37818 /* Perhaps we should use the same rule as declarations in different
37819 namespaces? */
37820 if (!DECL_NAMESPACE_SCOPE_P (decl))
37822 error_at (name_loc,
37823 "%qD does not refer to a namespace scope function", decl);
37824 parser->oacc_routine = NULL;
37825 return;
37828 if (TREE_CODE (decl) != FUNCTION_DECL)
37830 error_at (name_loc, "%qD does not refer to a function", decl);
37831 parser->oacc_routine = NULL;
37832 return;
37835 cp_finalize_oacc_routine (parser, decl, false);
37836 parser->oacc_routine = NULL;
37838 else /* No optional '( name )'. */
37840 /* Store away all pragma tokens. */
37841 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37842 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37843 cp_lexer_consume_token (parser->lexer);
37844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37845 parser->oacc_routine->error_seen = true;
37846 cp_parser_require_pragma_eol (parser, pragma_tok);
37847 struct cp_token_cache *cp
37848 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37849 parser->oacc_routine->tokens.safe_push (cp);
37851 /* Emit a helpful diagnostic if there's another pragma following this
37852 one. */
37853 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37855 cp_ensure_no_oacc_routine (parser);
37856 data.tokens.release ();
37857 /* ..., and then just keep going. */
37858 return;
37861 /* We only have to consider the pragma_external case here. */
37862 cp_parser_declaration (parser);
37863 if (parser->oacc_routine
37864 && !parser->oacc_routine->fndecl_seen)
37865 cp_ensure_no_oacc_routine (parser);
37866 else
37867 parser->oacc_routine = NULL;
37868 data.tokens.release ();
37872 /* Finalize #pragma acc routine clauses after direct declarator has
37873 been parsed. */
37875 static tree
37876 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37878 struct cp_token_cache *ce;
37879 cp_oacc_routine_data *data = parser->oacc_routine;
37881 if (!data->error_seen && data->fndecl_seen)
37883 error_at (data->loc,
37884 "%<#pragma acc routine%> not immediately followed by "
37885 "a single function declaration or definition");
37886 data->error_seen = true;
37888 if (data->error_seen)
37889 return attrs;
37891 gcc_checking_assert (data->tokens.length () == 1);
37892 ce = data->tokens[0];
37894 cp_parser_push_lexer_for_tokens (parser, ce);
37895 parser->lexer->in_pragma = true;
37896 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37898 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37899 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37900 parser->oacc_routine->clauses
37901 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37902 "#pragma acc routine", pragma_tok);
37903 cp_parser_pop_lexer (parser);
37904 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37905 fndecl_seen. */
37907 return attrs;
37910 /* Apply any saved OpenACC routine clauses to a just-parsed
37911 declaration. */
37913 static void
37914 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37916 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37918 /* Keep going if we're in error reporting mode. */
37919 if (parser->oacc_routine->error_seen
37920 || fndecl == error_mark_node)
37921 return;
37923 if (parser->oacc_routine->fndecl_seen)
37925 error_at (parser->oacc_routine->loc,
37926 "%<#pragma acc routine%> not immediately followed by"
37927 " a single function declaration or definition");
37928 parser->oacc_routine = NULL;
37929 return;
37931 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37933 cp_ensure_no_oacc_routine (parser);
37934 return;
37937 if (oacc_get_fn_attrib (fndecl))
37939 error_at (parser->oacc_routine->loc,
37940 "%<#pragma acc routine%> already applied to %qD", fndecl);
37941 parser->oacc_routine = NULL;
37942 return;
37945 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37947 error_at (parser->oacc_routine->loc,
37948 TREE_USED (fndecl)
37949 ? G_("%<#pragma acc routine%> must be applied before use")
37950 : G_("%<#pragma acc routine%> must be applied before "
37951 "definition"));
37952 parser->oacc_routine = NULL;
37953 return;
37956 /* Process the routine's dimension clauses. */
37957 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37958 oacc_replace_fn_attrib (fndecl, dims);
37960 /* Add an "omp declare target" attribute. */
37961 DECL_ATTRIBUTES (fndecl)
37962 = tree_cons (get_identifier ("omp declare target"),
37963 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37965 /* Don't unset parser->oacc_routine here: we may still need it to
37966 diagnose wrong usage. But, remember that we've used this "#pragma acc
37967 routine". */
37968 parser->oacc_routine->fndecl_seen = true;
37972 /* Main entry point to OpenMP statement pragmas. */
37974 static void
37975 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37977 tree stmt;
37978 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37979 omp_clause_mask mask (0);
37981 switch (cp_parser_pragma_kind (pragma_tok))
37983 case PRAGMA_OACC_ATOMIC:
37984 cp_parser_omp_atomic (parser, pragma_tok);
37985 return;
37986 case PRAGMA_OACC_CACHE:
37987 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37988 break;
37989 case PRAGMA_OACC_DATA:
37990 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37991 break;
37992 case PRAGMA_OACC_ENTER_DATA:
37993 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37994 break;
37995 case PRAGMA_OACC_EXIT_DATA:
37996 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37997 break;
37998 case PRAGMA_OACC_HOST_DATA:
37999 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38000 break;
38001 case PRAGMA_OACC_KERNELS:
38002 case PRAGMA_OACC_PARALLEL:
38003 strcpy (p_name, "#pragma acc");
38004 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38005 if_p);
38006 break;
38007 case PRAGMA_OACC_LOOP:
38008 strcpy (p_name, "#pragma acc");
38009 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38010 if_p);
38011 break;
38012 case PRAGMA_OACC_UPDATE:
38013 stmt = cp_parser_oacc_update (parser, pragma_tok);
38014 break;
38015 case PRAGMA_OACC_WAIT:
38016 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38017 break;
38018 case PRAGMA_OMP_ATOMIC:
38019 cp_parser_omp_atomic (parser, pragma_tok);
38020 return;
38021 case PRAGMA_OMP_CRITICAL:
38022 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38023 break;
38024 case PRAGMA_OMP_DISTRIBUTE:
38025 strcpy (p_name, "#pragma omp");
38026 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38027 if_p);
38028 break;
38029 case PRAGMA_OMP_FOR:
38030 strcpy (p_name, "#pragma omp");
38031 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38032 if_p);
38033 break;
38034 case PRAGMA_OMP_MASTER:
38035 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38036 break;
38037 case PRAGMA_OMP_PARALLEL:
38038 strcpy (p_name, "#pragma omp");
38039 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38040 if_p);
38041 break;
38042 case PRAGMA_OMP_SECTIONS:
38043 strcpy (p_name, "#pragma omp");
38044 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38045 break;
38046 case PRAGMA_OMP_SIMD:
38047 strcpy (p_name, "#pragma omp");
38048 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38049 if_p);
38050 break;
38051 case PRAGMA_OMP_SINGLE:
38052 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38053 break;
38054 case PRAGMA_OMP_TASK:
38055 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38056 break;
38057 case PRAGMA_OMP_TASKGROUP:
38058 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38059 break;
38060 case PRAGMA_OMP_TASKLOOP:
38061 strcpy (p_name, "#pragma omp");
38062 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38063 if_p);
38064 break;
38065 case PRAGMA_OMP_TEAMS:
38066 strcpy (p_name, "#pragma omp");
38067 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38068 if_p);
38069 break;
38070 default:
38071 gcc_unreachable ();
38074 protected_set_expr_location (stmt, pragma_tok->location);
38077 /* Transactional Memory parsing routines. */
38079 /* Parse a transaction attribute.
38081 txn-attribute:
38082 attribute
38083 [ [ identifier ] ]
38085 We use this instead of cp_parser_attributes_opt for transactions to avoid
38086 the pedwarn in C++98 mode. */
38088 static tree
38089 cp_parser_txn_attribute_opt (cp_parser *parser)
38091 cp_token *token;
38092 tree attr_name, attr = NULL;
38094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38095 return cp_parser_attributes_opt (parser);
38097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38098 return NULL_TREE;
38099 cp_lexer_consume_token (parser->lexer);
38100 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38101 goto error1;
38103 token = cp_lexer_peek_token (parser->lexer);
38104 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38106 token = cp_lexer_consume_token (parser->lexer);
38108 attr_name = (token->type == CPP_KEYWORD
38109 /* For keywords, use the canonical spelling,
38110 not the parsed identifier. */
38111 ? ridpointers[(int) token->keyword]
38112 : token->u.value);
38113 attr = build_tree_list (attr_name, NULL_TREE);
38115 else
38116 cp_parser_error (parser, "expected identifier");
38118 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38119 error1:
38120 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38121 return attr;
38124 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38126 transaction-statement:
38127 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38128 compound-statement
38129 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38132 static tree
38133 cp_parser_transaction (cp_parser *parser, cp_token *token)
38135 unsigned char old_in = parser->in_transaction;
38136 unsigned char this_in = 1, new_in;
38137 enum rid keyword = token->keyword;
38138 tree stmt, attrs, noex;
38140 cp_lexer_consume_token (parser->lexer);
38142 if (keyword == RID_TRANSACTION_RELAXED
38143 || keyword == RID_SYNCHRONIZED)
38144 this_in |= TM_STMT_ATTR_RELAXED;
38145 else
38147 attrs = cp_parser_txn_attribute_opt (parser);
38148 if (attrs)
38149 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38152 /* Parse a noexcept specification. */
38153 if (keyword == RID_ATOMIC_NOEXCEPT)
38154 noex = boolean_true_node;
38155 else if (keyword == RID_ATOMIC_CANCEL)
38157 /* cancel-and-throw is unimplemented. */
38158 sorry ("atomic_cancel");
38159 noex = NULL_TREE;
38161 else
38162 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38164 /* Keep track if we're in the lexical scope of an outer transaction. */
38165 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38167 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38169 parser->in_transaction = new_in;
38170 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38171 parser->in_transaction = old_in;
38173 finish_transaction_stmt (stmt, NULL, this_in, noex);
38175 return stmt;
38178 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38180 transaction-expression:
38181 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38182 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38185 static tree
38186 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38188 unsigned char old_in = parser->in_transaction;
38189 unsigned char this_in = 1;
38190 cp_token *token;
38191 tree expr, noex;
38192 bool noex_expr;
38193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38195 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38196 || keyword == RID_TRANSACTION_RELAXED);
38198 if (!flag_tm)
38199 error_at (loc,
38200 keyword == RID_TRANSACTION_RELAXED
38201 ? G_("%<__transaction_relaxed%> without transactional memory "
38202 "support enabled")
38203 : G_("%<__transaction_atomic%> without transactional memory "
38204 "support enabled"));
38206 token = cp_parser_require_keyword (parser, keyword,
38207 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38208 : RT_TRANSACTION_RELAXED));
38209 gcc_assert (token != NULL);
38211 if (keyword == RID_TRANSACTION_RELAXED)
38212 this_in |= TM_STMT_ATTR_RELAXED;
38214 /* Set this early. This might mean that we allow transaction_cancel in
38215 an expression that we find out later actually has to be a constexpr.
38216 However, we expect that cxx_constant_value will be able to deal with
38217 this; also, if the noexcept has no constexpr, then what we parse next
38218 really is a transaction's body. */
38219 parser->in_transaction = this_in;
38221 /* Parse a noexcept specification. */
38222 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38223 true);
38225 if (!noex || !noex_expr
38226 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38228 matching_parens parens;
38229 parens.require_open (parser);
38231 expr = cp_parser_expression (parser);
38232 expr = finish_parenthesized_expr (expr);
38234 parens.require_close (parser);
38236 else
38238 /* The only expression that is available got parsed for the noexcept
38239 already. noexcept is true then. */
38240 expr = noex;
38241 noex = boolean_true_node;
38244 expr = build_transaction_expr (token->location, expr, this_in, noex);
38245 parser->in_transaction = old_in;
38247 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38248 return error_mark_node;
38250 return (flag_tm ? expr : error_mark_node);
38253 /* Parse a function-transaction-block.
38255 function-transaction-block:
38256 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38257 function-body
38258 __transaction_atomic txn-attribute[opt] function-try-block
38259 __transaction_relaxed ctor-initializer[opt] function-body
38260 __transaction_relaxed function-try-block
38263 static void
38264 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38266 unsigned char old_in = parser->in_transaction;
38267 unsigned char new_in = 1;
38268 tree compound_stmt, stmt, attrs;
38269 cp_token *token;
38271 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38272 || keyword == RID_TRANSACTION_RELAXED);
38273 token = cp_parser_require_keyword (parser, keyword,
38274 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38275 : RT_TRANSACTION_RELAXED));
38276 gcc_assert (token != NULL);
38278 if (keyword == RID_TRANSACTION_RELAXED)
38279 new_in |= TM_STMT_ATTR_RELAXED;
38280 else
38282 attrs = cp_parser_txn_attribute_opt (parser);
38283 if (attrs)
38284 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38287 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38289 parser->in_transaction = new_in;
38291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38292 cp_parser_function_try_block (parser);
38293 else
38294 cp_parser_ctor_initializer_opt_and_function_body
38295 (parser, /*in_function_try_block=*/false);
38297 parser->in_transaction = old_in;
38299 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38302 /* Parse a __transaction_cancel statement.
38304 cancel-statement:
38305 __transaction_cancel txn-attribute[opt] ;
38306 __transaction_cancel txn-attribute[opt] throw-expression ;
38308 ??? Cancel and throw is not yet implemented. */
38310 static tree
38311 cp_parser_transaction_cancel (cp_parser *parser)
38313 cp_token *token;
38314 bool is_outer = false;
38315 tree stmt, attrs;
38317 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38318 RT_TRANSACTION_CANCEL);
38319 gcc_assert (token != NULL);
38321 attrs = cp_parser_txn_attribute_opt (parser);
38322 if (attrs)
38323 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38325 /* ??? Parse cancel-and-throw here. */
38327 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38329 if (!flag_tm)
38331 error_at (token->location, "%<__transaction_cancel%> without "
38332 "transactional memory support enabled");
38333 return error_mark_node;
38335 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38337 error_at (token->location, "%<__transaction_cancel%> within a "
38338 "%<__transaction_relaxed%>");
38339 return error_mark_node;
38341 else if (is_outer)
38343 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38344 && !is_tm_may_cancel_outer (current_function_decl))
38346 error_at (token->location, "outer %<__transaction_cancel%> not "
38347 "within outer %<__transaction_atomic%>");
38348 error_at (token->location,
38349 " or a %<transaction_may_cancel_outer%> function");
38350 return error_mark_node;
38353 else if (parser->in_transaction == 0)
38355 error_at (token->location, "%<__transaction_cancel%> not within "
38356 "%<__transaction_atomic%>");
38357 return error_mark_node;
38360 stmt = build_tm_abort_call (token->location, is_outer);
38361 add_stmt (stmt);
38363 return stmt;
38366 /* The parser. */
38368 static GTY (()) cp_parser *the_parser;
38371 /* Special handling for the first token or line in the file. The first
38372 thing in the file might be #pragma GCC pch_preprocess, which loads a
38373 PCH file, which is a GC collection point. So we need to handle this
38374 first pragma without benefit of an existing lexer structure.
38376 Always returns one token to the caller in *FIRST_TOKEN. This is
38377 either the true first token of the file, or the first token after
38378 the initial pragma. */
38380 static void
38381 cp_parser_initial_pragma (cp_token *first_token)
38383 tree name = NULL;
38385 cp_lexer_get_preprocessor_token (NULL, first_token);
38386 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38387 return;
38389 cp_lexer_get_preprocessor_token (NULL, first_token);
38390 if (first_token->type == CPP_STRING)
38392 name = first_token->u.value;
38394 cp_lexer_get_preprocessor_token (NULL, first_token);
38395 if (first_token->type != CPP_PRAGMA_EOL)
38396 error_at (first_token->location,
38397 "junk at end of %<#pragma GCC pch_preprocess%>");
38399 else
38400 error_at (first_token->location, "expected string literal");
38402 /* Skip to the end of the pragma. */
38403 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38404 cp_lexer_get_preprocessor_token (NULL, first_token);
38406 /* Now actually load the PCH file. */
38407 if (name)
38408 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38410 /* Read one more token to return to our caller. We have to do this
38411 after reading the PCH file in, since its pointers have to be
38412 live. */
38413 cp_lexer_get_preprocessor_token (NULL, first_token);
38416 /* Normal parsing of a pragma token. Here we can (and must) use the
38417 regular lexer. */
38419 static bool
38420 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38422 cp_token *pragma_tok;
38423 unsigned int id;
38424 tree stmt;
38425 bool ret;
38427 pragma_tok = cp_lexer_consume_token (parser->lexer);
38428 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38429 parser->lexer->in_pragma = true;
38431 id = cp_parser_pragma_kind (pragma_tok);
38432 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38433 cp_ensure_no_omp_declare_simd (parser);
38434 switch (id)
38436 case PRAGMA_GCC_PCH_PREPROCESS:
38437 error_at (pragma_tok->location,
38438 "%<#pragma GCC pch_preprocess%> must be first");
38439 break;
38441 case PRAGMA_OMP_BARRIER:
38442 switch (context)
38444 case pragma_compound:
38445 cp_parser_omp_barrier (parser, pragma_tok);
38446 return false;
38447 case pragma_stmt:
38448 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38449 "used in compound statements", "omp barrier");
38450 break;
38451 default:
38452 goto bad_stmt;
38454 break;
38456 case PRAGMA_OMP_FLUSH:
38457 switch (context)
38459 case pragma_compound:
38460 cp_parser_omp_flush (parser, pragma_tok);
38461 return false;
38462 case pragma_stmt:
38463 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38464 "used in compound statements", "omp flush");
38465 break;
38466 default:
38467 goto bad_stmt;
38469 break;
38471 case PRAGMA_OMP_TASKWAIT:
38472 switch (context)
38474 case pragma_compound:
38475 cp_parser_omp_taskwait (parser, pragma_tok);
38476 return false;
38477 case pragma_stmt:
38478 error_at (pragma_tok->location,
38479 "%<#pragma %s%> may only be used in compound statements",
38480 "omp taskwait");
38481 break;
38482 default:
38483 goto bad_stmt;
38485 break;
38487 case PRAGMA_OMP_TASKYIELD:
38488 switch (context)
38490 case pragma_compound:
38491 cp_parser_omp_taskyield (parser, pragma_tok);
38492 return false;
38493 case pragma_stmt:
38494 error_at (pragma_tok->location,
38495 "%<#pragma %s%> may only be used in compound statements",
38496 "omp taskyield");
38497 break;
38498 default:
38499 goto bad_stmt;
38501 break;
38503 case PRAGMA_OMP_CANCEL:
38504 switch (context)
38506 case pragma_compound:
38507 cp_parser_omp_cancel (parser, pragma_tok);
38508 return false;
38509 case pragma_stmt:
38510 error_at (pragma_tok->location,
38511 "%<#pragma %s%> may only be used in compound statements",
38512 "omp cancel");
38513 break;
38514 default:
38515 goto bad_stmt;
38517 break;
38519 case PRAGMA_OMP_CANCELLATION_POINT:
38520 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38521 return false;
38523 case PRAGMA_OMP_THREADPRIVATE:
38524 cp_parser_omp_threadprivate (parser, pragma_tok);
38525 return false;
38527 case PRAGMA_OMP_DECLARE:
38528 return cp_parser_omp_declare (parser, pragma_tok, context);
38530 case PRAGMA_OACC_DECLARE:
38531 cp_parser_oacc_declare (parser, pragma_tok);
38532 return false;
38534 case PRAGMA_OACC_ENTER_DATA:
38535 if (context == pragma_stmt)
38537 error_at (pragma_tok->location,
38538 "%<#pragma %s%> may only be used in compound statements",
38539 "acc enter data");
38540 break;
38542 else if (context != pragma_compound)
38543 goto bad_stmt;
38544 cp_parser_omp_construct (parser, pragma_tok, if_p);
38545 return true;
38547 case PRAGMA_OACC_EXIT_DATA:
38548 if (context == pragma_stmt)
38550 error_at (pragma_tok->location,
38551 "%<#pragma %s%> may only be used in compound statements",
38552 "acc exit data");
38553 break;
38555 else if (context != pragma_compound)
38556 goto bad_stmt;
38557 cp_parser_omp_construct (parser, pragma_tok, if_p);
38558 return true;
38560 case PRAGMA_OACC_ROUTINE:
38561 if (context != pragma_external)
38563 error_at (pragma_tok->location,
38564 "%<#pragma acc routine%> must be at file scope");
38565 break;
38567 cp_parser_oacc_routine (parser, pragma_tok, context);
38568 return false;
38570 case PRAGMA_OACC_UPDATE:
38571 if (context == pragma_stmt)
38573 error_at (pragma_tok->location,
38574 "%<#pragma %s%> may only be used in compound statements",
38575 "acc update");
38576 break;
38578 else if (context != pragma_compound)
38579 goto bad_stmt;
38580 cp_parser_omp_construct (parser, pragma_tok, if_p);
38581 return true;
38583 case PRAGMA_OACC_WAIT:
38584 if (context == pragma_stmt)
38586 error_at (pragma_tok->location,
38587 "%<#pragma %s%> may only be used in compound statements",
38588 "acc wait");
38589 break;
38591 else if (context != pragma_compound)
38592 goto bad_stmt;
38593 cp_parser_omp_construct (parser, pragma_tok, if_p);
38594 return true;
38596 case PRAGMA_OACC_ATOMIC:
38597 case PRAGMA_OACC_CACHE:
38598 case PRAGMA_OACC_DATA:
38599 case PRAGMA_OACC_HOST_DATA:
38600 case PRAGMA_OACC_KERNELS:
38601 case PRAGMA_OACC_PARALLEL:
38602 case PRAGMA_OACC_LOOP:
38603 case PRAGMA_OMP_ATOMIC:
38604 case PRAGMA_OMP_CRITICAL:
38605 case PRAGMA_OMP_DISTRIBUTE:
38606 case PRAGMA_OMP_FOR:
38607 case PRAGMA_OMP_MASTER:
38608 case PRAGMA_OMP_PARALLEL:
38609 case PRAGMA_OMP_SECTIONS:
38610 case PRAGMA_OMP_SIMD:
38611 case PRAGMA_OMP_SINGLE:
38612 case PRAGMA_OMP_TASK:
38613 case PRAGMA_OMP_TASKGROUP:
38614 case PRAGMA_OMP_TASKLOOP:
38615 case PRAGMA_OMP_TEAMS:
38616 if (context != pragma_stmt && context != pragma_compound)
38617 goto bad_stmt;
38618 stmt = push_omp_privatization_clauses (false);
38619 cp_parser_omp_construct (parser, pragma_tok, if_p);
38620 pop_omp_privatization_clauses (stmt);
38621 return true;
38623 case PRAGMA_OMP_ORDERED:
38624 if (context != pragma_stmt && context != pragma_compound)
38625 goto bad_stmt;
38626 stmt = push_omp_privatization_clauses (false);
38627 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38628 pop_omp_privatization_clauses (stmt);
38629 return ret;
38631 case PRAGMA_OMP_TARGET:
38632 if (context != pragma_stmt && context != pragma_compound)
38633 goto bad_stmt;
38634 stmt = push_omp_privatization_clauses (false);
38635 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38636 pop_omp_privatization_clauses (stmt);
38637 return ret;
38639 case PRAGMA_OMP_END_DECLARE_TARGET:
38640 cp_parser_omp_end_declare_target (parser, pragma_tok);
38641 return false;
38643 case PRAGMA_OMP_SECTION:
38644 error_at (pragma_tok->location,
38645 "%<#pragma omp section%> may only be used in "
38646 "%<#pragma omp sections%> construct");
38647 break;
38649 case PRAGMA_IVDEP:
38651 if (context == pragma_external)
38653 error_at (pragma_tok->location,
38654 "%<#pragma GCC ivdep%> must be inside a function");
38655 break;
38657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38658 cp_token *tok;
38659 tok = cp_lexer_peek_token (the_parser->lexer);
38660 if (tok->type != CPP_KEYWORD
38661 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38662 && tok->keyword != RID_DO))
38664 cp_parser_error (parser, "for, while or do statement expected");
38665 return false;
38667 cp_parser_iteration_statement (parser, if_p, true);
38668 return true;
38671 default:
38672 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38673 c_invoke_pragma_handler (id);
38674 break;
38676 bad_stmt:
38677 cp_parser_error (parser, "expected declaration specifiers");
38678 break;
38681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38682 return false;
38685 /* The interface the pragma parsers have to the lexer. */
38687 enum cpp_ttype
38688 pragma_lex (tree *value, location_t *loc)
38690 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38691 enum cpp_ttype ret = tok->type;
38693 *value = tok->u.value;
38694 if (loc)
38695 *loc = tok->location;
38697 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38698 ret = CPP_EOF;
38699 else if (ret == CPP_STRING)
38700 *value = cp_parser_string_literal (the_parser, false, false);
38701 else
38703 if (ret == CPP_KEYWORD)
38704 ret = CPP_NAME;
38705 cp_lexer_consume_token (the_parser->lexer);
38708 return ret;
38712 /* External interface. */
38714 /* Parse one entire translation unit. */
38716 void
38717 c_parse_file (void)
38719 static bool already_called = false;
38721 if (already_called)
38722 fatal_error (input_location,
38723 "inter-module optimizations not implemented for C++");
38724 already_called = true;
38726 the_parser = cp_parser_new ();
38727 push_deferring_access_checks (flag_access_control
38728 ? dk_no_deferred : dk_no_check);
38729 cp_parser_translation_unit (the_parser);
38730 the_parser = NULL;
38733 /* Create an identifier for a generic parameter type (a synthesized
38734 template parameter implied by `auto' or a concept identifier). */
38736 static GTY(()) int generic_parm_count;
38737 static tree
38738 make_generic_type_name ()
38740 char buf[32];
38741 sprintf (buf, "auto:%d", ++generic_parm_count);
38742 return get_identifier (buf);
38745 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38746 (creating a new template parameter list if necessary). Returns the newly
38747 created template type parm. */
38749 static tree
38750 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38752 gcc_assert (current_binding_level->kind == sk_function_parms);
38754 /* Before committing to modifying any scope, if we're in an
38755 implicit template scope, and we're trying to synthesize a
38756 constrained parameter, try to find a previous parameter with
38757 the same name. This is the same-type rule for abbreviated
38758 function templates.
38760 NOTE: We can generate implicit parameters when tentatively
38761 parsing a nested name specifier, only to reject that parse
38762 later. However, matching the same template-id as part of a
38763 direct-declarator should generate an identical template
38764 parameter, so this rule will merge them. */
38765 if (parser->implicit_template_scope && constr)
38767 tree t = parser->implicit_template_parms;
38768 while (t)
38770 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38772 tree d = TREE_VALUE (t);
38773 if (TREE_CODE (d) == PARM_DECL)
38774 /* Return the TEMPLATE_PARM_INDEX. */
38775 d = DECL_INITIAL (d);
38776 return d;
38778 t = TREE_CHAIN (t);
38782 /* We are either continuing a function template that already contains implicit
38783 template parameters, creating a new fully-implicit function template, or
38784 extending an existing explicit function template with implicit template
38785 parameters. */
38787 cp_binding_level *const entry_scope = current_binding_level;
38789 bool become_template = false;
38790 cp_binding_level *parent_scope = 0;
38792 if (parser->implicit_template_scope)
38794 gcc_assert (parser->implicit_template_parms);
38796 current_binding_level = parser->implicit_template_scope;
38798 else
38800 /* Roll back to the existing template parameter scope (in the case of
38801 extending an explicit function template) or introduce a new template
38802 parameter scope ahead of the function parameter scope (or class scope
38803 in the case of out-of-line member definitions). The function scope is
38804 added back after template parameter synthesis below. */
38806 cp_binding_level *scope = entry_scope;
38808 while (scope->kind == sk_function_parms)
38810 parent_scope = scope;
38811 scope = scope->level_chain;
38813 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38815 /* If not defining a class, then any class scope is a scope level in
38816 an out-of-line member definition. In this case simply wind back
38817 beyond the first such scope to inject the template parameter list.
38818 Otherwise wind back to the class being defined. The latter can
38819 occur in class member friend declarations such as:
38821 class A {
38822 void foo (auto);
38824 class B {
38825 friend void A::foo (auto);
38828 The template parameter list synthesized for the friend declaration
38829 must be injected in the scope of 'B'. This can also occur in
38830 erroneous cases such as:
38832 struct A {
38833 struct B {
38834 void foo (auto);
38836 void B::foo (auto) {}
38839 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38840 but, nevertheless, the template parameter list synthesized for the
38841 declarator should be injected into the scope of 'A' as if the
38842 ill-formed template was specified explicitly. */
38844 while (scope->kind == sk_class && !scope->defining_class_p)
38846 parent_scope = scope;
38847 scope = scope->level_chain;
38851 current_binding_level = scope;
38853 if (scope->kind != sk_template_parms
38854 || !function_being_declared_is_template_p (parser))
38856 /* Introduce a new template parameter list for implicit template
38857 parameters. */
38859 become_template = true;
38861 parser->implicit_template_scope
38862 = begin_scope (sk_template_parms, NULL);
38864 ++processing_template_decl;
38866 parser->fully_implicit_function_template_p = true;
38867 ++parser->num_template_parameter_lists;
38869 else
38871 /* Synthesize implicit template parameters at the end of the explicit
38872 template parameter list. */
38874 gcc_assert (current_template_parms);
38876 parser->implicit_template_scope = scope;
38878 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38879 parser->implicit_template_parms
38880 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38884 /* Synthesize a new template parameter and track the current template
38885 parameter chain with implicit_template_parms. */
38887 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38888 tree synth_id = make_generic_type_name ();
38889 tree synth_tmpl_parm;
38890 bool non_type = false;
38892 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38893 synth_tmpl_parm
38894 = finish_template_type_parm (class_type_node, synth_id);
38895 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38896 synth_tmpl_parm
38897 = finish_constrained_template_template_parm (proto, synth_id);
38898 else
38900 synth_tmpl_parm = copy_decl (proto);
38901 DECL_NAME (synth_tmpl_parm) = synth_id;
38902 non_type = true;
38905 // Attach the constraint to the parm before processing.
38906 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38907 TREE_TYPE (node) = constr;
38908 tree new_parm
38909 = process_template_parm (parser->implicit_template_parms,
38910 input_location,
38911 node,
38912 /*non_type=*/non_type,
38913 /*param_pack=*/false);
38915 // Chain the new parameter to the list of implicit parameters.
38916 if (parser->implicit_template_parms)
38917 parser->implicit_template_parms
38918 = TREE_CHAIN (parser->implicit_template_parms);
38919 else
38920 parser->implicit_template_parms = new_parm;
38922 tree new_decl = get_local_decls ();
38923 if (non_type)
38924 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38925 new_decl = DECL_INITIAL (new_decl);
38927 /* If creating a fully implicit function template, start the new implicit
38928 template parameter list with this synthesized type, otherwise grow the
38929 current template parameter list. */
38931 if (become_template)
38933 parent_scope->level_chain = current_binding_level;
38935 tree new_parms = make_tree_vec (1);
38936 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38937 current_template_parms = tree_cons (size_int (processing_template_decl),
38938 new_parms, current_template_parms);
38940 else
38942 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38943 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38944 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38945 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38948 // If the new parameter was constrained, we need to add that to the
38949 // constraints in the template parameter list.
38950 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38952 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38953 reqs = conjoin_constraints (reqs, req);
38954 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38957 current_binding_level = entry_scope;
38959 return new_decl;
38962 /* Finish the declaration of a fully implicit function template. Such a
38963 template has no explicit template parameter list so has not been through the
38964 normal template head and tail processing. synthesize_implicit_template_parm
38965 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38966 provided if the declaration is a class member such that its template
38967 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38968 form is returned. Otherwise NULL_TREE is returned. */
38970 static tree
38971 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38973 gcc_assert (parser->fully_implicit_function_template_p);
38975 if (member_decl_opt && member_decl_opt != error_mark_node
38976 && DECL_VIRTUAL_P (member_decl_opt))
38978 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38979 "implicit templates may not be %<virtual%>");
38980 DECL_VIRTUAL_P (member_decl_opt) = false;
38983 if (member_decl_opt)
38984 member_decl_opt = finish_member_template_decl (member_decl_opt);
38985 end_template_decl ();
38987 parser->fully_implicit_function_template_p = false;
38988 --parser->num_template_parameter_lists;
38990 return member_decl_opt;
38993 /* Helper function for diagnostics that have complained about things
38994 being used with 'extern "C"' linkage.
38996 Attempt to issue a note showing where the 'extern "C"' linkage began. */
38998 void
38999 maybe_show_extern_c_location (void)
39001 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39002 inform (the_parser->innermost_linkage_specification_location,
39003 "%<extern \"C\"%> linkage started here");
39006 #include "gt-cp-parser.h"