PR c++/84341
[official-gcc.git] / gcc / cp / parser.c
blob9a05e4fc812b4549c6a00e0ac0ad775a737634d7
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static bool cp_parser_extension_opt
2397 (cp_parser *, int *);
2398 static void cp_parser_label_declaration
2399 (cp_parser *);
2401 /* Concept Extensions */
2403 static tree cp_parser_requires_clause
2404 (cp_parser *);
2405 static tree cp_parser_requires_clause_opt
2406 (cp_parser *);
2407 static tree cp_parser_requires_expression
2408 (cp_parser *);
2409 static tree cp_parser_requirement_parameter_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement_body
2412 (cp_parser *);
2413 static tree cp_parser_requirement_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement
2416 (cp_parser *);
2417 static tree cp_parser_simple_requirement
2418 (cp_parser *);
2419 static tree cp_parser_compound_requirement
2420 (cp_parser *);
2421 static tree cp_parser_type_requirement
2422 (cp_parser *);
2423 static tree cp_parser_nested_requirement
2424 (cp_parser *);
2426 /* Transactional Memory Extensions */
2428 static tree cp_parser_transaction
2429 (cp_parser *, cp_token *);
2430 static tree cp_parser_transaction_expression
2431 (cp_parser *, enum rid);
2432 static void cp_parser_function_transaction
2433 (cp_parser *, enum rid);
2434 static tree cp_parser_transaction_cancel
2435 (cp_parser *);
2437 enum pragma_context {
2438 pragma_external,
2439 pragma_member,
2440 pragma_objc_icode,
2441 pragma_stmt,
2442 pragma_compound
2444 static bool cp_parser_pragma
2445 (cp_parser *, enum pragma_context, bool *);
2447 /* Objective-C++ Productions */
2449 static tree cp_parser_objc_message_receiver
2450 (cp_parser *);
2451 static tree cp_parser_objc_message_args
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_expression
2454 (cp_parser *);
2455 static cp_expr cp_parser_objc_encode_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_defs_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_protocol_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_selector_expression
2462 (cp_parser *);
2463 static cp_expr cp_parser_objc_expression
2464 (cp_parser *);
2465 static bool cp_parser_objc_selector_p
2466 (enum cpp_ttype);
2467 static tree cp_parser_objc_selector
2468 (cp_parser *);
2469 static tree cp_parser_objc_protocol_refs_opt
2470 (cp_parser *);
2471 static void cp_parser_objc_declaration
2472 (cp_parser *, tree);
2473 static tree cp_parser_objc_statement
2474 (cp_parser *);
2475 static bool cp_parser_objc_valid_prefix_attributes
2476 (cp_parser *, tree *);
2477 static void cp_parser_objc_at_property_declaration
2478 (cp_parser *) ;
2479 static void cp_parser_objc_at_synthesize_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_dynamic_declaration
2482 (cp_parser *) ;
2483 static tree cp_parser_objc_struct_declaration
2484 (cp_parser *) ;
2486 /* Utility Routines */
2488 static cp_expr cp_parser_lookup_name
2489 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2490 static tree cp_parser_lookup_name_simple
2491 (cp_parser *, tree, location_t);
2492 static tree cp_parser_maybe_treat_template_as_class
2493 (tree, bool);
2494 static bool cp_parser_check_declarator_template_parameters
2495 (cp_parser *, cp_declarator *, location_t);
2496 static bool cp_parser_check_template_parameters
2497 (cp_parser *, unsigned, location_t, cp_declarator *);
2498 static cp_expr cp_parser_simple_cast_expression
2499 (cp_parser *);
2500 static tree cp_parser_global_scope_opt
2501 (cp_parser *, bool);
2502 static bool cp_parser_constructor_declarator_p
2503 (cp_parser *, bool);
2504 static tree cp_parser_function_definition_from_specifiers_and_declarator
2505 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2506 static tree cp_parser_function_definition_after_declarator
2507 (cp_parser *, bool);
2508 static bool cp_parser_template_declaration_after_export
2509 (cp_parser *, bool);
2510 static void cp_parser_perform_template_parameter_access_checks
2511 (vec<deferred_access_check, va_gc> *);
2512 static tree cp_parser_single_declaration
2513 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2514 static cp_expr cp_parser_functional_cast
2515 (cp_parser *, tree);
2516 static tree cp_parser_save_member_function_body
2517 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2518 static tree cp_parser_save_nsdmi
2519 (cp_parser *);
2520 static tree cp_parser_enclosed_template_argument_list
2521 (cp_parser *);
2522 static void cp_parser_save_default_args
2523 (cp_parser *, tree);
2524 static void cp_parser_late_parsing_for_member
2525 (cp_parser *, tree);
2526 static tree cp_parser_late_parse_one_default_arg
2527 (cp_parser *, tree, tree, tree);
2528 static void cp_parser_late_parsing_nsdmi
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_default_args
2531 (cp_parser *, tree);
2532 static tree cp_parser_sizeof_operand
2533 (cp_parser *, enum rid);
2534 static cp_expr cp_parser_trait_expr
2535 (cp_parser *, enum rid);
2536 static bool cp_parser_declares_only_class_p
2537 (cp_parser *);
2538 static void cp_parser_set_storage_class
2539 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2540 static void cp_parser_set_decl_spec_type
2541 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2542 static void set_and_check_decl_spec_loc
2543 (cp_decl_specifier_seq *decl_specs,
2544 cp_decl_spec ds, cp_token *);
2545 static bool cp_parser_friend_p
2546 (const cp_decl_specifier_seq *);
2547 static void cp_parser_required_error
2548 (cp_parser *, required_token, bool, location_t);
2549 static cp_token *cp_parser_require
2550 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2551 static cp_token *cp_parser_require_keyword
2552 (cp_parser *, enum rid, required_token);
2553 static bool cp_parser_token_starts_function_definition_p
2554 (cp_token *);
2555 static bool cp_parser_next_token_starts_class_definition_p
2556 (cp_parser *);
2557 static bool cp_parser_next_token_ends_template_argument_p
2558 (cp_parser *);
2559 static bool cp_parser_nth_token_starts_template_argument_list_p
2560 (cp_parser *, size_t);
2561 static enum tag_types cp_parser_token_is_class_key
2562 (cp_token *);
2563 static enum tag_types cp_parser_token_is_type_parameter_key
2564 (cp_token *);
2565 static void cp_parser_check_class_key
2566 (enum tag_types, tree type);
2567 static void cp_parser_check_access_in_redeclaration
2568 (tree type, location_t location);
2569 static bool cp_parser_optional_template_keyword
2570 (cp_parser *);
2571 static void cp_parser_pre_parsed_nested_name_specifier
2572 (cp_parser *);
2573 static bool cp_parser_cache_group
2574 (cp_parser *, enum cpp_ttype, unsigned);
2575 static tree cp_parser_cache_defarg
2576 (cp_parser *parser, bool nsdmi);
2577 static void cp_parser_parse_tentatively
2578 (cp_parser *);
2579 static void cp_parser_commit_to_tentative_parse
2580 (cp_parser *);
2581 static void cp_parser_commit_to_topmost_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_abort_tentative_parse
2584 (cp_parser *);
2585 static bool cp_parser_parse_definitely
2586 (cp_parser *);
2587 static inline bool cp_parser_parsing_tentatively
2588 (cp_parser *);
2589 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 (cp_parser *);
2591 static void cp_parser_error
2592 (cp_parser *, const char *);
2593 static void cp_parser_name_lookup_error
2594 (cp_parser *, tree, tree, name_lookup_error, location_t);
2595 static bool cp_parser_simulate_error
2596 (cp_parser *);
2597 static bool cp_parser_check_type_definition
2598 (cp_parser *);
2599 static void cp_parser_check_for_definition_in_return_type
2600 (cp_declarator *, tree, location_t type_location);
2601 static void cp_parser_check_for_invalid_template_id
2602 (cp_parser *, tree, enum tag_types, location_t location);
2603 static bool cp_parser_non_integral_constant_expression
2604 (cp_parser *, non_integral_constant);
2605 static void cp_parser_diagnose_invalid_type_name
2606 (cp_parser *, tree, location_t);
2607 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 (cp_parser *);
2609 static int cp_parser_skip_to_closing_parenthesis
2610 (cp_parser *, bool, bool, bool);
2611 static void cp_parser_skip_to_end_of_statement
2612 (cp_parser *);
2613 static void cp_parser_consume_semicolon_at_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_block_or_statement
2616 (cp_parser *);
2617 static bool cp_parser_skip_to_closing_brace
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_template_parameter_list
2620 (cp_parser *);
2621 static void cp_parser_skip_to_pragma_eol
2622 (cp_parser*, cp_token *);
2623 static bool cp_parser_error_occurred
2624 (cp_parser *);
2625 static bool cp_parser_allow_gnu_extensions_p
2626 (cp_parser *);
2627 static bool cp_parser_is_pure_string_literal
2628 (cp_token *);
2629 static bool cp_parser_is_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_keyword
2632 (cp_token *, enum rid);
2633 static tree cp_parser_make_typename_type
2634 (cp_parser *, tree, location_t location);
2635 static cp_declarator * cp_parser_make_indirect_declarator
2636 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2637 static bool cp_parser_compound_literal_p
2638 (cp_parser *);
2639 static bool cp_parser_array_designator_p
2640 (cp_parser *);
2641 static bool cp_parser_init_statement_p
2642 (cp_parser *);
2643 static bool cp_parser_skip_to_closing_square_bracket
2644 (cp_parser *);
2646 /* Concept-related syntactic transformations */
2648 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2649 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2651 // -------------------------------------------------------------------------- //
2652 // Unevaluated Operand Guard
2654 // Implementation of an RAII helper for unevaluated operand parsing.
2655 cp_unevaluated::cp_unevaluated ()
2657 ++cp_unevaluated_operand;
2658 ++c_inhibit_evaluation_warnings;
2661 cp_unevaluated::~cp_unevaluated ()
2663 --c_inhibit_evaluation_warnings;
2664 --cp_unevaluated_operand;
2667 // -------------------------------------------------------------------------- //
2668 // Tentative Parsing
2670 /* Returns nonzero if we are parsing tentatively. */
2672 static inline bool
2673 cp_parser_parsing_tentatively (cp_parser* parser)
2675 return parser->context->next != NULL;
2678 /* Returns nonzero if TOKEN is a string literal. */
2680 static bool
2681 cp_parser_is_pure_string_literal (cp_token* token)
2683 return (token->type == CPP_STRING ||
2684 token->type == CPP_STRING16 ||
2685 token->type == CPP_STRING32 ||
2686 token->type == CPP_WSTRING ||
2687 token->type == CPP_UTF8STRING);
2690 /* Returns nonzero if TOKEN is a string literal
2691 of a user-defined string literal. */
2693 static bool
2694 cp_parser_is_string_literal (cp_token* token)
2696 return (cp_parser_is_pure_string_literal (token) ||
2697 token->type == CPP_STRING_USERDEF ||
2698 token->type == CPP_STRING16_USERDEF ||
2699 token->type == CPP_STRING32_USERDEF ||
2700 token->type == CPP_WSTRING_USERDEF ||
2701 token->type == CPP_UTF8STRING_USERDEF);
2704 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 static bool
2707 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2709 return token->keyword == keyword;
2712 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2713 PRAGMA_NONE. */
2715 static enum pragma_kind
2716 cp_parser_pragma_kind (cp_token *token)
2718 if (token->type != CPP_PRAGMA)
2719 return PRAGMA_NONE;
2720 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2721 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2724 /* Helper function for cp_parser_error.
2725 Having peeked a token of kind TOK1_KIND that might signify
2726 a conflict marker, peek successor tokens to determine
2727 if we actually do have a conflict marker.
2728 Specifically, we consider a run of 7 '<', '=' or '>' characters
2729 at the start of a line as a conflict marker.
2730 These come through the lexer as three pairs and a single,
2731 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2732 If it returns true, *OUT_LOC is written to with the location/range
2733 of the marker. */
2735 static bool
2736 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2737 location_t *out_loc)
2739 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2740 if (token2->type != tok1_kind)
2741 return false;
2742 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2743 if (token3->type != tok1_kind)
2744 return false;
2745 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2746 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2747 return false;
2749 /* It must be at the start of the line. */
2750 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2751 if (LOCATION_COLUMN (start_loc) != 1)
2752 return false;
2754 /* We have a conflict marker. Construct a location of the form:
2755 <<<<<<<
2756 ^~~~~~~
2757 with start == caret, finishing at the end of the marker. */
2758 location_t finish_loc = get_finish (token4->location);
2759 *out_loc = make_location (start_loc, start_loc, finish_loc);
2761 return true;
2764 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2765 RT_CLOSE_PAREN. */
2767 static const char *
2768 get_matching_symbol (required_token token_desc)
2770 switch (token_desc)
2772 default:
2773 gcc_unreachable ();
2774 return "";
2775 case RT_CLOSE_BRACE:
2776 return "{";
2777 case RT_CLOSE_PAREN:
2778 return "(";
2782 /* Attempt to convert TOKEN_DESC from a required_token to an
2783 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785 static enum cpp_ttype
2786 get_required_cpp_ttype (required_token token_desc)
2788 switch (token_desc)
2790 case RT_SEMICOLON:
2791 return CPP_SEMICOLON;
2792 case RT_OPEN_PAREN:
2793 return CPP_OPEN_PAREN;
2794 case RT_CLOSE_BRACE:
2795 return CPP_CLOSE_BRACE;
2796 case RT_OPEN_BRACE:
2797 return CPP_OPEN_BRACE;
2798 case RT_CLOSE_SQUARE:
2799 return CPP_CLOSE_SQUARE;
2800 case RT_OPEN_SQUARE:
2801 return CPP_OPEN_SQUARE;
2802 case RT_COMMA:
2803 return CPP_COMMA;
2804 case RT_COLON:
2805 return CPP_COLON;
2806 case RT_CLOSE_PAREN:
2807 return CPP_CLOSE_PAREN;
2809 default:
2810 /* Use CPP_EOF as a "no completions possible" code. */
2811 return CPP_EOF;
2816 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818 Issue a diagnostic of the form
2819 FILE:LINE: MESSAGE before TOKEN
2820 where TOKEN is the next token in the input stream. MESSAGE
2821 (specified by the caller) is usually of the form "expected
2822 OTHER-TOKEN".
2824 This bypasses the check for tentative passing, and potentially
2825 adds material needed by cp_parser_required_error.
2827 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2828 suggesting insertion of the missing token.
2830 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2831 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2832 location. */
2834 static void
2835 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2836 required_token missing_token_desc,
2837 location_t matching_location)
2839 cp_token *token = cp_lexer_peek_token (parser->lexer);
2840 /* This diagnostic makes more sense if it is tagged to the line
2841 of the token we just peeked at. */
2842 cp_lexer_set_source_position_from_token (token);
2844 if (token->type == CPP_PRAGMA)
2846 error_at (token->location,
2847 "%<#pragma%> is not allowed here");
2848 cp_parser_skip_to_pragma_eol (parser, token);
2849 return;
2852 /* If this is actually a conflict marker, report it as such. */
2853 if (token->type == CPP_LSHIFT
2854 || token->type == CPP_RSHIFT
2855 || token->type == CPP_EQ_EQ)
2857 location_t loc;
2858 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2860 error_at (loc, "version control conflict marker in file");
2861 return;
2865 gcc_rich_location richloc (input_location);
2867 bool added_matching_location = false;
2869 if (missing_token_desc != RT_NONE)
2871 /* Potentially supply a fix-it hint, suggesting to add the
2872 missing token immediately after the *previous* token.
2873 This may move the primary location within richloc. */
2874 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2875 location_t prev_token_loc
2876 = cp_lexer_previous_token (parser->lexer)->location;
2877 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2879 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2880 Attempt to consolidate diagnostics by printing it as a
2881 secondary range within the main diagnostic. */
2882 if (matching_location != UNKNOWN_LOCATION)
2883 added_matching_location
2884 = richloc.add_location_if_nearby (matching_location);
2887 /* Actually emit the error. */
2888 c_parse_error (gmsgid,
2889 /* Because c_parser_error does not understand
2890 CPP_KEYWORD, keywords are treated like
2891 identifiers. */
2892 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2893 token->u.value, token->flags, &richloc);
2895 if (missing_token_desc != RT_NONE)
2897 /* If we weren't able to consolidate matching_location, then
2898 print it as a secondary diagnostic. */
2899 if (matching_location != UNKNOWN_LOCATION
2900 && !added_matching_location)
2901 inform (matching_location, "to match this %qs",
2902 get_matching_symbol (missing_token_desc));
2906 /* If not parsing tentatively, issue a diagnostic of the form
2907 FILE:LINE: MESSAGE before TOKEN
2908 where TOKEN is the next token in the input stream. MESSAGE
2909 (specified by the caller) is usually of the form "expected
2910 OTHER-TOKEN". */
2912 static void
2913 cp_parser_error (cp_parser* parser, const char* gmsgid)
2915 if (!cp_parser_simulate_error (parser))
2916 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2919 /* Issue an error about name-lookup failing. NAME is the
2920 IDENTIFIER_NODE DECL is the result of
2921 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2922 the thing that we hoped to find. */
2924 static void
2925 cp_parser_name_lookup_error (cp_parser* parser,
2926 tree name,
2927 tree decl,
2928 name_lookup_error desired,
2929 location_t location)
2931 /* If name lookup completely failed, tell the user that NAME was not
2932 declared. */
2933 if (decl == error_mark_node)
2935 if (parser->scope && parser->scope != global_namespace)
2936 error_at (location, "%<%E::%E%> has not been declared",
2937 parser->scope, name);
2938 else if (parser->scope == global_namespace)
2939 error_at (location, "%<::%E%> has not been declared", name);
2940 else if (parser->object_scope
2941 && !CLASS_TYPE_P (parser->object_scope))
2942 error_at (location, "request for member %qE in non-class type %qT",
2943 name, parser->object_scope);
2944 else if (parser->object_scope)
2945 error_at (location, "%<%T::%E%> has not been declared",
2946 parser->object_scope, name);
2947 else
2948 error_at (location, "%qE has not been declared", name);
2950 else if (parser->scope && parser->scope != global_namespace)
2952 switch (desired)
2954 case NLE_TYPE:
2955 error_at (location, "%<%E::%E%> is not a type",
2956 parser->scope, name);
2957 break;
2958 case NLE_CXX98:
2959 error_at (location, "%<%E::%E%> is not a class or namespace",
2960 parser->scope, name);
2961 break;
2962 case NLE_NOT_CXX98:
2963 error_at (location,
2964 "%<%E::%E%> is not a class, namespace, or enumeration",
2965 parser->scope, name);
2966 break;
2967 default:
2968 gcc_unreachable ();
2972 else if (parser->scope == global_namespace)
2974 switch (desired)
2976 case NLE_TYPE:
2977 error_at (location, "%<::%E%> is not a type", name);
2978 break;
2979 case NLE_CXX98:
2980 error_at (location, "%<::%E%> is not a class or namespace", name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<::%E%> is not a class, namespace, or enumeration",
2985 name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2991 else
2993 switch (desired)
2995 case NLE_TYPE:
2996 error_at (location, "%qE is not a type", name);
2997 break;
2998 case NLE_CXX98:
2999 error_at (location, "%qE is not a class or namespace", name);
3000 break;
3001 case NLE_NOT_CXX98:
3002 error_at (location,
3003 "%qE is not a class, namespace, or enumeration", name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3011 /* If we are parsing tentatively, remember that an error has occurred
3012 during this tentative parse. Returns true if the error was
3013 simulated; false if a message should be issued by the caller. */
3015 static bool
3016 cp_parser_simulate_error (cp_parser* parser)
3018 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3020 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3021 return true;
3023 return false;
3026 /* This function is called when a type is defined. If type
3027 definitions are forbidden at this point, an error message is
3028 issued. */
3030 static bool
3031 cp_parser_check_type_definition (cp_parser* parser)
3033 /* If types are forbidden here, issue a message. */
3034 if (parser->type_definition_forbidden_message)
3036 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3037 in the message need to be interpreted. */
3038 error (parser->type_definition_forbidden_message);
3039 return false;
3041 return true;
3044 /* This function is called when the DECLARATOR is processed. The TYPE
3045 was a type defined in the decl-specifiers. If it is invalid to
3046 define a type in the decl-specifiers for DECLARATOR, an error is
3047 issued. TYPE_LOCATION is the location of TYPE and is used
3048 for error reporting. */
3050 static void
3051 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3052 tree type, location_t type_location)
3054 /* [dcl.fct] forbids type definitions in return types.
3055 Unfortunately, it's not easy to know whether or not we are
3056 processing a return type until after the fact. */
3057 while (declarator
3058 && (declarator->kind == cdk_pointer
3059 || declarator->kind == cdk_reference
3060 || declarator->kind == cdk_ptrmem))
3061 declarator = declarator->declarator;
3062 if (declarator
3063 && declarator->kind == cdk_function)
3065 error_at (type_location,
3066 "new types may not be defined in a return type");
3067 inform (type_location,
3068 "(perhaps a semicolon is missing after the definition of %qT)",
3069 type);
3073 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3074 "<" in any valid C++ program. If the next token is indeed "<",
3075 issue a message warning the user about what appears to be an
3076 invalid attempt to form a template-id. LOCATION is the location
3077 of the type-specifier (TYPE) */
3079 static void
3080 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3081 tree type,
3082 enum tag_types tag_type,
3083 location_t location)
3085 cp_token_position start = 0;
3087 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3089 if (TREE_CODE (type) == TYPE_DECL)
3090 type = TREE_TYPE (type);
3091 if (TYPE_P (type) && !template_placeholder_p (type))
3092 error_at (location, "%qT is not a template", type);
3093 else if (identifier_p (type))
3095 if (tag_type != none_type)
3096 error_at (location, "%qE is not a class template", type);
3097 else
3098 error_at (location, "%qE is not a template", type);
3100 else
3101 error_at (location, "invalid template-id");
3102 /* Remember the location of the invalid "<". */
3103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3104 start = cp_lexer_token_position (parser->lexer, true);
3105 /* Consume the "<". */
3106 cp_lexer_consume_token (parser->lexer);
3107 /* Parse the template arguments. */
3108 cp_parser_enclosed_template_argument_list (parser);
3109 /* Permanently remove the invalid template arguments so that
3110 this error message is not issued again. */
3111 if (start)
3112 cp_lexer_purge_tokens_after (parser->lexer, start);
3116 /* If parsing an integral constant-expression, issue an error message
3117 about the fact that THING appeared and return true. Otherwise,
3118 return false. In either case, set
3119 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3121 static bool
3122 cp_parser_non_integral_constant_expression (cp_parser *parser,
3123 non_integral_constant thing)
3125 parser->non_integral_constant_expression_p = true;
3126 if (parser->integral_constant_expression_p)
3128 if (!parser->allow_non_integral_constant_expression_p)
3130 const char *msg = NULL;
3131 switch (thing)
3133 case NIC_FLOAT:
3134 pedwarn (input_location, OPT_Wpedantic,
3135 "ISO C++ forbids using a floating-point literal "
3136 "in a constant-expression");
3137 return true;
3138 case NIC_CAST:
3139 error ("a cast to a type other than an integral or "
3140 "enumeration type cannot appear in a "
3141 "constant-expression");
3142 return true;
3143 case NIC_TYPEID:
3144 error ("%<typeid%> operator "
3145 "cannot appear in a constant-expression");
3146 return true;
3147 case NIC_NCC:
3148 error ("non-constant compound literals "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_FUNC_CALL:
3152 error ("a function call "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_INC:
3156 error ("an increment "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_DEC:
3160 error ("an decrement "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_ARRAY_REF:
3164 error ("an array reference "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ADDR_LABEL:
3168 error ("the address of a label "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_OVERLOADED:
3172 error ("calls to overloaded operators "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_ASSIGNMENT:
3176 error ("an assignment cannot appear in a constant-expression");
3177 return true;
3178 case NIC_COMMA:
3179 error ("a comma operator "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_CONSTRUCTOR:
3183 error ("a call to a constructor "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_TRANSACTION:
3187 error ("a transaction expression "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_THIS:
3191 msg = "this";
3192 break;
3193 case NIC_FUNC_NAME:
3194 msg = "__FUNCTION__";
3195 break;
3196 case NIC_PRETTY_FUNC:
3197 msg = "__PRETTY_FUNCTION__";
3198 break;
3199 case NIC_C99_FUNC:
3200 msg = "__func__";
3201 break;
3202 case NIC_VA_ARG:
3203 msg = "va_arg";
3204 break;
3205 case NIC_ARROW:
3206 msg = "->";
3207 break;
3208 case NIC_POINT:
3209 msg = ".";
3210 break;
3211 case NIC_STAR:
3212 msg = "*";
3213 break;
3214 case NIC_ADDR:
3215 msg = "&";
3216 break;
3217 case NIC_PREINCREMENT:
3218 msg = "++";
3219 break;
3220 case NIC_PREDECREMENT:
3221 msg = "--";
3222 break;
3223 case NIC_NEW:
3224 msg = "new";
3225 break;
3226 case NIC_DEL:
3227 msg = "delete";
3228 break;
3229 default:
3230 gcc_unreachable ();
3232 if (msg)
3233 error ("%qs cannot appear in a constant-expression", msg);
3234 return true;
3237 return false;
3240 /* Emit a diagnostic for an invalid type name. This function commits
3241 to the current active tentative parse, if any. (Otherwise, the
3242 problematic construct might be encountered again later, resulting
3243 in duplicate error messages.) LOCATION is the location of ID. */
3245 static void
3246 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3247 location_t location)
3249 tree decl, ambiguous_decls;
3250 cp_parser_commit_to_tentative_parse (parser);
3251 /* Try to lookup the identifier. */
3252 decl = cp_parser_lookup_name (parser, id, none_type,
3253 /*is_template=*/false,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3256 &ambiguous_decls, location);
3257 if (ambiguous_decls)
3258 /* If the lookup was ambiguous, an error will already have
3259 been issued. */
3260 return;
3261 /* If the lookup found a template-name, it means that the user forgot
3262 to specify an argument list. Emit a useful error message. */
3263 if (DECL_TYPE_TEMPLATE_P (decl))
3265 error_at (location,
3266 "invalid use of template-name %qE without an argument list",
3267 decl);
3268 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3269 inform (location, "class template argument deduction is only available "
3270 "with -std=c++17 or -std=gnu++17");
3271 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3273 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3274 error_at (location, "invalid use of destructor %qD as a type", id);
3275 else if (TREE_CODE (decl) == TYPE_DECL)
3276 /* Something like 'unsigned A a;' */
3277 error_at (location, "invalid combination of multiple type-specifiers");
3278 else if (!parser->scope)
3280 /* Issue an error message. */
3281 name_hint hint;
3282 if (TREE_CODE (id) == IDENTIFIER_NODE)
3283 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3284 if (hint)
3286 gcc_rich_location richloc (location);
3287 richloc.add_fixit_replace (hint.suggestion ());
3288 error_at (&richloc,
3289 "%qE does not name a type; did you mean %qs?",
3290 id, hint.suggestion ());
3292 else
3293 error_at (location, "%qE does not name a type", id);
3294 /* If we're in a template class, it's possible that the user was
3295 referring to a type from a base class. For example:
3297 template <typename T> struct A { typedef T X; };
3298 template <typename T> struct B : public A<T> { X x; };
3300 The user should have said "typename A<T>::X". */
3301 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3302 inform (location, "C++11 %<constexpr%> only available with "
3303 "-std=c++11 or -std=gnu++11");
3304 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3305 inform (location, "C++11 %<noexcept%> only available with "
3306 "-std=c++11 or -std=gnu++11");
3307 else if (cxx_dialect < cxx11
3308 && TREE_CODE (id) == IDENTIFIER_NODE
3309 && id_equal (id, "thread_local"))
3310 inform (location, "C++11 %<thread_local%> only available with "
3311 "-std=c++11 or -std=gnu++11");
3312 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3313 inform (location, "%<concept%> only available with -fconcepts");
3314 else if (processing_template_decl && current_class_type
3315 && TYPE_BINFO (current_class_type))
3317 tree b;
3319 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3321 b = TREE_CHAIN (b))
3323 tree base_type = BINFO_TYPE (b);
3324 if (CLASS_TYPE_P (base_type)
3325 && dependent_type_p (base_type))
3327 tree field;
3328 /* Go from a particular instantiation of the
3329 template (which will have an empty TYPE_FIELDs),
3330 to the main version. */
3331 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3332 for (field = TYPE_FIELDS (base_type);
3333 field;
3334 field = DECL_CHAIN (field))
3335 if (TREE_CODE (field) == TYPE_DECL
3336 && DECL_NAME (field) == id)
3338 inform (location,
3339 "(perhaps %<typename %T::%E%> was intended)",
3340 BINFO_TYPE (b), id);
3341 break;
3343 if (field)
3344 break;
3349 /* Here we diagnose qualified-ids where the scope is actually correct,
3350 but the identifier does not resolve to a valid type name. */
3351 else if (parser->scope != error_mark_node)
3353 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3355 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3356 error_at (location_of (id),
3357 "%qE in namespace %qE does not name a template type",
3358 id, parser->scope);
3359 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 TREE_OPERAND (id, 0), parser->scope);
3363 else
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a type",
3366 id, parser->scope);
3367 if (DECL_P (decl))
3368 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3369 else if (decl == error_mark_node)
3370 suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3373 else if (CLASS_TYPE_P (parser->scope)
3374 && constructor_name_p (id, parser->scope))
3376 /* A<T>::A<T>() */
3377 error_at (location, "%<%T::%E%> names the constructor, not"
3378 " the type", parser->scope, id);
3379 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3380 error_at (location, "and %qT has no template constructors",
3381 parser->scope);
3383 else if (TYPE_P (parser->scope)
3384 && dependent_scope_p (parser->scope))
3386 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3387 error_at (location,
3388 "need %<typename%> before %<%T::%D::%E%> because "
3389 "%<%T::%D%> is a dependent scope",
3390 TYPE_CONTEXT (parser->scope),
3391 TYPENAME_TYPE_FULLNAME (parser->scope),
3393 TYPE_CONTEXT (parser->scope),
3394 TYPENAME_TYPE_FULLNAME (parser->scope));
3395 else
3396 error_at (location, "need %<typename%> before %<%T::%E%> because "
3397 "%qT is a dependent scope",
3398 parser->scope, id, parser->scope);
3400 else if (TYPE_P (parser->scope))
3402 if (!COMPLETE_TYPE_P (parser->scope))
3403 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3404 parser->scope);
3405 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3406 error_at (location_of (id),
3407 "%qE in %q#T does not name a template type",
3408 id, parser->scope);
3409 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 TREE_OPERAND (id, 0), parser->scope);
3413 else
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a type",
3416 id, parser->scope);
3417 if (DECL_P (decl))
3418 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3420 else
3421 gcc_unreachable ();
3425 /* Check for a common situation where a type-name should be present,
3426 but is not, and issue a sensible error message. Returns true if an
3427 invalid type-name was detected.
3429 The situation handled by this function are variable declarations of the
3430 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3431 Usually, `ID' should name a type, but if we got here it means that it
3432 does not. We try to emit the best possible error message depending on
3433 how exactly the id-expression looks like. */
3435 static bool
3436 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3438 tree id;
3439 cp_token *token = cp_lexer_peek_token (parser->lexer);
3441 /* Avoid duplicate error about ambiguous lookup. */
3442 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3444 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3445 if (next->type == CPP_NAME && next->error_reported)
3446 goto out;
3449 cp_parser_parse_tentatively (parser);
3450 id = cp_parser_id_expression (parser,
3451 /*template_keyword_p=*/false,
3452 /*check_dependency_p=*/true,
3453 /*template_p=*/NULL,
3454 /*declarator_p=*/true,
3455 /*optional_p=*/false);
3456 /* If the next token is a (, this is a function with no explicit return
3457 type, i.e. constructor, destructor or conversion op. */
3458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3459 || TREE_CODE (id) == TYPE_DECL)
3461 cp_parser_abort_tentative_parse (parser);
3462 return false;
3464 if (!cp_parser_parse_definitely (parser))
3465 return false;
3467 /* Emit a diagnostic for the invalid type. */
3468 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3469 out:
3470 /* If we aren't in the middle of a declarator (i.e. in a
3471 parameter-declaration-clause), skip to the end of the declaration;
3472 there's no point in trying to process it. */
3473 if (!parser->in_declarator_p)
3474 cp_parser_skip_to_end_of_block_or_statement (parser);
3475 return true;
3478 /* Consume tokens up to, and including, the next non-nested closing `)'.
3479 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3480 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3481 found an unnested token of that type. */
3483 static int
3484 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3485 bool recovering,
3486 cpp_ttype or_ttype,
3487 bool consume_paren)
3489 unsigned paren_depth = 0;
3490 unsigned brace_depth = 0;
3491 unsigned square_depth = 0;
3493 if (recovering && or_ttype == CPP_EOF
3494 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3495 return 0;
3497 while (true)
3499 cp_token * token = cp_lexer_peek_token (parser->lexer);
3501 /* Have we found what we're looking for before the closing paren? */
3502 if (token->type == or_ttype && or_ttype != CPP_EOF
3503 && !brace_depth && !paren_depth && !square_depth)
3504 return -1;
3506 switch (token->type)
3508 case CPP_EOF:
3509 case CPP_PRAGMA_EOL:
3510 /* If we've run out of tokens, then there is no closing `)'. */
3511 return 0;
3513 /* This is good for lambda expression capture-lists. */
3514 case CPP_OPEN_SQUARE:
3515 ++square_depth;
3516 break;
3517 case CPP_CLOSE_SQUARE:
3518 if (!square_depth--)
3519 return 0;
3520 break;
3522 case CPP_SEMICOLON:
3523 /* This matches the processing in skip_to_end_of_statement. */
3524 if (!brace_depth)
3525 return 0;
3526 break;
3528 case CPP_OPEN_BRACE:
3529 ++brace_depth;
3530 break;
3531 case CPP_CLOSE_BRACE:
3532 if (!brace_depth--)
3533 return 0;
3534 break;
3536 case CPP_OPEN_PAREN:
3537 if (!brace_depth)
3538 ++paren_depth;
3539 break;
3541 case CPP_CLOSE_PAREN:
3542 if (!brace_depth && !paren_depth--)
3544 if (consume_paren)
3545 cp_lexer_consume_token (parser->lexer);
3546 return 1;
3548 break;
3550 default:
3551 break;
3554 /* Consume the token. */
3555 cp_lexer_consume_token (parser->lexer);
3559 /* Consume tokens up to, and including, the next non-nested closing `)'.
3560 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3561 are doing error recovery. Returns -1 if OR_COMMA is true and we
3562 found an unnested token of that type. */
3564 static int
3565 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3566 bool recovering,
3567 bool or_comma,
3568 bool consume_paren)
3570 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3571 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3572 ttype, consume_paren);
3575 /* Consume tokens until we reach the end of the current statement.
3576 Normally, that will be just before consuming a `;'. However, if a
3577 non-nested `}' comes first, then we stop before consuming that. */
3579 static void
3580 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3582 unsigned nesting_depth = 0;
3584 /* Unwind generic function template scope if necessary. */
3585 if (parser->fully_implicit_function_template_p)
3586 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3588 while (true)
3590 cp_token *token = cp_lexer_peek_token (parser->lexer);
3592 switch (token->type)
3594 case CPP_EOF:
3595 case CPP_PRAGMA_EOL:
3596 /* If we've run out of tokens, stop. */
3597 return;
3599 case CPP_SEMICOLON:
3600 /* If the next token is a `;', we have reached the end of the
3601 statement. */
3602 if (!nesting_depth)
3603 return;
3604 break;
3606 case CPP_CLOSE_BRACE:
3607 /* If this is a non-nested '}', stop before consuming it.
3608 That way, when confronted with something like:
3610 { 3 + }
3612 we stop before consuming the closing '}', even though we
3613 have not yet reached a `;'. */
3614 if (nesting_depth == 0)
3615 return;
3617 /* If it is the closing '}' for a block that we have
3618 scanned, stop -- but only after consuming the token.
3619 That way given:
3621 void f g () { ... }
3622 typedef int I;
3624 we will stop after the body of the erroneously declared
3625 function, but before consuming the following `typedef'
3626 declaration. */
3627 if (--nesting_depth == 0)
3629 cp_lexer_consume_token (parser->lexer);
3630 return;
3632 break;
3634 case CPP_OPEN_BRACE:
3635 ++nesting_depth;
3636 break;
3638 default:
3639 break;
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser->lexer);
3647 /* This function is called at the end of a statement or declaration.
3648 If the next token is a semicolon, it is consumed; otherwise, error
3649 recovery is attempted. */
3651 static void
3652 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3654 /* Look for the trailing `;'. */
3655 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3657 /* If there is additional (erroneous) input, skip to the end of
3658 the statement. */
3659 cp_parser_skip_to_end_of_statement (parser);
3660 /* If the next token is now a `;', consume it. */
3661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3662 cp_lexer_consume_token (parser->lexer);
3666 /* Skip tokens until we have consumed an entire block, or until we
3667 have consumed a non-nested `;'. */
3669 static void
3670 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3672 int nesting_depth = 0;
3674 /* Unwind generic function template scope if necessary. */
3675 if (parser->fully_implicit_function_template_p)
3676 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3678 while (nesting_depth >= 0)
3680 cp_token *token = cp_lexer_peek_token (parser->lexer);
3682 switch (token->type)
3684 case CPP_EOF:
3685 case CPP_PRAGMA_EOL:
3686 /* If we've run out of tokens, stop. */
3687 return;
3689 case CPP_SEMICOLON:
3690 /* Stop if this is an unnested ';'. */
3691 if (!nesting_depth)
3692 nesting_depth = -1;
3693 break;
3695 case CPP_CLOSE_BRACE:
3696 /* Stop if this is an unnested '}', or closes the outermost
3697 nesting level. */
3698 nesting_depth--;
3699 if (nesting_depth < 0)
3700 return;
3701 if (!nesting_depth)
3702 nesting_depth = -1;
3703 break;
3705 case CPP_OPEN_BRACE:
3706 /* Nest. */
3707 nesting_depth++;
3708 break;
3710 default:
3711 break;
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser->lexer);
3719 /* Skip tokens until a non-nested closing curly brace is the next
3720 token, or there are no more tokens. Return true in the first case,
3721 false otherwise. */
3723 static bool
3724 cp_parser_skip_to_closing_brace (cp_parser *parser)
3726 unsigned nesting_depth = 0;
3728 while (true)
3730 cp_token *token = cp_lexer_peek_token (parser->lexer);
3732 switch (token->type)
3734 case CPP_EOF:
3735 case CPP_PRAGMA_EOL:
3736 /* If we've run out of tokens, stop. */
3737 return false;
3739 case CPP_CLOSE_BRACE:
3740 /* If the next token is a non-nested `}', then we have reached
3741 the end of the current block. */
3742 if (nesting_depth-- == 0)
3743 return true;
3744 break;
3746 case CPP_OPEN_BRACE:
3747 /* If it the next token is a `{', then we are entering a new
3748 block. Consume the entire block. */
3749 ++nesting_depth;
3750 break;
3752 default:
3753 break;
3756 /* Consume the token. */
3757 cp_lexer_consume_token (parser->lexer);
3761 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3762 parameter is the PRAGMA token, allowing us to purge the entire pragma
3763 sequence. */
3765 static void
3766 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3768 cp_token *token;
3770 parser->lexer->in_pragma = false;
3773 token = cp_lexer_consume_token (parser->lexer);
3774 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3776 /* Ensure that the pragma is not parsed again. */
3777 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3780 /* Require pragma end of line, resyncing with it as necessary. The
3781 arguments are as for cp_parser_skip_to_pragma_eol. */
3783 static void
3784 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3786 parser->lexer->in_pragma = false;
3787 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3791 /* This is a simple wrapper around make_typename_type. When the id is
3792 an unresolved identifier node, we can provide a superior diagnostic
3793 using cp_parser_diagnose_invalid_type_name. */
3795 static tree
3796 cp_parser_make_typename_type (cp_parser *parser, tree id,
3797 location_t id_location)
3799 tree result;
3800 if (identifier_p (id))
3802 result = make_typename_type (parser->scope, id, typename_type,
3803 /*complain=*/tf_none);
3804 if (result == error_mark_node)
3805 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3806 return result;
3808 return make_typename_type (parser->scope, id, typename_type, tf_error);
3811 /* This is a wrapper around the
3812 make_{pointer,ptrmem,reference}_declarator functions that decides
3813 which one to call based on the CODE and CLASS_TYPE arguments. The
3814 CODE argument should be one of the values returned by
3815 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3816 appertain to the pointer or reference. */
3818 static cp_declarator *
3819 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3820 cp_cv_quals cv_qualifiers,
3821 cp_declarator *target,
3822 tree attributes)
3824 if (code == ERROR_MARK)
3825 return cp_error_declarator;
3827 if (code == INDIRECT_REF)
3828 if (class_type == NULL_TREE)
3829 return make_pointer_declarator (cv_qualifiers, target, attributes);
3830 else
3831 return make_ptrmem_declarator (cv_qualifiers, class_type,
3832 target, attributes);
3833 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3834 return make_reference_declarator (cv_qualifiers, target,
3835 false, attributes);
3836 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3837 return make_reference_declarator (cv_qualifiers, target,
3838 true, attributes);
3839 gcc_unreachable ();
3842 /* Create a new C++ parser. */
3844 static cp_parser *
3845 cp_parser_new (void)
3847 cp_parser *parser;
3848 cp_lexer *lexer;
3849 unsigned i;
3851 /* cp_lexer_new_main is called before doing GC allocation because
3852 cp_lexer_new_main might load a PCH file. */
3853 lexer = cp_lexer_new_main ();
3855 /* Initialize the binops_by_token so that we can get the tree
3856 directly from the token. */
3857 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3858 binops_by_token[binops[i].token_type] = binops[i];
3860 parser = ggc_cleared_alloc<cp_parser> ();
3861 parser->lexer = lexer;
3862 parser->context = cp_parser_context_new (NULL);
3864 /* For now, we always accept GNU extensions. */
3865 parser->allow_gnu_extensions_p = 1;
3867 /* The `>' token is a greater-than operator, not the end of a
3868 template-id. */
3869 parser->greater_than_is_operator_p = true;
3871 parser->default_arg_ok_p = true;
3873 /* We are not parsing a constant-expression. */
3874 parser->integral_constant_expression_p = false;
3875 parser->allow_non_integral_constant_expression_p = false;
3876 parser->non_integral_constant_expression_p = false;
3878 /* Local variable names are not forbidden. */
3879 parser->local_variables_forbidden_p = false;
3881 /* We are not processing an `extern "C"' declaration. */
3882 parser->in_unbraced_linkage_specification_p = false;
3884 /* We are not processing a declarator. */
3885 parser->in_declarator_p = false;
3887 /* We are not processing a template-argument-list. */
3888 parser->in_template_argument_list_p = false;
3890 /* We are not in an iteration statement. */
3891 parser->in_statement = 0;
3893 /* We are not in a switch statement. */
3894 parser->in_switch_statement_p = false;
3896 /* We are not parsing a type-id inside an expression. */
3897 parser->in_type_id_in_expr_p = false;
3899 /* Declarations aren't implicitly extern "C". */
3900 parser->implicit_extern_c = false;
3902 /* String literals should be translated to the execution character set. */
3903 parser->translate_strings_p = true;
3905 /* We are not parsing a function body. */
3906 parser->in_function_body = false;
3908 /* We can correct until told otherwise. */
3909 parser->colon_corrects_to_scope_p = true;
3911 /* The unparsed function queue is empty. */
3912 push_unparsed_function_queues (parser);
3914 /* There are no classes being defined. */
3915 parser->num_classes_being_defined = 0;
3917 /* No template parameters apply. */
3918 parser->num_template_parameter_lists = 0;
3920 /* Special parsing data structures. */
3921 parser->omp_declare_simd = NULL;
3922 parser->oacc_routine = NULL;
3924 /* Not declaring an implicit function template. */
3925 parser->auto_is_implicit_function_template_parm_p = false;
3926 parser->fully_implicit_function_template_p = false;
3927 parser->implicit_template_parms = 0;
3928 parser->implicit_template_scope = 0;
3930 /* Allow constrained-type-specifiers. */
3931 parser->prevent_constrained_type_specifiers = 0;
3933 /* We haven't yet seen an 'extern "C"'. */
3934 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3936 return parser;
3939 /* Create a cp_lexer structure which will emit the tokens in CACHE
3940 and push it onto the parser's lexer stack. This is used for delayed
3941 parsing of in-class method bodies and default arguments, and should
3942 not be confused with tentative parsing. */
3943 static void
3944 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3946 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3947 lexer->next = parser->lexer;
3948 parser->lexer = lexer;
3950 /* Move the current source position to that of the first token in the
3951 new lexer. */
3952 cp_lexer_set_source_position_from_token (lexer->next_token);
3955 /* Pop the top lexer off the parser stack. This is never used for the
3956 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3957 static void
3958 cp_parser_pop_lexer (cp_parser *parser)
3960 cp_lexer *lexer = parser->lexer;
3961 parser->lexer = lexer->next;
3962 cp_lexer_destroy (lexer);
3964 /* Put the current source position back where it was before this
3965 lexer was pushed. */
3966 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3969 /* Lexical conventions [gram.lex] */
3971 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3972 identifier. */
3974 static cp_expr
3975 cp_parser_identifier (cp_parser* parser)
3977 cp_token *token;
3979 /* Look for the identifier. */
3980 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3981 /* Return the value. */
3982 if (token)
3983 return cp_expr (token->u.value, token->location);
3984 else
3985 return error_mark_node;
3988 /* Parse a sequence of adjacent string constants. Returns a
3989 TREE_STRING representing the combined, nul-terminated string
3990 constant. If TRANSLATE is true, translate the string to the
3991 execution character set. If WIDE_OK is true, a wide string is
3992 invalid here.
3994 C++98 [lex.string] says that if a narrow string literal token is
3995 adjacent to a wide string literal token, the behavior is undefined.
3996 However, C99 6.4.5p4 says that this results in a wide string literal.
3997 We follow C99 here, for consistency with the C front end.
3999 This code is largely lifted from lex_string() in c-lex.c.
4001 FUTURE: ObjC++ will need to handle @-strings here. */
4002 static cp_expr
4003 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4004 bool lookup_udlit = true)
4006 tree value;
4007 size_t count;
4008 struct obstack str_ob;
4009 cpp_string str, istr, *strs;
4010 cp_token *tok;
4011 enum cpp_ttype type, curr_type;
4012 int have_suffix_p = 0;
4013 tree string_tree;
4014 tree suffix_id = NULL_TREE;
4015 bool curr_tok_is_userdef_p = false;
4017 tok = cp_lexer_peek_token (parser->lexer);
4018 if (!cp_parser_is_string_literal (tok))
4020 cp_parser_error (parser, "expected string-literal");
4021 return error_mark_node;
4024 location_t loc = tok->location;
4026 if (cpp_userdef_string_p (tok->type))
4028 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4029 curr_type = cpp_userdef_string_remove_type (tok->type);
4030 curr_tok_is_userdef_p = true;
4032 else
4034 string_tree = tok->u.value;
4035 curr_type = tok->type;
4037 type = curr_type;
4039 /* Try to avoid the overhead of creating and destroying an obstack
4040 for the common case of just one string. */
4041 if (!cp_parser_is_string_literal
4042 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4044 cp_lexer_consume_token (parser->lexer);
4046 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4047 str.len = TREE_STRING_LENGTH (string_tree);
4048 count = 1;
4050 if (curr_tok_is_userdef_p)
4052 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4053 have_suffix_p = 1;
4054 curr_type = cpp_userdef_string_remove_type (tok->type);
4056 else
4057 curr_type = tok->type;
4059 strs = &str;
4061 else
4063 location_t last_tok_loc = tok->location;
4064 gcc_obstack_init (&str_ob);
4065 count = 0;
4069 cp_lexer_consume_token (parser->lexer);
4070 count++;
4071 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4072 str.len = TREE_STRING_LENGTH (string_tree);
4074 if (curr_tok_is_userdef_p)
4076 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4077 if (have_suffix_p == 0)
4079 suffix_id = curr_suffix_id;
4080 have_suffix_p = 1;
4082 else if (have_suffix_p == 1
4083 && curr_suffix_id != suffix_id)
4085 error ("inconsistent user-defined literal suffixes"
4086 " %qD and %qD in string literal",
4087 suffix_id, curr_suffix_id);
4088 have_suffix_p = -1;
4090 curr_type = cpp_userdef_string_remove_type (tok->type);
4092 else
4093 curr_type = tok->type;
4095 if (type != curr_type)
4097 if (type == CPP_STRING)
4098 type = curr_type;
4099 else if (curr_type != CPP_STRING)
4101 rich_location rich_loc (line_table, tok->location);
4102 rich_loc.add_range (last_tok_loc, false);
4103 error_at (&rich_loc,
4104 "unsupported non-standard concatenation "
4105 "of string literals");
4109 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4111 last_tok_loc = tok->location;
4113 tok = cp_lexer_peek_token (parser->lexer);
4114 if (cpp_userdef_string_p (tok->type))
4116 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4117 curr_type = cpp_userdef_string_remove_type (tok->type);
4118 curr_tok_is_userdef_p = true;
4120 else
4122 string_tree = tok->u.value;
4123 curr_type = tok->type;
4124 curr_tok_is_userdef_p = false;
4127 while (cp_parser_is_string_literal (tok));
4129 /* A string literal built by concatenation has its caret=start at
4130 the start of the initial string, and its finish at the finish of
4131 the final string literal. */
4132 loc = make_location (loc, loc, get_finish (last_tok_loc));
4134 strs = (cpp_string *) obstack_finish (&str_ob);
4137 if (type != CPP_STRING && !wide_ok)
4139 cp_parser_error (parser, "a wide string is invalid in this context");
4140 type = CPP_STRING;
4143 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4144 (parse_in, strs, count, &istr, type))
4146 value = build_string (istr.len, (const char *)istr.text);
4147 free (CONST_CAST (unsigned char *, istr.text));
4149 switch (type)
4151 default:
4152 case CPP_STRING:
4153 case CPP_UTF8STRING:
4154 TREE_TYPE (value) = char_array_type_node;
4155 break;
4156 case CPP_STRING16:
4157 TREE_TYPE (value) = char16_array_type_node;
4158 break;
4159 case CPP_STRING32:
4160 TREE_TYPE (value) = char32_array_type_node;
4161 break;
4162 case CPP_WSTRING:
4163 TREE_TYPE (value) = wchar_array_type_node;
4164 break;
4167 value = fix_string_type (value);
4169 if (have_suffix_p)
4171 tree literal = build_userdef_literal (suffix_id, value,
4172 OT_NONE, NULL_TREE);
4173 if (lookup_udlit)
4174 value = cp_parser_userdef_string_literal (literal);
4175 else
4176 value = literal;
4179 else
4180 /* cpp_interpret_string has issued an error. */
4181 value = error_mark_node;
4183 if (count > 1)
4184 obstack_free (&str_ob, 0);
4186 return cp_expr (value, loc);
4189 /* Look up a literal operator with the name and the exact arguments. */
4191 static tree
4192 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4194 tree decl;
4195 decl = lookup_name (name);
4196 if (!decl || !is_overloaded_fn (decl))
4197 return error_mark_node;
4199 for (lkp_iterator iter (decl); iter; ++iter)
4201 unsigned int ix;
4202 bool found = true;
4203 tree fn = *iter;
4204 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4205 if (parmtypes != NULL_TREE)
4207 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4208 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4210 tree tparm = TREE_VALUE (parmtypes);
4211 tree targ = TREE_TYPE ((*args)[ix]);
4212 bool ptr = TYPE_PTR_P (tparm);
4213 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4214 if ((ptr || arr || !same_type_p (tparm, targ))
4215 && (!ptr || !arr
4216 || !same_type_p (TREE_TYPE (tparm),
4217 TREE_TYPE (targ))))
4218 found = false;
4220 if (found
4221 && ix == vec_safe_length (args)
4222 /* May be this should be sufficient_parms_p instead,
4223 depending on how exactly should user-defined literals
4224 work in presence of default arguments on the literal
4225 operator parameters. */
4226 && parmtypes == void_list_node)
4227 return decl;
4231 return error_mark_node;
4234 /* Parse a user-defined char constant. Returns a call to a user-defined
4235 literal operator taking the character as an argument. */
4237 static cp_expr
4238 cp_parser_userdef_char_literal (cp_parser *parser)
4240 cp_token *token = cp_lexer_consume_token (parser->lexer);
4241 tree literal = token->u.value;
4242 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4243 tree value = USERDEF_LITERAL_VALUE (literal);
4244 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4245 tree decl, result;
4247 /* Build up a call to the user-defined operator */
4248 /* Lookup the name we got back from the id-expression. */
4249 vec<tree, va_gc> *args = make_tree_vector ();
4250 vec_safe_push (args, value);
4251 decl = lookup_literal_operator (name, args);
4252 if (!decl || decl == error_mark_node)
4254 error ("unable to find character literal operator %qD with %qT argument",
4255 name, TREE_TYPE (value));
4256 release_tree_vector (args);
4257 return error_mark_node;
4259 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4260 release_tree_vector (args);
4261 return result;
4264 /* A subroutine of cp_parser_userdef_numeric_literal to
4265 create a char... template parameter pack from a string node. */
4267 static tree
4268 make_char_string_pack (tree value)
4270 tree charvec;
4271 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4272 const char *str = TREE_STRING_POINTER (value);
4273 int i, len = TREE_STRING_LENGTH (value) - 1;
4274 tree argvec = make_tree_vec (1);
4276 /* Fill in CHARVEC with all of the parameters. */
4277 charvec = make_tree_vec (len);
4278 for (i = 0; i < len; ++i)
4279 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4281 /* Build the argument packs. */
4282 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4284 TREE_VEC_ELT (argvec, 0) = argpack;
4286 return argvec;
4289 /* A subroutine of cp_parser_userdef_numeric_literal to
4290 create a char... template parameter pack from a string node. */
4292 static tree
4293 make_string_pack (tree value)
4295 tree charvec;
4296 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4297 const unsigned char *str
4298 = (const unsigned char *) TREE_STRING_POINTER (value);
4299 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4300 int len = TREE_STRING_LENGTH (value) / sz - 1;
4301 tree argvec = make_tree_vec (2);
4303 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4304 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4306 /* First template parm is character type. */
4307 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4309 /* Fill in CHARVEC with all of the parameters. */
4310 charvec = make_tree_vec (len);
4311 for (int i = 0; i < len; ++i)
4312 TREE_VEC_ELT (charvec, i)
4313 = double_int_to_tree (str_char_type_node,
4314 double_int::from_buffer (str + i * sz, sz));
4316 /* Build the argument packs. */
4317 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4319 TREE_VEC_ELT (argvec, 1) = argpack;
4321 return argvec;
4324 /* Parse a user-defined numeric constant. returns a call to a user-defined
4325 literal operator. */
4327 static cp_expr
4328 cp_parser_userdef_numeric_literal (cp_parser *parser)
4330 cp_token *token = cp_lexer_consume_token (parser->lexer);
4331 tree literal = token->u.value;
4332 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4333 tree value = USERDEF_LITERAL_VALUE (literal);
4334 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4335 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4336 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4337 tree decl, result;
4338 vec<tree, va_gc> *args;
4340 /* Look for a literal operator taking the exact type of numeric argument
4341 as the literal value. */
4342 args = make_tree_vector ();
4343 vec_safe_push (args, value);
4344 decl = lookup_literal_operator (name, args);
4345 if (decl && decl != error_mark_node)
4347 result = finish_call_expr (decl, &args, false, true,
4348 tf_warning_or_error);
4350 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4352 warning_at (token->location, OPT_Woverflow,
4353 "integer literal exceeds range of %qT type",
4354 long_long_unsigned_type_node);
4356 else
4358 if (overflow > 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "floating literal exceeds range of %qT type",
4361 long_double_type_node);
4362 else if (overflow < 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal truncated to zero");
4367 release_tree_vector (args);
4368 return result;
4370 release_tree_vector (args);
4372 /* If the numeric argument didn't work, look for a raw literal
4373 operator taking a const char* argument consisting of the number
4374 in string format. */
4375 args = make_tree_vector ();
4376 vec_safe_push (args, num_string);
4377 decl = lookup_literal_operator (name, args);
4378 if (decl && decl != error_mark_node)
4380 result = finish_call_expr (decl, &args, false, true,
4381 tf_warning_or_error);
4382 release_tree_vector (args);
4383 return result;
4385 release_tree_vector (args);
4387 /* If the raw literal didn't work, look for a non-type template
4388 function with parameter pack char.... Call the function with
4389 template parameter characters representing the number. */
4390 args = make_tree_vector ();
4391 decl = lookup_literal_operator (name, args);
4392 if (decl && decl != error_mark_node)
4394 tree tmpl_args = make_char_string_pack (num_string);
4395 decl = lookup_template_function (decl, tmpl_args);
4396 result = finish_call_expr (decl, &args, false, true,
4397 tf_warning_or_error);
4398 release_tree_vector (args);
4399 return result;
4402 release_tree_vector (args);
4404 /* In C++14 the standard library defines complex number suffixes that
4405 conflict with GNU extensions. Prefer them if <complex> is #included. */
4406 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4407 bool i14 = (cxx_dialect > cxx11
4408 && (id_equal (suffix_id, "i")
4409 || id_equal (suffix_id, "if")
4410 || id_equal (suffix_id, "il")));
4411 diagnostic_t kind = DK_ERROR;
4412 int opt = 0;
4414 if (i14 && ext)
4416 tree cxlit = lookup_qualified_name (std_node,
4417 get_identifier ("complex_literals"),
4418 0, false, false);
4419 if (cxlit == error_mark_node)
4421 /* No <complex>, so pedwarn and use GNU semantics. */
4422 kind = DK_PEDWARN;
4423 opt = OPT_Wpedantic;
4427 bool complained
4428 = emit_diagnostic (kind, input_location, opt,
4429 "unable to find numeric literal operator %qD", name);
4431 if (!complained)
4432 /* Don't inform either. */;
4433 else if (i14)
4435 inform (token->location, "add %<using namespace std::complex_literals%> "
4436 "(from <complex>) to enable the C++14 user-defined literal "
4437 "suffixes");
4438 if (ext)
4439 inform (token->location, "or use %<j%> instead of %<i%> for the "
4440 "GNU built-in suffix");
4442 else if (!ext)
4443 inform (token->location, "use -fext-numeric-literals "
4444 "to enable more built-in suffixes");
4446 if (kind == DK_ERROR)
4447 value = error_mark_node;
4448 else
4450 /* Use the built-in semantics. */
4451 tree type;
4452 if (id_equal (suffix_id, "i"))
4454 if (TREE_CODE (value) == INTEGER_CST)
4455 type = integer_type_node;
4456 else
4457 type = double_type_node;
4459 else if (id_equal (suffix_id, "if"))
4460 type = float_type_node;
4461 else /* if (id_equal (suffix_id, "il")) */
4462 type = long_double_type_node;
4464 value = build_complex (build_complex_type (type),
4465 fold_convert (type, integer_zero_node),
4466 fold_convert (type, value));
4469 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4470 /* Avoid repeated diagnostics. */
4471 token->u.value = value;
4472 return value;
4475 /* Parse a user-defined string constant. Returns a call to a user-defined
4476 literal operator taking a character pointer and the length of the string
4477 as arguments. */
4479 static tree
4480 cp_parser_userdef_string_literal (tree literal)
4482 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4483 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4484 tree value = USERDEF_LITERAL_VALUE (literal);
4485 int len = TREE_STRING_LENGTH (value)
4486 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4487 tree decl, result;
4488 vec<tree, va_gc> *args;
4490 /* Build up a call to the user-defined operator. */
4491 /* Lookup the name we got back from the id-expression. */
4492 args = make_tree_vector ();
4493 vec_safe_push (args, value);
4494 vec_safe_push (args, build_int_cst (size_type_node, len));
4495 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4499 result = finish_call_expr (decl, &args, false, true,
4500 tf_warning_or_error);
4501 release_tree_vector (args);
4502 return result;
4504 release_tree_vector (args);
4506 /* Look for a template function with typename parameter CharT
4507 and parameter pack CharT... Call the function with
4508 template parameter characters representing the string. */
4509 args = make_tree_vector ();
4510 decl = lookup_literal_operator (name, args);
4511 if (decl && decl != error_mark_node)
4513 tree tmpl_args = make_string_pack (value);
4514 decl = lookup_template_function (decl, tmpl_args);
4515 result = finish_call_expr (decl, &args, false, true,
4516 tf_warning_or_error);
4517 release_tree_vector (args);
4518 return result;
4520 release_tree_vector (args);
4522 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4523 name, TREE_TYPE (value), size_type_node);
4524 return error_mark_node;
4528 /* Basic concepts [gram.basic] */
4530 /* Parse a translation-unit.
4532 translation-unit:
4533 declaration-seq [opt]
4535 Returns TRUE if all went well. */
4537 static bool
4538 cp_parser_translation_unit (cp_parser* parser)
4540 /* The address of the first non-permanent object on the declarator
4541 obstack. */
4542 static void *declarator_obstack_base;
4544 bool success;
4546 /* Create the declarator obstack, if necessary. */
4547 if (!cp_error_declarator)
4549 gcc_obstack_init (&declarator_obstack);
4550 /* Create the error declarator. */
4551 cp_error_declarator = make_declarator (cdk_error);
4552 /* Create the empty parameter list. */
4553 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4554 UNKNOWN_LOCATION);
4555 /* Remember where the base of the declarator obstack lies. */
4556 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4559 cp_parser_declaration_seq_opt (parser);
4561 /* If there are no tokens left then all went well. */
4562 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4564 /* Get rid of the token array; we don't need it any more. */
4565 cp_lexer_destroy (parser->lexer);
4566 parser->lexer = NULL;
4568 /* This file might have been a context that's implicitly extern
4569 "C". If so, pop the lang context. (Only relevant for PCH.) */
4570 if (parser->implicit_extern_c)
4572 pop_lang_context ();
4573 parser->implicit_extern_c = false;
4576 /* Finish up. */
4577 finish_translation_unit ();
4579 success = true;
4581 else
4583 cp_parser_error (parser, "expected declaration");
4584 success = false;
4587 /* Make sure the declarator obstack was fully cleaned up. */
4588 gcc_assert (obstack_next_free (&declarator_obstack)
4589 == declarator_obstack_base);
4591 /* All went well. */
4592 return success;
4595 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4596 decltype context. */
4598 static inline tsubst_flags_t
4599 complain_flags (bool decltype_p)
4601 tsubst_flags_t complain = tf_warning_or_error;
4602 if (decltype_p)
4603 complain |= tf_decltype;
4604 return complain;
4607 /* We're about to parse a collection of statements. If we're currently
4608 parsing tentatively, set up a firewall so that any nested
4609 cp_parser_commit_to_tentative_parse won't affect the current context. */
4611 static cp_token_position
4612 cp_parser_start_tentative_firewall (cp_parser *parser)
4614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4615 return 0;
4617 cp_parser_parse_tentatively (parser);
4618 cp_parser_commit_to_topmost_tentative_parse (parser);
4619 return cp_lexer_token_position (parser->lexer, false);
4622 /* We've finished parsing the collection of statements. Wrap up the
4623 firewall and replace the relevant tokens with the parsed form. */
4625 static void
4626 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4627 tree expr)
4629 if (!start)
4630 return;
4632 /* Finish the firewall level. */
4633 cp_parser_parse_definitely (parser);
4634 /* And remember the result of the parse for when we try again. */
4635 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4636 token->type = CPP_PREPARSED_EXPR;
4637 token->u.value = expr;
4638 token->keyword = RID_MAX;
4639 cp_lexer_purge_tokens_after (parser->lexer, start);
4642 /* Like the above functions, but let the user modify the tokens. Used by
4643 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4644 later parses, so it makes sense to localize the effects of
4645 cp_parser_commit_to_tentative_parse. */
4647 struct tentative_firewall
4649 cp_parser *parser;
4650 bool set;
4652 tentative_firewall (cp_parser *p): parser(p)
4654 /* If we're currently parsing tentatively, start a committed level as a
4655 firewall and then an inner tentative parse. */
4656 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4658 cp_parser_parse_tentatively (parser);
4659 cp_parser_commit_to_topmost_tentative_parse (parser);
4660 cp_parser_parse_tentatively (parser);
4664 ~tentative_firewall()
4666 if (set)
4668 /* Finish the inner tentative parse and the firewall, propagating any
4669 uncommitted error state to the outer tentative parse. */
4670 bool err = cp_parser_error_occurred (parser);
4671 cp_parser_parse_definitely (parser);
4672 cp_parser_parse_definitely (parser);
4673 if (err)
4674 cp_parser_simulate_error (parser);
4679 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4680 This class is for tracking such a matching pair of symbols.
4681 In particular, it tracks the location of the first token,
4682 so that if the second token is missing, we can highlight the
4683 location of the first token when notifying the user about the
4684 problem. */
4686 template <typename traits_t>
4687 class token_pair
4689 public:
4690 /* token_pair's ctor. */
4691 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4693 /* If the next token is the opening symbol for this pair, consume it and
4694 return true.
4695 Otherwise, issue an error and return false.
4696 In either case, record the location of the opening token. */
4698 bool require_open (cp_parser *parser)
4700 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4701 return cp_parser_require (parser, traits_t::open_token_type,
4702 traits_t::required_token_open);
4705 /* Consume the next token from PARSER, recording its location as
4706 that of the opening token within the pair. */
4708 cp_token * consume_open (cp_parser *parser)
4710 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4711 gcc_assert (tok->type == traits_t::open_token_type);
4712 m_open_loc = tok->location;
4713 return tok;
4716 /* If the next token is the closing symbol for this pair, consume it
4717 and return it.
4718 Otherwise, issue an error, highlighting the location of the
4719 corresponding opening token, and return NULL. */
4721 cp_token *require_close (cp_parser *parser) const
4723 return cp_parser_require (parser, traits_t::close_token_type,
4724 traits_t::required_token_close,
4725 m_open_loc);
4728 private:
4729 location_t m_open_loc;
4732 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4734 struct matching_paren_traits
4736 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4737 static const enum required_token required_token_open = RT_OPEN_PAREN;
4738 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4739 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4742 /* "matching_parens" is a token_pair<T> class for tracking matching
4743 pairs of parentheses. */
4745 typedef token_pair<matching_paren_traits> matching_parens;
4747 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4749 struct matching_brace_traits
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4752 static const enum required_token required_token_open = RT_OPEN_BRACE;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4754 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4757 /* "matching_braces" is a token_pair<T> class for tracking matching
4758 pairs of braces. */
4760 typedef token_pair<matching_brace_traits> matching_braces;
4763 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4764 enclosing parentheses. */
4766 static cp_expr
4767 cp_parser_statement_expr (cp_parser *parser)
4769 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4771 /* Consume the '('. */
4772 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4773 matching_parens parens;
4774 parens.consume_open (parser);
4775 /* Start the statement-expression. */
4776 tree expr = begin_stmt_expr ();
4777 /* Parse the compound-statement. */
4778 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4779 /* Finish up. */
4780 expr = finish_stmt_expr (expr, false);
4781 /* Consume the ')'. */
4782 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4783 if (!parens.require_close (parser))
4784 cp_parser_skip_to_end_of_statement (parser);
4786 cp_parser_end_tentative_firewall (parser, start, expr);
4787 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4788 return cp_expr (expr, combined_loc);
4791 /* Expressions [gram.expr] */
4793 /* Parse a fold-operator.
4795 fold-operator:
4796 - * / % ^ & | = < > << >>
4797 = -= *= /= %= ^= &= |= <<= >>=
4798 == != <= >= && || , .* ->*
4800 This returns the tree code corresponding to the matched operator
4801 as an int. When the current token matches a compound assignment
4802 opertor, the resulting tree code is the negative value of the
4803 non-assignment operator. */
4805 static int
4806 cp_parser_fold_operator (cp_token *token)
4808 switch (token->type)
4810 case CPP_PLUS: return PLUS_EXPR;
4811 case CPP_MINUS: return MINUS_EXPR;
4812 case CPP_MULT: return MULT_EXPR;
4813 case CPP_DIV: return TRUNC_DIV_EXPR;
4814 case CPP_MOD: return TRUNC_MOD_EXPR;
4815 case CPP_XOR: return BIT_XOR_EXPR;
4816 case CPP_AND: return BIT_AND_EXPR;
4817 case CPP_OR: return BIT_IOR_EXPR;
4818 case CPP_LSHIFT: return LSHIFT_EXPR;
4819 case CPP_RSHIFT: return RSHIFT_EXPR;
4821 case CPP_EQ: return -NOP_EXPR;
4822 case CPP_PLUS_EQ: return -PLUS_EXPR;
4823 case CPP_MINUS_EQ: return -MINUS_EXPR;
4824 case CPP_MULT_EQ: return -MULT_EXPR;
4825 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4826 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4827 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4828 case CPP_AND_EQ: return -BIT_AND_EXPR;
4829 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4830 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4831 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4833 case CPP_EQ_EQ: return EQ_EXPR;
4834 case CPP_NOT_EQ: return NE_EXPR;
4835 case CPP_LESS: return LT_EXPR;
4836 case CPP_GREATER: return GT_EXPR;
4837 case CPP_LESS_EQ: return LE_EXPR;
4838 case CPP_GREATER_EQ: return GE_EXPR;
4840 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4841 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4843 case CPP_COMMA: return COMPOUND_EXPR;
4845 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4846 case CPP_DEREF_STAR: return MEMBER_REF;
4848 default: return ERROR_MARK;
4852 /* Returns true if CODE indicates a binary expression, which is not allowed in
4853 the LHS of a fold-expression. More codes will need to be added to use this
4854 function in other contexts. */
4856 static bool
4857 is_binary_op (tree_code code)
4859 switch (code)
4861 case PLUS_EXPR:
4862 case POINTER_PLUS_EXPR:
4863 case MINUS_EXPR:
4864 case MULT_EXPR:
4865 case TRUNC_DIV_EXPR:
4866 case TRUNC_MOD_EXPR:
4867 case BIT_XOR_EXPR:
4868 case BIT_AND_EXPR:
4869 case BIT_IOR_EXPR:
4870 case LSHIFT_EXPR:
4871 case RSHIFT_EXPR:
4873 case MODOP_EXPR:
4875 case EQ_EXPR:
4876 case NE_EXPR:
4877 case LE_EXPR:
4878 case GE_EXPR:
4879 case LT_EXPR:
4880 case GT_EXPR:
4882 case TRUTH_ANDIF_EXPR:
4883 case TRUTH_ORIF_EXPR:
4885 case COMPOUND_EXPR:
4887 case DOTSTAR_EXPR:
4888 case MEMBER_REF:
4889 return true;
4891 default:
4892 return false;
4896 /* If the next token is a suitable fold operator, consume it and return as
4897 the function above. */
4899 static int
4900 cp_parser_fold_operator (cp_parser *parser)
4902 cp_token* token = cp_lexer_peek_token (parser->lexer);
4903 int code = cp_parser_fold_operator (token);
4904 if (code != ERROR_MARK)
4905 cp_lexer_consume_token (parser->lexer);
4906 return code;
4909 /* Parse a fold-expression.
4911 fold-expression:
4912 ( ... folding-operator cast-expression)
4913 ( cast-expression folding-operator ... )
4914 ( cast-expression folding operator ... folding-operator cast-expression)
4916 Note that the '(' and ')' are matched in primary expression. */
4918 static cp_expr
4919 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4921 cp_id_kind pidk;
4923 // Left fold.
4924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4926 cp_lexer_consume_token (parser->lexer);
4927 int op = cp_parser_fold_operator (parser);
4928 if (op == ERROR_MARK)
4930 cp_parser_error (parser, "expected binary operator");
4931 return error_mark_node;
4934 tree expr = cp_parser_cast_expression (parser, false, false,
4935 false, &pidk);
4936 if (expr == error_mark_node)
4937 return error_mark_node;
4938 return finish_left_unary_fold_expr (expr, op);
4941 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4951 cp_parser_error (parser, "expected ...");
4952 return error_mark_node;
4954 cp_lexer_consume_token (parser->lexer);
4956 /* The operands of a fold-expression are cast-expressions, so binary or
4957 conditional expressions are not allowed. We check this here to avoid
4958 tentative parsing. */
4959 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4960 /* OK, the expression was parenthesized. */;
4961 else if (is_binary_op (TREE_CODE (expr1)))
4962 error_at (location_of (expr1),
4963 "binary expression in operand of fold-expression");
4964 else if (TREE_CODE (expr1) == COND_EXPR)
4965 error_at (location_of (expr1),
4966 "conditional expression in operand of fold-expression");
4968 // Right fold.
4969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4970 return finish_right_unary_fold_expr (expr1, op);
4972 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4974 cp_parser_error (parser, "mismatched operator in fold-expression");
4975 return error_mark_node;
4977 cp_lexer_consume_token (parser->lexer);
4979 // Binary left or right fold.
4980 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4981 if (expr2 == error_mark_node)
4982 return error_mark_node;
4983 return finish_binary_fold_expr (expr1, expr2, op);
4986 /* Parse a primary-expression.
4988 primary-expression:
4989 literal
4990 this
4991 ( expression )
4992 id-expression
4993 lambda-expression (C++11)
4995 GNU Extensions:
4997 primary-expression:
4998 ( compound-statement )
4999 __builtin_va_arg ( assignment-expression , type-id )
5000 __builtin_offsetof ( type-id , offsetof-expression )
5002 C++ Extensions:
5003 __has_nothrow_assign ( type-id )
5004 __has_nothrow_constructor ( type-id )
5005 __has_nothrow_copy ( type-id )
5006 __has_trivial_assign ( type-id )
5007 __has_trivial_constructor ( type-id )
5008 __has_trivial_copy ( type-id )
5009 __has_trivial_destructor ( type-id )
5010 __has_virtual_destructor ( type-id )
5011 __is_abstract ( type-id )
5012 __is_base_of ( type-id , type-id )
5013 __is_class ( type-id )
5014 __is_empty ( type-id )
5015 __is_enum ( type-id )
5016 __is_final ( type-id )
5017 __is_literal_type ( type-id )
5018 __is_pod ( type-id )
5019 __is_polymorphic ( type-id )
5020 __is_std_layout ( type-id )
5021 __is_trivial ( type-id )
5022 __is_union ( type-id )
5024 Objective-C++ Extension:
5026 primary-expression:
5027 objc-expression
5029 literal:
5030 __null
5032 ADDRESS_P is true iff this expression was immediately preceded by
5033 "&" and therefore might denote a pointer-to-member. CAST_P is true
5034 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5035 true iff this expression is a template argument.
5037 Returns a representation of the expression. Upon return, *IDK
5038 indicates what kind of id-expression (if any) was present. */
5040 static cp_expr
5041 cp_parser_primary_expression (cp_parser *parser,
5042 bool address_p,
5043 bool cast_p,
5044 bool template_arg_p,
5045 bool decltype_p,
5046 cp_id_kind *idk)
5048 cp_token *token = NULL;
5050 /* Assume the primary expression is not an id-expression. */
5051 *idk = CP_ID_KIND_NONE;
5053 /* Peek at the next token. */
5054 token = cp_lexer_peek_token (parser->lexer);
5055 switch ((int) token->type)
5057 /* literal:
5058 integer-literal
5059 character-literal
5060 floating-literal
5061 string-literal
5062 boolean-literal
5063 pointer-literal
5064 user-defined-literal */
5065 case CPP_CHAR:
5066 case CPP_CHAR16:
5067 case CPP_CHAR32:
5068 case CPP_WCHAR:
5069 case CPP_UTF8CHAR:
5070 case CPP_NUMBER:
5071 case CPP_PREPARSED_EXPR:
5072 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5073 return cp_parser_userdef_numeric_literal (parser);
5074 token = cp_lexer_consume_token (parser->lexer);
5075 if (TREE_CODE (token->u.value) == FIXED_CST)
5077 error_at (token->location,
5078 "fixed-point types not supported in C++");
5079 return error_mark_node;
5081 /* Floating-point literals are only allowed in an integral
5082 constant expression if they are cast to an integral or
5083 enumeration type. */
5084 if (TREE_CODE (token->u.value) == REAL_CST
5085 && parser->integral_constant_expression_p
5086 && pedantic)
5088 /* CAST_P will be set even in invalid code like "int(2.7 +
5089 ...)". Therefore, we have to check that the next token
5090 is sure to end the cast. */
5091 if (cast_p)
5093 cp_token *next_token;
5095 next_token = cp_lexer_peek_token (parser->lexer);
5096 if (/* The comma at the end of an
5097 enumerator-definition. */
5098 next_token->type != CPP_COMMA
5099 /* The curly brace at the end of an enum-specifier. */
5100 && next_token->type != CPP_CLOSE_BRACE
5101 /* The end of a statement. */
5102 && next_token->type != CPP_SEMICOLON
5103 /* The end of the cast-expression. */
5104 && next_token->type != CPP_CLOSE_PAREN
5105 /* The end of an array bound. */
5106 && next_token->type != CPP_CLOSE_SQUARE
5107 /* The closing ">" in a template-argument-list. */
5108 && (next_token->type != CPP_GREATER
5109 || parser->greater_than_is_operator_p)
5110 /* C++0x only: A ">>" treated like two ">" tokens,
5111 in a template-argument-list. */
5112 && (next_token->type != CPP_RSHIFT
5113 || (cxx_dialect == cxx98)
5114 || parser->greater_than_is_operator_p))
5115 cast_p = false;
5118 /* If we are within a cast, then the constraint that the
5119 cast is to an integral or enumeration type will be
5120 checked at that point. If we are not within a cast, then
5121 this code is invalid. */
5122 if (!cast_p)
5123 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5125 return cp_expr (token->u.value, token->location);
5127 case CPP_CHAR_USERDEF:
5128 case CPP_CHAR16_USERDEF:
5129 case CPP_CHAR32_USERDEF:
5130 case CPP_WCHAR_USERDEF:
5131 case CPP_UTF8CHAR_USERDEF:
5132 return cp_parser_userdef_char_literal (parser);
5134 case CPP_STRING:
5135 case CPP_STRING16:
5136 case CPP_STRING32:
5137 case CPP_WSTRING:
5138 case CPP_UTF8STRING:
5139 case CPP_STRING_USERDEF:
5140 case CPP_STRING16_USERDEF:
5141 case CPP_STRING32_USERDEF:
5142 case CPP_WSTRING_USERDEF:
5143 case CPP_UTF8STRING_USERDEF:
5144 /* ??? Should wide strings be allowed when parser->translate_strings_p
5145 is false (i.e. in attributes)? If not, we can kill the third
5146 argument to cp_parser_string_literal. */
5147 return cp_parser_string_literal (parser,
5148 parser->translate_strings_p,
5149 true);
5151 case CPP_OPEN_PAREN:
5152 /* If we see `( { ' then we are looking at the beginning of
5153 a GNU statement-expression. */
5154 if (cp_parser_allow_gnu_extensions_p (parser)
5155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5157 /* Statement-expressions are not allowed by the standard. */
5158 pedwarn (token->location, OPT_Wpedantic,
5159 "ISO C++ forbids braced-groups within expressions");
5161 /* And they're not allowed outside of a function-body; you
5162 cannot, for example, write:
5164 int i = ({ int j = 3; j + 1; });
5166 at class or namespace scope. */
5167 if (!parser->in_function_body
5168 || parser->in_template_argument_list_p)
5170 error_at (token->location,
5171 "statement-expressions are not allowed outside "
5172 "functions nor in template-argument lists");
5173 cp_parser_skip_to_end_of_block_or_statement (parser);
5174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5175 cp_lexer_consume_token (parser->lexer);
5176 return error_mark_node;
5178 else
5179 return cp_parser_statement_expr (parser);
5181 /* Otherwise it's a normal parenthesized expression. */
5183 cp_expr expr;
5184 bool saved_greater_than_is_operator_p;
5186 location_t open_paren_loc = token->location;
5188 /* Consume the `('. */
5189 matching_parens parens;
5190 parens.consume_open (parser);
5191 /* Within a parenthesized expression, a `>' token is always
5192 the greater-than operator. */
5193 saved_greater_than_is_operator_p
5194 = parser->greater_than_is_operator_p;
5195 parser->greater_than_is_operator_p = true;
5197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5198 /* Left fold expression. */
5199 expr = NULL_TREE;
5200 else
5201 /* Parse the parenthesized expression. */
5202 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5204 token = cp_lexer_peek_token (parser->lexer);
5205 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5207 expr = cp_parser_fold_expression (parser, expr);
5208 if (expr != error_mark_node
5209 && cxx_dialect < cxx17
5210 && !in_system_header_at (input_location))
5211 pedwarn (input_location, 0, "fold-expressions only available "
5212 "with -std=c++17 or -std=gnu++17");
5214 else
5215 /* Let the front end know that this expression was
5216 enclosed in parentheses. This matters in case, for
5217 example, the expression is of the form `A::B', since
5218 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5219 not. */
5220 expr = finish_parenthesized_expr (expr);
5222 /* DR 705: Wrapping an unqualified name in parentheses
5223 suppresses arg-dependent lookup. We want to pass back
5224 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5225 (c++/37862), but none of the others. */
5226 if (*idk != CP_ID_KIND_QUALIFIED)
5227 *idk = CP_ID_KIND_NONE;
5229 /* The `>' token might be the end of a template-id or
5230 template-parameter-list now. */
5231 parser->greater_than_is_operator_p
5232 = saved_greater_than_is_operator_p;
5234 /* Consume the `)'. */
5235 token = cp_lexer_peek_token (parser->lexer);
5236 location_t close_paren_loc = token->location;
5237 expr.set_range (open_paren_loc, close_paren_loc);
5238 if (!parens.require_close (parser)
5239 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5240 cp_parser_skip_to_end_of_statement (parser);
5242 return expr;
5245 case CPP_OPEN_SQUARE:
5247 if (c_dialect_objc ())
5249 /* We might have an Objective-C++ message. */
5250 cp_parser_parse_tentatively (parser);
5251 tree msg = cp_parser_objc_message_expression (parser);
5252 /* If that works out, we're done ... */
5253 if (cp_parser_parse_definitely (parser))
5254 return msg;
5255 /* ... else, fall though to see if it's a lambda. */
5257 cp_expr lam = cp_parser_lambda_expression (parser);
5258 /* Don't warn about a failed tentative parse. */
5259 if (cp_parser_error_occurred (parser))
5260 return error_mark_node;
5261 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5262 return lam;
5265 case CPP_OBJC_STRING:
5266 if (c_dialect_objc ())
5267 /* We have an Objective-C++ string literal. */
5268 return cp_parser_objc_expression (parser);
5269 cp_parser_error (parser, "expected primary-expression");
5270 return error_mark_node;
5272 case CPP_KEYWORD:
5273 switch (token->keyword)
5275 /* These two are the boolean literals. */
5276 case RID_TRUE:
5277 cp_lexer_consume_token (parser->lexer);
5278 return cp_expr (boolean_true_node, token->location);
5279 case RID_FALSE:
5280 cp_lexer_consume_token (parser->lexer);
5281 return cp_expr (boolean_false_node, token->location);
5283 /* The `__null' literal. */
5284 case RID_NULL:
5285 cp_lexer_consume_token (parser->lexer);
5286 return cp_expr (null_node, token->location);
5288 /* The `nullptr' literal. */
5289 case RID_NULLPTR:
5290 cp_lexer_consume_token (parser->lexer);
5291 return cp_expr (nullptr_node, token->location);
5293 /* Recognize the `this' keyword. */
5294 case RID_THIS:
5295 cp_lexer_consume_token (parser->lexer);
5296 if (parser->local_variables_forbidden_p)
5298 error_at (token->location,
5299 "%<this%> may not be used in this context");
5300 return error_mark_node;
5302 /* Pointers cannot appear in constant-expressions. */
5303 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5304 return error_mark_node;
5305 return cp_expr (finish_this_expr (), token->location);
5307 /* The `operator' keyword can be the beginning of an
5308 id-expression. */
5309 case RID_OPERATOR:
5310 goto id_expression;
5312 case RID_FUNCTION_NAME:
5313 case RID_PRETTY_FUNCTION_NAME:
5314 case RID_C99_FUNCTION_NAME:
5316 non_integral_constant name;
5318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5319 __func__ are the names of variables -- but they are
5320 treated specially. Therefore, they are handled here,
5321 rather than relying on the generic id-expression logic
5322 below. Grammatically, these names are id-expressions.
5324 Consume the token. */
5325 token = cp_lexer_consume_token (parser->lexer);
5327 switch (token->keyword)
5329 case RID_FUNCTION_NAME:
5330 name = NIC_FUNC_NAME;
5331 break;
5332 case RID_PRETTY_FUNCTION_NAME:
5333 name = NIC_PRETTY_FUNC;
5334 break;
5335 case RID_C99_FUNCTION_NAME:
5336 name = NIC_C99_FUNC;
5337 break;
5338 default:
5339 gcc_unreachable ();
5342 if (cp_parser_non_integral_constant_expression (parser, name))
5343 return error_mark_node;
5345 /* Look up the name. */
5346 return finish_fname (token->u.value);
5349 case RID_VA_ARG:
5351 tree expression;
5352 tree type;
5353 source_location type_location;
5354 location_t start_loc
5355 = cp_lexer_peek_token (parser->lexer)->location;
5356 /* The `__builtin_va_arg' construct is used to handle
5357 `va_arg'. Consume the `__builtin_va_arg' token. */
5358 cp_lexer_consume_token (parser->lexer);
5359 /* Look for the opening `('. */
5360 matching_parens parens;
5361 parens.require_open (parser);
5362 /* Now, parse the assignment-expression. */
5363 expression = cp_parser_assignment_expression (parser);
5364 /* Look for the `,'. */
5365 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5366 type_location = cp_lexer_peek_token (parser->lexer)->location;
5367 /* Parse the type-id. */
5369 type_id_in_expr_sentinel s (parser);
5370 type = cp_parser_type_id (parser);
5372 /* Look for the closing `)'. */
5373 location_t finish_loc
5374 = cp_lexer_peek_token (parser->lexer)->location;
5375 parens.require_close (parser);
5376 /* Using `va_arg' in a constant-expression is not
5377 allowed. */
5378 if (cp_parser_non_integral_constant_expression (parser,
5379 NIC_VA_ARG))
5380 return error_mark_node;
5381 /* Construct a location of the form:
5382 __builtin_va_arg (v, int)
5383 ~~~~~~~~~~~~~~~~~~~~~^~~~
5384 with the caret at the type, ranging from the start of the
5385 "__builtin_va_arg" token to the close paren. */
5386 location_t combined_loc
5387 = make_location (type_location, start_loc, finish_loc);
5388 return build_x_va_arg (combined_loc, expression, type);
5391 case RID_OFFSETOF:
5392 return cp_parser_builtin_offsetof (parser);
5394 case RID_HAS_NOTHROW_ASSIGN:
5395 case RID_HAS_NOTHROW_CONSTRUCTOR:
5396 case RID_HAS_NOTHROW_COPY:
5397 case RID_HAS_TRIVIAL_ASSIGN:
5398 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5399 case RID_HAS_TRIVIAL_COPY:
5400 case RID_HAS_TRIVIAL_DESTRUCTOR:
5401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5402 case RID_HAS_VIRTUAL_DESTRUCTOR:
5403 case RID_IS_ABSTRACT:
5404 case RID_IS_AGGREGATE:
5405 case RID_IS_BASE_OF:
5406 case RID_IS_CLASS:
5407 case RID_IS_EMPTY:
5408 case RID_IS_ENUM:
5409 case RID_IS_FINAL:
5410 case RID_IS_LITERAL_TYPE:
5411 case RID_IS_POD:
5412 case RID_IS_POLYMORPHIC:
5413 case RID_IS_SAME_AS:
5414 case RID_IS_STD_LAYOUT:
5415 case RID_IS_TRIVIAL:
5416 case RID_IS_TRIVIALLY_ASSIGNABLE:
5417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5418 case RID_IS_TRIVIALLY_COPYABLE:
5419 case RID_IS_UNION:
5420 case RID_IS_ASSIGNABLE:
5421 case RID_IS_CONSTRUCTIBLE:
5422 return cp_parser_trait_expr (parser, token->keyword);
5424 // C++ concepts
5425 case RID_REQUIRES:
5426 return cp_parser_requires_expression (parser);
5428 /* Objective-C++ expressions. */
5429 case RID_AT_ENCODE:
5430 case RID_AT_PROTOCOL:
5431 case RID_AT_SELECTOR:
5432 return cp_parser_objc_expression (parser);
5434 case RID_TEMPLATE:
5435 if (parser->in_function_body
5436 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5437 == CPP_LESS))
5439 error_at (token->location,
5440 "a template declaration cannot appear at block scope");
5441 cp_parser_skip_to_end_of_block_or_statement (parser);
5442 return error_mark_node;
5444 /* FALLTHRU */
5445 default:
5446 cp_parser_error (parser, "expected primary-expression");
5447 return error_mark_node;
5450 /* An id-expression can start with either an identifier, a
5451 `::' as the beginning of a qualified-id, or the "operator"
5452 keyword. */
5453 case CPP_NAME:
5454 case CPP_SCOPE:
5455 case CPP_TEMPLATE_ID:
5456 case CPP_NESTED_NAME_SPECIFIER:
5458 id_expression:
5459 cp_expr id_expression;
5460 cp_expr decl;
5461 const char *error_msg;
5462 bool template_p;
5463 bool done;
5464 cp_token *id_expr_token;
5466 /* Parse the id-expression. */
5467 id_expression
5468 = cp_parser_id_expression (parser,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5471 &template_p,
5472 /*declarator_p=*/false,
5473 /*optional_p=*/false);
5474 if (id_expression == error_mark_node)
5475 return error_mark_node;
5476 id_expr_token = token;
5477 token = cp_lexer_peek_token (parser->lexer);
5478 done = (token->type != CPP_OPEN_SQUARE
5479 && token->type != CPP_OPEN_PAREN
5480 && token->type != CPP_DOT
5481 && token->type != CPP_DEREF
5482 && token->type != CPP_PLUS_PLUS
5483 && token->type != CPP_MINUS_MINUS);
5484 /* If we have a template-id, then no further lookup is
5485 required. If the template-id was for a template-class, we
5486 will sometimes have a TYPE_DECL at this point. */
5487 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5488 || TREE_CODE (id_expression) == TYPE_DECL)
5489 decl = id_expression;
5490 /* Look up the name. */
5491 else
5493 tree ambiguous_decls;
5495 /* If we already know that this lookup is ambiguous, then
5496 we've already issued an error message; there's no reason
5497 to check again. */
5498 if (id_expr_token->type == CPP_NAME
5499 && id_expr_token->error_reported)
5501 cp_parser_simulate_error (parser);
5502 return error_mark_node;
5505 decl = cp_parser_lookup_name (parser, id_expression,
5506 none_type,
5507 template_p,
5508 /*is_namespace=*/false,
5509 /*check_dependency=*/true,
5510 &ambiguous_decls,
5511 id_expr_token->location);
5512 /* If the lookup was ambiguous, an error will already have
5513 been issued. */
5514 if (ambiguous_decls)
5515 return error_mark_node;
5517 /* In Objective-C++, we may have an Objective-C 2.0
5518 dot-syntax for classes here. */
5519 if (c_dialect_objc ()
5520 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5521 && TREE_CODE (decl) == TYPE_DECL
5522 && objc_is_class_name (decl))
5524 tree component;
5525 cp_lexer_consume_token (parser->lexer);
5526 component = cp_parser_identifier (parser);
5527 if (component == error_mark_node)
5528 return error_mark_node;
5530 tree result = objc_build_class_component_ref (id_expression,
5531 component);
5532 /* Build a location of the form:
5533 expr.component
5534 ~~~~~^~~~~~~~~
5535 with caret at the start of the component name (at
5536 input_location), ranging from the start of the id_expression
5537 to the end of the component name. */
5538 location_t combined_loc
5539 = make_location (input_location, id_expression.get_start (),
5540 get_finish (input_location));
5541 protected_set_expr_location (result, combined_loc);
5542 return result;
5545 /* In Objective-C++, an instance variable (ivar) may be preferred
5546 to whatever cp_parser_lookup_name() found.
5547 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5548 rest of c-family, we have to do a little extra work to preserve
5549 any location information in cp_expr "decl". Given that
5550 objc_lookup_ivar is implemented in "c-family" and "objc", we
5551 have a trip through the pure "tree" type, rather than cp_expr.
5552 Naively copying it back to "decl" would implicitly give the
5553 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5554 store an EXPR_LOCATION. Hence we only update "decl" (and
5555 hence its location_t) if we get back a different tree node. */
5556 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5557 id_expression);
5558 if (decl_tree != decl.get_value ())
5559 decl = cp_expr (decl_tree);
5561 /* If name lookup gives us a SCOPE_REF, then the
5562 qualifying scope was dependent. */
5563 if (TREE_CODE (decl) == SCOPE_REF)
5565 /* At this point, we do not know if DECL is a valid
5566 integral constant expression. We assume that it is
5567 in fact such an expression, so that code like:
5569 template <int N> struct A {
5570 int a[B<N>::i];
5573 is accepted. At template-instantiation time, we
5574 will check that B<N>::i is actually a constant. */
5575 return decl;
5577 /* Check to see if DECL is a local variable in a context
5578 where that is forbidden. */
5579 if (parser->local_variables_forbidden_p
5580 && local_variable_p (decl))
5582 /* It might be that we only found DECL because we are
5583 trying to be generous with pre-ISO scoping rules.
5584 For example, consider:
5586 int i;
5587 void g() {
5588 for (int i = 0; i < 10; ++i) {}
5589 extern void f(int j = i);
5592 Here, name look up will originally find the out
5593 of scope `i'. We need to issue a warning message,
5594 but then use the global `i'. */
5595 decl = check_for_out_of_scope_variable (decl);
5596 if (local_variable_p (decl))
5598 error_at (id_expr_token->location,
5599 "local variable %qD may not appear in this context",
5600 decl.get_value ());
5601 return error_mark_node;
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5640 /* Parse an id-expression.
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5735 return unqualified_id;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5741 cp_token *token;
5742 tree id;
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5770 switch (token->type)
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5792 /* Parse an unqualified-id.
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5821 cp_token *token;
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5826 switch ((int) token->type)
5828 case CPP_NAME:
5830 tree id;
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5853 case CPP_COMPL:
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5884 struct S {
5885 int S;
5886 ~S();
5889 S::~S() {}
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5920 gcc_assert (!scope || TYPE_P (scope));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6005 /* Look in the surrounding context. */
6006 if (!done)
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6059 /* [class.dtor]
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6077 cp_expr id;
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6097 else if (UDLIT_OPER_P (id))
6099 /* 17.6.3.3.5 */
6100 const char *name = UDLIT_OP_SUFFIX (id);
6101 if (name[0] != '_' && !in_system_header_at (input_location)
6102 && declarator_p)
6103 warning (OPT_Wliteral_suffix,
6104 "literal operator suffixes not preceded by %<_%>"
6105 " are reserved for future standardization");
6108 return id;
6110 /* Fall through. */
6112 default:
6113 if (optional_p)
6114 return NULL_TREE;
6115 cp_parser_error (parser, "expected unqualified-id");
6116 return error_mark_node;
6120 /* Parse an (optional) nested-name-specifier.
6122 nested-name-specifier: [C++98]
6123 class-or-namespace-name :: nested-name-specifier [opt]
6124 class-or-namespace-name :: template nested-name-specifier [opt]
6126 nested-name-specifier: [C++0x]
6127 type-name ::
6128 namespace-name ::
6129 nested-name-specifier identifier ::
6130 nested-name-specifier template [opt] simple-template-id ::
6132 PARSER->SCOPE should be set appropriately before this function is
6133 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6135 in name lookups.
6137 Sets PARSER->SCOPE to the class (TYPE) or namespace
6138 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139 it unchanged if there is no nested-name-specifier. Returns the new
6140 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6142 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143 part of a declaration and/or decl-specifier. */
6145 static tree
6146 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6147 bool typename_keyword_p,
6148 bool check_dependency_p,
6149 bool type_p,
6150 bool is_declaration,
6151 bool template_keyword_p /* = false */)
6153 bool success = false;
6154 cp_token_position start = 0;
6155 cp_token *token;
6157 /* Remember where the nested-name-specifier starts. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6160 start = cp_lexer_token_position (parser->lexer, false);
6161 push_deferring_access_checks (dk_deferred);
6164 while (true)
6166 tree new_scope;
6167 tree old_scope;
6168 tree saved_qualifying_scope;
6170 /* Spot cases that cannot be the beginning of a
6171 nested-name-specifier. */
6172 token = cp_lexer_peek_token (parser->lexer);
6174 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 the already parsed nested-name-specifier. */
6176 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6178 /* Grab the nested-name-specifier and continue the loop. */
6179 cp_parser_pre_parsed_nested_name_specifier (parser);
6180 /* If we originally encountered this nested-name-specifier
6181 with IS_DECLARATION set to false, we will not have
6182 resolved TYPENAME_TYPEs, so we must do so here. */
6183 if (is_declaration
6184 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6186 new_scope = resolve_typename_type (parser->scope,
6187 /*only_current_p=*/false);
6188 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6189 parser->scope = new_scope;
6191 success = true;
6192 continue;
6195 /* Spot cases that cannot be the beginning of a
6196 nested-name-specifier. On the second and subsequent times
6197 through the loop, we look for the `template' keyword. */
6198 if (success && token->keyword == RID_TEMPLATE)
6200 /* A template-id can start a nested-name-specifier. */
6201 else if (token->type == CPP_TEMPLATE_ID)
6203 /* DR 743: decltype can be used in a nested-name-specifier. */
6204 else if (token_is_decltype (token))
6206 else
6208 /* If the next token is not an identifier, then it is
6209 definitely not a type-name or namespace-name. */
6210 if (token->type != CPP_NAME)
6211 break;
6212 /* If the following token is neither a `<' (to begin a
6213 template-id), nor a `::', then we are not looking at a
6214 nested-name-specifier. */
6215 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6217 if (token->type == CPP_COLON
6218 && parser->colon_corrects_to_scope_p
6219 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6221 gcc_rich_location richloc (token->location);
6222 richloc.add_fixit_replace ("::");
6223 error_at (&richloc,
6224 "found %<:%> in nested-name-specifier, "
6225 "expected %<::%>");
6226 token->type = CPP_SCOPE;
6229 if (token->type != CPP_SCOPE
6230 && !cp_parser_nth_token_starts_template_argument_list_p
6231 (parser, 2))
6232 break;
6235 /* The nested-name-specifier is optional, so we parse
6236 tentatively. */
6237 cp_parser_parse_tentatively (parser);
6239 /* Look for the optional `template' keyword, if this isn't the
6240 first time through the loop. */
6241 if (success)
6242 template_keyword_p = cp_parser_optional_template_keyword (parser);
6244 /* Save the old scope since the name lookup we are about to do
6245 might destroy it. */
6246 old_scope = parser->scope;
6247 saved_qualifying_scope = parser->qualifying_scope;
6248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 look up names in "X<T>::I" in order to determine that "Y" is
6250 a template. So, if we have a typename at this point, we make
6251 an effort to look through it. */
6252 if (is_declaration
6253 && !typename_keyword_p
6254 && parser->scope
6255 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6256 parser->scope = resolve_typename_type (parser->scope,
6257 /*only_current_p=*/false);
6258 /* Parse the qualifying entity. */
6259 new_scope
6260 = cp_parser_qualifying_entity (parser,
6261 typename_keyword_p,
6262 template_keyword_p,
6263 check_dependency_p,
6264 type_p,
6265 is_declaration);
6266 /* Look for the `::' token. */
6267 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6269 /* If we found what we wanted, we keep going; otherwise, we're
6270 done. */
6271 if (!cp_parser_parse_definitely (parser))
6273 bool error_p = false;
6275 /* Restore the OLD_SCOPE since it was valid before the
6276 failed attempt at finding the last
6277 class-or-namespace-name. */
6278 parser->scope = old_scope;
6279 parser->qualifying_scope = saved_qualifying_scope;
6281 /* If the next token is a decltype, and the one after that is a
6282 `::', then the decltype has failed to resolve to a class or
6283 enumeration type. Give this error even when parsing
6284 tentatively since it can't possibly be valid--and we're going
6285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 won't get another chance.*/
6287 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6288 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6289 == CPP_SCOPE))
6291 token = cp_lexer_consume_token (parser->lexer);
6292 error_at (token->location, "decltype evaluates to %qT, "
6293 "which is not a class or enumeration type",
6294 token->u.tree_check_value->value);
6295 parser->scope = error_mark_node;
6296 error_p = true;
6297 /* As below. */
6298 success = true;
6299 cp_lexer_consume_token (parser->lexer);
6302 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6303 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6305 /* If we have a non-type template-id followed by ::, it can't
6306 possibly be valid. */
6307 token = cp_lexer_peek_token (parser->lexer);
6308 tree tid = token->u.tree_check_value->value;
6309 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6310 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6312 tree tmpl = NULL_TREE;
6313 if (is_overloaded_fn (tid))
6315 tree fns = get_fns (tid);
6316 if (OVL_SINGLE_P (fns))
6317 tmpl = OVL_FIRST (fns);
6318 error_at (token->location, "function template-id %qD "
6319 "in nested-name-specifier", tid);
6321 else
6323 /* Variable template. */
6324 tmpl = TREE_OPERAND (tid, 0);
6325 gcc_assert (variable_template_p (tmpl));
6326 error_at (token->location, "variable template-id %qD "
6327 "in nested-name-specifier", tid);
6329 if (tmpl)
6330 inform (DECL_SOURCE_LOCATION (tmpl),
6331 "%qD declared here", tmpl);
6333 parser->scope = error_mark_node;
6334 error_p = true;
6335 /* As below. */
6336 success = true;
6337 cp_lexer_consume_token (parser->lexer);
6338 cp_lexer_consume_token (parser->lexer);
6342 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6343 break;
6344 /* If the next token is an identifier, and the one after
6345 that is a `::', then any valid interpretation would have
6346 found a class-or-namespace-name. */
6347 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6348 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6349 == CPP_SCOPE)
6350 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6351 != CPP_COMPL))
6353 token = cp_lexer_consume_token (parser->lexer);
6354 if (!error_p)
6356 if (!token->error_reported)
6358 tree decl;
6359 tree ambiguous_decls;
6361 decl = cp_parser_lookup_name (parser, token->u.value,
6362 none_type,
6363 /*is_template=*/false,
6364 /*is_namespace=*/false,
6365 /*check_dependency=*/true,
6366 &ambiguous_decls,
6367 token->location);
6368 if (TREE_CODE (decl) == TEMPLATE_DECL)
6369 error_at (token->location,
6370 "%qD used without template parameters",
6371 decl);
6372 else if (ambiguous_decls)
6374 // cp_parser_lookup_name has the same diagnostic,
6375 // thus make sure to emit it at most once.
6376 if (cp_parser_uncommitted_to_tentative_parse_p
6377 (parser))
6379 error_at (token->location,
6380 "reference to %qD is ambiguous",
6381 token->u.value);
6382 print_candidates (ambiguous_decls);
6384 decl = error_mark_node;
6386 else
6388 if (cxx_dialect != cxx98)
6389 cp_parser_name_lookup_error
6390 (parser, token->u.value, decl, NLE_NOT_CXX98,
6391 token->location);
6392 else
6393 cp_parser_name_lookup_error
6394 (parser, token->u.value, decl, NLE_CXX98,
6395 token->location);
6398 parser->scope = error_mark_node;
6399 error_p = true;
6400 /* Treat this as a successful nested-name-specifier
6401 due to:
6403 [basic.lookup.qual]
6405 If the name found is not a class-name (clause
6406 _class_) or namespace-name (_namespace.def_), the
6407 program is ill-formed. */
6408 success = true;
6410 cp_lexer_consume_token (parser->lexer);
6412 break;
6414 /* We've found one valid nested-name-specifier. */
6415 success = true;
6416 /* Name lookup always gives us a DECL. */
6417 if (TREE_CODE (new_scope) == TYPE_DECL)
6418 new_scope = TREE_TYPE (new_scope);
6419 /* Uses of "template" must be followed by actual templates. */
6420 if (template_keyword_p
6421 && !(CLASS_TYPE_P (new_scope)
6422 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6424 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6425 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6426 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6427 == TEMPLATE_ID_EXPR)))
6428 permerror (input_location, TYPE_P (new_scope)
6429 ? G_("%qT is not a template")
6430 : G_("%qD is not a template"),
6431 new_scope);
6432 /* If it is a class scope, try to complete it; we are about to
6433 be looking up names inside the class. */
6434 if (TYPE_P (new_scope)
6435 /* Since checking types for dependency can be expensive,
6436 avoid doing it if the type is already complete. */
6437 && !COMPLETE_TYPE_P (new_scope)
6438 /* Do not try to complete dependent types. */
6439 && !dependent_type_p (new_scope))
6441 new_scope = complete_type (new_scope);
6442 /* If it is a typedef to current class, use the current
6443 class instead, as the typedef won't have any names inside
6444 it yet. */
6445 if (!COMPLETE_TYPE_P (new_scope)
6446 && currently_open_class (new_scope))
6447 new_scope = TYPE_MAIN_VARIANT (new_scope);
6449 /* Make sure we look in the right scope the next time through
6450 the loop. */
6451 parser->scope = new_scope;
6454 /* If parsing tentatively, replace the sequence of tokens that makes
6455 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456 token. That way, should we re-parse the token stream, we will
6457 not have to repeat the effort required to do the parse, nor will
6458 we issue duplicate error messages. */
6459 if (success && start)
6461 cp_token *token;
6463 token = cp_lexer_token_at (parser->lexer, start);
6464 /* Reset the contents of the START token. */
6465 token->type = CPP_NESTED_NAME_SPECIFIER;
6466 /* Retrieve any deferred checks. Do not pop this access checks yet
6467 so the memory will not be reclaimed during token replacing below. */
6468 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6469 token->u.tree_check_value->value = parser->scope;
6470 token->u.tree_check_value->checks = get_deferred_access_checks ();
6471 token->u.tree_check_value->qualifying_scope =
6472 parser->qualifying_scope;
6473 token->keyword = RID_MAX;
6475 /* Purge all subsequent tokens. */
6476 cp_lexer_purge_tokens_after (parser->lexer, start);
6479 if (start)
6480 pop_to_parent_deferring_access_checks ();
6482 return success ? parser->scope : NULL_TREE;
6485 /* Parse a nested-name-specifier. See
6486 cp_parser_nested_name_specifier_opt for details. This function
6487 behaves identically, except that it will an issue an error if no
6488 nested-name-specifier is present. */
6490 static tree
6491 cp_parser_nested_name_specifier (cp_parser *parser,
6492 bool typename_keyword_p,
6493 bool check_dependency_p,
6494 bool type_p,
6495 bool is_declaration)
6497 tree scope;
6499 /* Look for the nested-name-specifier. */
6500 scope = cp_parser_nested_name_specifier_opt (parser,
6501 typename_keyword_p,
6502 check_dependency_p,
6503 type_p,
6504 is_declaration);
6505 /* If it was not present, issue an error message. */
6506 if (!scope)
6508 cp_parser_error (parser, "expected nested-name-specifier");
6509 parser->scope = NULL_TREE;
6512 return scope;
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516 this is either a class-name or a namespace-name (which corresponds
6517 to the class-or-namespace-name production in the grammar). For
6518 C++0x, it can also be a type-name that refers to an enumeration
6519 type or a simple-template-id.
6521 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524 TYPE_P is TRUE iff the next name should be taken as a class-name,
6525 even the same name is declared to be another entity in the same
6526 scope.
6528 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529 specified by the class-or-namespace-name. If neither is found the
6530 ERROR_MARK_NODE is returned. */
6532 static tree
6533 cp_parser_qualifying_entity (cp_parser *parser,
6534 bool typename_keyword_p,
6535 bool template_keyword_p,
6536 bool check_dependency_p,
6537 bool type_p,
6538 bool is_declaration)
6540 tree saved_scope;
6541 tree saved_qualifying_scope;
6542 tree saved_object_scope;
6543 tree scope;
6544 bool only_class_p;
6545 bool successful_parse_p;
6547 /* DR 743: decltype can appear in a nested-name-specifier. */
6548 if (cp_lexer_next_token_is_decltype (parser->lexer))
6550 scope = cp_parser_decltype (parser);
6551 if (TREE_CODE (scope) != ENUMERAL_TYPE
6552 && !MAYBE_CLASS_TYPE_P (scope))
6554 cp_parser_simulate_error (parser);
6555 return error_mark_node;
6557 if (TYPE_NAME (scope))
6558 scope = TYPE_NAME (scope);
6559 return scope;
6562 /* Before we try to parse the class-name, we must save away the
6563 current PARSER->SCOPE since cp_parser_class_name will destroy
6564 it. */
6565 saved_scope = parser->scope;
6566 saved_qualifying_scope = parser->qualifying_scope;
6567 saved_object_scope = parser->object_scope;
6568 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6569 there is no need to look for a namespace-name. */
6570 only_class_p = template_keyword_p
6571 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6572 if (!only_class_p)
6573 cp_parser_parse_tentatively (parser);
6574 scope = cp_parser_class_name (parser,
6575 typename_keyword_p,
6576 template_keyword_p,
6577 type_p ? class_type : none_type,
6578 check_dependency_p,
6579 /*class_head_p=*/false,
6580 is_declaration,
6581 /*enum_ok=*/cxx_dialect > cxx98);
6582 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6583 /* If that didn't work, try for a namespace-name. */
6584 if (!only_class_p && !successful_parse_p)
6586 /* Restore the saved scope. */
6587 parser->scope = saved_scope;
6588 parser->qualifying_scope = saved_qualifying_scope;
6589 parser->object_scope = saved_object_scope;
6590 /* If we are not looking at an identifier followed by the scope
6591 resolution operator, then this is not part of a
6592 nested-name-specifier. (Note that this function is only used
6593 to parse the components of a nested-name-specifier.) */
6594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6595 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6596 return error_mark_node;
6597 scope = cp_parser_namespace_name (parser);
6600 return scope;
6603 /* Return true if we are looking at a compound-literal, false otherwise. */
6605 static bool
6606 cp_parser_compound_literal_p (cp_parser *parser)
6608 cp_lexer_save_tokens (parser->lexer);
6610 /* Skip tokens until the next token is a closing parenthesis.
6611 If we find the closing `)', and the next token is a `{', then
6612 we are looking at a compound-literal. */
6613 bool compound_literal_p
6614 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6615 /*consume_paren=*/true)
6616 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6618 /* Roll back the tokens we skipped. */
6619 cp_lexer_rollback_tokens (parser->lexer);
6621 return compound_literal_p;
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625 of zero, without any folding, but ignoring location wrappers. */
6627 static bool
6628 literal_integer_zerop (const_tree expr)
6630 STRIP_ANY_LOCATION_WRAPPER (expr);
6631 return integer_zerop (expr);
6634 /* Parse a postfix-expression.
6636 postfix-expression:
6637 primary-expression
6638 postfix-expression [ expression ]
6639 postfix-expression ( expression-list [opt] )
6640 simple-type-specifier ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier identifier
6642 ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier template [opt] template-id
6644 ( expression-list [opt] )
6645 postfix-expression . template [opt] id-expression
6646 postfix-expression -> template [opt] id-expression
6647 postfix-expression . pseudo-destructor-name
6648 postfix-expression -> pseudo-destructor-name
6649 postfix-expression ++
6650 postfix-expression --
6651 dynamic_cast < type-id > ( expression )
6652 static_cast < type-id > ( expression )
6653 reinterpret_cast < type-id > ( expression )
6654 const_cast < type-id > ( expression )
6655 typeid ( expression )
6656 typeid ( type-id )
6658 GNU Extension:
6660 postfix-expression:
6661 ( type-id ) { initializer-list , [opt] }
6663 This extension is a GNU version of the C99 compound-literal
6664 construct. (The C99 grammar uses `type-name' instead of `type-id',
6665 but they are essentially the same concept.)
6667 If ADDRESS_P is true, the postfix expression is the operand of the
6668 `&' operator. CAST_P is true if this expression is the target of a
6669 cast.
6671 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672 class member access expressions [expr.ref].
6674 Returns a representation of the expression. */
6676 static cp_expr
6677 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6678 bool member_access_only_p, bool decltype_p,
6679 cp_id_kind * pidk_return)
6681 cp_token *token;
6682 location_t loc;
6683 enum rid keyword;
6684 cp_id_kind idk = CP_ID_KIND_NONE;
6685 cp_expr postfix_expression = NULL_TREE;
6686 bool is_member_access = false;
6688 /* Peek at the next token. */
6689 token = cp_lexer_peek_token (parser->lexer);
6690 loc = token->location;
6691 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6693 /* Some of the productions are determined by keywords. */
6694 keyword = token->keyword;
6695 switch (keyword)
6697 case RID_DYNCAST:
6698 case RID_STATCAST:
6699 case RID_REINTCAST:
6700 case RID_CONSTCAST:
6702 tree type;
6703 cp_expr expression;
6704 const char *saved_message;
6705 bool saved_in_type_id_in_expr_p;
6707 /* All of these can be handled in the same way from the point
6708 of view of parsing. Begin by consuming the token
6709 identifying the cast. */
6710 cp_lexer_consume_token (parser->lexer);
6712 /* New types cannot be defined in the cast. */
6713 saved_message = parser->type_definition_forbidden_message;
6714 parser->type_definition_forbidden_message
6715 = G_("types may not be defined in casts");
6717 /* Look for the opening `<'. */
6718 cp_parser_require (parser, CPP_LESS, RT_LESS);
6719 /* Parse the type to which we are casting. */
6720 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6721 parser->in_type_id_in_expr_p = true;
6722 type = cp_parser_type_id (parser);
6723 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6724 /* Look for the closing `>'. */
6725 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6726 /* Restore the old message. */
6727 parser->type_definition_forbidden_message = saved_message;
6729 bool saved_greater_than_is_operator_p
6730 = parser->greater_than_is_operator_p;
6731 parser->greater_than_is_operator_p = true;
6733 /* And the expression which is being cast. */
6734 matching_parens parens;
6735 parens.require_open (parser);
6736 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6737 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6738 RT_CLOSE_PAREN);
6739 location_t end_loc = close_paren ?
6740 close_paren->location : UNKNOWN_LOCATION;
6742 parser->greater_than_is_operator_p
6743 = saved_greater_than_is_operator_p;
6745 /* Only type conversions to integral or enumeration types
6746 can be used in constant-expressions. */
6747 if (!cast_valid_in_integral_constant_expression_p (type)
6748 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6750 postfix_expression = error_mark_node;
6751 break;
6754 switch (keyword)
6756 case RID_DYNCAST:
6757 postfix_expression
6758 = build_dynamic_cast (type, expression, tf_warning_or_error);
6759 break;
6760 case RID_STATCAST:
6761 postfix_expression
6762 = build_static_cast (type, expression, tf_warning_or_error);
6763 break;
6764 case RID_REINTCAST:
6765 postfix_expression
6766 = build_reinterpret_cast (type, expression,
6767 tf_warning_or_error);
6768 break;
6769 case RID_CONSTCAST:
6770 postfix_expression
6771 = build_const_cast (type, expression, tf_warning_or_error);
6772 break;
6773 default:
6774 gcc_unreachable ();
6777 /* Construct a location e.g. :
6778 reinterpret_cast <int *> (expr)
6779 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 ranging from the start of the "*_cast" token to the final closing
6781 paren, with the caret at the start. */
6782 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6783 postfix_expression.set_location (cp_cast_loc);
6785 break;
6787 case RID_TYPEID:
6789 tree type;
6790 const char *saved_message;
6791 bool saved_in_type_id_in_expr_p;
6793 /* Consume the `typeid' token. */
6794 cp_lexer_consume_token (parser->lexer);
6795 /* Look for the `(' token. */
6796 matching_parens parens;
6797 parens.require_open (parser);
6798 /* Types cannot be defined in a `typeid' expression. */
6799 saved_message = parser->type_definition_forbidden_message;
6800 parser->type_definition_forbidden_message
6801 = G_("types may not be defined in a %<typeid%> expression");
6802 /* We can't be sure yet whether we're looking at a type-id or an
6803 expression. */
6804 cp_parser_parse_tentatively (parser);
6805 /* Try a type-id first. */
6806 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6807 parser->in_type_id_in_expr_p = true;
6808 type = cp_parser_type_id (parser);
6809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6810 /* Look for the `)' token. Otherwise, we can't be sure that
6811 we're not looking at an expression: consider `typeid (int
6812 (3))', for example. */
6813 cp_token *close_paren = parens.require_close (parser);
6814 /* If all went well, simply lookup the type-id. */
6815 if (cp_parser_parse_definitely (parser))
6816 postfix_expression = get_typeid (type, tf_warning_or_error);
6817 /* Otherwise, fall back to the expression variant. */
6818 else
6820 tree expression;
6822 /* Look for an expression. */
6823 expression = cp_parser_expression (parser, & idk);
6824 /* Compute its typeid. */
6825 postfix_expression = build_typeid (expression, tf_warning_or_error);
6826 /* Look for the `)' token. */
6827 close_paren = parens.require_close (parser);
6829 /* Restore the saved message. */
6830 parser->type_definition_forbidden_message = saved_message;
6831 /* `typeid' may not appear in an integral constant expression. */
6832 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6833 postfix_expression = error_mark_node;
6835 /* Construct a location e.g. :
6836 typeid (expr)
6837 ^~~~~~~~~~~~~
6838 ranging from the start of the "typeid" token to the final closing
6839 paren, with the caret at the start. */
6840 if (close_paren)
6842 location_t typeid_loc
6843 = make_location (start_loc, start_loc, close_paren->location);
6844 postfix_expression.set_location (typeid_loc);
6845 postfix_expression.maybe_add_location_wrapper ();
6848 break;
6850 case RID_TYPENAME:
6852 tree type;
6853 /* The syntax permitted here is the same permitted for an
6854 elaborated-type-specifier. */
6855 ++parser->prevent_constrained_type_specifiers;
6856 type = cp_parser_elaborated_type_specifier (parser,
6857 /*is_friend=*/false,
6858 /*is_declaration=*/false);
6859 --parser->prevent_constrained_type_specifiers;
6860 postfix_expression = cp_parser_functional_cast (parser, type);
6862 break;
6864 case RID_ADDRESSOF:
6865 case RID_BUILTIN_SHUFFLE:
6866 case RID_BUILTIN_LAUNDER:
6868 vec<tree, va_gc> *vec;
6869 unsigned int i;
6870 tree p;
6872 cp_lexer_consume_token (parser->lexer);
6873 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6874 /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 /*non_constant_p=*/NULL);
6876 if (vec == NULL)
6878 postfix_expression = error_mark_node;
6879 break;
6882 FOR_EACH_VEC_ELT (*vec, i, p)
6883 mark_exp_read (p);
6885 switch (keyword)
6887 case RID_ADDRESSOF:
6888 if (vec->length () == 1)
6889 postfix_expression
6890 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6891 else
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_addressof%>");
6895 postfix_expression = error_mark_node;
6897 break;
6899 case RID_BUILTIN_LAUNDER:
6900 if (vec->length () == 1)
6901 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6902 tf_warning_or_error);
6903 else
6905 error_at (loc, "wrong number of arguments to "
6906 "%<__builtin_launder%>");
6907 postfix_expression = error_mark_node;
6909 break;
6911 case RID_BUILTIN_SHUFFLE:
6912 if (vec->length () == 2)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6915 (*vec)[1], tf_warning_or_error);
6916 else if (vec->length () == 3)
6917 postfix_expression
6918 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6919 (*vec)[2], tf_warning_or_error);
6920 else
6922 error_at (loc, "wrong number of arguments to "
6923 "%<__builtin_shuffle%>");
6924 postfix_expression = error_mark_node;
6926 break;
6928 default:
6929 gcc_unreachable ();
6931 break;
6934 default:
6936 tree type;
6938 /* If the next thing is a simple-type-specifier, we may be
6939 looking at a functional cast. We could also be looking at
6940 an id-expression. So, we try the functional cast, and if
6941 that doesn't work we fall back to the primary-expression. */
6942 cp_parser_parse_tentatively (parser);
6943 /* Look for the simple-type-specifier. */
6944 ++parser->prevent_constrained_type_specifiers;
6945 type = cp_parser_simple_type_specifier (parser,
6946 /*decl_specs=*/NULL,
6947 CP_PARSER_FLAGS_NONE);
6948 --parser->prevent_constrained_type_specifiers;
6949 /* Parse the cast itself. */
6950 if (!cp_parser_error_occurred (parser))
6951 postfix_expression
6952 = cp_parser_functional_cast (parser, type);
6953 /* If that worked, we're done. */
6954 if (cp_parser_parse_definitely (parser))
6955 break;
6957 /* If the functional-cast didn't work out, try a
6958 compound-literal. */
6959 if (cp_parser_allow_gnu_extensions_p (parser)
6960 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6962 cp_expr initializer = NULL_TREE;
6964 cp_parser_parse_tentatively (parser);
6966 matching_parens parens;
6967 parens.consume_open (parser);
6969 /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 in cp_parser_cast_expression about c++/29234. */
6971 if (!cp_parser_compound_literal_p (parser))
6972 cp_parser_simulate_error (parser);
6973 else
6975 /* Parse the type. */
6976 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6977 parser->in_type_id_in_expr_p = true;
6978 type = cp_parser_type_id (parser);
6979 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6980 parens.require_close (parser);
6983 /* If things aren't going well, there's no need to
6984 keep going. */
6985 if (!cp_parser_error_occurred (parser))
6987 bool non_constant_p;
6988 /* Parse the brace-enclosed initializer list. */
6989 initializer = cp_parser_braced_list (parser,
6990 &non_constant_p);
6992 /* If that worked, we're definitely looking at a
6993 compound-literal expression. */
6994 if (cp_parser_parse_definitely (parser))
6996 /* Warn the user that a compound literal is not
6997 allowed in standard C++. */
6998 pedwarn (input_location, OPT_Wpedantic,
6999 "ISO C++ forbids compound-literals");
7000 /* For simplicity, we disallow compound literals in
7001 constant-expressions. We could
7002 allow compound literals of integer type, whose
7003 initializer was a constant, in constant
7004 expressions. Permitting that usage, as a further
7005 extension, would not change the meaning of any
7006 currently accepted programs. (Of course, as
7007 compound literals are not part of ISO C++, the
7008 standard has nothing to say.) */
7009 if (cp_parser_non_integral_constant_expression (parser,
7010 NIC_NCC))
7012 postfix_expression = error_mark_node;
7013 break;
7015 /* Form the representation of the compound-literal. */
7016 postfix_expression
7017 = finish_compound_literal (type, initializer,
7018 tf_warning_or_error, fcl_c99);
7019 postfix_expression.set_location (initializer.get_location ());
7020 break;
7024 /* It must be a primary-expression. */
7025 postfix_expression
7026 = cp_parser_primary_expression (parser, address_p, cast_p,
7027 /*template_arg_p=*/false,
7028 decltype_p,
7029 &idk);
7031 break;
7034 /* Note that we don't need to worry about calling build_cplus_new on a
7035 class-valued CALL_EXPR in decltype when it isn't the end of the
7036 postfix-expression; unary_complex_lvalue will take care of that for
7037 all these cases. */
7039 /* Keep looping until the postfix-expression is complete. */
7040 while (true)
7042 if (idk == CP_ID_KIND_UNQUALIFIED
7043 && identifier_p (postfix_expression)
7044 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7045 /* It is not a Koenig lookup function call. */
7046 postfix_expression
7047 = unqualified_name_lookup_error (postfix_expression);
7049 /* Peek at the next token. */
7050 token = cp_lexer_peek_token (parser->lexer);
7052 switch (token->type)
7054 case CPP_OPEN_SQUARE:
7055 if (cp_next_tokens_can_be_std_attribute_p (parser))
7057 cp_parser_error (parser,
7058 "two consecutive %<[%> shall "
7059 "only introduce an attribute");
7060 return error_mark_node;
7062 postfix_expression
7063 = cp_parser_postfix_open_square_expression (parser,
7064 postfix_expression,
7065 false,
7066 decltype_p);
7067 postfix_expression.set_range (start_loc,
7068 postfix_expression.get_location ());
7070 idk = CP_ID_KIND_NONE;
7071 is_member_access = false;
7072 break;
7074 case CPP_OPEN_PAREN:
7075 /* postfix-expression ( expression-list [opt] ) */
7077 bool koenig_p;
7078 bool is_builtin_constant_p;
7079 bool saved_integral_constant_expression_p = false;
7080 bool saved_non_integral_constant_expression_p = false;
7081 tsubst_flags_t complain = complain_flags (decltype_p);
7082 vec<tree, va_gc> *args;
7083 location_t close_paren_loc = UNKNOWN_LOCATION;
7085 is_member_access = false;
7087 is_builtin_constant_p
7088 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7089 if (is_builtin_constant_p)
7091 /* The whole point of __builtin_constant_p is to allow
7092 non-constant expressions to appear as arguments. */
7093 saved_integral_constant_expression_p
7094 = parser->integral_constant_expression_p;
7095 saved_non_integral_constant_expression_p
7096 = parser->non_integral_constant_expression_p;
7097 parser->integral_constant_expression_p = false;
7099 args = (cp_parser_parenthesized_expression_list
7100 (parser, non_attr,
7101 /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 /*non_constant_p=*/NULL,
7103 /*close_paren_loc=*/&close_paren_loc,
7104 /*wrap_locations_p=*/true));
7105 if (is_builtin_constant_p)
7107 parser->integral_constant_expression_p
7108 = saved_integral_constant_expression_p;
7109 parser->non_integral_constant_expression_p
7110 = saved_non_integral_constant_expression_p;
7113 if (args == NULL)
7115 postfix_expression = error_mark_node;
7116 break;
7119 /* Function calls are not permitted in
7120 constant-expressions. */
7121 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7122 && cp_parser_non_integral_constant_expression (parser,
7123 NIC_FUNC_CALL))
7125 postfix_expression = error_mark_node;
7126 release_tree_vector (args);
7127 break;
7130 koenig_p = false;
7131 if (idk == CP_ID_KIND_UNQUALIFIED
7132 || idk == CP_ID_KIND_TEMPLATE_ID)
7134 if (identifier_p (postfix_expression))
7136 if (!args->is_empty ())
7138 koenig_p = true;
7139 if (!any_type_dependent_arguments_p (args))
7140 postfix_expression
7141 = perform_koenig_lookup (postfix_expression, args,
7142 complain);
7144 else
7145 postfix_expression
7146 = unqualified_fn_lookup_error (postfix_expression);
7148 /* We do not perform argument-dependent lookup if
7149 normal lookup finds a non-function, in accordance
7150 with the expected resolution of DR 218. */
7151 else if (!args->is_empty ()
7152 && is_overloaded_fn (postfix_expression))
7154 tree fn = get_first_fn (postfix_expression);
7155 fn = STRIP_TEMPLATE (fn);
7157 /* Do not do argument dependent lookup if regular
7158 lookup finds a member function or a block-scope
7159 function declaration. [basic.lookup.argdep]/3 */
7160 if (!DECL_FUNCTION_MEMBER_P (fn)
7161 && !DECL_LOCAL_FUNCTION_P (fn))
7163 koenig_p = true;
7164 if (!any_type_dependent_arguments_p (args))
7165 postfix_expression
7166 = perform_koenig_lookup (postfix_expression, args,
7167 complain);
7172 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7173 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7175 && vec_safe_length (args) == 3)
7177 tree arg0 = (*args)[0];
7178 tree arg1 = (*args)[1];
7179 tree arg2 = (*args)[2];
7180 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7181 | (literal_integer_zerop (arg2) << 2));
7182 warn_for_memset (input_location, arg0, arg2, literal_mask);
7185 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7187 tree instance = TREE_OPERAND (postfix_expression, 0);
7188 tree fn = TREE_OPERAND (postfix_expression, 1);
7190 if (processing_template_decl
7191 && (type_dependent_object_expression_p (instance)
7192 || (!BASELINK_P (fn)
7193 && TREE_CODE (fn) != FIELD_DECL)
7194 || type_dependent_expression_p (fn)
7195 || any_type_dependent_arguments_p (args)))
7197 maybe_generic_this_capture (instance, fn);
7198 postfix_expression
7199 = build_min_nt_call_vec (postfix_expression, args);
7200 release_tree_vector (args);
7201 break;
7204 if (BASELINK_P (fn))
7206 postfix_expression
7207 = (build_new_method_call
7208 (instance, fn, &args, NULL_TREE,
7209 (idk == CP_ID_KIND_QUALIFIED
7210 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7211 : LOOKUP_NORMAL),
7212 /*fn_p=*/NULL,
7213 complain));
7215 else
7216 postfix_expression
7217 = finish_call_expr (postfix_expression, &args,
7218 /*disallow_virtual=*/false,
7219 /*koenig_p=*/false,
7220 complain);
7222 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7223 || TREE_CODE (postfix_expression) == MEMBER_REF
7224 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7225 postfix_expression = (build_offset_ref_call_from_tree
7226 (postfix_expression, &args,
7227 complain));
7228 else if (idk == CP_ID_KIND_QUALIFIED)
7229 /* A call to a static class member, or a namespace-scope
7230 function. */
7231 postfix_expression
7232 = finish_call_expr (postfix_expression, &args,
7233 /*disallow_virtual=*/true,
7234 koenig_p,
7235 complain);
7236 else
7237 /* All other function calls. */
7238 postfix_expression
7239 = finish_call_expr (postfix_expression, &args,
7240 /*disallow_virtual=*/false,
7241 koenig_p,
7242 complain);
7244 if (close_paren_loc != UNKNOWN_LOCATION)
7246 location_t combined_loc = make_location (token->location,
7247 start_loc,
7248 close_paren_loc);
7249 postfix_expression.set_location (combined_loc);
7252 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7253 idk = CP_ID_KIND_NONE;
7255 release_tree_vector (args);
7257 break;
7259 case CPP_DOT:
7260 case CPP_DEREF:
7261 /* postfix-expression . template [opt] id-expression
7262 postfix-expression . pseudo-destructor-name
7263 postfix-expression -> template [opt] id-expression
7264 postfix-expression -> pseudo-destructor-name */
7266 /* Consume the `.' or `->' operator. */
7267 cp_lexer_consume_token (parser->lexer);
7269 postfix_expression
7270 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7271 postfix_expression,
7272 false, &idk, loc);
7274 is_member_access = true;
7275 break;
7277 case CPP_PLUS_PLUS:
7278 /* postfix-expression ++ */
7279 /* Consume the `++' token. */
7280 cp_lexer_consume_token (parser->lexer);
7281 /* Generate a representation for the complete expression. */
7282 postfix_expression
7283 = finish_increment_expr (postfix_expression,
7284 POSTINCREMENT_EXPR);
7285 /* Increments may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7287 postfix_expression = error_mark_node;
7288 idk = CP_ID_KIND_NONE;
7289 is_member_access = false;
7290 break;
7292 case CPP_MINUS_MINUS:
7293 /* postfix-expression -- */
7294 /* Consume the `--' token. */
7295 cp_lexer_consume_token (parser->lexer);
7296 /* Generate a representation for the complete expression. */
7297 postfix_expression
7298 = finish_increment_expr (postfix_expression,
7299 POSTDECREMENT_EXPR);
7300 /* Decrements may not appear in constant-expressions. */
7301 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7302 postfix_expression = error_mark_node;
7303 idk = CP_ID_KIND_NONE;
7304 is_member_access = false;
7305 break;
7307 default:
7308 if (pidk_return != NULL)
7309 * pidk_return = idk;
7310 if (member_access_only_p)
7311 return is_member_access
7312 ? postfix_expression
7313 : cp_expr (error_mark_node);
7314 else
7315 return postfix_expression;
7319 /* We should never get here. */
7320 gcc_unreachable ();
7321 return error_mark_node;
7324 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7325 by cp_parser_builtin_offsetof. We're looking for
7327 postfix-expression [ expression ]
7328 postfix-expression [ braced-init-list ] (C++11)
7330 FOR_OFFSETOF is set if we're being called in that context, which
7331 changes how we deal with integer constant expressions. */
7333 static tree
7334 cp_parser_postfix_open_square_expression (cp_parser *parser,
7335 tree postfix_expression,
7336 bool for_offsetof,
7337 bool decltype_p)
7339 tree index = NULL_TREE;
7340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7341 bool saved_greater_than_is_operator_p;
7343 /* Consume the `[' token. */
7344 cp_lexer_consume_token (parser->lexer);
7346 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7347 parser->greater_than_is_operator_p = true;
7349 /* Parse the index expression. */
7350 /* ??? For offsetof, there is a question of what to allow here. If
7351 offsetof is not being used in an integral constant expression context,
7352 then we *could* get the right answer by computing the value at runtime.
7353 If we are in an integral constant expression context, then we might
7354 could accept any constant expression; hard to say without analysis.
7355 Rather than open the barn door too wide right away, allow only integer
7356 constant expressions here. */
7357 if (for_offsetof)
7358 index = cp_parser_constant_expression (parser);
7359 else
7361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7363 bool expr_nonconst_p;
7364 cp_lexer_set_source_position (parser->lexer);
7365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7366 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7368 else
7369 index = cp_parser_expression (parser);
7372 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7374 /* Look for the closing `]'. */
7375 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7377 /* Build the ARRAY_REF. */
7378 postfix_expression = grok_array_decl (loc, postfix_expression,
7379 index, decltype_p);
7381 /* When not doing offsetof, array references are not permitted in
7382 constant-expressions. */
7383 if (!for_offsetof
7384 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7385 postfix_expression = error_mark_node;
7387 return postfix_expression;
7390 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7391 dereference of incomplete type, returns true if error_mark_node should
7392 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7393 and *DEPENDENT_P. */
7395 bool
7396 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7397 bool *dependent_p)
7399 /* In a template, be permissive by treating an object expression
7400 of incomplete type as dependent (after a pedwarn). */
7401 diagnostic_t kind = (processing_template_decl
7402 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7404 switch (TREE_CODE (*postfix_expression))
7406 case CAST_EXPR:
7407 case REINTERPRET_CAST_EXPR:
7408 case CONST_CAST_EXPR:
7409 case STATIC_CAST_EXPR:
7410 case DYNAMIC_CAST_EXPR:
7411 case IMPLICIT_CONV_EXPR:
7412 case VIEW_CONVERT_EXPR:
7413 case NON_LVALUE_EXPR:
7414 kind = DK_ERROR;
7415 break;
7416 case OVERLOAD:
7417 /* Don't emit any diagnostic for OVERLOADs. */
7418 kind = DK_IGNORED;
7419 break;
7420 default:
7421 /* Avoid clobbering e.g. DECLs. */
7422 if (!EXPR_P (*postfix_expression))
7423 kind = DK_ERROR;
7424 break;
7427 if (kind == DK_IGNORED)
7428 return false;
7430 location_t exploc = location_of (*postfix_expression);
7431 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7432 if (!MAYBE_CLASS_TYPE_P (*scope))
7433 return true;
7434 if (kind == DK_ERROR)
7435 *scope = *postfix_expression = error_mark_node;
7436 else if (processing_template_decl)
7438 *dependent_p = true;
7439 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7441 return false;
7444 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7445 by cp_parser_builtin_offsetof. We're looking for
7447 postfix-expression . template [opt] id-expression
7448 postfix-expression . pseudo-destructor-name
7449 postfix-expression -> template [opt] id-expression
7450 postfix-expression -> pseudo-destructor-name
7452 FOR_OFFSETOF is set if we're being called in that context. That sorta
7453 limits what of the above we'll actually accept, but nevermind.
7454 TOKEN_TYPE is the "." or "->" token, which will already have been
7455 removed from the stream. */
7457 static tree
7458 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7459 enum cpp_ttype token_type,
7460 cp_expr postfix_expression,
7461 bool for_offsetof, cp_id_kind *idk,
7462 location_t location)
7464 tree name;
7465 bool dependent_p;
7466 bool pseudo_destructor_p;
7467 tree scope = NULL_TREE;
7468 location_t start_loc = postfix_expression.get_start ();
7470 /* If this is a `->' operator, dereference the pointer. */
7471 if (token_type == CPP_DEREF)
7472 postfix_expression = build_x_arrow (location, postfix_expression,
7473 tf_warning_or_error);
7474 /* Check to see whether or not the expression is type-dependent and
7475 not the current instantiation. */
7476 dependent_p = type_dependent_object_expression_p (postfix_expression);
7477 /* The identifier following the `->' or `.' is not qualified. */
7478 parser->scope = NULL_TREE;
7479 parser->qualifying_scope = NULL_TREE;
7480 parser->object_scope = NULL_TREE;
7481 *idk = CP_ID_KIND_NONE;
7483 /* Enter the scope corresponding to the type of the object
7484 given by the POSTFIX_EXPRESSION. */
7485 if (!dependent_p)
7487 scope = TREE_TYPE (postfix_expression);
7488 /* According to the standard, no expression should ever have
7489 reference type. Unfortunately, we do not currently match
7490 the standard in this respect in that our internal representation
7491 of an expression may have reference type even when the standard
7492 says it does not. Therefore, we have to manually obtain the
7493 underlying type here. */
7494 scope = non_reference (scope);
7495 /* The type of the POSTFIX_EXPRESSION must be complete. */
7496 /* Unlike the object expression in other contexts, *this is not
7497 required to be of complete type for purposes of class member
7498 access (5.2.5) outside the member function body. */
7499 if (postfix_expression != current_class_ref
7500 && scope != error_mark_node
7501 && !(processing_template_decl
7502 && current_class_type
7503 && (same_type_ignoring_top_level_qualifiers_p
7504 (scope, current_class_type))))
7506 scope = complete_type (scope);
7507 if (!COMPLETE_TYPE_P (scope)
7508 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7509 &dependent_p))
7510 return error_mark_node;
7513 if (!dependent_p)
7515 /* Let the name lookup machinery know that we are processing a
7516 class member access expression. */
7517 parser->context->object_type = scope;
7518 /* If something went wrong, we want to be able to discern that case,
7519 as opposed to the case where there was no SCOPE due to the type
7520 of expression being dependent. */
7521 if (!scope)
7522 scope = error_mark_node;
7523 /* If the SCOPE was erroneous, make the various semantic analysis
7524 functions exit quickly -- and without issuing additional error
7525 messages. */
7526 if (scope == error_mark_node)
7527 postfix_expression = error_mark_node;
7531 if (dependent_p)
7532 /* Tell cp_parser_lookup_name that there was an object, even though it's
7533 type-dependent. */
7534 parser->context->object_type = unknown_type_node;
7536 /* Assume this expression is not a pseudo-destructor access. */
7537 pseudo_destructor_p = false;
7539 /* If the SCOPE is a scalar type, then, if this is a valid program,
7540 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7541 is type dependent, it can be pseudo-destructor-name or something else.
7542 Try to parse it as pseudo-destructor-name first. */
7543 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7545 tree s;
7546 tree type;
7548 cp_parser_parse_tentatively (parser);
7549 /* Parse the pseudo-destructor-name. */
7550 s = NULL_TREE;
7551 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7552 &s, &type);
7553 if (dependent_p
7554 && (cp_parser_error_occurred (parser)
7555 || !SCALAR_TYPE_P (type)))
7556 cp_parser_abort_tentative_parse (parser);
7557 else if (cp_parser_parse_definitely (parser))
7559 pseudo_destructor_p = true;
7560 postfix_expression
7561 = finish_pseudo_destructor_expr (postfix_expression,
7562 s, type, location);
7566 if (!pseudo_destructor_p)
7568 /* If the SCOPE is not a scalar type, we are looking at an
7569 ordinary class member access expression, rather than a
7570 pseudo-destructor-name. */
7571 bool template_p;
7572 cp_token *token = cp_lexer_peek_token (parser->lexer);
7573 /* Parse the id-expression. */
7574 name = (cp_parser_id_expression
7575 (parser,
7576 cp_parser_optional_template_keyword (parser),
7577 /*check_dependency_p=*/true,
7578 &template_p,
7579 /*declarator_p=*/false,
7580 /*optional_p=*/false));
7581 /* In general, build a SCOPE_REF if the member name is qualified.
7582 However, if the name was not dependent and has already been
7583 resolved; there is no need to build the SCOPE_REF. For example;
7585 struct X { void f(); };
7586 template <typename T> void f(T* t) { t->X::f(); }
7588 Even though "t" is dependent, "X::f" is not and has been resolved
7589 to a BASELINK; there is no need to include scope information. */
7591 /* But we do need to remember that there was an explicit scope for
7592 virtual function calls. */
7593 if (parser->scope)
7594 *idk = CP_ID_KIND_QUALIFIED;
7596 /* If the name is a template-id that names a type, we will get a
7597 TYPE_DECL here. That is invalid code. */
7598 if (TREE_CODE (name) == TYPE_DECL)
7600 error_at (token->location, "invalid use of %qD", name);
7601 postfix_expression = error_mark_node;
7603 else
7605 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7607 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7609 error_at (token->location, "%<%D::%D%> is not a class member",
7610 parser->scope, name);
7611 postfix_expression = error_mark_node;
7613 else
7614 name = build_qualified_name (/*type=*/NULL_TREE,
7615 parser->scope,
7616 name,
7617 template_p);
7618 parser->scope = NULL_TREE;
7619 parser->qualifying_scope = NULL_TREE;
7620 parser->object_scope = NULL_TREE;
7622 if (parser->scope && name && BASELINK_P (name))
7623 adjust_result_of_qualified_name_lookup
7624 (name, parser->scope, scope);
7625 postfix_expression
7626 = finish_class_member_access_expr (postfix_expression, name,
7627 template_p,
7628 tf_warning_or_error);
7629 /* Build a location e.g.:
7630 ptr->access_expr
7631 ~~~^~~~~~~~~~~~~
7632 where the caret is at the deref token, ranging from
7633 the start of postfix_expression to the end of the access expr. */
7634 location_t end_loc
7635 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7636 location_t combined_loc
7637 = make_location (input_location, start_loc, end_loc);
7638 protected_set_expr_location (postfix_expression, combined_loc);
7642 /* We no longer need to look up names in the scope of the object on
7643 the left-hand side of the `.' or `->' operator. */
7644 parser->context->object_type = NULL_TREE;
7646 /* Outside of offsetof, these operators may not appear in
7647 constant-expressions. */
7648 if (!for_offsetof
7649 && (cp_parser_non_integral_constant_expression
7650 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7651 postfix_expression = error_mark_node;
7653 return postfix_expression;
7656 /* Parse a parenthesized expression-list.
7658 expression-list:
7659 assignment-expression
7660 expression-list, assignment-expression
7662 attribute-list:
7663 expression-list
7664 identifier
7665 identifier, expression-list
7667 CAST_P is true if this expression is the target of a cast.
7669 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7670 argument pack.
7672 WRAP_LOCATIONS_P is true if expressions within this list for which
7673 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7674 their source locations.
7676 Returns a vector of trees. Each element is a representation of an
7677 assignment-expression. NULL is returned if the ( and or ) are
7678 missing. An empty, but allocated, vector is returned on no
7679 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7680 if we are parsing an attribute list for an attribute that wants a
7681 plain identifier argument, normal_attr for an attribute that wants
7682 an expression, or non_attr if we aren't parsing an attribute list. If
7683 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7684 not all of the expressions in the list were constant.
7685 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7686 will be written to with the location of the closing parenthesis. If
7687 an error occurs, it may or may not be written to. */
7689 static vec<tree, va_gc> *
7690 cp_parser_parenthesized_expression_list (cp_parser* parser,
7691 int is_attribute_list,
7692 bool cast_p,
7693 bool allow_expansion_p,
7694 bool *non_constant_p,
7695 location_t *close_paren_loc,
7696 bool wrap_locations_p)
7698 vec<tree, va_gc> *expression_list;
7699 bool fold_expr_p = is_attribute_list != non_attr;
7700 tree identifier = NULL_TREE;
7701 bool saved_greater_than_is_operator_p;
7703 /* Assume all the expressions will be constant. */
7704 if (non_constant_p)
7705 *non_constant_p = false;
7707 matching_parens parens;
7708 if (!parens.require_open (parser))
7709 return NULL;
7711 expression_list = make_tree_vector ();
7713 /* Within a parenthesized expression, a `>' token is always
7714 the greater-than operator. */
7715 saved_greater_than_is_operator_p
7716 = parser->greater_than_is_operator_p;
7717 parser->greater_than_is_operator_p = true;
7719 cp_expr expr (NULL_TREE);
7721 /* Consume expressions until there are no more. */
7722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7723 while (true)
7725 /* At the beginning of attribute lists, check to see if the
7726 next token is an identifier. */
7727 if (is_attribute_list == id_attr
7728 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7730 cp_token *token;
7732 /* Consume the identifier. */
7733 token = cp_lexer_consume_token (parser->lexer);
7734 /* Save the identifier. */
7735 identifier = token->u.value;
7737 else
7739 bool expr_non_constant_p;
7741 /* Parse the next assignment-expression. */
7742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7744 /* A braced-init-list. */
7745 cp_lexer_set_source_position (parser->lexer);
7746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7747 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7748 if (non_constant_p && expr_non_constant_p)
7749 *non_constant_p = true;
7751 else if (non_constant_p)
7753 expr = (cp_parser_constant_expression
7754 (parser, /*allow_non_constant_p=*/true,
7755 &expr_non_constant_p));
7756 if (expr_non_constant_p)
7757 *non_constant_p = true;
7759 else
7760 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7761 cast_p);
7763 if (fold_expr_p)
7764 expr = instantiate_non_dependent_expr (expr);
7766 /* If we have an ellipsis, then this is an expression
7767 expansion. */
7768 if (allow_expansion_p
7769 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7771 /* Consume the `...'. */
7772 cp_lexer_consume_token (parser->lexer);
7774 /* Build the argument pack. */
7775 expr = make_pack_expansion (expr);
7778 if (wrap_locations_p)
7779 expr.maybe_add_location_wrapper ();
7781 /* Add it to the list. We add error_mark_node
7782 expressions to the list, so that we can still tell if
7783 the correct form for a parenthesized expression-list
7784 is found. That gives better errors. */
7785 vec_safe_push (expression_list, expr.get_value ());
7787 if (expr == error_mark_node)
7788 goto skip_comma;
7791 /* After the first item, attribute lists look the same as
7792 expression lists. */
7793 is_attribute_list = non_attr;
7795 get_comma:;
7796 /* If the next token isn't a `,', then we are done. */
7797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7798 break;
7800 /* Otherwise, consume the `,' and keep going. */
7801 cp_lexer_consume_token (parser->lexer);
7804 if (close_paren_loc)
7805 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7807 if (!parens.require_close (parser))
7809 int ending;
7811 skip_comma:;
7812 /* We try and resync to an unnested comma, as that will give the
7813 user better diagnostics. */
7814 ending = cp_parser_skip_to_closing_parenthesis (parser,
7815 /*recovering=*/true,
7816 /*or_comma=*/true,
7817 /*consume_paren=*/true);
7818 if (ending < 0)
7819 goto get_comma;
7820 if (!ending)
7822 parser->greater_than_is_operator_p
7823 = saved_greater_than_is_operator_p;
7824 return NULL;
7828 parser->greater_than_is_operator_p
7829 = saved_greater_than_is_operator_p;
7831 if (identifier)
7832 vec_safe_insert (expression_list, 0, identifier);
7834 return expression_list;
7837 /* Parse a pseudo-destructor-name.
7839 pseudo-destructor-name:
7840 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7841 :: [opt] nested-name-specifier template template-id :: ~ type-name
7842 :: [opt] nested-name-specifier [opt] ~ type-name
7844 If either of the first two productions is used, sets *SCOPE to the
7845 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7846 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7847 or ERROR_MARK_NODE if the parse fails. */
7849 static void
7850 cp_parser_pseudo_destructor_name (cp_parser* parser,
7851 tree object,
7852 tree* scope,
7853 tree* type)
7855 bool nested_name_specifier_p;
7857 /* Handle ~auto. */
7858 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7859 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7860 && !type_dependent_expression_p (object))
7862 if (cxx_dialect < cxx14)
7863 pedwarn (input_location, 0,
7864 "%<~auto%> only available with "
7865 "-std=c++14 or -std=gnu++14");
7866 cp_lexer_consume_token (parser->lexer);
7867 cp_lexer_consume_token (parser->lexer);
7868 *scope = NULL_TREE;
7869 *type = TREE_TYPE (object);
7870 return;
7873 /* Assume that things will not work out. */
7874 *type = error_mark_node;
7876 /* Look for the optional `::' operator. */
7877 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7878 /* Look for the optional nested-name-specifier. */
7879 nested_name_specifier_p
7880 = (cp_parser_nested_name_specifier_opt (parser,
7881 /*typename_keyword_p=*/false,
7882 /*check_dependency_p=*/true,
7883 /*type_p=*/false,
7884 /*is_declaration=*/false)
7885 != NULL_TREE);
7886 /* Now, if we saw a nested-name-specifier, we might be doing the
7887 second production. */
7888 if (nested_name_specifier_p
7889 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7891 /* Consume the `template' keyword. */
7892 cp_lexer_consume_token (parser->lexer);
7893 /* Parse the template-id. */
7894 cp_parser_template_id (parser,
7895 /*template_keyword_p=*/true,
7896 /*check_dependency_p=*/false,
7897 class_type,
7898 /*is_declaration=*/true);
7899 /* Look for the `::' token. */
7900 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7902 /* If the next token is not a `~', then there might be some
7903 additional qualification. */
7904 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7906 /* At this point, we're looking for "type-name :: ~". The type-name
7907 must not be a class-name, since this is a pseudo-destructor. So,
7908 it must be either an enum-name, or a typedef-name -- both of which
7909 are just identifiers. So, we peek ahead to check that the "::"
7910 and "~" tokens are present; if they are not, then we can avoid
7911 calling type_name. */
7912 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7913 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7914 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7916 cp_parser_error (parser, "non-scalar type");
7917 return;
7920 /* Look for the type-name. */
7921 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7922 if (*scope == error_mark_node)
7923 return;
7925 /* Look for the `::' token. */
7926 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7928 else
7929 *scope = NULL_TREE;
7931 /* Look for the `~'. */
7932 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7934 /* Once we see the ~, this has to be a pseudo-destructor. */
7935 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7936 cp_parser_commit_to_topmost_tentative_parse (parser);
7938 /* Look for the type-name again. We are not responsible for
7939 checking that it matches the first type-name. */
7940 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7943 /* Parse a unary-expression.
7945 unary-expression:
7946 postfix-expression
7947 ++ cast-expression
7948 -- cast-expression
7949 unary-operator cast-expression
7950 sizeof unary-expression
7951 sizeof ( type-id )
7952 alignof ( type-id ) [C++0x]
7953 new-expression
7954 delete-expression
7956 GNU Extensions:
7958 unary-expression:
7959 __extension__ cast-expression
7960 __alignof__ unary-expression
7961 __alignof__ ( type-id )
7962 alignof unary-expression [C++0x]
7963 __real__ cast-expression
7964 __imag__ cast-expression
7965 && identifier
7966 sizeof ( type-id ) { initializer-list , [opt] }
7967 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7968 __alignof__ ( type-id ) { initializer-list , [opt] }
7970 ADDRESS_P is true iff the unary-expression is appearing as the
7971 operand of the `&' operator. CAST_P is true if this expression is
7972 the target of a cast.
7974 Returns a representation of the expression. */
7976 static cp_expr
7977 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7978 bool address_p, bool cast_p, bool decltype_p)
7980 cp_token *token;
7981 enum tree_code unary_operator;
7983 /* Peek at the next token. */
7984 token = cp_lexer_peek_token (parser->lexer);
7985 /* Some keywords give away the kind of expression. */
7986 if (token->type == CPP_KEYWORD)
7988 enum rid keyword = token->keyword;
7990 switch (keyword)
7992 case RID_ALIGNOF:
7993 case RID_SIZEOF:
7995 tree operand, ret;
7996 enum tree_code op;
7997 location_t start_loc = token->location;
7999 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8000 /* Consume the token. */
8001 cp_lexer_consume_token (parser->lexer);
8002 /* Parse the operand. */
8003 operand = cp_parser_sizeof_operand (parser, keyword);
8005 if (TYPE_P (operand))
8006 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8007 else
8009 /* ISO C++ defines alignof only with types, not with
8010 expressions. So pedwarn if alignof is used with a non-
8011 type expression. However, __alignof__ is ok. */
8012 if (id_equal (token->u.value, "alignof"))
8013 pedwarn (token->location, OPT_Wpedantic,
8014 "ISO C++ does not allow %<alignof%> "
8015 "with a non-type");
8017 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8019 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8020 SIZEOF_EXPR with the original operand. */
8021 if (op == SIZEOF_EXPR && ret != error_mark_node)
8023 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8025 if (!processing_template_decl && TYPE_P (operand))
8027 ret = build_min (SIZEOF_EXPR, size_type_node,
8028 build1 (NOP_EXPR, operand,
8029 error_mark_node));
8030 SIZEOF_EXPR_TYPE_P (ret) = 1;
8032 else
8033 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8034 TREE_SIDE_EFFECTS (ret) = 0;
8035 TREE_READONLY (ret) = 1;
8039 /* Construct a location e.g. :
8040 alignof (expr)
8041 ^~~~~~~~~~~~~~
8042 with start == caret at the start of the "alignof"/"sizeof"
8043 token, with the endpoint at the final closing paren. */
8044 location_t finish_loc
8045 = cp_lexer_previous_token (parser->lexer)->location;
8046 location_t compound_loc
8047 = make_location (start_loc, start_loc, finish_loc);
8049 cp_expr ret_expr (ret);
8050 ret_expr.set_location (compound_loc);
8051 ret_expr = ret_expr.maybe_add_location_wrapper ();
8052 return ret_expr;
8055 case RID_NEW:
8056 return cp_parser_new_expression (parser);
8058 case RID_DELETE:
8059 return cp_parser_delete_expression (parser);
8061 case RID_EXTENSION:
8063 /* The saved value of the PEDANTIC flag. */
8064 int saved_pedantic;
8065 tree expr;
8067 /* Save away the PEDANTIC flag. */
8068 cp_parser_extension_opt (parser, &saved_pedantic);
8069 /* Parse the cast-expression. */
8070 expr = cp_parser_simple_cast_expression (parser);
8071 /* Restore the PEDANTIC flag. */
8072 pedantic = saved_pedantic;
8074 return expr;
8077 case RID_REALPART:
8078 case RID_IMAGPART:
8080 tree expression;
8082 /* Consume the `__real__' or `__imag__' token. */
8083 cp_lexer_consume_token (parser->lexer);
8084 /* Parse the cast-expression. */
8085 expression = cp_parser_simple_cast_expression (parser);
8086 /* Create the complete representation. */
8087 return build_x_unary_op (token->location,
8088 (keyword == RID_REALPART
8089 ? REALPART_EXPR : IMAGPART_EXPR),
8090 expression,
8091 tf_warning_or_error);
8093 break;
8095 case RID_TRANSACTION_ATOMIC:
8096 case RID_TRANSACTION_RELAXED:
8097 return cp_parser_transaction_expression (parser, keyword);
8099 case RID_NOEXCEPT:
8101 tree expr;
8102 const char *saved_message;
8103 bool saved_integral_constant_expression_p;
8104 bool saved_non_integral_constant_expression_p;
8105 bool saved_greater_than_is_operator_p;
8107 location_t start_loc = token->location;
8109 cp_lexer_consume_token (parser->lexer);
8110 matching_parens parens;
8111 parens.require_open (parser);
8113 saved_message = parser->type_definition_forbidden_message;
8114 parser->type_definition_forbidden_message
8115 = G_("types may not be defined in %<noexcept%> expressions");
8117 saved_integral_constant_expression_p
8118 = parser->integral_constant_expression_p;
8119 saved_non_integral_constant_expression_p
8120 = parser->non_integral_constant_expression_p;
8121 parser->integral_constant_expression_p = false;
8123 saved_greater_than_is_operator_p
8124 = parser->greater_than_is_operator_p;
8125 parser->greater_than_is_operator_p = true;
8127 ++cp_unevaluated_operand;
8128 ++c_inhibit_evaluation_warnings;
8129 ++cp_noexcept_operand;
8130 expr = cp_parser_expression (parser);
8131 --cp_noexcept_operand;
8132 --c_inhibit_evaluation_warnings;
8133 --cp_unevaluated_operand;
8135 parser->greater_than_is_operator_p
8136 = saved_greater_than_is_operator_p;
8138 parser->integral_constant_expression_p
8139 = saved_integral_constant_expression_p;
8140 parser->non_integral_constant_expression_p
8141 = saved_non_integral_constant_expression_p;
8143 parser->type_definition_forbidden_message = saved_message;
8145 location_t finish_loc
8146 = cp_lexer_peek_token (parser->lexer)->location;
8147 parens.require_close (parser);
8149 /* Construct a location of the form:
8150 noexcept (expr)
8151 ^~~~~~~~~~~~~~~
8152 with start == caret, finishing at the close-paren. */
8153 location_t noexcept_loc
8154 = make_location (start_loc, start_loc, finish_loc);
8156 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8157 noexcept_loc);
8160 default:
8161 break;
8165 /* Look for the `:: new' and `:: delete', which also signal the
8166 beginning of a new-expression, or delete-expression,
8167 respectively. If the next token is `::', then it might be one of
8168 these. */
8169 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8171 enum rid keyword;
8173 /* See if the token after the `::' is one of the keywords in
8174 which we're interested. */
8175 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8176 /* If it's `new', we have a new-expression. */
8177 if (keyword == RID_NEW)
8178 return cp_parser_new_expression (parser);
8179 /* Similarly, for `delete'. */
8180 else if (keyword == RID_DELETE)
8181 return cp_parser_delete_expression (parser);
8184 /* Look for a unary operator. */
8185 unary_operator = cp_parser_unary_operator (token);
8186 /* The `++' and `--' operators can be handled similarly, even though
8187 they are not technically unary-operators in the grammar. */
8188 if (unary_operator == ERROR_MARK)
8190 if (token->type == CPP_PLUS_PLUS)
8191 unary_operator = PREINCREMENT_EXPR;
8192 else if (token->type == CPP_MINUS_MINUS)
8193 unary_operator = PREDECREMENT_EXPR;
8194 /* Handle the GNU address-of-label extension. */
8195 else if (cp_parser_allow_gnu_extensions_p (parser)
8196 && token->type == CPP_AND_AND)
8198 tree identifier;
8199 tree expression;
8200 location_t start_loc = token->location;
8202 /* Consume the '&&' token. */
8203 cp_lexer_consume_token (parser->lexer);
8204 /* Look for the identifier. */
8205 location_t finish_loc
8206 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8207 identifier = cp_parser_identifier (parser);
8208 /* Construct a location of the form:
8209 &&label
8210 ^~~~~~~
8211 with caret==start at the "&&", finish at the end of the label. */
8212 location_t combined_loc
8213 = make_location (start_loc, start_loc, finish_loc);
8214 /* Create an expression representing the address. */
8215 expression = finish_label_address_expr (identifier, combined_loc);
8216 if (cp_parser_non_integral_constant_expression (parser,
8217 NIC_ADDR_LABEL))
8218 expression = error_mark_node;
8219 return expression;
8222 if (unary_operator != ERROR_MARK)
8224 cp_expr cast_expression;
8225 cp_expr expression = error_mark_node;
8226 non_integral_constant non_constant_p = NIC_NONE;
8227 location_t loc = token->location;
8228 tsubst_flags_t complain = complain_flags (decltype_p);
8230 /* Consume the operator token. */
8231 token = cp_lexer_consume_token (parser->lexer);
8232 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8234 /* Parse the cast-expression. */
8235 cast_expression
8236 = cp_parser_cast_expression (parser,
8237 unary_operator == ADDR_EXPR,
8238 /*cast_p=*/false,
8239 /*decltype*/false,
8240 pidk);
8242 /* Make a location:
8243 OP_TOKEN CAST_EXPRESSION
8244 ^~~~~~~~~~~~~~~~~~~~~~~~~
8245 with start==caret at the operator token, and
8246 extending to the end of the cast_expression. */
8247 loc = make_location (loc, loc, cast_expression.get_finish ());
8249 /* Now, build an appropriate representation. */
8250 switch (unary_operator)
8252 case INDIRECT_REF:
8253 non_constant_p = NIC_STAR;
8254 expression = build_x_indirect_ref (loc, cast_expression,
8255 RO_UNARY_STAR,
8256 complain);
8257 /* TODO: build_x_indirect_ref does not always honor the
8258 location, so ensure it is set. */
8259 expression.set_location (loc);
8260 break;
8262 case ADDR_EXPR:
8263 non_constant_p = NIC_ADDR;
8264 /* Fall through. */
8265 case BIT_NOT_EXPR:
8266 expression = build_x_unary_op (loc, unary_operator,
8267 cast_expression,
8268 complain);
8269 /* TODO: build_x_unary_op does not always honor the location,
8270 so ensure it is set. */
8271 expression.set_location (loc);
8272 break;
8274 case PREINCREMENT_EXPR:
8275 case PREDECREMENT_EXPR:
8276 non_constant_p = unary_operator == PREINCREMENT_EXPR
8277 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8278 /* Fall through. */
8279 case NEGATE_EXPR:
8280 /* Immediately fold negation of a constant, unless the constant is 0
8281 (since -0 == 0) or it would overflow. */
8282 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8283 && CONSTANT_CLASS_P (cast_expression)
8284 && !integer_zerop (cast_expression)
8285 && !TREE_OVERFLOW (cast_expression))
8287 tree folded = fold_build1 (unary_operator,
8288 TREE_TYPE (cast_expression),
8289 cast_expression);
8290 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8292 expression = cp_expr (folded, loc);
8293 break;
8296 /* Fall through. */
8297 case UNARY_PLUS_EXPR:
8298 case TRUTH_NOT_EXPR:
8299 expression = finish_unary_op_expr (loc, unary_operator,
8300 cast_expression, complain);
8301 break;
8303 default:
8304 gcc_unreachable ();
8307 if (non_constant_p != NIC_NONE
8308 && cp_parser_non_integral_constant_expression (parser,
8309 non_constant_p))
8310 expression = error_mark_node;
8312 return expression;
8315 return cp_parser_postfix_expression (parser, address_p, cast_p,
8316 /*member_access_only_p=*/false,
8317 decltype_p,
8318 pidk);
8321 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8322 unary-operator, the corresponding tree code is returned. */
8324 static enum tree_code
8325 cp_parser_unary_operator (cp_token* token)
8327 switch (token->type)
8329 case CPP_MULT:
8330 return INDIRECT_REF;
8332 case CPP_AND:
8333 return ADDR_EXPR;
8335 case CPP_PLUS:
8336 return UNARY_PLUS_EXPR;
8338 case CPP_MINUS:
8339 return NEGATE_EXPR;
8341 case CPP_NOT:
8342 return TRUTH_NOT_EXPR;
8344 case CPP_COMPL:
8345 return BIT_NOT_EXPR;
8347 default:
8348 return ERROR_MARK;
8352 /* Parse a new-expression.
8354 new-expression:
8355 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8356 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8358 Returns a representation of the expression. */
8360 static tree
8361 cp_parser_new_expression (cp_parser* parser)
8363 bool global_scope_p;
8364 vec<tree, va_gc> *placement;
8365 tree type;
8366 vec<tree, va_gc> *initializer;
8367 tree nelts = NULL_TREE;
8368 tree ret;
8370 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8372 /* Look for the optional `::' operator. */
8373 global_scope_p
8374 = (cp_parser_global_scope_opt (parser,
8375 /*current_scope_valid_p=*/false)
8376 != NULL_TREE);
8377 /* Look for the `new' operator. */
8378 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8379 /* There's no easy way to tell a new-placement from the
8380 `( type-id )' construct. */
8381 cp_parser_parse_tentatively (parser);
8382 /* Look for a new-placement. */
8383 placement = cp_parser_new_placement (parser);
8384 /* If that didn't work out, there's no new-placement. */
8385 if (!cp_parser_parse_definitely (parser))
8387 if (placement != NULL)
8388 release_tree_vector (placement);
8389 placement = NULL;
8392 /* If the next token is a `(', then we have a parenthesized
8393 type-id. */
8394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8396 cp_token *token;
8397 const char *saved_message = parser->type_definition_forbidden_message;
8399 /* Consume the `('. */
8400 matching_parens parens;
8401 parens.consume_open (parser);
8403 /* Parse the type-id. */
8404 parser->type_definition_forbidden_message
8405 = G_("types may not be defined in a new-expression");
8407 type_id_in_expr_sentinel s (parser);
8408 type = cp_parser_type_id (parser);
8410 parser->type_definition_forbidden_message = saved_message;
8412 /* Look for the closing `)'. */
8413 parens.require_close (parser);
8414 token = cp_lexer_peek_token (parser->lexer);
8415 /* There should not be a direct-new-declarator in this production,
8416 but GCC used to allowed this, so we check and emit a sensible error
8417 message for this case. */
8418 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8420 error_at (token->location,
8421 "array bound forbidden after parenthesized type-id");
8422 inform (token->location,
8423 "try removing the parentheses around the type-id");
8424 cp_parser_direct_new_declarator (parser);
8427 /* Otherwise, there must be a new-type-id. */
8428 else
8429 type = cp_parser_new_type_id (parser, &nelts);
8431 /* If the next token is a `(' or '{', then we have a new-initializer. */
8432 cp_token *token = cp_lexer_peek_token (parser->lexer);
8433 if (token->type == CPP_OPEN_PAREN
8434 || token->type == CPP_OPEN_BRACE)
8435 initializer = cp_parser_new_initializer (parser);
8436 else
8437 initializer = NULL;
8439 /* A new-expression may not appear in an integral constant
8440 expression. */
8441 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8442 ret = error_mark_node;
8443 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8444 of a new-type-id or type-id of a new-expression, the new-expression shall
8445 contain a new-initializer of the form ( assignment-expression )".
8446 Additionally, consistently with the spirit of DR 1467, we want to accept
8447 'new auto { 2 }' too. */
8448 else if ((ret = type_uses_auto (type))
8449 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8450 && (vec_safe_length (initializer) != 1
8451 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8452 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8454 error_at (token->location,
8455 "initialization of new-expression for type %<auto%> "
8456 "requires exactly one element");
8457 ret = error_mark_node;
8459 else
8461 /* Construct a location e.g.:
8462 ptr = new int[100]
8463 ^~~~~~~~~~~~
8464 with caret == start at the start of the "new" token, and the end
8465 at the end of the final token we consumed. */
8466 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8467 location_t end_loc = get_finish (end_tok->location);
8468 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8470 /* Create a representation of the new-expression. */
8471 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8472 tf_warning_or_error);
8473 protected_set_expr_location (ret, combined_loc);
8476 if (placement != NULL)
8477 release_tree_vector (placement);
8478 if (initializer != NULL)
8479 release_tree_vector (initializer);
8481 return ret;
8484 /* Parse a new-placement.
8486 new-placement:
8487 ( expression-list )
8489 Returns the same representation as for an expression-list. */
8491 static vec<tree, va_gc> *
8492 cp_parser_new_placement (cp_parser* parser)
8494 vec<tree, va_gc> *expression_list;
8496 /* Parse the expression-list. */
8497 expression_list = (cp_parser_parenthesized_expression_list
8498 (parser, non_attr, /*cast_p=*/false,
8499 /*allow_expansion_p=*/true,
8500 /*non_constant_p=*/NULL));
8502 if (expression_list && expression_list->is_empty ())
8503 error ("expected expression-list or type-id");
8505 return expression_list;
8508 /* Parse a new-type-id.
8510 new-type-id:
8511 type-specifier-seq new-declarator [opt]
8513 Returns the TYPE allocated. If the new-type-id indicates an array
8514 type, *NELTS is set to the number of elements in the last array
8515 bound; the TYPE will not include the last array bound. */
8517 static tree
8518 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8520 cp_decl_specifier_seq type_specifier_seq;
8521 cp_declarator *new_declarator;
8522 cp_declarator *declarator;
8523 cp_declarator *outer_declarator;
8524 const char *saved_message;
8526 /* The type-specifier sequence must not contain type definitions.
8527 (It cannot contain declarations of new types either, but if they
8528 are not definitions we will catch that because they are not
8529 complete.) */
8530 saved_message = parser->type_definition_forbidden_message;
8531 parser->type_definition_forbidden_message
8532 = G_("types may not be defined in a new-type-id");
8533 /* Parse the type-specifier-seq. */
8534 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8535 /*is_trailing_return=*/false,
8536 &type_specifier_seq);
8537 /* Restore the old message. */
8538 parser->type_definition_forbidden_message = saved_message;
8540 if (type_specifier_seq.type == error_mark_node)
8541 return error_mark_node;
8543 /* Parse the new-declarator. */
8544 new_declarator = cp_parser_new_declarator_opt (parser);
8546 /* Determine the number of elements in the last array dimension, if
8547 any. */
8548 *nelts = NULL_TREE;
8549 /* Skip down to the last array dimension. */
8550 declarator = new_declarator;
8551 outer_declarator = NULL;
8552 while (declarator && (declarator->kind == cdk_pointer
8553 || declarator->kind == cdk_ptrmem))
8555 outer_declarator = declarator;
8556 declarator = declarator->declarator;
8558 while (declarator
8559 && declarator->kind == cdk_array
8560 && declarator->declarator
8561 && declarator->declarator->kind == cdk_array)
8563 outer_declarator = declarator;
8564 declarator = declarator->declarator;
8567 if (declarator && declarator->kind == cdk_array)
8569 *nelts = declarator->u.array.bounds;
8570 if (*nelts == error_mark_node)
8571 *nelts = integer_one_node;
8573 if (outer_declarator)
8574 outer_declarator->declarator = declarator->declarator;
8575 else
8576 new_declarator = NULL;
8579 return groktypename (&type_specifier_seq, new_declarator, false);
8582 /* Parse an (optional) new-declarator.
8584 new-declarator:
8585 ptr-operator new-declarator [opt]
8586 direct-new-declarator
8588 Returns the declarator. */
8590 static cp_declarator *
8591 cp_parser_new_declarator_opt (cp_parser* parser)
8593 enum tree_code code;
8594 tree type, std_attributes = NULL_TREE;
8595 cp_cv_quals cv_quals;
8597 /* We don't know if there's a ptr-operator next, or not. */
8598 cp_parser_parse_tentatively (parser);
8599 /* Look for a ptr-operator. */
8600 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8601 /* If that worked, look for more new-declarators. */
8602 if (cp_parser_parse_definitely (parser))
8604 cp_declarator *declarator;
8606 /* Parse another optional declarator. */
8607 declarator = cp_parser_new_declarator_opt (parser);
8609 declarator = cp_parser_make_indirect_declarator
8610 (code, type, cv_quals, declarator, std_attributes);
8612 return declarator;
8615 /* If the next token is a `[', there is a direct-new-declarator. */
8616 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8617 return cp_parser_direct_new_declarator (parser);
8619 return NULL;
8622 /* Parse a direct-new-declarator.
8624 direct-new-declarator:
8625 [ expression ]
8626 direct-new-declarator [constant-expression]
8630 static cp_declarator *
8631 cp_parser_direct_new_declarator (cp_parser* parser)
8633 cp_declarator *declarator = NULL;
8635 while (true)
8637 tree expression;
8638 cp_token *token;
8640 /* Look for the opening `['. */
8641 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8643 token = cp_lexer_peek_token (parser->lexer);
8644 expression = cp_parser_expression (parser);
8645 /* The standard requires that the expression have integral
8646 type. DR 74 adds enumeration types. We believe that the
8647 real intent is that these expressions be handled like the
8648 expression in a `switch' condition, which also allows
8649 classes with a single conversion to integral or
8650 enumeration type. */
8651 if (!processing_template_decl)
8653 expression
8654 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8655 expression,
8656 /*complain=*/true);
8657 if (!expression)
8659 error_at (token->location,
8660 "expression in new-declarator must have integral "
8661 "or enumeration type");
8662 expression = error_mark_node;
8666 /* Look for the closing `]'. */
8667 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8669 /* Add this bound to the declarator. */
8670 declarator = make_array_declarator (declarator, expression);
8672 /* If the next token is not a `[', then there are no more
8673 bounds. */
8674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8675 break;
8678 return declarator;
8681 /* Parse a new-initializer.
8683 new-initializer:
8684 ( expression-list [opt] )
8685 braced-init-list
8687 Returns a representation of the expression-list. */
8689 static vec<tree, va_gc> *
8690 cp_parser_new_initializer (cp_parser* parser)
8692 vec<tree, va_gc> *expression_list;
8694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8696 tree t;
8697 bool expr_non_constant_p;
8698 cp_lexer_set_source_position (parser->lexer);
8699 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8700 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8701 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8702 expression_list = make_tree_vector_single (t);
8704 else
8705 expression_list = (cp_parser_parenthesized_expression_list
8706 (parser, non_attr, /*cast_p=*/false,
8707 /*allow_expansion_p=*/true,
8708 /*non_constant_p=*/NULL));
8710 return expression_list;
8713 /* Parse a delete-expression.
8715 delete-expression:
8716 :: [opt] delete cast-expression
8717 :: [opt] delete [ ] cast-expression
8719 Returns a representation of the expression. */
8721 static tree
8722 cp_parser_delete_expression (cp_parser* parser)
8724 bool global_scope_p;
8725 bool array_p;
8726 tree expression;
8728 /* Look for the optional `::' operator. */
8729 global_scope_p
8730 = (cp_parser_global_scope_opt (parser,
8731 /*current_scope_valid_p=*/false)
8732 != NULL_TREE);
8733 /* Look for the `delete' keyword. */
8734 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8735 /* See if the array syntax is in use. */
8736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8738 /* Consume the `[' token. */
8739 cp_lexer_consume_token (parser->lexer);
8740 /* Look for the `]' token. */
8741 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8742 /* Remember that this is the `[]' construct. */
8743 array_p = true;
8745 else
8746 array_p = false;
8748 /* Parse the cast-expression. */
8749 expression = cp_parser_simple_cast_expression (parser);
8751 /* A delete-expression may not appear in an integral constant
8752 expression. */
8753 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8754 return error_mark_node;
8756 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8757 tf_warning_or_error);
8760 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8761 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8762 0 otherwise. */
8764 static int
8765 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8767 cp_token *token = cp_lexer_peek_token (parser->lexer);
8768 switch (token->type)
8770 case CPP_COMMA:
8771 case CPP_SEMICOLON:
8772 case CPP_QUERY:
8773 case CPP_COLON:
8774 case CPP_CLOSE_SQUARE:
8775 case CPP_CLOSE_PAREN:
8776 case CPP_CLOSE_BRACE:
8777 case CPP_OPEN_BRACE:
8778 case CPP_DOT:
8779 case CPP_DOT_STAR:
8780 case CPP_DEREF:
8781 case CPP_DEREF_STAR:
8782 case CPP_DIV:
8783 case CPP_MOD:
8784 case CPP_LSHIFT:
8785 case CPP_RSHIFT:
8786 case CPP_LESS:
8787 case CPP_GREATER:
8788 case CPP_LESS_EQ:
8789 case CPP_GREATER_EQ:
8790 case CPP_EQ_EQ:
8791 case CPP_NOT_EQ:
8792 case CPP_EQ:
8793 case CPP_MULT_EQ:
8794 case CPP_DIV_EQ:
8795 case CPP_MOD_EQ:
8796 case CPP_PLUS_EQ:
8797 case CPP_MINUS_EQ:
8798 case CPP_RSHIFT_EQ:
8799 case CPP_LSHIFT_EQ:
8800 case CPP_AND_EQ:
8801 case CPP_XOR_EQ:
8802 case CPP_OR_EQ:
8803 case CPP_XOR:
8804 case CPP_OR:
8805 case CPP_OR_OR:
8806 case CPP_EOF:
8807 case CPP_ELLIPSIS:
8808 return 0;
8810 case CPP_OPEN_PAREN:
8811 /* In ((type ()) () the last () isn't a valid cast-expression,
8812 so the whole must be parsed as postfix-expression. */
8813 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8814 != CPP_CLOSE_PAREN;
8816 case CPP_OPEN_SQUARE:
8817 /* '[' may start a primary-expression in obj-c++ and in C++11,
8818 as a lambda-expression, eg, '(void)[]{}'. */
8819 if (cxx_dialect >= cxx11)
8820 return -1;
8821 return c_dialect_objc ();
8823 case CPP_PLUS_PLUS:
8824 case CPP_MINUS_MINUS:
8825 /* '++' and '--' may or may not start a cast-expression:
8827 struct T { void operator++(int); };
8828 void f() { (T())++; }
8832 int a;
8833 (int)++a; */
8834 return -1;
8836 default:
8837 return 1;
8841 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8842 in the order: const_cast, static_cast, reinterpret_cast.
8844 Don't suggest dynamic_cast.
8846 Return the first legal cast kind found, or NULL otherwise. */
8848 static const char *
8849 get_cast_suggestion (tree dst_type, tree orig_expr)
8851 tree trial;
8853 /* Reuse the parser logic by attempting to build the various kinds of
8854 cast, with "complain" disabled.
8855 Identify the first such cast that is valid. */
8857 /* Don't attempt to run such logic within template processing. */
8858 if (processing_template_decl)
8859 return NULL;
8861 /* First try const_cast. */
8862 trial = build_const_cast (dst_type, orig_expr, tf_none);
8863 if (trial != error_mark_node)
8864 return "const_cast";
8866 /* If that fails, try static_cast. */
8867 trial = build_static_cast (dst_type, orig_expr, tf_none);
8868 if (trial != error_mark_node)
8869 return "static_cast";
8871 /* Finally, try reinterpret_cast. */
8872 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8873 if (trial != error_mark_node)
8874 return "reinterpret_cast";
8876 /* No such cast possible. */
8877 return NULL;
8880 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8881 suggesting how to convert a C-style cast of the form:
8883 (DST_TYPE)ORIG_EXPR
8885 to a C++-style cast.
8887 The primary range of RICHLOC is asssumed to be that of the original
8888 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8889 of the parens in the C-style cast. */
8891 static void
8892 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8893 location_t close_paren_loc, tree orig_expr,
8894 tree dst_type)
8896 /* This function is non-trivial, so bail out now if the warning isn't
8897 going to be emitted. */
8898 if (!warn_old_style_cast)
8899 return;
8901 /* Try to find a legal C++ cast, trying them in order:
8902 const_cast, static_cast, reinterpret_cast. */
8903 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8904 if (!cast_suggestion)
8905 return;
8907 /* Replace the open paren with "CAST_SUGGESTION<". */
8908 pretty_printer pp;
8909 pp_printf (&pp, "%s<", cast_suggestion);
8910 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8912 /* Replace the close paren with "> (". */
8913 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8915 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8916 rich_loc->add_fixit_insert_after (")");
8920 /* Parse a cast-expression.
8922 cast-expression:
8923 unary-expression
8924 ( type-id ) cast-expression
8926 ADDRESS_P is true iff the unary-expression is appearing as the
8927 operand of the `&' operator. CAST_P is true if this expression is
8928 the target of a cast.
8930 Returns a representation of the expression. */
8932 static cp_expr
8933 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8934 bool decltype_p, cp_id_kind * pidk)
8936 /* If it's a `(', then we might be looking at a cast. */
8937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8939 tree type = NULL_TREE;
8940 cp_expr expr (NULL_TREE);
8941 int cast_expression = 0;
8942 const char *saved_message;
8944 /* There's no way to know yet whether or not this is a cast.
8945 For example, `(int (3))' is a unary-expression, while `(int)
8946 3' is a cast. So, we resort to parsing tentatively. */
8947 cp_parser_parse_tentatively (parser);
8948 /* Types may not be defined in a cast. */
8949 saved_message = parser->type_definition_forbidden_message;
8950 parser->type_definition_forbidden_message
8951 = G_("types may not be defined in casts");
8952 /* Consume the `('. */
8953 matching_parens parens;
8954 cp_token *open_paren = parens.consume_open (parser);
8955 location_t open_paren_loc = open_paren->location;
8956 location_t close_paren_loc = UNKNOWN_LOCATION;
8958 /* A very tricky bit is that `(struct S) { 3 }' is a
8959 compound-literal (which we permit in C++ as an extension).
8960 But, that construct is not a cast-expression -- it is a
8961 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8962 is legal; if the compound-literal were a cast-expression,
8963 you'd need an extra set of parentheses.) But, if we parse
8964 the type-id, and it happens to be a class-specifier, then we
8965 will commit to the parse at that point, because we cannot
8966 undo the action that is done when creating a new class. So,
8967 then we cannot back up and do a postfix-expression.
8969 Another tricky case is the following (c++/29234):
8971 struct S { void operator () (); };
8973 void foo ()
8975 ( S()() );
8978 As a type-id we parse the parenthesized S()() as a function
8979 returning a function, groktypename complains and we cannot
8980 back up in this case either.
8982 Therefore, we scan ahead to the closing `)', and check to see
8983 if the tokens after the `)' can start a cast-expression. Otherwise
8984 we are dealing with an unary-expression, a postfix-expression
8985 or something else.
8987 Yet another tricky case, in C++11, is the following (c++/54891):
8989 (void)[]{};
8991 The issue is that usually, besides the case of lambda-expressions,
8992 the parenthesized type-id cannot be followed by '[', and, eg, we
8993 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8994 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8995 we don't commit, we try a cast-expression, then an unary-expression.
8997 Save tokens so that we can put them back. */
8998 cp_lexer_save_tokens (parser->lexer);
9000 /* We may be looking at a cast-expression. */
9001 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9002 /*consume_paren=*/true))
9003 cast_expression
9004 = cp_parser_tokens_start_cast_expression (parser);
9006 /* Roll back the tokens we skipped. */
9007 cp_lexer_rollback_tokens (parser->lexer);
9008 /* If we aren't looking at a cast-expression, simulate an error so
9009 that the call to cp_parser_error_occurred below returns true. */
9010 if (!cast_expression)
9011 cp_parser_simulate_error (parser);
9012 else
9014 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9015 parser->in_type_id_in_expr_p = true;
9016 /* Look for the type-id. */
9017 type = cp_parser_type_id (parser);
9018 /* Look for the closing `)'. */
9019 cp_token *close_paren = parens.require_close (parser);
9020 if (close_paren)
9021 close_paren_loc = close_paren->location;
9022 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9025 /* Restore the saved message. */
9026 parser->type_definition_forbidden_message = saved_message;
9028 /* At this point this can only be either a cast or a
9029 parenthesized ctor such as `(T ())' that looks like a cast to
9030 function returning T. */
9031 if (!cp_parser_error_occurred (parser))
9033 /* Only commit if the cast-expression doesn't start with
9034 '++', '--', or '[' in C++11. */
9035 if (cast_expression > 0)
9036 cp_parser_commit_to_topmost_tentative_parse (parser);
9038 expr = cp_parser_cast_expression (parser,
9039 /*address_p=*/false,
9040 /*cast_p=*/true,
9041 /*decltype_p=*/false,
9042 pidk);
9044 if (cp_parser_parse_definitely (parser))
9046 /* Warn about old-style casts, if so requested. */
9047 if (warn_old_style_cast
9048 && !in_system_header_at (input_location)
9049 && !VOID_TYPE_P (type)
9050 && current_lang_name != lang_name_c)
9052 gcc_rich_location rich_loc (input_location);
9053 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9054 expr, type);
9055 warning_at (&rich_loc, OPT_Wold_style_cast,
9056 "use of old-style cast to %q#T", type);
9059 /* Only type conversions to integral or enumeration types
9060 can be used in constant-expressions. */
9061 if (!cast_valid_in_integral_constant_expression_p (type)
9062 && cp_parser_non_integral_constant_expression (parser,
9063 NIC_CAST))
9064 return error_mark_node;
9066 /* Perform the cast. */
9067 /* Make a location:
9068 (TYPE) EXPR
9069 ^~~~~~~~~~~
9070 with start==caret at the open paren, extending to the
9071 end of "expr". */
9072 location_t cast_loc = make_location (open_paren_loc,
9073 open_paren_loc,
9074 expr.get_finish ());
9075 expr = build_c_cast (cast_loc, type, expr);
9076 return expr;
9079 else
9080 cp_parser_abort_tentative_parse (parser);
9083 /* If we get here, then it's not a cast, so it must be a
9084 unary-expression. */
9085 return cp_parser_unary_expression (parser, pidk, address_p,
9086 cast_p, decltype_p);
9089 /* Parse a binary expression of the general form:
9091 pm-expression:
9092 cast-expression
9093 pm-expression .* cast-expression
9094 pm-expression ->* cast-expression
9096 multiplicative-expression:
9097 pm-expression
9098 multiplicative-expression * pm-expression
9099 multiplicative-expression / pm-expression
9100 multiplicative-expression % pm-expression
9102 additive-expression:
9103 multiplicative-expression
9104 additive-expression + multiplicative-expression
9105 additive-expression - multiplicative-expression
9107 shift-expression:
9108 additive-expression
9109 shift-expression << additive-expression
9110 shift-expression >> additive-expression
9112 relational-expression:
9113 shift-expression
9114 relational-expression < shift-expression
9115 relational-expression > shift-expression
9116 relational-expression <= shift-expression
9117 relational-expression >= shift-expression
9119 GNU Extension:
9121 relational-expression:
9122 relational-expression <? shift-expression
9123 relational-expression >? shift-expression
9125 equality-expression:
9126 relational-expression
9127 equality-expression == relational-expression
9128 equality-expression != relational-expression
9130 and-expression:
9131 equality-expression
9132 and-expression & equality-expression
9134 exclusive-or-expression:
9135 and-expression
9136 exclusive-or-expression ^ and-expression
9138 inclusive-or-expression:
9139 exclusive-or-expression
9140 inclusive-or-expression | exclusive-or-expression
9142 logical-and-expression:
9143 inclusive-or-expression
9144 logical-and-expression && inclusive-or-expression
9146 logical-or-expression:
9147 logical-and-expression
9148 logical-or-expression || logical-and-expression
9150 All these are implemented with a single function like:
9152 binary-expression:
9153 simple-cast-expression
9154 binary-expression <token> binary-expression
9156 CAST_P is true if this expression is the target of a cast.
9158 The binops_by_token map is used to get the tree codes for each <token> type.
9159 binary-expressions are associated according to a precedence table. */
9161 #define TOKEN_PRECEDENCE(token) \
9162 (((token->type == CPP_GREATER \
9163 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9164 && !parser->greater_than_is_operator_p) \
9165 ? PREC_NOT_OPERATOR \
9166 : binops_by_token[token->type].prec)
9168 static cp_expr
9169 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9170 bool no_toplevel_fold_p,
9171 bool decltype_p,
9172 enum cp_parser_prec prec,
9173 cp_id_kind * pidk)
9175 cp_parser_expression_stack stack;
9176 cp_parser_expression_stack_entry *sp = &stack[0];
9177 cp_parser_expression_stack_entry current;
9178 cp_expr rhs;
9179 cp_token *token;
9180 enum tree_code rhs_type;
9181 enum cp_parser_prec new_prec, lookahead_prec;
9182 tree overload;
9184 /* Parse the first expression. */
9185 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9186 ? TRUTH_NOT_EXPR : ERROR_MARK);
9187 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9188 cast_p, decltype_p, pidk);
9189 current.prec = prec;
9191 if (cp_parser_error_occurred (parser))
9192 return error_mark_node;
9194 for (;;)
9196 /* Get an operator token. */
9197 token = cp_lexer_peek_token (parser->lexer);
9199 if (warn_cxx11_compat
9200 && token->type == CPP_RSHIFT
9201 && !parser->greater_than_is_operator_p)
9203 if (warning_at (token->location, OPT_Wc__11_compat,
9204 "%<>>%> operator is treated"
9205 " as two right angle brackets in C++11"))
9206 inform (token->location,
9207 "suggest parentheses around %<>>%> expression");
9210 new_prec = TOKEN_PRECEDENCE (token);
9211 if (new_prec != PREC_NOT_OPERATOR
9212 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9213 /* This is a fold-expression; handle it later. */
9214 new_prec = PREC_NOT_OPERATOR;
9216 /* Popping an entry off the stack means we completed a subexpression:
9217 - either we found a token which is not an operator (`>' where it is not
9218 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9219 will happen repeatedly;
9220 - or, we found an operator which has lower priority. This is the case
9221 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9222 parsing `3 * 4'. */
9223 if (new_prec <= current.prec)
9225 if (sp == stack)
9226 break;
9227 else
9228 goto pop;
9231 get_rhs:
9232 current.tree_type = binops_by_token[token->type].tree_type;
9233 current.loc = token->location;
9235 /* We used the operator token. */
9236 cp_lexer_consume_token (parser->lexer);
9238 /* For "false && x" or "true || x", x will never be executed;
9239 disable warnings while evaluating it. */
9240 if (current.tree_type == TRUTH_ANDIF_EXPR)
9241 c_inhibit_evaluation_warnings +=
9242 cp_fully_fold (current.lhs) == truthvalue_false_node;
9243 else if (current.tree_type == TRUTH_ORIF_EXPR)
9244 c_inhibit_evaluation_warnings +=
9245 cp_fully_fold (current.lhs) == truthvalue_true_node;
9247 /* Extract another operand. It may be the RHS of this expression
9248 or the LHS of a new, higher priority expression. */
9249 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9250 ? TRUTH_NOT_EXPR : ERROR_MARK);
9251 rhs = cp_parser_simple_cast_expression (parser);
9253 /* Get another operator token. Look up its precedence to avoid
9254 building a useless (immediately popped) stack entry for common
9255 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9256 token = cp_lexer_peek_token (parser->lexer);
9257 lookahead_prec = TOKEN_PRECEDENCE (token);
9258 if (lookahead_prec != PREC_NOT_OPERATOR
9259 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9260 lookahead_prec = PREC_NOT_OPERATOR;
9261 if (lookahead_prec > new_prec)
9263 /* ... and prepare to parse the RHS of the new, higher priority
9264 expression. Since precedence levels on the stack are
9265 monotonically increasing, we do not have to care about
9266 stack overflows. */
9267 *sp = current;
9268 ++sp;
9269 current.lhs = rhs;
9270 current.lhs_type = rhs_type;
9271 current.prec = new_prec;
9272 new_prec = lookahead_prec;
9273 goto get_rhs;
9275 pop:
9276 lookahead_prec = new_prec;
9277 /* If the stack is not empty, we have parsed into LHS the right side
9278 (`4' in the example above) of an expression we had suspended.
9279 We can use the information on the stack to recover the LHS (`3')
9280 from the stack together with the tree code (`MULT_EXPR'), and
9281 the precedence of the higher level subexpression
9282 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9283 which will be used to actually build the additive expression. */
9284 rhs = current.lhs;
9285 rhs_type = current.lhs_type;
9286 --sp;
9287 current = *sp;
9290 /* Undo the disabling of warnings done above. */
9291 if (current.tree_type == TRUTH_ANDIF_EXPR)
9292 c_inhibit_evaluation_warnings -=
9293 cp_fully_fold (current.lhs) == truthvalue_false_node;
9294 else if (current.tree_type == TRUTH_ORIF_EXPR)
9295 c_inhibit_evaluation_warnings -=
9296 cp_fully_fold (current.lhs) == truthvalue_true_node;
9298 if (warn_logical_not_paren
9299 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9300 && current.lhs_type == TRUTH_NOT_EXPR
9301 /* Avoid warning for !!x == y. */
9302 && (TREE_CODE (current.lhs) != NE_EXPR
9303 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9304 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9305 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9306 /* Avoid warning for !b == y where b is boolean. */
9307 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9308 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9309 != BOOLEAN_TYPE))))
9310 /* Avoid warning for !!b == y where b is boolean. */
9311 && (!DECL_P (current.lhs)
9312 || TREE_TYPE (current.lhs) == NULL_TREE
9313 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9314 warn_logical_not_parentheses (current.loc, current.tree_type,
9315 current.lhs, maybe_constant_value (rhs));
9317 overload = NULL;
9319 location_t combined_loc = make_location (current.loc,
9320 current.lhs.get_start (),
9321 rhs.get_finish ());
9323 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9324 ERROR_MARK for everything that is not a binary expression.
9325 This makes warn_about_parentheses miss some warnings that
9326 involve unary operators. For unary expressions we should
9327 pass the correct tree_code unless the unary expression was
9328 surrounded by parentheses.
9330 if (no_toplevel_fold_p
9331 && lookahead_prec <= current.prec
9332 && sp == stack)
9334 current.lhs
9335 = build_min (current.tree_type,
9336 TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9337 ? boolean_type_node : TREE_TYPE (current.lhs),
9338 current.lhs.get_value (), rhs.get_value ());
9339 SET_EXPR_LOCATION (current.lhs, combined_loc);
9341 else
9343 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9344 current.lhs, current.lhs_type,
9345 rhs, rhs_type, &overload,
9346 complain_flags (decltype_p));
9347 /* TODO: build_x_binary_op doesn't always honor the location. */
9348 current.lhs.set_location (combined_loc);
9350 current.lhs_type = current.tree_type;
9352 /* If the binary operator required the use of an overloaded operator,
9353 then this expression cannot be an integral constant-expression.
9354 An overloaded operator can be used even if both operands are
9355 otherwise permissible in an integral constant-expression if at
9356 least one of the operands is of enumeration type. */
9358 if (overload
9359 && cp_parser_non_integral_constant_expression (parser,
9360 NIC_OVERLOADED))
9361 return error_mark_node;
9364 return current.lhs;
9367 static cp_expr
9368 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9369 bool no_toplevel_fold_p,
9370 enum cp_parser_prec prec,
9371 cp_id_kind * pidk)
9373 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9374 /*decltype*/false, prec, pidk);
9377 /* Parse the `? expression : assignment-expression' part of a
9378 conditional-expression. The LOGICAL_OR_EXPR is the
9379 logical-or-expression that started the conditional-expression.
9380 Returns a representation of the entire conditional-expression.
9382 This routine is used by cp_parser_assignment_expression.
9384 ? expression : assignment-expression
9386 GNU Extensions:
9388 ? : assignment-expression */
9390 static tree
9391 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9393 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9394 cp_expr assignment_expr;
9395 struct cp_token *token;
9396 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9398 /* Consume the `?' token. */
9399 cp_lexer_consume_token (parser->lexer);
9400 token = cp_lexer_peek_token (parser->lexer);
9401 if (cp_parser_allow_gnu_extensions_p (parser)
9402 && token->type == CPP_COLON)
9404 pedwarn (token->location, OPT_Wpedantic,
9405 "ISO C++ does not allow ?: with omitted middle operand");
9406 /* Implicit true clause. */
9407 expr = NULL_TREE;
9408 c_inhibit_evaluation_warnings +=
9409 folded_logical_or_expr == truthvalue_true_node;
9410 warn_for_omitted_condop (token->location, logical_or_expr);
9412 else
9414 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9415 parser->colon_corrects_to_scope_p = false;
9416 /* Parse the expression. */
9417 c_inhibit_evaluation_warnings +=
9418 folded_logical_or_expr == truthvalue_false_node;
9419 expr = cp_parser_expression (parser);
9420 c_inhibit_evaluation_warnings +=
9421 ((folded_logical_or_expr == truthvalue_true_node)
9422 - (folded_logical_or_expr == truthvalue_false_node));
9423 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9426 /* The next token should be a `:'. */
9427 cp_parser_require (parser, CPP_COLON, RT_COLON);
9428 /* Parse the assignment-expression. */
9429 assignment_expr = cp_parser_assignment_expression (parser);
9430 c_inhibit_evaluation_warnings -=
9431 folded_logical_or_expr == truthvalue_true_node;
9433 /* Make a location:
9434 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9435 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9436 with the caret at the "?", ranging from the start of
9437 the logical_or_expr to the end of the assignment_expr. */
9438 loc = make_location (loc,
9439 logical_or_expr.get_start (),
9440 assignment_expr.get_finish ());
9442 /* Build the conditional-expression. */
9443 return build_x_conditional_expr (loc, logical_or_expr,
9444 expr,
9445 assignment_expr,
9446 tf_warning_or_error);
9449 /* Parse an assignment-expression.
9451 assignment-expression:
9452 conditional-expression
9453 logical-or-expression assignment-operator assignment_expression
9454 throw-expression
9456 CAST_P is true if this expression is the target of a cast.
9457 DECLTYPE_P is true if this expression is the operand of decltype.
9459 Returns a representation for the expression. */
9461 static cp_expr
9462 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9463 bool cast_p, bool decltype_p)
9465 cp_expr expr;
9467 /* If the next token is the `throw' keyword, then we're looking at
9468 a throw-expression. */
9469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9470 expr = cp_parser_throw_expression (parser);
9471 /* Otherwise, it must be that we are looking at a
9472 logical-or-expression. */
9473 else
9475 /* Parse the binary expressions (logical-or-expression). */
9476 expr = cp_parser_binary_expression (parser, cast_p, false,
9477 decltype_p,
9478 PREC_NOT_OPERATOR, pidk);
9479 /* If the next token is a `?' then we're actually looking at a
9480 conditional-expression. */
9481 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9482 return cp_parser_question_colon_clause (parser, expr);
9483 else
9485 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9487 /* If it's an assignment-operator, we're using the second
9488 production. */
9489 enum tree_code assignment_operator
9490 = cp_parser_assignment_operator_opt (parser);
9491 if (assignment_operator != ERROR_MARK)
9493 bool non_constant_p;
9495 /* Parse the right-hand side of the assignment. */
9496 cp_expr rhs = cp_parser_initializer_clause (parser,
9497 &non_constant_p);
9499 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9500 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9502 /* An assignment may not appear in a
9503 constant-expression. */
9504 if (cp_parser_non_integral_constant_expression (parser,
9505 NIC_ASSIGNMENT))
9506 return error_mark_node;
9507 /* Build the assignment expression. Its default
9508 location:
9509 LHS = RHS
9510 ~~~~^~~~~
9511 is the location of the '=' token as the
9512 caret, ranging from the start of the lhs to the
9513 end of the rhs. */
9514 loc = make_location (loc,
9515 expr.get_start (),
9516 rhs.get_finish ());
9517 expr = build_x_modify_expr (loc, expr,
9518 assignment_operator,
9519 rhs,
9520 complain_flags (decltype_p));
9521 /* TODO: build_x_modify_expr doesn't honor the location,
9522 so we must set it here. */
9523 expr.set_location (loc);
9528 return expr;
9531 /* Parse an (optional) assignment-operator.
9533 assignment-operator: one of
9534 = *= /= %= += -= >>= <<= &= ^= |=
9536 GNU Extension:
9538 assignment-operator: one of
9539 <?= >?=
9541 If the next token is an assignment operator, the corresponding tree
9542 code is returned, and the token is consumed. For example, for
9543 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9544 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9545 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9546 operator, ERROR_MARK is returned. */
9548 static enum tree_code
9549 cp_parser_assignment_operator_opt (cp_parser* parser)
9551 enum tree_code op;
9552 cp_token *token;
9554 /* Peek at the next token. */
9555 token = cp_lexer_peek_token (parser->lexer);
9557 switch (token->type)
9559 case CPP_EQ:
9560 op = NOP_EXPR;
9561 break;
9563 case CPP_MULT_EQ:
9564 op = MULT_EXPR;
9565 break;
9567 case CPP_DIV_EQ:
9568 op = TRUNC_DIV_EXPR;
9569 break;
9571 case CPP_MOD_EQ:
9572 op = TRUNC_MOD_EXPR;
9573 break;
9575 case CPP_PLUS_EQ:
9576 op = PLUS_EXPR;
9577 break;
9579 case CPP_MINUS_EQ:
9580 op = MINUS_EXPR;
9581 break;
9583 case CPP_RSHIFT_EQ:
9584 op = RSHIFT_EXPR;
9585 break;
9587 case CPP_LSHIFT_EQ:
9588 op = LSHIFT_EXPR;
9589 break;
9591 case CPP_AND_EQ:
9592 op = BIT_AND_EXPR;
9593 break;
9595 case CPP_XOR_EQ:
9596 op = BIT_XOR_EXPR;
9597 break;
9599 case CPP_OR_EQ:
9600 op = BIT_IOR_EXPR;
9601 break;
9603 default:
9604 /* Nothing else is an assignment operator. */
9605 op = ERROR_MARK;
9608 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9609 if (op != ERROR_MARK
9610 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9611 op = ERROR_MARK;
9613 /* If it was an assignment operator, consume it. */
9614 if (op != ERROR_MARK)
9615 cp_lexer_consume_token (parser->lexer);
9617 return op;
9620 /* Parse an expression.
9622 expression:
9623 assignment-expression
9624 expression , assignment-expression
9626 CAST_P is true if this expression is the target of a cast.
9627 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9628 except possibly parenthesized or on the RHS of a comma (N3276).
9630 Returns a representation of the expression. */
9632 static cp_expr
9633 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9634 bool cast_p, bool decltype_p)
9636 cp_expr expression = NULL_TREE;
9637 location_t loc = UNKNOWN_LOCATION;
9639 while (true)
9641 cp_expr assignment_expression;
9643 /* Parse the next assignment-expression. */
9644 assignment_expression
9645 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9647 /* We don't create a temporary for a call that is the immediate operand
9648 of decltype or on the RHS of a comma. But when we see a comma, we
9649 need to create a temporary for a call on the LHS. */
9650 if (decltype_p && !processing_template_decl
9651 && TREE_CODE (assignment_expression) == CALL_EXPR
9652 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9653 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9654 assignment_expression
9655 = build_cplus_new (TREE_TYPE (assignment_expression),
9656 assignment_expression, tf_warning_or_error);
9658 /* If this is the first assignment-expression, we can just
9659 save it away. */
9660 if (!expression)
9661 expression = assignment_expression;
9662 else
9664 /* Create a location with caret at the comma, ranging
9665 from the start of the LHS to the end of the RHS. */
9666 loc = make_location (loc,
9667 expression.get_start (),
9668 assignment_expression.get_finish ());
9669 expression = build_x_compound_expr (loc, expression,
9670 assignment_expression,
9671 complain_flags (decltype_p));
9672 expression.set_location (loc);
9674 /* If the next token is not a comma, or we're in a fold-expression, then
9675 we are done with the expression. */
9676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9677 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9678 break;
9679 /* Consume the `,'. */
9680 loc = cp_lexer_peek_token (parser->lexer)->location;
9681 cp_lexer_consume_token (parser->lexer);
9682 /* A comma operator cannot appear in a constant-expression. */
9683 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9684 expression = error_mark_node;
9687 return expression;
9690 /* Parse a constant-expression.
9692 constant-expression:
9693 conditional-expression
9695 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9696 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9697 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9698 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9699 only parse a conditional-expression, otherwise parse an
9700 assignment-expression. See below for rationale. */
9702 static cp_expr
9703 cp_parser_constant_expression (cp_parser* parser,
9704 bool allow_non_constant_p,
9705 bool *non_constant_p,
9706 bool strict_p)
9708 bool saved_integral_constant_expression_p;
9709 bool saved_allow_non_integral_constant_expression_p;
9710 bool saved_non_integral_constant_expression_p;
9711 cp_expr expression;
9713 /* It might seem that we could simply parse the
9714 conditional-expression, and then check to see if it were
9715 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9716 one that the compiler can figure out is constant, possibly after
9717 doing some simplifications or optimizations. The standard has a
9718 precise definition of constant-expression, and we must honor
9719 that, even though it is somewhat more restrictive.
9721 For example:
9723 int i[(2, 3)];
9725 is not a legal declaration, because `(2, 3)' is not a
9726 constant-expression. The `,' operator is forbidden in a
9727 constant-expression. However, GCC's constant-folding machinery
9728 will fold this operation to an INTEGER_CST for `3'. */
9730 /* Save the old settings. */
9731 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9732 saved_allow_non_integral_constant_expression_p
9733 = parser->allow_non_integral_constant_expression_p;
9734 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9735 /* We are now parsing a constant-expression. */
9736 parser->integral_constant_expression_p = true;
9737 parser->allow_non_integral_constant_expression_p
9738 = (allow_non_constant_p || cxx_dialect >= cxx11);
9739 parser->non_integral_constant_expression_p = false;
9740 /* Although the grammar says "conditional-expression", when not STRICT_P,
9741 we parse an "assignment-expression", which also permits
9742 "throw-expression" and the use of assignment operators. In the case
9743 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9744 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9745 actually essential that we look for an assignment-expression.
9746 For example, cp_parser_initializer_clauses uses this function to
9747 determine whether a particular assignment-expression is in fact
9748 constant. */
9749 if (strict_p)
9751 /* Parse the binary expressions (logical-or-expression). */
9752 expression = cp_parser_binary_expression (parser, false, false, false,
9753 PREC_NOT_OPERATOR, NULL);
9754 /* If the next token is a `?' then we're actually looking at
9755 a conditional-expression; otherwise we're done. */
9756 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9757 expression = cp_parser_question_colon_clause (parser, expression);
9759 else
9760 expression = cp_parser_assignment_expression (parser);
9761 /* Restore the old settings. */
9762 parser->integral_constant_expression_p
9763 = saved_integral_constant_expression_p;
9764 parser->allow_non_integral_constant_expression_p
9765 = saved_allow_non_integral_constant_expression_p;
9766 if (cxx_dialect >= cxx11)
9768 /* Require an rvalue constant expression here; that's what our
9769 callers expect. Reference constant expressions are handled
9770 separately in e.g. cp_parser_template_argument. */
9771 tree decay = expression;
9772 if (TREE_TYPE (expression)
9773 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9774 decay = build_address (expression);
9775 bool is_const = potential_rvalue_constant_expression (decay);
9776 parser->non_integral_constant_expression_p = !is_const;
9777 if (!is_const && !allow_non_constant_p)
9778 require_potential_rvalue_constant_expression (decay);
9780 if (allow_non_constant_p)
9781 *non_constant_p = parser->non_integral_constant_expression_p;
9782 parser->non_integral_constant_expression_p
9783 = saved_non_integral_constant_expression_p;
9785 return expression;
9788 /* Parse __builtin_offsetof.
9790 offsetof-expression:
9791 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9793 offsetof-member-designator:
9794 id-expression
9795 | offsetof-member-designator "." id-expression
9796 | offsetof-member-designator "[" expression "]"
9797 | offsetof-member-designator "->" id-expression */
9799 static cp_expr
9800 cp_parser_builtin_offsetof (cp_parser *parser)
9802 int save_ice_p, save_non_ice_p;
9803 tree type;
9804 cp_expr expr;
9805 cp_id_kind dummy;
9806 cp_token *token;
9807 location_t finish_loc;
9809 /* We're about to accept non-integral-constant things, but will
9810 definitely yield an integral constant expression. Save and
9811 restore these values around our local parsing. */
9812 save_ice_p = parser->integral_constant_expression_p;
9813 save_non_ice_p = parser->non_integral_constant_expression_p;
9815 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9817 /* Consume the "__builtin_offsetof" token. */
9818 cp_lexer_consume_token (parser->lexer);
9819 /* Consume the opening `('. */
9820 matching_parens parens;
9821 parens.require_open (parser);
9822 /* Parse the type-id. */
9823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9824 type = cp_parser_type_id (parser);
9825 /* Look for the `,'. */
9826 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9827 token = cp_lexer_peek_token (parser->lexer);
9829 /* Build the (type *)null that begins the traditional offsetof macro. */
9830 tree object_ptr
9831 = build_static_cast (build_pointer_type (type), null_pointer_node,
9832 tf_warning_or_error);
9834 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9835 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9836 true, &dummy, token->location);
9837 while (true)
9839 token = cp_lexer_peek_token (parser->lexer);
9840 switch (token->type)
9842 case CPP_OPEN_SQUARE:
9843 /* offsetof-member-designator "[" expression "]" */
9844 expr = cp_parser_postfix_open_square_expression (parser, expr,
9845 true, false);
9846 break;
9848 case CPP_DEREF:
9849 /* offsetof-member-designator "->" identifier */
9850 expr = grok_array_decl (token->location, expr,
9851 integer_zero_node, false);
9852 /* FALLTHRU */
9854 case CPP_DOT:
9855 /* offsetof-member-designator "." identifier */
9856 cp_lexer_consume_token (parser->lexer);
9857 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9858 expr, true, &dummy,
9859 token->location);
9860 break;
9862 case CPP_CLOSE_PAREN:
9863 /* Consume the ")" token. */
9864 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9865 cp_lexer_consume_token (parser->lexer);
9866 goto success;
9868 default:
9869 /* Error. We know the following require will fail, but
9870 that gives the proper error message. */
9871 parens.require_close (parser);
9872 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9873 expr = error_mark_node;
9874 goto failure;
9878 success:
9879 /* Make a location of the form:
9880 __builtin_offsetof (struct s, f)
9881 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9882 with caret at the type-id, ranging from the start of the
9883 "_builtin_offsetof" token to the close paren. */
9884 loc = make_location (loc, start_loc, finish_loc);
9885 /* The result will be an INTEGER_CST, so we need to explicitly
9886 preserve the location. */
9887 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9889 failure:
9890 parser->integral_constant_expression_p = save_ice_p;
9891 parser->non_integral_constant_expression_p = save_non_ice_p;
9893 expr = expr.maybe_add_location_wrapper ();
9894 return expr;
9897 /* Parse a trait expression.
9899 Returns a representation of the expression, the underlying type
9900 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9902 static cp_expr
9903 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9905 cp_trait_kind kind;
9906 tree type1, type2 = NULL_TREE;
9907 bool binary = false;
9908 bool variadic = false;
9910 switch (keyword)
9912 case RID_HAS_NOTHROW_ASSIGN:
9913 kind = CPTK_HAS_NOTHROW_ASSIGN;
9914 break;
9915 case RID_HAS_NOTHROW_CONSTRUCTOR:
9916 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9917 break;
9918 case RID_HAS_NOTHROW_COPY:
9919 kind = CPTK_HAS_NOTHROW_COPY;
9920 break;
9921 case RID_HAS_TRIVIAL_ASSIGN:
9922 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9923 break;
9924 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9925 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9926 break;
9927 case RID_HAS_TRIVIAL_COPY:
9928 kind = CPTK_HAS_TRIVIAL_COPY;
9929 break;
9930 case RID_HAS_TRIVIAL_DESTRUCTOR:
9931 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9932 break;
9933 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9934 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9935 break;
9936 case RID_HAS_VIRTUAL_DESTRUCTOR:
9937 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9938 break;
9939 case RID_IS_ABSTRACT:
9940 kind = CPTK_IS_ABSTRACT;
9941 break;
9942 case RID_IS_AGGREGATE:
9943 kind = CPTK_IS_AGGREGATE;
9944 break;
9945 case RID_IS_BASE_OF:
9946 kind = CPTK_IS_BASE_OF;
9947 binary = true;
9948 break;
9949 case RID_IS_CLASS:
9950 kind = CPTK_IS_CLASS;
9951 break;
9952 case RID_IS_EMPTY:
9953 kind = CPTK_IS_EMPTY;
9954 break;
9955 case RID_IS_ENUM:
9956 kind = CPTK_IS_ENUM;
9957 break;
9958 case RID_IS_FINAL:
9959 kind = CPTK_IS_FINAL;
9960 break;
9961 case RID_IS_LITERAL_TYPE:
9962 kind = CPTK_IS_LITERAL_TYPE;
9963 break;
9964 case RID_IS_POD:
9965 kind = CPTK_IS_POD;
9966 break;
9967 case RID_IS_POLYMORPHIC:
9968 kind = CPTK_IS_POLYMORPHIC;
9969 break;
9970 case RID_IS_SAME_AS:
9971 kind = CPTK_IS_SAME_AS;
9972 binary = true;
9973 break;
9974 case RID_IS_STD_LAYOUT:
9975 kind = CPTK_IS_STD_LAYOUT;
9976 break;
9977 case RID_IS_TRIVIAL:
9978 kind = CPTK_IS_TRIVIAL;
9979 break;
9980 case RID_IS_TRIVIALLY_ASSIGNABLE:
9981 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9982 binary = true;
9983 break;
9984 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9985 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9986 variadic = true;
9987 break;
9988 case RID_IS_TRIVIALLY_COPYABLE:
9989 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9990 break;
9991 case RID_IS_UNION:
9992 kind = CPTK_IS_UNION;
9993 break;
9994 case RID_UNDERLYING_TYPE:
9995 kind = CPTK_UNDERLYING_TYPE;
9996 break;
9997 case RID_BASES:
9998 kind = CPTK_BASES;
9999 break;
10000 case RID_DIRECT_BASES:
10001 kind = CPTK_DIRECT_BASES;
10002 break;
10003 case RID_IS_ASSIGNABLE:
10004 kind = CPTK_IS_ASSIGNABLE;
10005 binary = true;
10006 break;
10007 case RID_IS_CONSTRUCTIBLE:
10008 kind = CPTK_IS_CONSTRUCTIBLE;
10009 variadic = true;
10010 break;
10011 default:
10012 gcc_unreachable ();
10015 /* Get location of initial token. */
10016 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10018 /* Consume the token. */
10019 cp_lexer_consume_token (parser->lexer);
10021 matching_parens parens;
10022 parens.require_open (parser);
10025 type_id_in_expr_sentinel s (parser);
10026 type1 = cp_parser_type_id (parser);
10029 if (type1 == error_mark_node)
10030 return error_mark_node;
10032 if (binary)
10034 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10037 type_id_in_expr_sentinel s (parser);
10038 type2 = cp_parser_type_id (parser);
10041 if (type2 == error_mark_node)
10042 return error_mark_node;
10044 else if (variadic)
10046 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10048 cp_lexer_consume_token (parser->lexer);
10049 tree elt = cp_parser_type_id (parser);
10050 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10052 cp_lexer_consume_token (parser->lexer);
10053 elt = make_pack_expansion (elt);
10055 if (elt == error_mark_node)
10056 return error_mark_node;
10057 type2 = tree_cons (NULL_TREE, elt, type2);
10061 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10062 parens.require_close (parser);
10064 /* Construct a location of the form:
10065 __is_trivially_copyable(_Tp)
10066 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10067 with start == caret, finishing at the close-paren. */
10068 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10070 /* Complete the trait expression, which may mean either processing
10071 the trait expr now or saving it for template instantiation. */
10072 switch (kind)
10074 case CPTK_UNDERLYING_TYPE:
10075 return cp_expr (finish_underlying_type (type1), trait_loc);
10076 case CPTK_BASES:
10077 return cp_expr (finish_bases (type1, false), trait_loc);
10078 case CPTK_DIRECT_BASES:
10079 return cp_expr (finish_bases (type1, true), trait_loc);
10080 default:
10081 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10085 /* Parse a lambda expression.
10087 lambda-expression:
10088 lambda-introducer lambda-declarator [opt] compound-statement
10090 Returns a representation of the expression. */
10092 static cp_expr
10093 cp_parser_lambda_expression (cp_parser* parser)
10095 tree lambda_expr = build_lambda_expr ();
10096 tree type;
10097 bool ok = true;
10098 cp_token *token = cp_lexer_peek_token (parser->lexer);
10099 cp_token_position start = 0;
10101 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10103 if (cp_unevaluated_operand)
10105 if (!token->error_reported)
10107 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10108 "lambda-expression in unevaluated context");
10109 token->error_reported = true;
10111 ok = false;
10113 else if (parser->in_template_argument_list_p)
10115 if (!token->error_reported)
10117 error_at (token->location, "lambda-expression in template-argument");
10118 token->error_reported = true;
10120 ok = false;
10123 /* We may be in the middle of deferred access check. Disable
10124 it now. */
10125 push_deferring_access_checks (dk_no_deferred);
10127 cp_parser_lambda_introducer (parser, lambda_expr);
10129 type = begin_lambda_type (lambda_expr);
10130 if (type == error_mark_node)
10131 return error_mark_node;
10133 record_lambda_scope (lambda_expr);
10135 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10136 determine_visibility (TYPE_NAME (type));
10138 /* Now that we've started the type, add the capture fields for any
10139 explicit captures. */
10140 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10143 /* Inside the class, surrounding template-parameter-lists do not apply. */
10144 unsigned int saved_num_template_parameter_lists
10145 = parser->num_template_parameter_lists;
10146 unsigned char in_statement = parser->in_statement;
10147 bool in_switch_statement_p = parser->in_switch_statement_p;
10148 bool fully_implicit_function_template_p
10149 = parser->fully_implicit_function_template_p;
10150 tree implicit_template_parms = parser->implicit_template_parms;
10151 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10152 bool auto_is_implicit_function_template_parm_p
10153 = parser->auto_is_implicit_function_template_parm_p;
10155 parser->num_template_parameter_lists = 0;
10156 parser->in_statement = 0;
10157 parser->in_switch_statement_p = false;
10158 parser->fully_implicit_function_template_p = false;
10159 parser->implicit_template_parms = 0;
10160 parser->implicit_template_scope = 0;
10161 parser->auto_is_implicit_function_template_parm_p = false;
10163 /* By virtue of defining a local class, a lambda expression has access to
10164 the private variables of enclosing classes. */
10166 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10168 if (ok && cp_parser_error_occurred (parser))
10169 ok = false;
10171 if (ok)
10173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10174 && cp_parser_start_tentative_firewall (parser))
10175 start = token;
10176 cp_parser_lambda_body (parser, lambda_expr);
10178 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10180 if (cp_parser_skip_to_closing_brace (parser))
10181 cp_lexer_consume_token (parser->lexer);
10184 /* The capture list was built up in reverse order; fix that now. */
10185 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10186 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10188 if (ok)
10189 maybe_add_lambda_conv_op (type);
10191 type = finish_struct (type, /*attributes=*/NULL_TREE);
10193 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10194 parser->in_statement = in_statement;
10195 parser->in_switch_statement_p = in_switch_statement_p;
10196 parser->fully_implicit_function_template_p
10197 = fully_implicit_function_template_p;
10198 parser->implicit_template_parms = implicit_template_parms;
10199 parser->implicit_template_scope = implicit_template_scope;
10200 parser->auto_is_implicit_function_template_parm_p
10201 = auto_is_implicit_function_template_parm_p;
10204 /* This field is only used during parsing of the lambda. */
10205 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10207 /* This lambda shouldn't have any proxies left at this point. */
10208 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10209 /* And now that we're done, push proxies for an enclosing lambda. */
10210 insert_pending_capture_proxies ();
10212 if (ok)
10213 lambda_expr = build_lambda_object (lambda_expr);
10214 else
10215 lambda_expr = error_mark_node;
10217 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10219 pop_deferring_access_checks ();
10221 return lambda_expr;
10224 /* Parse the beginning of a lambda expression.
10226 lambda-introducer:
10227 [ lambda-capture [opt] ]
10229 LAMBDA_EXPR is the current representation of the lambda expression. */
10231 static void
10232 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10234 /* Need commas after the first capture. */
10235 bool first = true;
10237 /* Eat the leading `['. */
10238 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10240 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10241 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10242 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10243 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10244 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10245 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10247 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10249 cp_lexer_consume_token (parser->lexer);
10250 first = false;
10253 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10255 cp_token* capture_token;
10256 tree capture_id;
10257 tree capture_init_expr;
10258 cp_id_kind idk = CP_ID_KIND_NONE;
10259 bool explicit_init_p = false;
10261 enum capture_kind_type
10263 BY_COPY,
10264 BY_REFERENCE
10266 enum capture_kind_type capture_kind = BY_COPY;
10268 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10270 error ("expected end of capture-list");
10271 return;
10274 if (first)
10275 first = false;
10276 else
10277 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10279 /* Possibly capture `this'. */
10280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10282 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10283 if (cxx_dialect < cxx2a
10284 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10285 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10286 "with by-copy capture default");
10287 cp_lexer_consume_token (parser->lexer);
10288 add_capture (lambda_expr,
10289 /*id=*/this_identifier,
10290 /*initializer=*/finish_this_expr (),
10291 /*by_reference_p=*/true,
10292 explicit_init_p);
10293 continue;
10296 /* Possibly capture `*this'. */
10297 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10298 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10301 if (cxx_dialect < cxx17)
10302 pedwarn (loc, 0, "%<*this%> capture only available with "
10303 "-std=c++17 or -std=gnu++17");
10304 cp_lexer_consume_token (parser->lexer);
10305 cp_lexer_consume_token (parser->lexer);
10306 add_capture (lambda_expr,
10307 /*id=*/this_identifier,
10308 /*initializer=*/finish_this_expr (),
10309 /*by_reference_p=*/false,
10310 explicit_init_p);
10311 continue;
10314 /* Remember whether we want to capture as a reference or not. */
10315 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10317 capture_kind = BY_REFERENCE;
10318 cp_lexer_consume_token (parser->lexer);
10321 /* Get the identifier. */
10322 capture_token = cp_lexer_peek_token (parser->lexer);
10323 capture_id = cp_parser_identifier (parser);
10325 if (capture_id == error_mark_node)
10326 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10327 delimiters, but I modified this to stop on unnested ']' as well. It
10328 was already changed to stop on unnested '}', so the
10329 "closing_parenthesis" name is no more misleading with my change. */
10331 cp_parser_skip_to_closing_parenthesis (parser,
10332 /*recovering=*/true,
10333 /*or_comma=*/true,
10334 /*consume_paren=*/true);
10335 break;
10338 /* Find the initializer for this capture. */
10339 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10340 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10341 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10343 bool direct, non_constant;
10344 /* An explicit initializer exists. */
10345 if (cxx_dialect < cxx14)
10346 pedwarn (input_location, 0,
10347 "lambda capture initializers "
10348 "only available with -std=c++14 or -std=gnu++14");
10349 capture_init_expr = cp_parser_initializer (parser, &direct,
10350 &non_constant);
10351 explicit_init_p = true;
10352 if (capture_init_expr == NULL_TREE)
10354 error ("empty initializer for lambda init-capture");
10355 capture_init_expr = error_mark_node;
10358 else
10360 const char* error_msg;
10362 /* Turn the identifier into an id-expression. */
10363 capture_init_expr
10364 = cp_parser_lookup_name_simple (parser, capture_id,
10365 capture_token->location);
10367 if (capture_init_expr == error_mark_node)
10369 unqualified_name_lookup_error (capture_id);
10370 continue;
10372 else if (DECL_P (capture_init_expr)
10373 && (!VAR_P (capture_init_expr)
10374 && TREE_CODE (capture_init_expr) != PARM_DECL))
10376 error_at (capture_token->location,
10377 "capture of non-variable %qD ",
10378 capture_init_expr);
10379 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10380 "%q#D declared here", capture_init_expr);
10381 continue;
10383 if (VAR_P (capture_init_expr)
10384 && decl_storage_duration (capture_init_expr) != dk_auto)
10386 if (pedwarn (capture_token->location, 0, "capture of variable "
10387 "%qD with non-automatic storage duration",
10388 capture_init_expr))
10389 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10390 "%q#D declared here", capture_init_expr);
10391 continue;
10394 capture_init_expr
10395 = finish_id_expression
10396 (capture_id,
10397 capture_init_expr,
10398 parser->scope,
10399 &idk,
10400 /*integral_constant_expression_p=*/false,
10401 /*allow_non_integral_constant_expression_p=*/false,
10402 /*non_integral_constant_expression_p=*/NULL,
10403 /*template_p=*/false,
10404 /*done=*/true,
10405 /*address_p=*/false,
10406 /*template_arg_p=*/false,
10407 &error_msg,
10408 capture_token->location);
10410 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10412 cp_lexer_consume_token (parser->lexer);
10413 capture_init_expr = make_pack_expansion (capture_init_expr);
10415 else
10416 check_for_bare_parameter_packs (capture_init_expr);
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10420 && !explicit_init_p)
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10423 && capture_kind == BY_COPY)
10424 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10425 "of %qD redundant with by-copy capture default",
10426 capture_id);
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10428 && capture_kind == BY_REFERENCE)
10429 pedwarn (capture_token->location, 0, "explicit by-reference "
10430 "capture of %qD redundant with by-reference capture "
10431 "default", capture_id);
10434 add_capture (lambda_expr,
10435 capture_id,
10436 capture_init_expr,
10437 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10438 explicit_init_p);
10441 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10444 /* Parse the (optional) middle of a lambda expression.
10446 lambda-declarator:
10447 < template-parameter-list [opt] >
10448 ( parameter-declaration-clause [opt] )
10449 attribute-specifier [opt]
10450 decl-specifier-seq [opt]
10451 exception-specification [opt]
10452 lambda-return-type-clause [opt]
10454 LAMBDA_EXPR is the current representation of the lambda expression. */
10456 static bool
10457 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10459 /* 5.1.1.4 of the standard says:
10460 If a lambda-expression does not include a lambda-declarator, it is as if
10461 the lambda-declarator were ().
10462 This means an empty parameter list, no attributes, and no exception
10463 specification. */
10464 tree param_list = void_list_node;
10465 tree attributes = NULL_TREE;
10466 tree exception_spec = NULL_TREE;
10467 tree template_param_list = NULL_TREE;
10468 tree tx_qual = NULL_TREE;
10469 tree return_type = NULL_TREE;
10470 cp_decl_specifier_seq lambda_specs;
10471 clear_decl_specs (&lambda_specs);
10473 /* The template-parameter-list is optional, but must begin with
10474 an opening angle if present. */
10475 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10477 if (cxx_dialect < cxx14)
10478 pedwarn (parser->lexer->next_token->location, 0,
10479 "lambda templates are only available with "
10480 "-std=c++14 or -std=gnu++14");
10481 else if (cxx_dialect < cxx2a)
10482 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10483 "lambda templates are only available with "
10484 "-std=c++2a or -std=gnu++2a");
10486 cp_lexer_consume_token (parser->lexer);
10488 template_param_list = cp_parser_template_parameter_list (parser);
10490 cp_parser_skip_to_end_of_template_parameter_list (parser);
10492 /* We just processed one more parameter list. */
10493 ++parser->num_template_parameter_lists;
10496 /* The parameter-declaration-clause is optional (unless
10497 template-parameter-list was given), but must begin with an
10498 opening parenthesis if present. */
10499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10501 matching_parens parens;
10502 parens.consume_open (parser);
10504 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10506 /* Parse parameters. */
10507 param_list = cp_parser_parameter_declaration_clause (parser);
10509 /* Default arguments shall not be specified in the
10510 parameter-declaration-clause of a lambda-declarator. */
10511 if (cxx_dialect < cxx14)
10512 for (tree t = param_list; t; t = TREE_CHAIN (t))
10513 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10514 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10515 "default argument specified for lambda parameter");
10517 parens.require_close (parser);
10519 attributes = cp_parser_attributes_opt (parser);
10521 /* In the decl-specifier-seq of the lambda-declarator, each
10522 decl-specifier shall either be mutable or constexpr. */
10523 int declares_class_or_enum;
10524 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10525 cp_parser_decl_specifier_seq (parser,
10526 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10527 &lambda_specs, &declares_class_or_enum);
10528 if (lambda_specs.storage_class == sc_mutable)
10530 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10531 if (lambda_specs.conflicting_specifiers_p)
10532 error_at (lambda_specs.locations[ds_storage_class],
10533 "duplicate %<mutable%>");
10536 tx_qual = cp_parser_tx_qualifier_opt (parser);
10538 /* Parse optional exception specification. */
10539 exception_spec = cp_parser_exception_specification_opt (parser);
10541 /* Parse optional trailing return type. */
10542 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10544 cp_lexer_consume_token (parser->lexer);
10545 return_type = cp_parser_trailing_type_id (parser);
10548 /* The function parameters must be in scope all the way until after the
10549 trailing-return-type in case of decltype. */
10550 pop_bindings_and_leave_scope ();
10552 else if (template_param_list != NULL_TREE) // generate diagnostic
10553 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10555 /* Create the function call operator.
10557 Messing with declarators like this is no uglier than building up the
10558 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10559 other code. */
10561 cp_decl_specifier_seq return_type_specs;
10562 cp_declarator* declarator;
10563 tree fco;
10564 int quals;
10565 void *p;
10567 clear_decl_specs (&return_type_specs);
10568 if (return_type)
10569 return_type_specs.type = return_type;
10570 else
10571 /* Maybe we will deduce the return type later. */
10572 return_type_specs.type = make_auto ();
10574 if (lambda_specs.locations[ds_constexpr])
10576 if (cxx_dialect >= cxx17)
10577 return_type_specs.locations[ds_constexpr]
10578 = lambda_specs.locations[ds_constexpr];
10579 else
10580 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10581 "lambda only available with -std=c++17 or -std=gnu++17");
10584 p = obstack_alloc (&declarator_obstack, 0);
10586 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10588 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10589 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10590 declarator = make_call_declarator (declarator, param_list, quals,
10591 VIRT_SPEC_UNSPECIFIED,
10592 REF_QUAL_NONE,
10593 tx_qual,
10594 exception_spec,
10595 /*late_return_type=*/NULL_TREE,
10596 /*requires_clause*/NULL_TREE);
10597 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10599 fco = grokmethod (&return_type_specs,
10600 declarator,
10601 attributes);
10602 if (fco != error_mark_node)
10604 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10605 DECL_ARTIFICIAL (fco) = 1;
10606 /* Give the object parameter a different name. */
10607 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10608 if (return_type)
10609 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10611 if (template_param_list)
10613 fco = finish_member_template_decl (fco);
10614 finish_template_decl (template_param_list);
10615 --parser->num_template_parameter_lists;
10617 else if (parser->fully_implicit_function_template_p)
10618 fco = finish_fully_implicit_template (parser, fco);
10620 finish_member_declaration (fco);
10622 obstack_free (&declarator_obstack, p);
10624 return (fco != error_mark_node);
10628 /* Parse the body of a lambda expression, which is simply
10630 compound-statement
10632 but which requires special handling.
10633 LAMBDA_EXPR is the current representation of the lambda expression. */
10635 static void
10636 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10638 bool nested = (current_function_decl != NULL_TREE);
10639 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10640 bool in_function_body = parser->in_function_body;
10642 if (nested)
10643 push_function_context ();
10644 else
10645 /* Still increment function_depth so that we don't GC in the
10646 middle of an expression. */
10647 ++function_depth;
10649 vec<tree> omp_privatization_save;
10650 save_omp_privatization_clauses (omp_privatization_save);
10651 /* Clear this in case we're in the middle of a default argument. */
10652 parser->local_variables_forbidden_p = false;
10653 parser->in_function_body = true;
10656 local_specialization_stack s (lss_copy);
10657 tree fco = lambda_function (lambda_expr);
10658 tree body = start_lambda_function (fco, lambda_expr);
10659 matching_braces braces;
10661 if (braces.require_open (parser))
10663 tree compound_stmt = begin_compound_stmt (0);
10665 /* Originally C++11 required us to peek for 'return expr'; and
10666 process it specially here to deduce the return type. N3638
10667 removed the need for that. */
10669 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10670 cp_parser_label_declaration (parser);
10671 cp_parser_statement_seq_opt (parser, NULL_TREE);
10672 braces.require_close (parser);
10674 finish_compound_stmt (compound_stmt);
10677 finish_lambda_function (body);
10680 restore_omp_privatization_clauses (omp_privatization_save);
10681 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10682 parser->in_function_body = in_function_body;
10683 if (nested)
10684 pop_function_context();
10685 else
10686 --function_depth;
10689 /* Statements [gram.stmt.stmt] */
10691 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10693 static void
10694 add_debug_begin_stmt (location_t loc)
10696 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10697 return;
10699 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10700 SET_EXPR_LOCATION (stmt, loc);
10701 add_stmt (stmt);
10704 /* Parse a statement.
10706 statement:
10707 labeled-statement
10708 expression-statement
10709 compound-statement
10710 selection-statement
10711 iteration-statement
10712 jump-statement
10713 declaration-statement
10714 try-block
10716 C++11:
10718 statement:
10719 labeled-statement
10720 attribute-specifier-seq (opt) expression-statement
10721 attribute-specifier-seq (opt) compound-statement
10722 attribute-specifier-seq (opt) selection-statement
10723 attribute-specifier-seq (opt) iteration-statement
10724 attribute-specifier-seq (opt) jump-statement
10725 declaration-statement
10726 attribute-specifier-seq (opt) try-block
10728 init-statement:
10729 expression-statement
10730 simple-declaration
10732 TM Extension:
10734 statement:
10735 atomic-statement
10737 IN_COMPOUND is true when the statement is nested inside a
10738 cp_parser_compound_statement; this matters for certain pragmas.
10740 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10741 is a (possibly labeled) if statement which is not enclosed in braces
10742 and has an else clause. This is used to implement -Wparentheses.
10744 CHAIN is a vector of if-else-if conditions. */
10746 static void
10747 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10748 bool in_compound, bool *if_p, vec<tree> *chain,
10749 location_t *loc_after_labels)
10751 tree statement, std_attrs = NULL_TREE;
10752 cp_token *token;
10753 location_t statement_location, attrs_location;
10755 restart:
10756 if (if_p != NULL)
10757 *if_p = false;
10758 /* There is no statement yet. */
10759 statement = NULL_TREE;
10761 saved_token_sentinel saved_tokens (parser->lexer);
10762 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10763 if (c_dialect_objc ())
10764 /* In obj-c++, seeing '[[' might be the either the beginning of
10765 c++11 attributes, or a nested objc-message-expression. So
10766 let's parse the c++11 attributes tentatively. */
10767 cp_parser_parse_tentatively (parser);
10768 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10769 if (c_dialect_objc ())
10771 if (!cp_parser_parse_definitely (parser))
10772 std_attrs = NULL_TREE;
10775 /* Peek at the next token. */
10776 token = cp_lexer_peek_token (parser->lexer);
10777 /* Remember the location of the first token in the statement. */
10778 statement_location = token->location;
10779 add_debug_begin_stmt (statement_location);
10780 /* If this is a keyword, then that will often determine what kind of
10781 statement we have. */
10782 if (token->type == CPP_KEYWORD)
10784 enum rid keyword = token->keyword;
10786 switch (keyword)
10788 case RID_CASE:
10789 case RID_DEFAULT:
10790 /* Looks like a labeled-statement with a case label.
10791 Parse the label, and then use tail recursion to parse
10792 the statement. */
10793 cp_parser_label_for_labeled_statement (parser, std_attrs);
10794 in_compound = false;
10795 goto restart;
10797 case RID_IF:
10798 case RID_SWITCH:
10799 statement = cp_parser_selection_statement (parser, if_p, chain);
10800 break;
10802 case RID_WHILE:
10803 case RID_DO:
10804 case RID_FOR:
10805 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10806 break;
10808 case RID_BREAK:
10809 case RID_CONTINUE:
10810 case RID_RETURN:
10811 case RID_GOTO:
10812 statement = cp_parser_jump_statement (parser);
10813 break;
10815 /* Objective-C++ exception-handling constructs. */
10816 case RID_AT_TRY:
10817 case RID_AT_CATCH:
10818 case RID_AT_FINALLY:
10819 case RID_AT_SYNCHRONIZED:
10820 case RID_AT_THROW:
10821 statement = cp_parser_objc_statement (parser);
10822 break;
10824 case RID_TRY:
10825 statement = cp_parser_try_block (parser);
10826 break;
10828 case RID_NAMESPACE:
10829 /* This must be a namespace alias definition. */
10830 cp_parser_declaration_statement (parser);
10831 return;
10833 case RID_TRANSACTION_ATOMIC:
10834 case RID_TRANSACTION_RELAXED:
10835 case RID_SYNCHRONIZED:
10836 case RID_ATOMIC_NOEXCEPT:
10837 case RID_ATOMIC_CANCEL:
10838 statement = cp_parser_transaction (parser, token);
10839 break;
10840 case RID_TRANSACTION_CANCEL:
10841 statement = cp_parser_transaction_cancel (parser);
10842 break;
10844 default:
10845 /* It might be a keyword like `int' that can start a
10846 declaration-statement. */
10847 break;
10850 else if (token->type == CPP_NAME)
10852 /* If the next token is a `:', then we are looking at a
10853 labeled-statement. */
10854 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10855 if (token->type == CPP_COLON)
10857 /* Looks like a labeled-statement with an ordinary label.
10858 Parse the label, and then use tail recursion to parse
10859 the statement. */
10861 cp_parser_label_for_labeled_statement (parser, std_attrs);
10862 in_compound = false;
10863 goto restart;
10866 /* Anything that starts with a `{' must be a compound-statement. */
10867 else if (token->type == CPP_OPEN_BRACE)
10868 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10869 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10870 a statement all its own. */
10871 else if (token->type == CPP_PRAGMA)
10873 /* Only certain OpenMP pragmas are attached to statements, and thus
10874 are considered statements themselves. All others are not. In
10875 the context of a compound, accept the pragma as a "statement" and
10876 return so that we can check for a close brace. Otherwise we
10877 require a real statement and must go back and read one. */
10878 if (in_compound)
10879 cp_parser_pragma (parser, pragma_compound, if_p);
10880 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10881 goto restart;
10882 return;
10884 else if (token->type == CPP_EOF)
10886 cp_parser_error (parser, "expected statement");
10887 return;
10890 /* Everything else must be a declaration-statement or an
10891 expression-statement. Try for the declaration-statement
10892 first, unless we are looking at a `;', in which case we know that
10893 we have an expression-statement. */
10894 if (!statement)
10896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10898 if (std_attrs != NULL_TREE)
10900 /* Attributes should be parsed as part of the the
10901 declaration, so let's un-parse them. */
10902 saved_tokens.rollback();
10903 std_attrs = NULL_TREE;
10906 cp_parser_parse_tentatively (parser);
10907 /* Try to parse the declaration-statement. */
10908 cp_parser_declaration_statement (parser);
10909 /* If that worked, we're done. */
10910 if (cp_parser_parse_definitely (parser))
10911 return;
10913 /* All preceding labels have been parsed at this point. */
10914 if (loc_after_labels != NULL)
10915 *loc_after_labels = statement_location;
10917 /* Look for an expression-statement instead. */
10918 statement = cp_parser_expression_statement (parser, in_statement_expr);
10920 /* Handle [[fallthrough]];. */
10921 if (attribute_fallthrough_p (std_attrs))
10923 /* The next token after the fallthrough attribute is ';'. */
10924 if (statement == NULL_TREE)
10926 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10927 statement = build_call_expr_internal_loc (statement_location,
10928 IFN_FALLTHROUGH,
10929 void_type_node, 0);
10930 finish_expr_stmt (statement);
10932 else
10933 warning_at (statement_location, OPT_Wattributes,
10934 "%<fallthrough%> attribute not followed by %<;%>");
10935 std_attrs = NULL_TREE;
10939 /* Set the line number for the statement. */
10940 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10941 SET_EXPR_LOCATION (statement, statement_location);
10943 /* Allow "[[fallthrough]];", but warn otherwise. */
10944 if (std_attrs != NULL_TREE)
10945 warning_at (attrs_location,
10946 OPT_Wattributes,
10947 "attributes at the beginning of statement are ignored");
10950 /* Append ATTR to attribute list ATTRS. */
10952 static tree
10953 attr_chainon (tree attrs, tree attr)
10955 if (attrs == error_mark_node)
10956 return error_mark_node;
10957 if (attr == error_mark_node)
10958 return error_mark_node;
10959 return chainon (attrs, attr);
10962 /* Parse the label for a labeled-statement, i.e.
10964 identifier :
10965 case constant-expression :
10966 default :
10968 GNU Extension:
10969 case constant-expression ... constant-expression : statement
10971 When a label is parsed without errors, the label is added to the
10972 parse tree by the finish_* functions, so this function doesn't
10973 have to return the label. */
10975 static void
10976 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10978 cp_token *token;
10979 tree label = NULL_TREE;
10980 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10982 /* The next token should be an identifier. */
10983 token = cp_lexer_peek_token (parser->lexer);
10984 if (token->type != CPP_NAME
10985 && token->type != CPP_KEYWORD)
10987 cp_parser_error (parser, "expected labeled-statement");
10988 return;
10991 /* Remember whether this case or a user-defined label is allowed to fall
10992 through to. */
10993 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10995 parser->colon_corrects_to_scope_p = false;
10996 switch (token->keyword)
10998 case RID_CASE:
11000 tree expr, expr_hi;
11001 cp_token *ellipsis;
11003 /* Consume the `case' token. */
11004 cp_lexer_consume_token (parser->lexer);
11005 /* Parse the constant-expression. */
11006 expr = cp_parser_constant_expression (parser);
11007 if (check_for_bare_parameter_packs (expr))
11008 expr = error_mark_node;
11010 ellipsis = cp_lexer_peek_token (parser->lexer);
11011 if (ellipsis->type == CPP_ELLIPSIS)
11013 /* Consume the `...' token. */
11014 cp_lexer_consume_token (parser->lexer);
11015 expr_hi = cp_parser_constant_expression (parser);
11016 if (check_for_bare_parameter_packs (expr_hi))
11017 expr_hi = error_mark_node;
11019 /* We don't need to emit warnings here, as the common code
11020 will do this for us. */
11022 else
11023 expr_hi = NULL_TREE;
11025 if (parser->in_switch_statement_p)
11027 tree l = finish_case_label (token->location, expr, expr_hi);
11028 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11029 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11031 else
11032 error_at (token->location,
11033 "case label %qE not within a switch statement",
11034 expr);
11036 break;
11038 case RID_DEFAULT:
11039 /* Consume the `default' token. */
11040 cp_lexer_consume_token (parser->lexer);
11042 if (parser->in_switch_statement_p)
11044 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11045 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11046 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11048 else
11049 error_at (token->location, "case label not within a switch statement");
11050 break;
11052 default:
11053 /* Anything else must be an ordinary label. */
11054 label = finish_label_stmt (cp_parser_identifier (parser));
11055 if (label && TREE_CODE (label) == LABEL_DECL)
11056 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11057 break;
11060 /* Require the `:' token. */
11061 cp_parser_require (parser, CPP_COLON, RT_COLON);
11063 /* An ordinary label may optionally be followed by attributes.
11064 However, this is only permitted if the attributes are then
11065 followed by a semicolon. This is because, for backward
11066 compatibility, when parsing
11067 lab: __attribute__ ((unused)) int i;
11068 we want the attribute to attach to "i", not "lab". */
11069 if (label != NULL_TREE
11070 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11072 tree attrs;
11073 cp_parser_parse_tentatively (parser);
11074 attrs = cp_parser_gnu_attributes_opt (parser);
11075 if (attrs == NULL_TREE
11076 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11077 cp_parser_abort_tentative_parse (parser);
11078 else if (!cp_parser_parse_definitely (parser))
11080 else
11081 attributes = attr_chainon (attributes, attrs);
11084 if (attributes != NULL_TREE)
11085 cplus_decl_attributes (&label, attributes, 0);
11087 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11090 /* Parse an expression-statement.
11092 expression-statement:
11093 expression [opt] ;
11095 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11096 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11097 indicates whether this expression-statement is part of an
11098 expression statement. */
11100 static tree
11101 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11103 tree statement = NULL_TREE;
11104 cp_token *token = cp_lexer_peek_token (parser->lexer);
11105 location_t loc = token->location;
11107 /* There might be attribute fallthrough. */
11108 tree attr = cp_parser_gnu_attributes_opt (parser);
11110 /* If the next token is a ';', then there is no expression
11111 statement. */
11112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11114 statement = cp_parser_expression (parser);
11115 if (statement == error_mark_node
11116 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11118 cp_parser_skip_to_end_of_block_or_statement (parser);
11119 return error_mark_node;
11123 /* Handle [[fallthrough]];. */
11124 if (attribute_fallthrough_p (attr))
11126 /* The next token after the fallthrough attribute is ';'. */
11127 if (statement == NULL_TREE)
11128 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11129 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11130 void_type_node, 0);
11131 else
11132 warning_at (loc, OPT_Wattributes,
11133 "%<fallthrough%> attribute not followed by %<;%>");
11134 attr = NULL_TREE;
11137 /* Allow "[[fallthrough]];", but warn otherwise. */
11138 if (attr != NULL_TREE)
11139 warning_at (loc, OPT_Wattributes,
11140 "attributes at the beginning of statement are ignored");
11142 /* Give a helpful message for "A<T>::type t;" and the like. */
11143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11144 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11146 if (TREE_CODE (statement) == SCOPE_REF)
11147 error_at (token->location, "need %<typename%> before %qE because "
11148 "%qT is a dependent scope",
11149 statement, TREE_OPERAND (statement, 0));
11150 else if (is_overloaded_fn (statement)
11151 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11153 /* A::A a; */
11154 tree fn = get_first_fn (statement);
11155 error_at (token->location,
11156 "%<%T::%D%> names the constructor, not the type",
11157 DECL_CONTEXT (fn), DECL_NAME (fn));
11161 /* Consume the final `;'. */
11162 cp_parser_consume_semicolon_at_end_of_statement (parser);
11164 if (in_statement_expr
11165 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11166 /* This is the final expression statement of a statement
11167 expression. */
11168 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11169 else if (statement)
11170 statement = finish_expr_stmt (statement);
11172 return statement;
11175 /* Parse a compound-statement.
11177 compound-statement:
11178 { statement-seq [opt] }
11180 GNU extension:
11182 compound-statement:
11183 { label-declaration-seq [opt] statement-seq [opt] }
11185 label-declaration-seq:
11186 label-declaration
11187 label-declaration-seq label-declaration
11189 Returns a tree representing the statement. */
11191 static tree
11192 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11193 int bcs_flags, bool function_body)
11195 tree compound_stmt;
11196 matching_braces braces;
11198 /* Consume the `{'. */
11199 if (!braces.require_open (parser))
11200 return error_mark_node;
11201 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11202 && !function_body && cxx_dialect < cxx14)
11203 pedwarn (input_location, OPT_Wpedantic,
11204 "compound-statement in %<constexpr%> function");
11205 /* Begin the compound-statement. */
11206 compound_stmt = begin_compound_stmt (bcs_flags);
11207 /* If the next keyword is `__label__' we have a label declaration. */
11208 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11209 cp_parser_label_declaration (parser);
11210 /* Parse an (optional) statement-seq. */
11211 cp_parser_statement_seq_opt (parser, in_statement_expr);
11212 /* Finish the compound-statement. */
11213 finish_compound_stmt (compound_stmt);
11214 /* Consume the `}'. */
11215 braces.require_close (parser);
11217 return compound_stmt;
11220 /* Parse an (optional) statement-seq.
11222 statement-seq:
11223 statement
11224 statement-seq [opt] statement */
11226 static void
11227 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11229 /* Scan statements until there aren't any more. */
11230 while (true)
11232 cp_token *token = cp_lexer_peek_token (parser->lexer);
11234 /* If we are looking at a `}', then we have run out of
11235 statements; the same is true if we have reached the end
11236 of file, or have stumbled upon a stray '@end'. */
11237 if (token->type == CPP_CLOSE_BRACE
11238 || token->type == CPP_EOF
11239 || token->type == CPP_PRAGMA_EOL
11240 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11241 break;
11243 /* If we are in a compound statement and find 'else' then
11244 something went wrong. */
11245 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11247 if (parser->in_statement & IN_IF_STMT)
11248 break;
11249 else
11251 token = cp_lexer_consume_token (parser->lexer);
11252 error_at (token->location, "%<else%> without a previous %<if%>");
11256 /* Parse the statement. */
11257 cp_parser_statement (parser, in_statement_expr, true, NULL);
11261 /* Return true if we're looking at (init; cond), false otherwise. */
11263 static bool
11264 cp_parser_init_statement_p (cp_parser *parser)
11266 /* Save tokens so that we can put them back. */
11267 cp_lexer_save_tokens (parser->lexer);
11269 /* Look for ';' that is not nested in () or {}. */
11270 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11271 /*recovering=*/false,
11272 CPP_SEMICOLON,
11273 /*consume_paren=*/false);
11275 /* Roll back the tokens we skipped. */
11276 cp_lexer_rollback_tokens (parser->lexer);
11278 return ret == -1;
11281 /* Parse a selection-statement.
11283 selection-statement:
11284 if ( init-statement [opt] condition ) statement
11285 if ( init-statement [opt] condition ) statement else statement
11286 switch ( init-statement [opt] condition ) statement
11288 Returns the new IF_STMT or SWITCH_STMT.
11290 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11291 is a (possibly labeled) if statement which is not enclosed in
11292 braces and has an else clause. This is used to implement
11293 -Wparentheses.
11295 CHAIN is a vector of if-else-if conditions. This is used to implement
11296 -Wduplicated-cond. */
11298 static tree
11299 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11300 vec<tree> *chain)
11302 cp_token *token;
11303 enum rid keyword;
11304 token_indent_info guard_tinfo;
11306 if (if_p != NULL)
11307 *if_p = false;
11309 /* Peek at the next token. */
11310 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11311 guard_tinfo = get_token_indent_info (token);
11313 /* See what kind of keyword it is. */
11314 keyword = token->keyword;
11315 switch (keyword)
11317 case RID_IF:
11318 case RID_SWITCH:
11320 tree statement;
11321 tree condition;
11323 bool cx = false;
11324 if (keyword == RID_IF
11325 && cp_lexer_next_token_is_keyword (parser->lexer,
11326 RID_CONSTEXPR))
11328 cx = true;
11329 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11330 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11331 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11332 "with -std=c++17 or -std=gnu++17");
11335 /* Look for the `('. */
11336 matching_parens parens;
11337 if (!parens.require_open (parser))
11339 cp_parser_skip_to_end_of_statement (parser);
11340 return error_mark_node;
11343 /* Begin the selection-statement. */
11344 if (keyword == RID_IF)
11346 statement = begin_if_stmt ();
11347 IF_STMT_CONSTEXPR_P (statement) = cx;
11349 else
11350 statement = begin_switch_stmt ();
11352 /* Parse the optional init-statement. */
11353 if (cp_parser_init_statement_p (parser))
11355 tree decl;
11356 if (cxx_dialect < cxx17)
11357 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11358 "init-statement in selection statements only available "
11359 "with -std=c++17 or -std=gnu++17");
11360 cp_parser_init_statement (parser, &decl);
11363 /* Parse the condition. */
11364 condition = cp_parser_condition (parser);
11365 /* Look for the `)'. */
11366 if (!parens.require_close (parser))
11367 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11368 /*consume_paren=*/true);
11370 if (keyword == RID_IF)
11372 bool nested_if;
11373 unsigned char in_statement;
11375 /* Add the condition. */
11376 condition = finish_if_stmt_cond (condition, statement);
11378 if (warn_duplicated_cond)
11379 warn_duplicated_cond_add_or_warn (token->location, condition,
11380 &chain);
11382 /* Parse the then-clause. */
11383 in_statement = parser->in_statement;
11384 parser->in_statement |= IN_IF_STMT;
11386 /* Outside a template, the non-selected branch of a constexpr
11387 if is a 'discarded statement', i.e. unevaluated. */
11388 bool was_discarded = in_discarded_stmt;
11389 bool discard_then = (cx && !processing_template_decl
11390 && integer_zerop (condition));
11391 if (discard_then)
11393 in_discarded_stmt = true;
11394 ++c_inhibit_evaluation_warnings;
11397 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11398 guard_tinfo);
11400 parser->in_statement = in_statement;
11402 finish_then_clause (statement);
11404 if (discard_then)
11406 THEN_CLAUSE (statement) = NULL_TREE;
11407 in_discarded_stmt = was_discarded;
11408 --c_inhibit_evaluation_warnings;
11411 /* If the next token is `else', parse the else-clause. */
11412 if (cp_lexer_next_token_is_keyword (parser->lexer,
11413 RID_ELSE))
11415 bool discard_else = (cx && !processing_template_decl
11416 && integer_nonzerop (condition));
11417 if (discard_else)
11419 in_discarded_stmt = true;
11420 ++c_inhibit_evaluation_warnings;
11423 guard_tinfo
11424 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11425 /* Consume the `else' keyword. */
11426 cp_lexer_consume_token (parser->lexer);
11427 if (warn_duplicated_cond)
11429 if (cp_lexer_next_token_is_keyword (parser->lexer,
11430 RID_IF)
11431 && chain == NULL)
11433 /* We've got "if (COND) else if (COND2)". Start
11434 the condition chain and add COND as the first
11435 element. */
11436 chain = new vec<tree> ();
11437 if (!CONSTANT_CLASS_P (condition)
11438 && !TREE_SIDE_EFFECTS (condition))
11440 /* Wrap it in a NOP_EXPR so that we can set the
11441 location of the condition. */
11442 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11443 condition);
11444 SET_EXPR_LOCATION (e, token->location);
11445 chain->safe_push (e);
11448 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11449 RID_IF))
11451 /* This is if-else without subsequent if. Zap the
11452 condition chain; we would have already warned at
11453 this point. */
11454 delete chain;
11455 chain = NULL;
11458 begin_else_clause (statement);
11459 /* Parse the else-clause. */
11460 cp_parser_implicitly_scoped_statement (parser, NULL,
11461 guard_tinfo, chain);
11463 finish_else_clause (statement);
11465 /* If we are currently parsing a then-clause, then
11466 IF_P will not be NULL. We set it to true to
11467 indicate that this if statement has an else clause.
11468 This may trigger the Wparentheses warning below
11469 when we get back up to the parent if statement. */
11470 if (if_p != NULL)
11471 *if_p = true;
11473 if (discard_else)
11475 ELSE_CLAUSE (statement) = NULL_TREE;
11476 in_discarded_stmt = was_discarded;
11477 --c_inhibit_evaluation_warnings;
11480 else
11482 /* This if statement does not have an else clause. If
11483 NESTED_IF is true, then the then-clause has an if
11484 statement which does have an else clause. We warn
11485 about the potential ambiguity. */
11486 if (nested_if)
11487 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11488 "suggest explicit braces to avoid ambiguous"
11489 " %<else%>");
11490 if (warn_duplicated_cond)
11492 /* We don't need the condition chain anymore. */
11493 delete chain;
11494 chain = NULL;
11498 /* Now we're all done with the if-statement. */
11499 finish_if_stmt (statement);
11501 else
11503 bool in_switch_statement_p;
11504 unsigned char in_statement;
11506 /* Add the condition. */
11507 finish_switch_cond (condition, statement);
11509 /* Parse the body of the switch-statement. */
11510 in_switch_statement_p = parser->in_switch_statement_p;
11511 in_statement = parser->in_statement;
11512 parser->in_switch_statement_p = true;
11513 parser->in_statement |= IN_SWITCH_STMT;
11514 cp_parser_implicitly_scoped_statement (parser, if_p,
11515 guard_tinfo);
11516 parser->in_switch_statement_p = in_switch_statement_p;
11517 parser->in_statement = in_statement;
11519 /* Now we're all done with the switch-statement. */
11520 finish_switch_stmt (statement);
11523 return statement;
11525 break;
11527 default:
11528 cp_parser_error (parser, "expected selection-statement");
11529 return error_mark_node;
11533 /* Parse a condition.
11535 condition:
11536 expression
11537 type-specifier-seq declarator = initializer-clause
11538 type-specifier-seq declarator braced-init-list
11540 GNU Extension:
11542 condition:
11543 type-specifier-seq declarator asm-specification [opt]
11544 attributes [opt] = assignment-expression
11546 Returns the expression that should be tested. */
11548 static tree
11549 cp_parser_condition (cp_parser* parser)
11551 cp_decl_specifier_seq type_specifiers;
11552 const char *saved_message;
11553 int declares_class_or_enum;
11555 /* Try the declaration first. */
11556 cp_parser_parse_tentatively (parser);
11557 /* New types are not allowed in the type-specifier-seq for a
11558 condition. */
11559 saved_message = parser->type_definition_forbidden_message;
11560 parser->type_definition_forbidden_message
11561 = G_("types may not be defined in conditions");
11562 /* Parse the type-specifier-seq. */
11563 cp_parser_decl_specifier_seq (parser,
11564 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11565 &type_specifiers,
11566 &declares_class_or_enum);
11567 /* Restore the saved message. */
11568 parser->type_definition_forbidden_message = saved_message;
11569 /* If all is well, we might be looking at a declaration. */
11570 if (!cp_parser_error_occurred (parser))
11572 tree decl;
11573 tree asm_specification;
11574 tree attributes;
11575 cp_declarator *declarator;
11576 tree initializer = NULL_TREE;
11578 /* Parse the declarator. */
11579 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11580 /*ctor_dtor_or_conv_p=*/NULL,
11581 /*parenthesized_p=*/NULL,
11582 /*member_p=*/false,
11583 /*friend_p=*/false);
11584 /* Parse the attributes. */
11585 attributes = cp_parser_attributes_opt (parser);
11586 /* Parse the asm-specification. */
11587 asm_specification = cp_parser_asm_specification_opt (parser);
11588 /* If the next token is not an `=' or '{', then we might still be
11589 looking at an expression. For example:
11591 if (A(a).x)
11593 looks like a decl-specifier-seq and a declarator -- but then
11594 there is no `=', so this is an expression. */
11595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11596 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11597 cp_parser_simulate_error (parser);
11599 /* If we did see an `=' or '{', then we are looking at a declaration
11600 for sure. */
11601 if (cp_parser_parse_definitely (parser))
11603 tree pushed_scope;
11604 bool non_constant_p;
11605 int flags = LOOKUP_ONLYCONVERTING;
11607 /* Create the declaration. */
11608 decl = start_decl (declarator, &type_specifiers,
11609 /*initialized_p=*/true,
11610 attributes, /*prefix_attributes=*/NULL_TREE,
11611 &pushed_scope);
11613 /* Parse the initializer. */
11614 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11616 initializer = cp_parser_braced_list (parser, &non_constant_p);
11617 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11618 flags = 0;
11620 else
11622 /* Consume the `='. */
11623 cp_parser_require (parser, CPP_EQ, RT_EQ);
11624 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11626 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11627 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11629 /* Process the initializer. */
11630 cp_finish_decl (decl,
11631 initializer, !non_constant_p,
11632 asm_specification,
11633 flags);
11635 if (pushed_scope)
11636 pop_scope (pushed_scope);
11638 return convert_from_reference (decl);
11641 /* If we didn't even get past the declarator successfully, we are
11642 definitely not looking at a declaration. */
11643 else
11644 cp_parser_abort_tentative_parse (parser);
11646 /* Otherwise, we are looking at an expression. */
11647 return cp_parser_expression (parser);
11650 /* Parses a for-statement or range-for-statement until the closing ')',
11651 not included. */
11653 static tree
11654 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11656 tree init, scope, decl;
11657 bool is_range_for;
11659 /* Begin the for-statement. */
11660 scope = begin_for_scope (&init);
11662 /* Parse the initialization. */
11663 is_range_for = cp_parser_init_statement (parser, &decl);
11665 if (is_range_for)
11666 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11667 else
11668 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11671 static tree
11672 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11673 unsigned short unroll)
11675 /* Normal for loop */
11676 tree condition = NULL_TREE;
11677 tree expression = NULL_TREE;
11678 tree stmt;
11680 stmt = begin_for_stmt (scope, init);
11681 /* The init-statement has already been parsed in
11682 cp_parser_init_statement, so no work is needed here. */
11683 finish_init_stmt (stmt);
11685 /* If there's a condition, process it. */
11686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11687 condition = cp_parser_condition (parser);
11688 else if (ivdep)
11690 cp_parser_error (parser, "missing loop condition in loop with "
11691 "%<GCC ivdep%> pragma");
11692 condition = error_mark_node;
11694 else if (unroll)
11696 cp_parser_error (parser, "missing loop condition in loop with "
11697 "%<GCC unroll%> pragma");
11698 condition = error_mark_node;
11700 finish_for_cond (condition, stmt, ivdep, unroll);
11701 /* Look for the `;'. */
11702 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11704 /* If there's an expression, process it. */
11705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11706 expression = cp_parser_expression (parser);
11707 finish_for_expr (expression, stmt);
11709 return stmt;
11712 /* Tries to parse a range-based for-statement:
11714 range-based-for:
11715 decl-specifier-seq declarator : expression
11717 The decl-specifier-seq declarator and the `:' are already parsed by
11718 cp_parser_init_statement. If processing_template_decl it returns a
11719 newly created RANGE_FOR_STMT; if not, it is converted to a
11720 regular FOR_STMT. */
11722 static tree
11723 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11724 bool ivdep, unsigned short unroll)
11726 tree stmt, range_expr;
11727 auto_vec <cxx_binding *, 16> bindings;
11728 auto_vec <tree, 16> names;
11729 tree decomp_first_name = NULL_TREE;
11730 unsigned int decomp_cnt = 0;
11732 /* Get the range declaration momentarily out of the way so that
11733 the range expression doesn't clash with it. */
11734 if (range_decl != error_mark_node)
11736 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11738 tree v = DECL_VALUE_EXPR (range_decl);
11739 /* For decomposition declaration get all of the corresponding
11740 declarations out of the way. */
11741 if (TREE_CODE (v) == ARRAY_REF
11742 && VAR_P (TREE_OPERAND (v, 0))
11743 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11745 tree d = range_decl;
11746 range_decl = TREE_OPERAND (v, 0);
11747 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11748 decomp_first_name = d;
11749 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11751 tree name = DECL_NAME (d);
11752 names.safe_push (name);
11753 bindings.safe_push (IDENTIFIER_BINDING (name));
11754 IDENTIFIER_BINDING (name)
11755 = IDENTIFIER_BINDING (name)->previous;
11759 if (names.is_empty ())
11761 tree name = DECL_NAME (range_decl);
11762 names.safe_push (name);
11763 bindings.safe_push (IDENTIFIER_BINDING (name));
11764 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11768 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11770 bool expr_non_constant_p;
11771 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11773 else
11774 range_expr = cp_parser_expression (parser);
11776 /* Put the range declaration(s) back into scope. */
11777 for (unsigned int i = 0; i < names.length (); i++)
11779 cxx_binding *binding = bindings[i];
11780 binding->previous = IDENTIFIER_BINDING (names[i]);
11781 IDENTIFIER_BINDING (names[i]) = binding;
11784 /* If in template, STMT is converted to a normal for-statement
11785 at instantiation. If not, it is done just ahead. */
11786 if (processing_template_decl)
11788 if (check_for_bare_parameter_packs (range_expr))
11789 range_expr = error_mark_node;
11790 stmt = begin_range_for_stmt (scope, init);
11791 if (ivdep)
11792 RANGE_FOR_IVDEP (stmt) = 1;
11793 if (unroll)
11794 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11795 finish_range_for_decl (stmt, range_decl, range_expr);
11796 if (!type_dependent_expression_p (range_expr)
11797 /* do_auto_deduction doesn't mess with template init-lists. */
11798 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11799 do_range_for_auto_deduction (range_decl, range_expr);
11801 else
11803 stmt = begin_for_stmt (scope, init);
11804 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11805 decomp_first_name, decomp_cnt, ivdep,
11806 unroll);
11808 return stmt;
11811 /* Subroutine of cp_convert_range_for: given the initializer expression,
11812 builds up the range temporary. */
11814 static tree
11815 build_range_temp (tree range_expr)
11817 tree range_type, range_temp;
11819 /* Find out the type deduced by the declaration
11820 `auto &&__range = range_expr'. */
11821 range_type = cp_build_reference_type (make_auto (), true);
11822 range_type = do_auto_deduction (range_type, range_expr,
11823 type_uses_auto (range_type));
11825 /* Create the __range variable. */
11826 range_temp = build_decl (input_location, VAR_DECL,
11827 get_identifier ("__for_range"), range_type);
11828 TREE_USED (range_temp) = 1;
11829 DECL_ARTIFICIAL (range_temp) = 1;
11831 return range_temp;
11834 /* Used by cp_parser_range_for in template context: we aren't going to
11835 do a full conversion yet, but we still need to resolve auto in the
11836 type of the for-range-declaration if present. This is basically
11837 a shortcut version of cp_convert_range_for. */
11839 static void
11840 do_range_for_auto_deduction (tree decl, tree range_expr)
11842 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11843 if (auto_node)
11845 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11846 range_temp = convert_from_reference (build_range_temp (range_expr));
11847 iter_type = (cp_parser_perform_range_for_lookup
11848 (range_temp, &begin_dummy, &end_dummy));
11849 if (iter_type)
11851 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11852 iter_type);
11853 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11854 RO_UNARY_STAR,
11855 tf_warning_or_error);
11856 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11857 iter_decl, auto_node);
11862 /* Converts a range-based for-statement into a normal
11863 for-statement, as per the definition.
11865 for (RANGE_DECL : RANGE_EXPR)
11866 BLOCK
11868 should be equivalent to:
11871 auto &&__range = RANGE_EXPR;
11872 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11873 __begin != __end;
11874 ++__begin)
11876 RANGE_DECL = *__begin;
11877 BLOCK
11881 If RANGE_EXPR is an array:
11882 BEGIN_EXPR = __range
11883 END_EXPR = __range + ARRAY_SIZE(__range)
11884 Else if RANGE_EXPR has a member 'begin' or 'end':
11885 BEGIN_EXPR = __range.begin()
11886 END_EXPR = __range.end()
11887 Else:
11888 BEGIN_EXPR = begin(__range)
11889 END_EXPR = end(__range);
11891 If __range has a member 'begin' but not 'end', or vice versa, we must
11892 still use the second alternative (it will surely fail, however).
11893 When calling begin()/end() in the third alternative we must use
11894 argument dependent lookup, but always considering 'std' as an associated
11895 namespace. */
11897 tree
11898 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11899 tree decomp_first_name, unsigned int decomp_cnt,
11900 bool ivdep, unsigned short unroll)
11902 tree begin, end;
11903 tree iter_type, begin_expr, end_expr;
11904 tree condition, expression;
11906 range_expr = mark_lvalue_use (range_expr);
11908 if (range_decl == error_mark_node || range_expr == error_mark_node)
11909 /* If an error happened previously do nothing or else a lot of
11910 unhelpful errors would be issued. */
11911 begin_expr = end_expr = iter_type = error_mark_node;
11912 else
11914 tree range_temp;
11916 if (VAR_P (range_expr)
11917 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11918 /* Can't bind a reference to an array of runtime bound. */
11919 range_temp = range_expr;
11920 else
11922 range_temp = build_range_temp (range_expr);
11923 pushdecl (range_temp);
11924 cp_finish_decl (range_temp, range_expr,
11925 /*is_constant_init*/false, NULL_TREE,
11926 LOOKUP_ONLYCONVERTING);
11927 range_temp = convert_from_reference (range_temp);
11929 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11930 &begin_expr, &end_expr);
11933 /* The new for initialization statement. */
11934 begin = build_decl (input_location, VAR_DECL,
11935 get_identifier ("__for_begin"), iter_type);
11936 TREE_USED (begin) = 1;
11937 DECL_ARTIFICIAL (begin) = 1;
11938 pushdecl (begin);
11939 cp_finish_decl (begin, begin_expr,
11940 /*is_constant_init*/false, NULL_TREE,
11941 LOOKUP_ONLYCONVERTING);
11943 if (cxx_dialect >= cxx17)
11944 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11945 end = build_decl (input_location, VAR_DECL,
11946 get_identifier ("__for_end"), iter_type);
11947 TREE_USED (end) = 1;
11948 DECL_ARTIFICIAL (end) = 1;
11949 pushdecl (end);
11950 cp_finish_decl (end, end_expr,
11951 /*is_constant_init*/false, NULL_TREE,
11952 LOOKUP_ONLYCONVERTING);
11954 finish_init_stmt (statement);
11956 /* The new for condition. */
11957 condition = build_x_binary_op (input_location, NE_EXPR,
11958 begin, ERROR_MARK,
11959 end, ERROR_MARK,
11960 NULL, tf_warning_or_error);
11961 finish_for_cond (condition, statement, ivdep, unroll);
11963 /* The new increment expression. */
11964 expression = finish_unary_op_expr (input_location,
11965 PREINCREMENT_EXPR, begin,
11966 tf_warning_or_error);
11967 finish_for_expr (expression, statement);
11969 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11970 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11972 /* The declaration is initialized with *__begin inside the loop body. */
11973 cp_finish_decl (range_decl,
11974 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11975 tf_warning_or_error),
11976 /*is_constant_init*/false, NULL_TREE,
11977 LOOKUP_ONLYCONVERTING);
11978 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11979 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11981 return statement;
11984 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11985 We need to solve both at the same time because the method used
11986 depends on the existence of members begin or end.
11987 Returns the type deduced for the iterator expression. */
11989 static tree
11990 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11992 if (error_operand_p (range))
11994 *begin = *end = error_mark_node;
11995 return error_mark_node;
11998 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12000 error ("range-based %<for%> expression of type %qT "
12001 "has incomplete type", TREE_TYPE (range));
12002 *begin = *end = error_mark_node;
12003 return error_mark_node;
12005 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12007 /* If RANGE is an array, we will use pointer arithmetic. */
12008 *begin = decay_conversion (range, tf_warning_or_error);
12009 *end = build_binary_op (input_location, PLUS_EXPR,
12010 range,
12011 array_type_nelts_top (TREE_TYPE (range)),
12012 false);
12013 return TREE_TYPE (*begin);
12015 else
12017 /* If it is not an array, we must do a bit of magic. */
12018 tree id_begin, id_end;
12019 tree member_begin, member_end;
12021 *begin = *end = error_mark_node;
12023 id_begin = get_identifier ("begin");
12024 id_end = get_identifier ("end");
12025 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12026 /*protect=*/2, /*want_type=*/false,
12027 tf_warning_or_error);
12028 member_end = lookup_member (TREE_TYPE (range), id_end,
12029 /*protect=*/2, /*want_type=*/false,
12030 tf_warning_or_error);
12032 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12034 /* Use the member functions. */
12035 if (member_begin != NULL_TREE)
12036 *begin = cp_parser_range_for_member_function (range, id_begin);
12037 else
12038 error ("range-based %<for%> expression of type %qT has an "
12039 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12041 if (member_end != NULL_TREE)
12042 *end = cp_parser_range_for_member_function (range, id_end);
12043 else
12044 error ("range-based %<for%> expression of type %qT has a "
12045 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12047 else
12049 /* Use global functions with ADL. */
12050 vec<tree, va_gc> *vec;
12051 vec = make_tree_vector ();
12053 vec_safe_push (vec, range);
12055 member_begin = perform_koenig_lookup (id_begin, vec,
12056 tf_warning_or_error);
12057 *begin = finish_call_expr (member_begin, &vec, false, true,
12058 tf_warning_or_error);
12059 member_end = perform_koenig_lookup (id_end, vec,
12060 tf_warning_or_error);
12061 *end = finish_call_expr (member_end, &vec, false, true,
12062 tf_warning_or_error);
12064 release_tree_vector (vec);
12067 /* Last common checks. */
12068 if (*begin == error_mark_node || *end == error_mark_node)
12070 /* If one of the expressions is an error do no more checks. */
12071 *begin = *end = error_mark_node;
12072 return error_mark_node;
12074 else if (type_dependent_expression_p (*begin)
12075 || type_dependent_expression_p (*end))
12076 /* Can happen, when, eg, in a template context, Koenig lookup
12077 can't resolve begin/end (c++/58503). */
12078 return NULL_TREE;
12079 else
12081 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12082 /* The unqualified type of the __begin and __end temporaries should
12083 be the same, as required by the multiple auto declaration. */
12084 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12086 if (cxx_dialect >= cxx17
12087 && (build_x_binary_op (input_location, NE_EXPR,
12088 *begin, ERROR_MARK,
12089 *end, ERROR_MARK,
12090 NULL, tf_none)
12091 != error_mark_node))
12092 /* P0184R0 allows __begin and __end to have different types,
12093 but make sure they are comparable so we can give a better
12094 diagnostic. */;
12095 else
12096 error ("inconsistent begin/end types in range-based %<for%> "
12097 "statement: %qT and %qT",
12098 TREE_TYPE (*begin), TREE_TYPE (*end));
12100 return iter_type;
12105 /* Helper function for cp_parser_perform_range_for_lookup.
12106 Builds a tree for RANGE.IDENTIFIER(). */
12108 static tree
12109 cp_parser_range_for_member_function (tree range, tree identifier)
12111 tree member, res;
12112 vec<tree, va_gc> *vec;
12114 member = finish_class_member_access_expr (range, identifier,
12115 false, tf_warning_or_error);
12116 if (member == error_mark_node)
12117 return error_mark_node;
12119 vec = make_tree_vector ();
12120 res = finish_call_expr (member, &vec,
12121 /*disallow_virtual=*/false,
12122 /*koenig_p=*/false,
12123 tf_warning_or_error);
12124 release_tree_vector (vec);
12125 return res;
12128 /* Parse an iteration-statement.
12130 iteration-statement:
12131 while ( condition ) statement
12132 do statement while ( expression ) ;
12133 for ( init-statement condition [opt] ; expression [opt] )
12134 statement
12136 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12138 static tree
12139 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12140 unsigned short unroll)
12142 cp_token *token;
12143 enum rid keyword;
12144 tree statement;
12145 unsigned char in_statement;
12146 token_indent_info guard_tinfo;
12148 /* Peek at the next token. */
12149 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12150 if (!token)
12151 return error_mark_node;
12153 guard_tinfo = get_token_indent_info (token);
12155 /* Remember whether or not we are already within an iteration
12156 statement. */
12157 in_statement = parser->in_statement;
12159 /* See what kind of keyword it is. */
12160 keyword = token->keyword;
12161 switch (keyword)
12163 case RID_WHILE:
12165 tree condition;
12167 /* Begin the while-statement. */
12168 statement = begin_while_stmt ();
12169 /* Look for the `('. */
12170 matching_parens parens;
12171 parens.require_open (parser);
12172 /* Parse the condition. */
12173 condition = cp_parser_condition (parser);
12174 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12175 /* Look for the `)'. */
12176 parens.require_close (parser);
12177 /* Parse the dependent statement. */
12178 parser->in_statement = IN_ITERATION_STMT;
12179 bool prev = note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12181 note_iteration_stmt_body_end (prev);
12182 parser->in_statement = in_statement;
12183 /* We're done with the while-statement. */
12184 finish_while_stmt (statement);
12186 break;
12188 case RID_DO:
12190 tree expression;
12192 /* Begin the do-statement. */
12193 statement = begin_do_stmt ();
12194 /* Parse the body of the do-statement. */
12195 parser->in_statement = IN_ITERATION_STMT;
12196 bool prev = note_iteration_stmt_body_start ();
12197 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12198 note_iteration_stmt_body_end (prev);
12199 parser->in_statement = in_statement;
12200 finish_do_body (statement);
12201 /* Look for the `while' keyword. */
12202 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12203 /* Look for the `('. */
12204 matching_parens parens;
12205 parens.require_open (parser);
12206 /* Parse the expression. */
12207 expression = cp_parser_expression (parser);
12208 /* We're done with the do-statement. */
12209 finish_do_stmt (expression, statement, ivdep, unroll);
12210 /* Look for the `)'. */
12211 parens.require_close (parser);
12212 /* Look for the `;'. */
12213 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12215 break;
12217 case RID_FOR:
12219 /* Look for the `('. */
12220 matching_parens parens;
12221 parens.require_open (parser);
12223 statement = cp_parser_for (parser, ivdep, unroll);
12225 /* Look for the `)'. */
12226 parens.require_close (parser);
12228 /* Parse the body of the for-statement. */
12229 parser->in_statement = IN_ITERATION_STMT;
12230 bool prev = note_iteration_stmt_body_start ();
12231 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12232 note_iteration_stmt_body_end (prev);
12233 parser->in_statement = in_statement;
12235 /* We're done with the for-statement. */
12236 finish_for_stmt (statement);
12238 break;
12240 default:
12241 cp_parser_error (parser, "expected iteration-statement");
12242 statement = error_mark_node;
12243 break;
12246 return statement;
12249 /* Parse a init-statement or the declarator of a range-based-for.
12250 Returns true if a range-based-for declaration is seen.
12252 init-statement:
12253 expression-statement
12254 simple-declaration */
12256 static bool
12257 cp_parser_init_statement (cp_parser* parser, tree *decl)
12259 /* If the next token is a `;', then we have an empty
12260 expression-statement. Grammatically, this is also a
12261 simple-declaration, but an invalid one, because it does not
12262 declare anything. Therefore, if we did not handle this case
12263 specially, we would issue an error message about an invalid
12264 declaration. */
12265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12267 bool is_range_for = false;
12268 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12270 /* A colon is used in range-based for. */
12271 parser->colon_corrects_to_scope_p = false;
12273 /* We're going to speculatively look for a declaration, falling back
12274 to an expression, if necessary. */
12275 cp_parser_parse_tentatively (parser);
12276 /* Parse the declaration. */
12277 cp_parser_simple_declaration (parser,
12278 /*function_definition_allowed_p=*/false,
12279 decl);
12280 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12281 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12283 /* It is a range-for, consume the ':' */
12284 cp_lexer_consume_token (parser->lexer);
12285 is_range_for = true;
12286 if (cxx_dialect < cxx11)
12288 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12289 "range-based %<for%> loops only available with "
12290 "-std=c++11 or -std=gnu++11");
12291 *decl = error_mark_node;
12294 else
12295 /* The ';' is not consumed yet because we told
12296 cp_parser_simple_declaration not to. */
12297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12299 if (cp_parser_parse_definitely (parser))
12300 return is_range_for;
12301 /* If the tentative parse failed, then we shall need to look for an
12302 expression-statement. */
12304 /* If we are here, it is an expression-statement. */
12305 cp_parser_expression_statement (parser, NULL_TREE);
12306 return false;
12309 /* Parse a jump-statement.
12311 jump-statement:
12312 break ;
12313 continue ;
12314 return expression [opt] ;
12315 return braced-init-list ;
12316 goto identifier ;
12318 GNU extension:
12320 jump-statement:
12321 goto * expression ;
12323 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12325 static tree
12326 cp_parser_jump_statement (cp_parser* parser)
12328 tree statement = error_mark_node;
12329 cp_token *token;
12330 enum rid keyword;
12331 unsigned char in_statement;
12333 /* Peek at the next token. */
12334 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12335 if (!token)
12336 return error_mark_node;
12338 /* See what kind of keyword it is. */
12339 keyword = token->keyword;
12340 switch (keyword)
12342 case RID_BREAK:
12343 in_statement = parser->in_statement & ~IN_IF_STMT;
12344 switch (in_statement)
12346 case 0:
12347 error_at (token->location, "break statement not within loop or switch");
12348 break;
12349 default:
12350 gcc_assert ((in_statement & IN_SWITCH_STMT)
12351 || in_statement == IN_ITERATION_STMT);
12352 statement = finish_break_stmt ();
12353 if (in_statement == IN_ITERATION_STMT)
12354 break_maybe_infinite_loop ();
12355 break;
12356 case IN_OMP_BLOCK:
12357 error_at (token->location, "invalid exit from OpenMP structured block");
12358 break;
12359 case IN_OMP_FOR:
12360 error_at (token->location, "break statement used with OpenMP for loop");
12361 break;
12363 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12364 break;
12366 case RID_CONTINUE:
12367 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12369 case 0:
12370 error_at (token->location, "continue statement not within a loop");
12371 break;
12372 /* Fall through. */
12373 case IN_ITERATION_STMT:
12374 case IN_OMP_FOR:
12375 statement = finish_continue_stmt ();
12376 break;
12377 case IN_OMP_BLOCK:
12378 error_at (token->location, "invalid exit from OpenMP structured block");
12379 break;
12380 default:
12381 gcc_unreachable ();
12383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12384 break;
12386 case RID_RETURN:
12388 tree expr;
12389 bool expr_non_constant_p;
12391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12393 cp_lexer_set_source_position (parser->lexer);
12394 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12395 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12397 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12398 expr = cp_parser_expression (parser);
12399 else
12400 /* If the next token is a `;', then there is no
12401 expression. */
12402 expr = NULL_TREE;
12403 /* Build the return-statement. */
12404 if (current_function_auto_return_pattern && in_discarded_stmt)
12405 /* Don't deduce from a discarded return statement. */;
12406 else
12407 statement = finish_return_stmt (expr);
12408 /* Look for the final `;'. */
12409 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12411 break;
12413 case RID_GOTO:
12414 if (parser->in_function_body
12415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12417 error ("%<goto%> in %<constexpr%> function");
12418 cp_function_chain->invalid_constexpr = true;
12421 /* Create the goto-statement. */
12422 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12424 /* Issue a warning about this use of a GNU extension. */
12425 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12426 /* Consume the '*' token. */
12427 cp_lexer_consume_token (parser->lexer);
12428 /* Parse the dependent expression. */
12429 finish_goto_stmt (cp_parser_expression (parser));
12431 else
12432 finish_goto_stmt (cp_parser_identifier (parser));
12433 /* Look for the final `;'. */
12434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12435 break;
12437 default:
12438 cp_parser_error (parser, "expected jump-statement");
12439 break;
12442 return statement;
12445 /* Parse a declaration-statement.
12447 declaration-statement:
12448 block-declaration */
12450 static void
12451 cp_parser_declaration_statement (cp_parser* parser)
12453 void *p;
12455 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12456 p = obstack_alloc (&declarator_obstack, 0);
12458 /* Parse the block-declaration. */
12459 cp_parser_block_declaration (parser, /*statement_p=*/true);
12461 /* Free any declarators allocated. */
12462 obstack_free (&declarator_obstack, p);
12465 /* Some dependent statements (like `if (cond) statement'), are
12466 implicitly in their own scope. In other words, if the statement is
12467 a single statement (as opposed to a compound-statement), it is
12468 none-the-less treated as if it were enclosed in braces. Any
12469 declarations appearing in the dependent statement are out of scope
12470 after control passes that point. This function parses a statement,
12471 but ensures that is in its own scope, even if it is not a
12472 compound-statement.
12474 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12475 is a (possibly labeled) if statement which is not enclosed in
12476 braces and has an else clause. This is used to implement
12477 -Wparentheses.
12479 CHAIN is a vector of if-else-if conditions. This is used to implement
12480 -Wduplicated-cond.
12482 Returns the new statement. */
12484 static tree
12485 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12486 const token_indent_info &guard_tinfo,
12487 vec<tree> *chain)
12489 tree statement;
12490 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12491 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12492 token_indent_info body_tinfo
12493 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12495 if (if_p != NULL)
12496 *if_p = false;
12498 /* Mark if () ; with a special NOP_EXPR. */
12499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12501 cp_lexer_consume_token (parser->lexer);
12502 statement = add_stmt (build_empty_stmt (body_loc));
12504 if (guard_tinfo.keyword == RID_IF
12505 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12506 warning_at (body_loc, OPT_Wempty_body,
12507 "suggest braces around empty body in an %<if%> statement");
12508 else if (guard_tinfo.keyword == RID_ELSE)
12509 warning_at (body_loc, OPT_Wempty_body,
12510 "suggest braces around empty body in an %<else%> statement");
12512 /* if a compound is opened, we simply parse the statement directly. */
12513 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12514 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12515 /* If the token is not a `{', then we must take special action. */
12516 else
12518 /* Create a compound-statement. */
12519 statement = begin_compound_stmt (0);
12520 /* Parse the dependent-statement. */
12521 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12522 &body_loc_after_labels);
12523 /* Finish the dummy compound-statement. */
12524 finish_compound_stmt (statement);
12527 token_indent_info next_tinfo
12528 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12529 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12531 if (body_loc_after_labels != UNKNOWN_LOCATION
12532 && next_tinfo.type != CPP_SEMICOLON)
12533 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12534 guard_tinfo.location, guard_tinfo.keyword);
12536 /* Return the statement. */
12537 return statement;
12540 /* For some dependent statements (like `while (cond) statement'), we
12541 have already created a scope. Therefore, even if the dependent
12542 statement is a compound-statement, we do not want to create another
12543 scope. */
12545 static void
12546 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12547 const token_indent_info &guard_tinfo)
12549 /* If the token is a `{', then we must take special action. */
12550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12552 token_indent_info body_tinfo
12553 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12554 location_t loc_after_labels = UNKNOWN_LOCATION;
12556 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12557 &loc_after_labels);
12558 token_indent_info next_tinfo
12559 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12560 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12562 if (loc_after_labels != UNKNOWN_LOCATION
12563 && next_tinfo.type != CPP_SEMICOLON)
12564 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12565 guard_tinfo.location,
12566 guard_tinfo.keyword);
12568 else
12570 /* Avoid calling cp_parser_compound_statement, so that we
12571 don't create a new scope. Do everything else by hand. */
12572 matching_braces braces;
12573 braces.require_open (parser);
12574 /* If the next keyword is `__label__' we have a label declaration. */
12575 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12576 cp_parser_label_declaration (parser);
12577 /* Parse an (optional) statement-seq. */
12578 cp_parser_statement_seq_opt (parser, NULL_TREE);
12579 braces.require_close (parser);
12583 /* Declarations [gram.dcl.dcl] */
12585 /* Parse an optional declaration-sequence.
12587 declaration-seq:
12588 declaration
12589 declaration-seq declaration */
12591 static void
12592 cp_parser_declaration_seq_opt (cp_parser* parser)
12594 while (true)
12596 cp_token *token;
12598 token = cp_lexer_peek_token (parser->lexer);
12600 if (token->type == CPP_CLOSE_BRACE
12601 || token->type == CPP_EOF
12602 || token->type == CPP_PRAGMA_EOL)
12603 break;
12605 if (token->type == CPP_SEMICOLON)
12607 /* A declaration consisting of a single semicolon is
12608 invalid. Allow it unless we're being pedantic. */
12609 cp_lexer_consume_token (parser->lexer);
12610 if (!in_system_header_at (input_location))
12611 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12612 continue;
12615 /* If we're entering or exiting a region that's implicitly
12616 extern "C", modify the lang context appropriately. */
12617 if (!parser->implicit_extern_c && token->implicit_extern_c)
12619 push_lang_context (lang_name_c);
12620 parser->implicit_extern_c = true;
12622 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12624 pop_lang_context ();
12625 parser->implicit_extern_c = false;
12628 if (token->type == CPP_PRAGMA)
12630 /* A top-level declaration can consist solely of a #pragma.
12631 A nested declaration cannot, so this is done here and not
12632 in cp_parser_declaration. (A #pragma at block scope is
12633 handled in cp_parser_statement.) */
12634 cp_parser_pragma (parser, pragma_external, NULL);
12635 continue;
12638 /* Parse the declaration itself. */
12639 cp_parser_declaration (parser);
12643 /* Parse a declaration.
12645 declaration:
12646 block-declaration
12647 function-definition
12648 template-declaration
12649 explicit-instantiation
12650 explicit-specialization
12651 linkage-specification
12652 namespace-definition
12654 C++17:
12655 deduction-guide
12657 GNU extension:
12659 declaration:
12660 __extension__ declaration */
12662 static void
12663 cp_parser_declaration (cp_parser* parser)
12665 cp_token token1;
12666 cp_token token2;
12667 int saved_pedantic;
12668 void *p;
12669 tree attributes = NULL_TREE;
12671 /* Check for the `__extension__' keyword. */
12672 if (cp_parser_extension_opt (parser, &saved_pedantic))
12674 /* Parse the qualified declaration. */
12675 cp_parser_declaration (parser);
12676 /* Restore the PEDANTIC flag. */
12677 pedantic = saved_pedantic;
12679 return;
12682 /* Try to figure out what kind of declaration is present. */
12683 token1 = *cp_lexer_peek_token (parser->lexer);
12685 if (token1.type != CPP_EOF)
12686 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12687 else
12689 token2.type = CPP_EOF;
12690 token2.keyword = RID_MAX;
12693 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12694 p = obstack_alloc (&declarator_obstack, 0);
12696 /* If the next token is `extern' and the following token is a string
12697 literal, then we have a linkage specification. */
12698 if (token1.keyword == RID_EXTERN
12699 && cp_parser_is_pure_string_literal (&token2))
12700 cp_parser_linkage_specification (parser);
12701 /* If the next token is `template', then we have either a template
12702 declaration, an explicit instantiation, or an explicit
12703 specialization. */
12704 else if (token1.keyword == RID_TEMPLATE)
12706 /* `template <>' indicates a template specialization. */
12707 if (token2.type == CPP_LESS
12708 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12709 cp_parser_explicit_specialization (parser);
12710 /* `template <' indicates a template declaration. */
12711 else if (token2.type == CPP_LESS)
12712 cp_parser_template_declaration (parser, /*member_p=*/false);
12713 /* Anything else must be an explicit instantiation. */
12714 else
12715 cp_parser_explicit_instantiation (parser);
12717 /* If the next token is `export', then we have a template
12718 declaration. */
12719 else if (token1.keyword == RID_EXPORT)
12720 cp_parser_template_declaration (parser, /*member_p=*/false);
12721 /* If the next token is `extern', 'static' or 'inline' and the one
12722 after that is `template', we have a GNU extended explicit
12723 instantiation directive. */
12724 else if (cp_parser_allow_gnu_extensions_p (parser)
12725 && (token1.keyword == RID_EXTERN
12726 || token1.keyword == RID_STATIC
12727 || token1.keyword == RID_INLINE)
12728 && token2.keyword == RID_TEMPLATE)
12729 cp_parser_explicit_instantiation (parser);
12730 /* If the next token is `namespace', check for a named or unnamed
12731 namespace definition. */
12732 else if (token1.keyword == RID_NAMESPACE
12733 && (/* A named namespace definition. */
12734 (token2.type == CPP_NAME
12735 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12736 != CPP_EQ))
12737 || (token2.type == CPP_OPEN_SQUARE
12738 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12739 == CPP_OPEN_SQUARE)
12740 /* An unnamed namespace definition. */
12741 || token2.type == CPP_OPEN_BRACE
12742 || token2.keyword == RID_ATTRIBUTE))
12743 cp_parser_namespace_definition (parser);
12744 /* An inline (associated) namespace definition. */
12745 else if (token1.keyword == RID_INLINE
12746 && token2.keyword == RID_NAMESPACE)
12747 cp_parser_namespace_definition (parser);
12748 /* Objective-C++ declaration/definition. */
12749 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12750 cp_parser_objc_declaration (parser, NULL_TREE);
12751 else if (c_dialect_objc ()
12752 && token1.keyword == RID_ATTRIBUTE
12753 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12754 cp_parser_objc_declaration (parser, attributes);
12755 /* At this point we may have a template declared by a concept
12756 introduction. */
12757 else if (flag_concepts
12758 && cp_parser_template_declaration_after_export (parser,
12759 /*member_p=*/false))
12760 /* We did. */;
12761 else
12762 /* Try to parse a block-declaration, or a function-definition. */
12763 cp_parser_block_declaration (parser, /*statement_p=*/false);
12765 /* Free any declarators allocated. */
12766 obstack_free (&declarator_obstack, p);
12769 /* Parse a block-declaration.
12771 block-declaration:
12772 simple-declaration
12773 asm-definition
12774 namespace-alias-definition
12775 using-declaration
12776 using-directive
12778 GNU Extension:
12780 block-declaration:
12781 __extension__ block-declaration
12783 C++0x Extension:
12785 block-declaration:
12786 static_assert-declaration
12788 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12789 part of a declaration-statement. */
12791 static void
12792 cp_parser_block_declaration (cp_parser *parser,
12793 bool statement_p)
12795 cp_token *token1;
12796 int saved_pedantic;
12798 /* Check for the `__extension__' keyword. */
12799 if (cp_parser_extension_opt (parser, &saved_pedantic))
12801 /* Parse the qualified declaration. */
12802 cp_parser_block_declaration (parser, statement_p);
12803 /* Restore the PEDANTIC flag. */
12804 pedantic = saved_pedantic;
12806 return;
12809 /* Peek at the next token to figure out which kind of declaration is
12810 present. */
12811 token1 = cp_lexer_peek_token (parser->lexer);
12813 /* If the next keyword is `asm', we have an asm-definition. */
12814 if (token1->keyword == RID_ASM)
12816 if (statement_p)
12817 cp_parser_commit_to_tentative_parse (parser);
12818 cp_parser_asm_definition (parser);
12820 /* If the next keyword is `namespace', we have a
12821 namespace-alias-definition. */
12822 else if (token1->keyword == RID_NAMESPACE)
12823 cp_parser_namespace_alias_definition (parser);
12824 /* If the next keyword is `using', we have a
12825 using-declaration, a using-directive, or an alias-declaration. */
12826 else if (token1->keyword == RID_USING)
12828 cp_token *token2;
12830 if (statement_p)
12831 cp_parser_commit_to_tentative_parse (parser);
12832 /* If the token after `using' is `namespace', then we have a
12833 using-directive. */
12834 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12835 if (token2->keyword == RID_NAMESPACE)
12836 cp_parser_using_directive (parser);
12837 /* If the second token after 'using' is '=', then we have an
12838 alias-declaration. */
12839 else if (cxx_dialect >= cxx11
12840 && token2->type == CPP_NAME
12841 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12842 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12843 cp_parser_alias_declaration (parser);
12844 /* Otherwise, it's a using-declaration. */
12845 else
12846 cp_parser_using_declaration (parser,
12847 /*access_declaration_p=*/false);
12849 /* If the next keyword is `__label__' we have a misplaced label
12850 declaration. */
12851 else if (token1->keyword == RID_LABEL)
12853 cp_lexer_consume_token (parser->lexer);
12854 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12855 cp_parser_skip_to_end_of_statement (parser);
12856 /* If the next token is now a `;', consume it. */
12857 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12858 cp_lexer_consume_token (parser->lexer);
12860 /* If the next token is `static_assert' we have a static assertion. */
12861 else if (token1->keyword == RID_STATIC_ASSERT)
12862 cp_parser_static_assert (parser, /*member_p=*/false);
12863 /* Anything else must be a simple-declaration. */
12864 else
12865 cp_parser_simple_declaration (parser, !statement_p,
12866 /*maybe_range_for_decl*/NULL);
12869 /* Parse a simple-declaration.
12871 simple-declaration:
12872 decl-specifier-seq [opt] init-declarator-list [opt] ;
12873 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12874 brace-or-equal-initializer ;
12876 init-declarator-list:
12877 init-declarator
12878 init-declarator-list , init-declarator
12880 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12881 function-definition as a simple-declaration.
12883 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12884 parsed declaration if it is an uninitialized single declarator not followed
12885 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12886 if present, will not be consumed. */
12888 static void
12889 cp_parser_simple_declaration (cp_parser* parser,
12890 bool function_definition_allowed_p,
12891 tree *maybe_range_for_decl)
12893 cp_decl_specifier_seq decl_specifiers;
12894 int declares_class_or_enum;
12895 bool saw_declarator;
12896 location_t comma_loc = UNKNOWN_LOCATION;
12897 location_t init_loc = UNKNOWN_LOCATION;
12899 if (maybe_range_for_decl)
12900 *maybe_range_for_decl = NULL_TREE;
12902 /* Defer access checks until we know what is being declared; the
12903 checks for names appearing in the decl-specifier-seq should be
12904 done as if we were in the scope of the thing being declared. */
12905 push_deferring_access_checks (dk_deferred);
12907 /* Parse the decl-specifier-seq. We have to keep track of whether
12908 or not the decl-specifier-seq declares a named class or
12909 enumeration type, since that is the only case in which the
12910 init-declarator-list is allowed to be empty.
12912 [dcl.dcl]
12914 In a simple-declaration, the optional init-declarator-list can be
12915 omitted only when declaring a class or enumeration, that is when
12916 the decl-specifier-seq contains either a class-specifier, an
12917 elaborated-type-specifier, or an enum-specifier. */
12918 cp_parser_decl_specifier_seq (parser,
12919 CP_PARSER_FLAGS_OPTIONAL,
12920 &decl_specifiers,
12921 &declares_class_or_enum);
12922 /* We no longer need to defer access checks. */
12923 stop_deferring_access_checks ();
12925 /* In a block scope, a valid declaration must always have a
12926 decl-specifier-seq. By not trying to parse declarators, we can
12927 resolve the declaration/expression ambiguity more quickly. */
12928 if (!function_definition_allowed_p
12929 && !decl_specifiers.any_specifiers_p)
12931 cp_parser_error (parser, "expected declaration");
12932 goto done;
12935 /* If the next two tokens are both identifiers, the code is
12936 erroneous. The usual cause of this situation is code like:
12938 T t;
12940 where "T" should name a type -- but does not. */
12941 if (!decl_specifiers.any_type_specifiers_p
12942 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12944 /* If parsing tentatively, we should commit; we really are
12945 looking at a declaration. */
12946 cp_parser_commit_to_tentative_parse (parser);
12947 /* Give up. */
12948 goto done;
12951 /* If we have seen at least one decl-specifier, and the next token
12952 is not a parenthesis, then we must be looking at a declaration.
12953 (After "int (" we might be looking at a functional cast.) */
12954 if (decl_specifiers.any_specifiers_p
12955 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12956 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12957 && !cp_parser_error_occurred (parser))
12958 cp_parser_commit_to_tentative_parse (parser);
12960 /* Look for C++17 decomposition declaration. */
12961 for (size_t n = 1; ; n++)
12962 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12963 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12964 continue;
12965 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12966 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12967 && decl_specifiers.any_specifiers_p)
12969 tree decl
12970 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12971 maybe_range_for_decl,
12972 &init_loc);
12974 /* The next token should be either a `,' or a `;'. */
12975 cp_token *token = cp_lexer_peek_token (parser->lexer);
12976 /* If it's a `;', we are done. */
12977 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12978 goto finish;
12979 /* Anything else is an error. */
12980 else
12982 /* If we have already issued an error message we don't need
12983 to issue another one. */
12984 if ((decl != error_mark_node
12985 && DECL_INITIAL (decl) != error_mark_node)
12986 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12987 cp_parser_error (parser, "expected %<,%> or %<;%>");
12988 /* Skip tokens until we reach the end of the statement. */
12989 cp_parser_skip_to_end_of_statement (parser);
12990 /* If the next token is now a `;', consume it. */
12991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12992 cp_lexer_consume_token (parser->lexer);
12993 goto done;
12996 else
12997 break;
12999 tree last_type;
13000 bool auto_specifier_p;
13001 /* NULL_TREE if both variable and function declaration are allowed,
13002 error_mark_node if function declaration are not allowed and
13003 a FUNCTION_DECL that should be diagnosed if it is followed by
13004 variable declarations. */
13005 tree auto_function_declaration;
13007 last_type = NULL_TREE;
13008 auto_specifier_p
13009 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13010 auto_function_declaration = NULL_TREE;
13012 /* Keep going until we hit the `;' at the end of the simple
13013 declaration. */
13014 saw_declarator = false;
13015 while (cp_lexer_next_token_is_not (parser->lexer,
13016 CPP_SEMICOLON))
13018 cp_token *token;
13019 bool function_definition_p;
13020 tree decl;
13021 tree auto_result = NULL_TREE;
13023 if (saw_declarator)
13025 /* If we are processing next declarator, comma is expected */
13026 token = cp_lexer_peek_token (parser->lexer);
13027 gcc_assert (token->type == CPP_COMMA);
13028 cp_lexer_consume_token (parser->lexer);
13029 if (maybe_range_for_decl)
13031 *maybe_range_for_decl = error_mark_node;
13032 if (comma_loc == UNKNOWN_LOCATION)
13033 comma_loc = token->location;
13036 else
13037 saw_declarator = true;
13039 /* Parse the init-declarator. */
13040 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13041 /*checks=*/NULL,
13042 function_definition_allowed_p,
13043 /*member_p=*/false,
13044 declares_class_or_enum,
13045 &function_definition_p,
13046 maybe_range_for_decl,
13047 &init_loc,
13048 &auto_result);
13049 /* If an error occurred while parsing tentatively, exit quickly.
13050 (That usually happens when in the body of a function; each
13051 statement is treated as a declaration-statement until proven
13052 otherwise.) */
13053 if (cp_parser_error_occurred (parser))
13054 goto done;
13056 if (auto_specifier_p && cxx_dialect >= cxx14)
13058 /* If the init-declarator-list contains more than one
13059 init-declarator, they shall all form declarations of
13060 variables. */
13061 if (auto_function_declaration == NULL_TREE)
13062 auto_function_declaration
13063 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13064 else if (TREE_CODE (decl) == FUNCTION_DECL
13065 || auto_function_declaration != error_mark_node)
13067 error_at (decl_specifiers.locations[ds_type_spec],
13068 "non-variable %qD in declaration with more than one "
13069 "declarator with placeholder type",
13070 TREE_CODE (decl) == FUNCTION_DECL
13071 ? decl : auto_function_declaration);
13072 auto_function_declaration = error_mark_node;
13076 if (auto_result
13077 && (!processing_template_decl || !type_uses_auto (auto_result)))
13079 if (last_type
13080 && last_type != error_mark_node
13081 && !same_type_p (auto_result, last_type))
13083 /* If the list of declarators contains more than one declarator,
13084 the type of each declared variable is determined as described
13085 above. If the type deduced for the template parameter U is not
13086 the same in each deduction, the program is ill-formed. */
13087 error_at (decl_specifiers.locations[ds_type_spec],
13088 "inconsistent deduction for %qT: %qT and then %qT",
13089 decl_specifiers.type, last_type, auto_result);
13090 last_type = error_mark_node;
13092 else
13093 last_type = auto_result;
13096 /* Handle function definitions specially. */
13097 if (function_definition_p)
13099 /* If the next token is a `,', then we are probably
13100 processing something like:
13102 void f() {}, *p;
13104 which is erroneous. */
13105 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13107 cp_token *token = cp_lexer_peek_token (parser->lexer);
13108 error_at (token->location,
13109 "mixing"
13110 " declarations and function-definitions is forbidden");
13112 /* Otherwise, we're done with the list of declarators. */
13113 else
13115 pop_deferring_access_checks ();
13116 return;
13119 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13120 *maybe_range_for_decl = decl;
13121 /* The next token should be either a `,' or a `;'. */
13122 token = cp_lexer_peek_token (parser->lexer);
13123 /* If it's a `,', there are more declarators to come. */
13124 if (token->type == CPP_COMMA)
13125 /* will be consumed next time around */;
13126 /* If it's a `;', we are done. */
13127 else if (token->type == CPP_SEMICOLON)
13128 break;
13129 else if (maybe_range_for_decl)
13131 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13132 permerror (decl_specifiers.locations[ds_type_spec],
13133 "types may not be defined in a for-range-declaration");
13134 break;
13136 /* Anything else is an error. */
13137 else
13139 /* If we have already issued an error message we don't need
13140 to issue another one. */
13141 if ((decl != error_mark_node
13142 && DECL_INITIAL (decl) != error_mark_node)
13143 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13144 cp_parser_error (parser, "expected %<,%> or %<;%>");
13145 /* Skip tokens until we reach the end of the statement. */
13146 cp_parser_skip_to_end_of_statement (parser);
13147 /* If the next token is now a `;', consume it. */
13148 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13149 cp_lexer_consume_token (parser->lexer);
13150 goto done;
13152 /* After the first time around, a function-definition is not
13153 allowed -- even if it was OK at first. For example:
13155 int i, f() {}
13157 is not valid. */
13158 function_definition_allowed_p = false;
13161 /* Issue an error message if no declarators are present, and the
13162 decl-specifier-seq does not itself declare a class or
13163 enumeration: [dcl.dcl]/3. */
13164 if (!saw_declarator)
13166 if (cp_parser_declares_only_class_p (parser))
13168 if (!declares_class_or_enum
13169 && decl_specifiers.type
13170 && OVERLOAD_TYPE_P (decl_specifiers.type))
13171 /* Ensure an error is issued anyway when finish_decltype_type,
13172 called via cp_parser_decl_specifier_seq, returns a class or
13173 an enumeration (c++/51786). */
13174 decl_specifiers.type = NULL_TREE;
13175 shadow_tag (&decl_specifiers);
13177 /* Perform any deferred access checks. */
13178 perform_deferred_access_checks (tf_warning_or_error);
13181 /* Consume the `;'. */
13182 finish:
13183 if (!maybe_range_for_decl)
13184 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13185 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13187 if (init_loc != UNKNOWN_LOCATION)
13188 error_at (init_loc, "initializer in range-based %<for%> loop");
13189 if (comma_loc != UNKNOWN_LOCATION)
13190 error_at (comma_loc,
13191 "multiple declarations in range-based %<for%> loop");
13194 done:
13195 pop_deferring_access_checks ();
13198 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13199 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13200 initializer ; */
13202 static tree
13203 cp_parser_decomposition_declaration (cp_parser *parser,
13204 cp_decl_specifier_seq *decl_specifiers,
13205 tree *maybe_range_for_decl,
13206 location_t *init_loc)
13208 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13209 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13210 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13212 /* Parse the identifier-list. */
13213 auto_vec<cp_expr, 10> v;
13214 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13215 while (true)
13217 cp_expr e = cp_parser_identifier (parser);
13218 if (e.get_value () == error_mark_node)
13219 break;
13220 v.safe_push (e);
13221 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13222 break;
13223 cp_lexer_consume_token (parser->lexer);
13226 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13227 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13229 end_loc = UNKNOWN_LOCATION;
13230 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13231 false);
13232 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13233 cp_lexer_consume_token (parser->lexer);
13234 else
13236 cp_parser_skip_to_end_of_statement (parser);
13237 return error_mark_node;
13241 if (cxx_dialect < cxx17)
13242 pedwarn (loc, 0, "structured bindings only available with "
13243 "-std=c++17 or -std=gnu++17");
13245 tree pushed_scope;
13246 cp_declarator *declarator = make_declarator (cdk_decomp);
13247 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13248 declarator->id_loc = loc;
13249 if (ref_qual != REF_QUAL_NONE)
13250 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13251 ref_qual == REF_QUAL_RVALUE,
13252 NULL_TREE);
13253 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13254 NULL_TREE, decl_specifiers->attributes,
13255 &pushed_scope);
13256 tree orig_decl = decl;
13258 unsigned int i;
13259 cp_expr e;
13260 cp_decl_specifier_seq decl_specs;
13261 clear_decl_specs (&decl_specs);
13262 decl_specs.type = make_auto ();
13263 tree prev = decl;
13264 FOR_EACH_VEC_ELT (v, i, e)
13266 if (i == 0)
13267 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13268 else
13269 declarator->u.id.unqualified_name = e.get_value ();
13270 declarator->id_loc = e.get_location ();
13271 tree elt_pushed_scope;
13272 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13273 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13274 if (decl2 == error_mark_node)
13275 decl = error_mark_node;
13276 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13278 /* Ensure we've diagnosed redeclaration if we aren't creating
13279 a new VAR_DECL. */
13280 gcc_assert (errorcount);
13281 decl = error_mark_node;
13283 else
13284 prev = decl2;
13285 if (elt_pushed_scope)
13286 pop_scope (elt_pushed_scope);
13289 if (v.is_empty ())
13291 error_at (loc, "empty structured binding declaration");
13292 decl = error_mark_node;
13295 if (maybe_range_for_decl == NULL
13296 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13298 bool non_constant_p = false, is_direct_init = false;
13299 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13300 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13301 &non_constant_p);
13302 if (initializer == NULL_TREE
13303 || (TREE_CODE (initializer) == TREE_LIST
13304 && TREE_CHAIN (initializer))
13305 || (is_direct_init
13306 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13307 && CONSTRUCTOR_NELTS (initializer) != 1))
13309 error_at (loc, "invalid initializer for structured binding "
13310 "declaration");
13311 initializer = error_mark_node;
13314 if (decl != error_mark_node)
13316 cp_maybe_mangle_decomp (decl, prev, v.length ());
13317 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13318 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13319 cp_finish_decomp (decl, prev, v.length ());
13322 else if (decl != error_mark_node)
13324 *maybe_range_for_decl = prev;
13325 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13326 the underlying DECL. */
13327 cp_finish_decomp (decl, prev, v.length ());
13330 if (pushed_scope)
13331 pop_scope (pushed_scope);
13333 if (decl == error_mark_node && DECL_P (orig_decl))
13335 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13336 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13339 return decl;
13342 /* Parse a decl-specifier-seq.
13344 decl-specifier-seq:
13345 decl-specifier-seq [opt] decl-specifier
13346 decl-specifier attribute-specifier-seq [opt] (C++11)
13348 decl-specifier:
13349 storage-class-specifier
13350 type-specifier
13351 function-specifier
13352 friend
13353 typedef
13355 GNU Extension:
13357 decl-specifier:
13358 attributes
13360 Concepts Extension:
13362 decl-specifier:
13363 concept
13365 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13367 The parser flags FLAGS is used to control type-specifier parsing.
13369 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13370 flags:
13372 1: one of the decl-specifiers is an elaborated-type-specifier
13373 (i.e., a type declaration)
13374 2: one of the decl-specifiers is an enum-specifier or a
13375 class-specifier (i.e., a type definition)
13379 static void
13380 cp_parser_decl_specifier_seq (cp_parser* parser,
13381 cp_parser_flags flags,
13382 cp_decl_specifier_seq *decl_specs,
13383 int* declares_class_or_enum)
13385 bool constructor_possible_p = !parser->in_declarator_p;
13386 bool found_decl_spec = false;
13387 cp_token *start_token = NULL;
13388 cp_decl_spec ds;
13390 /* Clear DECL_SPECS. */
13391 clear_decl_specs (decl_specs);
13393 /* Assume no class or enumeration type is declared. */
13394 *declares_class_or_enum = 0;
13396 /* Keep reading specifiers until there are no more to read. */
13397 while (true)
13399 bool constructor_p;
13400 cp_token *token;
13401 ds = ds_last;
13403 /* Peek at the next token. */
13404 token = cp_lexer_peek_token (parser->lexer);
13406 /* Save the first token of the decl spec list for error
13407 reporting. */
13408 if (!start_token)
13409 start_token = token;
13410 /* Handle attributes. */
13411 if (cp_next_tokens_can_be_attribute_p (parser))
13413 /* Parse the attributes. */
13414 tree attrs = cp_parser_attributes_opt (parser);
13416 /* In a sequence of declaration specifiers, c++11 attributes
13417 appertain to the type that precede them. In that case
13418 [dcl.spec]/1 says:
13420 The attribute-specifier-seq affects the type only for
13421 the declaration it appears in, not other declarations
13422 involving the same type.
13424 But for now let's force the user to position the
13425 attribute either at the beginning of the declaration or
13426 after the declarator-id, which would clearly mean that it
13427 applies to the declarator. */
13428 if (cxx11_attribute_p (attrs))
13430 if (!found_decl_spec)
13431 /* The c++11 attribute is at the beginning of the
13432 declaration. It appertains to the entity being
13433 declared. */;
13434 else
13436 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13438 /* This is an attribute following a
13439 class-specifier. */
13440 if (decl_specs->type_definition_p)
13441 warn_misplaced_attr_for_class_type (token->location,
13442 decl_specs->type);
13443 attrs = NULL_TREE;
13445 else
13447 decl_specs->std_attributes
13448 = attr_chainon (decl_specs->std_attributes, attrs);
13449 if (decl_specs->locations[ds_std_attribute] == 0)
13450 decl_specs->locations[ds_std_attribute] = token->location;
13452 continue;
13456 decl_specs->attributes
13457 = attr_chainon (decl_specs->attributes, attrs);
13458 if (decl_specs->locations[ds_attribute] == 0)
13459 decl_specs->locations[ds_attribute] = token->location;
13460 continue;
13462 /* Assume we will find a decl-specifier keyword. */
13463 found_decl_spec = true;
13464 /* If the next token is an appropriate keyword, we can simply
13465 add it to the list. */
13466 switch (token->keyword)
13468 /* decl-specifier:
13469 friend
13470 constexpr */
13471 case RID_FRIEND:
13472 if (!at_class_scope_p ())
13474 gcc_rich_location richloc (token->location);
13475 richloc.add_fixit_remove ();
13476 error_at (&richloc, "%<friend%> used outside of class");
13477 cp_lexer_purge_token (parser->lexer);
13479 else
13481 ds = ds_friend;
13482 /* Consume the token. */
13483 cp_lexer_consume_token (parser->lexer);
13485 break;
13487 case RID_CONSTEXPR:
13488 ds = ds_constexpr;
13489 cp_lexer_consume_token (parser->lexer);
13490 break;
13492 case RID_CONCEPT:
13493 ds = ds_concept;
13494 cp_lexer_consume_token (parser->lexer);
13495 break;
13497 /* function-specifier:
13498 inline
13499 virtual
13500 explicit */
13501 case RID_INLINE:
13502 case RID_VIRTUAL:
13503 case RID_EXPLICIT:
13504 cp_parser_function_specifier_opt (parser, decl_specs);
13505 break;
13507 /* decl-specifier:
13508 typedef */
13509 case RID_TYPEDEF:
13510 ds = ds_typedef;
13511 /* Consume the token. */
13512 cp_lexer_consume_token (parser->lexer);
13513 /* A constructor declarator cannot appear in a typedef. */
13514 constructor_possible_p = false;
13515 /* The "typedef" keyword can only occur in a declaration; we
13516 may as well commit at this point. */
13517 cp_parser_commit_to_tentative_parse (parser);
13519 if (decl_specs->storage_class != sc_none)
13520 decl_specs->conflicting_specifiers_p = true;
13521 break;
13523 /* storage-class-specifier:
13524 auto
13525 register
13526 static
13527 extern
13528 mutable
13530 GNU Extension:
13531 thread */
13532 case RID_AUTO:
13533 if (cxx_dialect == cxx98)
13535 /* Consume the token. */
13536 cp_lexer_consume_token (parser->lexer);
13538 /* Complain about `auto' as a storage specifier, if
13539 we're complaining about C++0x compatibility. */
13540 gcc_rich_location richloc (token->location);
13541 richloc.add_fixit_remove ();
13542 warning_at (&richloc, OPT_Wc__11_compat,
13543 "%<auto%> changes meaning in C++11; "
13544 "please remove it");
13546 /* Set the storage class anyway. */
13547 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13548 token);
13550 else
13551 /* C++0x auto type-specifier. */
13552 found_decl_spec = false;
13553 break;
13555 case RID_REGISTER:
13556 case RID_STATIC:
13557 case RID_EXTERN:
13558 case RID_MUTABLE:
13559 /* Consume the token. */
13560 cp_lexer_consume_token (parser->lexer);
13561 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13562 token);
13563 break;
13564 case RID_THREAD:
13565 /* Consume the token. */
13566 ds = ds_thread;
13567 cp_lexer_consume_token (parser->lexer);
13568 break;
13570 default:
13571 /* We did not yet find a decl-specifier yet. */
13572 found_decl_spec = false;
13573 break;
13576 if (found_decl_spec
13577 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13578 && token->keyword != RID_CONSTEXPR)
13579 error ("decl-specifier invalid in condition");
13581 if (found_decl_spec
13582 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13583 && token->keyword != RID_MUTABLE
13584 && token->keyword != RID_CONSTEXPR)
13585 error_at (token->location, "%qD invalid in lambda",
13586 ridpointers[token->keyword]);
13588 if (ds != ds_last)
13589 set_and_check_decl_spec_loc (decl_specs, ds, token);
13591 /* Constructors are a special case. The `S' in `S()' is not a
13592 decl-specifier; it is the beginning of the declarator. */
13593 constructor_p
13594 = (!found_decl_spec
13595 && constructor_possible_p
13596 && (cp_parser_constructor_declarator_p
13597 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13599 /* If we don't have a DECL_SPEC yet, then we must be looking at
13600 a type-specifier. */
13601 if (!found_decl_spec && !constructor_p)
13603 int decl_spec_declares_class_or_enum;
13604 bool is_cv_qualifier;
13605 tree type_spec;
13607 type_spec
13608 = cp_parser_type_specifier (parser, flags,
13609 decl_specs,
13610 /*is_declaration=*/true,
13611 &decl_spec_declares_class_or_enum,
13612 &is_cv_qualifier);
13613 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13615 /* If this type-specifier referenced a user-defined type
13616 (a typedef, class-name, etc.), then we can't allow any
13617 more such type-specifiers henceforth.
13619 [dcl.spec]
13621 The longest sequence of decl-specifiers that could
13622 possibly be a type name is taken as the
13623 decl-specifier-seq of a declaration. The sequence shall
13624 be self-consistent as described below.
13626 [dcl.type]
13628 As a general rule, at most one type-specifier is allowed
13629 in the complete decl-specifier-seq of a declaration. The
13630 only exceptions are the following:
13632 -- const or volatile can be combined with any other
13633 type-specifier.
13635 -- signed or unsigned can be combined with char, long,
13636 short, or int.
13638 -- ..
13640 Example:
13642 typedef char* Pc;
13643 void g (const int Pc);
13645 Here, Pc is *not* part of the decl-specifier seq; it's
13646 the declarator. Therefore, once we see a type-specifier
13647 (other than a cv-qualifier), we forbid any additional
13648 user-defined types. We *do* still allow things like `int
13649 int' to be considered a decl-specifier-seq, and issue the
13650 error message later. */
13651 if (type_spec && !is_cv_qualifier)
13652 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13653 /* A constructor declarator cannot follow a type-specifier. */
13654 if (type_spec)
13656 constructor_possible_p = false;
13657 found_decl_spec = true;
13658 if (!is_cv_qualifier)
13659 decl_specs->any_type_specifiers_p = true;
13663 /* If we still do not have a DECL_SPEC, then there are no more
13664 decl-specifiers. */
13665 if (!found_decl_spec)
13666 break;
13668 decl_specs->any_specifiers_p = true;
13669 /* After we see one decl-specifier, further decl-specifiers are
13670 always optional. */
13671 flags |= CP_PARSER_FLAGS_OPTIONAL;
13674 /* Don't allow a friend specifier with a class definition. */
13675 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13676 && (*declares_class_or_enum & 2))
13677 error_at (decl_specs->locations[ds_friend],
13678 "class definition may not be declared a friend");
13681 /* Parse an (optional) storage-class-specifier.
13683 storage-class-specifier:
13684 auto
13685 register
13686 static
13687 extern
13688 mutable
13690 GNU Extension:
13692 storage-class-specifier:
13693 thread
13695 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13697 static tree
13698 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13700 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13702 case RID_AUTO:
13703 if (cxx_dialect != cxx98)
13704 return NULL_TREE;
13705 /* Fall through for C++98. */
13706 gcc_fallthrough ();
13708 case RID_REGISTER:
13709 case RID_STATIC:
13710 case RID_EXTERN:
13711 case RID_MUTABLE:
13712 case RID_THREAD:
13713 /* Consume the token. */
13714 return cp_lexer_consume_token (parser->lexer)->u.value;
13716 default:
13717 return NULL_TREE;
13721 /* Parse an (optional) function-specifier.
13723 function-specifier:
13724 inline
13725 virtual
13726 explicit
13728 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13729 Updates DECL_SPECS, if it is non-NULL. */
13731 static tree
13732 cp_parser_function_specifier_opt (cp_parser* parser,
13733 cp_decl_specifier_seq *decl_specs)
13735 cp_token *token = cp_lexer_peek_token (parser->lexer);
13736 switch (token->keyword)
13738 case RID_INLINE:
13739 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13740 break;
13742 case RID_VIRTUAL:
13743 /* 14.5.2.3 [temp.mem]
13745 A member function template shall not be virtual. */
13746 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13747 && current_class_type)
13748 error_at (token->location, "templates may not be %<virtual%>");
13749 else
13750 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13751 break;
13753 case RID_EXPLICIT:
13754 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13755 break;
13757 default:
13758 return NULL_TREE;
13761 /* Consume the token. */
13762 return cp_lexer_consume_token (parser->lexer)->u.value;
13765 /* Parse a linkage-specification.
13767 linkage-specification:
13768 extern string-literal { declaration-seq [opt] }
13769 extern string-literal declaration */
13771 static void
13772 cp_parser_linkage_specification (cp_parser* parser)
13774 tree linkage;
13776 /* Look for the `extern' keyword. */
13777 cp_token *extern_token
13778 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13780 /* Look for the string-literal. */
13781 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13782 linkage = cp_parser_string_literal (parser, false, false);
13784 /* Transform the literal into an identifier. If the literal is a
13785 wide-character string, or contains embedded NULs, then we can't
13786 handle it as the user wants. */
13787 if (strlen (TREE_STRING_POINTER (linkage))
13788 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13790 cp_parser_error (parser, "invalid linkage-specification");
13791 /* Assume C++ linkage. */
13792 linkage = lang_name_cplusplus;
13794 else
13795 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13797 /* We're now using the new linkage. */
13798 push_lang_context (linkage);
13800 /* Preserve the location of the the innermost linkage specification,
13801 tracking the locations of nested specifications via a local. */
13802 location_t saved_location
13803 = parser->innermost_linkage_specification_location;
13804 /* Construct a location ranging from the start of the "extern" to
13805 the end of the string-literal, with the caret at the start, e.g.:
13806 extern "C" {
13807 ^~~~~~~~~~
13809 parser->innermost_linkage_specification_location
13810 = make_location (extern_token->location,
13811 extern_token->location,
13812 get_finish (string_token->location));
13814 /* If the next token is a `{', then we're using the first
13815 production. */
13816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13818 cp_ensure_no_omp_declare_simd (parser);
13819 cp_ensure_no_oacc_routine (parser);
13821 /* Consume the `{' token. */
13822 matching_braces braces;
13823 braces.consume_open (parser)->location;
13824 /* Parse the declarations. */
13825 cp_parser_declaration_seq_opt (parser);
13826 /* Look for the closing `}'. */
13827 braces.require_close (parser);
13829 /* Otherwise, there's just one declaration. */
13830 else
13832 bool saved_in_unbraced_linkage_specification_p;
13834 saved_in_unbraced_linkage_specification_p
13835 = parser->in_unbraced_linkage_specification_p;
13836 parser->in_unbraced_linkage_specification_p = true;
13837 cp_parser_declaration (parser);
13838 parser->in_unbraced_linkage_specification_p
13839 = saved_in_unbraced_linkage_specification_p;
13842 /* We're done with the linkage-specification. */
13843 pop_lang_context ();
13845 /* Restore location of parent linkage specification, if any. */
13846 parser->innermost_linkage_specification_location = saved_location;
13849 /* Parse a static_assert-declaration.
13851 static_assert-declaration:
13852 static_assert ( constant-expression , string-literal ) ;
13853 static_assert ( constant-expression ) ; (C++17)
13855 If MEMBER_P, this static_assert is a class member. */
13857 static void
13858 cp_parser_static_assert(cp_parser *parser, bool member_p)
13860 cp_expr condition;
13861 location_t token_loc;
13862 tree message;
13863 bool dummy;
13865 /* Peek at the `static_assert' token so we can keep track of exactly
13866 where the static assertion started. */
13867 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13869 /* Look for the `static_assert' keyword. */
13870 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13871 RT_STATIC_ASSERT))
13872 return;
13874 /* We know we are in a static assertion; commit to any tentative
13875 parse. */
13876 if (cp_parser_parsing_tentatively (parser))
13877 cp_parser_commit_to_tentative_parse (parser);
13879 /* Parse the `(' starting the static assertion condition. */
13880 matching_parens parens;
13881 parens.require_open (parser);
13883 /* Parse the constant-expression. Allow a non-constant expression
13884 here in order to give better diagnostics in finish_static_assert. */
13885 condition =
13886 cp_parser_constant_expression (parser,
13887 /*allow_non_constant_p=*/true,
13888 /*non_constant_p=*/&dummy);
13890 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13892 if (cxx_dialect < cxx17)
13893 pedwarn (input_location, OPT_Wpedantic,
13894 "static_assert without a message "
13895 "only available with -std=c++17 or -std=gnu++17");
13896 /* Eat the ')' */
13897 cp_lexer_consume_token (parser->lexer);
13898 message = build_string (1, "");
13899 TREE_TYPE (message) = char_array_type_node;
13900 fix_string_type (message);
13902 else
13904 /* Parse the separating `,'. */
13905 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13907 /* Parse the string-literal message. */
13908 message = cp_parser_string_literal (parser,
13909 /*translate=*/false,
13910 /*wide_ok=*/true);
13912 /* A `)' completes the static assertion. */
13913 if (!parens.require_close (parser))
13914 cp_parser_skip_to_closing_parenthesis (parser,
13915 /*recovering=*/true,
13916 /*or_comma=*/false,
13917 /*consume_paren=*/true);
13920 /* A semicolon terminates the declaration. */
13921 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13923 /* Get the location for the static assertion. Use that of the
13924 condition if available, otherwise, use that of the "static_assert"
13925 token. */
13926 location_t assert_loc = condition.get_location ();
13927 if (assert_loc == UNKNOWN_LOCATION)
13928 assert_loc = token_loc;
13930 /* Complete the static assertion, which may mean either processing
13931 the static assert now or saving it for template instantiation. */
13932 finish_static_assert (condition, message, assert_loc, member_p);
13935 /* Parse the expression in decltype ( expression ). */
13937 static tree
13938 cp_parser_decltype_expr (cp_parser *parser,
13939 bool &id_expression_or_member_access_p)
13941 cp_token *id_expr_start_token;
13942 tree expr;
13944 /* Since we're going to preserve any side-effects from this parse, set up a
13945 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13946 in the expression. */
13947 tentative_firewall firewall (parser);
13949 /* First, try parsing an id-expression. */
13950 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13951 cp_parser_parse_tentatively (parser);
13952 expr = cp_parser_id_expression (parser,
13953 /*template_keyword_p=*/false,
13954 /*check_dependency_p=*/true,
13955 /*template_p=*/NULL,
13956 /*declarator_p=*/false,
13957 /*optional_p=*/false);
13959 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13961 bool non_integral_constant_expression_p = false;
13962 tree id_expression = expr;
13963 cp_id_kind idk;
13964 const char *error_msg;
13966 if (identifier_p (expr))
13967 /* Lookup the name we got back from the id-expression. */
13968 expr = cp_parser_lookup_name_simple (parser, expr,
13969 id_expr_start_token->location);
13971 if (expr
13972 && expr != error_mark_node
13973 && TREE_CODE (expr) != TYPE_DECL
13974 && (TREE_CODE (expr) != BIT_NOT_EXPR
13975 || !TYPE_P (TREE_OPERAND (expr, 0)))
13976 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13978 /* Complete lookup of the id-expression. */
13979 expr = (finish_id_expression
13980 (id_expression, expr, parser->scope, &idk,
13981 /*integral_constant_expression_p=*/false,
13982 /*allow_non_integral_constant_expression_p=*/true,
13983 &non_integral_constant_expression_p,
13984 /*template_p=*/false,
13985 /*done=*/true,
13986 /*address_p=*/false,
13987 /*template_arg_p=*/false,
13988 &error_msg,
13989 id_expr_start_token->location));
13991 if (expr == error_mark_node)
13992 /* We found an id-expression, but it was something that we
13993 should not have found. This is an error, not something
13994 we can recover from, so note that we found an
13995 id-expression and we'll recover as gracefully as
13996 possible. */
13997 id_expression_or_member_access_p = true;
14000 if (expr
14001 && expr != error_mark_node
14002 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14003 /* We have an id-expression. */
14004 id_expression_or_member_access_p = true;
14007 if (!id_expression_or_member_access_p)
14009 /* Abort the id-expression parse. */
14010 cp_parser_abort_tentative_parse (parser);
14012 /* Parsing tentatively, again. */
14013 cp_parser_parse_tentatively (parser);
14015 /* Parse a class member access. */
14016 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14017 /*cast_p=*/false, /*decltype*/true,
14018 /*member_access_only_p=*/true, NULL);
14020 if (expr
14021 && expr != error_mark_node
14022 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14023 /* We have an id-expression. */
14024 id_expression_or_member_access_p = true;
14027 if (id_expression_or_member_access_p)
14028 /* We have parsed the complete id-expression or member access. */
14029 cp_parser_parse_definitely (parser);
14030 else
14032 /* Abort our attempt to parse an id-expression or member access
14033 expression. */
14034 cp_parser_abort_tentative_parse (parser);
14036 /* Parse a full expression. */
14037 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14038 /*decltype_p=*/true);
14041 return expr;
14044 /* Parse a `decltype' type. Returns the type.
14046 simple-type-specifier:
14047 decltype ( expression )
14048 C++14 proposal:
14049 decltype ( auto ) */
14051 static tree
14052 cp_parser_decltype (cp_parser *parser)
14054 bool id_expression_or_member_access_p = false;
14055 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14057 if (start_token->type == CPP_DECLTYPE)
14059 /* Already parsed. */
14060 cp_lexer_consume_token (parser->lexer);
14061 return saved_checks_value (start_token->u.tree_check_value);
14064 /* Look for the `decltype' token. */
14065 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14066 return error_mark_node;
14068 /* Parse the opening `('. */
14069 matching_parens parens;
14070 if (!parens.require_open (parser))
14071 return error_mark_node;
14073 push_deferring_access_checks (dk_deferred);
14075 tree expr = NULL_TREE;
14077 if (cxx_dialect >= cxx14
14078 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14079 /* decltype (auto) */
14080 cp_lexer_consume_token (parser->lexer);
14081 else
14083 /* decltype (expression) */
14085 /* Types cannot be defined in a `decltype' expression. Save away the
14086 old message and set the new one. */
14087 const char *saved_message = parser->type_definition_forbidden_message;
14088 parser->type_definition_forbidden_message
14089 = G_("types may not be defined in %<decltype%> expressions");
14091 /* The restrictions on constant-expressions do not apply inside
14092 decltype expressions. */
14093 bool saved_integral_constant_expression_p
14094 = parser->integral_constant_expression_p;
14095 bool saved_non_integral_constant_expression_p
14096 = parser->non_integral_constant_expression_p;
14097 parser->integral_constant_expression_p = false;
14099 /* Within a parenthesized expression, a `>' token is always
14100 the greater-than operator. */
14101 bool saved_greater_than_is_operator_p
14102 = parser->greater_than_is_operator_p;
14103 parser->greater_than_is_operator_p = true;
14105 /* Do not actually evaluate the expression. */
14106 ++cp_unevaluated_operand;
14108 /* Do not warn about problems with the expression. */
14109 ++c_inhibit_evaluation_warnings;
14111 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14113 /* Go back to evaluating expressions. */
14114 --cp_unevaluated_operand;
14115 --c_inhibit_evaluation_warnings;
14117 /* The `>' token might be the end of a template-id or
14118 template-parameter-list now. */
14119 parser->greater_than_is_operator_p
14120 = saved_greater_than_is_operator_p;
14122 /* Restore the old message and the integral constant expression
14123 flags. */
14124 parser->type_definition_forbidden_message = saved_message;
14125 parser->integral_constant_expression_p
14126 = saved_integral_constant_expression_p;
14127 parser->non_integral_constant_expression_p
14128 = saved_non_integral_constant_expression_p;
14131 /* Parse to the closing `)'. */
14132 if (!parens.require_close (parser))
14134 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14135 /*consume_paren=*/true);
14136 pop_deferring_access_checks ();
14137 return error_mark_node;
14140 if (!expr)
14142 /* Build auto. */
14143 expr = make_decltype_auto ();
14144 AUTO_IS_DECLTYPE (expr) = true;
14146 else
14147 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14148 tf_warning_or_error);
14150 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14151 it again. */
14152 start_token->type = CPP_DECLTYPE;
14153 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14154 start_token->u.tree_check_value->value = expr;
14155 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14156 start_token->keyword = RID_MAX;
14157 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14159 pop_to_parent_deferring_access_checks ();
14161 return expr;
14164 /* Special member functions [gram.special] */
14166 /* Parse a conversion-function-id.
14168 conversion-function-id:
14169 operator conversion-type-id
14171 Returns an IDENTIFIER_NODE representing the operator. */
14173 static tree
14174 cp_parser_conversion_function_id (cp_parser* parser)
14176 tree type;
14177 tree saved_scope;
14178 tree saved_qualifying_scope;
14179 tree saved_object_scope;
14180 tree pushed_scope = NULL_TREE;
14182 /* Look for the `operator' token. */
14183 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14184 return error_mark_node;
14185 /* When we parse the conversion-type-id, the current scope will be
14186 reset. However, we need that information in able to look up the
14187 conversion function later, so we save it here. */
14188 saved_scope = parser->scope;
14189 saved_qualifying_scope = parser->qualifying_scope;
14190 saved_object_scope = parser->object_scope;
14191 /* We must enter the scope of the class so that the names of
14192 entities declared within the class are available in the
14193 conversion-type-id. For example, consider:
14195 struct S {
14196 typedef int I;
14197 operator I();
14200 S::operator I() { ... }
14202 In order to see that `I' is a type-name in the definition, we
14203 must be in the scope of `S'. */
14204 if (saved_scope)
14205 pushed_scope = push_scope (saved_scope);
14206 /* Parse the conversion-type-id. */
14207 type = cp_parser_conversion_type_id (parser);
14208 /* Leave the scope of the class, if any. */
14209 if (pushed_scope)
14210 pop_scope (pushed_scope);
14211 /* Restore the saved scope. */
14212 parser->scope = saved_scope;
14213 parser->qualifying_scope = saved_qualifying_scope;
14214 parser->object_scope = saved_object_scope;
14215 /* If the TYPE is invalid, indicate failure. */
14216 if (type == error_mark_node)
14217 return error_mark_node;
14218 return make_conv_op_name (type);
14221 /* Parse a conversion-type-id:
14223 conversion-type-id:
14224 type-specifier-seq conversion-declarator [opt]
14226 Returns the TYPE specified. */
14228 static tree
14229 cp_parser_conversion_type_id (cp_parser* parser)
14231 tree attributes;
14232 cp_decl_specifier_seq type_specifiers;
14233 cp_declarator *declarator;
14234 tree type_specified;
14235 const char *saved_message;
14237 /* Parse the attributes. */
14238 attributes = cp_parser_attributes_opt (parser);
14240 saved_message = parser->type_definition_forbidden_message;
14241 parser->type_definition_forbidden_message
14242 = G_("types may not be defined in a conversion-type-id");
14244 /* Parse the type-specifiers. */
14245 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14246 /*is_trailing_return=*/false,
14247 &type_specifiers);
14249 parser->type_definition_forbidden_message = saved_message;
14251 /* If that didn't work, stop. */
14252 if (type_specifiers.type == error_mark_node)
14253 return error_mark_node;
14254 /* Parse the conversion-declarator. */
14255 declarator = cp_parser_conversion_declarator_opt (parser);
14257 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14258 /*initialized=*/0, &attributes);
14259 if (attributes)
14260 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14262 /* Don't give this error when parsing tentatively. This happens to
14263 work because we always parse this definitively once. */
14264 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14265 && type_uses_auto (type_specified))
14267 if (cxx_dialect < cxx14)
14269 error ("invalid use of %<auto%> in conversion operator");
14270 return error_mark_node;
14272 else if (template_parm_scope_p ())
14273 warning (0, "use of %<auto%> in member template "
14274 "conversion operator can never be deduced");
14277 return type_specified;
14280 /* Parse an (optional) conversion-declarator.
14282 conversion-declarator:
14283 ptr-operator conversion-declarator [opt]
14287 static cp_declarator *
14288 cp_parser_conversion_declarator_opt (cp_parser* parser)
14290 enum tree_code code;
14291 tree class_type, std_attributes = NULL_TREE;
14292 cp_cv_quals cv_quals;
14294 /* We don't know if there's a ptr-operator next, or not. */
14295 cp_parser_parse_tentatively (parser);
14296 /* Try the ptr-operator. */
14297 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14298 &std_attributes);
14299 /* If it worked, look for more conversion-declarators. */
14300 if (cp_parser_parse_definitely (parser))
14302 cp_declarator *declarator;
14304 /* Parse another optional declarator. */
14305 declarator = cp_parser_conversion_declarator_opt (parser);
14307 declarator = cp_parser_make_indirect_declarator
14308 (code, class_type, cv_quals, declarator, std_attributes);
14310 return declarator;
14313 return NULL;
14316 /* Parse an (optional) ctor-initializer.
14318 ctor-initializer:
14319 : mem-initializer-list */
14321 static void
14322 cp_parser_ctor_initializer_opt (cp_parser* parser)
14324 /* If the next token is not a `:', then there is no
14325 ctor-initializer. */
14326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14328 /* Do default initialization of any bases and members. */
14329 if (DECL_CONSTRUCTOR_P (current_function_decl))
14330 finish_mem_initializers (NULL_TREE);
14331 return;
14334 /* Consume the `:' token. */
14335 cp_lexer_consume_token (parser->lexer);
14336 /* And the mem-initializer-list. */
14337 cp_parser_mem_initializer_list (parser);
14340 /* Parse a mem-initializer-list.
14342 mem-initializer-list:
14343 mem-initializer ... [opt]
14344 mem-initializer ... [opt] , mem-initializer-list */
14346 static void
14347 cp_parser_mem_initializer_list (cp_parser* parser)
14349 tree mem_initializer_list = NULL_TREE;
14350 tree target_ctor = error_mark_node;
14351 cp_token *token = cp_lexer_peek_token (parser->lexer);
14353 /* Let the semantic analysis code know that we are starting the
14354 mem-initializer-list. */
14355 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14356 error_at (token->location,
14357 "only constructors take member initializers");
14359 /* Loop through the list. */
14360 while (true)
14362 tree mem_initializer;
14364 token = cp_lexer_peek_token (parser->lexer);
14365 /* Parse the mem-initializer. */
14366 mem_initializer = cp_parser_mem_initializer (parser);
14367 /* If the next token is a `...', we're expanding member initializers. */
14368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14370 /* Consume the `...'. */
14371 cp_lexer_consume_token (parser->lexer);
14373 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14374 can be expanded but members cannot. */
14375 if (mem_initializer != error_mark_node
14376 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14378 error_at (token->location,
14379 "cannot expand initializer for member %qD",
14380 TREE_PURPOSE (mem_initializer));
14381 mem_initializer = error_mark_node;
14384 /* Construct the pack expansion type. */
14385 if (mem_initializer != error_mark_node)
14386 mem_initializer = make_pack_expansion (mem_initializer);
14388 if (target_ctor != error_mark_node
14389 && mem_initializer != error_mark_node)
14391 error ("mem-initializer for %qD follows constructor delegation",
14392 TREE_PURPOSE (mem_initializer));
14393 mem_initializer = error_mark_node;
14395 /* Look for a target constructor. */
14396 if (mem_initializer != error_mark_node
14397 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14398 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14400 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14401 if (mem_initializer_list)
14403 error ("constructor delegation follows mem-initializer for %qD",
14404 TREE_PURPOSE (mem_initializer_list));
14405 mem_initializer = error_mark_node;
14407 target_ctor = mem_initializer;
14409 /* Add it to the list, unless it was erroneous. */
14410 if (mem_initializer != error_mark_node)
14412 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14413 mem_initializer_list = mem_initializer;
14415 /* If the next token is not a `,', we're done. */
14416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14417 break;
14418 /* Consume the `,' token. */
14419 cp_lexer_consume_token (parser->lexer);
14422 /* Perform semantic analysis. */
14423 if (DECL_CONSTRUCTOR_P (current_function_decl))
14424 finish_mem_initializers (mem_initializer_list);
14427 /* Parse a mem-initializer.
14429 mem-initializer:
14430 mem-initializer-id ( expression-list [opt] )
14431 mem-initializer-id braced-init-list
14433 GNU extension:
14435 mem-initializer:
14436 ( expression-list [opt] )
14438 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14439 class) or FIELD_DECL (for a non-static data member) to initialize;
14440 the TREE_VALUE is the expression-list. An empty initialization
14441 list is represented by void_list_node. */
14443 static tree
14444 cp_parser_mem_initializer (cp_parser* parser)
14446 tree mem_initializer_id;
14447 tree expression_list;
14448 tree member;
14449 cp_token *token = cp_lexer_peek_token (parser->lexer);
14451 /* Find out what is being initialized. */
14452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14454 permerror (token->location,
14455 "anachronistic old-style base class initializer");
14456 mem_initializer_id = NULL_TREE;
14458 else
14460 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14461 if (mem_initializer_id == error_mark_node)
14462 return mem_initializer_id;
14464 member = expand_member_init (mem_initializer_id);
14465 if (member && !DECL_P (member))
14466 in_base_initializer = 1;
14468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14470 bool expr_non_constant_p;
14471 cp_lexer_set_source_position (parser->lexer);
14472 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14473 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14474 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14475 expression_list = build_tree_list (NULL_TREE, expression_list);
14477 else
14479 vec<tree, va_gc> *vec;
14480 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14481 /*cast_p=*/false,
14482 /*allow_expansion_p=*/true,
14483 /*non_constant_p=*/NULL);
14484 if (vec == NULL)
14485 return error_mark_node;
14486 expression_list = build_tree_list_vec (vec);
14487 release_tree_vector (vec);
14490 if (expression_list == error_mark_node)
14491 return error_mark_node;
14492 if (!expression_list)
14493 expression_list = void_type_node;
14495 in_base_initializer = 0;
14497 return member ? build_tree_list (member, expression_list) : error_mark_node;
14500 /* Parse a mem-initializer-id.
14502 mem-initializer-id:
14503 :: [opt] nested-name-specifier [opt] class-name
14504 decltype-specifier (C++11)
14505 identifier
14507 Returns a TYPE indicating the class to be initialized for the first
14508 production (and the second in C++11). Returns an IDENTIFIER_NODE
14509 indicating the data member to be initialized for the last production. */
14511 static tree
14512 cp_parser_mem_initializer_id (cp_parser* parser)
14514 bool global_scope_p;
14515 bool nested_name_specifier_p;
14516 bool template_p = false;
14517 tree id;
14519 cp_token *token = cp_lexer_peek_token (parser->lexer);
14521 /* `typename' is not allowed in this context ([temp.res]). */
14522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14524 error_at (token->location,
14525 "keyword %<typename%> not allowed in this context (a qualified "
14526 "member initializer is implicitly a type)");
14527 cp_lexer_consume_token (parser->lexer);
14529 /* Look for the optional `::' operator. */
14530 global_scope_p
14531 = (cp_parser_global_scope_opt (parser,
14532 /*current_scope_valid_p=*/false)
14533 != NULL_TREE);
14534 /* Look for the optional nested-name-specifier. The simplest way to
14535 implement:
14537 [temp.res]
14539 The keyword `typename' is not permitted in a base-specifier or
14540 mem-initializer; in these contexts a qualified name that
14541 depends on a template-parameter is implicitly assumed to be a
14542 type name.
14544 is to assume that we have seen the `typename' keyword at this
14545 point. */
14546 nested_name_specifier_p
14547 = (cp_parser_nested_name_specifier_opt (parser,
14548 /*typename_keyword_p=*/true,
14549 /*check_dependency_p=*/true,
14550 /*type_p=*/true,
14551 /*is_declaration=*/true)
14552 != NULL_TREE);
14553 if (nested_name_specifier_p)
14554 template_p = cp_parser_optional_template_keyword (parser);
14555 /* If there is a `::' operator or a nested-name-specifier, then we
14556 are definitely looking for a class-name. */
14557 if (global_scope_p || nested_name_specifier_p)
14558 return cp_parser_class_name (parser,
14559 /*typename_keyword_p=*/true,
14560 /*template_keyword_p=*/template_p,
14561 typename_type,
14562 /*check_dependency_p=*/true,
14563 /*class_head_p=*/false,
14564 /*is_declaration=*/true);
14565 /* Otherwise, we could also be looking for an ordinary identifier. */
14566 cp_parser_parse_tentatively (parser);
14567 if (cp_lexer_next_token_is_decltype (parser->lexer))
14568 /* Try a decltype-specifier. */
14569 id = cp_parser_decltype (parser);
14570 else
14571 /* Otherwise, try a class-name. */
14572 id = cp_parser_class_name (parser,
14573 /*typename_keyword_p=*/true,
14574 /*template_keyword_p=*/false,
14575 none_type,
14576 /*check_dependency_p=*/true,
14577 /*class_head_p=*/false,
14578 /*is_declaration=*/true);
14579 /* If we found one, we're done. */
14580 if (cp_parser_parse_definitely (parser))
14581 return id;
14582 /* Otherwise, look for an ordinary identifier. */
14583 return cp_parser_identifier (parser);
14586 /* Overloading [gram.over] */
14588 /* Parse an operator-function-id.
14590 operator-function-id:
14591 operator operator
14593 Returns an IDENTIFIER_NODE for the operator which is a
14594 human-readable spelling of the identifier, e.g., `operator +'. */
14596 static cp_expr
14597 cp_parser_operator_function_id (cp_parser* parser)
14599 /* Look for the `operator' keyword. */
14600 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14601 return error_mark_node;
14602 /* And then the name of the operator itself. */
14603 return cp_parser_operator (parser);
14606 /* Return an identifier node for a user-defined literal operator.
14607 The suffix identifier is chained to the operator name identifier. */
14609 tree
14610 cp_literal_operator_id (const char* name)
14612 tree identifier;
14613 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14614 + strlen (name) + 10);
14615 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14616 identifier = get_identifier (buffer);
14618 return identifier;
14621 /* Parse an operator.
14623 operator:
14624 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14625 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14626 || ++ -- , ->* -> () []
14628 GNU Extensions:
14630 operator:
14631 <? >? <?= >?=
14633 Returns an IDENTIFIER_NODE for the operator which is a
14634 human-readable spelling of the identifier, e.g., `operator +'. */
14636 static cp_expr
14637 cp_parser_operator (cp_parser* parser)
14639 tree id = NULL_TREE;
14640 cp_token *token;
14641 bool utf8 = false;
14643 /* Peek at the next token. */
14644 token = cp_lexer_peek_token (parser->lexer);
14646 location_t start_loc = token->location;
14648 /* Figure out which operator we have. */
14649 enum tree_code op = ERROR_MARK;
14650 bool assop = false;
14651 bool consumed = false;
14652 switch (token->type)
14654 case CPP_KEYWORD:
14656 /* The keyword should be either `new' or `delete'. */
14657 if (token->keyword == RID_NEW)
14658 op = NEW_EXPR;
14659 else if (token->keyword == RID_DELETE)
14660 op = DELETE_EXPR;
14661 else
14662 break;
14664 /* Consume the `new' or `delete' token. */
14665 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14667 /* Peek at the next token. */
14668 token = cp_lexer_peek_token (parser->lexer);
14669 /* If it's a `[' token then this is the array variant of the
14670 operator. */
14671 if (token->type == CPP_OPEN_SQUARE)
14673 /* Consume the `[' token. */
14674 cp_lexer_consume_token (parser->lexer);
14675 /* Look for the `]' token. */
14676 if (cp_token *close_token
14677 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14678 end_loc = close_token->location;
14679 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14681 start_loc = make_location (start_loc, start_loc, end_loc);
14682 consumed = true;
14683 break;
14686 case CPP_PLUS:
14687 op = PLUS_EXPR;
14688 break;
14690 case CPP_MINUS:
14691 op = MINUS_EXPR;
14692 break;
14694 case CPP_MULT:
14695 op = MULT_EXPR;
14696 break;
14698 case CPP_DIV:
14699 op = TRUNC_DIV_EXPR;
14700 break;
14702 case CPP_MOD:
14703 op = TRUNC_MOD_EXPR;
14704 break;
14706 case CPP_XOR:
14707 op = BIT_XOR_EXPR;
14708 break;
14710 case CPP_AND:
14711 op = BIT_AND_EXPR;
14712 break;
14714 case CPP_OR:
14715 op = BIT_IOR_EXPR;
14716 break;
14718 case CPP_COMPL:
14719 op = BIT_NOT_EXPR;
14720 break;
14722 case CPP_NOT:
14723 op = TRUTH_NOT_EXPR;
14724 break;
14726 case CPP_EQ:
14727 assop = true;
14728 op = NOP_EXPR;
14729 break;
14731 case CPP_LESS:
14732 op = LT_EXPR;
14733 break;
14735 case CPP_GREATER:
14736 op = GT_EXPR;
14737 break;
14739 case CPP_PLUS_EQ:
14740 assop = true;
14741 op = PLUS_EXPR;
14742 break;
14744 case CPP_MINUS_EQ:
14745 assop = true;
14746 op = MINUS_EXPR;
14747 break;
14749 case CPP_MULT_EQ:
14750 assop = true;
14751 op = MULT_EXPR;
14752 break;
14754 case CPP_DIV_EQ:
14755 assop = true;
14756 op = TRUNC_DIV_EXPR;
14757 break;
14759 case CPP_MOD_EQ:
14760 assop = true;
14761 op = TRUNC_MOD_EXPR;
14762 break;
14764 case CPP_XOR_EQ:
14765 assop = true;
14766 op = BIT_XOR_EXPR;
14767 break;
14769 case CPP_AND_EQ:
14770 assop = true;
14771 op = BIT_AND_EXPR;
14772 break;
14774 case CPP_OR_EQ:
14775 assop = true;
14776 op = BIT_IOR_EXPR;
14777 break;
14779 case CPP_LSHIFT:
14780 op = LSHIFT_EXPR;
14781 break;
14783 case CPP_RSHIFT:
14784 op = RSHIFT_EXPR;
14785 break;
14787 case CPP_LSHIFT_EQ:
14788 assop = true;
14789 op = LSHIFT_EXPR;
14790 break;
14792 case CPP_RSHIFT_EQ:
14793 assop = true;
14794 op = RSHIFT_EXPR;
14795 break;
14797 case CPP_EQ_EQ:
14798 op = EQ_EXPR;
14799 break;
14801 case CPP_NOT_EQ:
14802 op = NE_EXPR;
14803 break;
14805 case CPP_LESS_EQ:
14806 op = LE_EXPR;
14807 break;
14809 case CPP_GREATER_EQ:
14810 op = GE_EXPR;
14811 break;
14813 case CPP_AND_AND:
14814 op = TRUTH_ANDIF_EXPR;
14815 break;
14817 case CPP_OR_OR:
14818 op = TRUTH_ORIF_EXPR;
14819 break;
14821 case CPP_PLUS_PLUS:
14822 op = POSTINCREMENT_EXPR;
14823 break;
14825 case CPP_MINUS_MINUS:
14826 op = PREDECREMENT_EXPR;
14827 break;
14829 case CPP_COMMA:
14830 op = COMPOUND_EXPR;
14831 break;
14833 case CPP_DEREF_STAR:
14834 op = MEMBER_REF;
14835 break;
14837 case CPP_DEREF:
14838 op = COMPONENT_REF;
14839 break;
14841 case CPP_OPEN_PAREN:
14843 /* Consume the `('. */
14844 matching_parens parens;
14845 parens.consume_open (parser);
14846 /* Look for the matching `)'. */
14847 parens.require_close (parser);
14848 op = CALL_EXPR;
14849 consumed = true;
14850 break;
14853 case CPP_OPEN_SQUARE:
14854 /* Consume the `['. */
14855 cp_lexer_consume_token (parser->lexer);
14856 /* Look for the matching `]'. */
14857 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14858 op = ARRAY_REF;
14859 consumed = true;
14860 break;
14862 case CPP_UTF8STRING:
14863 case CPP_UTF8STRING_USERDEF:
14864 utf8 = true;
14865 /* FALLTHRU */
14866 case CPP_STRING:
14867 case CPP_WSTRING:
14868 case CPP_STRING16:
14869 case CPP_STRING32:
14870 case CPP_STRING_USERDEF:
14871 case CPP_WSTRING_USERDEF:
14872 case CPP_STRING16_USERDEF:
14873 case CPP_STRING32_USERDEF:
14875 tree str, string_tree;
14876 int sz, len;
14878 if (cxx_dialect == cxx98)
14879 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14881 /* Consume the string. */
14882 str = cp_parser_string_literal (parser, /*translate=*/true,
14883 /*wide_ok=*/true, /*lookup_udlit=*/false);
14884 if (str == error_mark_node)
14885 return error_mark_node;
14886 else if (TREE_CODE (str) == USERDEF_LITERAL)
14888 string_tree = USERDEF_LITERAL_VALUE (str);
14889 id = USERDEF_LITERAL_SUFFIX_ID (str);
14891 else
14893 string_tree = str;
14894 /* Look for the suffix identifier. */
14895 token = cp_lexer_peek_token (parser->lexer);
14896 if (token->type == CPP_NAME)
14897 id = cp_parser_identifier (parser);
14898 else if (token->type == CPP_KEYWORD)
14900 error ("unexpected keyword;"
14901 " remove space between quotes and suffix identifier");
14902 return error_mark_node;
14904 else
14906 error ("expected suffix identifier");
14907 return error_mark_node;
14910 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14911 (TREE_TYPE (TREE_TYPE (string_tree))));
14912 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14913 if (len != 0)
14915 error ("expected empty string after %<operator%> keyword");
14916 return error_mark_node;
14918 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14919 != char_type_node)
14921 error ("invalid encoding prefix in literal operator");
14922 return error_mark_node;
14924 if (id != error_mark_node)
14926 const char *name = IDENTIFIER_POINTER (id);
14927 id = cp_literal_operator_id (name);
14929 return id;
14932 default:
14933 /* Anything else is an error. */
14934 break;
14937 /* If we have selected an identifier, we need to consume the
14938 operator token. */
14939 if (op != ERROR_MARK)
14941 id = ovl_op_identifier (assop, op);
14942 if (!consumed)
14943 cp_lexer_consume_token (parser->lexer);
14945 /* Otherwise, no valid operator name was present. */
14946 else
14948 cp_parser_error (parser, "expected operator");
14949 id = error_mark_node;
14952 return cp_expr (id, start_loc);
14955 /* Parse a template-declaration.
14957 template-declaration:
14958 export [opt] template < template-parameter-list > declaration
14960 If MEMBER_P is TRUE, this template-declaration occurs within a
14961 class-specifier.
14963 The grammar rule given by the standard isn't correct. What
14964 is really meant is:
14966 template-declaration:
14967 export [opt] template-parameter-list-seq
14968 decl-specifier-seq [opt] init-declarator [opt] ;
14969 export [opt] template-parameter-list-seq
14970 function-definition
14972 template-parameter-list-seq:
14973 template-parameter-list-seq [opt]
14974 template < template-parameter-list >
14976 Concept Extensions:
14978 template-parameter-list-seq:
14979 template < template-parameter-list > requires-clause [opt]
14981 requires-clause:
14982 requires logical-or-expression */
14984 static void
14985 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14987 /* Check for `export'. */
14988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14990 /* Consume the `export' token. */
14991 cp_lexer_consume_token (parser->lexer);
14992 /* Warn that we do not support `export'. */
14993 warning (0, "keyword %<export%> not implemented, and will be ignored");
14996 cp_parser_template_declaration_after_export (parser, member_p);
14999 /* Parse a template-parameter-list.
15001 template-parameter-list:
15002 template-parameter
15003 template-parameter-list , template-parameter
15005 Returns a TREE_LIST. Each node represents a template parameter.
15006 The nodes are connected via their TREE_CHAINs. */
15008 static tree
15009 cp_parser_template_parameter_list (cp_parser* parser)
15011 tree parameter_list = NULL_TREE;
15013 begin_template_parm_list ();
15015 /* The loop below parses the template parms. We first need to know
15016 the total number of template parms to be able to compute proper
15017 canonical types of each dependent type. So after the loop, when
15018 we know the total number of template parms,
15019 end_template_parm_list computes the proper canonical types and
15020 fixes up the dependent types accordingly. */
15021 while (true)
15023 tree parameter;
15024 bool is_non_type;
15025 bool is_parameter_pack;
15026 location_t parm_loc;
15028 /* Parse the template-parameter. */
15029 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15030 parameter = cp_parser_template_parameter (parser,
15031 &is_non_type,
15032 &is_parameter_pack);
15033 /* Add it to the list. */
15034 if (parameter != error_mark_node)
15035 parameter_list = process_template_parm (parameter_list,
15036 parm_loc,
15037 parameter,
15038 is_non_type,
15039 is_parameter_pack);
15040 else
15042 tree err_parm = build_tree_list (parameter, parameter);
15043 parameter_list = chainon (parameter_list, err_parm);
15046 /* If the next token is not a `,', we're done. */
15047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15048 break;
15049 /* Otherwise, consume the `,' token. */
15050 cp_lexer_consume_token (parser->lexer);
15053 return end_template_parm_list (parameter_list);
15056 /* Parse a introduction-list.
15058 introduction-list:
15059 introduced-parameter
15060 introduction-list , introduced-parameter
15062 introduced-parameter:
15063 ...[opt] identifier
15065 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15066 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15067 WILDCARD_DECL will also have DECL_NAME set and token location in
15068 DECL_SOURCE_LOCATION. */
15070 static tree
15071 cp_parser_introduction_list (cp_parser *parser)
15073 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15075 while (true)
15077 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15078 if (is_pack)
15079 cp_lexer_consume_token (parser->lexer);
15081 /* Build placeholder. */
15082 tree parm = build_nt (WILDCARD_DECL);
15083 DECL_SOURCE_LOCATION (parm)
15084 = cp_lexer_peek_token (parser->lexer)->location;
15085 DECL_NAME (parm) = cp_parser_identifier (parser);
15086 WILDCARD_PACK_P (parm) = is_pack;
15087 vec_safe_push (introduction_vec, parm);
15089 /* If the next token is not a `,', we're done. */
15090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15091 break;
15092 /* Otherwise, consume the `,' token. */
15093 cp_lexer_consume_token (parser->lexer);
15096 /* Convert the vec into a TREE_VEC. */
15097 tree introduction_list = make_tree_vec (introduction_vec->length ());
15098 unsigned int n;
15099 tree parm;
15100 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15101 TREE_VEC_ELT (introduction_list, n) = parm;
15103 release_tree_vector (introduction_vec);
15104 return introduction_list;
15107 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15108 is an abstract declarator. */
15110 static inline cp_declarator*
15111 get_id_declarator (cp_declarator *declarator)
15113 cp_declarator *d = declarator;
15114 while (d && d->kind != cdk_id)
15115 d = d->declarator;
15116 return d;
15119 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15120 is an abstract declarator. */
15122 static inline tree
15123 get_unqualified_id (cp_declarator *declarator)
15125 declarator = get_id_declarator (declarator);
15126 if (declarator)
15127 return declarator->u.id.unqualified_name;
15128 else
15129 return NULL_TREE;
15132 /* Returns true if DECL represents a constrained-parameter. */
15134 static inline bool
15135 is_constrained_parameter (tree decl)
15137 return (decl
15138 && TREE_CODE (decl) == TYPE_DECL
15139 && CONSTRAINED_PARM_CONCEPT (decl)
15140 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15143 /* Returns true if PARM declares a constrained-parameter. */
15145 static inline bool
15146 is_constrained_parameter (cp_parameter_declarator *parm)
15148 return is_constrained_parameter (parm->decl_specifiers.type);
15151 /* Check that the type parameter is only a declarator-id, and that its
15152 type is not cv-qualified. */
15154 bool
15155 cp_parser_check_constrained_type_parm (cp_parser *parser,
15156 cp_parameter_declarator *parm)
15158 if (!parm->declarator)
15159 return true;
15161 if (parm->declarator->kind != cdk_id)
15163 cp_parser_error (parser, "invalid constrained type parameter");
15164 return false;
15167 /* Don't allow cv-qualified type parameters. */
15168 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15169 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15171 cp_parser_error (parser, "cv-qualified type parameter");
15172 return false;
15175 return true;
15178 /* Finish parsing/processing a template type parameter and checking
15179 various restrictions. */
15181 static inline tree
15182 cp_parser_constrained_type_template_parm (cp_parser *parser,
15183 tree id,
15184 cp_parameter_declarator* parmdecl)
15186 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15187 return finish_template_type_parm (class_type_node, id);
15188 else
15189 return error_mark_node;
15192 static tree
15193 finish_constrained_template_template_parm (tree proto, tree id)
15195 /* FIXME: This should probably be copied, and we may need to adjust
15196 the template parameter depths. */
15197 tree saved_parms = current_template_parms;
15198 begin_template_parm_list ();
15199 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15200 end_template_parm_list ();
15202 tree parm = finish_template_template_parm (class_type_node, id);
15203 current_template_parms = saved_parms;
15205 return parm;
15208 /* Finish parsing/processing a template template parameter by borrowing
15209 the template parameter list from the prototype parameter. */
15211 static tree
15212 cp_parser_constrained_template_template_parm (cp_parser *parser,
15213 tree proto,
15214 tree id,
15215 cp_parameter_declarator *parmdecl)
15217 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15218 return error_mark_node;
15219 return finish_constrained_template_template_parm (proto, id);
15222 /* Create a new non-type template parameter from the given PARM
15223 declarator. */
15225 static tree
15226 constrained_non_type_template_parm (bool *is_non_type,
15227 cp_parameter_declarator *parm)
15229 *is_non_type = true;
15230 cp_declarator *decl = parm->declarator;
15231 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15232 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15233 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15236 /* Build a constrained template parameter based on the PARMDECL
15237 declarator. The type of PARMDECL is the constrained type, which
15238 refers to the prototype template parameter that ultimately
15239 specifies the type of the declared parameter. */
15241 static tree
15242 finish_constrained_parameter (cp_parser *parser,
15243 cp_parameter_declarator *parmdecl,
15244 bool *is_non_type,
15245 bool *is_parameter_pack)
15247 tree decl = parmdecl->decl_specifiers.type;
15248 tree id = get_unqualified_id (parmdecl->declarator);
15249 tree def = parmdecl->default_argument;
15250 tree proto = DECL_INITIAL (decl);
15252 /* A template parameter constrained by a variadic concept shall also
15253 be declared as a template parameter pack. */
15254 bool is_variadic = template_parameter_pack_p (proto);
15255 if (is_variadic && !*is_parameter_pack)
15256 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15258 /* Build the parameter. Return an error if the declarator was invalid. */
15259 tree parm;
15260 if (TREE_CODE (proto) == TYPE_DECL)
15261 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15262 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15263 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15264 parmdecl);
15265 else
15266 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15267 if (parm == error_mark_node)
15268 return error_mark_node;
15270 /* Finish the parameter decl and create a node attaching the
15271 default argument and constraint. */
15272 parm = build_tree_list (def, parm);
15273 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15275 return parm;
15278 /* Returns true if the parsed type actually represents the declaration
15279 of a type template-parameter. */
15281 static inline bool
15282 declares_constrained_type_template_parameter (tree type)
15284 return (is_constrained_parameter (type)
15285 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15289 /* Returns true if the parsed type actually represents the declaration of
15290 a template template-parameter. */
15292 static bool
15293 declares_constrained_template_template_parameter (tree type)
15295 return (is_constrained_parameter (type)
15296 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15299 /* Parse a default argument for a type template-parameter.
15300 Note that diagnostics are handled in cp_parser_template_parameter. */
15302 static tree
15303 cp_parser_default_type_template_argument (cp_parser *parser)
15305 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15307 /* Consume the `=' token. */
15308 cp_lexer_consume_token (parser->lexer);
15310 cp_token *token = cp_lexer_peek_token (parser->lexer);
15312 /* Parse the default-argument. */
15313 push_deferring_access_checks (dk_no_deferred);
15314 tree default_argument = cp_parser_type_id (parser);
15315 pop_deferring_access_checks ();
15317 if (flag_concepts && type_uses_auto (default_argument))
15319 error_at (token->location,
15320 "invalid use of %<auto%> in default template argument");
15321 return error_mark_node;
15324 return default_argument;
15327 /* Parse a default argument for a template template-parameter. */
15329 static tree
15330 cp_parser_default_template_template_argument (cp_parser *parser)
15332 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15334 bool is_template;
15336 /* Consume the `='. */
15337 cp_lexer_consume_token (parser->lexer);
15338 /* Parse the id-expression. */
15339 push_deferring_access_checks (dk_no_deferred);
15340 /* save token before parsing the id-expression, for error
15341 reporting */
15342 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15343 tree default_argument
15344 = cp_parser_id_expression (parser,
15345 /*template_keyword_p=*/false,
15346 /*check_dependency_p=*/true,
15347 /*template_p=*/&is_template,
15348 /*declarator_p=*/false,
15349 /*optional_p=*/false);
15350 if (TREE_CODE (default_argument) == TYPE_DECL)
15351 /* If the id-expression was a template-id that refers to
15352 a template-class, we already have the declaration here,
15353 so no further lookup is needed. */
15355 else
15356 /* Look up the name. */
15357 default_argument
15358 = cp_parser_lookup_name (parser, default_argument,
15359 none_type,
15360 /*is_template=*/is_template,
15361 /*is_namespace=*/false,
15362 /*check_dependency=*/true,
15363 /*ambiguous_decls=*/NULL,
15364 token->location);
15365 /* See if the default argument is valid. */
15366 default_argument = check_template_template_default_arg (default_argument);
15367 pop_deferring_access_checks ();
15368 return default_argument;
15371 /* Parse a template-parameter.
15373 template-parameter:
15374 type-parameter
15375 parameter-declaration
15377 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15378 the parameter. The TREE_PURPOSE is the default value, if any.
15379 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15380 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15381 set to true iff this parameter is a parameter pack. */
15383 static tree
15384 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15385 bool *is_parameter_pack)
15387 cp_token *token;
15388 cp_parameter_declarator *parameter_declarator;
15389 tree parm;
15391 /* Assume it is a type parameter or a template parameter. */
15392 *is_non_type = false;
15393 /* Assume it not a parameter pack. */
15394 *is_parameter_pack = false;
15395 /* Peek at the next token. */
15396 token = cp_lexer_peek_token (parser->lexer);
15397 /* If it is `template', we have a type-parameter. */
15398 if (token->keyword == RID_TEMPLATE)
15399 return cp_parser_type_parameter (parser, is_parameter_pack);
15400 /* If it is `class' or `typename' we do not know yet whether it is a
15401 type parameter or a non-type parameter. Consider:
15403 template <typename T, typename T::X X> ...
15407 template <class C, class D*> ...
15409 Here, the first parameter is a type parameter, and the second is
15410 a non-type parameter. We can tell by looking at the token after
15411 the identifier -- if it is a `,', `=', or `>' then we have a type
15412 parameter. */
15413 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15415 /* Peek at the token after `class' or `typename'. */
15416 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15417 /* If it's an ellipsis, we have a template type parameter
15418 pack. */
15419 if (token->type == CPP_ELLIPSIS)
15420 return cp_parser_type_parameter (parser, is_parameter_pack);
15421 /* If it's an identifier, skip it. */
15422 if (token->type == CPP_NAME)
15423 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15424 /* Now, see if the token looks like the end of a template
15425 parameter. */
15426 if (token->type == CPP_COMMA
15427 || token->type == CPP_EQ
15428 || token->type == CPP_GREATER)
15429 return cp_parser_type_parameter (parser, is_parameter_pack);
15432 /* Otherwise, it is a non-type parameter or a constrained parameter.
15434 [temp.param]
15436 When parsing a default template-argument for a non-type
15437 template-parameter, the first non-nested `>' is taken as the end
15438 of the template parameter-list rather than a greater-than
15439 operator. */
15440 parameter_declarator
15441 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15442 /*parenthesized_p=*/NULL);
15444 if (!parameter_declarator)
15445 return error_mark_node;
15447 /* If the parameter declaration is marked as a parameter pack, set
15448 *IS_PARAMETER_PACK to notify the caller. */
15449 if (parameter_declarator->template_parameter_pack_p)
15450 *is_parameter_pack = true;
15452 if (parameter_declarator->default_argument)
15454 /* Can happen in some cases of erroneous input (c++/34892). */
15455 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15456 /* Consume the `...' for better error recovery. */
15457 cp_lexer_consume_token (parser->lexer);
15460 // The parameter may have been constrained.
15461 if (is_constrained_parameter (parameter_declarator))
15462 return finish_constrained_parameter (parser,
15463 parameter_declarator,
15464 is_non_type,
15465 is_parameter_pack);
15467 // Now we're sure that the parameter is a non-type parameter.
15468 *is_non_type = true;
15470 parm = grokdeclarator (parameter_declarator->declarator,
15471 &parameter_declarator->decl_specifiers,
15472 TPARM, /*initialized=*/0,
15473 /*attrlist=*/NULL);
15474 if (parm == error_mark_node)
15475 return error_mark_node;
15477 return build_tree_list (parameter_declarator->default_argument, parm);
15480 /* Parse a type-parameter.
15482 type-parameter:
15483 class identifier [opt]
15484 class identifier [opt] = type-id
15485 typename identifier [opt]
15486 typename identifier [opt] = type-id
15487 template < template-parameter-list > class identifier [opt]
15488 template < template-parameter-list > class identifier [opt]
15489 = id-expression
15491 GNU Extension (variadic templates):
15493 type-parameter:
15494 class ... identifier [opt]
15495 typename ... identifier [opt]
15497 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15498 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15499 the declaration of the parameter.
15501 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15503 static tree
15504 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15506 cp_token *token;
15507 tree parameter;
15509 /* Look for a keyword to tell us what kind of parameter this is. */
15510 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15511 if (!token)
15512 return error_mark_node;
15514 switch (token->keyword)
15516 case RID_CLASS:
15517 case RID_TYPENAME:
15519 tree identifier;
15520 tree default_argument;
15522 /* If the next token is an ellipsis, we have a template
15523 argument pack. */
15524 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15526 /* Consume the `...' token. */
15527 cp_lexer_consume_token (parser->lexer);
15528 maybe_warn_variadic_templates ();
15530 *is_parameter_pack = true;
15533 /* If the next token is an identifier, then it names the
15534 parameter. */
15535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15536 identifier = cp_parser_identifier (parser);
15537 else
15538 identifier = NULL_TREE;
15540 /* Create the parameter. */
15541 parameter = finish_template_type_parm (class_type_node, identifier);
15543 /* If the next token is an `=', we have a default argument. */
15544 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15546 default_argument
15547 = cp_parser_default_type_template_argument (parser);
15549 /* Template parameter packs cannot have default
15550 arguments. */
15551 if (*is_parameter_pack)
15553 if (identifier)
15554 error_at (token->location,
15555 "template parameter pack %qD cannot have a "
15556 "default argument", identifier);
15557 else
15558 error_at (token->location,
15559 "template parameter packs cannot have "
15560 "default arguments");
15561 default_argument = NULL_TREE;
15563 else if (check_for_bare_parameter_packs (default_argument))
15564 default_argument = error_mark_node;
15566 else
15567 default_argument = NULL_TREE;
15569 /* Create the combined representation of the parameter and the
15570 default argument. */
15571 parameter = build_tree_list (default_argument, parameter);
15573 break;
15575 case RID_TEMPLATE:
15577 tree identifier;
15578 tree default_argument;
15580 /* Look for the `<'. */
15581 cp_parser_require (parser, CPP_LESS, RT_LESS);
15582 /* Parse the template-parameter-list. */
15583 cp_parser_template_parameter_list (parser);
15584 /* Look for the `>'. */
15585 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15587 // If template requirements are present, parse them.
15588 if (flag_concepts)
15590 tree reqs = get_shorthand_constraints (current_template_parms);
15591 if (tree r = cp_parser_requires_clause_opt (parser))
15592 reqs = conjoin_constraints (reqs, normalize_expression (r));
15593 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15596 /* Look for the `class' or 'typename' keywords. */
15597 cp_parser_type_parameter_key (parser);
15598 /* If the next token is an ellipsis, we have a template
15599 argument pack. */
15600 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15602 /* Consume the `...' token. */
15603 cp_lexer_consume_token (parser->lexer);
15604 maybe_warn_variadic_templates ();
15606 *is_parameter_pack = true;
15608 /* If the next token is an `=', then there is a
15609 default-argument. If the next token is a `>', we are at
15610 the end of the parameter-list. If the next token is a `,',
15611 then we are at the end of this parameter. */
15612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15613 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15614 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15616 identifier = cp_parser_identifier (parser);
15617 /* Treat invalid names as if the parameter were nameless. */
15618 if (identifier == error_mark_node)
15619 identifier = NULL_TREE;
15621 else
15622 identifier = NULL_TREE;
15624 /* Create the template parameter. */
15625 parameter = finish_template_template_parm (class_type_node,
15626 identifier);
15628 /* If the next token is an `=', then there is a
15629 default-argument. */
15630 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15632 default_argument
15633 = cp_parser_default_template_template_argument (parser);
15635 /* Template parameter packs cannot have default
15636 arguments. */
15637 if (*is_parameter_pack)
15639 if (identifier)
15640 error_at (token->location,
15641 "template parameter pack %qD cannot "
15642 "have a default argument",
15643 identifier);
15644 else
15645 error_at (token->location, "template parameter packs cannot "
15646 "have default arguments");
15647 default_argument = NULL_TREE;
15650 else
15651 default_argument = NULL_TREE;
15653 /* Create the combined representation of the parameter and the
15654 default argument. */
15655 parameter = build_tree_list (default_argument, parameter);
15657 break;
15659 default:
15660 gcc_unreachable ();
15661 break;
15664 return parameter;
15667 /* Parse a template-id.
15669 template-id:
15670 template-name < template-argument-list [opt] >
15672 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15673 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15674 returned. Otherwise, if the template-name names a function, or set
15675 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15676 names a class, returns a TYPE_DECL for the specialization.
15678 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15679 uninstantiated templates. */
15681 static tree
15682 cp_parser_template_id (cp_parser *parser,
15683 bool template_keyword_p,
15684 bool check_dependency_p,
15685 enum tag_types tag_type,
15686 bool is_declaration)
15688 tree templ;
15689 tree arguments;
15690 tree template_id;
15691 cp_token_position start_of_id = 0;
15692 cp_token *next_token = NULL, *next_token_2 = NULL;
15693 bool is_identifier;
15695 /* If the next token corresponds to a template-id, there is no need
15696 to reparse it. */
15697 cp_token *token = cp_lexer_peek_token (parser->lexer);
15698 if (token->type == CPP_TEMPLATE_ID)
15700 cp_lexer_consume_token (parser->lexer);
15701 return saved_checks_value (token->u.tree_check_value);
15704 /* Avoid performing name lookup if there is no possibility of
15705 finding a template-id. */
15706 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15707 || (token->type == CPP_NAME
15708 && !cp_parser_nth_token_starts_template_argument_list_p
15709 (parser, 2)))
15711 cp_parser_error (parser, "expected template-id");
15712 return error_mark_node;
15715 /* Remember where the template-id starts. */
15716 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15717 start_of_id = cp_lexer_token_position (parser->lexer, false);
15719 push_deferring_access_checks (dk_deferred);
15721 /* Parse the template-name. */
15722 is_identifier = false;
15723 templ = cp_parser_template_name (parser, template_keyword_p,
15724 check_dependency_p,
15725 is_declaration,
15726 tag_type,
15727 &is_identifier);
15728 if (templ == error_mark_node || is_identifier)
15730 pop_deferring_access_checks ();
15731 return templ;
15734 /* Since we're going to preserve any side-effects from this parse, set up a
15735 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15736 in the template arguments. */
15737 tentative_firewall firewall (parser);
15739 /* If we find the sequence `[:' after a template-name, it's probably
15740 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15741 parse correctly the argument list. */
15742 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15743 == CPP_OPEN_SQUARE)
15744 && next_token->flags & DIGRAPH
15745 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15746 == CPP_COLON)
15747 && !(next_token_2->flags & PREV_WHITE))
15749 cp_parser_parse_tentatively (parser);
15750 /* Change `:' into `::'. */
15751 next_token_2->type = CPP_SCOPE;
15752 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15753 CPP_LESS. */
15754 cp_lexer_consume_token (parser->lexer);
15756 /* Parse the arguments. */
15757 arguments = cp_parser_enclosed_template_argument_list (parser);
15758 if (!cp_parser_parse_definitely (parser))
15760 /* If we couldn't parse an argument list, then we revert our changes
15761 and return simply an error. Maybe this is not a template-id
15762 after all. */
15763 next_token_2->type = CPP_COLON;
15764 cp_parser_error (parser, "expected %<<%>");
15765 pop_deferring_access_checks ();
15766 return error_mark_node;
15768 /* Otherwise, emit an error about the invalid digraph, but continue
15769 parsing because we got our argument list. */
15770 if (permerror (next_token->location,
15771 "%<<::%> cannot begin a template-argument list"))
15773 static bool hint = false;
15774 inform (next_token->location,
15775 "%<<:%> is an alternate spelling for %<[%>."
15776 " Insert whitespace between %<<%> and %<::%>");
15777 if (!hint && !flag_permissive)
15779 inform (next_token->location, "(if you use %<-fpermissive%> "
15780 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15781 "accept your code)");
15782 hint = true;
15786 else
15788 /* Look for the `<' that starts the template-argument-list. */
15789 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15791 pop_deferring_access_checks ();
15792 return error_mark_node;
15794 /* Parse the arguments. */
15795 arguments = cp_parser_enclosed_template_argument_list (parser);
15798 /* Set the location to be of the form:
15799 template-name < template-argument-list [opt] >
15800 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15801 with caret == start at the start of the template-name,
15802 ranging until the closing '>'. */
15803 location_t finish_loc
15804 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15805 location_t combined_loc
15806 = make_location (token->location, token->location, finish_loc);
15808 /* Build a representation of the specialization. */
15809 if (identifier_p (templ))
15810 template_id = build_min_nt_loc (combined_loc,
15811 TEMPLATE_ID_EXPR,
15812 templ, arguments);
15813 else if (DECL_TYPE_TEMPLATE_P (templ)
15814 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15816 bool entering_scope;
15817 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15818 template (rather than some instantiation thereof) only if
15819 is not nested within some other construct. For example, in
15820 "template <typename T> void f(T) { A<T>::", A<T> is just an
15821 instantiation of A. */
15822 entering_scope = (template_parm_scope_p ()
15823 && cp_lexer_next_token_is (parser->lexer,
15824 CPP_SCOPE));
15825 template_id
15826 = finish_template_type (templ, arguments, entering_scope);
15828 /* A template-like identifier may be a partial concept id. */
15829 else if (flag_concepts
15830 && (template_id = (cp_parser_maybe_partial_concept_id
15831 (parser, templ, arguments))))
15832 return template_id;
15833 else if (variable_template_p (templ))
15835 template_id = lookup_template_variable (templ, arguments);
15836 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15837 SET_EXPR_LOCATION (template_id, combined_loc);
15839 else
15841 /* If it's not a class-template or a template-template, it should be
15842 a function-template. */
15843 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15844 || TREE_CODE (templ) == OVERLOAD
15845 || BASELINK_P (templ)));
15847 template_id = lookup_template_function (templ, arguments);
15848 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15849 SET_EXPR_LOCATION (template_id, combined_loc);
15852 /* If parsing tentatively, replace the sequence of tokens that makes
15853 up the template-id with a CPP_TEMPLATE_ID token. That way,
15854 should we re-parse the token stream, we will not have to repeat
15855 the effort required to do the parse, nor will we issue duplicate
15856 error messages about problems during instantiation of the
15857 template. */
15858 if (start_of_id
15859 /* Don't do this if we had a parse error in a declarator; re-parsing
15860 might succeed if a name changes meaning (60361). */
15861 && !(cp_parser_error_occurred (parser)
15862 && cp_parser_parsing_tentatively (parser)
15863 && parser->in_declarator_p))
15865 /* Reset the contents of the START_OF_ID token. */
15866 token->type = CPP_TEMPLATE_ID;
15867 token->location = combined_loc;
15869 /* We must mark the lookup as kept, so we don't throw it away on
15870 the first parse. */
15871 if (is_overloaded_fn (template_id))
15872 lookup_keep (get_fns (template_id), true);
15874 /* Retrieve any deferred checks. Do not pop this access checks yet
15875 so the memory will not be reclaimed during token replacing below. */
15876 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15877 token->u.tree_check_value->value = template_id;
15878 token->u.tree_check_value->checks = get_deferred_access_checks ();
15879 token->keyword = RID_MAX;
15881 /* Purge all subsequent tokens. */
15882 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15884 /* ??? Can we actually assume that, if template_id ==
15885 error_mark_node, we will have issued a diagnostic to the
15886 user, as opposed to simply marking the tentative parse as
15887 failed? */
15888 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15889 error_at (token->location, "parse error in template argument list");
15892 pop_to_parent_deferring_access_checks ();
15893 return template_id;
15896 /* Parse a template-name.
15898 template-name:
15899 identifier
15901 The standard should actually say:
15903 template-name:
15904 identifier
15905 operator-function-id
15907 A defect report has been filed about this issue.
15909 A conversion-function-id cannot be a template name because they cannot
15910 be part of a template-id. In fact, looking at this code:
15912 a.operator K<int>()
15914 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15915 It is impossible to call a templated conversion-function-id with an
15916 explicit argument list, since the only allowed template parameter is
15917 the type to which it is converting.
15919 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15920 `template' keyword, in a construction like:
15922 T::template f<3>()
15924 In that case `f' is taken to be a template-name, even though there
15925 is no way of knowing for sure.
15927 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15928 name refers to a set of overloaded functions, at least one of which
15929 is a template, or an IDENTIFIER_NODE with the name of the template,
15930 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15931 names are looked up inside uninstantiated templates. */
15933 static tree
15934 cp_parser_template_name (cp_parser* parser,
15935 bool template_keyword_p,
15936 bool check_dependency_p,
15937 bool is_declaration,
15938 enum tag_types tag_type,
15939 bool *is_identifier)
15941 tree identifier;
15942 tree decl;
15943 cp_token *token = cp_lexer_peek_token (parser->lexer);
15945 /* If the next token is `operator', then we have either an
15946 operator-function-id or a conversion-function-id. */
15947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15949 /* We don't know whether we're looking at an
15950 operator-function-id or a conversion-function-id. */
15951 cp_parser_parse_tentatively (parser);
15952 /* Try an operator-function-id. */
15953 identifier = cp_parser_operator_function_id (parser);
15954 /* If that didn't work, try a conversion-function-id. */
15955 if (!cp_parser_parse_definitely (parser))
15957 cp_parser_error (parser, "expected template-name");
15958 return error_mark_node;
15961 /* Look for the identifier. */
15962 else
15963 identifier = cp_parser_identifier (parser);
15965 /* If we didn't find an identifier, we don't have a template-id. */
15966 if (identifier == error_mark_node)
15967 return error_mark_node;
15969 /* If the name immediately followed the `template' keyword, then it
15970 is a template-name. However, if the next token is not `<', then
15971 we do not treat it as a template-name, since it is not being used
15972 as part of a template-id. This enables us to handle constructs
15973 like:
15975 template <typename T> struct S { S(); };
15976 template <typename T> S<T>::S();
15978 correctly. We would treat `S' as a template -- if it were `S<T>'
15979 -- but we do not if there is no `<'. */
15981 if (processing_template_decl
15982 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15984 /* In a declaration, in a dependent context, we pretend that the
15985 "template" keyword was present in order to improve error
15986 recovery. For example, given:
15988 template <typename T> void f(T::X<int>);
15990 we want to treat "X<int>" as a template-id. */
15991 if (is_declaration
15992 && !template_keyword_p
15993 && parser->scope && TYPE_P (parser->scope)
15994 && check_dependency_p
15995 && dependent_scope_p (parser->scope)
15996 /* Do not do this for dtors (or ctors), since they never
15997 need the template keyword before their name. */
15998 && !constructor_name_p (identifier, parser->scope))
16000 cp_token_position start = 0;
16002 /* Explain what went wrong. */
16003 error_at (token->location, "non-template %qD used as template",
16004 identifier);
16005 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16006 parser->scope, identifier);
16007 /* If parsing tentatively, find the location of the "<" token. */
16008 if (cp_parser_simulate_error (parser))
16009 start = cp_lexer_token_position (parser->lexer, true);
16010 /* Parse the template arguments so that we can issue error
16011 messages about them. */
16012 cp_lexer_consume_token (parser->lexer);
16013 cp_parser_enclosed_template_argument_list (parser);
16014 /* Skip tokens until we find a good place from which to
16015 continue parsing. */
16016 cp_parser_skip_to_closing_parenthesis (parser,
16017 /*recovering=*/true,
16018 /*or_comma=*/true,
16019 /*consume_paren=*/false);
16020 /* If parsing tentatively, permanently remove the
16021 template argument list. That will prevent duplicate
16022 error messages from being issued about the missing
16023 "template" keyword. */
16024 if (start)
16025 cp_lexer_purge_tokens_after (parser->lexer, start);
16026 if (is_identifier)
16027 *is_identifier = true;
16028 parser->context->object_type = NULL_TREE;
16029 return identifier;
16032 /* If the "template" keyword is present, then there is generally
16033 no point in doing name-lookup, so we just return IDENTIFIER.
16034 But, if the qualifying scope is non-dependent then we can
16035 (and must) do name-lookup normally. */
16036 if (template_keyword_p)
16038 tree scope = (parser->scope ? parser->scope
16039 : parser->context->object_type);
16040 if (scope && TYPE_P (scope)
16041 && (!CLASS_TYPE_P (scope)
16042 || (check_dependency_p && dependent_type_p (scope))))
16044 /* We're optimizing away the call to cp_parser_lookup_name, but
16045 we still need to do this. */
16046 parser->context->object_type = NULL_TREE;
16047 return identifier;
16052 /* Look up the name. */
16053 decl = cp_parser_lookup_name (parser, identifier,
16054 tag_type,
16055 /*is_template=*/true,
16056 /*is_namespace=*/false,
16057 check_dependency_p,
16058 /*ambiguous_decls=*/NULL,
16059 token->location);
16061 decl = strip_using_decl (decl);
16063 /* If DECL is a template, then the name was a template-name. */
16064 if (TREE_CODE (decl) == TEMPLATE_DECL)
16066 if (TREE_DEPRECATED (decl)
16067 && deprecated_state != DEPRECATED_SUPPRESS)
16068 warn_deprecated_use (decl, NULL_TREE);
16070 else
16072 /* The standard does not explicitly indicate whether a name that
16073 names a set of overloaded declarations, some of which are
16074 templates, is a template-name. However, such a name should
16075 be a template-name; otherwise, there is no way to form a
16076 template-id for the overloaded templates. */
16077 bool found = false;
16079 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16080 !found && iter; ++iter)
16081 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16082 found = true;
16084 if (!found)
16086 /* The name does not name a template. */
16087 cp_parser_error (parser, "expected template-name");
16088 return error_mark_node;
16092 /* If DECL is dependent, and refers to a function, then just return
16093 its name; we will look it up again during template instantiation. */
16094 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16096 tree scope = ovl_scope (decl);
16097 if (TYPE_P (scope) && dependent_type_p (scope))
16098 return identifier;
16101 return decl;
16104 /* Parse a template-argument-list.
16106 template-argument-list:
16107 template-argument ... [opt]
16108 template-argument-list , template-argument ... [opt]
16110 Returns a TREE_VEC containing the arguments. */
16112 static tree
16113 cp_parser_template_argument_list (cp_parser* parser)
16115 tree fixed_args[10];
16116 unsigned n_args = 0;
16117 unsigned alloced = 10;
16118 tree *arg_ary = fixed_args;
16119 tree vec;
16120 bool saved_in_template_argument_list_p;
16121 bool saved_ice_p;
16122 bool saved_non_ice_p;
16124 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16125 parser->in_template_argument_list_p = true;
16126 /* Even if the template-id appears in an integral
16127 constant-expression, the contents of the argument list do
16128 not. */
16129 saved_ice_p = parser->integral_constant_expression_p;
16130 parser->integral_constant_expression_p = false;
16131 saved_non_ice_p = parser->non_integral_constant_expression_p;
16132 parser->non_integral_constant_expression_p = false;
16134 /* Parse the arguments. */
16137 tree argument;
16139 if (n_args)
16140 /* Consume the comma. */
16141 cp_lexer_consume_token (parser->lexer);
16143 /* Parse the template-argument. */
16144 argument = cp_parser_template_argument (parser);
16146 /* If the next token is an ellipsis, we're expanding a template
16147 argument pack. */
16148 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16150 if (argument == error_mark_node)
16152 cp_token *token = cp_lexer_peek_token (parser->lexer);
16153 error_at (token->location,
16154 "expected parameter pack before %<...%>");
16156 /* Consume the `...' token. */
16157 cp_lexer_consume_token (parser->lexer);
16159 /* Make the argument into a TYPE_PACK_EXPANSION or
16160 EXPR_PACK_EXPANSION. */
16161 argument = make_pack_expansion (argument);
16164 if (n_args == alloced)
16166 alloced *= 2;
16168 if (arg_ary == fixed_args)
16170 arg_ary = XNEWVEC (tree, alloced);
16171 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16173 else
16174 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16176 arg_ary[n_args++] = argument;
16178 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16180 vec = make_tree_vec (n_args);
16182 while (n_args--)
16183 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16185 if (arg_ary != fixed_args)
16186 free (arg_ary);
16187 parser->non_integral_constant_expression_p = saved_non_ice_p;
16188 parser->integral_constant_expression_p = saved_ice_p;
16189 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16190 if (CHECKING_P)
16191 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16192 return vec;
16195 /* Parse a template-argument.
16197 template-argument:
16198 assignment-expression
16199 type-id
16200 id-expression
16202 The representation is that of an assignment-expression, type-id, or
16203 id-expression -- except that the qualified id-expression is
16204 evaluated, so that the value returned is either a DECL or an
16205 OVERLOAD.
16207 Although the standard says "assignment-expression", it forbids
16208 throw-expressions or assignments in the template argument.
16209 Therefore, we use "conditional-expression" instead. */
16211 static tree
16212 cp_parser_template_argument (cp_parser* parser)
16214 tree argument;
16215 bool template_p;
16216 bool address_p;
16217 bool maybe_type_id = false;
16218 cp_token *token = NULL, *argument_start_token = NULL;
16219 location_t loc = 0;
16220 cp_id_kind idk;
16222 /* There's really no way to know what we're looking at, so we just
16223 try each alternative in order.
16225 [temp.arg]
16227 In a template-argument, an ambiguity between a type-id and an
16228 expression is resolved to a type-id, regardless of the form of
16229 the corresponding template-parameter.
16231 Therefore, we try a type-id first. */
16232 cp_parser_parse_tentatively (parser);
16233 argument = cp_parser_template_type_arg (parser);
16234 /* If there was no error parsing the type-id but the next token is a
16235 '>>', our behavior depends on which dialect of C++ we're
16236 parsing. In C++98, we probably found a typo for '> >'. But there
16237 are type-id which are also valid expressions. For instance:
16239 struct X { int operator >> (int); };
16240 template <int V> struct Foo {};
16241 Foo<X () >> 5> r;
16243 Here 'X()' is a valid type-id of a function type, but the user just
16244 wanted to write the expression "X() >> 5". Thus, we remember that we
16245 found a valid type-id, but we still try to parse the argument as an
16246 expression to see what happens.
16248 In C++0x, the '>>' will be considered two separate '>'
16249 tokens. */
16250 if (!cp_parser_error_occurred (parser)
16251 && cxx_dialect == cxx98
16252 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16254 maybe_type_id = true;
16255 cp_parser_abort_tentative_parse (parser);
16257 else
16259 /* If the next token isn't a `,' or a `>', then this argument wasn't
16260 really finished. This means that the argument is not a valid
16261 type-id. */
16262 if (!cp_parser_next_token_ends_template_argument_p (parser))
16263 cp_parser_error (parser, "expected template-argument");
16264 /* If that worked, we're done. */
16265 if (cp_parser_parse_definitely (parser))
16266 return argument;
16268 /* We're still not sure what the argument will be. */
16269 cp_parser_parse_tentatively (parser);
16270 /* Try a template. */
16271 argument_start_token = cp_lexer_peek_token (parser->lexer);
16272 argument = cp_parser_id_expression (parser,
16273 /*template_keyword_p=*/false,
16274 /*check_dependency_p=*/true,
16275 &template_p,
16276 /*declarator_p=*/false,
16277 /*optional_p=*/false);
16278 /* If the next token isn't a `,' or a `>', then this argument wasn't
16279 really finished. */
16280 if (!cp_parser_next_token_ends_template_argument_p (parser))
16281 cp_parser_error (parser, "expected template-argument");
16282 if (!cp_parser_error_occurred (parser))
16284 /* Figure out what is being referred to. If the id-expression
16285 was for a class template specialization, then we will have a
16286 TYPE_DECL at this point. There is no need to do name lookup
16287 at this point in that case. */
16288 if (TREE_CODE (argument) != TYPE_DECL)
16289 argument = cp_parser_lookup_name (parser, argument,
16290 none_type,
16291 /*is_template=*/template_p,
16292 /*is_namespace=*/false,
16293 /*check_dependency=*/true,
16294 /*ambiguous_decls=*/NULL,
16295 argument_start_token->location);
16296 /* Handle a constrained-type-specifier for a non-type template
16297 parameter. */
16298 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16299 argument = decl;
16300 else if (TREE_CODE (argument) != TEMPLATE_DECL
16301 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16302 cp_parser_error (parser, "expected template-name");
16304 if (cp_parser_parse_definitely (parser))
16306 if (TREE_DEPRECATED (argument))
16307 warn_deprecated_use (argument, NULL_TREE);
16308 return argument;
16310 /* It must be a non-type argument. In C++17 any constant-expression is
16311 allowed. */
16312 if (cxx_dialect > cxx14)
16313 goto general_expr;
16315 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16317 -- an integral constant-expression of integral or enumeration
16318 type; or
16320 -- the name of a non-type template-parameter; or
16322 -- the name of an object or function with external linkage...
16324 -- the address of an object or function with external linkage...
16326 -- a pointer to member... */
16327 /* Look for a non-type template parameter. */
16328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16330 cp_parser_parse_tentatively (parser);
16331 argument = cp_parser_primary_expression (parser,
16332 /*address_p=*/false,
16333 /*cast_p=*/false,
16334 /*template_arg_p=*/true,
16335 &idk);
16336 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16337 || !cp_parser_next_token_ends_template_argument_p (parser))
16338 cp_parser_simulate_error (parser);
16339 if (cp_parser_parse_definitely (parser))
16340 return argument;
16343 /* If the next token is "&", the argument must be the address of an
16344 object or function with external linkage. */
16345 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16346 if (address_p)
16348 loc = cp_lexer_peek_token (parser->lexer)->location;
16349 cp_lexer_consume_token (parser->lexer);
16351 /* See if we might have an id-expression. */
16352 token = cp_lexer_peek_token (parser->lexer);
16353 if (token->type == CPP_NAME
16354 || token->keyword == RID_OPERATOR
16355 || token->type == CPP_SCOPE
16356 || token->type == CPP_TEMPLATE_ID
16357 || token->type == CPP_NESTED_NAME_SPECIFIER)
16359 cp_parser_parse_tentatively (parser);
16360 argument = cp_parser_primary_expression (parser,
16361 address_p,
16362 /*cast_p=*/false,
16363 /*template_arg_p=*/true,
16364 &idk);
16365 if (cp_parser_error_occurred (parser)
16366 || !cp_parser_next_token_ends_template_argument_p (parser))
16367 cp_parser_abort_tentative_parse (parser);
16368 else
16370 tree probe;
16372 if (INDIRECT_REF_P (argument))
16374 /* Strip the dereference temporarily. */
16375 gcc_assert (REFERENCE_REF_P (argument));
16376 argument = TREE_OPERAND (argument, 0);
16379 /* If we're in a template, we represent a qualified-id referring
16380 to a static data member as a SCOPE_REF even if the scope isn't
16381 dependent so that we can check access control later. */
16382 probe = argument;
16383 if (TREE_CODE (probe) == SCOPE_REF)
16384 probe = TREE_OPERAND (probe, 1);
16385 if (VAR_P (probe))
16387 /* A variable without external linkage might still be a
16388 valid constant-expression, so no error is issued here
16389 if the external-linkage check fails. */
16390 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16391 cp_parser_simulate_error (parser);
16393 else if (is_overloaded_fn (argument))
16394 /* All overloaded functions are allowed; if the external
16395 linkage test does not pass, an error will be issued
16396 later. */
16398 else if (address_p
16399 && (TREE_CODE (argument) == OFFSET_REF
16400 || TREE_CODE (argument) == SCOPE_REF))
16401 /* A pointer-to-member. */
16403 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16405 else
16406 cp_parser_simulate_error (parser);
16408 if (cp_parser_parse_definitely (parser))
16410 if (address_p)
16411 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16412 tf_warning_or_error);
16413 else
16414 argument = convert_from_reference (argument);
16415 return argument;
16419 /* If the argument started with "&", there are no other valid
16420 alternatives at this point. */
16421 if (address_p)
16423 cp_parser_error (parser, "invalid non-type template argument");
16424 return error_mark_node;
16427 general_expr:
16428 /* If the argument wasn't successfully parsed as a type-id followed
16429 by '>>', the argument can only be a constant expression now.
16430 Otherwise, we try parsing the constant-expression tentatively,
16431 because the argument could really be a type-id. */
16432 if (maybe_type_id)
16433 cp_parser_parse_tentatively (parser);
16435 if (cxx_dialect <= cxx14)
16436 argument = cp_parser_constant_expression (parser);
16437 else
16439 /* With C++17 generalized non-type template arguments we need to handle
16440 lvalue constant expressions, too. */
16441 argument = cp_parser_assignment_expression (parser);
16442 require_potential_constant_expression (argument);
16445 if (!maybe_type_id)
16446 return argument;
16447 if (!cp_parser_next_token_ends_template_argument_p (parser))
16448 cp_parser_error (parser, "expected template-argument");
16449 if (cp_parser_parse_definitely (parser))
16450 return argument;
16451 /* We did our best to parse the argument as a non type-id, but that
16452 was the only alternative that matched (albeit with a '>' after
16453 it). We can assume it's just a typo from the user, and a
16454 diagnostic will then be issued. */
16455 return cp_parser_template_type_arg (parser);
16458 /* Parse an explicit-instantiation.
16460 explicit-instantiation:
16461 template declaration
16463 Although the standard says `declaration', what it really means is:
16465 explicit-instantiation:
16466 template decl-specifier-seq [opt] declarator [opt] ;
16468 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16469 supposed to be allowed. A defect report has been filed about this
16470 issue.
16472 GNU Extension:
16474 explicit-instantiation:
16475 storage-class-specifier template
16476 decl-specifier-seq [opt] declarator [opt] ;
16477 function-specifier template
16478 decl-specifier-seq [opt] declarator [opt] ; */
16480 static void
16481 cp_parser_explicit_instantiation (cp_parser* parser)
16483 int declares_class_or_enum;
16484 cp_decl_specifier_seq decl_specifiers;
16485 tree extension_specifier = NULL_TREE;
16487 timevar_push (TV_TEMPLATE_INST);
16489 /* Look for an (optional) storage-class-specifier or
16490 function-specifier. */
16491 if (cp_parser_allow_gnu_extensions_p (parser))
16493 extension_specifier
16494 = cp_parser_storage_class_specifier_opt (parser);
16495 if (!extension_specifier)
16496 extension_specifier
16497 = cp_parser_function_specifier_opt (parser,
16498 /*decl_specs=*/NULL);
16501 /* Look for the `template' keyword. */
16502 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16503 /* Let the front end know that we are processing an explicit
16504 instantiation. */
16505 begin_explicit_instantiation ();
16506 /* [temp.explicit] says that we are supposed to ignore access
16507 control while processing explicit instantiation directives. */
16508 push_deferring_access_checks (dk_no_check);
16509 /* Parse a decl-specifier-seq. */
16510 cp_parser_decl_specifier_seq (parser,
16511 CP_PARSER_FLAGS_OPTIONAL,
16512 &decl_specifiers,
16513 &declares_class_or_enum);
16514 /* If there was exactly one decl-specifier, and it declared a class,
16515 and there's no declarator, then we have an explicit type
16516 instantiation. */
16517 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16519 tree type;
16521 type = check_tag_decl (&decl_specifiers,
16522 /*explicit_type_instantiation_p=*/true);
16523 /* Turn access control back on for names used during
16524 template instantiation. */
16525 pop_deferring_access_checks ();
16526 if (type)
16527 do_type_instantiation (type, extension_specifier,
16528 /*complain=*/tf_error);
16530 else
16532 cp_declarator *declarator;
16533 tree decl;
16535 /* Parse the declarator. */
16536 declarator
16537 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16538 /*ctor_dtor_or_conv_p=*/NULL,
16539 /*parenthesized_p=*/NULL,
16540 /*member_p=*/false,
16541 /*friend_p=*/false);
16542 if (declares_class_or_enum & 2)
16543 cp_parser_check_for_definition_in_return_type (declarator,
16544 decl_specifiers.type,
16545 decl_specifiers.locations[ds_type_spec]);
16546 if (declarator != cp_error_declarator)
16548 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16549 permerror (decl_specifiers.locations[ds_inline],
16550 "explicit instantiation shall not use"
16551 " %<inline%> specifier");
16552 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16553 permerror (decl_specifiers.locations[ds_constexpr],
16554 "explicit instantiation shall not use"
16555 " %<constexpr%> specifier");
16557 decl = grokdeclarator (declarator, &decl_specifiers,
16558 NORMAL, 0, &decl_specifiers.attributes);
16559 /* Turn access control back on for names used during
16560 template instantiation. */
16561 pop_deferring_access_checks ();
16562 /* Do the explicit instantiation. */
16563 do_decl_instantiation (decl, extension_specifier);
16565 else
16567 pop_deferring_access_checks ();
16568 /* Skip the body of the explicit instantiation. */
16569 cp_parser_skip_to_end_of_statement (parser);
16572 /* We're done with the instantiation. */
16573 end_explicit_instantiation ();
16575 cp_parser_consume_semicolon_at_end_of_statement (parser);
16577 timevar_pop (TV_TEMPLATE_INST);
16580 /* Parse an explicit-specialization.
16582 explicit-specialization:
16583 template < > declaration
16585 Although the standard says `declaration', what it really means is:
16587 explicit-specialization:
16588 template <> decl-specifier [opt] init-declarator [opt] ;
16589 template <> function-definition
16590 template <> explicit-specialization
16591 template <> template-declaration */
16593 static void
16594 cp_parser_explicit_specialization (cp_parser* parser)
16596 bool need_lang_pop;
16597 cp_token *token = cp_lexer_peek_token (parser->lexer);
16599 /* Look for the `template' keyword. */
16600 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16601 /* Look for the `<'. */
16602 cp_parser_require (parser, CPP_LESS, RT_LESS);
16603 /* Look for the `>'. */
16604 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16605 /* We have processed another parameter list. */
16606 ++parser->num_template_parameter_lists;
16607 /* [temp]
16609 A template ... explicit specialization ... shall not have C
16610 linkage. */
16611 if (current_lang_name == lang_name_c)
16613 error_at (token->location, "template specialization with C linkage");
16614 maybe_show_extern_c_location ();
16615 /* Give it C++ linkage to avoid confusing other parts of the
16616 front end. */
16617 push_lang_context (lang_name_cplusplus);
16618 need_lang_pop = true;
16620 else
16621 need_lang_pop = false;
16622 /* Let the front end know that we are beginning a specialization. */
16623 if (!begin_specialization ())
16625 end_specialization ();
16626 return;
16629 /* If the next keyword is `template', we need to figure out whether
16630 or not we're looking a template-declaration. */
16631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16633 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16634 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16635 cp_parser_template_declaration_after_export (parser,
16636 /*member_p=*/false);
16637 else
16638 cp_parser_explicit_specialization (parser);
16640 else
16641 /* Parse the dependent declaration. */
16642 cp_parser_single_declaration (parser,
16643 /*checks=*/NULL,
16644 /*member_p=*/false,
16645 /*explicit_specialization_p=*/true,
16646 /*friend_p=*/NULL);
16647 /* We're done with the specialization. */
16648 end_specialization ();
16649 /* For the erroneous case of a template with C linkage, we pushed an
16650 implicit C++ linkage scope; exit that scope now. */
16651 if (need_lang_pop)
16652 pop_lang_context ();
16653 /* We're done with this parameter list. */
16654 --parser->num_template_parameter_lists;
16657 /* Parse a type-specifier.
16659 type-specifier:
16660 simple-type-specifier
16661 class-specifier
16662 enum-specifier
16663 elaborated-type-specifier
16664 cv-qualifier
16666 GNU Extension:
16668 type-specifier:
16669 __complex__
16671 Returns a representation of the type-specifier. For a
16672 class-specifier, enum-specifier, or elaborated-type-specifier, a
16673 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16675 The parser flags FLAGS is used to control type-specifier parsing.
16677 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16678 in a decl-specifier-seq.
16680 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16681 class-specifier, enum-specifier, or elaborated-type-specifier, then
16682 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16683 if a type is declared; 2 if it is defined. Otherwise, it is set to
16684 zero.
16686 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16687 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16688 is set to FALSE. */
16690 static tree
16691 cp_parser_type_specifier (cp_parser* parser,
16692 cp_parser_flags flags,
16693 cp_decl_specifier_seq *decl_specs,
16694 bool is_declaration,
16695 int* declares_class_or_enum,
16696 bool* is_cv_qualifier)
16698 tree type_spec = NULL_TREE;
16699 cp_token *token;
16700 enum rid keyword;
16701 cp_decl_spec ds = ds_last;
16703 /* Assume this type-specifier does not declare a new type. */
16704 if (declares_class_or_enum)
16705 *declares_class_or_enum = 0;
16706 /* And that it does not specify a cv-qualifier. */
16707 if (is_cv_qualifier)
16708 *is_cv_qualifier = false;
16709 /* Peek at the next token. */
16710 token = cp_lexer_peek_token (parser->lexer);
16712 /* If we're looking at a keyword, we can use that to guide the
16713 production we choose. */
16714 keyword = token->keyword;
16715 switch (keyword)
16717 case RID_ENUM:
16718 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16719 goto elaborated_type_specifier;
16721 /* Look for the enum-specifier. */
16722 type_spec = cp_parser_enum_specifier (parser);
16723 /* If that worked, we're done. */
16724 if (type_spec)
16726 if (declares_class_or_enum)
16727 *declares_class_or_enum = 2;
16728 if (decl_specs)
16729 cp_parser_set_decl_spec_type (decl_specs,
16730 type_spec,
16731 token,
16732 /*type_definition_p=*/true);
16733 return type_spec;
16735 else
16736 goto elaborated_type_specifier;
16738 /* Any of these indicate either a class-specifier, or an
16739 elaborated-type-specifier. */
16740 case RID_CLASS:
16741 case RID_STRUCT:
16742 case RID_UNION:
16743 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16744 goto elaborated_type_specifier;
16746 /* Parse tentatively so that we can back up if we don't find a
16747 class-specifier. */
16748 cp_parser_parse_tentatively (parser);
16749 /* Look for the class-specifier. */
16750 type_spec = cp_parser_class_specifier (parser);
16751 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16752 /* If that worked, we're done. */
16753 if (cp_parser_parse_definitely (parser))
16755 if (declares_class_or_enum)
16756 *declares_class_or_enum = 2;
16757 if (decl_specs)
16758 cp_parser_set_decl_spec_type (decl_specs,
16759 type_spec,
16760 token,
16761 /*type_definition_p=*/true);
16762 return type_spec;
16765 /* Fall through. */
16766 elaborated_type_specifier:
16767 /* We're declaring (not defining) a class or enum. */
16768 if (declares_class_or_enum)
16769 *declares_class_or_enum = 1;
16771 /* Fall through. */
16772 case RID_TYPENAME:
16773 /* Look for an elaborated-type-specifier. */
16774 type_spec
16775 = (cp_parser_elaborated_type_specifier
16776 (parser,
16777 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16778 is_declaration));
16779 if (decl_specs)
16780 cp_parser_set_decl_spec_type (decl_specs,
16781 type_spec,
16782 token,
16783 /*type_definition_p=*/false);
16784 return type_spec;
16786 case RID_CONST:
16787 ds = ds_const;
16788 if (is_cv_qualifier)
16789 *is_cv_qualifier = true;
16790 break;
16792 case RID_VOLATILE:
16793 ds = ds_volatile;
16794 if (is_cv_qualifier)
16795 *is_cv_qualifier = true;
16796 break;
16798 case RID_RESTRICT:
16799 ds = ds_restrict;
16800 if (is_cv_qualifier)
16801 *is_cv_qualifier = true;
16802 break;
16804 case RID_COMPLEX:
16805 /* The `__complex__' keyword is a GNU extension. */
16806 ds = ds_complex;
16807 break;
16809 default:
16810 break;
16813 /* Handle simple keywords. */
16814 if (ds != ds_last)
16816 if (decl_specs)
16818 set_and_check_decl_spec_loc (decl_specs, ds, token);
16819 decl_specs->any_specifiers_p = true;
16821 return cp_lexer_consume_token (parser->lexer)->u.value;
16824 /* If we do not already have a type-specifier, assume we are looking
16825 at a simple-type-specifier. */
16826 type_spec = cp_parser_simple_type_specifier (parser,
16827 decl_specs,
16828 flags);
16830 /* If we didn't find a type-specifier, and a type-specifier was not
16831 optional in this context, issue an error message. */
16832 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16834 cp_parser_error (parser, "expected type specifier");
16835 return error_mark_node;
16838 return type_spec;
16841 /* Parse a simple-type-specifier.
16843 simple-type-specifier:
16844 :: [opt] nested-name-specifier [opt] type-name
16845 :: [opt] nested-name-specifier template template-id
16846 char
16847 wchar_t
16848 bool
16849 short
16851 long
16852 signed
16853 unsigned
16854 float
16855 double
16856 void
16858 C++11 Extension:
16860 simple-type-specifier:
16861 auto
16862 decltype ( expression )
16863 char16_t
16864 char32_t
16865 __underlying_type ( type-id )
16867 C++17 extension:
16869 nested-name-specifier(opt) template-name
16871 GNU Extension:
16873 simple-type-specifier:
16874 __int128
16875 __typeof__ unary-expression
16876 __typeof__ ( type-id )
16877 __typeof__ ( type-id ) { initializer-list , [opt] }
16879 Concepts Extension:
16881 simple-type-specifier:
16882 constrained-type-specifier
16884 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16885 appropriately updated. */
16887 static tree
16888 cp_parser_simple_type_specifier (cp_parser* parser,
16889 cp_decl_specifier_seq *decl_specs,
16890 cp_parser_flags flags)
16892 tree type = NULL_TREE;
16893 cp_token *token;
16894 int idx;
16896 /* Peek at the next token. */
16897 token = cp_lexer_peek_token (parser->lexer);
16899 /* If we're looking at a keyword, things are easy. */
16900 switch (token->keyword)
16902 case RID_CHAR:
16903 if (decl_specs)
16904 decl_specs->explicit_char_p = true;
16905 type = char_type_node;
16906 break;
16907 case RID_CHAR16:
16908 type = char16_type_node;
16909 break;
16910 case RID_CHAR32:
16911 type = char32_type_node;
16912 break;
16913 case RID_WCHAR:
16914 type = wchar_type_node;
16915 break;
16916 case RID_BOOL:
16917 type = boolean_type_node;
16918 break;
16919 case RID_SHORT:
16920 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16921 type = short_integer_type_node;
16922 break;
16923 case RID_INT:
16924 if (decl_specs)
16925 decl_specs->explicit_int_p = true;
16926 type = integer_type_node;
16927 break;
16928 case RID_INT_N_0:
16929 case RID_INT_N_1:
16930 case RID_INT_N_2:
16931 case RID_INT_N_3:
16932 idx = token->keyword - RID_INT_N_0;
16933 if (! int_n_enabled_p [idx])
16934 break;
16935 if (decl_specs)
16937 decl_specs->explicit_intN_p = true;
16938 decl_specs->int_n_idx = idx;
16940 type = int_n_trees [idx].signed_type;
16941 break;
16942 case RID_LONG:
16943 if (decl_specs)
16944 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16945 type = long_integer_type_node;
16946 break;
16947 case RID_SIGNED:
16948 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16949 type = integer_type_node;
16950 break;
16951 case RID_UNSIGNED:
16952 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16953 type = unsigned_type_node;
16954 break;
16955 case RID_FLOAT:
16956 type = float_type_node;
16957 break;
16958 case RID_DOUBLE:
16959 type = double_type_node;
16960 break;
16961 case RID_VOID:
16962 type = void_type_node;
16963 break;
16965 case RID_AUTO:
16966 maybe_warn_cpp0x (CPP0X_AUTO);
16967 if (parser->auto_is_implicit_function_template_parm_p)
16969 /* The 'auto' might be the placeholder return type for a function decl
16970 with trailing return type. */
16971 bool have_trailing_return_fn_decl = false;
16973 cp_parser_parse_tentatively (parser);
16974 cp_lexer_consume_token (parser->lexer);
16975 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16976 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16977 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16978 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16982 cp_lexer_consume_token (parser->lexer);
16983 cp_parser_skip_to_closing_parenthesis (parser,
16984 /*recovering*/false,
16985 /*or_comma*/false,
16986 /*consume_paren*/true);
16987 continue;
16990 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16992 have_trailing_return_fn_decl = true;
16993 break;
16996 cp_lexer_consume_token (parser->lexer);
16998 cp_parser_abort_tentative_parse (parser);
17000 if (have_trailing_return_fn_decl)
17002 type = make_auto ();
17003 break;
17006 if (cxx_dialect >= cxx14)
17008 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17009 type = TREE_TYPE (type);
17011 else
17012 type = error_mark_node;
17014 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17016 if (cxx_dialect < cxx14)
17017 error_at (token->location,
17018 "use of %<auto%> in lambda parameter declaration "
17019 "only available with "
17020 "-std=c++14 or -std=gnu++14");
17022 else if (cxx_dialect < cxx14)
17023 error_at (token->location,
17024 "use of %<auto%> in parameter declaration "
17025 "only available with "
17026 "-std=c++14 or -std=gnu++14");
17027 else if (!flag_concepts)
17028 pedwarn (token->location, OPT_Wpedantic,
17029 "ISO C++ forbids use of %<auto%> in parameter "
17030 "declaration");
17032 else
17033 type = make_auto ();
17034 break;
17036 case RID_DECLTYPE:
17037 /* Since DR 743, decltype can either be a simple-type-specifier by
17038 itself or begin a nested-name-specifier. Parsing it will replace
17039 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17040 handling below decide what to do. */
17041 cp_parser_decltype (parser);
17042 cp_lexer_set_token_position (parser->lexer, token);
17043 break;
17045 case RID_TYPEOF:
17046 /* Consume the `typeof' token. */
17047 cp_lexer_consume_token (parser->lexer);
17048 /* Parse the operand to `typeof'. */
17049 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17050 /* If it is not already a TYPE, take its type. */
17051 if (!TYPE_P (type))
17052 type = finish_typeof (type);
17054 if (decl_specs)
17055 cp_parser_set_decl_spec_type (decl_specs, type,
17056 token,
17057 /*type_definition_p=*/false);
17059 return type;
17061 case RID_UNDERLYING_TYPE:
17062 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17063 if (decl_specs)
17064 cp_parser_set_decl_spec_type (decl_specs, type,
17065 token,
17066 /*type_definition_p=*/false);
17068 return type;
17070 case RID_BASES:
17071 case RID_DIRECT_BASES:
17072 type = cp_parser_trait_expr (parser, token->keyword);
17073 if (decl_specs)
17074 cp_parser_set_decl_spec_type (decl_specs, type,
17075 token,
17076 /*type_definition_p=*/false);
17077 return type;
17078 default:
17079 break;
17082 /* If token is an already-parsed decltype not followed by ::,
17083 it's a simple-type-specifier. */
17084 if (token->type == CPP_DECLTYPE
17085 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17087 type = saved_checks_value (token->u.tree_check_value);
17088 if (decl_specs)
17090 cp_parser_set_decl_spec_type (decl_specs, type,
17091 token,
17092 /*type_definition_p=*/false);
17093 /* Remember that we are handling a decltype in order to
17094 implement the resolution of DR 1510 when the argument
17095 isn't instantiation dependent. */
17096 decl_specs->decltype_p = true;
17098 cp_lexer_consume_token (parser->lexer);
17099 return type;
17102 /* If the type-specifier was for a built-in type, we're done. */
17103 if (type)
17105 /* Record the type. */
17106 if (decl_specs
17107 && (token->keyword != RID_SIGNED
17108 && token->keyword != RID_UNSIGNED
17109 && token->keyword != RID_SHORT
17110 && token->keyword != RID_LONG))
17111 cp_parser_set_decl_spec_type (decl_specs,
17112 type,
17113 token,
17114 /*type_definition_p=*/false);
17115 if (decl_specs)
17116 decl_specs->any_specifiers_p = true;
17118 /* Consume the token. */
17119 cp_lexer_consume_token (parser->lexer);
17121 if (type == error_mark_node)
17122 return error_mark_node;
17124 /* There is no valid C++ program where a non-template type is
17125 followed by a "<". That usually indicates that the user thought
17126 that the type was a template. */
17127 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17128 token->location);
17130 return TYPE_NAME (type);
17133 /* The type-specifier must be a user-defined type. */
17134 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17136 bool qualified_p;
17137 bool global_p;
17139 /* Don't gobble tokens or issue error messages if this is an
17140 optional type-specifier. */
17141 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17142 cp_parser_parse_tentatively (parser);
17144 token = cp_lexer_peek_token (parser->lexer);
17146 /* Look for the optional `::' operator. */
17147 global_p
17148 = (cp_parser_global_scope_opt (parser,
17149 /*current_scope_valid_p=*/false)
17150 != NULL_TREE);
17151 /* Look for the nested-name specifier. */
17152 qualified_p
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 != NULL_TREE);
17159 /* If we have seen a nested-name-specifier, and the next token
17160 is `template', then we are using the template-id production. */
17161 if (parser->scope
17162 && cp_parser_optional_template_keyword (parser))
17164 /* Look for the template-id. */
17165 type = cp_parser_template_id (parser,
17166 /*template_keyword_p=*/true,
17167 /*check_dependency_p=*/true,
17168 none_type,
17169 /*is_declaration=*/false);
17170 /* If the template-id did not name a type, we are out of
17171 luck. */
17172 if (TREE_CODE (type) != TYPE_DECL)
17174 cp_parser_error (parser, "expected template-id for type");
17175 type = NULL_TREE;
17178 /* Otherwise, look for a type-name. */
17179 else
17180 type = cp_parser_type_name (parser);
17181 /* Keep track of all name-lookups performed in class scopes. */
17182 if (type
17183 && !global_p
17184 && !qualified_p
17185 && TREE_CODE (type) == TYPE_DECL
17186 && identifier_p (DECL_NAME (type)))
17187 maybe_note_name_used_in_class (DECL_NAME (type), type);
17188 /* If it didn't work out, we don't have a TYPE. */
17189 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17190 && !cp_parser_parse_definitely (parser))
17191 type = NULL_TREE;
17192 if (!type && cxx_dialect >= cxx17)
17194 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17195 cp_parser_parse_tentatively (parser);
17197 cp_parser_global_scope_opt (parser,
17198 /*current_scope_valid_p=*/false);
17199 cp_parser_nested_name_specifier_opt (parser,
17200 /*typename_keyword_p=*/false,
17201 /*check_dependency_p=*/true,
17202 /*type_p=*/false,
17203 /*is_declaration=*/false);
17204 tree name = cp_parser_identifier (parser);
17205 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17206 && parser->scope != error_mark_node)
17208 tree tmpl = cp_parser_lookup_name (parser, name,
17209 none_type,
17210 /*is_template=*/false,
17211 /*is_namespace=*/false,
17212 /*check_dependency=*/true,
17213 /*ambiguous_decls=*/NULL,
17214 token->location);
17215 if (tmpl && tmpl != error_mark_node
17216 && (DECL_CLASS_TEMPLATE_P (tmpl)
17217 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17218 type = make_template_placeholder (tmpl);
17219 else
17221 type = error_mark_node;
17222 if (!cp_parser_simulate_error (parser))
17223 cp_parser_name_lookup_error (parser, name, tmpl,
17224 NLE_TYPE, token->location);
17227 else
17228 type = error_mark_node;
17230 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17231 && !cp_parser_parse_definitely (parser))
17232 type = NULL_TREE;
17234 if (type && decl_specs)
17235 cp_parser_set_decl_spec_type (decl_specs, type,
17236 token,
17237 /*type_definition_p=*/false);
17240 /* If we didn't get a type-name, issue an error message. */
17241 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17243 cp_parser_error (parser, "expected type-name");
17244 return error_mark_node;
17247 if (type && type != error_mark_node)
17249 /* See if TYPE is an Objective-C type, and if so, parse and
17250 accept any protocol references following it. Do this before
17251 the cp_parser_check_for_invalid_template_id() call, because
17252 Objective-C types can be followed by '<...>' which would
17253 enclose protocol names rather than template arguments, and so
17254 everything is fine. */
17255 if (c_dialect_objc () && !parser->scope
17256 && (objc_is_id (type) || objc_is_class_name (type)))
17258 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17259 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17261 /* Clobber the "unqualified" type previously entered into
17262 DECL_SPECS with the new, improved protocol-qualified version. */
17263 if (decl_specs)
17264 decl_specs->type = qual_type;
17266 return qual_type;
17269 /* There is no valid C++ program where a non-template type is
17270 followed by a "<". That usually indicates that the user
17271 thought that the type was a template. */
17272 cp_parser_check_for_invalid_template_id (parser, type,
17273 none_type,
17274 token->location);
17277 return type;
17280 /* Parse a type-name.
17282 type-name:
17283 class-name
17284 enum-name
17285 typedef-name
17286 simple-template-id [in c++0x]
17288 enum-name:
17289 identifier
17291 typedef-name:
17292 identifier
17294 Concepts:
17296 type-name:
17297 concept-name
17298 partial-concept-id
17300 concept-name:
17301 identifier
17303 Returns a TYPE_DECL for the type. */
17305 static tree
17306 cp_parser_type_name (cp_parser* parser)
17308 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17311 /* See above. */
17312 static tree
17313 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17315 tree type_decl;
17317 /* We can't know yet whether it is a class-name or not. */
17318 cp_parser_parse_tentatively (parser);
17319 /* Try a class-name. */
17320 type_decl = cp_parser_class_name (parser,
17321 typename_keyword_p,
17322 /*template_keyword_p=*/false,
17323 none_type,
17324 /*check_dependency_p=*/true,
17325 /*class_head_p=*/false,
17326 /*is_declaration=*/false);
17327 /* If it's not a class-name, keep looking. */
17328 if (!cp_parser_parse_definitely (parser))
17330 if (cxx_dialect < cxx11)
17331 /* It must be a typedef-name or an enum-name. */
17332 return cp_parser_nonclass_name (parser);
17334 cp_parser_parse_tentatively (parser);
17335 /* It is either a simple-template-id representing an
17336 instantiation of an alias template... */
17337 type_decl = cp_parser_template_id (parser,
17338 /*template_keyword_p=*/false,
17339 /*check_dependency_p=*/true,
17340 none_type,
17341 /*is_declaration=*/false);
17342 /* Note that this must be an instantiation of an alias template
17343 because [temp.names]/6 says:
17345 A template-id that names an alias template specialization
17346 is a type-name.
17348 Whereas [temp.names]/7 says:
17350 A simple-template-id that names a class template
17351 specialization is a class-name.
17353 With concepts, this could also be a partial-concept-id that
17354 declares a non-type template parameter. */
17355 if (type_decl != NULL_TREE
17356 && TREE_CODE (type_decl) == TYPE_DECL
17357 && TYPE_DECL_ALIAS_P (type_decl))
17358 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17359 else if (is_constrained_parameter (type_decl))
17360 /* Don't do anything. */ ;
17361 else
17362 cp_parser_simulate_error (parser);
17364 if (!cp_parser_parse_definitely (parser))
17365 /* ... Or a typedef-name or an enum-name. */
17366 return cp_parser_nonclass_name (parser);
17369 return type_decl;
17372 /* Check if DECL and ARGS can form a constrained-type-specifier.
17373 If ARGS is non-null, we try to form a concept check of the
17374 form DECL<?, ARGS> where ? is a wildcard that matches any
17375 kind of template argument. If ARGS is NULL, then we try to
17376 form a concept check of the form DECL<?>. */
17378 static tree
17379 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17380 tree decl, tree args)
17382 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17384 /* If we a constrained-type-specifier cannot be deduced. */
17385 if (parser->prevent_constrained_type_specifiers)
17386 return NULL_TREE;
17388 /* A constrained type specifier can only be found in an
17389 overload set or as a reference to a template declaration.
17391 FIXME: This might be masking a bug. It's possible that
17392 that the deduction below is causing template specializations
17393 to be formed with the wildcard as an argument. */
17394 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17395 return NULL_TREE;
17397 /* Try to build a call expression that evaluates the
17398 concept. This can fail if the overload set refers
17399 only to non-templates. */
17400 tree placeholder = build_nt (WILDCARD_DECL);
17401 tree check = build_concept_check (decl, placeholder, args);
17402 if (check == error_mark_node)
17403 return NULL_TREE;
17405 /* Deduce the checked constraint and the prototype parameter.
17407 FIXME: In certain cases, failure to deduce should be a
17408 diagnosable error. */
17409 tree conc;
17410 tree proto;
17411 if (!deduce_constrained_parameter (check, conc, proto))
17412 return NULL_TREE;
17414 /* In template parameter scope, this results in a constrained
17415 parameter. Return a descriptor of that parm. */
17416 if (processing_template_parmlist)
17417 return build_constrained_parameter (conc, proto, args);
17419 /* In a parameter-declaration-clause, constrained-type
17420 specifiers result in invented template parameters. */
17421 if (parser->auto_is_implicit_function_template_parm_p)
17423 tree x = build_constrained_parameter (conc, proto, args);
17424 return synthesize_implicit_template_parm (parser, x);
17426 else
17428 /* Otherwise, we're in a context where the constrained
17429 type name is deduced and the constraint applies
17430 after deduction. */
17431 return make_constrained_auto (conc, args);
17434 return NULL_TREE;
17437 /* If DECL refers to a concept, return a TYPE_DECL representing
17438 the result of using the constrained type specifier in the
17439 current context. DECL refers to a concept if
17441 - it is an overload set containing a function concept taking a single
17442 type argument, or
17444 - it is a variable concept taking a single type argument. */
17446 static tree
17447 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17449 if (flag_concepts
17450 && (TREE_CODE (decl) == OVERLOAD
17451 || BASELINK_P (decl)
17452 || variable_concept_p (decl)))
17453 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17454 else
17455 return NULL_TREE;
17458 /* Check if DECL and ARGS form a partial-concept-id. If so,
17459 assign ID to the resulting constrained placeholder.
17461 Returns true if the partial-concept-id designates a placeholder
17462 and false otherwise. Note that *id is set to NULL_TREE in
17463 this case. */
17465 static tree
17466 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17468 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17471 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17472 or a concept-name.
17474 enum-name:
17475 identifier
17477 typedef-name:
17478 identifier
17480 concept-name:
17481 identifier
17483 Returns a TYPE_DECL for the type. */
17485 static tree
17486 cp_parser_nonclass_name (cp_parser* parser)
17488 tree type_decl;
17489 tree identifier;
17491 cp_token *token = cp_lexer_peek_token (parser->lexer);
17492 identifier = cp_parser_identifier (parser);
17493 if (identifier == error_mark_node)
17494 return error_mark_node;
17496 /* Look up the type-name. */
17497 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17499 type_decl = strip_using_decl (type_decl);
17501 /* If we found an overload set, then it may refer to a concept-name. */
17502 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17503 type_decl = decl;
17505 if (TREE_CODE (type_decl) != TYPE_DECL
17506 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17508 /* See if this is an Objective-C type. */
17509 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17510 tree type = objc_get_protocol_qualified_type (identifier, protos);
17511 if (type)
17512 type_decl = TYPE_NAME (type);
17515 /* Issue an error if we did not find a type-name. */
17516 if (TREE_CODE (type_decl) != TYPE_DECL
17517 /* In Objective-C, we have the complication that class names are
17518 normally type names and start declarations (eg, the
17519 "NSObject" in "NSObject *object;"), but can be used in an
17520 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17521 is an expression. So, a classname followed by a dot is not a
17522 valid type-name. */
17523 || (objc_is_class_name (TREE_TYPE (type_decl))
17524 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17526 if (!cp_parser_simulate_error (parser))
17527 cp_parser_name_lookup_error (parser, identifier, type_decl,
17528 NLE_TYPE, token->location);
17529 return error_mark_node;
17531 /* Remember that the name was used in the definition of the
17532 current class so that we can check later to see if the
17533 meaning would have been different after the class was
17534 entirely defined. */
17535 else if (type_decl != error_mark_node
17536 && !parser->scope)
17537 maybe_note_name_used_in_class (identifier, type_decl);
17539 return type_decl;
17542 /* Parse an elaborated-type-specifier. Note that the grammar given
17543 here incorporates the resolution to DR68.
17545 elaborated-type-specifier:
17546 class-key :: [opt] nested-name-specifier [opt] identifier
17547 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17548 enum-key :: [opt] nested-name-specifier [opt] identifier
17549 typename :: [opt] nested-name-specifier identifier
17550 typename :: [opt] nested-name-specifier template [opt]
17551 template-id
17553 GNU extension:
17555 elaborated-type-specifier:
17556 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17557 class-key attributes :: [opt] nested-name-specifier [opt]
17558 template [opt] template-id
17559 enum attributes :: [opt] nested-name-specifier [opt] identifier
17561 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17562 declared `friend'. If IS_DECLARATION is TRUE, then this
17563 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17564 something is being declared.
17566 Returns the TYPE specified. */
17568 static tree
17569 cp_parser_elaborated_type_specifier (cp_parser* parser,
17570 bool is_friend,
17571 bool is_declaration)
17573 enum tag_types tag_type;
17574 tree identifier;
17575 tree type = NULL_TREE;
17576 tree attributes = NULL_TREE;
17577 tree globalscope;
17578 cp_token *token = NULL;
17580 /* See if we're looking at the `enum' keyword. */
17581 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17583 /* Consume the `enum' token. */
17584 cp_lexer_consume_token (parser->lexer);
17585 /* Remember that it's an enumeration type. */
17586 tag_type = enum_type;
17587 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17588 enums) is used here. */
17589 cp_token *token = cp_lexer_peek_token (parser->lexer);
17590 if (cp_parser_is_keyword (token, RID_CLASS)
17591 || cp_parser_is_keyword (token, RID_STRUCT))
17593 gcc_rich_location richloc (token->location);
17594 richloc.add_range (input_location, false);
17595 richloc.add_fixit_remove ();
17596 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17597 "a scoped enum must not use the %qD keyword",
17598 token->u.value);
17599 /* Consume the `struct' or `class' and parse it anyway. */
17600 cp_lexer_consume_token (parser->lexer);
17602 /* Parse the attributes. */
17603 attributes = cp_parser_attributes_opt (parser);
17605 /* Or, it might be `typename'. */
17606 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17607 RID_TYPENAME))
17609 /* Consume the `typename' token. */
17610 cp_lexer_consume_token (parser->lexer);
17611 /* Remember that it's a `typename' type. */
17612 tag_type = typename_type;
17614 /* Otherwise it must be a class-key. */
17615 else
17617 tag_type = cp_parser_class_key (parser);
17618 if (tag_type == none_type)
17619 return error_mark_node;
17620 /* Parse the attributes. */
17621 attributes = cp_parser_attributes_opt (parser);
17624 /* Look for the `::' operator. */
17625 globalscope = cp_parser_global_scope_opt (parser,
17626 /*current_scope_valid_p=*/false);
17627 /* Look for the nested-name-specifier. */
17628 tree nested_name_specifier;
17629 if (tag_type == typename_type && !globalscope)
17631 nested_name_specifier
17632 = cp_parser_nested_name_specifier (parser,
17633 /*typename_keyword_p=*/true,
17634 /*check_dependency_p=*/true,
17635 /*type_p=*/true,
17636 is_declaration);
17637 if (!nested_name_specifier)
17638 return error_mark_node;
17640 else
17641 /* Even though `typename' is not present, the proposed resolution
17642 to Core Issue 180 says that in `class A<T>::B', `B' should be
17643 considered a type-name, even if `A<T>' is dependent. */
17644 nested_name_specifier
17645 = cp_parser_nested_name_specifier_opt (parser,
17646 /*typename_keyword_p=*/true,
17647 /*check_dependency_p=*/true,
17648 /*type_p=*/true,
17649 is_declaration);
17650 /* For everything but enumeration types, consider a template-id.
17651 For an enumeration type, consider only a plain identifier. */
17652 if (tag_type != enum_type)
17654 bool template_p = false;
17655 tree decl;
17657 /* Allow the `template' keyword. */
17658 template_p = cp_parser_optional_template_keyword (parser);
17659 /* If we didn't see `template', we don't know if there's a
17660 template-id or not. */
17661 if (!template_p)
17662 cp_parser_parse_tentatively (parser);
17663 /* Parse the template-id. */
17664 token = cp_lexer_peek_token (parser->lexer);
17665 decl = cp_parser_template_id (parser, template_p,
17666 /*check_dependency_p=*/true,
17667 tag_type,
17668 is_declaration);
17669 /* If we didn't find a template-id, look for an ordinary
17670 identifier. */
17671 if (!template_p && !cp_parser_parse_definitely (parser))
17673 /* We can get here when cp_parser_template_id, called by
17674 cp_parser_class_name with tag_type == none_type, succeeds
17675 and caches a BASELINK. Then, when called again here,
17676 instead of failing and returning an error_mark_node
17677 returns it (see template/typename17.C in C++11).
17678 ??? Could we diagnose this earlier? */
17679 else if (tag_type == typename_type && BASELINK_P (decl))
17681 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17682 type = error_mark_node;
17684 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17685 in effect, then we must assume that, upon instantiation, the
17686 template will correspond to a class. */
17687 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17688 && tag_type == typename_type)
17689 type = make_typename_type (parser->scope, decl,
17690 typename_type,
17691 /*complain=*/tf_error);
17692 /* If the `typename' keyword is in effect and DECL is not a type
17693 decl, then type is non existent. */
17694 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17696 else if (TREE_CODE (decl) == TYPE_DECL)
17698 type = check_elaborated_type_specifier (tag_type, decl,
17699 /*allow_template_p=*/true);
17701 /* If the next token is a semicolon, this must be a specialization,
17702 instantiation, or friend declaration. Check the scope while we
17703 still know whether or not we had a nested-name-specifier. */
17704 if (type != error_mark_node
17705 && !nested_name_specifier && !is_friend
17706 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17707 check_unqualified_spec_or_inst (type, token->location);
17709 else if (decl == error_mark_node)
17710 type = error_mark_node;
17713 if (!type)
17715 token = cp_lexer_peek_token (parser->lexer);
17716 identifier = cp_parser_identifier (parser);
17718 if (identifier == error_mark_node)
17720 parser->scope = NULL_TREE;
17721 return error_mark_node;
17724 /* For a `typename', we needn't call xref_tag. */
17725 if (tag_type == typename_type
17726 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17727 return cp_parser_make_typename_type (parser, identifier,
17728 token->location);
17730 /* Template parameter lists apply only if we are not within a
17731 function parameter list. */
17732 bool template_parm_lists_apply
17733 = parser->num_template_parameter_lists;
17734 if (template_parm_lists_apply)
17735 for (cp_binding_level *s = current_binding_level;
17736 s && s->kind != sk_template_parms;
17737 s = s->level_chain)
17738 if (s->kind == sk_function_parms)
17739 template_parm_lists_apply = false;
17741 /* Look up a qualified name in the usual way. */
17742 if (parser->scope)
17744 tree decl;
17745 tree ambiguous_decls;
17747 decl = cp_parser_lookup_name (parser, identifier,
17748 tag_type,
17749 /*is_template=*/false,
17750 /*is_namespace=*/false,
17751 /*check_dependency=*/true,
17752 &ambiguous_decls,
17753 token->location);
17755 /* If the lookup was ambiguous, an error will already have been
17756 issued. */
17757 if (ambiguous_decls)
17758 return error_mark_node;
17760 /* If we are parsing friend declaration, DECL may be a
17761 TEMPLATE_DECL tree node here. However, we need to check
17762 whether this TEMPLATE_DECL results in valid code. Consider
17763 the following example:
17765 namespace N {
17766 template <class T> class C {};
17768 class X {
17769 template <class T> friend class N::C; // #1, valid code
17771 template <class T> class Y {
17772 friend class N::C; // #2, invalid code
17775 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17776 name lookup of `N::C'. We see that friend declaration must
17777 be template for the code to be valid. Note that
17778 processing_template_decl does not work here since it is
17779 always 1 for the above two cases. */
17781 decl = (cp_parser_maybe_treat_template_as_class
17782 (decl, /*tag_name_p=*/is_friend
17783 && template_parm_lists_apply));
17785 if (TREE_CODE (decl) != TYPE_DECL)
17787 cp_parser_diagnose_invalid_type_name (parser,
17788 identifier,
17789 token->location);
17790 return error_mark_node;
17793 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17795 bool allow_template = (template_parm_lists_apply
17796 || DECL_SELF_REFERENCE_P (decl));
17797 type = check_elaborated_type_specifier (tag_type, decl,
17798 allow_template);
17800 if (type == error_mark_node)
17801 return error_mark_node;
17804 /* Forward declarations of nested types, such as
17806 class C1::C2;
17807 class C1::C2::C3;
17809 are invalid unless all components preceding the final '::'
17810 are complete. If all enclosing types are complete, these
17811 declarations become merely pointless.
17813 Invalid forward declarations of nested types are errors
17814 caught elsewhere in parsing. Those that are pointless arrive
17815 here. */
17817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17818 && !is_friend && !processing_explicit_instantiation)
17819 warning (0, "declaration %qD does not declare anything", decl);
17821 type = TREE_TYPE (decl);
17823 else
17825 /* An elaborated-type-specifier sometimes introduces a new type and
17826 sometimes names an existing type. Normally, the rule is that it
17827 introduces a new type only if there is not an existing type of
17828 the same name already in scope. For example, given:
17830 struct S {};
17831 void f() { struct S s; }
17833 the `struct S' in the body of `f' is the same `struct S' as in
17834 the global scope; the existing definition is used. However, if
17835 there were no global declaration, this would introduce a new
17836 local class named `S'.
17838 An exception to this rule applies to the following code:
17840 namespace N { struct S; }
17842 Here, the elaborated-type-specifier names a new type
17843 unconditionally; even if there is already an `S' in the
17844 containing scope this declaration names a new type.
17845 This exception only applies if the elaborated-type-specifier
17846 forms the complete declaration:
17848 [class.name]
17850 A declaration consisting solely of `class-key identifier ;' is
17851 either a redeclaration of the name in the current scope or a
17852 forward declaration of the identifier as a class name. It
17853 introduces the name into the current scope.
17855 We are in this situation precisely when the next token is a `;'.
17857 An exception to the exception is that a `friend' declaration does
17858 *not* name a new type; i.e., given:
17860 struct S { friend struct T; };
17862 `T' is not a new type in the scope of `S'.
17864 Also, `new struct S' or `sizeof (struct S)' never results in the
17865 definition of a new type; a new type can only be declared in a
17866 declaration context. */
17868 tag_scope ts;
17869 bool template_p;
17871 if (is_friend)
17872 /* Friends have special name lookup rules. */
17873 ts = ts_within_enclosing_non_class;
17874 else if (is_declaration
17875 && cp_lexer_next_token_is (parser->lexer,
17876 CPP_SEMICOLON))
17877 /* This is a `class-key identifier ;' */
17878 ts = ts_current;
17879 else
17880 ts = ts_global;
17882 template_p =
17883 (template_parm_lists_apply
17884 && (cp_parser_next_token_starts_class_definition_p (parser)
17885 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17886 /* An unqualified name was used to reference this type, so
17887 there were no qualifying templates. */
17888 if (template_parm_lists_apply
17889 && !cp_parser_check_template_parameters (parser,
17890 /*num_templates=*/0,
17891 token->location,
17892 /*declarator=*/NULL))
17893 return error_mark_node;
17894 type = xref_tag (tag_type, identifier, ts, template_p);
17898 if (type == error_mark_node)
17899 return error_mark_node;
17901 /* Allow attributes on forward declarations of classes. */
17902 if (attributes)
17904 if (TREE_CODE (type) == TYPENAME_TYPE)
17905 warning (OPT_Wattributes,
17906 "attributes ignored on uninstantiated type");
17907 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17908 && ! processing_explicit_instantiation)
17909 warning (OPT_Wattributes,
17910 "attributes ignored on template instantiation");
17911 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17912 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17913 else
17914 warning (OPT_Wattributes,
17915 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17918 if (tag_type != enum_type)
17920 /* Indicate whether this class was declared as a `class' or as a
17921 `struct'. */
17922 if (CLASS_TYPE_P (type))
17923 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17924 cp_parser_check_class_key (tag_type, type);
17927 /* A "<" cannot follow an elaborated type specifier. If that
17928 happens, the user was probably trying to form a template-id. */
17929 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17930 token->location);
17932 return type;
17935 /* Parse an enum-specifier.
17937 enum-specifier:
17938 enum-head { enumerator-list [opt] }
17939 enum-head { enumerator-list , } [C++0x]
17941 enum-head:
17942 enum-key identifier [opt] enum-base [opt]
17943 enum-key nested-name-specifier identifier enum-base [opt]
17945 enum-key:
17946 enum
17947 enum class [C++0x]
17948 enum struct [C++0x]
17950 enum-base: [C++0x]
17951 : type-specifier-seq
17953 opaque-enum-specifier:
17954 enum-key identifier enum-base [opt] ;
17956 GNU Extensions:
17957 enum-key attributes[opt] identifier [opt] enum-base [opt]
17958 { enumerator-list [opt] }attributes[opt]
17959 enum-key attributes[opt] identifier [opt] enum-base [opt]
17960 { enumerator-list, }attributes[opt] [C++0x]
17962 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17963 if the token stream isn't an enum-specifier after all. */
17965 static tree
17966 cp_parser_enum_specifier (cp_parser* parser)
17968 tree identifier;
17969 tree type = NULL_TREE;
17970 tree prev_scope;
17971 tree nested_name_specifier = NULL_TREE;
17972 tree attributes;
17973 bool scoped_enum_p = false;
17974 bool has_underlying_type = false;
17975 bool nested_being_defined = false;
17976 bool new_value_list = false;
17977 bool is_new_type = false;
17978 bool is_unnamed = false;
17979 tree underlying_type = NULL_TREE;
17980 cp_token *type_start_token = NULL;
17981 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17983 parser->colon_corrects_to_scope_p = false;
17985 /* Parse tentatively so that we can back up if we don't find a
17986 enum-specifier. */
17987 cp_parser_parse_tentatively (parser);
17989 /* Caller guarantees that the current token is 'enum', an identifier
17990 possibly follows, and the token after that is an opening brace.
17991 If we don't have an identifier, fabricate an anonymous name for
17992 the enumeration being defined. */
17993 cp_lexer_consume_token (parser->lexer);
17995 /* Parse the "class" or "struct", which indicates a scoped
17996 enumeration type in C++0x. */
17997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17998 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18000 if (cxx_dialect < cxx11)
18001 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18003 /* Consume the `struct' or `class' token. */
18004 cp_lexer_consume_token (parser->lexer);
18006 scoped_enum_p = true;
18009 attributes = cp_parser_attributes_opt (parser);
18011 /* Clear the qualification. */
18012 parser->scope = NULL_TREE;
18013 parser->qualifying_scope = NULL_TREE;
18014 parser->object_scope = NULL_TREE;
18016 /* Figure out in what scope the declaration is being placed. */
18017 prev_scope = current_scope ();
18019 type_start_token = cp_lexer_peek_token (parser->lexer);
18021 push_deferring_access_checks (dk_no_check);
18022 nested_name_specifier
18023 = cp_parser_nested_name_specifier_opt (parser,
18024 /*typename_keyword_p=*/true,
18025 /*check_dependency_p=*/false,
18026 /*type_p=*/false,
18027 /*is_declaration=*/false);
18029 if (nested_name_specifier)
18031 tree name;
18033 identifier = cp_parser_identifier (parser);
18034 name = cp_parser_lookup_name (parser, identifier,
18035 enum_type,
18036 /*is_template=*/false,
18037 /*is_namespace=*/false,
18038 /*check_dependency=*/true,
18039 /*ambiguous_decls=*/NULL,
18040 input_location);
18041 if (name && name != error_mark_node)
18043 type = TREE_TYPE (name);
18044 if (TREE_CODE (type) == TYPENAME_TYPE)
18046 /* Are template enums allowed in ISO? */
18047 if (template_parm_scope_p ())
18048 pedwarn (type_start_token->location, OPT_Wpedantic,
18049 "%qD is an enumeration template", name);
18050 /* ignore a typename reference, for it will be solved by name
18051 in start_enum. */
18052 type = NULL_TREE;
18055 else if (nested_name_specifier == error_mark_node)
18056 /* We already issued an error. */;
18057 else
18059 error_at (type_start_token->location,
18060 "%qD does not name an enumeration in %qT",
18061 identifier, nested_name_specifier);
18062 nested_name_specifier = error_mark_node;
18065 else
18067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18068 identifier = cp_parser_identifier (parser);
18069 else
18071 identifier = make_anon_name ();
18072 is_unnamed = true;
18073 if (scoped_enum_p)
18074 error_at (type_start_token->location,
18075 "unnamed scoped enum is not allowed");
18078 pop_deferring_access_checks ();
18080 /* Check for the `:' that denotes a specified underlying type in C++0x.
18081 Note that a ':' could also indicate a bitfield width, however. */
18082 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18084 cp_decl_specifier_seq type_specifiers;
18086 /* Consume the `:'. */
18087 cp_lexer_consume_token (parser->lexer);
18089 /* Parse the type-specifier-seq. */
18090 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18091 /*is_trailing_return=*/false,
18092 &type_specifiers);
18094 /* At this point this is surely not elaborated type specifier. */
18095 if (!cp_parser_parse_definitely (parser))
18096 return NULL_TREE;
18098 if (cxx_dialect < cxx11)
18099 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18101 has_underlying_type = true;
18103 /* If that didn't work, stop. */
18104 if (type_specifiers.type != error_mark_node)
18106 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18107 /*initialized=*/0, NULL);
18108 if (underlying_type == error_mark_node
18109 || check_for_bare_parameter_packs (underlying_type))
18110 underlying_type = NULL_TREE;
18114 /* Look for the `{' but don't consume it yet. */
18115 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18117 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18119 cp_parser_error (parser, "expected %<{%>");
18120 if (has_underlying_type)
18122 type = NULL_TREE;
18123 goto out;
18126 /* An opaque-enum-specifier must have a ';' here. */
18127 if ((scoped_enum_p || underlying_type)
18128 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18130 cp_parser_error (parser, "expected %<;%> or %<{%>");
18131 if (has_underlying_type)
18133 type = NULL_TREE;
18134 goto out;
18139 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18140 return NULL_TREE;
18142 if (nested_name_specifier)
18144 if (CLASS_TYPE_P (nested_name_specifier))
18146 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18147 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18148 push_scope (nested_name_specifier);
18150 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18152 push_nested_namespace (nested_name_specifier);
18156 /* Issue an error message if type-definitions are forbidden here. */
18157 if (!cp_parser_check_type_definition (parser))
18158 type = error_mark_node;
18159 else
18160 /* Create the new type. We do this before consuming the opening
18161 brace so the enum will be recorded as being on the line of its
18162 tag (or the 'enum' keyword, if there is no tag). */
18163 type = start_enum (identifier, type, underlying_type,
18164 attributes, scoped_enum_p, &is_new_type);
18166 /* If the next token is not '{' it is an opaque-enum-specifier or an
18167 elaborated-type-specifier. */
18168 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18170 timevar_push (TV_PARSE_ENUM);
18171 if (nested_name_specifier
18172 && nested_name_specifier != error_mark_node)
18174 /* The following catches invalid code such as:
18175 enum class S<int>::E { A, B, C }; */
18176 if (!processing_specialization
18177 && CLASS_TYPE_P (nested_name_specifier)
18178 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18179 error_at (type_start_token->location, "cannot add an enumerator "
18180 "list to a template instantiation");
18182 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18184 error_at (type_start_token->location,
18185 "%<%T::%E%> has not been declared",
18186 TYPE_CONTEXT (nested_name_specifier),
18187 nested_name_specifier);
18188 type = error_mark_node;
18190 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18191 && !CLASS_TYPE_P (nested_name_specifier))
18193 error_at (type_start_token->location, "nested name specifier "
18194 "%qT for enum declaration does not name a class "
18195 "or namespace", nested_name_specifier);
18196 type = error_mark_node;
18198 /* If that scope does not contain the scope in which the
18199 class was originally declared, the program is invalid. */
18200 else if (prev_scope && !is_ancestor (prev_scope,
18201 nested_name_specifier))
18203 if (at_namespace_scope_p ())
18204 error_at (type_start_token->location,
18205 "declaration of %qD in namespace %qD which does not "
18206 "enclose %qD",
18207 type, prev_scope, nested_name_specifier);
18208 else
18209 error_at (type_start_token->location,
18210 "declaration of %qD in %qD which does not "
18211 "enclose %qD",
18212 type, prev_scope, nested_name_specifier);
18213 type = error_mark_node;
18215 /* If that scope is the scope where the declaration is being placed
18216 the program is invalid. */
18217 else if (CLASS_TYPE_P (nested_name_specifier)
18218 && CLASS_TYPE_P (prev_scope)
18219 && same_type_p (nested_name_specifier, prev_scope))
18221 permerror (type_start_token->location,
18222 "extra qualification not allowed");
18223 nested_name_specifier = NULL_TREE;
18227 if (scoped_enum_p)
18228 begin_scope (sk_scoped_enum, type);
18230 /* Consume the opening brace. */
18231 matching_braces braces;
18232 braces.consume_open (parser);
18234 if (type == error_mark_node)
18235 ; /* Nothing to add */
18236 else if (OPAQUE_ENUM_P (type)
18237 || (cxx_dialect > cxx98 && processing_specialization))
18239 new_value_list = true;
18240 SET_OPAQUE_ENUM_P (type, false);
18241 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18243 else
18245 error_at (type_start_token->location,
18246 "multiple definition of %q#T", type);
18247 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18248 "previous definition here");
18249 type = error_mark_node;
18252 if (type == error_mark_node)
18253 cp_parser_skip_to_end_of_block_or_statement (parser);
18254 /* If the next token is not '}', then there are some enumerators. */
18255 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18257 if (is_unnamed && !scoped_enum_p)
18258 pedwarn (type_start_token->location, OPT_Wpedantic,
18259 "ISO C++ forbids empty unnamed enum");
18261 else
18262 cp_parser_enumerator_list (parser, type);
18264 /* Consume the final '}'. */
18265 braces.require_close (parser);
18267 if (scoped_enum_p)
18268 finish_scope ();
18269 timevar_pop (TV_PARSE_ENUM);
18271 else
18273 /* If a ';' follows, then it is an opaque-enum-specifier
18274 and additional restrictions apply. */
18275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18277 if (is_unnamed)
18278 error_at (type_start_token->location,
18279 "opaque-enum-specifier without name");
18280 else if (nested_name_specifier)
18281 error_at (type_start_token->location,
18282 "opaque-enum-specifier must use a simple identifier");
18286 /* Look for trailing attributes to apply to this enumeration, and
18287 apply them if appropriate. */
18288 if (cp_parser_allow_gnu_extensions_p (parser))
18290 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18291 cplus_decl_attributes (&type,
18292 trailing_attr,
18293 (int) ATTR_FLAG_TYPE_IN_PLACE);
18296 /* Finish up the enumeration. */
18297 if (type != error_mark_node)
18299 if (new_value_list)
18300 finish_enum_value_list (type);
18301 if (is_new_type)
18302 finish_enum (type);
18305 if (nested_name_specifier)
18307 if (CLASS_TYPE_P (nested_name_specifier))
18309 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18310 pop_scope (nested_name_specifier);
18312 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18314 pop_nested_namespace (nested_name_specifier);
18317 out:
18318 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18319 return type;
18322 /* Parse an enumerator-list. The enumerators all have the indicated
18323 TYPE.
18325 enumerator-list:
18326 enumerator-definition
18327 enumerator-list , enumerator-definition */
18329 static void
18330 cp_parser_enumerator_list (cp_parser* parser, tree type)
18332 while (true)
18334 /* Parse an enumerator-definition. */
18335 cp_parser_enumerator_definition (parser, type);
18337 /* If the next token is not a ',', we've reached the end of
18338 the list. */
18339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18340 break;
18341 /* Otherwise, consume the `,' and keep going. */
18342 cp_lexer_consume_token (parser->lexer);
18343 /* If the next token is a `}', there is a trailing comma. */
18344 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18346 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18347 pedwarn (input_location, OPT_Wpedantic,
18348 "comma at end of enumerator list");
18349 break;
18354 /* Parse an enumerator-definition. The enumerator has the indicated
18355 TYPE.
18357 enumerator-definition:
18358 enumerator
18359 enumerator = constant-expression
18361 enumerator:
18362 identifier
18364 GNU Extensions:
18366 enumerator-definition:
18367 enumerator attributes [opt]
18368 enumerator attributes [opt] = constant-expression */
18370 static void
18371 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18373 tree identifier;
18374 tree value;
18375 location_t loc;
18377 /* Save the input location because we are interested in the location
18378 of the identifier and not the location of the explicit value. */
18379 loc = cp_lexer_peek_token (parser->lexer)->location;
18381 /* Look for the identifier. */
18382 identifier = cp_parser_identifier (parser);
18383 if (identifier == error_mark_node)
18384 return;
18386 /* Parse any specified attributes. */
18387 tree attrs = cp_parser_attributes_opt (parser);
18389 /* If the next token is an '=', then there is an explicit value. */
18390 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18392 /* Consume the `=' token. */
18393 cp_lexer_consume_token (parser->lexer);
18394 /* Parse the value. */
18395 value = cp_parser_constant_expression (parser);
18397 else
18398 value = NULL_TREE;
18400 /* If we are processing a template, make sure the initializer of the
18401 enumerator doesn't contain any bare template parameter pack. */
18402 if (check_for_bare_parameter_packs (value))
18403 value = error_mark_node;
18405 /* Create the enumerator. */
18406 build_enumerator (identifier, value, type, attrs, loc);
18409 /* Parse a namespace-name.
18411 namespace-name:
18412 original-namespace-name
18413 namespace-alias
18415 Returns the NAMESPACE_DECL for the namespace. */
18417 static tree
18418 cp_parser_namespace_name (cp_parser* parser)
18420 tree identifier;
18421 tree namespace_decl;
18423 cp_token *token = cp_lexer_peek_token (parser->lexer);
18425 /* Get the name of the namespace. */
18426 identifier = cp_parser_identifier (parser);
18427 if (identifier == error_mark_node)
18428 return error_mark_node;
18430 /* Look up the identifier in the currently active scope. Look only
18431 for namespaces, due to:
18433 [basic.lookup.udir]
18435 When looking up a namespace-name in a using-directive or alias
18436 definition, only namespace names are considered.
18438 And:
18440 [basic.lookup.qual]
18442 During the lookup of a name preceding the :: scope resolution
18443 operator, object, function, and enumerator names are ignored.
18445 (Note that cp_parser_qualifying_entity only calls this
18446 function if the token after the name is the scope resolution
18447 operator.) */
18448 namespace_decl = cp_parser_lookup_name (parser, identifier,
18449 none_type,
18450 /*is_template=*/false,
18451 /*is_namespace=*/true,
18452 /*check_dependency=*/true,
18453 /*ambiguous_decls=*/NULL,
18454 token->location);
18455 /* If it's not a namespace, issue an error. */
18456 if (namespace_decl == error_mark_node
18457 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18459 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18461 error_at (token->location, "%qD is not a namespace-name", identifier);
18462 if (namespace_decl == error_mark_node
18463 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18464 suggest_alternative_in_explicit_scope (token->location, identifier,
18465 parser->scope);
18467 cp_parser_error (parser, "expected namespace-name");
18468 namespace_decl = error_mark_node;
18471 return namespace_decl;
18474 /* Parse a namespace-definition.
18476 namespace-definition:
18477 named-namespace-definition
18478 unnamed-namespace-definition
18480 named-namespace-definition:
18481 original-namespace-definition
18482 extension-namespace-definition
18484 original-namespace-definition:
18485 namespace identifier { namespace-body }
18487 extension-namespace-definition:
18488 namespace original-namespace-name { namespace-body }
18490 unnamed-namespace-definition:
18491 namespace { namespace-body } */
18493 static void
18494 cp_parser_namespace_definition (cp_parser* parser)
18496 tree identifier;
18497 int nested_definition_count = 0;
18499 cp_ensure_no_omp_declare_simd (parser);
18500 cp_ensure_no_oacc_routine (parser);
18502 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18504 if (is_inline)
18506 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18507 cp_lexer_consume_token (parser->lexer);
18510 /* Look for the `namespace' keyword. */
18511 cp_token* token
18512 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18514 /* Parse any specified attributes before the identifier. */
18515 tree attribs = cp_parser_attributes_opt (parser);
18517 for (;;)
18519 identifier = NULL_TREE;
18521 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18523 identifier = cp_parser_identifier (parser);
18525 /* Parse any attributes specified after the identifier. */
18526 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18530 break;
18532 if (!nested_definition_count && cxx_dialect < cxx17)
18533 pedwarn (input_location, OPT_Wpedantic,
18534 "nested namespace definitions only available with "
18535 "-std=c++17 or -std=gnu++17");
18537 /* Nested namespace names can create new namespaces (unlike
18538 other qualified-ids). */
18539 if (int count = identifier ? push_namespace (identifier) : 0)
18540 nested_definition_count += count;
18541 else
18542 cp_parser_error (parser, "nested namespace name required");
18543 cp_lexer_consume_token (parser->lexer);
18546 if (nested_definition_count && !identifier)
18547 cp_parser_error (parser, "namespace name required");
18549 if (nested_definition_count && attribs)
18550 error_at (token->location,
18551 "a nested namespace definition cannot have attributes");
18552 if (nested_definition_count && is_inline)
18553 error_at (token->location,
18554 "a nested namespace definition cannot be inline");
18556 /* Start the namespace. */
18557 nested_definition_count += push_namespace (identifier, is_inline);
18559 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18561 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18563 /* Look for the `{' to validate starting the namespace. */
18564 matching_braces braces;
18565 if (braces.require_open (parser))
18567 /* Parse the body of the namespace. */
18568 cp_parser_namespace_body (parser);
18570 /* Look for the final `}'. */
18571 braces.require_close (parser);
18574 if (has_visibility)
18575 pop_visibility (1);
18577 /* Pop the nested namespace definitions. */
18578 while (nested_definition_count--)
18579 pop_namespace ();
18582 /* Parse a namespace-body.
18584 namespace-body:
18585 declaration-seq [opt] */
18587 static void
18588 cp_parser_namespace_body (cp_parser* parser)
18590 cp_parser_declaration_seq_opt (parser);
18593 /* Parse a namespace-alias-definition.
18595 namespace-alias-definition:
18596 namespace identifier = qualified-namespace-specifier ; */
18598 static void
18599 cp_parser_namespace_alias_definition (cp_parser* parser)
18601 tree identifier;
18602 tree namespace_specifier;
18604 cp_token *token = cp_lexer_peek_token (parser->lexer);
18606 /* Look for the `namespace' keyword. */
18607 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18608 /* Look for the identifier. */
18609 identifier = cp_parser_identifier (parser);
18610 if (identifier == error_mark_node)
18611 return;
18612 /* Look for the `=' token. */
18613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18614 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18616 error_at (token->location, "%<namespace%> definition is not allowed here");
18617 /* Skip the definition. */
18618 cp_lexer_consume_token (parser->lexer);
18619 if (cp_parser_skip_to_closing_brace (parser))
18620 cp_lexer_consume_token (parser->lexer);
18621 return;
18623 cp_parser_require (parser, CPP_EQ, RT_EQ);
18624 /* Look for the qualified-namespace-specifier. */
18625 namespace_specifier
18626 = cp_parser_qualified_namespace_specifier (parser);
18627 /* Look for the `;' token. */
18628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18630 /* Register the alias in the symbol table. */
18631 do_namespace_alias (identifier, namespace_specifier);
18634 /* Parse a qualified-namespace-specifier.
18636 qualified-namespace-specifier:
18637 :: [opt] nested-name-specifier [opt] namespace-name
18639 Returns a NAMESPACE_DECL corresponding to the specified
18640 namespace. */
18642 static tree
18643 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18645 /* Look for the optional `::'. */
18646 cp_parser_global_scope_opt (parser,
18647 /*current_scope_valid_p=*/false);
18649 /* Look for the optional nested-name-specifier. */
18650 cp_parser_nested_name_specifier_opt (parser,
18651 /*typename_keyword_p=*/false,
18652 /*check_dependency_p=*/true,
18653 /*type_p=*/false,
18654 /*is_declaration=*/true);
18656 return cp_parser_namespace_name (parser);
18659 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18660 access declaration.
18662 using-declaration:
18663 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18664 using :: unqualified-id ;
18666 access-declaration:
18667 qualified-id ;
18671 static bool
18672 cp_parser_using_declaration (cp_parser* parser,
18673 bool access_declaration_p)
18675 cp_token *token;
18676 bool typename_p = false;
18677 bool global_scope_p;
18678 tree decl;
18679 tree identifier;
18680 tree qscope;
18681 int oldcount = errorcount;
18682 cp_token *diag_token = NULL;
18684 if (access_declaration_p)
18686 diag_token = cp_lexer_peek_token (parser->lexer);
18687 cp_parser_parse_tentatively (parser);
18689 else
18691 /* Look for the `using' keyword. */
18692 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18694 again:
18695 /* Peek at the next token. */
18696 token = cp_lexer_peek_token (parser->lexer);
18697 /* See if it's `typename'. */
18698 if (token->keyword == RID_TYPENAME)
18700 /* Remember that we've seen it. */
18701 typename_p = true;
18702 /* Consume the `typename' token. */
18703 cp_lexer_consume_token (parser->lexer);
18707 /* Look for the optional global scope qualification. */
18708 global_scope_p
18709 = (cp_parser_global_scope_opt (parser,
18710 /*current_scope_valid_p=*/false)
18711 != NULL_TREE);
18713 /* If we saw `typename', or didn't see `::', then there must be a
18714 nested-name-specifier present. */
18715 if (typename_p || !global_scope_p)
18717 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18718 /*check_dependency_p=*/true,
18719 /*type_p=*/false,
18720 /*is_declaration=*/true);
18721 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18723 cp_parser_skip_to_end_of_block_or_statement (parser);
18724 return false;
18727 /* Otherwise, we could be in either of the two productions. In that
18728 case, treat the nested-name-specifier as optional. */
18729 else
18730 qscope = cp_parser_nested_name_specifier_opt (parser,
18731 /*typename_keyword_p=*/false,
18732 /*check_dependency_p=*/true,
18733 /*type_p=*/false,
18734 /*is_declaration=*/true);
18735 if (!qscope)
18736 qscope = global_namespace;
18737 else if (UNSCOPED_ENUM_P (qscope))
18738 qscope = CP_TYPE_CONTEXT (qscope);
18740 if (access_declaration_p && cp_parser_error_occurred (parser))
18741 /* Something has already gone wrong; there's no need to parse
18742 further. Since an error has occurred, the return value of
18743 cp_parser_parse_definitely will be false, as required. */
18744 return cp_parser_parse_definitely (parser);
18746 token = cp_lexer_peek_token (parser->lexer);
18747 /* Parse the unqualified-id. */
18748 identifier = cp_parser_unqualified_id (parser,
18749 /*template_keyword_p=*/false,
18750 /*check_dependency_p=*/true,
18751 /*declarator_p=*/true,
18752 /*optional_p=*/false);
18754 if (access_declaration_p)
18756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18757 cp_parser_simulate_error (parser);
18758 if (!cp_parser_parse_definitely (parser))
18759 return false;
18761 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18763 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18764 if (cxx_dialect < cxx17
18765 && !in_system_header_at (ell->location))
18766 pedwarn (ell->location, 0,
18767 "pack expansion in using-declaration only available "
18768 "with -std=c++17 or -std=gnu++17");
18769 qscope = make_pack_expansion (qscope);
18772 /* The function we call to handle a using-declaration is different
18773 depending on what scope we are in. */
18774 if (qscope == error_mark_node || identifier == error_mark_node)
18776 else if (!identifier_p (identifier)
18777 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18778 /* [namespace.udecl]
18780 A using declaration shall not name a template-id. */
18781 error_at (token->location,
18782 "a template-id may not appear in a using-declaration");
18783 else
18785 if (at_class_scope_p ())
18787 /* Create the USING_DECL. */
18788 decl = do_class_using_decl (qscope, identifier);
18790 if (decl && typename_p)
18791 USING_DECL_TYPENAME_P (decl) = 1;
18793 if (check_for_bare_parameter_packs (decl))
18795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18796 return false;
18798 else
18799 /* Add it to the list of members in this class. */
18800 finish_member_declaration (decl);
18802 else
18804 decl = cp_parser_lookup_name_simple (parser,
18805 identifier,
18806 token->location);
18807 if (decl == error_mark_node)
18808 cp_parser_name_lookup_error (parser, identifier,
18809 decl, NLE_NULL,
18810 token->location);
18811 else if (check_for_bare_parameter_packs (decl))
18813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18814 return false;
18816 else if (!at_namespace_scope_p ())
18817 finish_local_using_decl (decl, qscope, identifier);
18818 else
18819 finish_namespace_using_decl (decl, qscope, identifier);
18823 if (!access_declaration_p
18824 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18826 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18827 if (cxx_dialect < cxx17)
18828 pedwarn (comma->location, 0,
18829 "comma-separated list in using-declaration only available "
18830 "with -std=c++17 or -std=gnu++17");
18831 goto again;
18834 /* Look for the final `;'. */
18835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18837 if (access_declaration_p && errorcount == oldcount)
18838 warning_at (diag_token->location, OPT_Wdeprecated,
18839 "access declarations are deprecated "
18840 "in favour of using-declarations; "
18841 "suggestion: add the %<using%> keyword");
18843 return true;
18846 /* Parse an alias-declaration.
18848 alias-declaration:
18849 using identifier attribute-specifier-seq [opt] = type-id */
18851 static tree
18852 cp_parser_alias_declaration (cp_parser* parser)
18854 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18855 location_t id_location;
18856 cp_declarator *declarator;
18857 cp_decl_specifier_seq decl_specs;
18858 bool member_p;
18859 const char *saved_message = NULL;
18861 /* Look for the `using' keyword. */
18862 cp_token *using_token
18863 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18864 if (using_token == NULL)
18865 return error_mark_node;
18867 id_location = cp_lexer_peek_token (parser->lexer)->location;
18868 id = cp_parser_identifier (parser);
18869 if (id == error_mark_node)
18870 return error_mark_node;
18872 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18873 attributes = cp_parser_attributes_opt (parser);
18874 if (attributes == error_mark_node)
18875 return error_mark_node;
18877 cp_parser_require (parser, CPP_EQ, RT_EQ);
18879 if (cp_parser_error_occurred (parser))
18880 return error_mark_node;
18882 cp_parser_commit_to_tentative_parse (parser);
18884 /* Now we are going to parse the type-id of the declaration. */
18887 [dcl.type]/3 says:
18889 "A type-specifier-seq shall not define a class or enumeration
18890 unless it appears in the type-id of an alias-declaration (7.1.3) that
18891 is not the declaration of a template-declaration."
18893 In other words, if we currently are in an alias template, the
18894 type-id should not define a type.
18896 So let's set parser->type_definition_forbidden_message in that
18897 case; cp_parser_check_type_definition (called by
18898 cp_parser_class_specifier) will then emit an error if a type is
18899 defined in the type-id. */
18900 if (parser->num_template_parameter_lists)
18902 saved_message = parser->type_definition_forbidden_message;
18903 parser->type_definition_forbidden_message =
18904 G_("types may not be defined in alias template declarations");
18907 type = cp_parser_type_id (parser);
18909 /* Restore the error message if need be. */
18910 if (parser->num_template_parameter_lists)
18911 parser->type_definition_forbidden_message = saved_message;
18913 if (type == error_mark_node
18914 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18916 cp_parser_skip_to_end_of_block_or_statement (parser);
18917 return error_mark_node;
18920 /* A typedef-name can also be introduced by an alias-declaration. The
18921 identifier following the using keyword becomes a typedef-name. It has
18922 the same semantics as if it were introduced by the typedef
18923 specifier. In particular, it does not define a new type and it shall
18924 not appear in the type-id. */
18926 clear_decl_specs (&decl_specs);
18927 decl_specs.type = type;
18928 if (attributes != NULL_TREE)
18930 decl_specs.attributes = attributes;
18931 set_and_check_decl_spec_loc (&decl_specs,
18932 ds_attribute,
18933 attrs_token);
18935 set_and_check_decl_spec_loc (&decl_specs,
18936 ds_typedef,
18937 using_token);
18938 set_and_check_decl_spec_loc (&decl_specs,
18939 ds_alias,
18940 using_token);
18942 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18943 declarator->id_loc = id_location;
18945 member_p = at_class_scope_p ();
18946 if (member_p)
18947 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18948 NULL_TREE, attributes);
18949 else
18950 decl = start_decl (declarator, &decl_specs, 0,
18951 attributes, NULL_TREE, &pushed_scope);
18952 if (decl == error_mark_node)
18953 return decl;
18955 // Attach constraints to the alias declaration.
18956 if (flag_concepts && current_template_parms)
18958 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18959 tree constr = build_constraints (reqs, NULL_TREE);
18960 set_constraints (decl, constr);
18963 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18965 if (pushed_scope)
18966 pop_scope (pushed_scope);
18968 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18969 added into the symbol table; otherwise, return the TYPE_DECL. */
18970 if (DECL_LANG_SPECIFIC (decl)
18971 && DECL_TEMPLATE_INFO (decl)
18972 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18974 decl = DECL_TI_TEMPLATE (decl);
18975 if (member_p)
18976 check_member_template (decl);
18979 return decl;
18982 /* Parse a using-directive.
18984 using-directive:
18985 using namespace :: [opt] nested-name-specifier [opt]
18986 namespace-name ; */
18988 static void
18989 cp_parser_using_directive (cp_parser* parser)
18991 tree namespace_decl;
18992 tree attribs;
18994 /* Look for the `using' keyword. */
18995 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18996 /* And the `namespace' keyword. */
18997 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18998 /* Look for the optional `::' operator. */
18999 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19000 /* And the optional nested-name-specifier. */
19001 cp_parser_nested_name_specifier_opt (parser,
19002 /*typename_keyword_p=*/false,
19003 /*check_dependency_p=*/true,
19004 /*type_p=*/false,
19005 /*is_declaration=*/true);
19006 /* Get the namespace being used. */
19007 namespace_decl = cp_parser_namespace_name (parser);
19008 /* And any specified attributes. */
19009 attribs = cp_parser_attributes_opt (parser);
19011 /* Update the symbol table. */
19012 if (namespace_bindings_p ())
19013 finish_namespace_using_directive (namespace_decl, attribs);
19014 else
19015 finish_local_using_directive (namespace_decl, attribs);
19017 /* Look for the final `;'. */
19018 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19021 /* Parse an asm-definition.
19023 asm-definition:
19024 asm ( string-literal ) ;
19026 GNU Extension:
19028 asm-definition:
19029 asm volatile [opt] ( string-literal ) ;
19030 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19031 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19032 : asm-operand-list [opt] ) ;
19033 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19034 : asm-operand-list [opt]
19035 : asm-clobber-list [opt] ) ;
19036 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19037 : asm-clobber-list [opt]
19038 : asm-goto-list ) ; */
19040 static void
19041 cp_parser_asm_definition (cp_parser* parser)
19043 tree string;
19044 tree outputs = NULL_TREE;
19045 tree inputs = NULL_TREE;
19046 tree clobbers = NULL_TREE;
19047 tree labels = NULL_TREE;
19048 tree asm_stmt;
19049 bool volatile_p = false;
19050 bool extended_p = false;
19051 bool invalid_inputs_p = false;
19052 bool invalid_outputs_p = false;
19053 bool goto_p = false;
19054 required_token missing = RT_NONE;
19056 /* Look for the `asm' keyword. */
19057 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19059 if (parser->in_function_body
19060 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19062 error ("%<asm%> in %<constexpr%> function");
19063 cp_function_chain->invalid_constexpr = true;
19066 /* See if the next token is `volatile'. */
19067 if (cp_parser_allow_gnu_extensions_p (parser)
19068 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19070 /* Remember that we saw the `volatile' keyword. */
19071 volatile_p = true;
19072 /* Consume the token. */
19073 cp_lexer_consume_token (parser->lexer);
19075 if (cp_parser_allow_gnu_extensions_p (parser)
19076 && parser->in_function_body
19077 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19079 /* Remember that we saw the `goto' keyword. */
19080 goto_p = true;
19081 /* Consume the token. */
19082 cp_lexer_consume_token (parser->lexer);
19084 /* Look for the opening `('. */
19085 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19086 return;
19087 /* Look for the string. */
19088 string = cp_parser_string_literal (parser, false, false);
19089 if (string == error_mark_node)
19091 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19092 /*consume_paren=*/true);
19093 return;
19096 /* If we're allowing GNU extensions, check for the extended assembly
19097 syntax. Unfortunately, the `:' tokens need not be separated by
19098 a space in C, and so, for compatibility, we tolerate that here
19099 too. Doing that means that we have to treat the `::' operator as
19100 two `:' tokens. */
19101 if (cp_parser_allow_gnu_extensions_p (parser)
19102 && parser->in_function_body
19103 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19104 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19106 bool inputs_p = false;
19107 bool clobbers_p = false;
19108 bool labels_p = false;
19110 /* The extended syntax was used. */
19111 extended_p = true;
19113 /* Look for outputs. */
19114 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19116 /* Consume the `:'. */
19117 cp_lexer_consume_token (parser->lexer);
19118 /* Parse the output-operands. */
19119 if (cp_lexer_next_token_is_not (parser->lexer,
19120 CPP_COLON)
19121 && cp_lexer_next_token_is_not (parser->lexer,
19122 CPP_SCOPE)
19123 && cp_lexer_next_token_is_not (parser->lexer,
19124 CPP_CLOSE_PAREN)
19125 && !goto_p)
19127 outputs = cp_parser_asm_operand_list (parser);
19128 if (outputs == error_mark_node)
19129 invalid_outputs_p = true;
19132 /* If the next token is `::', there are no outputs, and the
19133 next token is the beginning of the inputs. */
19134 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19135 /* The inputs are coming next. */
19136 inputs_p = true;
19138 /* Look for inputs. */
19139 if (inputs_p
19140 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19142 /* Consume the `:' or `::'. */
19143 cp_lexer_consume_token (parser->lexer);
19144 /* Parse the output-operands. */
19145 if (cp_lexer_next_token_is_not (parser->lexer,
19146 CPP_COLON)
19147 && cp_lexer_next_token_is_not (parser->lexer,
19148 CPP_SCOPE)
19149 && cp_lexer_next_token_is_not (parser->lexer,
19150 CPP_CLOSE_PAREN))
19152 inputs = cp_parser_asm_operand_list (parser);
19153 if (inputs == error_mark_node)
19154 invalid_inputs_p = true;
19157 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19158 /* The clobbers are coming next. */
19159 clobbers_p = true;
19161 /* Look for clobbers. */
19162 if (clobbers_p
19163 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19165 clobbers_p = true;
19166 /* Consume the `:' or `::'. */
19167 cp_lexer_consume_token (parser->lexer);
19168 /* Parse the clobbers. */
19169 if (cp_lexer_next_token_is_not (parser->lexer,
19170 CPP_COLON)
19171 && cp_lexer_next_token_is_not (parser->lexer,
19172 CPP_CLOSE_PAREN))
19173 clobbers = cp_parser_asm_clobber_list (parser);
19175 else if (goto_p
19176 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19177 /* The labels are coming next. */
19178 labels_p = true;
19180 /* Look for labels. */
19181 if (labels_p
19182 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19184 labels_p = true;
19185 /* Consume the `:' or `::'. */
19186 cp_lexer_consume_token (parser->lexer);
19187 /* Parse the labels. */
19188 labels = cp_parser_asm_label_list (parser);
19191 if (goto_p && !labels_p)
19192 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19194 else if (goto_p)
19195 missing = RT_COLON_SCOPE;
19197 /* Look for the closing `)'. */
19198 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19199 missing ? missing : RT_CLOSE_PAREN))
19200 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19201 /*consume_paren=*/true);
19202 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19204 if (!invalid_inputs_p && !invalid_outputs_p)
19206 /* Create the ASM_EXPR. */
19207 if (parser->in_function_body)
19209 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19210 inputs, clobbers, labels);
19211 /* If the extended syntax was not used, mark the ASM_EXPR. */
19212 if (!extended_p)
19214 tree temp = asm_stmt;
19215 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19216 temp = TREE_OPERAND (temp, 0);
19218 ASM_INPUT_P (temp) = 1;
19221 else
19222 symtab->finalize_toplevel_asm (string);
19226 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19227 type that comes from the decl-specifier-seq. */
19229 static tree
19230 strip_declarator_types (tree type, cp_declarator *declarator)
19232 for (cp_declarator *d = declarator; d;)
19233 switch (d->kind)
19235 case cdk_id:
19236 case cdk_decomp:
19237 case cdk_error:
19238 d = NULL;
19239 break;
19241 default:
19242 if (TYPE_PTRMEMFUNC_P (type))
19243 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19244 type = TREE_TYPE (type);
19245 d = d->declarator;
19246 break;
19249 return type;
19252 /* Declarators [gram.dcl.decl] */
19254 /* Parse an init-declarator.
19256 init-declarator:
19257 declarator initializer [opt]
19259 GNU Extension:
19261 init-declarator:
19262 declarator asm-specification [opt] attributes [opt] initializer [opt]
19264 function-definition:
19265 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19266 function-body
19267 decl-specifier-seq [opt] declarator function-try-block
19269 GNU Extension:
19271 function-definition:
19272 __extension__ function-definition
19274 TM Extension:
19276 function-definition:
19277 decl-specifier-seq [opt] declarator function-transaction-block
19279 The DECL_SPECIFIERS apply to this declarator. Returns a
19280 representation of the entity declared. If MEMBER_P is TRUE, then
19281 this declarator appears in a class scope. The new DECL created by
19282 this declarator is returned.
19284 The CHECKS are access checks that should be performed once we know
19285 what entity is being declared (and, therefore, what classes have
19286 befriended it).
19288 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19289 for a function-definition here as well. If the declarator is a
19290 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19291 be TRUE upon return. By that point, the function-definition will
19292 have been completely parsed.
19294 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19295 is FALSE.
19297 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19298 parsed declaration if it is an uninitialized single declarator not followed
19299 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19300 if present, will not be consumed. If returned, this declarator will be
19301 created with SD_INITIALIZED but will not call cp_finish_decl.
19303 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19304 and there is an initializer, the pointed location_t is set to the
19305 location of the '=' or `(', or '{' in C++11 token introducing the
19306 initializer. */
19308 static tree
19309 cp_parser_init_declarator (cp_parser* parser,
19310 cp_decl_specifier_seq *decl_specifiers,
19311 vec<deferred_access_check, va_gc> *checks,
19312 bool function_definition_allowed_p,
19313 bool member_p,
19314 int declares_class_or_enum,
19315 bool* function_definition_p,
19316 tree* maybe_range_for_decl,
19317 location_t* init_loc,
19318 tree* auto_result)
19320 cp_token *token = NULL, *asm_spec_start_token = NULL,
19321 *attributes_start_token = NULL;
19322 cp_declarator *declarator;
19323 tree prefix_attributes;
19324 tree attributes = NULL;
19325 tree asm_specification;
19326 tree initializer;
19327 tree decl = NULL_TREE;
19328 tree scope;
19329 int is_initialized;
19330 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19331 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19332 "(...)". */
19333 enum cpp_ttype initialization_kind;
19334 bool is_direct_init = false;
19335 bool is_non_constant_init;
19336 int ctor_dtor_or_conv_p;
19337 bool friend_p = cp_parser_friend_p (decl_specifiers);
19338 tree pushed_scope = NULL_TREE;
19339 bool range_for_decl_p = false;
19340 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19341 location_t tmp_init_loc = UNKNOWN_LOCATION;
19343 /* Gather the attributes that were provided with the
19344 decl-specifiers. */
19345 prefix_attributes = decl_specifiers->attributes;
19347 /* Assume that this is not the declarator for a function
19348 definition. */
19349 if (function_definition_p)
19350 *function_definition_p = false;
19352 /* Default arguments are only permitted for function parameters. */
19353 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19354 parser->default_arg_ok_p = false;
19356 /* Defer access checks while parsing the declarator; we cannot know
19357 what names are accessible until we know what is being
19358 declared. */
19359 resume_deferring_access_checks ();
19361 token = cp_lexer_peek_token (parser->lexer);
19363 /* Parse the declarator. */
19364 declarator
19365 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19366 &ctor_dtor_or_conv_p,
19367 /*parenthesized_p=*/NULL,
19368 member_p, friend_p);
19369 /* Gather up the deferred checks. */
19370 stop_deferring_access_checks ();
19372 parser->default_arg_ok_p = saved_default_arg_ok_p;
19374 /* If the DECLARATOR was erroneous, there's no need to go
19375 further. */
19376 if (declarator == cp_error_declarator)
19377 return error_mark_node;
19379 /* Check that the number of template-parameter-lists is OK. */
19380 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19381 token->location))
19382 return error_mark_node;
19384 if (declares_class_or_enum & 2)
19385 cp_parser_check_for_definition_in_return_type (declarator,
19386 decl_specifiers->type,
19387 decl_specifiers->locations[ds_type_spec]);
19389 /* Figure out what scope the entity declared by the DECLARATOR is
19390 located in. `grokdeclarator' sometimes changes the scope, so
19391 we compute it now. */
19392 scope = get_scope_of_declarator (declarator);
19394 /* Perform any lookups in the declared type which were thought to be
19395 dependent, but are not in the scope of the declarator. */
19396 decl_specifiers->type
19397 = maybe_update_decl_type (decl_specifiers->type, scope);
19399 /* If we're allowing GNU extensions, look for an
19400 asm-specification. */
19401 if (cp_parser_allow_gnu_extensions_p (parser))
19403 /* Look for an asm-specification. */
19404 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19405 asm_specification = cp_parser_asm_specification_opt (parser);
19407 else
19408 asm_specification = NULL_TREE;
19410 /* Look for attributes. */
19411 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19412 attributes = cp_parser_attributes_opt (parser);
19414 /* Peek at the next token. */
19415 token = cp_lexer_peek_token (parser->lexer);
19417 bool bogus_implicit_tmpl = false;
19419 if (function_declarator_p (declarator))
19421 /* Handle C++17 deduction guides. */
19422 if (!decl_specifiers->type
19423 && ctor_dtor_or_conv_p <= 0
19424 && cxx_dialect >= cxx17)
19426 cp_declarator *id = get_id_declarator (declarator);
19427 tree name = id->u.id.unqualified_name;
19428 parser->scope = id->u.id.qualifying_scope;
19429 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19430 if (tmpl
19431 && (DECL_CLASS_TEMPLATE_P (tmpl)
19432 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19434 id->u.id.unqualified_name = dguide_name (tmpl);
19435 id->u.id.sfk = sfk_deduction_guide;
19436 ctor_dtor_or_conv_p = 1;
19440 /* Check to see if the token indicates the start of a
19441 function-definition. */
19442 if (cp_parser_token_starts_function_definition_p (token))
19444 if (!function_definition_allowed_p)
19446 /* If a function-definition should not appear here, issue an
19447 error message. */
19448 cp_parser_error (parser,
19449 "a function-definition is not allowed here");
19450 return error_mark_node;
19453 location_t func_brace_location
19454 = cp_lexer_peek_token (parser->lexer)->location;
19456 /* Neither attributes nor an asm-specification are allowed
19457 on a function-definition. */
19458 if (asm_specification)
19459 error_at (asm_spec_start_token->location,
19460 "an asm-specification is not allowed "
19461 "on a function-definition");
19462 if (attributes)
19463 error_at (attributes_start_token->location,
19464 "attributes are not allowed "
19465 "on a function-definition");
19466 /* This is a function-definition. */
19467 *function_definition_p = true;
19469 /* Parse the function definition. */
19470 if (member_p)
19471 decl = cp_parser_save_member_function_body (parser,
19472 decl_specifiers,
19473 declarator,
19474 prefix_attributes);
19475 else
19476 decl =
19477 (cp_parser_function_definition_from_specifiers_and_declarator
19478 (parser, decl_specifiers, prefix_attributes, declarator));
19480 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19482 /* This is where the prologue starts... */
19483 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19484 = func_brace_location;
19487 return decl;
19490 else if (parser->fully_implicit_function_template_p)
19492 /* A non-template declaration involving a function parameter list
19493 containing an implicit template parameter will be made into a
19494 template. If the resulting declaration is not going to be an
19495 actual function then finish the template scope here to prevent it.
19496 An error message will be issued once we have a decl to talk about.
19498 FIXME probably we should do type deduction rather than create an
19499 implicit template, but the standard currently doesn't allow it. */
19500 bogus_implicit_tmpl = true;
19501 finish_fully_implicit_template (parser, NULL_TREE);
19504 /* [dcl.dcl]
19506 Only in function declarations for constructors, destructors, type
19507 conversions, and deduction guides can the decl-specifier-seq be omitted.
19509 We explicitly postpone this check past the point where we handle
19510 function-definitions because we tolerate function-definitions
19511 that are missing their return types in some modes. */
19512 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19514 cp_parser_error (parser,
19515 "expected constructor, destructor, or type conversion");
19516 return error_mark_node;
19519 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19520 if (token->type == CPP_EQ
19521 || token->type == CPP_OPEN_PAREN
19522 || token->type == CPP_OPEN_BRACE)
19524 is_initialized = SD_INITIALIZED;
19525 initialization_kind = token->type;
19526 if (maybe_range_for_decl)
19527 *maybe_range_for_decl = error_mark_node;
19528 tmp_init_loc = token->location;
19529 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19530 *init_loc = tmp_init_loc;
19532 if (token->type == CPP_EQ
19533 && function_declarator_p (declarator))
19535 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19536 if (t2->keyword == RID_DEFAULT)
19537 is_initialized = SD_DEFAULTED;
19538 else if (t2->keyword == RID_DELETE)
19539 is_initialized = SD_DELETED;
19542 else
19544 /* If the init-declarator isn't initialized and isn't followed by a
19545 `,' or `;', it's not a valid init-declarator. */
19546 if (token->type != CPP_COMMA
19547 && token->type != CPP_SEMICOLON)
19549 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19550 range_for_decl_p = true;
19551 else
19553 if (!maybe_range_for_decl)
19554 cp_parser_error (parser, "expected initializer");
19555 return error_mark_node;
19558 is_initialized = SD_UNINITIALIZED;
19559 initialization_kind = CPP_EOF;
19562 /* Because start_decl has side-effects, we should only call it if we
19563 know we're going ahead. By this point, we know that we cannot
19564 possibly be looking at any other construct. */
19565 cp_parser_commit_to_tentative_parse (parser);
19567 /* Enter the newly declared entry in the symbol table. If we're
19568 processing a declaration in a class-specifier, we wait until
19569 after processing the initializer. */
19570 if (!member_p)
19572 if (parser->in_unbraced_linkage_specification_p)
19573 decl_specifiers->storage_class = sc_extern;
19574 decl = start_decl (declarator, decl_specifiers,
19575 range_for_decl_p? SD_INITIALIZED : is_initialized,
19576 attributes, prefix_attributes, &pushed_scope);
19577 cp_finalize_omp_declare_simd (parser, decl);
19578 cp_finalize_oacc_routine (parser, decl, false);
19579 /* Adjust location of decl if declarator->id_loc is more appropriate:
19580 set, and decl wasn't merged with another decl, in which case its
19581 location would be different from input_location, and more accurate. */
19582 if (DECL_P (decl)
19583 && declarator->id_loc != UNKNOWN_LOCATION
19584 && DECL_SOURCE_LOCATION (decl) == input_location)
19585 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19587 else if (scope)
19588 /* Enter the SCOPE. That way unqualified names appearing in the
19589 initializer will be looked up in SCOPE. */
19590 pushed_scope = push_scope (scope);
19592 /* Perform deferred access control checks, now that we know in which
19593 SCOPE the declared entity resides. */
19594 if (!member_p && decl)
19596 tree saved_current_function_decl = NULL_TREE;
19598 /* If the entity being declared is a function, pretend that we
19599 are in its scope. If it is a `friend', it may have access to
19600 things that would not otherwise be accessible. */
19601 if (TREE_CODE (decl) == FUNCTION_DECL)
19603 saved_current_function_decl = current_function_decl;
19604 current_function_decl = decl;
19607 /* Perform access checks for template parameters. */
19608 cp_parser_perform_template_parameter_access_checks (checks);
19610 /* Perform the access control checks for the declarator and the
19611 decl-specifiers. */
19612 perform_deferred_access_checks (tf_warning_or_error);
19614 /* Restore the saved value. */
19615 if (TREE_CODE (decl) == FUNCTION_DECL)
19616 current_function_decl = saved_current_function_decl;
19619 /* Parse the initializer. */
19620 initializer = NULL_TREE;
19621 is_direct_init = false;
19622 is_non_constant_init = true;
19623 if (is_initialized)
19625 if (function_declarator_p (declarator))
19627 if (initialization_kind == CPP_EQ)
19628 initializer = cp_parser_pure_specifier (parser);
19629 else
19631 /* If the declaration was erroneous, we don't really
19632 know what the user intended, so just silently
19633 consume the initializer. */
19634 if (decl != error_mark_node)
19635 error_at (tmp_init_loc, "initializer provided for function");
19636 cp_parser_skip_to_closing_parenthesis (parser,
19637 /*recovering=*/true,
19638 /*or_comma=*/false,
19639 /*consume_paren=*/true);
19642 else
19644 /* We want to record the extra mangling scope for in-class
19645 initializers of class members and initializers of static data
19646 member templates. The former involves deferring
19647 parsing of the initializer until end of class as with default
19648 arguments. So right here we only handle the latter. */
19649 if (!member_p && processing_template_decl)
19650 start_lambda_scope (decl);
19651 initializer = cp_parser_initializer (parser,
19652 &is_direct_init,
19653 &is_non_constant_init);
19654 if (!member_p && processing_template_decl)
19655 finish_lambda_scope ();
19656 if (initializer == error_mark_node)
19657 cp_parser_skip_to_end_of_statement (parser);
19661 /* The old parser allows attributes to appear after a parenthesized
19662 initializer. Mark Mitchell proposed removing this functionality
19663 on the GCC mailing lists on 2002-08-13. This parser accepts the
19664 attributes -- but ignores them. */
19665 if (cp_parser_allow_gnu_extensions_p (parser)
19666 && initialization_kind == CPP_OPEN_PAREN)
19667 if (cp_parser_attributes_opt (parser))
19668 warning (OPT_Wattributes,
19669 "attributes after parenthesized initializer ignored");
19671 /* And now complain about a non-function implicit template. */
19672 if (bogus_implicit_tmpl && decl != error_mark_node)
19673 error_at (DECL_SOURCE_LOCATION (decl),
19674 "non-function %qD declared as implicit template", decl);
19676 /* For an in-class declaration, use `grokfield' to create the
19677 declaration. */
19678 if (member_p)
19680 if (pushed_scope)
19682 pop_scope (pushed_scope);
19683 pushed_scope = NULL_TREE;
19685 decl = grokfield (declarator, decl_specifiers,
19686 initializer, !is_non_constant_init,
19687 /*asmspec=*/NULL_TREE,
19688 attr_chainon (attributes, prefix_attributes));
19689 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19690 cp_parser_save_default_args (parser, decl);
19691 cp_finalize_omp_declare_simd (parser, decl);
19692 cp_finalize_oacc_routine (parser, decl, false);
19695 /* Finish processing the declaration. But, skip member
19696 declarations. */
19697 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19699 cp_finish_decl (decl,
19700 initializer, !is_non_constant_init,
19701 asm_specification,
19702 /* If the initializer is in parentheses, then this is
19703 a direct-initialization, which means that an
19704 `explicit' constructor is OK. Otherwise, an
19705 `explicit' constructor cannot be used. */
19706 ((is_direct_init || !is_initialized)
19707 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19709 else if ((cxx_dialect != cxx98) && friend_p
19710 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19711 /* Core issue #226 (C++0x only): A default template-argument
19712 shall not be specified in a friend class template
19713 declaration. */
19714 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19715 /*is_partial=*/false, /*is_friend_decl=*/1);
19717 if (!friend_p && pushed_scope)
19718 pop_scope (pushed_scope);
19720 if (function_declarator_p (declarator)
19721 && parser->fully_implicit_function_template_p)
19723 if (member_p)
19724 decl = finish_fully_implicit_template (parser, decl);
19725 else
19726 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19729 if (auto_result && is_initialized && decl_specifiers->type
19730 && type_uses_auto (decl_specifiers->type))
19731 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19733 return decl;
19736 /* Parse a declarator.
19738 declarator:
19739 direct-declarator
19740 ptr-operator declarator
19742 abstract-declarator:
19743 ptr-operator abstract-declarator [opt]
19744 direct-abstract-declarator
19746 GNU Extensions:
19748 declarator:
19749 attributes [opt] direct-declarator
19750 attributes [opt] ptr-operator declarator
19752 abstract-declarator:
19753 attributes [opt] ptr-operator abstract-declarator [opt]
19754 attributes [opt] direct-abstract-declarator
19756 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19757 detect constructors, destructors, deduction guides, or conversion operators.
19758 It is set to -1 if the declarator is a name, and +1 if it is a
19759 function. Otherwise it is set to zero. Usually you just want to
19760 test for >0, but internally the negative value is used.
19762 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19763 a decl-specifier-seq unless it declares a constructor, destructor,
19764 or conversion. It might seem that we could check this condition in
19765 semantic analysis, rather than parsing, but that makes it difficult
19766 to handle something like `f()'. We want to notice that there are
19767 no decl-specifiers, and therefore realize that this is an
19768 expression, not a declaration.)
19770 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19771 the declarator is a direct-declarator of the form "(...)".
19773 MEMBER_P is true iff this declarator is a member-declarator.
19775 FRIEND_P is true iff this declarator is a friend. */
19777 static cp_declarator *
19778 cp_parser_declarator (cp_parser* parser,
19779 cp_parser_declarator_kind dcl_kind,
19780 int* ctor_dtor_or_conv_p,
19781 bool* parenthesized_p,
19782 bool member_p, bool friend_p)
19784 cp_declarator *declarator;
19785 enum tree_code code;
19786 cp_cv_quals cv_quals;
19787 tree class_type;
19788 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19790 /* Assume this is not a constructor, destructor, or type-conversion
19791 operator. */
19792 if (ctor_dtor_or_conv_p)
19793 *ctor_dtor_or_conv_p = 0;
19795 if (cp_parser_allow_gnu_extensions_p (parser))
19796 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19798 /* Check for the ptr-operator production. */
19799 cp_parser_parse_tentatively (parser);
19800 /* Parse the ptr-operator. */
19801 code = cp_parser_ptr_operator (parser,
19802 &class_type,
19803 &cv_quals,
19804 &std_attributes);
19806 /* If that worked, then we have a ptr-operator. */
19807 if (cp_parser_parse_definitely (parser))
19809 /* If a ptr-operator was found, then this declarator was not
19810 parenthesized. */
19811 if (parenthesized_p)
19812 *parenthesized_p = true;
19813 /* The dependent declarator is optional if we are parsing an
19814 abstract-declarator. */
19815 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19816 cp_parser_parse_tentatively (parser);
19818 /* Parse the dependent declarator. */
19819 declarator = cp_parser_declarator (parser, dcl_kind,
19820 /*ctor_dtor_or_conv_p=*/NULL,
19821 /*parenthesized_p=*/NULL,
19822 /*member_p=*/false,
19823 friend_p);
19825 /* If we are parsing an abstract-declarator, we must handle the
19826 case where the dependent declarator is absent. */
19827 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19828 && !cp_parser_parse_definitely (parser))
19829 declarator = NULL;
19831 declarator = cp_parser_make_indirect_declarator
19832 (code, class_type, cv_quals, declarator, std_attributes);
19834 /* Everything else is a direct-declarator. */
19835 else
19837 if (parenthesized_p)
19838 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19839 CPP_OPEN_PAREN);
19840 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19841 ctor_dtor_or_conv_p,
19842 member_p, friend_p);
19845 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19846 declarator->attributes = gnu_attributes;
19847 return declarator;
19850 /* Parse a direct-declarator or direct-abstract-declarator.
19852 direct-declarator:
19853 declarator-id
19854 direct-declarator ( parameter-declaration-clause )
19855 cv-qualifier-seq [opt]
19856 ref-qualifier [opt]
19857 exception-specification [opt]
19858 direct-declarator [ constant-expression [opt] ]
19859 ( declarator )
19861 direct-abstract-declarator:
19862 direct-abstract-declarator [opt]
19863 ( parameter-declaration-clause )
19864 cv-qualifier-seq [opt]
19865 ref-qualifier [opt]
19866 exception-specification [opt]
19867 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19868 ( abstract-declarator )
19870 Returns a representation of the declarator. DCL_KIND is
19871 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19872 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19873 we are parsing a direct-declarator. It is
19874 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19875 of ambiguity we prefer an abstract declarator, as per
19876 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19877 as for cp_parser_declarator. */
19879 static cp_declarator *
19880 cp_parser_direct_declarator (cp_parser* parser,
19881 cp_parser_declarator_kind dcl_kind,
19882 int* ctor_dtor_or_conv_p,
19883 bool member_p, bool friend_p)
19885 cp_token *token;
19886 cp_declarator *declarator = NULL;
19887 tree scope = NULL_TREE;
19888 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19889 bool saved_in_declarator_p = parser->in_declarator_p;
19890 bool first = true;
19891 tree pushed_scope = NULL_TREE;
19892 cp_token *open_paren = NULL, *close_paren = NULL;
19894 while (true)
19896 /* Peek at the next token. */
19897 token = cp_lexer_peek_token (parser->lexer);
19898 if (token->type == CPP_OPEN_PAREN)
19900 /* This is either a parameter-declaration-clause, or a
19901 parenthesized declarator. When we know we are parsing a
19902 named declarator, it must be a parenthesized declarator
19903 if FIRST is true. For instance, `(int)' is a
19904 parameter-declaration-clause, with an omitted
19905 direct-abstract-declarator. But `((*))', is a
19906 parenthesized abstract declarator. Finally, when T is a
19907 template parameter `(T)' is a
19908 parameter-declaration-clause, and not a parenthesized
19909 named declarator.
19911 We first try and parse a parameter-declaration-clause,
19912 and then try a nested declarator (if FIRST is true).
19914 It is not an error for it not to be a
19915 parameter-declaration-clause, even when FIRST is
19916 false. Consider,
19918 int i (int);
19919 int i (3);
19921 The first is the declaration of a function while the
19922 second is the definition of a variable, including its
19923 initializer.
19925 Having seen only the parenthesis, we cannot know which of
19926 these two alternatives should be selected. Even more
19927 complex are examples like:
19929 int i (int (a));
19930 int i (int (3));
19932 The former is a function-declaration; the latter is a
19933 variable initialization.
19935 Thus again, we try a parameter-declaration-clause, and if
19936 that fails, we back out and return. */
19938 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19940 tree params;
19941 bool is_declarator = false;
19943 open_paren = NULL;
19945 /* In a member-declarator, the only valid interpretation
19946 of a parenthesis is the start of a
19947 parameter-declaration-clause. (It is invalid to
19948 initialize a static data member with a parenthesized
19949 initializer; only the "=" form of initialization is
19950 permitted.) */
19951 if (!member_p)
19952 cp_parser_parse_tentatively (parser);
19954 /* Consume the `('. */
19955 matching_parens parens;
19956 parens.consume_open (parser);
19957 if (first)
19959 /* If this is going to be an abstract declarator, we're
19960 in a declarator and we can't have default args. */
19961 parser->default_arg_ok_p = false;
19962 parser->in_declarator_p = true;
19965 begin_scope (sk_function_parms, NULL_TREE);
19967 /* Parse the parameter-declaration-clause. */
19968 params = cp_parser_parameter_declaration_clause (parser);
19970 /* Consume the `)'. */
19971 parens.require_close (parser);
19973 /* If all went well, parse the cv-qualifier-seq,
19974 ref-qualifier and the exception-specification. */
19975 if (member_p || cp_parser_parse_definitely (parser))
19977 cp_cv_quals cv_quals;
19978 cp_virt_specifiers virt_specifiers;
19979 cp_ref_qualifier ref_qual;
19980 tree exception_specification;
19981 tree late_return;
19982 tree attrs;
19983 bool memfn = (member_p || (pushed_scope
19984 && CLASS_TYPE_P (pushed_scope)));
19986 is_declarator = true;
19988 if (ctor_dtor_or_conv_p)
19989 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19990 first = false;
19992 /* Parse the cv-qualifier-seq. */
19993 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19994 /* Parse the ref-qualifier. */
19995 ref_qual = cp_parser_ref_qualifier_opt (parser);
19996 /* Parse the tx-qualifier. */
19997 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19998 /* And the exception-specification. */
19999 exception_specification
20000 = cp_parser_exception_specification_opt (parser);
20002 attrs = cp_parser_std_attribute_spec_seq (parser);
20004 /* In here, we handle cases where attribute is used after
20005 the function declaration. For example:
20006 void func (int x) __attribute__((vector(..))); */
20007 tree gnu_attrs = NULL_TREE;
20008 tree requires_clause = NULL_TREE;
20009 late_return = (cp_parser_late_return_type_opt
20010 (parser, declarator, requires_clause,
20011 memfn ? cv_quals : -1));
20013 /* Parse the virt-specifier-seq. */
20014 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20016 /* Create the function-declarator. */
20017 declarator = make_call_declarator (declarator,
20018 params,
20019 cv_quals,
20020 virt_specifiers,
20021 ref_qual,
20022 tx_qual,
20023 exception_specification,
20024 late_return,
20025 requires_clause);
20026 declarator->std_attributes = attrs;
20027 declarator->attributes = gnu_attrs;
20028 /* Any subsequent parameter lists are to do with
20029 return type, so are not those of the declared
20030 function. */
20031 parser->default_arg_ok_p = false;
20034 /* Remove the function parms from scope. */
20035 pop_bindings_and_leave_scope ();
20037 if (is_declarator)
20038 /* Repeat the main loop. */
20039 continue;
20042 /* If this is the first, we can try a parenthesized
20043 declarator. */
20044 if (first)
20046 bool saved_in_type_id_in_expr_p;
20048 parser->default_arg_ok_p = saved_default_arg_ok_p;
20049 parser->in_declarator_p = saved_in_declarator_p;
20051 open_paren = token;
20052 /* Consume the `('. */
20053 matching_parens parens;
20054 parens.consume_open (parser);
20055 /* Parse the nested declarator. */
20056 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20057 parser->in_type_id_in_expr_p = true;
20058 declarator
20059 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20060 /*parenthesized_p=*/NULL,
20061 member_p, friend_p);
20062 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20063 first = false;
20064 /* Expect a `)'. */
20065 close_paren = cp_lexer_peek_token (parser->lexer);
20066 if (!parens.require_close (parser))
20067 declarator = cp_error_declarator;
20068 if (declarator == cp_error_declarator)
20069 break;
20071 goto handle_declarator;
20073 /* Otherwise, we must be done. */
20074 else
20075 break;
20077 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20078 && token->type == CPP_OPEN_SQUARE
20079 && !cp_next_tokens_can_be_attribute_p (parser))
20081 /* Parse an array-declarator. */
20082 tree bounds, attrs;
20084 if (ctor_dtor_or_conv_p)
20085 *ctor_dtor_or_conv_p = 0;
20087 open_paren = NULL;
20088 first = false;
20089 parser->default_arg_ok_p = false;
20090 parser->in_declarator_p = true;
20091 /* Consume the `['. */
20092 cp_lexer_consume_token (parser->lexer);
20093 /* Peek at the next token. */
20094 token = cp_lexer_peek_token (parser->lexer);
20095 /* If the next token is `]', then there is no
20096 constant-expression. */
20097 if (token->type != CPP_CLOSE_SQUARE)
20099 bool non_constant_p;
20100 bounds
20101 = cp_parser_constant_expression (parser,
20102 /*allow_non_constant=*/true,
20103 &non_constant_p);
20104 if (!non_constant_p)
20105 /* OK */;
20106 else if (error_operand_p (bounds))
20107 /* Already gave an error. */;
20108 else if (!parser->in_function_body
20109 || current_binding_level->kind == sk_function_parms)
20111 /* Normally, the array bound must be an integral constant
20112 expression. However, as an extension, we allow VLAs
20113 in function scopes as long as they aren't part of a
20114 parameter declaration. */
20115 cp_parser_error (parser,
20116 "array bound is not an integer constant");
20117 bounds = error_mark_node;
20119 else if (processing_template_decl
20120 && !type_dependent_expression_p (bounds))
20122 /* Remember this wasn't a constant-expression. */
20123 bounds = build_nop (TREE_TYPE (bounds), bounds);
20124 TREE_SIDE_EFFECTS (bounds) = 1;
20127 else
20128 bounds = NULL_TREE;
20129 /* Look for the closing `]'. */
20130 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20132 declarator = cp_error_declarator;
20133 break;
20136 attrs = cp_parser_std_attribute_spec_seq (parser);
20137 declarator = make_array_declarator (declarator, bounds);
20138 declarator->std_attributes = attrs;
20140 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20143 tree qualifying_scope;
20144 tree unqualified_name;
20145 tree attrs;
20146 special_function_kind sfk;
20147 bool abstract_ok;
20148 bool pack_expansion_p = false;
20149 cp_token *declarator_id_start_token;
20151 /* Parse a declarator-id */
20152 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20153 if (abstract_ok)
20155 cp_parser_parse_tentatively (parser);
20157 /* If we see an ellipsis, we should be looking at a
20158 parameter pack. */
20159 if (token->type == CPP_ELLIPSIS)
20161 /* Consume the `...' */
20162 cp_lexer_consume_token (parser->lexer);
20164 pack_expansion_p = true;
20168 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20169 unqualified_name
20170 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20171 qualifying_scope = parser->scope;
20172 if (abstract_ok)
20174 bool okay = false;
20176 if (!unqualified_name && pack_expansion_p)
20178 /* Check whether an error occurred. */
20179 okay = !cp_parser_error_occurred (parser);
20181 /* We already consumed the ellipsis to mark a
20182 parameter pack, but we have no way to report it,
20183 so abort the tentative parse. We will be exiting
20184 immediately anyway. */
20185 cp_parser_abort_tentative_parse (parser);
20187 else
20188 okay = cp_parser_parse_definitely (parser);
20190 if (!okay)
20191 unqualified_name = error_mark_node;
20192 else if (unqualified_name
20193 && (qualifying_scope
20194 || (!identifier_p (unqualified_name))))
20196 cp_parser_error (parser, "expected unqualified-id");
20197 unqualified_name = error_mark_node;
20201 if (!unqualified_name)
20202 return NULL;
20203 if (unqualified_name == error_mark_node)
20205 declarator = cp_error_declarator;
20206 pack_expansion_p = false;
20207 declarator->parameter_pack_p = false;
20208 break;
20211 attrs = cp_parser_std_attribute_spec_seq (parser);
20213 if (qualifying_scope && at_namespace_scope_p ()
20214 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20216 /* In the declaration of a member of a template class
20217 outside of the class itself, the SCOPE will sometimes
20218 be a TYPENAME_TYPE. For example, given:
20220 template <typename T>
20221 int S<T>::R::i = 3;
20223 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20224 this context, we must resolve S<T>::R to an ordinary
20225 type, rather than a typename type.
20227 The reason we normally avoid resolving TYPENAME_TYPEs
20228 is that a specialization of `S' might render
20229 `S<T>::R' not a type. However, if `S' is
20230 specialized, then this `i' will not be used, so there
20231 is no harm in resolving the types here. */
20232 tree type;
20234 /* Resolve the TYPENAME_TYPE. */
20235 type = resolve_typename_type (qualifying_scope,
20236 /*only_current_p=*/false);
20237 /* If that failed, the declarator is invalid. */
20238 if (TREE_CODE (type) == TYPENAME_TYPE)
20240 if (typedef_variant_p (type))
20241 error_at (declarator_id_start_token->location,
20242 "cannot define member of dependent typedef "
20243 "%qT", type);
20244 else
20245 error_at (declarator_id_start_token->location,
20246 "%<%T::%E%> is not a type",
20247 TYPE_CONTEXT (qualifying_scope),
20248 TYPE_IDENTIFIER (qualifying_scope));
20250 qualifying_scope = type;
20253 sfk = sfk_none;
20255 if (unqualified_name)
20257 tree class_type;
20259 if (qualifying_scope
20260 && CLASS_TYPE_P (qualifying_scope))
20261 class_type = qualifying_scope;
20262 else
20263 class_type = current_class_type;
20265 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20267 tree name_type = TREE_TYPE (unqualified_name);
20269 if (!class_type || !same_type_p (name_type, class_type))
20271 /* We do not attempt to print the declarator
20272 here because we do not have enough
20273 information about its original syntactic
20274 form. */
20275 cp_parser_error (parser, "invalid declarator");
20276 declarator = cp_error_declarator;
20277 break;
20279 else if (qualifying_scope
20280 && CLASSTYPE_USE_TEMPLATE (name_type))
20282 error_at (declarator_id_start_token->location,
20283 "invalid use of constructor as a template");
20284 inform (declarator_id_start_token->location,
20285 "use %<%T::%D%> instead of %<%T::%D%> to "
20286 "name the constructor in a qualified name",
20287 class_type,
20288 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20289 class_type, name_type);
20290 declarator = cp_error_declarator;
20291 break;
20293 unqualified_name = constructor_name (class_type);
20296 if (class_type)
20298 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20299 sfk = sfk_destructor;
20300 else if (identifier_p (unqualified_name)
20301 && IDENTIFIER_CONV_OP_P (unqualified_name))
20302 sfk = sfk_conversion;
20303 else if (/* There's no way to declare a constructor
20304 for an unnamed type, even if the type
20305 got a name for linkage purposes. */
20306 !TYPE_WAS_UNNAMED (class_type)
20307 /* Handle correctly (c++/19200):
20309 struct S {
20310 struct T{};
20311 friend void S(T);
20314 and also:
20316 namespace N {
20317 void S();
20320 struct S {
20321 friend void N::S();
20322 }; */
20323 && (!friend_p || class_type == qualifying_scope)
20324 && constructor_name_p (unqualified_name,
20325 class_type))
20326 sfk = sfk_constructor;
20327 else if (is_overloaded_fn (unqualified_name)
20328 && DECL_CONSTRUCTOR_P (get_first_fn
20329 (unqualified_name)))
20330 sfk = sfk_constructor;
20332 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20333 *ctor_dtor_or_conv_p = -1;
20336 declarator = make_id_declarator (qualifying_scope,
20337 unqualified_name,
20338 sfk);
20339 declarator->std_attributes = attrs;
20340 declarator->id_loc = token->location;
20341 declarator->parameter_pack_p = pack_expansion_p;
20343 if (pack_expansion_p)
20344 maybe_warn_variadic_templates ();
20347 handle_declarator:;
20348 scope = get_scope_of_declarator (declarator);
20349 if (scope)
20351 /* Any names that appear after the declarator-id for a
20352 member are looked up in the containing scope. */
20353 if (at_function_scope_p ())
20355 /* But declarations with qualified-ids can't appear in a
20356 function. */
20357 cp_parser_error (parser, "qualified-id in declaration");
20358 declarator = cp_error_declarator;
20359 break;
20361 pushed_scope = push_scope (scope);
20363 parser->in_declarator_p = true;
20364 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20365 || (declarator && declarator->kind == cdk_id))
20366 /* Default args are only allowed on function
20367 declarations. */
20368 parser->default_arg_ok_p = saved_default_arg_ok_p;
20369 else
20370 parser->default_arg_ok_p = false;
20372 first = false;
20374 /* We're done. */
20375 else
20376 break;
20379 /* For an abstract declarator, we might wind up with nothing at this
20380 point. That's an error; the declarator is not optional. */
20381 if (!declarator)
20382 cp_parser_error (parser, "expected declarator");
20383 else if (open_paren)
20385 /* Record overly parenthesized declarator so we can give a
20386 diagnostic about confusing decl/expr disambiguation. */
20387 if (declarator->kind == cdk_array)
20389 /* If the open and close parens are on different lines, this
20390 is probably a formatting thing, so ignore. */
20391 expanded_location open = expand_location (open_paren->location);
20392 expanded_location close = expand_location (close_paren->location);
20393 if (open.line != close.line || open.file != close.file)
20394 open_paren = NULL;
20396 if (open_paren)
20397 declarator->parenthesized = open_paren->location;
20400 /* If we entered a scope, we must exit it now. */
20401 if (pushed_scope)
20402 pop_scope (pushed_scope);
20404 parser->default_arg_ok_p = saved_default_arg_ok_p;
20405 parser->in_declarator_p = saved_in_declarator_p;
20407 return declarator;
20410 /* Parse a ptr-operator.
20412 ptr-operator:
20413 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20414 * cv-qualifier-seq [opt]
20416 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20417 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20419 GNU Extension:
20421 ptr-operator:
20422 & cv-qualifier-seq [opt]
20424 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20425 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20426 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20427 filled in with the TYPE containing the member. *CV_QUALS is
20428 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20429 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20430 Note that the tree codes returned by this function have nothing
20431 to do with the types of trees that will be eventually be created
20432 to represent the pointer or reference type being parsed. They are
20433 just constants with suggestive names. */
20434 static enum tree_code
20435 cp_parser_ptr_operator (cp_parser* parser,
20436 tree* type,
20437 cp_cv_quals *cv_quals,
20438 tree *attributes)
20440 enum tree_code code = ERROR_MARK;
20441 cp_token *token;
20442 tree attrs = NULL_TREE;
20444 /* Assume that it's not a pointer-to-member. */
20445 *type = NULL_TREE;
20446 /* And that there are no cv-qualifiers. */
20447 *cv_quals = TYPE_UNQUALIFIED;
20449 /* Peek at the next token. */
20450 token = cp_lexer_peek_token (parser->lexer);
20452 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20453 if (token->type == CPP_MULT)
20454 code = INDIRECT_REF;
20455 else if (token->type == CPP_AND)
20456 code = ADDR_EXPR;
20457 else if ((cxx_dialect != cxx98) &&
20458 token->type == CPP_AND_AND) /* C++0x only */
20459 code = NON_LVALUE_EXPR;
20461 if (code != ERROR_MARK)
20463 /* Consume the `*', `&' or `&&'. */
20464 cp_lexer_consume_token (parser->lexer);
20466 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20467 `&', if we are allowing GNU extensions. (The only qualifier
20468 that can legally appear after `&' is `restrict', but that is
20469 enforced during semantic analysis. */
20470 if (code == INDIRECT_REF
20471 || cp_parser_allow_gnu_extensions_p (parser))
20472 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20474 attrs = cp_parser_std_attribute_spec_seq (parser);
20475 if (attributes != NULL)
20476 *attributes = attrs;
20478 else
20480 /* Try the pointer-to-member case. */
20481 cp_parser_parse_tentatively (parser);
20482 /* Look for the optional `::' operator. */
20483 cp_parser_global_scope_opt (parser,
20484 /*current_scope_valid_p=*/false);
20485 /* Look for the nested-name specifier. */
20486 token = cp_lexer_peek_token (parser->lexer);
20487 cp_parser_nested_name_specifier (parser,
20488 /*typename_keyword_p=*/false,
20489 /*check_dependency_p=*/true,
20490 /*type_p=*/false,
20491 /*is_declaration=*/false);
20492 /* If we found it, and the next token is a `*', then we are
20493 indeed looking at a pointer-to-member operator. */
20494 if (!cp_parser_error_occurred (parser)
20495 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20497 /* Indicate that the `*' operator was used. */
20498 code = INDIRECT_REF;
20500 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20501 error_at (token->location, "%qD is a namespace", parser->scope);
20502 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20503 error_at (token->location, "cannot form pointer to member of "
20504 "non-class %q#T", parser->scope);
20505 else
20507 /* The type of which the member is a member is given by the
20508 current SCOPE. */
20509 *type = parser->scope;
20510 /* The next name will not be qualified. */
20511 parser->scope = NULL_TREE;
20512 parser->qualifying_scope = NULL_TREE;
20513 parser->object_scope = NULL_TREE;
20514 /* Look for optional c++11 attributes. */
20515 attrs = cp_parser_std_attribute_spec_seq (parser);
20516 if (attributes != NULL)
20517 *attributes = attrs;
20518 /* Look for the optional cv-qualifier-seq. */
20519 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20522 /* If that didn't work we don't have a ptr-operator. */
20523 if (!cp_parser_parse_definitely (parser))
20524 cp_parser_error (parser, "expected ptr-operator");
20527 return code;
20530 /* Parse an (optional) cv-qualifier-seq.
20532 cv-qualifier-seq:
20533 cv-qualifier cv-qualifier-seq [opt]
20535 cv-qualifier:
20536 const
20537 volatile
20539 GNU Extension:
20541 cv-qualifier:
20542 __restrict__
20544 Returns a bitmask representing the cv-qualifiers. */
20546 static cp_cv_quals
20547 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20549 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20551 while (true)
20553 cp_token *token;
20554 cp_cv_quals cv_qualifier;
20556 /* Peek at the next token. */
20557 token = cp_lexer_peek_token (parser->lexer);
20558 /* See if it's a cv-qualifier. */
20559 switch (token->keyword)
20561 case RID_CONST:
20562 cv_qualifier = TYPE_QUAL_CONST;
20563 break;
20565 case RID_VOLATILE:
20566 cv_qualifier = TYPE_QUAL_VOLATILE;
20567 break;
20569 case RID_RESTRICT:
20570 cv_qualifier = TYPE_QUAL_RESTRICT;
20571 break;
20573 default:
20574 cv_qualifier = TYPE_UNQUALIFIED;
20575 break;
20578 if (!cv_qualifier)
20579 break;
20581 if (cv_quals & cv_qualifier)
20583 gcc_rich_location richloc (token->location);
20584 richloc.add_fixit_remove ();
20585 error_at (&richloc, "duplicate cv-qualifier");
20586 cp_lexer_purge_token (parser->lexer);
20588 else
20590 cp_lexer_consume_token (parser->lexer);
20591 cv_quals |= cv_qualifier;
20595 return cv_quals;
20598 /* Parse an (optional) ref-qualifier
20600 ref-qualifier:
20604 Returns cp_ref_qualifier representing ref-qualifier. */
20606 static cp_ref_qualifier
20607 cp_parser_ref_qualifier_opt (cp_parser* parser)
20609 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20611 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20612 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20613 return ref_qual;
20615 while (true)
20617 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20618 cp_token *token = cp_lexer_peek_token (parser->lexer);
20620 switch (token->type)
20622 case CPP_AND:
20623 curr_ref_qual = REF_QUAL_LVALUE;
20624 break;
20626 case CPP_AND_AND:
20627 curr_ref_qual = REF_QUAL_RVALUE;
20628 break;
20630 default:
20631 curr_ref_qual = REF_QUAL_NONE;
20632 break;
20635 if (!curr_ref_qual)
20636 break;
20637 else if (ref_qual)
20639 error_at (token->location, "multiple ref-qualifiers");
20640 cp_lexer_purge_token (parser->lexer);
20642 else
20644 ref_qual = curr_ref_qual;
20645 cp_lexer_consume_token (parser->lexer);
20649 return ref_qual;
20652 /* Parse an optional tx-qualifier.
20654 tx-qualifier:
20655 transaction_safe
20656 transaction_safe_dynamic */
20658 static tree
20659 cp_parser_tx_qualifier_opt (cp_parser *parser)
20661 cp_token *token = cp_lexer_peek_token (parser->lexer);
20662 if (token->type == CPP_NAME)
20664 tree name = token->u.value;
20665 const char *p = IDENTIFIER_POINTER (name);
20666 const int len = strlen ("transaction_safe");
20667 if (!strncmp (p, "transaction_safe", len))
20669 p += len;
20670 if (*p == '\0'
20671 || !strcmp (p, "_dynamic"))
20673 cp_lexer_consume_token (parser->lexer);
20674 if (!flag_tm)
20676 error ("%qE requires %<-fgnu-tm%>", name);
20677 return NULL_TREE;
20679 else
20680 return name;
20684 return NULL_TREE;
20687 /* Parse an (optional) virt-specifier-seq.
20689 virt-specifier-seq:
20690 virt-specifier virt-specifier-seq [opt]
20692 virt-specifier:
20693 override
20694 final
20696 Returns a bitmask representing the virt-specifiers. */
20698 static cp_virt_specifiers
20699 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20701 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20703 while (true)
20705 cp_token *token;
20706 cp_virt_specifiers virt_specifier;
20708 /* Peek at the next token. */
20709 token = cp_lexer_peek_token (parser->lexer);
20710 /* See if it's a virt-specifier-qualifier. */
20711 if (token->type != CPP_NAME)
20712 break;
20713 if (id_equal (token->u.value, "override"))
20715 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20716 virt_specifier = VIRT_SPEC_OVERRIDE;
20718 else if (id_equal (token->u.value, "final"))
20720 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20721 virt_specifier = VIRT_SPEC_FINAL;
20723 else if (id_equal (token->u.value, "__final"))
20725 virt_specifier = VIRT_SPEC_FINAL;
20727 else
20728 break;
20730 if (virt_specifiers & virt_specifier)
20732 gcc_rich_location richloc (token->location);
20733 richloc.add_fixit_remove ();
20734 error_at (&richloc, "duplicate virt-specifier");
20735 cp_lexer_purge_token (parser->lexer);
20737 else
20739 cp_lexer_consume_token (parser->lexer);
20740 virt_specifiers |= virt_specifier;
20743 return virt_specifiers;
20746 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20747 is in scope even though it isn't real. */
20749 void
20750 inject_this_parameter (tree ctype, cp_cv_quals quals)
20752 tree this_parm;
20754 if (current_class_ptr)
20756 /* We don't clear this between NSDMIs. Is it already what we want? */
20757 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20758 if (DECL_P (current_class_ptr)
20759 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20760 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20761 && cp_type_quals (type) == quals)
20762 return;
20765 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20766 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20767 current_class_ptr = NULL_TREE;
20768 current_class_ref
20769 = cp_build_fold_indirect_ref (this_parm);
20770 current_class_ptr = this_parm;
20773 /* Return true iff our current scope is a non-static data member
20774 initializer. */
20776 bool
20777 parsing_nsdmi (void)
20779 /* We recognize NSDMI context by the context-less 'this' pointer set up
20780 by the function above. */
20781 if (current_class_ptr
20782 && TREE_CODE (current_class_ptr) == PARM_DECL
20783 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20784 return true;
20785 return false;
20788 /* Parse a late-specified return type, if any. This is not a separate
20789 non-terminal, but part of a function declarator, which looks like
20791 -> trailing-type-specifier-seq abstract-declarator(opt)
20793 Returns the type indicated by the type-id.
20795 In addition to this, parse any queued up #pragma omp declare simd
20796 clauses, and #pragma acc routine clauses.
20798 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20799 function. */
20801 static tree
20802 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20803 tree& requires_clause, cp_cv_quals quals)
20805 cp_token *token;
20806 tree type = NULL_TREE;
20807 bool declare_simd_p = (parser->omp_declare_simd
20808 && declarator
20809 && declarator->kind == cdk_id);
20811 bool oacc_routine_p = (parser->oacc_routine
20812 && declarator
20813 && declarator->kind == cdk_id);
20815 /* Peek at the next token. */
20816 token = cp_lexer_peek_token (parser->lexer);
20817 /* A late-specified return type is indicated by an initial '->'. */
20818 if (token->type != CPP_DEREF
20819 && token->keyword != RID_REQUIRES
20820 && !(token->type == CPP_NAME
20821 && token->u.value == ridpointers[RID_REQUIRES])
20822 && !(declare_simd_p || oacc_routine_p))
20823 return NULL_TREE;
20825 tree save_ccp = current_class_ptr;
20826 tree save_ccr = current_class_ref;
20827 if (quals >= 0)
20829 /* DR 1207: 'this' is in scope in the trailing return type. */
20830 inject_this_parameter (current_class_type, quals);
20833 if (token->type == CPP_DEREF)
20835 /* Consume the ->. */
20836 cp_lexer_consume_token (parser->lexer);
20838 type = cp_parser_trailing_type_id (parser);
20841 /* Function declarations may be followed by a trailing
20842 requires-clause. */
20843 requires_clause = cp_parser_requires_clause_opt (parser);
20845 if (declare_simd_p)
20846 declarator->attributes
20847 = cp_parser_late_parsing_omp_declare_simd (parser,
20848 declarator->attributes);
20849 if (oacc_routine_p)
20850 declarator->attributes
20851 = cp_parser_late_parsing_oacc_routine (parser,
20852 declarator->attributes);
20854 if (quals >= 0)
20856 current_class_ptr = save_ccp;
20857 current_class_ref = save_ccr;
20860 return type;
20863 /* Parse a declarator-id.
20865 declarator-id:
20866 id-expression
20867 :: [opt] nested-name-specifier [opt] type-name
20869 In the `id-expression' case, the value returned is as for
20870 cp_parser_id_expression if the id-expression was an unqualified-id.
20871 If the id-expression was a qualified-id, then a SCOPE_REF is
20872 returned. The first operand is the scope (either a NAMESPACE_DECL
20873 or TREE_TYPE), but the second is still just a representation of an
20874 unqualified-id. */
20876 static tree
20877 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20879 tree id;
20880 /* The expression must be an id-expression. Assume that qualified
20881 names are the names of types so that:
20883 template <class T>
20884 int S<T>::R::i = 3;
20886 will work; we must treat `S<T>::R' as the name of a type.
20887 Similarly, assume that qualified names are templates, where
20888 required, so that:
20890 template <class T>
20891 int S<T>::R<T>::i = 3;
20893 will work, too. */
20894 id = cp_parser_id_expression (parser,
20895 /*template_keyword_p=*/false,
20896 /*check_dependency_p=*/false,
20897 /*template_p=*/NULL,
20898 /*declarator_p=*/true,
20899 optional_p);
20900 if (id && BASELINK_P (id))
20901 id = BASELINK_FUNCTIONS (id);
20902 return id;
20905 /* Parse a type-id.
20907 type-id:
20908 type-specifier-seq abstract-declarator [opt]
20910 Returns the TYPE specified. */
20912 static tree
20913 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20914 bool is_trailing_return)
20916 cp_decl_specifier_seq type_specifier_seq;
20917 cp_declarator *abstract_declarator;
20919 /* Parse the type-specifier-seq. */
20920 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20921 is_trailing_return,
20922 &type_specifier_seq);
20923 if (is_template_arg && type_specifier_seq.type
20924 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20925 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20926 /* A bare template name as a template argument is a template template
20927 argument, not a placeholder, so fail parsing it as a type argument. */
20929 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20930 cp_parser_simulate_error (parser);
20931 return error_mark_node;
20933 if (type_specifier_seq.type == error_mark_node)
20934 return error_mark_node;
20936 /* There might or might not be an abstract declarator. */
20937 cp_parser_parse_tentatively (parser);
20938 /* Look for the declarator. */
20939 abstract_declarator
20940 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20941 /*parenthesized_p=*/NULL,
20942 /*member_p=*/false,
20943 /*friend_p=*/false);
20944 /* Check to see if there really was a declarator. */
20945 if (!cp_parser_parse_definitely (parser))
20946 abstract_declarator = NULL;
20948 if (type_specifier_seq.type
20949 /* The concepts TS allows 'auto' as a type-id. */
20950 && (!flag_concepts || parser->in_type_id_in_expr_p)
20951 /* None of the valid uses of 'auto' in C++14 involve the type-id
20952 nonterminal, but it is valid in a trailing-return-type. */
20953 && !(cxx_dialect >= cxx14 && is_trailing_return))
20954 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20956 /* A type-id with type 'auto' is only ok if the abstract declarator
20957 is a function declarator with a late-specified return type.
20959 A type-id with 'auto' is also valid in a trailing-return-type
20960 in a compound-requirement. */
20961 if (abstract_declarator
20962 && abstract_declarator->kind == cdk_function
20963 && abstract_declarator->u.function.late_return_type)
20964 /* OK */;
20965 else if (parser->in_result_type_constraint_p)
20966 /* OK */;
20967 else
20969 location_t loc = type_specifier_seq.locations[ds_type_spec];
20970 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20972 error_at (loc, "missing template arguments after %qT",
20973 auto_node);
20974 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20975 tmpl);
20977 else
20978 error_at (loc, "invalid use of %qT", auto_node);
20979 return error_mark_node;
20983 return groktypename (&type_specifier_seq, abstract_declarator,
20984 is_template_arg);
20987 static tree
20988 cp_parser_type_id (cp_parser *parser)
20990 return cp_parser_type_id_1 (parser, false, false);
20993 static tree
20994 cp_parser_template_type_arg (cp_parser *parser)
20996 tree r;
20997 const char *saved_message = parser->type_definition_forbidden_message;
20998 parser->type_definition_forbidden_message
20999 = G_("types may not be defined in template arguments");
21000 r = cp_parser_type_id_1 (parser, true, false);
21001 parser->type_definition_forbidden_message = saved_message;
21002 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21004 error ("invalid use of %<auto%> in template argument");
21005 r = error_mark_node;
21007 return r;
21010 static tree
21011 cp_parser_trailing_type_id (cp_parser *parser)
21013 return cp_parser_type_id_1 (parser, false, true);
21016 /* Parse a type-specifier-seq.
21018 type-specifier-seq:
21019 type-specifier type-specifier-seq [opt]
21021 GNU extension:
21023 type-specifier-seq:
21024 attributes type-specifier-seq [opt]
21026 If IS_DECLARATION is true, we are at the start of a "condition" or
21027 exception-declaration, so we might be followed by a declarator-id.
21029 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21030 i.e. we've just seen "->".
21032 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21034 static void
21035 cp_parser_type_specifier_seq (cp_parser* parser,
21036 bool is_declaration,
21037 bool is_trailing_return,
21038 cp_decl_specifier_seq *type_specifier_seq)
21040 bool seen_type_specifier = false;
21041 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21042 cp_token *start_token = NULL;
21044 /* Clear the TYPE_SPECIFIER_SEQ. */
21045 clear_decl_specs (type_specifier_seq);
21047 /* In the context of a trailing return type, enum E { } is an
21048 elaborated-type-specifier followed by a function-body, not an
21049 enum-specifier. */
21050 if (is_trailing_return)
21051 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21053 /* Parse the type-specifiers and attributes. */
21054 while (true)
21056 tree type_specifier;
21057 bool is_cv_qualifier;
21059 /* Check for attributes first. */
21060 if (cp_next_tokens_can_be_attribute_p (parser))
21062 type_specifier_seq->attributes
21063 = attr_chainon (type_specifier_seq->attributes,
21064 cp_parser_attributes_opt (parser));
21065 continue;
21068 /* record the token of the beginning of the type specifier seq,
21069 for error reporting purposes*/
21070 if (!start_token)
21071 start_token = cp_lexer_peek_token (parser->lexer);
21073 /* Look for the type-specifier. */
21074 type_specifier = cp_parser_type_specifier (parser,
21075 flags,
21076 type_specifier_seq,
21077 /*is_declaration=*/false,
21078 NULL,
21079 &is_cv_qualifier);
21080 if (!type_specifier)
21082 /* If the first type-specifier could not be found, this is not a
21083 type-specifier-seq at all. */
21084 if (!seen_type_specifier)
21086 /* Set in_declarator_p to avoid skipping to the semicolon. */
21087 int in_decl = parser->in_declarator_p;
21088 parser->in_declarator_p = true;
21090 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21091 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21092 cp_parser_error (parser, "expected type-specifier");
21094 parser->in_declarator_p = in_decl;
21096 type_specifier_seq->type = error_mark_node;
21097 return;
21099 /* If subsequent type-specifiers could not be found, the
21100 type-specifier-seq is complete. */
21101 break;
21104 seen_type_specifier = true;
21105 /* The standard says that a condition can be:
21107 type-specifier-seq declarator = assignment-expression
21109 However, given:
21111 struct S {};
21112 if (int S = ...)
21114 we should treat the "S" as a declarator, not as a
21115 type-specifier. The standard doesn't say that explicitly for
21116 type-specifier-seq, but it does say that for
21117 decl-specifier-seq in an ordinary declaration. Perhaps it
21118 would be clearer just to allow a decl-specifier-seq here, and
21119 then add a semantic restriction that if any decl-specifiers
21120 that are not type-specifiers appear, the program is invalid. */
21121 if (is_declaration && !is_cv_qualifier)
21122 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21126 /* Return whether the function currently being declared has an associated
21127 template parameter list. */
21129 static bool
21130 function_being_declared_is_template_p (cp_parser* parser)
21132 if (!current_template_parms || processing_template_parmlist)
21133 return false;
21135 if (parser->implicit_template_scope)
21136 return true;
21138 if (at_class_scope_p ()
21139 && TYPE_BEING_DEFINED (current_class_type))
21140 return parser->num_template_parameter_lists != 0;
21142 return ((int) parser->num_template_parameter_lists > template_class_depth
21143 (current_class_type));
21146 /* Parse a parameter-declaration-clause.
21148 parameter-declaration-clause:
21149 parameter-declaration-list [opt] ... [opt]
21150 parameter-declaration-list , ...
21152 Returns a representation for the parameter declarations. A return
21153 value of NULL indicates a parameter-declaration-clause consisting
21154 only of an ellipsis. */
21156 static tree
21157 cp_parser_parameter_declaration_clause (cp_parser* parser)
21159 tree parameters;
21160 cp_token *token;
21161 bool ellipsis_p;
21162 bool is_error;
21164 struct cleanup {
21165 cp_parser* parser;
21166 int auto_is_implicit_function_template_parm_p;
21167 ~cleanup() {
21168 parser->auto_is_implicit_function_template_parm_p
21169 = auto_is_implicit_function_template_parm_p;
21171 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21173 (void) cleanup;
21175 if (!processing_specialization
21176 && !processing_template_parmlist
21177 && !processing_explicit_instantiation)
21178 if (!current_function_decl
21179 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21180 parser->auto_is_implicit_function_template_parm_p = true;
21182 /* Peek at the next token. */
21183 token = cp_lexer_peek_token (parser->lexer);
21184 /* Check for trivial parameter-declaration-clauses. */
21185 if (token->type == CPP_ELLIPSIS)
21187 /* Consume the `...' token. */
21188 cp_lexer_consume_token (parser->lexer);
21189 return NULL_TREE;
21191 else if (token->type == CPP_CLOSE_PAREN)
21192 /* There are no parameters. */
21194 #ifndef NO_IMPLICIT_EXTERN_C
21195 if (in_system_header_at (input_location)
21196 && current_class_type == NULL
21197 && current_lang_name == lang_name_c)
21198 return NULL_TREE;
21199 else
21200 #endif
21201 return void_list_node;
21203 /* Check for `(void)', too, which is a special case. */
21204 else if (token->keyword == RID_VOID
21205 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21206 == CPP_CLOSE_PAREN))
21208 /* Consume the `void' token. */
21209 cp_lexer_consume_token (parser->lexer);
21210 /* There are no parameters. */
21211 return void_list_node;
21214 /* Parse the parameter-declaration-list. */
21215 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21216 /* If a parse error occurred while parsing the
21217 parameter-declaration-list, then the entire
21218 parameter-declaration-clause is erroneous. */
21219 if (is_error)
21220 return NULL;
21222 /* Peek at the next token. */
21223 token = cp_lexer_peek_token (parser->lexer);
21224 /* If it's a `,', the clause should terminate with an ellipsis. */
21225 if (token->type == CPP_COMMA)
21227 /* Consume the `,'. */
21228 cp_lexer_consume_token (parser->lexer);
21229 /* Expect an ellipsis. */
21230 ellipsis_p
21231 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21233 /* It might also be `...' if the optional trailing `,' was
21234 omitted. */
21235 else if (token->type == CPP_ELLIPSIS)
21237 /* Consume the `...' token. */
21238 cp_lexer_consume_token (parser->lexer);
21239 /* And remember that we saw it. */
21240 ellipsis_p = true;
21242 else
21243 ellipsis_p = false;
21245 /* Finish the parameter list. */
21246 if (!ellipsis_p)
21247 parameters = chainon (parameters, void_list_node);
21249 return parameters;
21252 /* Parse a parameter-declaration-list.
21254 parameter-declaration-list:
21255 parameter-declaration
21256 parameter-declaration-list , parameter-declaration
21258 Returns a representation of the parameter-declaration-list, as for
21259 cp_parser_parameter_declaration_clause. However, the
21260 `void_list_node' is never appended to the list. Upon return,
21261 *IS_ERROR will be true iff an error occurred. */
21263 static tree
21264 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21266 tree parameters = NULL_TREE;
21267 tree *tail = &parameters;
21268 bool saved_in_unbraced_linkage_specification_p;
21269 int index = 0;
21271 /* Assume all will go well. */
21272 *is_error = false;
21273 /* The special considerations that apply to a function within an
21274 unbraced linkage specifications do not apply to the parameters
21275 to the function. */
21276 saved_in_unbraced_linkage_specification_p
21277 = parser->in_unbraced_linkage_specification_p;
21278 parser->in_unbraced_linkage_specification_p = false;
21280 /* Look for more parameters. */
21281 while (true)
21283 cp_parameter_declarator *parameter;
21284 tree decl = error_mark_node;
21285 bool parenthesized_p = false;
21286 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21287 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21288 (current_template_parms)) : 0);
21290 /* Parse the parameter. */
21291 parameter
21292 = cp_parser_parameter_declaration (parser,
21293 /*template_parm_p=*/false,
21294 &parenthesized_p);
21296 /* We don't know yet if the enclosing context is deprecated, so wait
21297 and warn in grokparms if appropriate. */
21298 deprecated_state = DEPRECATED_SUPPRESS;
21300 if (parameter)
21302 /* If a function parameter pack was specified and an implicit template
21303 parameter was introduced during cp_parser_parameter_declaration,
21304 change any implicit parameters introduced into packs. */
21305 if (parser->implicit_template_parms
21306 && parameter->declarator
21307 && parameter->declarator->parameter_pack_p)
21309 int latest_template_parm_idx = TREE_VEC_LENGTH
21310 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21312 if (latest_template_parm_idx != template_parm_idx)
21313 parameter->decl_specifiers.type = convert_generic_types_to_packs
21314 (parameter->decl_specifiers.type,
21315 template_parm_idx, latest_template_parm_idx);
21318 decl = grokdeclarator (parameter->declarator,
21319 &parameter->decl_specifiers,
21320 PARM,
21321 parameter->default_argument != NULL_TREE,
21322 &parameter->decl_specifiers.attributes);
21323 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21324 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21327 deprecated_state = DEPRECATED_NORMAL;
21329 /* If a parse error occurred parsing the parameter declaration,
21330 then the entire parameter-declaration-list is erroneous. */
21331 if (decl == error_mark_node)
21333 *is_error = true;
21334 parameters = error_mark_node;
21335 break;
21338 if (parameter->decl_specifiers.attributes)
21339 cplus_decl_attributes (&decl,
21340 parameter->decl_specifiers.attributes,
21342 if (DECL_NAME (decl))
21343 decl = pushdecl (decl);
21345 if (decl != error_mark_node)
21347 retrofit_lang_decl (decl);
21348 DECL_PARM_INDEX (decl) = ++index;
21349 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21352 /* Add the new parameter to the list. */
21353 *tail = build_tree_list (parameter->default_argument, decl);
21354 tail = &TREE_CHAIN (*tail);
21356 /* Peek at the next token. */
21357 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21358 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21359 /* These are for Objective-C++ */
21360 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21361 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21362 /* The parameter-declaration-list is complete. */
21363 break;
21364 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21366 cp_token *token;
21368 /* Peek at the next token. */
21369 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21370 /* If it's an ellipsis, then the list is complete. */
21371 if (token->type == CPP_ELLIPSIS)
21372 break;
21373 /* Otherwise, there must be more parameters. Consume the
21374 `,'. */
21375 cp_lexer_consume_token (parser->lexer);
21376 /* When parsing something like:
21378 int i(float f, double d)
21380 we can tell after seeing the declaration for "f" that we
21381 are not looking at an initialization of a variable "i",
21382 but rather at the declaration of a function "i".
21384 Due to the fact that the parsing of template arguments
21385 (as specified to a template-id) requires backtracking we
21386 cannot use this technique when inside a template argument
21387 list. */
21388 if (!parser->in_template_argument_list_p
21389 && !parser->in_type_id_in_expr_p
21390 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21391 /* However, a parameter-declaration of the form
21392 "float(f)" (which is a valid declaration of a
21393 parameter "f") can also be interpreted as an
21394 expression (the conversion of "f" to "float"). */
21395 && !parenthesized_p)
21396 cp_parser_commit_to_tentative_parse (parser);
21398 else
21400 cp_parser_error (parser, "expected %<,%> or %<...%>");
21401 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21402 cp_parser_skip_to_closing_parenthesis (parser,
21403 /*recovering=*/true,
21404 /*or_comma=*/false,
21405 /*consume_paren=*/false);
21406 break;
21410 parser->in_unbraced_linkage_specification_p
21411 = saved_in_unbraced_linkage_specification_p;
21413 /* Reset implicit_template_scope if we are about to leave the function
21414 parameter list that introduced it. Note that for out-of-line member
21415 definitions, there will be one or more class scopes before we get to
21416 the template parameter scope. */
21418 if (cp_binding_level *its = parser->implicit_template_scope)
21419 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21421 while (maybe_its->kind == sk_class)
21422 maybe_its = maybe_its->level_chain;
21423 if (maybe_its == its)
21425 parser->implicit_template_parms = 0;
21426 parser->implicit_template_scope = 0;
21430 return parameters;
21433 /* Parse a parameter declaration.
21435 parameter-declaration:
21436 decl-specifier-seq ... [opt] declarator
21437 decl-specifier-seq declarator = assignment-expression
21438 decl-specifier-seq ... [opt] abstract-declarator [opt]
21439 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21441 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21442 declares a template parameter. (In that case, a non-nested `>'
21443 token encountered during the parsing of the assignment-expression
21444 is not interpreted as a greater-than operator.)
21446 Returns a representation of the parameter, or NULL if an error
21447 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21448 true iff the declarator is of the form "(p)". */
21450 static cp_parameter_declarator *
21451 cp_parser_parameter_declaration (cp_parser *parser,
21452 bool template_parm_p,
21453 bool *parenthesized_p)
21455 int declares_class_or_enum;
21456 cp_decl_specifier_seq decl_specifiers;
21457 cp_declarator *declarator;
21458 tree default_argument;
21459 cp_token *token = NULL, *declarator_token_start = NULL;
21460 const char *saved_message;
21461 bool template_parameter_pack_p = false;
21463 /* In a template parameter, `>' is not an operator.
21465 [temp.param]
21467 When parsing a default template-argument for a non-type
21468 template-parameter, the first non-nested `>' is taken as the end
21469 of the template parameter-list rather than a greater-than
21470 operator. */
21472 /* Type definitions may not appear in parameter types. */
21473 saved_message = parser->type_definition_forbidden_message;
21474 parser->type_definition_forbidden_message
21475 = G_("types may not be defined in parameter types");
21477 /* Parse the declaration-specifiers. */
21478 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21479 cp_parser_decl_specifier_seq (parser,
21480 CP_PARSER_FLAGS_NONE,
21481 &decl_specifiers,
21482 &declares_class_or_enum);
21484 /* Complain about missing 'typename' or other invalid type names. */
21485 if (!decl_specifiers.any_type_specifiers_p
21486 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21487 decl_specifiers.type = error_mark_node;
21489 /* If an error occurred, there's no reason to attempt to parse the
21490 rest of the declaration. */
21491 if (cp_parser_error_occurred (parser))
21493 parser->type_definition_forbidden_message = saved_message;
21494 return NULL;
21497 /* Peek at the next token. */
21498 token = cp_lexer_peek_token (parser->lexer);
21500 /* If the next token is a `)', `,', `=', `>', or `...', then there
21501 is no declarator. However, when variadic templates are enabled,
21502 there may be a declarator following `...'. */
21503 if (token->type == CPP_CLOSE_PAREN
21504 || token->type == CPP_COMMA
21505 || token->type == CPP_EQ
21506 || token->type == CPP_GREATER)
21508 declarator = NULL;
21509 if (parenthesized_p)
21510 *parenthesized_p = false;
21512 /* Otherwise, there should be a declarator. */
21513 else
21515 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21516 parser->default_arg_ok_p = false;
21518 /* After seeing a decl-specifier-seq, if the next token is not a
21519 "(", there is no possibility that the code is a valid
21520 expression. Therefore, if parsing tentatively, we commit at
21521 this point. */
21522 if (!parser->in_template_argument_list_p
21523 /* In an expression context, having seen:
21525 (int((char ...
21527 we cannot be sure whether we are looking at a
21528 function-type (taking a "char" as a parameter) or a cast
21529 of some object of type "char" to "int". */
21530 && !parser->in_type_id_in_expr_p
21531 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21532 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21533 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21534 cp_parser_commit_to_tentative_parse (parser);
21535 /* Parse the declarator. */
21536 declarator_token_start = token;
21537 declarator = cp_parser_declarator (parser,
21538 CP_PARSER_DECLARATOR_EITHER,
21539 /*ctor_dtor_or_conv_p=*/NULL,
21540 parenthesized_p,
21541 /*member_p=*/false,
21542 /*friend_p=*/false);
21543 parser->default_arg_ok_p = saved_default_arg_ok_p;
21544 /* After the declarator, allow more attributes. */
21545 decl_specifiers.attributes
21546 = attr_chainon (decl_specifiers.attributes,
21547 cp_parser_attributes_opt (parser));
21549 /* If the declarator is a template parameter pack, remember that and
21550 clear the flag in the declarator itself so we don't get errors
21551 from grokdeclarator. */
21552 if (template_parm_p && declarator && declarator->parameter_pack_p)
21554 declarator->parameter_pack_p = false;
21555 template_parameter_pack_p = true;
21559 /* If the next token is an ellipsis, and we have not seen a declarator
21560 name, and if either the type of the declarator contains parameter
21561 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21562 for, eg, abbreviated integral type names), then we actually have a
21563 parameter pack expansion expression. Otherwise, leave the ellipsis
21564 for a C-style variadic function. */
21565 token = cp_lexer_peek_token (parser->lexer);
21566 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21568 tree type = decl_specifiers.type;
21570 if (type && DECL_P (type))
21571 type = TREE_TYPE (type);
21573 if (((type
21574 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21575 && (template_parm_p || uses_parameter_packs (type)))
21576 || (!type && template_parm_p))
21577 && declarator_can_be_parameter_pack (declarator))
21579 /* Consume the `...'. */
21580 cp_lexer_consume_token (parser->lexer);
21581 maybe_warn_variadic_templates ();
21583 /* Build a pack expansion type */
21584 if (template_parm_p)
21585 template_parameter_pack_p = true;
21586 else if (declarator)
21587 declarator->parameter_pack_p = true;
21588 else
21589 decl_specifiers.type = make_pack_expansion (type);
21593 /* The restriction on defining new types applies only to the type
21594 of the parameter, not to the default argument. */
21595 parser->type_definition_forbidden_message = saved_message;
21597 /* If the next token is `=', then process a default argument. */
21598 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21600 tree type = decl_specifiers.type;
21601 token = cp_lexer_peek_token (parser->lexer);
21602 /* If we are defining a class, then the tokens that make up the
21603 default argument must be saved and processed later. */
21604 if (!template_parm_p && at_class_scope_p ()
21605 && TYPE_BEING_DEFINED (current_class_type)
21606 && !LAMBDA_TYPE_P (current_class_type))
21607 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21609 // A constrained-type-specifier may declare a type template-parameter.
21610 else if (declares_constrained_type_template_parameter (type))
21611 default_argument
21612 = cp_parser_default_type_template_argument (parser);
21614 // A constrained-type-specifier may declare a template-template-parameter.
21615 else if (declares_constrained_template_template_parameter (type))
21616 default_argument
21617 = cp_parser_default_template_template_argument (parser);
21619 /* Outside of a class definition, we can just parse the
21620 assignment-expression. */
21621 else
21622 default_argument
21623 = cp_parser_default_argument (parser, template_parm_p);
21625 if (!parser->default_arg_ok_p)
21627 permerror (token->location,
21628 "default arguments are only "
21629 "permitted for function parameters");
21631 else if ((declarator && declarator->parameter_pack_p)
21632 || template_parameter_pack_p
21633 || (decl_specifiers.type
21634 && PACK_EXPANSION_P (decl_specifiers.type)))
21636 /* Find the name of the parameter pack. */
21637 cp_declarator *id_declarator = declarator;
21638 while (id_declarator && id_declarator->kind != cdk_id)
21639 id_declarator = id_declarator->declarator;
21641 if (id_declarator && id_declarator->kind == cdk_id)
21642 error_at (declarator_token_start->location,
21643 template_parm_p
21644 ? G_("template parameter pack %qD "
21645 "cannot have a default argument")
21646 : G_("parameter pack %qD cannot have "
21647 "a default argument"),
21648 id_declarator->u.id.unqualified_name);
21649 else
21650 error_at (declarator_token_start->location,
21651 template_parm_p
21652 ? G_("template parameter pack cannot have "
21653 "a default argument")
21654 : G_("parameter pack cannot have a "
21655 "default argument"));
21657 default_argument = NULL_TREE;
21660 else
21661 default_argument = NULL_TREE;
21663 /* Generate a location for the parameter, ranging from the start of the
21664 initial token to the end of the final token (using input_location for
21665 the latter, set up by cp_lexer_set_source_position_from_token when
21666 consuming tokens).
21668 If we have a identifier, then use it for the caret location, e.g.
21670 extern int callee (int one, int (*two)(int, int), float three);
21671 ~~~~~~^~~~~~~~~~~~~~
21673 otherwise, reuse the start location for the caret location e.g.:
21675 extern int callee (int one, int (*)(int, int), float three);
21676 ^~~~~~~~~~~~~~~~~
21679 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21680 ? declarator->id_loc
21681 : decl_spec_token_start->location);
21682 location_t param_loc = make_location (caret_loc,
21683 decl_spec_token_start->location,
21684 input_location);
21686 return make_parameter_declarator (&decl_specifiers,
21687 declarator,
21688 default_argument,
21689 param_loc,
21690 template_parameter_pack_p);
21693 /* Parse a default argument and return it.
21695 TEMPLATE_PARM_P is true if this is a default argument for a
21696 non-type template parameter. */
21697 static tree
21698 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21700 tree default_argument = NULL_TREE;
21701 bool saved_greater_than_is_operator_p;
21702 bool saved_local_variables_forbidden_p;
21703 bool non_constant_p, is_direct_init;
21705 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21706 set correctly. */
21707 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21708 parser->greater_than_is_operator_p = !template_parm_p;
21709 /* Local variable names (and the `this' keyword) may not
21710 appear in a default argument. */
21711 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21712 parser->local_variables_forbidden_p = true;
21713 /* Parse the assignment-expression. */
21714 if (template_parm_p)
21715 push_deferring_access_checks (dk_no_deferred);
21716 tree saved_class_ptr = NULL_TREE;
21717 tree saved_class_ref = NULL_TREE;
21718 /* The "this" pointer is not valid in a default argument. */
21719 if (cfun)
21721 saved_class_ptr = current_class_ptr;
21722 cp_function_chain->x_current_class_ptr = NULL_TREE;
21723 saved_class_ref = current_class_ref;
21724 cp_function_chain->x_current_class_ref = NULL_TREE;
21726 default_argument
21727 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21728 /* Restore the "this" pointer. */
21729 if (cfun)
21731 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21732 cp_function_chain->x_current_class_ref = saved_class_ref;
21734 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21735 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21736 if (template_parm_p)
21737 pop_deferring_access_checks ();
21738 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21739 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21741 return default_argument;
21744 /* Parse a function-body.
21746 function-body:
21747 compound_statement */
21749 static void
21750 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21752 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21753 ? BCS_TRY_BLOCK : BCS_NORMAL),
21754 true);
21757 /* Parse a ctor-initializer-opt followed by a function-body. Return
21758 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21759 is true we are parsing a function-try-block. */
21761 static void
21762 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21763 bool in_function_try_block)
21765 tree body, list;
21766 const bool check_body_p =
21767 DECL_CONSTRUCTOR_P (current_function_decl)
21768 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21769 tree last = NULL;
21771 /* Begin the function body. */
21772 body = begin_function_body ();
21773 /* Parse the optional ctor-initializer. */
21774 cp_parser_ctor_initializer_opt (parser);
21776 /* If we're parsing a constexpr constructor definition, we need
21777 to check that the constructor body is indeed empty. However,
21778 before we get to cp_parser_function_body lot of junk has been
21779 generated, so we can't just check that we have an empty block.
21780 Rather we take a snapshot of the outermost block, and check whether
21781 cp_parser_function_body changed its state. */
21782 if (check_body_p)
21784 list = cur_stmt_list;
21785 if (STATEMENT_LIST_TAIL (list))
21786 last = STATEMENT_LIST_TAIL (list)->stmt;
21788 /* Parse the function-body. */
21789 cp_parser_function_body (parser, in_function_try_block);
21790 if (check_body_p)
21791 check_constexpr_ctor_body (last, list, /*complain=*/true);
21792 /* Finish the function body. */
21793 finish_function_body (body);
21796 /* Parse an initializer.
21798 initializer:
21799 = initializer-clause
21800 ( expression-list )
21802 Returns an expression representing the initializer. If no
21803 initializer is present, NULL_TREE is returned.
21805 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21806 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21807 set to TRUE if there is no initializer present. If there is an
21808 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21809 is set to true; otherwise it is set to false. */
21811 static tree
21812 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21813 bool* non_constant_p)
21815 cp_token *token;
21816 tree init;
21818 /* Peek at the next token. */
21819 token = cp_lexer_peek_token (parser->lexer);
21821 /* Let our caller know whether or not this initializer was
21822 parenthesized. */
21823 *is_direct_init = (token->type != CPP_EQ);
21824 /* Assume that the initializer is constant. */
21825 *non_constant_p = false;
21827 if (token->type == CPP_EQ)
21829 /* Consume the `='. */
21830 cp_lexer_consume_token (parser->lexer);
21831 /* Parse the initializer-clause. */
21832 init = cp_parser_initializer_clause (parser, non_constant_p);
21834 else if (token->type == CPP_OPEN_PAREN)
21836 vec<tree, va_gc> *vec;
21837 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21838 /*cast_p=*/false,
21839 /*allow_expansion_p=*/true,
21840 non_constant_p);
21841 if (vec == NULL)
21842 return error_mark_node;
21843 init = build_tree_list_vec (vec);
21844 release_tree_vector (vec);
21846 else if (token->type == CPP_OPEN_BRACE)
21848 cp_lexer_set_source_position (parser->lexer);
21849 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21850 init = cp_parser_braced_list (parser, non_constant_p);
21851 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21853 else
21855 /* Anything else is an error. */
21856 cp_parser_error (parser, "expected initializer");
21857 init = error_mark_node;
21860 if (check_for_bare_parameter_packs (init))
21861 init = error_mark_node;
21863 return init;
21866 /* Parse an initializer-clause.
21868 initializer-clause:
21869 assignment-expression
21870 braced-init-list
21872 Returns an expression representing the initializer.
21874 If the `assignment-expression' production is used the value
21875 returned is simply a representation for the expression.
21877 Otherwise, calls cp_parser_braced_list. */
21879 static cp_expr
21880 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21882 cp_expr initializer;
21884 /* Assume the expression is constant. */
21885 *non_constant_p = false;
21887 /* If it is not a `{', then we are looking at an
21888 assignment-expression. */
21889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21891 initializer
21892 = cp_parser_constant_expression (parser,
21893 /*allow_non_constant_p=*/true,
21894 non_constant_p);
21896 else
21897 initializer = cp_parser_braced_list (parser, non_constant_p);
21899 return initializer;
21902 /* Parse a brace-enclosed initializer list.
21904 braced-init-list:
21905 { initializer-list , [opt] }
21906 { designated-initializer-list , [opt] }
21909 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21910 the elements of the initializer-list (or NULL, if the last
21911 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21912 NULL_TREE. There is no way to detect whether or not the optional
21913 trailing `,' was provided. NON_CONSTANT_P is as for
21914 cp_parser_initializer. */
21916 static cp_expr
21917 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21919 tree initializer;
21920 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21922 /* Consume the `{' token. */
21923 matching_braces braces;
21924 braces.consume_open (parser);
21925 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21926 initializer = make_node (CONSTRUCTOR);
21927 /* If it's not a `}', then there is a non-trivial initializer. */
21928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21930 /* Parse the initializer list. */
21931 CONSTRUCTOR_ELTS (initializer)
21932 = cp_parser_initializer_list (parser, non_constant_p);
21933 /* A trailing `,' token is allowed. */
21934 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21935 cp_lexer_consume_token (parser->lexer);
21937 else
21938 *non_constant_p = false;
21939 /* Now, there should be a trailing `}'. */
21940 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21941 braces.require_close (parser);
21942 TREE_TYPE (initializer) = init_list_type_node;
21944 cp_expr result (initializer);
21945 /* Build a location of the form:
21946 { ... }
21947 ^~~~~~~
21948 with caret==start at the open brace, finish at the close brace. */
21949 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21950 result.set_location (combined_loc);
21951 return result;
21954 /* Consume tokens up to, and including, the next non-nested closing `]'.
21955 Returns true iff we found a closing `]'. */
21957 static bool
21958 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21960 unsigned square_depth = 0;
21962 while (true)
21964 cp_token * token = cp_lexer_peek_token (parser->lexer);
21966 switch (token->type)
21968 case CPP_EOF:
21969 case CPP_PRAGMA_EOL:
21970 /* If we've run out of tokens, then there is no closing `]'. */
21971 return false;
21973 case CPP_OPEN_SQUARE:
21974 ++square_depth;
21975 break;
21977 case CPP_CLOSE_SQUARE:
21978 if (!square_depth--)
21980 cp_lexer_consume_token (parser->lexer);
21981 return true;
21983 break;
21985 default:
21986 break;
21989 /* Consume the token. */
21990 cp_lexer_consume_token (parser->lexer);
21994 /* Return true if we are looking at an array-designator, false otherwise. */
21996 static bool
21997 cp_parser_array_designator_p (cp_parser *parser)
21999 /* Consume the `['. */
22000 cp_lexer_consume_token (parser->lexer);
22002 cp_lexer_save_tokens (parser->lexer);
22004 /* Skip tokens until the next token is a closing square bracket.
22005 If we find the closing `]', and the next token is a `=', then
22006 we are looking at an array designator. */
22007 bool array_designator_p
22008 = (cp_parser_skip_to_closing_square_bracket (parser)
22009 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22011 /* Roll back the tokens we skipped. */
22012 cp_lexer_rollback_tokens (parser->lexer);
22014 return array_designator_p;
22017 /* Parse an initializer-list.
22019 initializer-list:
22020 initializer-clause ... [opt]
22021 initializer-list , initializer-clause ... [opt]
22023 C++2A Extension:
22025 designated-initializer-list:
22026 designated-initializer-clause
22027 designated-initializer-list , designated-initializer-clause
22029 designated-initializer-clause:
22030 designator brace-or-equal-initializer
22032 designator:
22033 . identifier
22035 GNU Extension:
22037 initializer-list:
22038 designation initializer-clause ...[opt]
22039 initializer-list , designation initializer-clause ...[opt]
22041 designation:
22042 . identifier =
22043 identifier :
22044 [ constant-expression ] =
22046 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22047 for the initializer. If the INDEX of the elt is non-NULL, it is the
22048 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22049 as for cp_parser_initializer. */
22051 static vec<constructor_elt, va_gc> *
22052 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22054 vec<constructor_elt, va_gc> *v = NULL;
22055 bool first_p = true;
22056 tree first_designator = NULL_TREE;
22058 /* Assume all of the expressions are constant. */
22059 *non_constant_p = false;
22061 /* Parse the rest of the list. */
22062 while (true)
22064 cp_token *token;
22065 tree designator;
22066 tree initializer;
22067 bool clause_non_constant_p;
22068 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22070 /* Handle the C++2A syntax, '. id ='. */
22071 if ((cxx_dialect >= cxx2a
22072 || cp_parser_allow_gnu_extensions_p (parser))
22073 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22074 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22075 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22076 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22077 == CPP_OPEN_BRACE)))
22079 if (cxx_dialect < cxx2a)
22080 pedwarn (loc, OPT_Wpedantic,
22081 "C++ designated initializers only available with "
22082 "-std=c++2a or -std=gnu++2a");
22083 /* Consume the `.'. */
22084 cp_lexer_consume_token (parser->lexer);
22085 /* Consume the identifier. */
22086 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22087 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22088 /* Consume the `='. */
22089 cp_lexer_consume_token (parser->lexer);
22091 /* Also, if the next token is an identifier and the following one is a
22092 colon, we are looking at the GNU designated-initializer
22093 syntax. */
22094 else if (cp_parser_allow_gnu_extensions_p (parser)
22095 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22096 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22097 == CPP_COLON))
22099 /* Warn the user that they are using an extension. */
22100 pedwarn (loc, OPT_Wpedantic,
22101 "ISO C++ does not allow GNU designated initializers");
22102 /* Consume the identifier. */
22103 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22104 /* Consume the `:'. */
22105 cp_lexer_consume_token (parser->lexer);
22107 /* Also handle C99 array designators, '[ const ] ='. */
22108 else if (cp_parser_allow_gnu_extensions_p (parser)
22109 && !c_dialect_objc ()
22110 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22112 /* In C++11, [ could start a lambda-introducer. */
22113 bool non_const = false;
22115 cp_parser_parse_tentatively (parser);
22117 if (!cp_parser_array_designator_p (parser))
22119 cp_parser_simulate_error (parser);
22120 designator = NULL_TREE;
22122 else
22124 designator = cp_parser_constant_expression (parser, true,
22125 &non_const);
22126 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22127 cp_parser_require (parser, CPP_EQ, RT_EQ);
22130 if (!cp_parser_parse_definitely (parser))
22131 designator = NULL_TREE;
22132 else if (non_const
22133 && (!require_potential_rvalue_constant_expression
22134 (designator)))
22135 designator = NULL_TREE;
22136 if (designator)
22137 /* Warn the user that they are using an extension. */
22138 pedwarn (loc, OPT_Wpedantic,
22139 "ISO C++ does not allow C99 designated initializers");
22141 else
22142 designator = NULL_TREE;
22144 if (first_p)
22146 first_designator = designator;
22147 first_p = false;
22149 else if (cxx_dialect >= cxx2a
22150 && first_designator != error_mark_node
22151 && (!first_designator != !designator))
22153 error_at (loc, "either all initializer clauses should be designated "
22154 "or none of them should be");
22155 first_designator = error_mark_node;
22157 else if (cxx_dialect < cxx2a && !first_designator)
22158 first_designator = designator;
22160 /* Parse the initializer. */
22161 initializer = cp_parser_initializer_clause (parser,
22162 &clause_non_constant_p);
22163 /* If any clause is non-constant, so is the entire initializer. */
22164 if (clause_non_constant_p)
22165 *non_constant_p = true;
22167 /* If we have an ellipsis, this is an initializer pack
22168 expansion. */
22169 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22173 /* Consume the `...'. */
22174 cp_lexer_consume_token (parser->lexer);
22176 if (designator && cxx_dialect >= cxx2a)
22177 error_at (loc,
22178 "%<...%> not allowed in designated initializer list");
22180 /* Turn the initializer into an initializer expansion. */
22181 initializer = make_pack_expansion (initializer);
22184 /* Add it to the vector. */
22185 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22187 /* If the next token is not a comma, we have reached the end of
22188 the list. */
22189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22190 break;
22192 /* Peek at the next token. */
22193 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22194 /* If the next token is a `}', then we're still done. An
22195 initializer-clause can have a trailing `,' after the
22196 initializer-list and before the closing `}'. */
22197 if (token->type == CPP_CLOSE_BRACE)
22198 break;
22200 /* Consume the `,' token. */
22201 cp_lexer_consume_token (parser->lexer);
22204 /* The same identifier shall not appear in multiple designators
22205 of a designated-initializer-list. */
22206 if (first_designator)
22208 unsigned int i;
22209 tree designator, val;
22210 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22211 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22213 if (IDENTIFIER_MARKED (designator))
22215 error_at (EXPR_LOC_OR_LOC (val, input_location),
22216 "%<.%s%> designator used multiple times in "
22217 "the same initializer list",
22218 IDENTIFIER_POINTER (designator));
22219 (*v)[i].index = NULL_TREE;
22221 else
22222 IDENTIFIER_MARKED (designator) = 1;
22224 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22225 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22226 IDENTIFIER_MARKED (designator) = 0;
22229 return v;
22232 /* Classes [gram.class] */
22234 /* Parse a class-name.
22236 class-name:
22237 identifier
22238 template-id
22240 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22241 to indicate that names looked up in dependent types should be
22242 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22243 keyword has been used to indicate that the name that appears next
22244 is a template. TAG_TYPE indicates the explicit tag given before
22245 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22246 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22247 is the class being defined in a class-head. If ENUM_OK is TRUE,
22248 enum-names are also accepted.
22250 Returns the TYPE_DECL representing the class. */
22252 static tree
22253 cp_parser_class_name (cp_parser *parser,
22254 bool typename_keyword_p,
22255 bool template_keyword_p,
22256 enum tag_types tag_type,
22257 bool check_dependency_p,
22258 bool class_head_p,
22259 bool is_declaration,
22260 bool enum_ok)
22262 tree decl;
22263 tree scope;
22264 bool typename_p;
22265 cp_token *token;
22266 tree identifier = NULL_TREE;
22268 /* All class-names start with an identifier. */
22269 token = cp_lexer_peek_token (parser->lexer);
22270 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22272 cp_parser_error (parser, "expected class-name");
22273 return error_mark_node;
22276 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22277 to a template-id, so we save it here. */
22278 scope = parser->scope;
22279 if (scope == error_mark_node)
22280 return error_mark_node;
22282 /* Any name names a type if we're following the `typename' keyword
22283 in a qualified name where the enclosing scope is type-dependent. */
22284 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22285 && dependent_type_p (scope));
22286 /* Handle the common case (an identifier, but not a template-id)
22287 efficiently. */
22288 if (token->type == CPP_NAME
22289 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22291 cp_token *identifier_token;
22292 bool ambiguous_p;
22294 /* Look for the identifier. */
22295 identifier_token = cp_lexer_peek_token (parser->lexer);
22296 ambiguous_p = identifier_token->error_reported;
22297 identifier = cp_parser_identifier (parser);
22298 /* If the next token isn't an identifier, we are certainly not
22299 looking at a class-name. */
22300 if (identifier == error_mark_node)
22301 decl = error_mark_node;
22302 /* If we know this is a type-name, there's no need to look it
22303 up. */
22304 else if (typename_p)
22305 decl = identifier;
22306 else
22308 tree ambiguous_decls;
22309 /* If we already know that this lookup is ambiguous, then
22310 we've already issued an error message; there's no reason
22311 to check again. */
22312 if (ambiguous_p)
22314 cp_parser_simulate_error (parser);
22315 return error_mark_node;
22317 /* If the next token is a `::', then the name must be a type
22318 name.
22320 [basic.lookup.qual]
22322 During the lookup for a name preceding the :: scope
22323 resolution operator, object, function, and enumerator
22324 names are ignored. */
22325 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22326 tag_type = scope_type;
22327 /* Look up the name. */
22328 decl = cp_parser_lookup_name (parser, identifier,
22329 tag_type,
22330 /*is_template=*/false,
22331 /*is_namespace=*/false,
22332 check_dependency_p,
22333 &ambiguous_decls,
22334 identifier_token->location);
22335 if (ambiguous_decls)
22337 if (cp_parser_parsing_tentatively (parser))
22338 cp_parser_simulate_error (parser);
22339 return error_mark_node;
22343 else
22345 /* Try a template-id. */
22346 decl = cp_parser_template_id (parser, template_keyword_p,
22347 check_dependency_p,
22348 tag_type,
22349 is_declaration);
22350 if (decl == error_mark_node)
22351 return error_mark_node;
22354 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22356 /* If this is a typename, create a TYPENAME_TYPE. */
22357 if (typename_p && decl != error_mark_node)
22359 decl = make_typename_type (scope, decl, typename_type,
22360 /*complain=*/tf_error);
22361 if (decl != error_mark_node)
22362 decl = TYPE_NAME (decl);
22365 decl = strip_using_decl (decl);
22367 /* Check to see that it is really the name of a class. */
22368 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22369 && identifier_p (TREE_OPERAND (decl, 0))
22370 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22371 /* Situations like this:
22373 template <typename T> struct A {
22374 typename T::template X<int>::I i;
22377 are problematic. Is `T::template X<int>' a class-name? The
22378 standard does not seem to be definitive, but there is no other
22379 valid interpretation of the following `::'. Therefore, those
22380 names are considered class-names. */
22382 decl = make_typename_type (scope, decl, tag_type, tf_error);
22383 if (decl != error_mark_node)
22384 decl = TYPE_NAME (decl);
22386 else if (TREE_CODE (decl) != TYPE_DECL
22387 || TREE_TYPE (decl) == error_mark_node
22388 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22389 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22390 /* In Objective-C 2.0, a classname followed by '.' starts a
22391 dot-syntax expression, and it's not a type-name. */
22392 || (c_dialect_objc ()
22393 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22394 && objc_is_class_name (decl)))
22395 decl = error_mark_node;
22397 if (decl == error_mark_node)
22398 cp_parser_error (parser, "expected class-name");
22399 else if (identifier && !parser->scope)
22400 maybe_note_name_used_in_class (identifier, decl);
22402 return decl;
22405 /* Parse a class-specifier.
22407 class-specifier:
22408 class-head { member-specification [opt] }
22410 Returns the TREE_TYPE representing the class. */
22412 static tree
22413 cp_parser_class_specifier_1 (cp_parser* parser)
22415 tree type;
22416 tree attributes = NULL_TREE;
22417 bool nested_name_specifier_p;
22418 unsigned saved_num_template_parameter_lists;
22419 bool saved_in_function_body;
22420 unsigned char in_statement;
22421 bool in_switch_statement_p;
22422 bool saved_in_unbraced_linkage_specification_p;
22423 tree old_scope = NULL_TREE;
22424 tree scope = NULL_TREE;
22425 cp_token *closing_brace;
22427 push_deferring_access_checks (dk_no_deferred);
22429 /* Parse the class-head. */
22430 type = cp_parser_class_head (parser,
22431 &nested_name_specifier_p);
22432 /* If the class-head was a semantic disaster, skip the entire body
22433 of the class. */
22434 if (!type)
22436 cp_parser_skip_to_end_of_block_or_statement (parser);
22437 pop_deferring_access_checks ();
22438 return error_mark_node;
22441 /* Look for the `{'. */
22442 matching_braces braces;
22443 if (!braces.require_open (parser))
22445 pop_deferring_access_checks ();
22446 return error_mark_node;
22449 cp_ensure_no_omp_declare_simd (parser);
22450 cp_ensure_no_oacc_routine (parser);
22452 /* Issue an error message if type-definitions are forbidden here. */
22453 cp_parser_check_type_definition (parser);
22454 /* Remember that we are defining one more class. */
22455 ++parser->num_classes_being_defined;
22456 /* Inside the class, surrounding template-parameter-lists do not
22457 apply. */
22458 saved_num_template_parameter_lists
22459 = parser->num_template_parameter_lists;
22460 parser->num_template_parameter_lists = 0;
22461 /* We are not in a function body. */
22462 saved_in_function_body = parser->in_function_body;
22463 parser->in_function_body = false;
22464 /* Or in a loop. */
22465 in_statement = parser->in_statement;
22466 parser->in_statement = 0;
22467 /* Or in a switch. */
22468 in_switch_statement_p = parser->in_switch_statement_p;
22469 parser->in_switch_statement_p = false;
22470 /* We are not immediately inside an extern "lang" block. */
22471 saved_in_unbraced_linkage_specification_p
22472 = parser->in_unbraced_linkage_specification_p;
22473 parser->in_unbraced_linkage_specification_p = false;
22475 // Associate constraints with the type.
22476 if (flag_concepts)
22477 type = associate_classtype_constraints (type);
22479 /* Start the class. */
22480 if (nested_name_specifier_p)
22482 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22483 old_scope = push_inner_scope (scope);
22485 type = begin_class_definition (type);
22487 if (type == error_mark_node)
22488 /* If the type is erroneous, skip the entire body of the class. */
22489 cp_parser_skip_to_closing_brace (parser);
22490 else
22491 /* Parse the member-specification. */
22492 cp_parser_member_specification_opt (parser);
22494 /* Look for the trailing `}'. */
22495 closing_brace = braces.require_close (parser);
22496 /* Look for trailing attributes to apply to this class. */
22497 if (cp_parser_allow_gnu_extensions_p (parser))
22498 attributes = cp_parser_gnu_attributes_opt (parser);
22499 if (type != error_mark_node)
22500 type = finish_struct (type, attributes);
22501 if (nested_name_specifier_p)
22502 pop_inner_scope (old_scope, scope);
22504 /* We've finished a type definition. Check for the common syntax
22505 error of forgetting a semicolon after the definition. We need to
22506 be careful, as we can't just check for not-a-semicolon and be done
22507 with it; the user might have typed:
22509 class X { } c = ...;
22510 class X { } *p = ...;
22512 and so forth. Instead, enumerate all the possible tokens that
22513 might follow this production; if we don't see one of them, then
22514 complain and silently insert the semicolon. */
22516 cp_token *token = cp_lexer_peek_token (parser->lexer);
22517 bool want_semicolon = true;
22519 if (cp_next_tokens_can_be_std_attribute_p (parser))
22520 /* Don't try to parse c++11 attributes here. As per the
22521 grammar, that should be a task for
22522 cp_parser_decl_specifier_seq. */
22523 want_semicolon = false;
22525 switch (token->type)
22527 case CPP_NAME:
22528 case CPP_SEMICOLON:
22529 case CPP_MULT:
22530 case CPP_AND:
22531 case CPP_OPEN_PAREN:
22532 case CPP_CLOSE_PAREN:
22533 case CPP_COMMA:
22534 want_semicolon = false;
22535 break;
22537 /* While it's legal for type qualifiers and storage class
22538 specifiers to follow type definitions in the grammar, only
22539 compiler testsuites contain code like that. Assume that if
22540 we see such code, then what we're really seeing is a case
22541 like:
22543 class X { }
22544 const <type> var = ...;
22548 class Y { }
22549 static <type> func (...) ...
22551 i.e. the qualifier or specifier applies to the next
22552 declaration. To do so, however, we need to look ahead one
22553 more token to see if *that* token is a type specifier.
22555 This code could be improved to handle:
22557 class Z { }
22558 static const <type> var = ...; */
22559 case CPP_KEYWORD:
22560 if (keyword_is_decl_specifier (token->keyword))
22562 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22564 /* Handling user-defined types here would be nice, but very
22565 tricky. */
22566 want_semicolon
22567 = (lookahead->type == CPP_KEYWORD
22568 && keyword_begins_type_specifier (lookahead->keyword));
22570 break;
22571 default:
22572 break;
22575 /* If we don't have a type, then something is very wrong and we
22576 shouldn't try to do anything clever. Likewise for not seeing the
22577 closing brace. */
22578 if (closing_brace && TYPE_P (type) && want_semicolon)
22580 /* Locate the closing brace. */
22581 cp_token_position prev
22582 = cp_lexer_previous_token_position (parser->lexer);
22583 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22584 location_t loc = prev_token->location;
22586 /* We want to suggest insertion of a ';' immediately *after* the
22587 closing brace, so, if we can, offset the location by 1 column. */
22588 location_t next_loc = loc;
22589 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22590 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22592 rich_location richloc (line_table, next_loc);
22594 /* If we successfully offset the location, suggest the fix-it. */
22595 if (next_loc != loc)
22596 richloc.add_fixit_insert_before (next_loc, ";");
22598 if (CLASSTYPE_DECLARED_CLASS (type))
22599 error_at (&richloc,
22600 "expected %<;%> after class definition");
22601 else if (TREE_CODE (type) == RECORD_TYPE)
22602 error_at (&richloc,
22603 "expected %<;%> after struct definition");
22604 else if (TREE_CODE (type) == UNION_TYPE)
22605 error_at (&richloc,
22606 "expected %<;%> after union definition");
22607 else
22608 gcc_unreachable ();
22610 /* Unget one token and smash it to look as though we encountered
22611 a semicolon in the input stream. */
22612 cp_lexer_set_token_position (parser->lexer, prev);
22613 token = cp_lexer_peek_token (parser->lexer);
22614 token->type = CPP_SEMICOLON;
22615 token->keyword = RID_MAX;
22619 /* If this class is not itself within the scope of another class,
22620 then we need to parse the bodies of all of the queued function
22621 definitions. Note that the queued functions defined in a class
22622 are not always processed immediately following the
22623 class-specifier for that class. Consider:
22625 struct A {
22626 struct B { void f() { sizeof (A); } };
22629 If `f' were processed before the processing of `A' were
22630 completed, there would be no way to compute the size of `A'.
22631 Note that the nesting we are interested in here is lexical --
22632 not the semantic nesting given by TYPE_CONTEXT. In particular,
22633 for:
22635 struct A { struct B; };
22636 struct A::B { void f() { } };
22638 there is no need to delay the parsing of `A::B::f'. */
22639 if (--parser->num_classes_being_defined == 0)
22641 tree decl;
22642 tree class_type = NULL_TREE;
22643 tree pushed_scope = NULL_TREE;
22644 unsigned ix;
22645 cp_default_arg_entry *e;
22646 tree save_ccp, save_ccr;
22648 /* In a first pass, parse default arguments to the functions.
22649 Then, in a second pass, parse the bodies of the functions.
22650 This two-phased approach handles cases like:
22652 struct S {
22653 void f() { g(); }
22654 void g(int i = 3);
22658 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22660 decl = e->decl;
22661 /* If there are default arguments that have not yet been processed,
22662 take care of them now. */
22663 if (class_type != e->class_type)
22665 if (pushed_scope)
22666 pop_scope (pushed_scope);
22667 class_type = e->class_type;
22668 pushed_scope = push_scope (class_type);
22670 /* Make sure that any template parameters are in scope. */
22671 maybe_begin_member_template_processing (decl);
22672 /* Parse the default argument expressions. */
22673 cp_parser_late_parsing_default_args (parser, decl);
22674 /* Remove any template parameters from the symbol table. */
22675 maybe_end_member_template_processing ();
22677 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22678 /* Now parse any NSDMIs. */
22679 save_ccp = current_class_ptr;
22680 save_ccr = current_class_ref;
22681 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22683 if (class_type != DECL_CONTEXT (decl))
22685 if (pushed_scope)
22686 pop_scope (pushed_scope);
22687 class_type = DECL_CONTEXT (decl);
22688 pushed_scope = push_scope (class_type);
22690 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22691 cp_parser_late_parsing_nsdmi (parser, decl);
22693 vec_safe_truncate (unparsed_nsdmis, 0);
22694 current_class_ptr = save_ccp;
22695 current_class_ref = save_ccr;
22696 if (pushed_scope)
22697 pop_scope (pushed_scope);
22699 /* Now do some post-NSDMI bookkeeping. */
22700 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22701 after_nsdmi_defaulted_late_checks (class_type);
22702 vec_safe_truncate (unparsed_classes, 0);
22703 after_nsdmi_defaulted_late_checks (type);
22705 /* Now parse the body of the functions. */
22706 if (flag_openmp)
22708 /* OpenMP UDRs need to be parsed before all other functions. */
22709 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22710 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22711 cp_parser_late_parsing_for_member (parser, decl);
22712 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22713 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22714 cp_parser_late_parsing_for_member (parser, decl);
22716 else
22717 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22718 cp_parser_late_parsing_for_member (parser, decl);
22719 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22721 else
22722 vec_safe_push (unparsed_classes, type);
22724 /* Put back any saved access checks. */
22725 pop_deferring_access_checks ();
22727 /* Restore saved state. */
22728 parser->in_switch_statement_p = in_switch_statement_p;
22729 parser->in_statement = in_statement;
22730 parser->in_function_body = saved_in_function_body;
22731 parser->num_template_parameter_lists
22732 = saved_num_template_parameter_lists;
22733 parser->in_unbraced_linkage_specification_p
22734 = saved_in_unbraced_linkage_specification_p;
22736 return type;
22739 static tree
22740 cp_parser_class_specifier (cp_parser* parser)
22742 tree ret;
22743 timevar_push (TV_PARSE_STRUCT);
22744 ret = cp_parser_class_specifier_1 (parser);
22745 timevar_pop (TV_PARSE_STRUCT);
22746 return ret;
22749 /* Parse a class-head.
22751 class-head:
22752 class-key identifier [opt] base-clause [opt]
22753 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22754 class-key nested-name-specifier [opt] template-id
22755 base-clause [opt]
22757 class-virt-specifier:
22758 final
22760 GNU Extensions:
22761 class-key attributes identifier [opt] base-clause [opt]
22762 class-key attributes nested-name-specifier identifier base-clause [opt]
22763 class-key attributes nested-name-specifier [opt] template-id
22764 base-clause [opt]
22766 Upon return BASES is initialized to the list of base classes (or
22767 NULL, if there are none) in the same form returned by
22768 cp_parser_base_clause.
22770 Returns the TYPE of the indicated class. Sets
22771 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22772 involving a nested-name-specifier was used, and FALSE otherwise.
22774 Returns error_mark_node if this is not a class-head.
22776 Returns NULL_TREE if the class-head is syntactically valid, but
22777 semantically invalid in a way that means we should skip the entire
22778 body of the class. */
22780 static tree
22781 cp_parser_class_head (cp_parser* parser,
22782 bool* nested_name_specifier_p)
22784 tree nested_name_specifier;
22785 enum tag_types class_key;
22786 tree id = NULL_TREE;
22787 tree type = NULL_TREE;
22788 tree attributes;
22789 tree bases;
22790 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22791 bool template_id_p = false;
22792 bool qualified_p = false;
22793 bool invalid_nested_name_p = false;
22794 bool invalid_explicit_specialization_p = false;
22795 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22796 tree pushed_scope = NULL_TREE;
22797 unsigned num_templates;
22798 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22799 /* Assume no nested-name-specifier will be present. */
22800 *nested_name_specifier_p = false;
22801 /* Assume no template parameter lists will be used in defining the
22802 type. */
22803 num_templates = 0;
22804 parser->colon_corrects_to_scope_p = false;
22806 /* Look for the class-key. */
22807 class_key = cp_parser_class_key (parser);
22808 if (class_key == none_type)
22809 return error_mark_node;
22811 location_t class_head_start_location = input_location;
22813 /* Parse the attributes. */
22814 attributes = cp_parser_attributes_opt (parser);
22816 /* If the next token is `::', that is invalid -- but sometimes
22817 people do try to write:
22819 struct ::S {};
22821 Handle this gracefully by accepting the extra qualifier, and then
22822 issuing an error about it later if this really is a
22823 class-head. If it turns out just to be an elaborated type
22824 specifier, remain silent. */
22825 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22826 qualified_p = true;
22828 push_deferring_access_checks (dk_no_check);
22830 /* Determine the name of the class. Begin by looking for an
22831 optional nested-name-specifier. */
22832 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22833 nested_name_specifier
22834 = cp_parser_nested_name_specifier_opt (parser,
22835 /*typename_keyword_p=*/false,
22836 /*check_dependency_p=*/false,
22837 /*type_p=*/true,
22838 /*is_declaration=*/false);
22839 /* If there was a nested-name-specifier, then there *must* be an
22840 identifier. */
22842 cp_token *bad_template_keyword = NULL;
22844 if (nested_name_specifier)
22846 type_start_token = cp_lexer_peek_token (parser->lexer);
22847 /* Although the grammar says `identifier', it really means
22848 `class-name' or `template-name'. You are only allowed to
22849 define a class that has already been declared with this
22850 syntax.
22852 The proposed resolution for Core Issue 180 says that wherever
22853 you see `class T::X' you should treat `X' as a type-name.
22855 It is OK to define an inaccessible class; for example:
22857 class A { class B; };
22858 class A::B {};
22860 We do not know if we will see a class-name, or a
22861 template-name. We look for a class-name first, in case the
22862 class-name is a template-id; if we looked for the
22863 template-name first we would stop after the template-name. */
22864 cp_parser_parse_tentatively (parser);
22865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22866 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22867 type = cp_parser_class_name (parser,
22868 /*typename_keyword_p=*/false,
22869 /*template_keyword_p=*/false,
22870 class_type,
22871 /*check_dependency_p=*/false,
22872 /*class_head_p=*/true,
22873 /*is_declaration=*/false);
22874 /* If that didn't work, ignore the nested-name-specifier. */
22875 if (!cp_parser_parse_definitely (parser))
22877 invalid_nested_name_p = true;
22878 type_start_token = cp_lexer_peek_token (parser->lexer);
22879 id = cp_parser_identifier (parser);
22880 if (id == error_mark_node)
22881 id = NULL_TREE;
22883 /* If we could not find a corresponding TYPE, treat this
22884 declaration like an unqualified declaration. */
22885 if (type == error_mark_node)
22886 nested_name_specifier = NULL_TREE;
22887 /* Otherwise, count the number of templates used in TYPE and its
22888 containing scopes. */
22889 else
22891 tree scope;
22893 for (scope = TREE_TYPE (type);
22894 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22895 scope = get_containing_scope (scope))
22896 if (TYPE_P (scope)
22897 && CLASS_TYPE_P (scope)
22898 && CLASSTYPE_TEMPLATE_INFO (scope)
22899 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22900 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22901 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22902 ++num_templates;
22905 /* Otherwise, the identifier is optional. */
22906 else
22908 /* We don't know whether what comes next is a template-id,
22909 an identifier, or nothing at all. */
22910 cp_parser_parse_tentatively (parser);
22911 /* Check for a template-id. */
22912 type_start_token = cp_lexer_peek_token (parser->lexer);
22913 id = cp_parser_template_id (parser,
22914 /*template_keyword_p=*/false,
22915 /*check_dependency_p=*/true,
22916 class_key,
22917 /*is_declaration=*/true);
22918 /* If that didn't work, it could still be an identifier. */
22919 if (!cp_parser_parse_definitely (parser))
22921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22923 type_start_token = cp_lexer_peek_token (parser->lexer);
22924 id = cp_parser_identifier (parser);
22926 else
22927 id = NULL_TREE;
22929 else
22931 template_id_p = true;
22932 ++num_templates;
22936 pop_deferring_access_checks ();
22938 if (id)
22940 cp_parser_check_for_invalid_template_id (parser, id,
22941 class_key,
22942 type_start_token->location);
22944 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22946 /* If it's not a `:' or a `{' then we can't really be looking at a
22947 class-head, since a class-head only appears as part of a
22948 class-specifier. We have to detect this situation before calling
22949 xref_tag, since that has irreversible side-effects. */
22950 if (!cp_parser_next_token_starts_class_definition_p (parser))
22952 cp_parser_error (parser, "expected %<{%> or %<:%>");
22953 type = error_mark_node;
22954 goto out;
22957 /* At this point, we're going ahead with the class-specifier, even
22958 if some other problem occurs. */
22959 cp_parser_commit_to_tentative_parse (parser);
22960 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22962 cp_parser_error (parser,
22963 "cannot specify %<override%> for a class");
22964 type = error_mark_node;
22965 goto out;
22967 /* Issue the error about the overly-qualified name now. */
22968 if (qualified_p)
22970 cp_parser_error (parser,
22971 "global qualification of class name is invalid");
22972 type = error_mark_node;
22973 goto out;
22975 else if (invalid_nested_name_p)
22977 cp_parser_error (parser,
22978 "qualified name does not name a class");
22979 type = error_mark_node;
22980 goto out;
22982 else if (nested_name_specifier)
22984 tree scope;
22986 if (bad_template_keyword)
22987 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22988 keyword template shall not appear at the top level. */
22989 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22990 "keyword %<template%> not allowed in class-head-name");
22992 /* Reject typedef-names in class heads. */
22993 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22995 error_at (type_start_token->location,
22996 "invalid class name in declaration of %qD",
22997 type);
22998 type = NULL_TREE;
22999 goto done;
23002 /* Figure out in what scope the declaration is being placed. */
23003 scope = current_scope ();
23004 /* If that scope does not contain the scope in which the
23005 class was originally declared, the program is invalid. */
23006 if (scope && !is_ancestor (scope, nested_name_specifier))
23008 if (at_namespace_scope_p ())
23009 error_at (type_start_token->location,
23010 "declaration of %qD in namespace %qD which does not "
23011 "enclose %qD",
23012 type, scope, nested_name_specifier);
23013 else
23014 error_at (type_start_token->location,
23015 "declaration of %qD in %qD which does not enclose %qD",
23016 type, scope, nested_name_specifier);
23017 type = NULL_TREE;
23018 goto done;
23020 /* [dcl.meaning]
23022 A declarator-id shall not be qualified except for the
23023 definition of a ... nested class outside of its class
23024 ... [or] the definition or explicit instantiation of a
23025 class member of a namespace outside of its namespace. */
23026 if (scope == nested_name_specifier)
23028 permerror (nested_name_specifier_token_start->location,
23029 "extra qualification not allowed");
23030 nested_name_specifier = NULL_TREE;
23031 num_templates = 0;
23034 /* An explicit-specialization must be preceded by "template <>". If
23035 it is not, try to recover gracefully. */
23036 if (at_namespace_scope_p ()
23037 && parser->num_template_parameter_lists == 0
23038 && !processing_template_parmlist
23039 && template_id_p)
23041 /* Build a location of this form:
23042 struct typename <ARGS>
23043 ^~~~~~~~~~~~~~~~~~~~~~
23044 with caret==start at the start token, and
23045 finishing at the end of the type. */
23046 location_t reported_loc
23047 = make_location (class_head_start_location,
23048 class_head_start_location,
23049 get_finish (type_start_token->location));
23050 rich_location richloc (line_table, reported_loc);
23051 richloc.add_fixit_insert_before (class_head_start_location,
23052 "template <> ");
23053 error_at (&richloc,
23054 "an explicit specialization must be preceded by"
23055 " %<template <>%>");
23056 invalid_explicit_specialization_p = true;
23057 /* Take the same action that would have been taken by
23058 cp_parser_explicit_specialization. */
23059 ++parser->num_template_parameter_lists;
23060 begin_specialization ();
23062 /* There must be no "return" statements between this point and the
23063 end of this function; set "type "to the correct return value and
23064 use "goto done;" to return. */
23065 /* Make sure that the right number of template parameters were
23066 present. */
23067 if (!cp_parser_check_template_parameters (parser, num_templates,
23068 type_start_token->location,
23069 /*declarator=*/NULL))
23071 /* If something went wrong, there is no point in even trying to
23072 process the class-definition. */
23073 type = NULL_TREE;
23074 goto done;
23077 /* Look up the type. */
23078 if (template_id_p)
23080 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23081 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23082 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23084 error_at (type_start_token->location,
23085 "function template %qD redeclared as a class template", id);
23086 type = error_mark_node;
23088 else
23090 type = TREE_TYPE (id);
23091 type = maybe_process_partial_specialization (type);
23093 /* Check the scope while we still know whether or not we had a
23094 nested-name-specifier. */
23095 if (type != error_mark_node)
23096 check_unqualified_spec_or_inst (type, type_start_token->location);
23098 if (nested_name_specifier)
23099 pushed_scope = push_scope (nested_name_specifier);
23101 else if (nested_name_specifier)
23103 tree class_type;
23105 /* Given:
23107 template <typename T> struct S { struct T };
23108 template <typename T> struct S<T>::T { };
23110 we will get a TYPENAME_TYPE when processing the definition of
23111 `S::T'. We need to resolve it to the actual type before we
23112 try to define it. */
23113 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23115 class_type = resolve_typename_type (TREE_TYPE (type),
23116 /*only_current_p=*/false);
23117 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23118 type = TYPE_NAME (class_type);
23119 else
23121 cp_parser_error (parser, "could not resolve typename type");
23122 type = error_mark_node;
23126 if (maybe_process_partial_specialization (TREE_TYPE (type))
23127 == error_mark_node)
23129 type = NULL_TREE;
23130 goto done;
23133 class_type = current_class_type;
23134 /* Enter the scope indicated by the nested-name-specifier. */
23135 pushed_scope = push_scope (nested_name_specifier);
23136 /* Get the canonical version of this type. */
23137 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23138 /* Call push_template_decl if it seems like we should be defining a
23139 template either from the template headers or the type we're
23140 defining, so that we diagnose both extra and missing headers. */
23141 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23142 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23143 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23145 type = push_template_decl (type);
23146 if (type == error_mark_node)
23148 type = NULL_TREE;
23149 goto done;
23153 type = TREE_TYPE (type);
23154 *nested_name_specifier_p = true;
23156 else /* The name is not a nested name. */
23158 /* If the class was unnamed, create a dummy name. */
23159 if (!id)
23160 id = make_anon_name ();
23161 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23162 ? ts_within_enclosing_non_class
23163 : ts_current);
23164 type = xref_tag (class_key, id, tag_scope,
23165 parser->num_template_parameter_lists);
23168 /* Indicate whether this class was declared as a `class' or as a
23169 `struct'. */
23170 if (TREE_CODE (type) == RECORD_TYPE)
23171 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23172 cp_parser_check_class_key (class_key, type);
23174 /* If this type was already complete, and we see another definition,
23175 that's an error. */
23176 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23178 error_at (type_start_token->location, "redefinition of %q#T",
23179 type);
23180 inform (location_of (type), "previous definition of %q#T",
23181 type);
23182 type = NULL_TREE;
23183 goto done;
23185 else if (type == error_mark_node)
23186 type = NULL_TREE;
23188 if (type)
23190 /* Apply attributes now, before any use of the class as a template
23191 argument in its base list. */
23192 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23193 fixup_attribute_variants (type);
23196 /* We will have entered the scope containing the class; the names of
23197 base classes should be looked up in that context. For example:
23199 struct A { struct B {}; struct C; };
23200 struct A::C : B {};
23202 is valid. */
23204 /* Get the list of base-classes, if there is one. */
23205 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23207 /* PR59482: enter the class scope so that base-specifiers are looked
23208 up correctly. */
23209 if (type)
23210 pushclass (type);
23211 bases = cp_parser_base_clause (parser);
23212 /* PR59482: get out of the previously pushed class scope so that the
23213 subsequent pops pop the right thing. */
23214 if (type)
23215 popclass ();
23217 else
23218 bases = NULL_TREE;
23220 /* If we're really defining a class, process the base classes.
23221 If they're invalid, fail. */
23222 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23223 xref_basetypes (type, bases);
23225 done:
23226 /* Leave the scope given by the nested-name-specifier. We will
23227 enter the class scope itself while processing the members. */
23228 if (pushed_scope)
23229 pop_scope (pushed_scope);
23231 if (invalid_explicit_specialization_p)
23233 end_specialization ();
23234 --parser->num_template_parameter_lists;
23237 if (type)
23238 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23239 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23240 CLASSTYPE_FINAL (type) = 1;
23241 out:
23242 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23243 return type;
23246 /* Parse a class-key.
23248 class-key:
23249 class
23250 struct
23251 union
23253 Returns the kind of class-key specified, or none_type to indicate
23254 error. */
23256 static enum tag_types
23257 cp_parser_class_key (cp_parser* parser)
23259 cp_token *token;
23260 enum tag_types tag_type;
23262 /* Look for the class-key. */
23263 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23264 if (!token)
23265 return none_type;
23267 /* Check to see if the TOKEN is a class-key. */
23268 tag_type = cp_parser_token_is_class_key (token);
23269 if (!tag_type)
23270 cp_parser_error (parser, "expected class-key");
23271 return tag_type;
23274 /* Parse a type-parameter-key.
23276 type-parameter-key:
23277 class
23278 typename
23281 static void
23282 cp_parser_type_parameter_key (cp_parser* parser)
23284 /* Look for the type-parameter-key. */
23285 enum tag_types tag_type = none_type;
23286 cp_token *token = cp_lexer_peek_token (parser->lexer);
23287 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23289 cp_lexer_consume_token (parser->lexer);
23290 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23291 /* typename is not allowed in a template template parameter
23292 by the standard until C++17. */
23293 pedwarn (token->location, OPT_Wpedantic,
23294 "ISO C++ forbids typename key in template template parameter;"
23295 " use -std=c++17 or -std=gnu++17");
23297 else
23298 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23300 return;
23303 /* Parse an (optional) member-specification.
23305 member-specification:
23306 member-declaration member-specification [opt]
23307 access-specifier : member-specification [opt] */
23309 static void
23310 cp_parser_member_specification_opt (cp_parser* parser)
23312 while (true)
23314 cp_token *token;
23315 enum rid keyword;
23317 /* Peek at the next token. */
23318 token = cp_lexer_peek_token (parser->lexer);
23319 /* If it's a `}', or EOF then we've seen all the members. */
23320 if (token->type == CPP_CLOSE_BRACE
23321 || token->type == CPP_EOF
23322 || token->type == CPP_PRAGMA_EOL)
23323 break;
23325 /* See if this token is a keyword. */
23326 keyword = token->keyword;
23327 switch (keyword)
23329 case RID_PUBLIC:
23330 case RID_PROTECTED:
23331 case RID_PRIVATE:
23332 /* Consume the access-specifier. */
23333 cp_lexer_consume_token (parser->lexer);
23334 /* Remember which access-specifier is active. */
23335 current_access_specifier = token->u.value;
23336 /* Look for the `:'. */
23337 cp_parser_require (parser, CPP_COLON, RT_COLON);
23338 break;
23340 default:
23341 /* Accept #pragmas at class scope. */
23342 if (token->type == CPP_PRAGMA)
23344 cp_parser_pragma (parser, pragma_member, NULL);
23345 break;
23348 /* Otherwise, the next construction must be a
23349 member-declaration. */
23350 cp_parser_member_declaration (parser);
23355 /* Parse a member-declaration.
23357 member-declaration:
23358 decl-specifier-seq [opt] member-declarator-list [opt] ;
23359 function-definition ; [opt]
23360 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23361 using-declaration
23362 template-declaration
23363 alias-declaration
23365 member-declarator-list:
23366 member-declarator
23367 member-declarator-list , member-declarator
23369 member-declarator:
23370 declarator pure-specifier [opt]
23371 declarator constant-initializer [opt]
23372 identifier [opt] : constant-expression
23374 GNU Extensions:
23376 member-declaration:
23377 __extension__ member-declaration
23379 member-declarator:
23380 declarator attributes [opt] pure-specifier [opt]
23381 declarator attributes [opt] constant-initializer [opt]
23382 identifier [opt] attributes [opt] : constant-expression
23384 C++0x Extensions:
23386 member-declaration:
23387 static_assert-declaration */
23389 static void
23390 cp_parser_member_declaration (cp_parser* parser)
23392 cp_decl_specifier_seq decl_specifiers;
23393 tree prefix_attributes;
23394 tree decl;
23395 int declares_class_or_enum;
23396 bool friend_p;
23397 cp_token *token = NULL;
23398 cp_token *decl_spec_token_start = NULL;
23399 cp_token *initializer_token_start = NULL;
23400 int saved_pedantic;
23401 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23403 /* Check for the `__extension__' keyword. */
23404 if (cp_parser_extension_opt (parser, &saved_pedantic))
23406 /* Recurse. */
23407 cp_parser_member_declaration (parser);
23408 /* Restore the old value of the PEDANTIC flag. */
23409 pedantic = saved_pedantic;
23411 return;
23414 /* Check for a template-declaration. */
23415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23417 /* An explicit specialization here is an error condition, and we
23418 expect the specialization handler to detect and report this. */
23419 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23420 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23421 cp_parser_explicit_specialization (parser);
23422 else
23423 cp_parser_template_declaration (parser, /*member_p=*/true);
23425 return;
23427 /* Check for a template introduction. */
23428 else if (cp_parser_template_declaration_after_export (parser, true))
23429 return;
23431 /* Check for a using-declaration. */
23432 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23434 if (cxx_dialect < cxx11)
23436 /* Parse the using-declaration. */
23437 cp_parser_using_declaration (parser,
23438 /*access_declaration_p=*/false);
23439 return;
23441 else
23443 tree decl;
23444 bool alias_decl_expected;
23445 cp_parser_parse_tentatively (parser);
23446 decl = cp_parser_alias_declaration (parser);
23447 /* Note that if we actually see the '=' token after the
23448 identifier, cp_parser_alias_declaration commits the
23449 tentative parse. In that case, we really expect an
23450 alias-declaration. Otherwise, we expect a using
23451 declaration. */
23452 alias_decl_expected =
23453 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23454 cp_parser_parse_definitely (parser);
23456 if (alias_decl_expected)
23457 finish_member_declaration (decl);
23458 else
23459 cp_parser_using_declaration (parser,
23460 /*access_declaration_p=*/false);
23461 return;
23465 /* Check for @defs. */
23466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23468 tree ivar, member;
23469 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23470 ivar = ivar_chains;
23471 while (ivar)
23473 member = ivar;
23474 ivar = TREE_CHAIN (member);
23475 TREE_CHAIN (member) = NULL_TREE;
23476 finish_member_declaration (member);
23478 return;
23481 /* If the next token is `static_assert' we have a static assertion. */
23482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23484 cp_parser_static_assert (parser, /*member_p=*/true);
23485 return;
23488 parser->colon_corrects_to_scope_p = false;
23490 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23491 goto out;
23493 /* Parse the decl-specifier-seq. */
23494 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23495 cp_parser_decl_specifier_seq (parser,
23496 CP_PARSER_FLAGS_OPTIONAL,
23497 &decl_specifiers,
23498 &declares_class_or_enum);
23499 /* Check for an invalid type-name. */
23500 if (!decl_specifiers.any_type_specifiers_p
23501 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23502 goto out;
23503 /* If there is no declarator, then the decl-specifier-seq should
23504 specify a type. */
23505 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23507 /* If there was no decl-specifier-seq, and the next token is a
23508 `;', then we have something like:
23510 struct S { ; };
23512 [class.mem]
23514 Each member-declaration shall declare at least one member
23515 name of the class. */
23516 if (!decl_specifiers.any_specifiers_p)
23518 cp_token *token = cp_lexer_peek_token (parser->lexer);
23519 if (!in_system_header_at (token->location))
23521 gcc_rich_location richloc (token->location);
23522 richloc.add_fixit_remove ();
23523 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23526 else
23528 tree type;
23530 /* See if this declaration is a friend. */
23531 friend_p = cp_parser_friend_p (&decl_specifiers);
23532 /* If there were decl-specifiers, check to see if there was
23533 a class-declaration. */
23534 type = check_tag_decl (&decl_specifiers,
23535 /*explicit_type_instantiation_p=*/false);
23536 /* Nested classes have already been added to the class, but
23537 a `friend' needs to be explicitly registered. */
23538 if (friend_p)
23540 /* If the `friend' keyword was present, the friend must
23541 be introduced with a class-key. */
23542 if (!declares_class_or_enum && cxx_dialect < cxx11)
23543 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23544 "in C++03 a class-key must be used "
23545 "when declaring a friend");
23546 /* In this case:
23548 template <typename T> struct A {
23549 friend struct A<T>::B;
23552 A<T>::B will be represented by a TYPENAME_TYPE, and
23553 therefore not recognized by check_tag_decl. */
23554 if (!type)
23556 type = decl_specifiers.type;
23557 if (type && TREE_CODE (type) == TYPE_DECL)
23558 type = TREE_TYPE (type);
23560 if (!type || !TYPE_P (type))
23561 error_at (decl_spec_token_start->location,
23562 "friend declaration does not name a class or "
23563 "function");
23564 else
23565 make_friend_class (current_class_type, type,
23566 /*complain=*/true);
23568 /* If there is no TYPE, an error message will already have
23569 been issued. */
23570 else if (!type || type == error_mark_node)
23572 /* An anonymous aggregate has to be handled specially; such
23573 a declaration really declares a data member (with a
23574 particular type), as opposed to a nested class. */
23575 else if (ANON_AGGR_TYPE_P (type))
23577 /* C++11 9.5/6. */
23578 if (decl_specifiers.storage_class != sc_none)
23579 error_at (decl_spec_token_start->location,
23580 "a storage class on an anonymous aggregate "
23581 "in class scope is not allowed");
23583 /* Remove constructors and such from TYPE, now that we
23584 know it is an anonymous aggregate. */
23585 fixup_anonymous_aggr (type);
23586 /* And make the corresponding data member. */
23587 decl = build_decl (decl_spec_token_start->location,
23588 FIELD_DECL, NULL_TREE, type);
23589 /* Add it to the class. */
23590 finish_member_declaration (decl);
23592 else
23593 cp_parser_check_access_in_redeclaration
23594 (TYPE_NAME (type),
23595 decl_spec_token_start->location);
23598 else
23600 bool assume_semicolon = false;
23602 /* Clear attributes from the decl_specifiers but keep them
23603 around as prefix attributes that apply them to the entity
23604 being declared. */
23605 prefix_attributes = decl_specifiers.attributes;
23606 decl_specifiers.attributes = NULL_TREE;
23608 /* See if these declarations will be friends. */
23609 friend_p = cp_parser_friend_p (&decl_specifiers);
23611 /* Keep going until we hit the `;' at the end of the
23612 declaration. */
23613 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23615 tree attributes = NULL_TREE;
23616 tree first_attribute;
23617 tree initializer;
23618 bool is_bitfld = false;
23619 bool named_bitfld = false;
23621 /* Peek at the next token. */
23622 token = cp_lexer_peek_token (parser->lexer);
23624 /* The following code wants to know early if it is a bit-field
23625 or some other declaration. Attributes can appear before
23626 the `:' token, but are hopefully rare enough that the
23627 simplicity of the tentative lookup pays off. */
23628 if (cp_next_tokens_can_be_attribute_p (parser)
23629 || (token->type == CPP_NAME
23630 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23631 && (named_bitfld = true)))
23633 cp_parser_parse_tentatively (parser);
23634 if (named_bitfld)
23635 cp_lexer_consume_token (parser->lexer);
23636 cp_parser_attributes_opt (parser);
23637 token = cp_lexer_peek_token (parser->lexer);
23638 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23639 cp_parser_abort_tentative_parse (parser);
23642 /* Check for a bitfield declaration. */
23643 if (is_bitfld
23644 || token->type == CPP_COLON
23645 || (token->type == CPP_NAME
23646 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23647 && (named_bitfld = true)))
23649 tree identifier;
23650 tree width;
23651 tree late_attributes = NULL_TREE;
23653 if (named_bitfld)
23654 identifier = cp_parser_identifier (parser);
23655 else
23656 identifier = NULL_TREE;
23658 /* Look for attributes that apply to the bitfield. */
23659 attributes = cp_parser_attributes_opt (parser);
23661 /* Consume the `:' token. */
23662 cp_lexer_consume_token (parser->lexer);
23664 /* Get the width of the bitfield. */
23665 width = cp_parser_constant_expression (parser, false, NULL,
23666 cxx_dialect >= cxx11);
23668 /* In C++2A and as extension for C++11 and above we allow
23669 default member initializers for bit-fields. */
23670 initializer = NULL_TREE;
23671 if (cxx_dialect >= cxx11
23672 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23673 || cp_lexer_next_token_is (parser->lexer,
23674 CPP_OPEN_BRACE)))
23676 location_t loc
23677 = cp_lexer_peek_token (parser->lexer)->location;
23678 if (cxx_dialect < cxx2a
23679 && !in_system_header_at (loc)
23680 && identifier != NULL_TREE)
23681 pedwarn (loc, 0,
23682 "default member initializers for bit-fields "
23683 "only available with -std=c++2a or "
23684 "-std=gnu++2a");
23686 initializer = cp_parser_save_nsdmi (parser);
23687 if (identifier == NULL_TREE)
23689 error_at (loc, "default member initializer for "
23690 "unnamed bit-field");
23691 initializer = NULL_TREE;
23694 else
23696 /* Look for attributes that apply to the bitfield after
23697 the `:' token and width. This is where GCC used to
23698 parse attributes in the past, pedwarn if there is
23699 a std attribute. */
23700 if (cp_next_tokens_can_be_std_attribute_p (parser))
23701 pedwarn (input_location, OPT_Wpedantic,
23702 "ISO C++ allows bit-field attributes only "
23703 "before the %<:%> token");
23705 late_attributes = cp_parser_attributes_opt (parser);
23708 attributes = attr_chainon (attributes, late_attributes);
23710 /* Remember which attributes are prefix attributes and
23711 which are not. */
23712 first_attribute = attributes;
23713 /* Combine the attributes. */
23714 attributes = attr_chainon (prefix_attributes, attributes);
23716 /* Create the bitfield declaration. */
23717 decl = grokbitfield (identifier
23718 ? make_id_declarator (NULL_TREE,
23719 identifier,
23720 sfk_none)
23721 : NULL,
23722 &decl_specifiers,
23723 width, initializer,
23724 attributes);
23726 else
23728 cp_declarator *declarator;
23729 tree asm_specification;
23730 int ctor_dtor_or_conv_p;
23732 /* Parse the declarator. */
23733 declarator
23734 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23735 &ctor_dtor_or_conv_p,
23736 /*parenthesized_p=*/NULL,
23737 /*member_p=*/true,
23738 friend_p);
23740 /* If something went wrong parsing the declarator, make sure
23741 that we at least consume some tokens. */
23742 if (declarator == cp_error_declarator)
23744 /* Skip to the end of the statement. */
23745 cp_parser_skip_to_end_of_statement (parser);
23746 /* If the next token is not a semicolon, that is
23747 probably because we just skipped over the body of
23748 a function. So, we consume a semicolon if
23749 present, but do not issue an error message if it
23750 is not present. */
23751 if (cp_lexer_next_token_is (parser->lexer,
23752 CPP_SEMICOLON))
23753 cp_lexer_consume_token (parser->lexer);
23754 goto out;
23757 if (declares_class_or_enum & 2)
23758 cp_parser_check_for_definition_in_return_type
23759 (declarator, decl_specifiers.type,
23760 decl_specifiers.locations[ds_type_spec]);
23762 /* Look for an asm-specification. */
23763 asm_specification = cp_parser_asm_specification_opt (parser);
23764 /* Look for attributes that apply to the declaration. */
23765 attributes = cp_parser_attributes_opt (parser);
23766 /* Remember which attributes are prefix attributes and
23767 which are not. */
23768 first_attribute = attributes;
23769 /* Combine the attributes. */
23770 attributes = attr_chainon (prefix_attributes, attributes);
23772 /* If it's an `=', then we have a constant-initializer or a
23773 pure-specifier. It is not correct to parse the
23774 initializer before registering the member declaration
23775 since the member declaration should be in scope while
23776 its initializer is processed. However, the rest of the
23777 front end does not yet provide an interface that allows
23778 us to handle this correctly. */
23779 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23781 /* In [class.mem]:
23783 A pure-specifier shall be used only in the declaration of
23784 a virtual function.
23786 A member-declarator can contain a constant-initializer
23787 only if it declares a static member of integral or
23788 enumeration type.
23790 Therefore, if the DECLARATOR is for a function, we look
23791 for a pure-specifier; otherwise, we look for a
23792 constant-initializer. When we call `grokfield', it will
23793 perform more stringent semantics checks. */
23794 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23795 if (function_declarator_p (declarator)
23796 || (decl_specifiers.type
23797 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23798 && declarator->kind == cdk_id
23799 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23800 == FUNCTION_TYPE)))
23801 initializer = cp_parser_pure_specifier (parser);
23802 else if (decl_specifiers.storage_class != sc_static)
23803 initializer = cp_parser_save_nsdmi (parser);
23804 else if (cxx_dialect >= cxx11)
23806 bool nonconst;
23807 /* Don't require a constant rvalue in C++11, since we
23808 might want a reference constant. We'll enforce
23809 constancy later. */
23810 cp_lexer_consume_token (parser->lexer);
23811 /* Parse the initializer. */
23812 initializer = cp_parser_initializer_clause (parser,
23813 &nonconst);
23815 else
23816 /* Parse the initializer. */
23817 initializer = cp_parser_constant_initializer (parser);
23819 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23820 && !function_declarator_p (declarator))
23822 bool x;
23823 if (decl_specifiers.storage_class != sc_static)
23824 initializer = cp_parser_save_nsdmi (parser);
23825 else
23826 initializer = cp_parser_initializer (parser, &x, &x);
23828 /* Otherwise, there is no initializer. */
23829 else
23830 initializer = NULL_TREE;
23832 /* See if we are probably looking at a function
23833 definition. We are certainly not looking at a
23834 member-declarator. Calling `grokfield' has
23835 side-effects, so we must not do it unless we are sure
23836 that we are looking at a member-declarator. */
23837 if (cp_parser_token_starts_function_definition_p
23838 (cp_lexer_peek_token (parser->lexer)))
23840 /* The grammar does not allow a pure-specifier to be
23841 used when a member function is defined. (It is
23842 possible that this fact is an oversight in the
23843 standard, since a pure function may be defined
23844 outside of the class-specifier. */
23845 if (initializer && initializer_token_start)
23846 error_at (initializer_token_start->location,
23847 "pure-specifier on function-definition");
23848 decl = cp_parser_save_member_function_body (parser,
23849 &decl_specifiers,
23850 declarator,
23851 attributes);
23852 if (parser->fully_implicit_function_template_p)
23853 decl = finish_fully_implicit_template (parser, decl);
23854 /* If the member was not a friend, declare it here. */
23855 if (!friend_p)
23856 finish_member_declaration (decl);
23857 /* Peek at the next token. */
23858 token = cp_lexer_peek_token (parser->lexer);
23859 /* If the next token is a semicolon, consume it. */
23860 if (token->type == CPP_SEMICOLON)
23862 location_t semicolon_loc
23863 = cp_lexer_consume_token (parser->lexer)->location;
23864 gcc_rich_location richloc (semicolon_loc);
23865 richloc.add_fixit_remove ();
23866 warning_at (&richloc, OPT_Wextra_semi,
23867 "extra %<;%> after in-class "
23868 "function definition");
23870 goto out;
23872 else
23873 if (declarator->kind == cdk_function)
23874 declarator->id_loc = token->location;
23875 /* Create the declaration. */
23876 decl = grokfield (declarator, &decl_specifiers,
23877 initializer, /*init_const_expr_p=*/true,
23878 asm_specification, attributes);
23879 if (parser->fully_implicit_function_template_p)
23881 if (friend_p)
23882 finish_fully_implicit_template (parser, 0);
23883 else
23884 decl = finish_fully_implicit_template (parser, decl);
23888 cp_finalize_omp_declare_simd (parser, decl);
23889 cp_finalize_oacc_routine (parser, decl, false);
23891 /* Reset PREFIX_ATTRIBUTES. */
23892 if (attributes != error_mark_node)
23894 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23895 attributes = TREE_CHAIN (attributes);
23896 if (attributes)
23897 TREE_CHAIN (attributes) = NULL_TREE;
23900 /* If there is any qualification still in effect, clear it
23901 now; we will be starting fresh with the next declarator. */
23902 parser->scope = NULL_TREE;
23903 parser->qualifying_scope = NULL_TREE;
23904 parser->object_scope = NULL_TREE;
23905 /* If it's a `,', then there are more declarators. */
23906 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23908 cp_lexer_consume_token (parser->lexer);
23909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23911 cp_token *token = cp_lexer_previous_token (parser->lexer);
23912 gcc_rich_location richloc (token->location);
23913 richloc.add_fixit_remove ();
23914 error_at (&richloc, "stray %<,%> at end of "
23915 "member declaration");
23918 /* If the next token isn't a `;', then we have a parse error. */
23919 else if (cp_lexer_next_token_is_not (parser->lexer,
23920 CPP_SEMICOLON))
23922 /* The next token might be a ways away from where the
23923 actual semicolon is missing. Find the previous token
23924 and use that for our error position. */
23925 cp_token *token = cp_lexer_previous_token (parser->lexer);
23926 gcc_rich_location richloc (token->location);
23927 richloc.add_fixit_insert_after (";");
23928 error_at (&richloc, "expected %<;%> at end of "
23929 "member declaration");
23931 /* Assume that the user meant to provide a semicolon. If
23932 we were to cp_parser_skip_to_end_of_statement, we might
23933 skip to a semicolon inside a member function definition
23934 and issue nonsensical error messages. */
23935 assume_semicolon = true;
23938 if (decl)
23940 /* Add DECL to the list of members. */
23941 if (!friend_p
23942 /* Explicitly include, eg, NSDMIs, for better error
23943 recovery (c++/58650). */
23944 || !DECL_DECLARES_FUNCTION_P (decl))
23945 finish_member_declaration (decl);
23947 if (TREE_CODE (decl) == FUNCTION_DECL)
23948 cp_parser_save_default_args (parser, decl);
23949 else if (TREE_CODE (decl) == FIELD_DECL
23950 && DECL_INITIAL (decl))
23951 /* Add DECL to the queue of NSDMI to be parsed later. */
23952 vec_safe_push (unparsed_nsdmis, decl);
23955 if (assume_semicolon)
23956 goto out;
23960 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23961 out:
23962 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23965 /* Parse a pure-specifier.
23967 pure-specifier:
23970 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23971 Otherwise, ERROR_MARK_NODE is returned. */
23973 static tree
23974 cp_parser_pure_specifier (cp_parser* parser)
23976 cp_token *token;
23978 /* Look for the `=' token. */
23979 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23980 return error_mark_node;
23981 /* Look for the `0' token. */
23982 token = cp_lexer_peek_token (parser->lexer);
23984 if (token->type == CPP_EOF
23985 || token->type == CPP_PRAGMA_EOL)
23986 return error_mark_node;
23988 cp_lexer_consume_token (parser->lexer);
23990 /* Accept = default or = delete in c++0x mode. */
23991 if (token->keyword == RID_DEFAULT
23992 || token->keyword == RID_DELETE)
23994 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23995 return token->u.value;
23998 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23999 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24001 cp_parser_error (parser,
24002 "invalid pure specifier (only %<= 0%> is allowed)");
24003 cp_parser_skip_to_end_of_statement (parser);
24004 return error_mark_node;
24006 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24008 error_at (token->location, "templates may not be %<virtual%>");
24009 return error_mark_node;
24012 return integer_zero_node;
24015 /* Parse a constant-initializer.
24017 constant-initializer:
24018 = constant-expression
24020 Returns a representation of the constant-expression. */
24022 static tree
24023 cp_parser_constant_initializer (cp_parser* parser)
24025 /* Look for the `=' token. */
24026 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24027 return error_mark_node;
24029 /* It is invalid to write:
24031 struct S { static const int i = { 7 }; };
24034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24036 cp_parser_error (parser,
24037 "a brace-enclosed initializer is not allowed here");
24038 /* Consume the opening brace. */
24039 matching_braces braces;
24040 braces.consume_open (parser);
24041 /* Skip the initializer. */
24042 cp_parser_skip_to_closing_brace (parser);
24043 /* Look for the trailing `}'. */
24044 braces.require_close (parser);
24046 return error_mark_node;
24049 return cp_parser_constant_expression (parser);
24052 /* Derived classes [gram.class.derived] */
24054 /* Parse a base-clause.
24056 base-clause:
24057 : base-specifier-list
24059 base-specifier-list:
24060 base-specifier ... [opt]
24061 base-specifier-list , base-specifier ... [opt]
24063 Returns a TREE_LIST representing the base-classes, in the order in
24064 which they were declared. The representation of each node is as
24065 described by cp_parser_base_specifier.
24067 In the case that no bases are specified, this function will return
24068 NULL_TREE, not ERROR_MARK_NODE. */
24070 static tree
24071 cp_parser_base_clause (cp_parser* parser)
24073 tree bases = NULL_TREE;
24075 /* Look for the `:' that begins the list. */
24076 cp_parser_require (parser, CPP_COLON, RT_COLON);
24078 /* Scan the base-specifier-list. */
24079 while (true)
24081 cp_token *token;
24082 tree base;
24083 bool pack_expansion_p = false;
24085 /* Look for the base-specifier. */
24086 base = cp_parser_base_specifier (parser);
24087 /* Look for the (optional) ellipsis. */
24088 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24090 /* Consume the `...'. */
24091 cp_lexer_consume_token (parser->lexer);
24093 pack_expansion_p = true;
24096 /* Add BASE to the front of the list. */
24097 if (base && base != error_mark_node)
24099 if (pack_expansion_p)
24100 /* Make this a pack expansion type. */
24101 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24103 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24105 TREE_CHAIN (base) = bases;
24106 bases = base;
24109 /* Peek at the next token. */
24110 token = cp_lexer_peek_token (parser->lexer);
24111 /* If it's not a comma, then the list is complete. */
24112 if (token->type != CPP_COMMA)
24113 break;
24114 /* Consume the `,'. */
24115 cp_lexer_consume_token (parser->lexer);
24118 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24119 base class had a qualified name. However, the next name that
24120 appears is certainly not qualified. */
24121 parser->scope = NULL_TREE;
24122 parser->qualifying_scope = NULL_TREE;
24123 parser->object_scope = NULL_TREE;
24125 return nreverse (bases);
24128 /* Parse a base-specifier.
24130 base-specifier:
24131 :: [opt] nested-name-specifier [opt] class-name
24132 virtual access-specifier [opt] :: [opt] nested-name-specifier
24133 [opt] class-name
24134 access-specifier virtual [opt] :: [opt] nested-name-specifier
24135 [opt] class-name
24137 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24138 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24139 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24140 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24142 static tree
24143 cp_parser_base_specifier (cp_parser* parser)
24145 cp_token *token;
24146 bool done = false;
24147 bool virtual_p = false;
24148 bool duplicate_virtual_error_issued_p = false;
24149 bool duplicate_access_error_issued_p = false;
24150 bool class_scope_p, template_p;
24151 tree access = access_default_node;
24152 tree type;
24154 /* Process the optional `virtual' and `access-specifier'. */
24155 while (!done)
24157 /* Peek at the next token. */
24158 token = cp_lexer_peek_token (parser->lexer);
24159 /* Process `virtual'. */
24160 switch (token->keyword)
24162 case RID_VIRTUAL:
24163 /* If `virtual' appears more than once, issue an error. */
24164 if (virtual_p && !duplicate_virtual_error_issued_p)
24166 cp_parser_error (parser,
24167 "%<virtual%> specified more than once in base-specifier");
24168 duplicate_virtual_error_issued_p = true;
24171 virtual_p = true;
24173 /* Consume the `virtual' token. */
24174 cp_lexer_consume_token (parser->lexer);
24176 break;
24178 case RID_PUBLIC:
24179 case RID_PROTECTED:
24180 case RID_PRIVATE:
24181 /* If more than one access specifier appears, issue an
24182 error. */
24183 if (access != access_default_node
24184 && !duplicate_access_error_issued_p)
24186 cp_parser_error (parser,
24187 "more than one access specifier in base-specifier");
24188 duplicate_access_error_issued_p = true;
24191 access = ridpointers[(int) token->keyword];
24193 /* Consume the access-specifier. */
24194 cp_lexer_consume_token (parser->lexer);
24196 break;
24198 default:
24199 done = true;
24200 break;
24203 /* It is not uncommon to see programs mechanically, erroneously, use
24204 the 'typename' keyword to denote (dependent) qualified types
24205 as base classes. */
24206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24208 token = cp_lexer_peek_token (parser->lexer);
24209 if (!processing_template_decl)
24210 error_at (token->location,
24211 "keyword %<typename%> not allowed outside of templates");
24212 else
24213 error_at (token->location,
24214 "keyword %<typename%> not allowed in this context "
24215 "(the base class is implicitly a type)");
24216 cp_lexer_consume_token (parser->lexer);
24219 /* Look for the optional `::' operator. */
24220 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24221 /* Look for the nested-name-specifier. The simplest way to
24222 implement:
24224 [temp.res]
24226 The keyword `typename' is not permitted in a base-specifier or
24227 mem-initializer; in these contexts a qualified name that
24228 depends on a template-parameter is implicitly assumed to be a
24229 type name.
24231 is to pretend that we have seen the `typename' keyword at this
24232 point. */
24233 cp_parser_nested_name_specifier_opt (parser,
24234 /*typename_keyword_p=*/true,
24235 /*check_dependency_p=*/true,
24236 /*type_p=*/true,
24237 /*is_declaration=*/true);
24238 /* If the base class is given by a qualified name, assume that names
24239 we see are type names or templates, as appropriate. */
24240 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24241 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24243 if (!parser->scope
24244 && cp_lexer_next_token_is_decltype (parser->lexer))
24245 /* DR 950 allows decltype as a base-specifier. */
24246 type = cp_parser_decltype (parser);
24247 else
24249 /* Otherwise, look for the class-name. */
24250 type = cp_parser_class_name (parser,
24251 class_scope_p,
24252 template_p,
24253 typename_type,
24254 /*check_dependency_p=*/true,
24255 /*class_head_p=*/false,
24256 /*is_declaration=*/true);
24257 type = TREE_TYPE (type);
24260 if (type == error_mark_node)
24261 return error_mark_node;
24263 return finish_base_specifier (type, access, virtual_p);
24266 /* Exception handling [gram.exception] */
24268 /* Parse an (optional) noexcept-specification.
24270 noexcept-specification:
24271 noexcept ( constant-expression ) [opt]
24273 If no noexcept-specification is present, returns NULL_TREE.
24274 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24275 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24276 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24277 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24278 in which case a boolean condition is returned instead. */
24280 static tree
24281 cp_parser_noexcept_specification_opt (cp_parser* parser,
24282 bool require_constexpr,
24283 bool* consumed_expr,
24284 bool return_cond)
24286 cp_token *token;
24287 const char *saved_message;
24289 /* Peek at the next token. */
24290 token = cp_lexer_peek_token (parser->lexer);
24292 /* Is it a noexcept-specification? */
24293 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24295 tree expr;
24296 cp_lexer_consume_token (parser->lexer);
24298 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24300 matching_parens parens;
24301 parens.consume_open (parser);
24303 if (require_constexpr)
24305 /* Types may not be defined in an exception-specification. */
24306 saved_message = parser->type_definition_forbidden_message;
24307 parser->type_definition_forbidden_message
24308 = G_("types may not be defined in an exception-specification");
24310 expr = cp_parser_constant_expression (parser);
24312 /* Restore the saved message. */
24313 parser->type_definition_forbidden_message = saved_message;
24315 else
24317 expr = cp_parser_expression (parser);
24318 *consumed_expr = true;
24321 parens.require_close (parser);
24323 else
24325 expr = boolean_true_node;
24326 if (!require_constexpr)
24327 *consumed_expr = false;
24330 /* We cannot build a noexcept-spec right away because this will check
24331 that expr is a constexpr. */
24332 if (!return_cond)
24333 return build_noexcept_spec (expr, tf_warning_or_error);
24334 else
24335 return expr;
24337 else
24338 return NULL_TREE;
24341 /* Parse an (optional) exception-specification.
24343 exception-specification:
24344 throw ( type-id-list [opt] )
24346 Returns a TREE_LIST representing the exception-specification. The
24347 TREE_VALUE of each node is a type. */
24349 static tree
24350 cp_parser_exception_specification_opt (cp_parser* parser)
24352 cp_token *token;
24353 tree type_id_list;
24354 const char *saved_message;
24356 /* Peek at the next token. */
24357 token = cp_lexer_peek_token (parser->lexer);
24359 /* Is it a noexcept-specification? */
24360 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24361 false);
24362 if (type_id_list != NULL_TREE)
24363 return type_id_list;
24365 /* If it's not `throw', then there's no exception-specification. */
24366 if (!cp_parser_is_keyword (token, RID_THROW))
24367 return NULL_TREE;
24369 location_t loc = token->location;
24371 /* Consume the `throw'. */
24372 cp_lexer_consume_token (parser->lexer);
24374 /* Look for the `('. */
24375 matching_parens parens;
24376 parens.require_open (parser);
24378 /* Peek at the next token. */
24379 token = cp_lexer_peek_token (parser->lexer);
24380 /* If it's not a `)', then there is a type-id-list. */
24381 if (token->type != CPP_CLOSE_PAREN)
24383 /* Types may not be defined in an exception-specification. */
24384 saved_message = parser->type_definition_forbidden_message;
24385 parser->type_definition_forbidden_message
24386 = G_("types may not be defined in an exception-specification");
24387 /* Parse the type-id-list. */
24388 type_id_list = cp_parser_type_id_list (parser);
24389 /* Restore the saved message. */
24390 parser->type_definition_forbidden_message = saved_message;
24392 if (cxx_dialect >= cxx17)
24394 error_at (loc, "ISO C++17 does not allow dynamic exception "
24395 "specifications");
24396 type_id_list = NULL_TREE;
24398 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24399 warning_at (loc, OPT_Wdeprecated,
24400 "dynamic exception specifications are deprecated in "
24401 "C++11");
24403 /* In C++17, throw() is equivalent to noexcept (true). throw()
24404 is deprecated in C++11 and above as well, but is still widely used,
24405 so don't warn about it yet. */
24406 else if (cxx_dialect >= cxx17)
24407 type_id_list = noexcept_true_spec;
24408 else
24409 type_id_list = empty_except_spec;
24411 /* Look for the `)'. */
24412 parens.require_close (parser);
24414 return type_id_list;
24417 /* Parse an (optional) type-id-list.
24419 type-id-list:
24420 type-id ... [opt]
24421 type-id-list , type-id ... [opt]
24423 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24424 in the order that the types were presented. */
24426 static tree
24427 cp_parser_type_id_list (cp_parser* parser)
24429 tree types = NULL_TREE;
24431 while (true)
24433 cp_token *token;
24434 tree type;
24436 token = cp_lexer_peek_token (parser->lexer);
24438 /* Get the next type-id. */
24439 type = cp_parser_type_id (parser);
24440 /* Check for invalid 'auto'. */
24441 if (flag_concepts && type_uses_auto (type))
24443 error_at (token->location,
24444 "invalid use of %<auto%> in exception-specification");
24445 type = error_mark_node;
24447 /* Parse the optional ellipsis. */
24448 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24450 /* Consume the `...'. */
24451 cp_lexer_consume_token (parser->lexer);
24453 /* Turn the type into a pack expansion expression. */
24454 type = make_pack_expansion (type);
24456 /* Add it to the list. */
24457 types = add_exception_specifier (types, type, /*complain=*/1);
24458 /* Peek at the next token. */
24459 token = cp_lexer_peek_token (parser->lexer);
24460 /* If it is not a `,', we are done. */
24461 if (token->type != CPP_COMMA)
24462 break;
24463 /* Consume the `,'. */
24464 cp_lexer_consume_token (parser->lexer);
24467 return nreverse (types);
24470 /* Parse a try-block.
24472 try-block:
24473 try compound-statement handler-seq */
24475 static tree
24476 cp_parser_try_block (cp_parser* parser)
24478 tree try_block;
24480 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24481 if (parser->in_function_body
24482 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24483 error ("%<try%> in %<constexpr%> function");
24485 try_block = begin_try_block ();
24486 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24487 finish_try_block (try_block);
24488 cp_parser_handler_seq (parser);
24489 finish_handler_sequence (try_block);
24491 return try_block;
24494 /* Parse a function-try-block.
24496 function-try-block:
24497 try ctor-initializer [opt] function-body handler-seq */
24499 static void
24500 cp_parser_function_try_block (cp_parser* parser)
24502 tree compound_stmt;
24503 tree try_block;
24505 /* Look for the `try' keyword. */
24506 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24507 return;
24508 /* Let the rest of the front end know where we are. */
24509 try_block = begin_function_try_block (&compound_stmt);
24510 /* Parse the function-body. */
24511 cp_parser_ctor_initializer_opt_and_function_body
24512 (parser, /*in_function_try_block=*/true);
24513 /* We're done with the `try' part. */
24514 finish_function_try_block (try_block);
24515 /* Parse the handlers. */
24516 cp_parser_handler_seq (parser);
24517 /* We're done with the handlers. */
24518 finish_function_handler_sequence (try_block, compound_stmt);
24521 /* Parse a handler-seq.
24523 handler-seq:
24524 handler handler-seq [opt] */
24526 static void
24527 cp_parser_handler_seq (cp_parser* parser)
24529 while (true)
24531 cp_token *token;
24533 /* Parse the handler. */
24534 cp_parser_handler (parser);
24535 /* Peek at the next token. */
24536 token = cp_lexer_peek_token (parser->lexer);
24537 /* If it's not `catch' then there are no more handlers. */
24538 if (!cp_parser_is_keyword (token, RID_CATCH))
24539 break;
24543 /* Parse a handler.
24545 handler:
24546 catch ( exception-declaration ) compound-statement */
24548 static void
24549 cp_parser_handler (cp_parser* parser)
24551 tree handler;
24552 tree declaration;
24554 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24555 handler = begin_handler ();
24556 matching_parens parens;
24557 parens.require_open (parser);
24558 declaration = cp_parser_exception_declaration (parser);
24559 finish_handler_parms (declaration, handler);
24560 parens.require_close (parser);
24561 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24562 finish_handler (handler);
24565 /* Parse an exception-declaration.
24567 exception-declaration:
24568 type-specifier-seq declarator
24569 type-specifier-seq abstract-declarator
24570 type-specifier-seq
24573 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24574 ellipsis variant is used. */
24576 static tree
24577 cp_parser_exception_declaration (cp_parser* parser)
24579 cp_decl_specifier_seq type_specifiers;
24580 cp_declarator *declarator;
24581 const char *saved_message;
24583 /* If it's an ellipsis, it's easy to handle. */
24584 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24586 /* Consume the `...' token. */
24587 cp_lexer_consume_token (parser->lexer);
24588 return NULL_TREE;
24591 /* Types may not be defined in exception-declarations. */
24592 saved_message = parser->type_definition_forbidden_message;
24593 parser->type_definition_forbidden_message
24594 = G_("types may not be defined in exception-declarations");
24596 /* Parse the type-specifier-seq. */
24597 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24598 /*is_trailing_return=*/false,
24599 &type_specifiers);
24600 /* If it's a `)', then there is no declarator. */
24601 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24602 declarator = NULL;
24603 else
24604 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24605 /*ctor_dtor_or_conv_p=*/NULL,
24606 /*parenthesized_p=*/NULL,
24607 /*member_p=*/false,
24608 /*friend_p=*/false);
24610 /* Restore the saved message. */
24611 parser->type_definition_forbidden_message = saved_message;
24613 if (!type_specifiers.any_specifiers_p)
24614 return error_mark_node;
24616 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24619 /* Parse a throw-expression.
24621 throw-expression:
24622 throw assignment-expression [opt]
24624 Returns a THROW_EXPR representing the throw-expression. */
24626 static tree
24627 cp_parser_throw_expression (cp_parser* parser)
24629 tree expression;
24630 cp_token* token;
24632 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24633 token = cp_lexer_peek_token (parser->lexer);
24634 /* Figure out whether or not there is an assignment-expression
24635 following the "throw" keyword. */
24636 if (token->type == CPP_COMMA
24637 || token->type == CPP_SEMICOLON
24638 || token->type == CPP_CLOSE_PAREN
24639 || token->type == CPP_CLOSE_SQUARE
24640 || token->type == CPP_CLOSE_BRACE
24641 || token->type == CPP_COLON)
24642 expression = NULL_TREE;
24643 else
24644 expression = cp_parser_assignment_expression (parser);
24646 return build_throw (expression);
24649 /* GNU Extensions */
24651 /* Parse an (optional) asm-specification.
24653 asm-specification:
24654 asm ( string-literal )
24656 If the asm-specification is present, returns a STRING_CST
24657 corresponding to the string-literal. Otherwise, returns
24658 NULL_TREE. */
24660 static tree
24661 cp_parser_asm_specification_opt (cp_parser* parser)
24663 cp_token *token;
24664 tree asm_specification;
24666 /* Peek at the next token. */
24667 token = cp_lexer_peek_token (parser->lexer);
24668 /* If the next token isn't the `asm' keyword, then there's no
24669 asm-specification. */
24670 if (!cp_parser_is_keyword (token, RID_ASM))
24671 return NULL_TREE;
24673 /* Consume the `asm' token. */
24674 cp_lexer_consume_token (parser->lexer);
24675 /* Look for the `('. */
24676 matching_parens parens;
24677 parens.require_open (parser);
24679 /* Look for the string-literal. */
24680 asm_specification = cp_parser_string_literal (parser, false, false);
24682 /* Look for the `)'. */
24683 parens.require_close (parser);
24685 return asm_specification;
24688 /* Parse an asm-operand-list.
24690 asm-operand-list:
24691 asm-operand
24692 asm-operand-list , asm-operand
24694 asm-operand:
24695 string-literal ( expression )
24696 [ string-literal ] string-literal ( expression )
24698 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24699 each node is the expression. The TREE_PURPOSE is itself a
24700 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24701 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24702 is a STRING_CST for the string literal before the parenthesis. Returns
24703 ERROR_MARK_NODE if any of the operands are invalid. */
24705 static tree
24706 cp_parser_asm_operand_list (cp_parser* parser)
24708 tree asm_operands = NULL_TREE;
24709 bool invalid_operands = false;
24711 while (true)
24713 tree string_literal;
24714 tree expression;
24715 tree name;
24717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24719 /* Consume the `[' token. */
24720 cp_lexer_consume_token (parser->lexer);
24721 /* Read the operand name. */
24722 name = cp_parser_identifier (parser);
24723 if (name != error_mark_node)
24724 name = build_string (IDENTIFIER_LENGTH (name),
24725 IDENTIFIER_POINTER (name));
24726 /* Look for the closing `]'. */
24727 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24729 else
24730 name = NULL_TREE;
24731 /* Look for the string-literal. */
24732 string_literal = cp_parser_string_literal (parser, false, false);
24734 /* Look for the `('. */
24735 matching_parens parens;
24736 parens.require_open (parser);
24737 /* Parse the expression. */
24738 expression = cp_parser_expression (parser);
24739 /* Look for the `)'. */
24740 parens.require_close (parser);
24742 if (name == error_mark_node
24743 || string_literal == error_mark_node
24744 || expression == error_mark_node)
24745 invalid_operands = true;
24747 /* Add this operand to the list. */
24748 asm_operands = tree_cons (build_tree_list (name, string_literal),
24749 expression,
24750 asm_operands);
24751 /* If the next token is not a `,', there are no more
24752 operands. */
24753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24754 break;
24755 /* Consume the `,'. */
24756 cp_lexer_consume_token (parser->lexer);
24759 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24762 /* Parse an asm-clobber-list.
24764 asm-clobber-list:
24765 string-literal
24766 asm-clobber-list , string-literal
24768 Returns a TREE_LIST, indicating the clobbers in the order that they
24769 appeared. The TREE_VALUE of each node is a STRING_CST. */
24771 static tree
24772 cp_parser_asm_clobber_list (cp_parser* parser)
24774 tree clobbers = NULL_TREE;
24776 while (true)
24778 tree string_literal;
24780 /* Look for the string literal. */
24781 string_literal = cp_parser_string_literal (parser, false, false);
24782 /* Add it to the list. */
24783 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24784 /* If the next token is not a `,', then the list is
24785 complete. */
24786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24787 break;
24788 /* Consume the `,' token. */
24789 cp_lexer_consume_token (parser->lexer);
24792 return clobbers;
24795 /* Parse an asm-label-list.
24797 asm-label-list:
24798 identifier
24799 asm-label-list , identifier
24801 Returns a TREE_LIST, indicating the labels in the order that they
24802 appeared. The TREE_VALUE of each node is a label. */
24804 static tree
24805 cp_parser_asm_label_list (cp_parser* parser)
24807 tree labels = NULL_TREE;
24809 while (true)
24811 tree identifier, label, name;
24813 /* Look for the identifier. */
24814 identifier = cp_parser_identifier (parser);
24815 if (!error_operand_p (identifier))
24817 label = lookup_label (identifier);
24818 if (TREE_CODE (label) == LABEL_DECL)
24820 TREE_USED (label) = 1;
24821 check_goto (label);
24822 name = build_string (IDENTIFIER_LENGTH (identifier),
24823 IDENTIFIER_POINTER (identifier));
24824 labels = tree_cons (name, label, labels);
24827 /* If the next token is not a `,', then the list is
24828 complete. */
24829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24830 break;
24831 /* Consume the `,' token. */
24832 cp_lexer_consume_token (parser->lexer);
24835 return nreverse (labels);
24838 /* Return TRUE iff the next tokens in the stream are possibly the
24839 beginning of a GNU extension attribute. */
24841 static bool
24842 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24844 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24847 /* Return TRUE iff the next tokens in the stream are possibly the
24848 beginning of a standard C++-11 attribute specifier. */
24850 static bool
24851 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24853 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24856 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24857 beginning of a standard C++-11 attribute specifier. */
24859 static bool
24860 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24862 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24864 return (cxx_dialect >= cxx11
24865 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24866 || (token->type == CPP_OPEN_SQUARE
24867 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24868 && token->type == CPP_OPEN_SQUARE)));
24871 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24872 beginning of a GNU extension attribute. */
24874 static bool
24875 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24877 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24879 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24882 /* Return true iff the next tokens can be the beginning of either a
24883 GNU attribute list, or a standard C++11 attribute sequence. */
24885 static bool
24886 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24888 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24889 || cp_next_tokens_can_be_std_attribute_p (parser));
24892 /* Return true iff the next Nth tokens can be the beginning of either
24893 a GNU attribute list, or a standard C++11 attribute sequence. */
24895 static bool
24896 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24898 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24899 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24902 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24903 of GNU attributes, or return NULL. */
24905 static tree
24906 cp_parser_attributes_opt (cp_parser *parser)
24908 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24909 return cp_parser_gnu_attributes_opt (parser);
24910 return cp_parser_std_attribute_spec_seq (parser);
24913 /* Parse an (optional) series of attributes.
24915 attributes:
24916 attributes attribute
24918 attribute:
24919 __attribute__ (( attribute-list [opt] ))
24921 The return value is as for cp_parser_gnu_attribute_list. */
24923 static tree
24924 cp_parser_gnu_attributes_opt (cp_parser* parser)
24926 tree attributes = NULL_TREE;
24928 while (true)
24930 cp_token *token;
24931 tree attribute_list;
24932 bool ok = true;
24934 /* Peek at the next token. */
24935 token = cp_lexer_peek_token (parser->lexer);
24936 /* If it's not `__attribute__', then we're done. */
24937 if (token->keyword != RID_ATTRIBUTE)
24938 break;
24940 /* Consume the `__attribute__' keyword. */
24941 cp_lexer_consume_token (parser->lexer);
24942 /* Look for the two `(' tokens. */
24943 matching_parens outer_parens;
24944 outer_parens.require_open (parser);
24945 matching_parens inner_parens;
24946 inner_parens.require_open (parser);
24948 /* Peek at the next token. */
24949 token = cp_lexer_peek_token (parser->lexer);
24950 if (token->type != CPP_CLOSE_PAREN)
24951 /* Parse the attribute-list. */
24952 attribute_list = cp_parser_gnu_attribute_list (parser);
24953 else
24954 /* If the next token is a `)', then there is no attribute
24955 list. */
24956 attribute_list = NULL;
24958 /* Look for the two `)' tokens. */
24959 if (!inner_parens.require_close (parser))
24960 ok = false;
24961 if (!outer_parens.require_close (parser))
24962 ok = false;
24963 if (!ok)
24964 cp_parser_skip_to_end_of_statement (parser);
24966 /* Add these new attributes to the list. */
24967 attributes = attr_chainon (attributes, attribute_list);
24970 return attributes;
24973 /* Parse a GNU attribute-list.
24975 attribute-list:
24976 attribute
24977 attribute-list , attribute
24979 attribute:
24980 identifier
24981 identifier ( identifier )
24982 identifier ( identifier , expression-list )
24983 identifier ( expression-list )
24985 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24986 to an attribute. The TREE_PURPOSE of each node is the identifier
24987 indicating which attribute is in use. The TREE_VALUE represents
24988 the arguments, if any. */
24990 static tree
24991 cp_parser_gnu_attribute_list (cp_parser* parser)
24993 tree attribute_list = NULL_TREE;
24994 bool save_translate_strings_p = parser->translate_strings_p;
24996 parser->translate_strings_p = false;
24997 while (true)
24999 cp_token *token;
25000 tree identifier;
25001 tree attribute;
25003 /* Look for the identifier. We also allow keywords here; for
25004 example `__attribute__ ((const))' is legal. */
25005 token = cp_lexer_peek_token (parser->lexer);
25006 if (token->type == CPP_NAME
25007 || token->type == CPP_KEYWORD)
25009 tree arguments = NULL_TREE;
25011 /* Consume the token, but save it since we need it for the
25012 SIMD enabled function parsing. */
25013 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25015 /* Save away the identifier that indicates which attribute
25016 this is. */
25017 identifier = (token->type == CPP_KEYWORD)
25018 /* For keywords, use the canonical spelling, not the
25019 parsed identifier. */
25020 ? ridpointers[(int) token->keyword]
25021 : id_token->u.value;
25023 identifier = canonicalize_attr_name (identifier);
25024 attribute = build_tree_list (identifier, NULL_TREE);
25026 /* Peek at the next token. */
25027 token = cp_lexer_peek_token (parser->lexer);
25028 /* If it's an `(', then parse the attribute arguments. */
25029 if (token->type == CPP_OPEN_PAREN)
25031 vec<tree, va_gc> *vec;
25032 int attr_flag = (attribute_takes_identifier_p (identifier)
25033 ? id_attr : normal_attr);
25034 vec = cp_parser_parenthesized_expression_list
25035 (parser, attr_flag, /*cast_p=*/false,
25036 /*allow_expansion_p=*/false,
25037 /*non_constant_p=*/NULL);
25038 if (vec == NULL)
25039 arguments = error_mark_node;
25040 else
25042 arguments = build_tree_list_vec (vec);
25043 release_tree_vector (vec);
25045 /* Save the arguments away. */
25046 TREE_VALUE (attribute) = arguments;
25049 if (arguments != error_mark_node)
25051 /* Add this attribute to the list. */
25052 TREE_CHAIN (attribute) = attribute_list;
25053 attribute_list = attribute;
25056 token = cp_lexer_peek_token (parser->lexer);
25058 /* Now, look for more attributes. If the next token isn't a
25059 `,', we're done. */
25060 if (token->type != CPP_COMMA)
25061 break;
25063 /* Consume the comma and keep going. */
25064 cp_lexer_consume_token (parser->lexer);
25066 parser->translate_strings_p = save_translate_strings_p;
25068 /* We built up the list in reverse order. */
25069 return nreverse (attribute_list);
25072 /* Parse a standard C++11 attribute.
25074 The returned representation is a TREE_LIST which TREE_PURPOSE is
25075 the scoped name of the attribute, and the TREE_VALUE is its
25076 arguments list.
25078 Note that the scoped name of the attribute is itself a TREE_LIST
25079 which TREE_PURPOSE is the namespace of the attribute, and
25080 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25081 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25082 and which TREE_PURPOSE is directly the attribute name.
25084 Clients of the attribute code should use get_attribute_namespace
25085 and get_attribute_name to get the actual namespace and name of
25086 attributes, regardless of their being GNU or C++11 attributes.
25088 attribute:
25089 attribute-token attribute-argument-clause [opt]
25091 attribute-token:
25092 identifier
25093 attribute-scoped-token
25095 attribute-scoped-token:
25096 attribute-namespace :: identifier
25098 attribute-namespace:
25099 identifier
25101 attribute-argument-clause:
25102 ( balanced-token-seq )
25104 balanced-token-seq:
25105 balanced-token [opt]
25106 balanced-token-seq balanced-token
25108 balanced-token:
25109 ( balanced-token-seq )
25110 [ balanced-token-seq ]
25111 { balanced-token-seq }. */
25113 static tree
25114 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25116 tree attribute, attr_id = NULL_TREE, arguments;
25117 cp_token *token;
25119 /* First, parse name of the attribute, a.k.a attribute-token. */
25121 token = cp_lexer_peek_token (parser->lexer);
25122 if (token->type == CPP_NAME)
25123 attr_id = token->u.value;
25124 else if (token->type == CPP_KEYWORD)
25125 attr_id = ridpointers[(int) token->keyword];
25126 else if (token->flags & NAMED_OP)
25127 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25129 if (attr_id == NULL_TREE)
25130 return NULL_TREE;
25132 cp_lexer_consume_token (parser->lexer);
25134 token = cp_lexer_peek_token (parser->lexer);
25135 if (token->type == CPP_SCOPE)
25137 /* We are seeing a scoped attribute token. */
25139 cp_lexer_consume_token (parser->lexer);
25140 if (attr_ns)
25141 error_at (token->location, "attribute using prefix used together "
25142 "with scoped attribute token");
25143 attr_ns = attr_id;
25145 token = cp_lexer_consume_token (parser->lexer);
25146 if (token->type == CPP_NAME)
25147 attr_id = token->u.value;
25148 else if (token->type == CPP_KEYWORD)
25149 attr_id = ridpointers[(int) token->keyword];
25150 else if (token->flags & NAMED_OP)
25151 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25152 else
25154 error_at (token->location,
25155 "expected an identifier for the attribute name");
25156 return error_mark_node;
25159 attr_id = canonicalize_attr_name (attr_id);
25160 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25161 NULL_TREE);
25162 token = cp_lexer_peek_token (parser->lexer);
25164 else if (attr_ns)
25165 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25166 NULL_TREE);
25167 else
25169 attr_id = canonicalize_attr_name (attr_id);
25170 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25171 NULL_TREE);
25172 /* C++11 noreturn attribute is equivalent to GNU's. */
25173 if (is_attribute_p ("noreturn", attr_id))
25174 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25175 /* C++14 deprecated attribute is equivalent to GNU's. */
25176 else if (is_attribute_p ("deprecated", attr_id))
25177 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25178 /* C++17 fallthrough attribute is equivalent to GNU's. */
25179 else if (is_attribute_p ("fallthrough", attr_id))
25180 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25181 /* Transactional Memory TS optimize_for_synchronized attribute is
25182 equivalent to GNU transaction_callable. */
25183 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25184 TREE_PURPOSE (attribute)
25185 = get_identifier ("transaction_callable");
25186 /* Transactional Memory attributes are GNU attributes. */
25187 else if (tm_attr_to_mask (attr_id))
25188 TREE_PURPOSE (attribute) = attr_id;
25191 /* Now parse the optional argument clause of the attribute. */
25193 if (token->type != CPP_OPEN_PAREN)
25194 return attribute;
25197 vec<tree, va_gc> *vec;
25198 int attr_flag = normal_attr;
25200 if (attr_ns == get_identifier ("gnu")
25201 && attribute_takes_identifier_p (attr_id))
25202 /* A GNU attribute that takes an identifier in parameter. */
25203 attr_flag = id_attr;
25205 vec = cp_parser_parenthesized_expression_list
25206 (parser, attr_flag, /*cast_p=*/false,
25207 /*allow_expansion_p=*/true,
25208 /*non_constant_p=*/NULL);
25209 if (vec == NULL)
25210 arguments = error_mark_node;
25211 else
25213 arguments = build_tree_list_vec (vec);
25214 release_tree_vector (vec);
25217 if (arguments == error_mark_node)
25218 attribute = error_mark_node;
25219 else
25220 TREE_VALUE (attribute) = arguments;
25223 return attribute;
25226 /* Check that the attribute ATTRIBUTE appears at most once in the
25227 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25228 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25229 isn't implemented yet in GCC. */
25231 static void
25232 cp_parser_check_std_attribute (tree attributes, tree attribute)
25234 if (attributes)
25236 tree name = get_attribute_name (attribute);
25237 if (is_attribute_p ("noreturn", name)
25238 && lookup_attribute ("noreturn", attributes))
25239 error ("attribute %<noreturn%> can appear at most once "
25240 "in an attribute-list");
25241 else if (is_attribute_p ("deprecated", name)
25242 && lookup_attribute ("deprecated", attributes))
25243 error ("attribute %<deprecated%> can appear at most once "
25244 "in an attribute-list");
25248 /* Parse a list of standard C++-11 attributes.
25250 attribute-list:
25251 attribute [opt]
25252 attribute-list , attribute[opt]
25253 attribute ...
25254 attribute-list , attribute ...
25257 static tree
25258 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25260 tree attributes = NULL_TREE, attribute = NULL_TREE;
25261 cp_token *token = NULL;
25263 while (true)
25265 attribute = cp_parser_std_attribute (parser, attr_ns);
25266 if (attribute == error_mark_node)
25267 break;
25268 if (attribute != NULL_TREE)
25270 cp_parser_check_std_attribute (attributes, attribute);
25271 TREE_CHAIN (attribute) = attributes;
25272 attributes = attribute;
25274 token = cp_lexer_peek_token (parser->lexer);
25275 if (token->type == CPP_ELLIPSIS)
25277 cp_lexer_consume_token (parser->lexer);
25278 if (attribute == NULL_TREE)
25279 error_at (token->location,
25280 "expected attribute before %<...%>");
25281 else
25283 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25284 if (pack == error_mark_node)
25285 return error_mark_node;
25286 TREE_VALUE (attribute) = pack;
25288 token = cp_lexer_peek_token (parser->lexer);
25290 if (token->type != CPP_COMMA)
25291 break;
25292 cp_lexer_consume_token (parser->lexer);
25294 attributes = nreverse (attributes);
25295 return attributes;
25298 /* Parse a standard C++-11 attribute specifier.
25300 attribute-specifier:
25301 [ [ attribute-using-prefix [opt] attribute-list ] ]
25302 alignment-specifier
25304 attribute-using-prefix:
25305 using attribute-namespace :
25307 alignment-specifier:
25308 alignas ( type-id ... [opt] )
25309 alignas ( alignment-expression ... [opt] ). */
25311 static tree
25312 cp_parser_std_attribute_spec (cp_parser *parser)
25314 tree attributes = NULL_TREE;
25315 cp_token *token = cp_lexer_peek_token (parser->lexer);
25317 if (token->type == CPP_OPEN_SQUARE
25318 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25320 tree attr_ns = NULL_TREE;
25322 cp_lexer_consume_token (parser->lexer);
25323 cp_lexer_consume_token (parser->lexer);
25325 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25327 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25328 if (token->type == CPP_NAME)
25329 attr_ns = token->u.value;
25330 else if (token->type == CPP_KEYWORD)
25331 attr_ns = ridpointers[(int) token->keyword];
25332 else if (token->flags & NAMED_OP)
25333 attr_ns = get_identifier (cpp_type2name (token->type,
25334 token->flags));
25335 if (attr_ns
25336 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25338 if (cxx_dialect < cxx17
25339 && !in_system_header_at (input_location))
25340 pedwarn (input_location, 0,
25341 "attribute using prefix only available "
25342 "with -std=c++17 or -std=gnu++17");
25344 cp_lexer_consume_token (parser->lexer);
25345 cp_lexer_consume_token (parser->lexer);
25346 cp_lexer_consume_token (parser->lexer);
25348 else
25349 attr_ns = NULL_TREE;
25352 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25354 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25355 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25356 cp_parser_skip_to_end_of_statement (parser);
25357 else
25358 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25359 when we are sure that we have actually parsed them. */
25360 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25362 else
25364 tree alignas_expr;
25366 /* Look for an alignment-specifier. */
25368 token = cp_lexer_peek_token (parser->lexer);
25370 if (token->type != CPP_KEYWORD
25371 || token->keyword != RID_ALIGNAS)
25372 return NULL_TREE;
25374 cp_lexer_consume_token (parser->lexer);
25375 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25377 matching_parens parens;
25378 if (!parens.require_open (parser))
25379 return error_mark_node;
25381 cp_parser_parse_tentatively (parser);
25382 alignas_expr = cp_parser_type_id (parser);
25384 if (!cp_parser_parse_definitely (parser))
25386 alignas_expr = cp_parser_assignment_expression (parser);
25387 if (alignas_expr == error_mark_node)
25388 cp_parser_skip_to_end_of_statement (parser);
25389 if (alignas_expr == NULL_TREE
25390 || alignas_expr == error_mark_node)
25391 return alignas_expr;
25394 alignas_expr = cxx_alignas_expr (alignas_expr);
25395 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25397 /* Handle alignas (pack...). */
25398 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25400 cp_lexer_consume_token (parser->lexer);
25401 alignas_expr = make_pack_expansion (alignas_expr);
25404 /* Something went wrong, so don't build the attribute. */
25405 if (alignas_expr == error_mark_node)
25406 return error_mark_node;
25408 if (!parens.require_close (parser))
25409 return error_mark_node;
25411 /* Build the C++-11 representation of an 'aligned'
25412 attribute. */
25413 attributes =
25414 build_tree_list (build_tree_list (get_identifier ("gnu"),
25415 get_identifier ("aligned")),
25416 alignas_expr);
25419 return attributes;
25422 /* Parse a standard C++-11 attribute-specifier-seq.
25424 attribute-specifier-seq:
25425 attribute-specifier-seq [opt] attribute-specifier
25428 static tree
25429 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25431 tree attr_specs = NULL_TREE;
25432 tree attr_last = NULL_TREE;
25434 while (true)
25436 tree attr_spec = cp_parser_std_attribute_spec (parser);
25437 if (attr_spec == NULL_TREE)
25438 break;
25439 if (attr_spec == error_mark_node)
25440 return error_mark_node;
25442 if (attr_last)
25443 TREE_CHAIN (attr_last) = attr_spec;
25444 else
25445 attr_specs = attr_last = attr_spec;
25446 attr_last = tree_last (attr_last);
25449 return attr_specs;
25452 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25453 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25454 current value of the PEDANTIC flag, regardless of whether or not
25455 the `__extension__' keyword is present. The caller is responsible
25456 for restoring the value of the PEDANTIC flag. */
25458 static bool
25459 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25461 /* Save the old value of the PEDANTIC flag. */
25462 *saved_pedantic = pedantic;
25464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25466 /* Consume the `__extension__' token. */
25467 cp_lexer_consume_token (parser->lexer);
25468 /* We're not being pedantic while the `__extension__' keyword is
25469 in effect. */
25470 pedantic = 0;
25472 return true;
25475 return false;
25478 /* Parse a label declaration.
25480 label-declaration:
25481 __label__ label-declarator-seq ;
25483 label-declarator-seq:
25484 identifier , label-declarator-seq
25485 identifier */
25487 static void
25488 cp_parser_label_declaration (cp_parser* parser)
25490 /* Look for the `__label__' keyword. */
25491 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25493 while (true)
25495 tree identifier;
25497 /* Look for an identifier. */
25498 identifier = cp_parser_identifier (parser);
25499 /* If we failed, stop. */
25500 if (identifier == error_mark_node)
25501 break;
25502 /* Declare it as a label. */
25503 finish_label_decl (identifier);
25504 /* If the next token is a `;', stop. */
25505 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25506 break;
25507 /* Look for the `,' separating the label declarations. */
25508 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25511 /* Look for the final `;'. */
25512 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25515 // -------------------------------------------------------------------------- //
25516 // Requires Clause
25518 // Parse a requires clause.
25520 // requires-clause:
25521 // 'requires' logical-or-expression
25523 // The required logical-or-expression must be a constant expression. Note
25524 // that we don't check that the expression is constepxr here. We defer until
25525 // we analyze constraints and then, we only check atomic constraints.
25526 static tree
25527 cp_parser_requires_clause (cp_parser *parser)
25529 // Parse the requires clause so that it is not automatically folded.
25530 ++processing_template_decl;
25531 tree expr = cp_parser_binary_expression (parser, false, false,
25532 PREC_NOT_OPERATOR, NULL);
25533 if (check_for_bare_parameter_packs (expr))
25534 expr = error_mark_node;
25535 --processing_template_decl;
25536 return expr;
25539 // Optionally parse a requires clause:
25540 static tree
25541 cp_parser_requires_clause_opt (cp_parser *parser)
25543 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25544 if (tok->keyword != RID_REQUIRES)
25546 if (!flag_concepts && tok->type == CPP_NAME
25547 && tok->u.value == ridpointers[RID_REQUIRES])
25549 error_at (cp_lexer_peek_token (parser->lexer)->location,
25550 "%<requires%> only available with -fconcepts");
25551 /* Parse and discard the requires-clause. */
25552 cp_lexer_consume_token (parser->lexer);
25553 cp_parser_requires_clause (parser);
25555 return NULL_TREE;
25557 cp_lexer_consume_token (parser->lexer);
25558 return cp_parser_requires_clause (parser);
25562 /*---------------------------------------------------------------------------
25563 Requires expressions
25564 ---------------------------------------------------------------------------*/
25566 /* Parse a requires expression
25568 requirement-expression:
25569 'requires' requirement-parameter-list [opt] requirement-body */
25570 static tree
25571 cp_parser_requires_expression (cp_parser *parser)
25573 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25574 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25576 /* A requires-expression shall appear only within a concept
25577 definition or a requires-clause.
25579 TODO: Implement this diagnostic correctly. */
25580 if (!processing_template_decl)
25582 error_at (loc, "a requires expression cannot appear outside a template");
25583 cp_parser_skip_to_end_of_statement (parser);
25584 return error_mark_node;
25587 tree parms, reqs;
25589 /* Local parameters are delared as variables within the scope
25590 of the expression. They are not visible past the end of
25591 the expression. Expressions within the requires-expression
25592 are unevaluated. */
25593 struct scope_sentinel
25595 scope_sentinel ()
25597 ++cp_unevaluated_operand;
25598 begin_scope (sk_block, NULL_TREE);
25601 ~scope_sentinel ()
25603 pop_bindings_and_leave_scope ();
25604 --cp_unevaluated_operand;
25606 } s;
25608 /* Parse the optional parameter list. */
25609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25611 parms = cp_parser_requirement_parameter_list (parser);
25612 if (parms == error_mark_node)
25613 return error_mark_node;
25615 else
25616 parms = NULL_TREE;
25618 /* Parse the requirement body. */
25619 reqs = cp_parser_requirement_body (parser);
25620 if (reqs == error_mark_node)
25621 return error_mark_node;
25624 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25625 the parm chain. */
25626 grokparms (parms, &parms);
25627 return finish_requires_expr (parms, reqs);
25630 /* Parse a parameterized requirement.
25632 requirement-parameter-list:
25633 '(' parameter-declaration-clause ')' */
25634 static tree
25635 cp_parser_requirement_parameter_list (cp_parser *parser)
25637 matching_parens parens;
25638 if (!parens.require_open (parser))
25639 return error_mark_node;
25641 tree parms = cp_parser_parameter_declaration_clause (parser);
25643 if (!parens.require_close (parser))
25644 return error_mark_node;
25646 return parms;
25649 /* Parse the body of a requirement.
25651 requirement-body:
25652 '{' requirement-list '}' */
25653 static tree
25654 cp_parser_requirement_body (cp_parser *parser)
25656 matching_braces braces;
25657 if (!braces.require_open (parser))
25658 return error_mark_node;
25660 tree reqs = cp_parser_requirement_list (parser);
25662 if (!braces.require_close (parser))
25663 return error_mark_node;
25665 return reqs;
25668 /* Parse a list of requirements.
25670 requirement-list:
25671 requirement
25672 requirement-list ';' requirement[opt] */
25673 static tree
25674 cp_parser_requirement_list (cp_parser *parser)
25676 tree result = NULL_TREE;
25677 while (true)
25679 tree req = cp_parser_requirement (parser);
25680 if (req == error_mark_node)
25681 return error_mark_node;
25683 result = tree_cons (NULL_TREE, req, result);
25685 /* If we see a semi-colon, consume it. */
25686 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25687 cp_lexer_consume_token (parser->lexer);
25689 /* Stop processing at the end of the list. */
25690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25691 break;
25694 /* Reverse the order of requirements so they are analyzed in
25695 declaration order. */
25696 return nreverse (result);
25699 /* Parse a syntactic requirement or type requirement.
25701 requirement:
25702 simple-requirement
25703 compound-requirement
25704 type-requirement
25705 nested-requirement */
25706 static tree
25707 cp_parser_requirement (cp_parser *parser)
25709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25710 return cp_parser_compound_requirement (parser);
25711 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25712 return cp_parser_type_requirement (parser);
25713 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25714 return cp_parser_nested_requirement (parser);
25715 else
25716 return cp_parser_simple_requirement (parser);
25719 /* Parse a simple requirement.
25721 simple-requirement:
25722 expression ';' */
25723 static tree
25724 cp_parser_simple_requirement (cp_parser *parser)
25726 tree expr = cp_parser_expression (parser, NULL, false, false);
25727 if (!expr || expr == error_mark_node)
25728 return error_mark_node;
25730 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25731 return error_mark_node;
25733 return finish_simple_requirement (expr);
25736 /* Parse a type requirement
25738 type-requirement
25739 nested-name-specifier [opt] required-type-name ';'
25741 required-type-name:
25742 type-name
25743 'template' [opt] simple-template-id */
25744 static tree
25745 cp_parser_type_requirement (cp_parser *parser)
25747 cp_lexer_consume_token (parser->lexer);
25749 // Save the scope before parsing name specifiers.
25750 tree saved_scope = parser->scope;
25751 tree saved_object_scope = parser->object_scope;
25752 tree saved_qualifying_scope = parser->qualifying_scope;
25753 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25754 cp_parser_nested_name_specifier_opt (parser,
25755 /*typename_keyword_p=*/true,
25756 /*check_dependency_p=*/false,
25757 /*type_p=*/true,
25758 /*is_declaration=*/false);
25760 tree type;
25761 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25763 cp_lexer_consume_token (parser->lexer);
25764 type = cp_parser_template_id (parser,
25765 /*template_keyword_p=*/true,
25766 /*check_dependency=*/false,
25767 /*tag_type=*/none_type,
25768 /*is_declaration=*/false);
25769 type = make_typename_type (parser->scope, type, typename_type,
25770 /*complain=*/tf_error);
25772 else
25773 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25775 if (TREE_CODE (type) == TYPE_DECL)
25776 type = TREE_TYPE (type);
25778 parser->scope = saved_scope;
25779 parser->object_scope = saved_object_scope;
25780 parser->qualifying_scope = saved_qualifying_scope;
25782 if (type == error_mark_node)
25783 cp_parser_skip_to_end_of_statement (parser);
25785 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25786 return error_mark_node;
25787 if (type == error_mark_node)
25788 return error_mark_node;
25790 return finish_type_requirement (type);
25793 /* Parse a compound requirement
25795 compound-requirement:
25796 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25797 static tree
25798 cp_parser_compound_requirement (cp_parser *parser)
25800 /* Parse an expression enclosed in '{ }'s. */
25801 matching_braces braces;
25802 if (!braces.require_open (parser))
25803 return error_mark_node;
25805 tree expr = cp_parser_expression (parser, NULL, false, false);
25806 if (!expr || expr == error_mark_node)
25807 return error_mark_node;
25809 if (!braces.require_close (parser))
25810 return error_mark_node;
25812 /* Parse the optional noexcept. */
25813 bool noexcept_p = false;
25814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25816 cp_lexer_consume_token (parser->lexer);
25817 noexcept_p = true;
25820 /* Parse the optional trailing return type. */
25821 tree type = NULL_TREE;
25822 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25824 cp_lexer_consume_token (parser->lexer);
25825 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25826 parser->in_result_type_constraint_p = true;
25827 type = cp_parser_trailing_type_id (parser);
25828 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25829 if (type == error_mark_node)
25830 return error_mark_node;
25833 return finish_compound_requirement (expr, type, noexcept_p);
25836 /* Parse a nested requirement. This is the same as a requires clause.
25838 nested-requirement:
25839 requires-clause */
25840 static tree
25841 cp_parser_nested_requirement (cp_parser *parser)
25843 cp_lexer_consume_token (parser->lexer);
25844 tree req = cp_parser_requires_clause (parser);
25845 if (req == error_mark_node)
25846 return error_mark_node;
25847 return finish_nested_requirement (req);
25850 /* Support Functions */
25852 /* Return the appropriate prefer_type argument for lookup_name_real based on
25853 tag_type and template_mem_access. */
25855 static inline int
25856 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25858 /* DR 141: When looking in the current enclosing context for a template-name
25859 after -> or ., only consider class templates. */
25860 if (template_mem_access)
25861 return 2;
25862 switch (tag_type)
25864 case none_type: return 0; // No preference.
25865 case scope_type: return 1; // Type or namespace.
25866 default: return 2; // Type only.
25870 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25871 NAME should have one of the representations used for an
25872 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25873 is returned. If PARSER->SCOPE is a dependent type, then a
25874 SCOPE_REF is returned.
25876 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25877 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25878 was formed. Abstractly, such entities should not be passed to this
25879 function, because they do not need to be looked up, but it is
25880 simpler to check for this special case here, rather than at the
25881 call-sites.
25883 In cases not explicitly covered above, this function returns a
25884 DECL, OVERLOAD, or baselink representing the result of the lookup.
25885 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25886 is returned.
25888 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25889 (e.g., "struct") that was used. In that case bindings that do not
25890 refer to types are ignored.
25892 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25893 ignored.
25895 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25896 are ignored.
25898 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25899 types.
25901 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25902 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25903 NULL_TREE otherwise. */
25905 static cp_expr
25906 cp_parser_lookup_name (cp_parser *parser, tree name,
25907 enum tag_types tag_type,
25908 bool is_template,
25909 bool is_namespace,
25910 bool check_dependency,
25911 tree *ambiguous_decls,
25912 location_t name_location)
25914 tree decl;
25915 tree object_type = parser->context->object_type;
25917 /* Assume that the lookup will be unambiguous. */
25918 if (ambiguous_decls)
25919 *ambiguous_decls = NULL_TREE;
25921 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25922 no longer valid. Note that if we are parsing tentatively, and
25923 the parse fails, OBJECT_TYPE will be automatically restored. */
25924 parser->context->object_type = NULL_TREE;
25926 if (name == error_mark_node)
25927 return error_mark_node;
25929 /* A template-id has already been resolved; there is no lookup to
25930 do. */
25931 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25932 return name;
25933 if (BASELINK_P (name))
25935 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25936 == TEMPLATE_ID_EXPR);
25937 return name;
25940 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25941 it should already have been checked to make sure that the name
25942 used matches the type being destroyed. */
25943 if (TREE_CODE (name) == BIT_NOT_EXPR)
25945 tree type;
25947 /* Figure out to which type this destructor applies. */
25948 if (parser->scope)
25949 type = parser->scope;
25950 else if (object_type)
25951 type = object_type;
25952 else
25953 type = current_class_type;
25954 /* If that's not a class type, there is no destructor. */
25955 if (!type || !CLASS_TYPE_P (type))
25956 return error_mark_node;
25958 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25959 lazily_declare_fn (sfk_destructor, type);
25961 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25962 return dtor;
25964 return error_mark_node;
25967 /* By this point, the NAME should be an ordinary identifier. If
25968 the id-expression was a qualified name, the qualifying scope is
25969 stored in PARSER->SCOPE at this point. */
25970 gcc_assert (identifier_p (name));
25972 /* Perform the lookup. */
25973 if (parser->scope)
25975 bool dependent_p;
25977 if (parser->scope == error_mark_node)
25978 return error_mark_node;
25980 /* If the SCOPE is dependent, the lookup must be deferred until
25981 the template is instantiated -- unless we are explicitly
25982 looking up names in uninstantiated templates. Even then, we
25983 cannot look up the name if the scope is not a class type; it
25984 might, for example, be a template type parameter. */
25985 dependent_p = (TYPE_P (parser->scope)
25986 && dependent_scope_p (parser->scope));
25987 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25988 && dependent_p)
25989 /* Defer lookup. */
25990 decl = error_mark_node;
25991 else
25993 tree pushed_scope = NULL_TREE;
25995 /* If PARSER->SCOPE is a dependent type, then it must be a
25996 class type, and we must not be checking dependencies;
25997 otherwise, we would have processed this lookup above. So
25998 that PARSER->SCOPE is not considered a dependent base by
25999 lookup_member, we must enter the scope here. */
26000 if (dependent_p)
26001 pushed_scope = push_scope (parser->scope);
26003 /* If the PARSER->SCOPE is a template specialization, it
26004 may be instantiated during name lookup. In that case,
26005 errors may be issued. Even if we rollback the current
26006 tentative parse, those errors are valid. */
26007 decl = lookup_qualified_name (parser->scope, name,
26008 prefer_type_arg (tag_type),
26009 /*complain=*/true);
26011 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26012 lookup result and the nested-name-specifier nominates a class C:
26013 * if the name specified after the nested-name-specifier, when
26014 looked up in C, is the injected-class-name of C (Clause 9), or
26015 * if the name specified after the nested-name-specifier is the
26016 same as the identifier or the simple-template-id's template-
26017 name in the last component of the nested-name-specifier,
26018 the name is instead considered to name the constructor of
26019 class C. [ Note: for example, the constructor is not an
26020 acceptable lookup result in an elaborated-type-specifier so
26021 the constructor would not be used in place of the
26022 injected-class-name. --end note ] Such a constructor name
26023 shall be used only in the declarator-id of a declaration that
26024 names a constructor or in a using-declaration. */
26025 if (tag_type == none_type
26026 && DECL_SELF_REFERENCE_P (decl)
26027 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26028 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26029 prefer_type_arg (tag_type),
26030 /*complain=*/true);
26032 /* If we have a single function from a using decl, pull it out. */
26033 if (TREE_CODE (decl) == OVERLOAD
26034 && !really_overloaded_fn (decl))
26035 decl = OVL_FUNCTION (decl);
26037 if (pushed_scope)
26038 pop_scope (pushed_scope);
26041 /* If the scope is a dependent type and either we deferred lookup or
26042 we did lookup but didn't find the name, rememeber the name. */
26043 if (decl == error_mark_node && TYPE_P (parser->scope)
26044 && dependent_type_p (parser->scope))
26046 if (tag_type)
26048 tree type;
26050 /* The resolution to Core Issue 180 says that `struct
26051 A::B' should be considered a type-name, even if `A'
26052 is dependent. */
26053 type = make_typename_type (parser->scope, name, tag_type,
26054 /*complain=*/tf_error);
26055 if (type != error_mark_node)
26056 decl = TYPE_NAME (type);
26058 else if (is_template
26059 && (cp_parser_next_token_ends_template_argument_p (parser)
26060 || cp_lexer_next_token_is (parser->lexer,
26061 CPP_CLOSE_PAREN)))
26062 decl = make_unbound_class_template (parser->scope,
26063 name, NULL_TREE,
26064 /*complain=*/tf_error);
26065 else
26066 decl = build_qualified_name (/*type=*/NULL_TREE,
26067 parser->scope, name,
26068 is_template);
26070 parser->qualifying_scope = parser->scope;
26071 parser->object_scope = NULL_TREE;
26073 else if (object_type)
26075 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26076 OBJECT_TYPE is not a class. */
26077 if (CLASS_TYPE_P (object_type))
26078 /* If the OBJECT_TYPE is a template specialization, it may
26079 be instantiated during name lookup. In that case, errors
26080 may be issued. Even if we rollback the current tentative
26081 parse, those errors are valid. */
26082 decl = lookup_member (object_type,
26083 name,
26084 /*protect=*/0,
26085 prefer_type_arg (tag_type),
26086 tf_warning_or_error);
26087 else
26088 decl = NULL_TREE;
26090 if (!decl)
26091 /* Look it up in the enclosing context. DR 141: When looking for a
26092 template-name after -> or ., only consider class templates. */
26093 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26094 /*nonclass=*/0,
26095 /*block_p=*/true, is_namespace, 0);
26096 if (object_type == unknown_type_node)
26097 /* The object is type-dependent, so we can't look anything up; we used
26098 this to get the DR 141 behavior. */
26099 object_type = NULL_TREE;
26100 parser->object_scope = object_type;
26101 parser->qualifying_scope = NULL_TREE;
26103 else
26105 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26106 /*nonclass=*/0,
26107 /*block_p=*/true, is_namespace, 0);
26108 parser->qualifying_scope = NULL_TREE;
26109 parser->object_scope = NULL_TREE;
26112 /* If the lookup failed, let our caller know. */
26113 if (!decl || decl == error_mark_node)
26114 return error_mark_node;
26116 /* Pull out the template from an injected-class-name (or multiple). */
26117 if (is_template)
26118 decl = maybe_get_template_decl_from_type_decl (decl);
26120 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26121 if (TREE_CODE (decl) == TREE_LIST)
26123 if (ambiguous_decls)
26124 *ambiguous_decls = decl;
26125 /* The error message we have to print is too complicated for
26126 cp_parser_error, so we incorporate its actions directly. */
26127 if (!cp_parser_simulate_error (parser))
26129 error_at (name_location, "reference to %qD is ambiguous",
26130 name);
26131 print_candidates (decl);
26133 return error_mark_node;
26136 gcc_assert (DECL_P (decl)
26137 || TREE_CODE (decl) == OVERLOAD
26138 || TREE_CODE (decl) == SCOPE_REF
26139 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26140 || BASELINK_P (decl));
26142 /* If we have resolved the name of a member declaration, check to
26143 see if the declaration is accessible. When the name resolves to
26144 set of overloaded functions, accessibility is checked when
26145 overload resolution is done.
26147 During an explicit instantiation, access is not checked at all,
26148 as per [temp.explicit]. */
26149 if (DECL_P (decl))
26150 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26152 maybe_record_typedef_use (decl);
26154 return cp_expr (decl, name_location);
26157 /* Like cp_parser_lookup_name, but for use in the typical case where
26158 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26159 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26161 static tree
26162 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26164 return cp_parser_lookup_name (parser, name,
26165 none_type,
26166 /*is_template=*/false,
26167 /*is_namespace=*/false,
26168 /*check_dependency=*/true,
26169 /*ambiguous_decls=*/NULL,
26170 location);
26173 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26174 the current context, return the TYPE_DECL. If TAG_NAME_P is
26175 true, the DECL indicates the class being defined in a class-head,
26176 or declared in an elaborated-type-specifier.
26178 Otherwise, return DECL. */
26180 static tree
26181 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26183 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26184 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26186 struct A {
26187 template <typename T> struct B;
26190 template <typename T> struct A::B {};
26192 Similarly, in an elaborated-type-specifier:
26194 namespace N { struct X{}; }
26196 struct A {
26197 template <typename T> friend struct N::X;
26200 However, if the DECL refers to a class type, and we are in
26201 the scope of the class, then the name lookup automatically
26202 finds the TYPE_DECL created by build_self_reference rather
26203 than a TEMPLATE_DECL. For example, in:
26205 template <class T> struct S {
26206 S s;
26209 there is no need to handle such case. */
26211 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26212 return DECL_TEMPLATE_RESULT (decl);
26214 return decl;
26217 /* If too many, or too few, template-parameter lists apply to the
26218 declarator, issue an error message. Returns TRUE if all went well,
26219 and FALSE otherwise. */
26221 static bool
26222 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26223 cp_declarator *declarator,
26224 location_t declarator_location)
26226 switch (declarator->kind)
26228 case cdk_id:
26230 unsigned num_templates = 0;
26231 tree scope = declarator->u.id.qualifying_scope;
26233 if (scope)
26234 num_templates = num_template_headers_for_class (scope);
26235 else if (TREE_CODE (declarator->u.id.unqualified_name)
26236 == TEMPLATE_ID_EXPR)
26237 /* If the DECLARATOR has the form `X<y>' then it uses one
26238 additional level of template parameters. */
26239 ++num_templates;
26241 return cp_parser_check_template_parameters
26242 (parser, num_templates, declarator_location, declarator);
26245 case cdk_function:
26246 case cdk_array:
26247 case cdk_pointer:
26248 case cdk_reference:
26249 case cdk_ptrmem:
26250 return (cp_parser_check_declarator_template_parameters
26251 (parser, declarator->declarator, declarator_location));
26253 case cdk_decomp:
26254 case cdk_error:
26255 return true;
26257 default:
26258 gcc_unreachable ();
26260 return false;
26263 /* NUM_TEMPLATES were used in the current declaration. If that is
26264 invalid, return FALSE and issue an error messages. Otherwise,
26265 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26266 declarator and we can print more accurate diagnostics. */
26268 static bool
26269 cp_parser_check_template_parameters (cp_parser* parser,
26270 unsigned num_templates,
26271 location_t location,
26272 cp_declarator *declarator)
26274 /* If there are the same number of template classes and parameter
26275 lists, that's OK. */
26276 if (parser->num_template_parameter_lists == num_templates)
26277 return true;
26278 /* If there are more, but only one more, then we are referring to a
26279 member template. That's OK too. */
26280 if (parser->num_template_parameter_lists == num_templates + 1)
26281 return true;
26282 /* If there are more template classes than parameter lists, we have
26283 something like:
26285 template <class T> void S<T>::R<T>::f (); */
26286 if (parser->num_template_parameter_lists < num_templates)
26288 if (declarator && !current_function_decl)
26289 error_at (location, "specializing member %<%T::%E%> "
26290 "requires %<template<>%> syntax",
26291 declarator->u.id.qualifying_scope,
26292 declarator->u.id.unqualified_name);
26293 else if (declarator)
26294 error_at (location, "invalid declaration of %<%T::%E%>",
26295 declarator->u.id.qualifying_scope,
26296 declarator->u.id.unqualified_name);
26297 else
26298 error_at (location, "too few template-parameter-lists");
26299 return false;
26301 /* Otherwise, there are too many template parameter lists. We have
26302 something like:
26304 template <class T> template <class U> void S::f(); */
26305 error_at (location, "too many template-parameter-lists");
26306 return false;
26309 /* Parse an optional `::' token indicating that the following name is
26310 from the global namespace. If so, PARSER->SCOPE is set to the
26311 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26312 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26313 Returns the new value of PARSER->SCOPE, if the `::' token is
26314 present, and NULL_TREE otherwise. */
26316 static tree
26317 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26319 cp_token *token;
26321 /* Peek at the next token. */
26322 token = cp_lexer_peek_token (parser->lexer);
26323 /* If we're looking at a `::' token then we're starting from the
26324 global namespace, not our current location. */
26325 if (token->type == CPP_SCOPE)
26327 /* Consume the `::' token. */
26328 cp_lexer_consume_token (parser->lexer);
26329 /* Set the SCOPE so that we know where to start the lookup. */
26330 parser->scope = global_namespace;
26331 parser->qualifying_scope = global_namespace;
26332 parser->object_scope = NULL_TREE;
26334 return parser->scope;
26336 else if (!current_scope_valid_p)
26338 parser->scope = NULL_TREE;
26339 parser->qualifying_scope = NULL_TREE;
26340 parser->object_scope = NULL_TREE;
26343 return NULL_TREE;
26346 /* Returns TRUE if the upcoming token sequence is the start of a
26347 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26348 declarator is preceded by the `friend' specifier. */
26350 static bool
26351 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26353 bool constructor_p;
26354 bool outside_class_specifier_p;
26355 tree nested_name_specifier;
26356 cp_token *next_token;
26358 /* The common case is that this is not a constructor declarator, so
26359 try to avoid doing lots of work if at all possible. It's not
26360 valid declare a constructor at function scope. */
26361 if (parser->in_function_body)
26362 return false;
26363 /* And only certain tokens can begin a constructor declarator. */
26364 next_token = cp_lexer_peek_token (parser->lexer);
26365 if (next_token->type != CPP_NAME
26366 && next_token->type != CPP_SCOPE
26367 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26368 && next_token->type != CPP_TEMPLATE_ID)
26369 return false;
26371 /* Parse tentatively; we are going to roll back all of the tokens
26372 consumed here. */
26373 cp_parser_parse_tentatively (parser);
26374 /* Assume that we are looking at a constructor declarator. */
26375 constructor_p = true;
26377 /* Look for the optional `::' operator. */
26378 cp_parser_global_scope_opt (parser,
26379 /*current_scope_valid_p=*/false);
26380 /* Look for the nested-name-specifier. */
26381 nested_name_specifier
26382 = (cp_parser_nested_name_specifier_opt (parser,
26383 /*typename_keyword_p=*/false,
26384 /*check_dependency_p=*/false,
26385 /*type_p=*/false,
26386 /*is_declaration=*/false));
26388 outside_class_specifier_p = (!at_class_scope_p ()
26389 || !TYPE_BEING_DEFINED (current_class_type)
26390 || friend_p);
26392 /* Outside of a class-specifier, there must be a
26393 nested-name-specifier. Except in C++17 mode, where we
26394 might be declaring a guiding declaration. */
26395 if (!nested_name_specifier && outside_class_specifier_p
26396 && cxx_dialect < cxx17)
26397 constructor_p = false;
26398 else if (nested_name_specifier == error_mark_node)
26399 constructor_p = false;
26401 /* If we have a class scope, this is easy; DR 147 says that S::S always
26402 names the constructor, and no other qualified name could. */
26403 if (constructor_p && nested_name_specifier
26404 && CLASS_TYPE_P (nested_name_specifier))
26406 tree id = cp_parser_unqualified_id (parser,
26407 /*template_keyword_p=*/false,
26408 /*check_dependency_p=*/false,
26409 /*declarator_p=*/true,
26410 /*optional_p=*/false);
26411 if (is_overloaded_fn (id))
26412 id = DECL_NAME (get_first_fn (id));
26413 if (!constructor_name_p (id, nested_name_specifier))
26414 constructor_p = false;
26416 /* If we still think that this might be a constructor-declarator,
26417 look for a class-name. */
26418 else if (constructor_p)
26420 /* If we have:
26422 template <typename T> struct S {
26423 S();
26426 we must recognize that the nested `S' names a class. */
26427 if (cxx_dialect >= cxx17)
26428 cp_parser_parse_tentatively (parser);
26430 tree type_decl;
26431 type_decl = cp_parser_class_name (parser,
26432 /*typename_keyword_p=*/false,
26433 /*template_keyword_p=*/false,
26434 none_type,
26435 /*check_dependency_p=*/false,
26436 /*class_head_p=*/false,
26437 /*is_declaration=*/false);
26439 if (cxx_dialect >= cxx17
26440 && !cp_parser_parse_definitely (parser))
26442 type_decl = NULL_TREE;
26443 tree tmpl = cp_parser_template_name (parser,
26444 /*template_keyword*/false,
26445 /*check_dependency_p*/false,
26446 /*is_declaration*/false,
26447 none_type,
26448 /*is_identifier*/NULL);
26449 if (DECL_CLASS_TEMPLATE_P (tmpl)
26450 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26451 /* It's a deduction guide, return true. */;
26452 else
26453 cp_parser_simulate_error (parser);
26456 /* If there was no class-name, then this is not a constructor.
26457 Otherwise, if we are in a class-specifier and we aren't
26458 handling a friend declaration, check that its type matches
26459 current_class_type (c++/38313). Note: error_mark_node
26460 is left alone for error recovery purposes. */
26461 constructor_p = (!cp_parser_error_occurred (parser)
26462 && (outside_class_specifier_p
26463 || type_decl == NULL_TREE
26464 || type_decl == error_mark_node
26465 || same_type_p (current_class_type,
26466 TREE_TYPE (type_decl))));
26468 /* If we're still considering a constructor, we have to see a `(',
26469 to begin the parameter-declaration-clause, followed by either a
26470 `)', an `...', or a decl-specifier. We need to check for a
26471 type-specifier to avoid being fooled into thinking that:
26473 S (f) (int);
26475 is a constructor. (It is actually a function named `f' that
26476 takes one parameter (of type `int') and returns a value of type
26477 `S'. */
26478 if (constructor_p
26479 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26480 constructor_p = false;
26482 if (constructor_p
26483 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26484 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26485 /* A parameter declaration begins with a decl-specifier,
26486 which is either the "attribute" keyword, a storage class
26487 specifier, or (usually) a type-specifier. */
26488 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26490 tree type;
26491 tree pushed_scope = NULL_TREE;
26492 unsigned saved_num_template_parameter_lists;
26494 /* Names appearing in the type-specifier should be looked up
26495 in the scope of the class. */
26496 if (current_class_type)
26497 type = NULL_TREE;
26498 else if (type_decl)
26500 type = TREE_TYPE (type_decl);
26501 if (TREE_CODE (type) == TYPENAME_TYPE)
26503 type = resolve_typename_type (type,
26504 /*only_current_p=*/false);
26505 if (TREE_CODE (type) == TYPENAME_TYPE)
26507 cp_parser_abort_tentative_parse (parser);
26508 return false;
26511 pushed_scope = push_scope (type);
26514 /* Inside the constructor parameter list, surrounding
26515 template-parameter-lists do not apply. */
26516 saved_num_template_parameter_lists
26517 = parser->num_template_parameter_lists;
26518 parser->num_template_parameter_lists = 0;
26520 /* Look for the type-specifier. */
26521 cp_parser_type_specifier (parser,
26522 CP_PARSER_FLAGS_NONE,
26523 /*decl_specs=*/NULL,
26524 /*is_declarator=*/true,
26525 /*declares_class_or_enum=*/NULL,
26526 /*is_cv_qualifier=*/NULL);
26528 parser->num_template_parameter_lists
26529 = saved_num_template_parameter_lists;
26531 /* Leave the scope of the class. */
26532 if (pushed_scope)
26533 pop_scope (pushed_scope);
26535 constructor_p = !cp_parser_error_occurred (parser);
26539 /* We did not really want to consume any tokens. */
26540 cp_parser_abort_tentative_parse (parser);
26542 return constructor_p;
26545 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26546 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26547 they must be performed once we are in the scope of the function.
26549 Returns the function defined. */
26551 static tree
26552 cp_parser_function_definition_from_specifiers_and_declarator
26553 (cp_parser* parser,
26554 cp_decl_specifier_seq *decl_specifiers,
26555 tree attributes,
26556 const cp_declarator *declarator)
26558 tree fn;
26559 bool success_p;
26561 /* Begin the function-definition. */
26562 success_p = start_function (decl_specifiers, declarator, attributes);
26564 /* The things we're about to see are not directly qualified by any
26565 template headers we've seen thus far. */
26566 reset_specialization ();
26568 /* If there were names looked up in the decl-specifier-seq that we
26569 did not check, check them now. We must wait until we are in the
26570 scope of the function to perform the checks, since the function
26571 might be a friend. */
26572 perform_deferred_access_checks (tf_warning_or_error);
26574 if (success_p)
26576 cp_finalize_omp_declare_simd (parser, current_function_decl);
26577 parser->omp_declare_simd = NULL;
26578 cp_finalize_oacc_routine (parser, current_function_decl, true);
26579 parser->oacc_routine = NULL;
26582 if (!success_p)
26584 /* Skip the entire function. */
26585 cp_parser_skip_to_end_of_block_or_statement (parser);
26586 fn = error_mark_node;
26588 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26590 /* Seen already, skip it. An error message has already been output. */
26591 cp_parser_skip_to_end_of_block_or_statement (parser);
26592 fn = current_function_decl;
26593 current_function_decl = NULL_TREE;
26594 /* If this is a function from a class, pop the nested class. */
26595 if (current_class_name)
26596 pop_nested_class ();
26598 else
26600 timevar_id_t tv;
26601 if (DECL_DECLARED_INLINE_P (current_function_decl))
26602 tv = TV_PARSE_INLINE;
26603 else
26604 tv = TV_PARSE_FUNC;
26605 timevar_push (tv);
26606 fn = cp_parser_function_definition_after_declarator (parser,
26607 /*inline_p=*/false);
26608 timevar_pop (tv);
26611 return fn;
26614 /* Parse the part of a function-definition that follows the
26615 declarator. INLINE_P is TRUE iff this function is an inline
26616 function defined within a class-specifier.
26618 Returns the function defined. */
26620 static tree
26621 cp_parser_function_definition_after_declarator (cp_parser* parser,
26622 bool inline_p)
26624 tree fn;
26625 bool saved_in_unbraced_linkage_specification_p;
26626 bool saved_in_function_body;
26627 unsigned saved_num_template_parameter_lists;
26628 cp_token *token;
26629 bool fully_implicit_function_template_p
26630 = parser->fully_implicit_function_template_p;
26631 parser->fully_implicit_function_template_p = false;
26632 tree implicit_template_parms
26633 = parser->implicit_template_parms;
26634 parser->implicit_template_parms = 0;
26635 cp_binding_level* implicit_template_scope
26636 = parser->implicit_template_scope;
26637 parser->implicit_template_scope = 0;
26639 saved_in_function_body = parser->in_function_body;
26640 parser->in_function_body = true;
26641 /* If the next token is `return', then the code may be trying to
26642 make use of the "named return value" extension that G++ used to
26643 support. */
26644 token = cp_lexer_peek_token (parser->lexer);
26645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26647 /* Consume the `return' keyword. */
26648 cp_lexer_consume_token (parser->lexer);
26649 /* Look for the identifier that indicates what value is to be
26650 returned. */
26651 cp_parser_identifier (parser);
26652 /* Issue an error message. */
26653 error_at (token->location,
26654 "named return values are no longer supported");
26655 /* Skip tokens until we reach the start of the function body. */
26656 while (true)
26658 cp_token *token = cp_lexer_peek_token (parser->lexer);
26659 if (token->type == CPP_OPEN_BRACE
26660 || token->type == CPP_EOF
26661 || token->type == CPP_PRAGMA_EOL)
26662 break;
26663 cp_lexer_consume_token (parser->lexer);
26666 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26667 anything declared inside `f'. */
26668 saved_in_unbraced_linkage_specification_p
26669 = parser->in_unbraced_linkage_specification_p;
26670 parser->in_unbraced_linkage_specification_p = false;
26671 /* Inside the function, surrounding template-parameter-lists do not
26672 apply. */
26673 saved_num_template_parameter_lists
26674 = parser->num_template_parameter_lists;
26675 parser->num_template_parameter_lists = 0;
26677 /* If the next token is `try', `__transaction_atomic', or
26678 `__transaction_relaxed`, then we are looking at either function-try-block
26679 or function-transaction-block. Note that all of these include the
26680 function-body. */
26681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26682 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26683 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26684 RID_TRANSACTION_RELAXED))
26685 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26687 cp_parser_function_try_block (parser);
26688 else
26689 cp_parser_ctor_initializer_opt_and_function_body
26690 (parser, /*in_function_try_block=*/false);
26692 /* Finish the function. */
26693 fn = finish_function (inline_p);
26694 /* Generate code for it, if necessary. */
26695 expand_or_defer_fn (fn);
26696 /* Restore the saved values. */
26697 parser->in_unbraced_linkage_specification_p
26698 = saved_in_unbraced_linkage_specification_p;
26699 parser->num_template_parameter_lists
26700 = saved_num_template_parameter_lists;
26701 parser->in_function_body = saved_in_function_body;
26703 parser->fully_implicit_function_template_p
26704 = fully_implicit_function_template_p;
26705 parser->implicit_template_parms
26706 = implicit_template_parms;
26707 parser->implicit_template_scope
26708 = implicit_template_scope;
26710 if (parser->fully_implicit_function_template_p)
26711 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26713 return fn;
26716 /* Parse a template-declaration body (following argument list). */
26718 static void
26719 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26720 tree parameter_list,
26721 bool member_p)
26723 tree decl = NULL_TREE;
26724 bool friend_p = false;
26726 /* We just processed one more parameter list. */
26727 ++parser->num_template_parameter_lists;
26729 /* Get the deferred access checks from the parameter list. These
26730 will be checked once we know what is being declared, as for a
26731 member template the checks must be performed in the scope of the
26732 class containing the member. */
26733 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26735 /* Tentatively parse for a new template parameter list, which can either be
26736 the template keyword or a template introduction. */
26737 if (cp_parser_template_declaration_after_export (parser, member_p))
26738 /* OK */;
26739 else if (cxx_dialect >= cxx11
26740 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26741 decl = cp_parser_alias_declaration (parser);
26742 else
26744 /* There are no access checks when parsing a template, as we do not
26745 know if a specialization will be a friend. */
26746 push_deferring_access_checks (dk_no_check);
26747 cp_token *token = cp_lexer_peek_token (parser->lexer);
26748 decl = cp_parser_single_declaration (parser,
26749 checks,
26750 member_p,
26751 /*explicit_specialization_p=*/false,
26752 &friend_p);
26753 pop_deferring_access_checks ();
26755 /* If this is a member template declaration, let the front
26756 end know. */
26757 if (member_p && !friend_p && decl)
26759 if (TREE_CODE (decl) == TYPE_DECL)
26760 cp_parser_check_access_in_redeclaration (decl, token->location);
26762 decl = finish_member_template_decl (decl);
26764 else if (friend_p && decl
26765 && DECL_DECLARES_TYPE_P (decl))
26766 make_friend_class (current_class_type, TREE_TYPE (decl),
26767 /*complain=*/true);
26769 /* We are done with the current parameter list. */
26770 --parser->num_template_parameter_lists;
26772 pop_deferring_access_checks ();
26774 /* Finish up. */
26775 finish_template_decl (parameter_list);
26777 /* Check the template arguments for a literal operator template. */
26778 if (decl
26779 && DECL_DECLARES_FUNCTION_P (decl)
26780 && UDLIT_OPER_P (DECL_NAME (decl)))
26782 bool ok = true;
26783 if (parameter_list == NULL_TREE)
26784 ok = false;
26785 else
26787 int num_parms = TREE_VEC_LENGTH (parameter_list);
26788 if (num_parms == 1)
26790 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26791 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26792 if (TREE_TYPE (parm) != char_type_node
26793 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26794 ok = false;
26796 else if (num_parms == 2 && cxx_dialect >= cxx14)
26798 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26799 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26800 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26801 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26802 if (parm == error_mark_node
26803 || TREE_TYPE (parm) != TREE_TYPE (type)
26804 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26805 ok = false;
26807 else
26808 ok = false;
26810 if (!ok)
26812 if (cxx_dialect >= cxx14)
26813 error ("literal operator template %qD has invalid parameter list."
26814 " Expected non-type template argument pack <char...>"
26815 " or <typename CharT, CharT...>",
26816 decl);
26817 else
26818 error ("literal operator template %qD has invalid parameter list."
26819 " Expected non-type template argument pack <char...>",
26820 decl);
26824 /* Register member declarations. */
26825 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26826 finish_member_declaration (decl);
26827 /* If DECL is a function template, we must return to parse it later.
26828 (Even though there is no definition, there might be default
26829 arguments that need handling.) */
26830 if (member_p && decl
26831 && DECL_DECLARES_FUNCTION_P (decl))
26832 vec_safe_push (unparsed_funs_with_definitions, decl);
26835 /* Parse a template introduction header for a template-declaration. Returns
26836 false if tentative parse fails. */
26838 static bool
26839 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26841 cp_parser_parse_tentatively (parser);
26843 tree saved_scope = parser->scope;
26844 tree saved_object_scope = parser->object_scope;
26845 tree saved_qualifying_scope = parser->qualifying_scope;
26847 /* Look for the optional `::' operator. */
26848 cp_parser_global_scope_opt (parser,
26849 /*current_scope_valid_p=*/false);
26850 /* Look for the nested-name-specifier. */
26851 cp_parser_nested_name_specifier_opt (parser,
26852 /*typename_keyword_p=*/false,
26853 /*check_dependency_p=*/true,
26854 /*type_p=*/false,
26855 /*is_declaration=*/false);
26857 cp_token *token = cp_lexer_peek_token (parser->lexer);
26858 tree concept_name = cp_parser_identifier (parser);
26860 /* Look up the concept for which we will be matching
26861 template parameters. */
26862 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26863 token->location);
26864 parser->scope = saved_scope;
26865 parser->object_scope = saved_object_scope;
26866 parser->qualifying_scope = saved_qualifying_scope;
26868 if (concept_name == error_mark_node)
26869 cp_parser_simulate_error (parser);
26871 /* Look for opening brace for introduction. */
26872 matching_braces braces;
26873 braces.require_open (parser);
26875 if (!cp_parser_parse_definitely (parser))
26876 return false;
26878 push_deferring_access_checks (dk_deferred);
26880 /* Build vector of placeholder parameters and grab
26881 matching identifiers. */
26882 tree introduction_list = cp_parser_introduction_list (parser);
26884 /* The introduction-list shall not be empty. */
26885 int nargs = TREE_VEC_LENGTH (introduction_list);
26886 if (nargs == 0)
26888 error ("empty introduction-list");
26889 return true;
26892 /* Look for closing brace for introduction. */
26893 if (!braces.require_close (parser))
26894 return true;
26896 if (tmpl_decl == error_mark_node)
26898 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26899 token->location);
26900 return true;
26903 /* Build and associate the constraint. */
26904 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26905 if (parms && parms != error_mark_node)
26907 cp_parser_template_declaration_after_parameters (parser, parms,
26908 member_p);
26909 return true;
26912 error_at (token->location, "no matching concept for template-introduction");
26913 return true;
26916 /* Parse a normal template-declaration following the template keyword. */
26918 static void
26919 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26921 tree parameter_list;
26922 bool need_lang_pop;
26923 location_t location = input_location;
26925 /* Look for the `<' token. */
26926 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26927 return;
26928 if (at_class_scope_p () && current_function_decl)
26930 /* 14.5.2.2 [temp.mem]
26932 A local class shall not have member templates. */
26933 error_at (location,
26934 "invalid declaration of member template in local class");
26935 cp_parser_skip_to_end_of_block_or_statement (parser);
26936 return;
26938 /* [temp]
26940 A template ... shall not have C linkage. */
26941 if (current_lang_name == lang_name_c)
26943 error_at (location, "template with C linkage");
26944 maybe_show_extern_c_location ();
26945 /* Give it C++ linkage to avoid confusing other parts of the
26946 front end. */
26947 push_lang_context (lang_name_cplusplus);
26948 need_lang_pop = true;
26950 else
26951 need_lang_pop = false;
26953 /* We cannot perform access checks on the template parameter
26954 declarations until we know what is being declared, just as we
26955 cannot check the decl-specifier list. */
26956 push_deferring_access_checks (dk_deferred);
26958 /* If the next token is `>', then we have an invalid
26959 specialization. Rather than complain about an invalid template
26960 parameter, issue an error message here. */
26961 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26963 cp_parser_error (parser, "invalid explicit specialization");
26964 begin_specialization ();
26965 parameter_list = NULL_TREE;
26967 else
26969 /* Parse the template parameters. */
26970 parameter_list = cp_parser_template_parameter_list (parser);
26973 /* Look for the `>'. */
26974 cp_parser_skip_to_end_of_template_parameter_list (parser);
26976 /* Manage template requirements */
26977 if (flag_concepts)
26979 tree reqs = get_shorthand_constraints (current_template_parms);
26980 if (tree r = cp_parser_requires_clause_opt (parser))
26981 reqs = conjoin_constraints (reqs, normalize_expression (r));
26982 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26985 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26986 member_p);
26988 /* For the erroneous case of a template with C linkage, we pushed an
26989 implicit C++ linkage scope; exit that scope now. */
26990 if (need_lang_pop)
26991 pop_lang_context ();
26994 /* Parse a template-declaration, assuming that the `export' (and
26995 `extern') keywords, if present, has already been scanned. MEMBER_P
26996 is as for cp_parser_template_declaration. */
26998 static bool
26999 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27003 cp_lexer_consume_token (parser->lexer);
27004 cp_parser_explicit_template_declaration (parser, member_p);
27005 return true;
27007 else if (flag_concepts)
27008 return cp_parser_template_introduction (parser, member_p);
27010 return false;
27013 /* Perform the deferred access checks from a template-parameter-list.
27014 CHECKS is a TREE_LIST of access checks, as returned by
27015 get_deferred_access_checks. */
27017 static void
27018 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27020 ++processing_template_parmlist;
27021 perform_access_checks (checks, tf_warning_or_error);
27022 --processing_template_parmlist;
27025 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27026 `function-definition' sequence that follows a template header.
27027 If MEMBER_P is true, this declaration appears in a class scope.
27029 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27030 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27032 static tree
27033 cp_parser_single_declaration (cp_parser* parser,
27034 vec<deferred_access_check, va_gc> *checks,
27035 bool member_p,
27036 bool explicit_specialization_p,
27037 bool* friend_p)
27039 int declares_class_or_enum;
27040 tree decl = NULL_TREE;
27041 cp_decl_specifier_seq decl_specifiers;
27042 bool function_definition_p = false;
27043 cp_token *decl_spec_token_start;
27045 /* This function is only used when processing a template
27046 declaration. */
27047 gcc_assert (innermost_scope_kind () == sk_template_parms
27048 || innermost_scope_kind () == sk_template_spec);
27050 /* Defer access checks until we know what is being declared. */
27051 push_deferring_access_checks (dk_deferred);
27053 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27054 alternative. */
27055 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27056 cp_parser_decl_specifier_seq (parser,
27057 CP_PARSER_FLAGS_OPTIONAL,
27058 &decl_specifiers,
27059 &declares_class_or_enum);
27060 if (friend_p)
27061 *friend_p = cp_parser_friend_p (&decl_specifiers);
27063 /* There are no template typedefs. */
27064 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27066 error_at (decl_spec_token_start->location,
27067 "template declaration of %<typedef%>");
27068 decl = error_mark_node;
27071 /* Gather up the access checks that occurred the
27072 decl-specifier-seq. */
27073 stop_deferring_access_checks ();
27075 /* Check for the declaration of a template class. */
27076 if (declares_class_or_enum)
27078 if (cp_parser_declares_only_class_p (parser)
27079 || (declares_class_or_enum & 2))
27081 // If this is a declaration, but not a definition, associate
27082 // any constraints with the type declaration. Constraints
27083 // are associated with definitions in cp_parser_class_specifier.
27084 if (declares_class_or_enum == 1)
27085 associate_classtype_constraints (decl_specifiers.type);
27087 decl = shadow_tag (&decl_specifiers);
27089 /* In this case:
27091 struct C {
27092 friend template <typename T> struct A<T>::B;
27095 A<T>::B will be represented by a TYPENAME_TYPE, and
27096 therefore not recognized by shadow_tag. */
27097 if (friend_p && *friend_p
27098 && !decl
27099 && decl_specifiers.type
27100 && TYPE_P (decl_specifiers.type))
27101 decl = decl_specifiers.type;
27103 if (decl && decl != error_mark_node)
27104 decl = TYPE_NAME (decl);
27105 else
27106 decl = error_mark_node;
27108 /* Perform access checks for template parameters. */
27109 cp_parser_perform_template_parameter_access_checks (checks);
27111 /* Give a helpful diagnostic for
27112 template <class T> struct A { } a;
27113 if we aren't already recovering from an error. */
27114 if (!cp_parser_declares_only_class_p (parser)
27115 && !seen_error ())
27117 error_at (cp_lexer_peek_token (parser->lexer)->location,
27118 "a class template declaration must not declare "
27119 "anything else");
27120 cp_parser_skip_to_end_of_block_or_statement (parser);
27121 goto out;
27126 /* Complain about missing 'typename' or other invalid type names. */
27127 if (!decl_specifiers.any_type_specifiers_p
27128 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27130 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27131 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27132 the rest of this declaration. */
27133 decl = error_mark_node;
27134 goto out;
27137 /* If it's not a template class, try for a template function. If
27138 the next token is a `;', then this declaration does not declare
27139 anything. But, if there were errors in the decl-specifiers, then
27140 the error might well have come from an attempted class-specifier.
27141 In that case, there's no need to warn about a missing declarator. */
27142 if (!decl
27143 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27144 || decl_specifiers.type != error_mark_node))
27146 decl = cp_parser_init_declarator (parser,
27147 &decl_specifiers,
27148 checks,
27149 /*function_definition_allowed_p=*/true,
27150 member_p,
27151 declares_class_or_enum,
27152 &function_definition_p,
27153 NULL, NULL, NULL);
27155 /* 7.1.1-1 [dcl.stc]
27157 A storage-class-specifier shall not be specified in an explicit
27158 specialization... */
27159 if (decl
27160 && explicit_specialization_p
27161 && decl_specifiers.storage_class != sc_none)
27163 error_at (decl_spec_token_start->location,
27164 "explicit template specialization cannot have a storage class");
27165 decl = error_mark_node;
27168 if (decl && VAR_P (decl))
27169 check_template_variable (decl);
27172 /* Look for a trailing `;' after the declaration. */
27173 if (!function_definition_p
27174 && (decl == error_mark_node
27175 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27176 cp_parser_skip_to_end_of_block_or_statement (parser);
27178 out:
27179 pop_deferring_access_checks ();
27181 /* Clear any current qualification; whatever comes next is the start
27182 of something new. */
27183 parser->scope = NULL_TREE;
27184 parser->qualifying_scope = NULL_TREE;
27185 parser->object_scope = NULL_TREE;
27187 return decl;
27190 /* Parse a cast-expression that is not the operand of a unary "&". */
27192 static cp_expr
27193 cp_parser_simple_cast_expression (cp_parser *parser)
27195 return cp_parser_cast_expression (parser, /*address_p=*/false,
27196 /*cast_p=*/false, /*decltype*/false, NULL);
27199 /* Parse a functional cast to TYPE. Returns an expression
27200 representing the cast. */
27202 static cp_expr
27203 cp_parser_functional_cast (cp_parser* parser, tree type)
27205 vec<tree, va_gc> *vec;
27206 tree expression_list;
27207 cp_expr cast;
27208 bool nonconst_p;
27210 location_t start_loc = input_location;
27212 if (!type)
27213 type = error_mark_node;
27215 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27217 cp_lexer_set_source_position (parser->lexer);
27218 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27219 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27220 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27221 if (TREE_CODE (type) == TYPE_DECL)
27222 type = TREE_TYPE (type);
27224 cast = finish_compound_literal (type, expression_list,
27225 tf_warning_or_error, fcl_functional);
27226 /* Create a location of the form:
27227 type_name{i, f}
27228 ^~~~~~~~~~~~~~~
27229 with caret == start at the start of the type name,
27230 finishing at the closing brace. */
27231 location_t finish_loc
27232 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27233 location_t combined_loc = make_location (start_loc, start_loc,
27234 finish_loc);
27235 cast.set_location (combined_loc);
27236 return cast;
27240 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27241 /*cast_p=*/true,
27242 /*allow_expansion_p=*/true,
27243 /*non_constant_p=*/NULL);
27244 if (vec == NULL)
27245 expression_list = error_mark_node;
27246 else
27248 expression_list = build_tree_list_vec (vec);
27249 release_tree_vector (vec);
27252 cast = build_functional_cast (type, expression_list,
27253 tf_warning_or_error);
27254 /* [expr.const]/1: In an integral constant expression "only type
27255 conversions to integral or enumeration type can be used". */
27256 if (TREE_CODE (type) == TYPE_DECL)
27257 type = TREE_TYPE (type);
27258 if (cast != error_mark_node
27259 && !cast_valid_in_integral_constant_expression_p (type)
27260 && cp_parser_non_integral_constant_expression (parser,
27261 NIC_CONSTRUCTOR))
27262 return error_mark_node;
27264 /* Create a location of the form:
27265 float(i)
27266 ^~~~~~~~
27267 with caret == start at the start of the type name,
27268 finishing at the closing paren. */
27269 location_t finish_loc
27270 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27271 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27272 cast.set_location (combined_loc);
27273 return cast;
27276 /* Save the tokens that make up the body of a member function defined
27277 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27278 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27279 specifiers applied to the declaration. Returns the FUNCTION_DECL
27280 for the member function. */
27282 static tree
27283 cp_parser_save_member_function_body (cp_parser* parser,
27284 cp_decl_specifier_seq *decl_specifiers,
27285 cp_declarator *declarator,
27286 tree attributes)
27288 cp_token *first;
27289 cp_token *last;
27290 tree fn;
27291 bool function_try_block = false;
27293 /* Create the FUNCTION_DECL. */
27294 fn = grokmethod (decl_specifiers, declarator, attributes);
27295 cp_finalize_omp_declare_simd (parser, fn);
27296 cp_finalize_oacc_routine (parser, fn, true);
27297 /* If something went badly wrong, bail out now. */
27298 if (fn == error_mark_node)
27300 /* If there's a function-body, skip it. */
27301 if (cp_parser_token_starts_function_definition_p
27302 (cp_lexer_peek_token (parser->lexer)))
27303 cp_parser_skip_to_end_of_block_or_statement (parser);
27304 return error_mark_node;
27307 /* Remember it, if there default args to post process. */
27308 cp_parser_save_default_args (parser, fn);
27310 /* Save away the tokens that make up the body of the
27311 function. */
27312 first = parser->lexer->next_token;
27314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27315 cp_lexer_consume_token (parser->lexer);
27316 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27317 RID_TRANSACTION_ATOMIC))
27319 cp_lexer_consume_token (parser->lexer);
27320 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27322 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27323 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27324 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27325 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27326 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27328 cp_lexer_consume_token (parser->lexer);
27329 cp_lexer_consume_token (parser->lexer);
27330 cp_lexer_consume_token (parser->lexer);
27331 cp_lexer_consume_token (parser->lexer);
27332 cp_lexer_consume_token (parser->lexer);
27334 else
27335 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27336 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27338 cp_lexer_consume_token (parser->lexer);
27339 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27340 break;
27344 /* Handle function try blocks. */
27345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27347 cp_lexer_consume_token (parser->lexer);
27348 function_try_block = true;
27350 /* We can have braced-init-list mem-initializers before the fn body. */
27351 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27353 cp_lexer_consume_token (parser->lexer);
27354 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27356 /* cache_group will stop after an un-nested { } pair, too. */
27357 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27358 break;
27360 /* variadic mem-inits have ... after the ')'. */
27361 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27362 cp_lexer_consume_token (parser->lexer);
27365 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27366 /* Handle function try blocks. */
27367 if (function_try_block)
27368 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27369 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27370 last = parser->lexer->next_token;
27372 /* Save away the inline definition; we will process it when the
27373 class is complete. */
27374 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27375 DECL_PENDING_INLINE_P (fn) = 1;
27377 /* We need to know that this was defined in the class, so that
27378 friend templates are handled correctly. */
27379 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27381 /* Add FN to the queue of functions to be parsed later. */
27382 vec_safe_push (unparsed_funs_with_definitions, fn);
27384 return fn;
27387 /* Save the tokens that make up the in-class initializer for a non-static
27388 data member. Returns a DEFAULT_ARG. */
27390 static tree
27391 cp_parser_save_nsdmi (cp_parser* parser)
27393 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27396 /* Parse a template-argument-list, as well as the trailing ">" (but
27397 not the opening "<"). See cp_parser_template_argument_list for the
27398 return value. */
27400 static tree
27401 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27403 tree arguments;
27404 tree saved_scope;
27405 tree saved_qualifying_scope;
27406 tree saved_object_scope;
27407 bool saved_greater_than_is_operator_p;
27408 int saved_unevaluated_operand;
27409 int saved_inhibit_evaluation_warnings;
27411 /* [temp.names]
27413 When parsing a template-id, the first non-nested `>' is taken as
27414 the end of the template-argument-list rather than a greater-than
27415 operator. */
27416 saved_greater_than_is_operator_p
27417 = parser->greater_than_is_operator_p;
27418 parser->greater_than_is_operator_p = false;
27419 /* Parsing the argument list may modify SCOPE, so we save it
27420 here. */
27421 saved_scope = parser->scope;
27422 saved_qualifying_scope = parser->qualifying_scope;
27423 saved_object_scope = parser->object_scope;
27424 /* We need to evaluate the template arguments, even though this
27425 template-id may be nested within a "sizeof". */
27426 saved_unevaluated_operand = cp_unevaluated_operand;
27427 cp_unevaluated_operand = 0;
27428 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27429 c_inhibit_evaluation_warnings = 0;
27430 /* Parse the template-argument-list itself. */
27431 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27432 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27433 arguments = NULL_TREE;
27434 else
27435 arguments = cp_parser_template_argument_list (parser);
27436 /* Look for the `>' that ends the template-argument-list. If we find
27437 a '>>' instead, it's probably just a typo. */
27438 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27440 if (cxx_dialect != cxx98)
27442 /* In C++0x, a `>>' in a template argument list or cast
27443 expression is considered to be two separate `>'
27444 tokens. So, change the current token to a `>', but don't
27445 consume it: it will be consumed later when the outer
27446 template argument list (or cast expression) is parsed.
27447 Note that this replacement of `>' for `>>' is necessary
27448 even if we are parsing tentatively: in the tentative
27449 case, after calling
27450 cp_parser_enclosed_template_argument_list we will always
27451 throw away all of the template arguments and the first
27452 closing `>', either because the template argument list
27453 was erroneous or because we are replacing those tokens
27454 with a CPP_TEMPLATE_ID token. The second `>' (which will
27455 not have been thrown away) is needed either to close an
27456 outer template argument list or to complete a new-style
27457 cast. */
27458 cp_token *token = cp_lexer_peek_token (parser->lexer);
27459 token->type = CPP_GREATER;
27461 else if (!saved_greater_than_is_operator_p)
27463 /* If we're in a nested template argument list, the '>>' has
27464 to be a typo for '> >'. We emit the error message, but we
27465 continue parsing and we push a '>' as next token, so that
27466 the argument list will be parsed correctly. Note that the
27467 global source location is still on the token before the
27468 '>>', so we need to say explicitly where we want it. */
27469 cp_token *token = cp_lexer_peek_token (parser->lexer);
27470 gcc_rich_location richloc (token->location);
27471 richloc.add_fixit_replace ("> >");
27472 error_at (&richloc, "%<>>%> should be %<> >%> "
27473 "within a nested template argument list");
27475 token->type = CPP_GREATER;
27477 else
27479 /* If this is not a nested template argument list, the '>>'
27480 is a typo for '>'. Emit an error message and continue.
27481 Same deal about the token location, but here we can get it
27482 right by consuming the '>>' before issuing the diagnostic. */
27483 cp_token *token = cp_lexer_consume_token (parser->lexer);
27484 error_at (token->location,
27485 "spurious %<>>%>, use %<>%> to terminate "
27486 "a template argument list");
27489 else
27490 cp_parser_skip_to_end_of_template_parameter_list (parser);
27491 /* The `>' token might be a greater-than operator again now. */
27492 parser->greater_than_is_operator_p
27493 = saved_greater_than_is_operator_p;
27494 /* Restore the SAVED_SCOPE. */
27495 parser->scope = saved_scope;
27496 parser->qualifying_scope = saved_qualifying_scope;
27497 parser->object_scope = saved_object_scope;
27498 cp_unevaluated_operand = saved_unevaluated_operand;
27499 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27501 return arguments;
27504 /* MEMBER_FUNCTION is a member function, or a friend. If default
27505 arguments, or the body of the function have not yet been parsed,
27506 parse them now. */
27508 static void
27509 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27511 timevar_push (TV_PARSE_INMETH);
27512 /* If this member is a template, get the underlying
27513 FUNCTION_DECL. */
27514 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27515 member_function = DECL_TEMPLATE_RESULT (member_function);
27517 /* There should not be any class definitions in progress at this
27518 point; the bodies of members are only parsed outside of all class
27519 definitions. */
27520 gcc_assert (parser->num_classes_being_defined == 0);
27521 /* While we're parsing the member functions we might encounter more
27522 classes. We want to handle them right away, but we don't want
27523 them getting mixed up with functions that are currently in the
27524 queue. */
27525 push_unparsed_function_queues (parser);
27527 /* Make sure that any template parameters are in scope. */
27528 maybe_begin_member_template_processing (member_function);
27530 /* If the body of the function has not yet been parsed, parse it
27531 now. */
27532 if (DECL_PENDING_INLINE_P (member_function))
27534 tree function_scope;
27535 cp_token_cache *tokens;
27537 /* The function is no longer pending; we are processing it. */
27538 tokens = DECL_PENDING_INLINE_INFO (member_function);
27539 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27540 DECL_PENDING_INLINE_P (member_function) = 0;
27542 /* If this is a local class, enter the scope of the containing
27543 function. */
27544 function_scope = current_function_decl;
27545 if (function_scope)
27546 push_function_context ();
27548 /* Push the body of the function onto the lexer stack. */
27549 cp_parser_push_lexer_for_tokens (parser, tokens);
27551 /* Let the front end know that we going to be defining this
27552 function. */
27553 start_preparsed_function (member_function, NULL_TREE,
27554 SF_PRE_PARSED | SF_INCLASS_INLINE);
27556 /* Don't do access checking if it is a templated function. */
27557 if (processing_template_decl)
27558 push_deferring_access_checks (dk_no_check);
27560 /* #pragma omp declare reduction needs special parsing. */
27561 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27563 parser->lexer->in_pragma = true;
27564 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27565 finish_function (/*inline_p=*/true);
27566 cp_check_omp_declare_reduction (member_function);
27568 else
27569 /* Now, parse the body of the function. */
27570 cp_parser_function_definition_after_declarator (parser,
27571 /*inline_p=*/true);
27573 if (processing_template_decl)
27574 pop_deferring_access_checks ();
27576 /* Leave the scope of the containing function. */
27577 if (function_scope)
27578 pop_function_context ();
27579 cp_parser_pop_lexer (parser);
27582 /* Remove any template parameters from the symbol table. */
27583 maybe_end_member_template_processing ();
27585 /* Restore the queue. */
27586 pop_unparsed_function_queues (parser);
27587 timevar_pop (TV_PARSE_INMETH);
27590 /* If DECL contains any default args, remember it on the unparsed
27591 functions queue. */
27593 static void
27594 cp_parser_save_default_args (cp_parser* parser, tree decl)
27596 tree probe;
27598 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27599 probe;
27600 probe = TREE_CHAIN (probe))
27601 if (TREE_PURPOSE (probe))
27603 cp_default_arg_entry entry = {current_class_type, decl};
27604 vec_safe_push (unparsed_funs_with_default_args, entry);
27605 break;
27609 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27610 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27611 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27612 from the parameter-type-list. */
27614 static tree
27615 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27616 tree default_arg, tree parmtype)
27618 cp_token_cache *tokens;
27619 tree parsed_arg;
27620 bool dummy;
27622 if (default_arg == error_mark_node)
27623 return error_mark_node;
27625 /* Push the saved tokens for the default argument onto the parser's
27626 lexer stack. */
27627 tokens = DEFARG_TOKENS (default_arg);
27628 cp_parser_push_lexer_for_tokens (parser, tokens);
27630 start_lambda_scope (decl);
27632 /* Parse the default argument. */
27633 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27634 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27635 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27637 finish_lambda_scope ();
27639 if (parsed_arg == error_mark_node)
27640 cp_parser_skip_to_end_of_statement (parser);
27642 if (!processing_template_decl)
27644 /* In a non-template class, check conversions now. In a template,
27645 we'll wait and instantiate these as needed. */
27646 if (TREE_CODE (decl) == PARM_DECL)
27647 parsed_arg = check_default_argument (parmtype, parsed_arg,
27648 tf_warning_or_error);
27649 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27650 parsed_arg = error_mark_node;
27651 else
27652 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27655 /* If the token stream has not been completely used up, then
27656 there was extra junk after the end of the default
27657 argument. */
27658 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27660 if (TREE_CODE (decl) == PARM_DECL)
27661 cp_parser_error (parser, "expected %<,%>");
27662 else
27663 cp_parser_error (parser, "expected %<;%>");
27666 /* Revert to the main lexer. */
27667 cp_parser_pop_lexer (parser);
27669 return parsed_arg;
27672 /* FIELD is a non-static data member with an initializer which we saved for
27673 later; parse it now. */
27675 static void
27676 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27678 tree def;
27680 maybe_begin_member_template_processing (field);
27682 push_unparsed_function_queues (parser);
27683 def = cp_parser_late_parse_one_default_arg (parser, field,
27684 DECL_INITIAL (field),
27685 NULL_TREE);
27686 pop_unparsed_function_queues (parser);
27688 maybe_end_member_template_processing ();
27690 DECL_INITIAL (field) = def;
27693 /* FN is a FUNCTION_DECL which may contains a parameter with an
27694 unparsed DEFAULT_ARG. Parse the default args now. This function
27695 assumes that the current scope is the scope in which the default
27696 argument should be processed. */
27698 static void
27699 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27701 bool saved_local_variables_forbidden_p;
27702 tree parm, parmdecl;
27704 /* While we're parsing the default args, we might (due to the
27705 statement expression extension) encounter more classes. We want
27706 to handle them right away, but we don't want them getting mixed
27707 up with default args that are currently in the queue. */
27708 push_unparsed_function_queues (parser);
27710 /* Local variable names (and the `this' keyword) may not appear
27711 in a default argument. */
27712 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27713 parser->local_variables_forbidden_p = true;
27715 push_defarg_context (fn);
27717 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27718 parmdecl = DECL_ARGUMENTS (fn);
27719 parm && parm != void_list_node;
27720 parm = TREE_CHAIN (parm),
27721 parmdecl = DECL_CHAIN (parmdecl))
27723 tree default_arg = TREE_PURPOSE (parm);
27724 tree parsed_arg;
27725 vec<tree, va_gc> *insts;
27726 tree copy;
27727 unsigned ix;
27729 if (!default_arg)
27730 continue;
27732 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27733 /* This can happen for a friend declaration for a function
27734 already declared with default arguments. */
27735 continue;
27737 parsed_arg
27738 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27739 default_arg,
27740 TREE_VALUE (parm));
27741 TREE_PURPOSE (parm) = parsed_arg;
27743 /* Update any instantiations we've already created. */
27744 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27745 vec_safe_iterate (insts, ix, &copy); ix++)
27746 TREE_PURPOSE (copy) = parsed_arg;
27749 pop_defarg_context ();
27751 /* Make sure no default arg is missing. */
27752 check_default_args (fn);
27754 /* Restore the state of local_variables_forbidden_p. */
27755 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27757 /* Restore the queue. */
27758 pop_unparsed_function_queues (parser);
27761 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27763 sizeof ... ( identifier )
27765 where the 'sizeof' token has already been consumed. */
27767 static tree
27768 cp_parser_sizeof_pack (cp_parser *parser)
27770 /* Consume the `...'. */
27771 cp_lexer_consume_token (parser->lexer);
27772 maybe_warn_variadic_templates ();
27774 matching_parens parens;
27775 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27776 if (paren)
27777 parens.consume_open (parser);
27778 else
27779 permerror (cp_lexer_peek_token (parser->lexer)->location,
27780 "%<sizeof...%> argument must be surrounded by parentheses");
27782 cp_token *token = cp_lexer_peek_token (parser->lexer);
27783 tree name = cp_parser_identifier (parser);
27784 if (name == error_mark_node)
27785 return error_mark_node;
27786 /* The name is not qualified. */
27787 parser->scope = NULL_TREE;
27788 parser->qualifying_scope = NULL_TREE;
27789 parser->object_scope = NULL_TREE;
27790 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27791 if (expr == error_mark_node)
27792 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27793 token->location);
27794 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27795 expr = TREE_TYPE (expr);
27796 else if (TREE_CODE (expr) == CONST_DECL)
27797 expr = DECL_INITIAL (expr);
27798 expr = make_pack_expansion (expr);
27799 PACK_EXPANSION_SIZEOF_P (expr) = true;
27801 if (paren)
27802 parens.require_close (parser);
27804 return expr;
27807 /* Parse the operand of `sizeof' (or a similar operator). Returns
27808 either a TYPE or an expression, depending on the form of the
27809 input. The KEYWORD indicates which kind of expression we have
27810 encountered. */
27812 static tree
27813 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27815 tree expr = NULL_TREE;
27816 const char *saved_message;
27817 char *tmp;
27818 bool saved_integral_constant_expression_p;
27819 bool saved_non_integral_constant_expression_p;
27821 /* If it's a `...', then we are computing the length of a parameter
27822 pack. */
27823 if (keyword == RID_SIZEOF
27824 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27825 return cp_parser_sizeof_pack (parser);
27827 /* Types cannot be defined in a `sizeof' expression. Save away the
27828 old message. */
27829 saved_message = parser->type_definition_forbidden_message;
27830 /* And create the new one. */
27831 tmp = concat ("types may not be defined in %<",
27832 IDENTIFIER_POINTER (ridpointers[keyword]),
27833 "%> expressions", NULL);
27834 parser->type_definition_forbidden_message = tmp;
27836 /* The restrictions on constant-expressions do not apply inside
27837 sizeof expressions. */
27838 saved_integral_constant_expression_p
27839 = parser->integral_constant_expression_p;
27840 saved_non_integral_constant_expression_p
27841 = parser->non_integral_constant_expression_p;
27842 parser->integral_constant_expression_p = false;
27844 /* Do not actually evaluate the expression. */
27845 ++cp_unevaluated_operand;
27846 ++c_inhibit_evaluation_warnings;
27847 /* If it's a `(', then we might be looking at the type-id
27848 construction. */
27849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27851 tree type = NULL_TREE;
27853 /* We can't be sure yet whether we're looking at a type-id or an
27854 expression. */
27855 cp_parser_parse_tentatively (parser);
27857 matching_parens parens;
27858 parens.consume_open (parser);
27860 /* Note: as a GNU Extension, compound literals are considered
27861 postfix-expressions as they are in C99, so they are valid
27862 arguments to sizeof. See comment in cp_parser_cast_expression
27863 for details. */
27864 if (cp_parser_compound_literal_p (parser))
27865 cp_parser_simulate_error (parser);
27866 else
27868 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27869 parser->in_type_id_in_expr_p = true;
27870 /* Look for the type-id. */
27871 type = cp_parser_type_id (parser);
27872 /* Look for the closing `)'. */
27873 parens.require_close (parser);
27874 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27877 /* If all went well, then we're done. */
27878 if (cp_parser_parse_definitely (parser))
27880 cp_decl_specifier_seq decl_specs;
27882 /* Build a trivial decl-specifier-seq. */
27883 clear_decl_specs (&decl_specs);
27884 decl_specs.type = type;
27886 /* Call grokdeclarator to figure out what type this is. */
27887 expr = grokdeclarator (NULL,
27888 &decl_specs,
27889 TYPENAME,
27890 /*initialized=*/0,
27891 /*attrlist=*/NULL);
27895 /* If the type-id production did not work out, then we must be
27896 looking at the unary-expression production. */
27897 if (!expr)
27898 expr = cp_parser_unary_expression (parser);
27900 /* Go back to evaluating expressions. */
27901 --cp_unevaluated_operand;
27902 --c_inhibit_evaluation_warnings;
27904 /* Free the message we created. */
27905 free (tmp);
27906 /* And restore the old one. */
27907 parser->type_definition_forbidden_message = saved_message;
27908 parser->integral_constant_expression_p
27909 = saved_integral_constant_expression_p;
27910 parser->non_integral_constant_expression_p
27911 = saved_non_integral_constant_expression_p;
27913 return expr;
27916 /* If the current declaration has no declarator, return true. */
27918 static bool
27919 cp_parser_declares_only_class_p (cp_parser *parser)
27921 /* If the next token is a `;' or a `,' then there is no
27922 declarator. */
27923 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27924 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27927 /* Update the DECL_SPECS to reflect the storage class indicated by
27928 KEYWORD. */
27930 static void
27931 cp_parser_set_storage_class (cp_parser *parser,
27932 cp_decl_specifier_seq *decl_specs,
27933 enum rid keyword,
27934 cp_token *token)
27936 cp_storage_class storage_class;
27938 if (parser->in_unbraced_linkage_specification_p)
27940 error_at (token->location, "invalid use of %qD in linkage specification",
27941 ridpointers[keyword]);
27942 return;
27944 else if (decl_specs->storage_class != sc_none)
27946 decl_specs->conflicting_specifiers_p = true;
27947 return;
27950 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27951 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27952 && decl_specs->gnu_thread_keyword_p)
27954 pedwarn (decl_specs->locations[ds_thread], 0,
27955 "%<__thread%> before %qD", ridpointers[keyword]);
27958 switch (keyword)
27960 case RID_AUTO:
27961 storage_class = sc_auto;
27962 break;
27963 case RID_REGISTER:
27964 storage_class = sc_register;
27965 break;
27966 case RID_STATIC:
27967 storage_class = sc_static;
27968 break;
27969 case RID_EXTERN:
27970 storage_class = sc_extern;
27971 break;
27972 case RID_MUTABLE:
27973 storage_class = sc_mutable;
27974 break;
27975 default:
27976 gcc_unreachable ();
27978 decl_specs->storage_class = storage_class;
27979 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27981 /* A storage class specifier cannot be applied alongside a typedef
27982 specifier. If there is a typedef specifier present then set
27983 conflicting_specifiers_p which will trigger an error later
27984 on in grokdeclarator. */
27985 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27986 decl_specs->conflicting_specifiers_p = true;
27989 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27990 is true, the type is a class or enum definition. */
27992 static void
27993 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27994 tree type_spec,
27995 cp_token *token,
27996 bool type_definition_p)
27998 decl_specs->any_specifiers_p = true;
28000 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28001 (with, for example, in "typedef int wchar_t;") we remember that
28002 this is what happened. In system headers, we ignore these
28003 declarations so that G++ can work with system headers that are not
28004 C++-safe. */
28005 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28006 && !type_definition_p
28007 && (type_spec == boolean_type_node
28008 || type_spec == char16_type_node
28009 || type_spec == char32_type_node
28010 || type_spec == wchar_type_node)
28011 && (decl_specs->type
28012 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28013 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28014 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28015 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28017 decl_specs->redefined_builtin_type = type_spec;
28018 set_and_check_decl_spec_loc (decl_specs,
28019 ds_redefined_builtin_type_spec,
28020 token);
28021 if (!decl_specs->type)
28023 decl_specs->type = type_spec;
28024 decl_specs->type_definition_p = false;
28025 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28028 else if (decl_specs->type)
28029 decl_specs->multiple_types_p = true;
28030 else
28032 decl_specs->type = type_spec;
28033 decl_specs->type_definition_p = type_definition_p;
28034 decl_specs->redefined_builtin_type = NULL_TREE;
28035 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28039 /* True iff TOKEN is the GNU keyword __thread. */
28041 static bool
28042 token_is__thread (cp_token *token)
28044 gcc_assert (token->keyword == RID_THREAD);
28045 return id_equal (token->u.value, "__thread");
28048 /* Set the location for a declarator specifier and check if it is
28049 duplicated.
28051 DECL_SPECS is the sequence of declarator specifiers onto which to
28052 set the location.
28054 DS is the single declarator specifier to set which location is to
28055 be set onto the existing sequence of declarators.
28057 LOCATION is the location for the declarator specifier to
28058 consider. */
28060 static void
28061 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28062 cp_decl_spec ds, cp_token *token)
28064 gcc_assert (ds < ds_last);
28066 if (decl_specs == NULL)
28067 return;
28069 source_location location = token->location;
28071 if (decl_specs->locations[ds] == 0)
28073 decl_specs->locations[ds] = location;
28074 if (ds == ds_thread)
28075 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28077 else
28079 if (ds == ds_long)
28081 if (decl_specs->locations[ds_long_long] != 0)
28082 error_at (location,
28083 "%<long long long%> is too long for GCC");
28084 else
28086 decl_specs->locations[ds_long_long] = location;
28087 pedwarn_cxx98 (location,
28088 OPT_Wlong_long,
28089 "ISO C++ 1998 does not support %<long long%>");
28092 else if (ds == ds_thread)
28094 bool gnu = token_is__thread (token);
28095 if (gnu != decl_specs->gnu_thread_keyword_p)
28096 error_at (location,
28097 "both %<__thread%> and %<thread_local%> specified");
28098 else
28100 gcc_rich_location richloc (location);
28101 richloc.add_fixit_remove ();
28102 error_at (&richloc, "duplicate %qD", token->u.value);
28105 else
28107 static const char *const decl_spec_names[] = {
28108 "signed",
28109 "unsigned",
28110 "short",
28111 "long",
28112 "const",
28113 "volatile",
28114 "restrict",
28115 "inline",
28116 "virtual",
28117 "explicit",
28118 "friend",
28119 "typedef",
28120 "using",
28121 "constexpr",
28122 "__complex"
28124 gcc_rich_location richloc (location);
28125 richloc.add_fixit_remove ();
28126 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28131 /* Return true iff the declarator specifier DS is present in the
28132 sequence of declarator specifiers DECL_SPECS. */
28134 bool
28135 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28136 cp_decl_spec ds)
28138 gcc_assert (ds < ds_last);
28140 if (decl_specs == NULL)
28141 return false;
28143 return decl_specs->locations[ds] != 0;
28146 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28147 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28149 static bool
28150 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28152 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28155 /* Issue an error message indicating that TOKEN_DESC was expected.
28156 If KEYWORD is true, it indicated this function is called by
28157 cp_parser_require_keword and the required token can only be
28158 a indicated keyword.
28160 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28161 within any error as the location of an "opening" token matching
28162 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28163 RT_CLOSE_PAREN). */
28165 static void
28166 cp_parser_required_error (cp_parser *parser,
28167 required_token token_desc,
28168 bool keyword,
28169 location_t matching_location)
28171 if (cp_parser_simulate_error (parser))
28172 return;
28174 const char *gmsgid = NULL;
28175 switch (token_desc)
28177 case RT_NEW:
28178 gmsgid = G_("expected %<new%>");
28179 break;
28180 case RT_DELETE:
28181 gmsgid = G_("expected %<delete%>");
28182 break;
28183 case RT_RETURN:
28184 gmsgid = G_("expected %<return%>");
28185 break;
28186 case RT_WHILE:
28187 gmsgid = G_("expected %<while%>");
28188 break;
28189 case RT_EXTERN:
28190 gmsgid = G_("expected %<extern%>");
28191 break;
28192 case RT_STATIC_ASSERT:
28193 gmsgid = G_("expected %<static_assert%>");
28194 break;
28195 case RT_DECLTYPE:
28196 gmsgid = G_("expected %<decltype%>");
28197 break;
28198 case RT_OPERATOR:
28199 gmsgid = G_("expected %<operator%>");
28200 break;
28201 case RT_CLASS:
28202 gmsgid = G_("expected %<class%>");
28203 break;
28204 case RT_TEMPLATE:
28205 gmsgid = G_("expected %<template%>");
28206 break;
28207 case RT_NAMESPACE:
28208 gmsgid = G_("expected %<namespace%>");
28209 break;
28210 case RT_USING:
28211 gmsgid = G_("expected %<using%>");
28212 break;
28213 case RT_ASM:
28214 gmsgid = G_("expected %<asm%>");
28215 break;
28216 case RT_TRY:
28217 gmsgid = G_("expected %<try%>");
28218 break;
28219 case RT_CATCH:
28220 gmsgid = G_("expected %<catch%>");
28221 break;
28222 case RT_THROW:
28223 gmsgid = G_("expected %<throw%>");
28224 break;
28225 case RT_LABEL:
28226 gmsgid = G_("expected %<__label__%>");
28227 break;
28228 case RT_AT_TRY:
28229 gmsgid = G_("expected %<@try%>");
28230 break;
28231 case RT_AT_SYNCHRONIZED:
28232 gmsgid = G_("expected %<@synchronized%>");
28233 break;
28234 case RT_AT_THROW:
28235 gmsgid = G_("expected %<@throw%>");
28236 break;
28237 case RT_TRANSACTION_ATOMIC:
28238 gmsgid = G_("expected %<__transaction_atomic%>");
28239 break;
28240 case RT_TRANSACTION_RELAXED:
28241 gmsgid = G_("expected %<__transaction_relaxed%>");
28242 break;
28243 default:
28244 break;
28247 if (!gmsgid && !keyword)
28249 switch (token_desc)
28251 case RT_SEMICOLON:
28252 gmsgid = G_("expected %<;%>");
28253 break;
28254 case RT_OPEN_PAREN:
28255 gmsgid = G_("expected %<(%>");
28256 break;
28257 case RT_CLOSE_BRACE:
28258 gmsgid = G_("expected %<}%>");
28259 break;
28260 case RT_OPEN_BRACE:
28261 gmsgid = G_("expected %<{%>");
28262 break;
28263 case RT_CLOSE_SQUARE:
28264 gmsgid = G_("expected %<]%>");
28265 break;
28266 case RT_OPEN_SQUARE:
28267 gmsgid = G_("expected %<[%>");
28268 break;
28269 case RT_COMMA:
28270 gmsgid = G_("expected %<,%>");
28271 break;
28272 case RT_SCOPE:
28273 gmsgid = G_("expected %<::%>");
28274 break;
28275 case RT_LESS:
28276 gmsgid = G_("expected %<<%>");
28277 break;
28278 case RT_GREATER:
28279 gmsgid = G_("expected %<>%>");
28280 break;
28281 case RT_EQ:
28282 gmsgid = G_("expected %<=%>");
28283 break;
28284 case RT_ELLIPSIS:
28285 gmsgid = G_("expected %<...%>");
28286 break;
28287 case RT_MULT:
28288 gmsgid = G_("expected %<*%>");
28289 break;
28290 case RT_COMPL:
28291 gmsgid = G_("expected %<~%>");
28292 break;
28293 case RT_COLON:
28294 gmsgid = G_("expected %<:%>");
28295 break;
28296 case RT_COLON_SCOPE:
28297 gmsgid = G_("expected %<:%> or %<::%>");
28298 break;
28299 case RT_CLOSE_PAREN:
28300 gmsgid = G_("expected %<)%>");
28301 break;
28302 case RT_COMMA_CLOSE_PAREN:
28303 gmsgid = G_("expected %<,%> or %<)%>");
28304 break;
28305 case RT_PRAGMA_EOL:
28306 gmsgid = G_("expected end of line");
28307 break;
28308 case RT_NAME:
28309 gmsgid = G_("expected identifier");
28310 break;
28311 case RT_SELECT:
28312 gmsgid = G_("expected selection-statement");
28313 break;
28314 case RT_ITERATION:
28315 gmsgid = G_("expected iteration-statement");
28316 break;
28317 case RT_JUMP:
28318 gmsgid = G_("expected jump-statement");
28319 break;
28320 case RT_CLASS_KEY:
28321 gmsgid = G_("expected class-key");
28322 break;
28323 case RT_CLASS_TYPENAME_TEMPLATE:
28324 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28325 break;
28326 default:
28327 gcc_unreachable ();
28331 if (gmsgid)
28332 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28336 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28337 issue an error message indicating that TOKEN_DESC was expected.
28339 Returns the token consumed, if the token had the appropriate type.
28340 Otherwise, returns NULL.
28342 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28343 within any error as the location of an "opening" token matching
28344 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28345 RT_CLOSE_PAREN). */
28347 static cp_token *
28348 cp_parser_require (cp_parser* parser,
28349 enum cpp_ttype type,
28350 required_token token_desc,
28351 location_t matching_location)
28353 if (cp_lexer_next_token_is (parser->lexer, type))
28354 return cp_lexer_consume_token (parser->lexer);
28355 else
28357 /* Output the MESSAGE -- unless we're parsing tentatively. */
28358 if (!cp_parser_simulate_error (parser))
28359 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28360 matching_location);
28361 return NULL;
28365 /* An error message is produced if the next token is not '>'.
28366 All further tokens are skipped until the desired token is
28367 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28369 static void
28370 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28372 /* Current level of '< ... >'. */
28373 unsigned level = 0;
28374 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28375 unsigned nesting_depth = 0;
28377 /* Are we ready, yet? If not, issue error message. */
28378 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28379 return;
28381 /* Skip tokens until the desired token is found. */
28382 while (true)
28384 /* Peek at the next token. */
28385 switch (cp_lexer_peek_token (parser->lexer)->type)
28387 case CPP_LESS:
28388 if (!nesting_depth)
28389 ++level;
28390 break;
28392 case CPP_RSHIFT:
28393 if (cxx_dialect == cxx98)
28394 /* C++0x views the `>>' operator as two `>' tokens, but
28395 C++98 does not. */
28396 break;
28397 else if (!nesting_depth && level-- == 0)
28399 /* We've hit a `>>' where the first `>' closes the
28400 template argument list, and the second `>' is
28401 spurious. Just consume the `>>' and stop; we've
28402 already produced at least one error. */
28403 cp_lexer_consume_token (parser->lexer);
28404 return;
28406 /* Fall through for C++0x, so we handle the second `>' in
28407 the `>>'. */
28408 gcc_fallthrough ();
28410 case CPP_GREATER:
28411 if (!nesting_depth && level-- == 0)
28413 /* We've reached the token we want, consume it and stop. */
28414 cp_lexer_consume_token (parser->lexer);
28415 return;
28417 break;
28419 case CPP_OPEN_PAREN:
28420 case CPP_OPEN_SQUARE:
28421 ++nesting_depth;
28422 break;
28424 case CPP_CLOSE_PAREN:
28425 case CPP_CLOSE_SQUARE:
28426 if (nesting_depth-- == 0)
28427 return;
28428 break;
28430 case CPP_EOF:
28431 case CPP_PRAGMA_EOL:
28432 case CPP_SEMICOLON:
28433 case CPP_OPEN_BRACE:
28434 case CPP_CLOSE_BRACE:
28435 /* The '>' was probably forgotten, don't look further. */
28436 return;
28438 default:
28439 break;
28442 /* Consume this token. */
28443 cp_lexer_consume_token (parser->lexer);
28447 /* If the next token is the indicated keyword, consume it. Otherwise,
28448 issue an error message indicating that TOKEN_DESC was expected.
28450 Returns the token consumed, if the token had the appropriate type.
28451 Otherwise, returns NULL. */
28453 static cp_token *
28454 cp_parser_require_keyword (cp_parser* parser,
28455 enum rid keyword,
28456 required_token token_desc)
28458 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28460 if (token && token->keyword != keyword)
28462 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28463 UNKNOWN_LOCATION);
28464 return NULL;
28467 return token;
28470 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28471 function-definition. */
28473 static bool
28474 cp_parser_token_starts_function_definition_p (cp_token* token)
28476 return (/* An ordinary function-body begins with an `{'. */
28477 token->type == CPP_OPEN_BRACE
28478 /* A ctor-initializer begins with a `:'. */
28479 || token->type == CPP_COLON
28480 /* A function-try-block begins with `try'. */
28481 || token->keyword == RID_TRY
28482 /* A function-transaction-block begins with `__transaction_atomic'
28483 or `__transaction_relaxed'. */
28484 || token->keyword == RID_TRANSACTION_ATOMIC
28485 || token->keyword == RID_TRANSACTION_RELAXED
28486 /* The named return value extension begins with `return'. */
28487 || token->keyword == RID_RETURN);
28490 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28491 definition. */
28493 static bool
28494 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28496 cp_token *token;
28498 token = cp_lexer_peek_token (parser->lexer);
28499 return (token->type == CPP_OPEN_BRACE
28500 || (token->type == CPP_COLON
28501 && !parser->colon_doesnt_start_class_def_p));
28504 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28505 C++0x) ending a template-argument. */
28507 static bool
28508 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28510 cp_token *token;
28512 token = cp_lexer_peek_token (parser->lexer);
28513 return (token->type == CPP_COMMA
28514 || token->type == CPP_GREATER
28515 || token->type == CPP_ELLIPSIS
28516 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28519 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28520 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28522 static bool
28523 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28524 size_t n)
28526 cp_token *token;
28528 token = cp_lexer_peek_nth_token (parser->lexer, n);
28529 if (token->type == CPP_LESS)
28530 return true;
28531 /* Check for the sequence `<::' in the original code. It would be lexed as
28532 `[:', where `[' is a digraph, and there is no whitespace before
28533 `:'. */
28534 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28536 cp_token *token2;
28537 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28538 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28539 return true;
28541 return false;
28544 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28545 or none_type otherwise. */
28547 static enum tag_types
28548 cp_parser_token_is_class_key (cp_token* token)
28550 switch (token->keyword)
28552 case RID_CLASS:
28553 return class_type;
28554 case RID_STRUCT:
28555 return record_type;
28556 case RID_UNION:
28557 return union_type;
28559 default:
28560 return none_type;
28564 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28565 or none_type otherwise or if the token is null. */
28567 static enum tag_types
28568 cp_parser_token_is_type_parameter_key (cp_token* token)
28570 if (!token)
28571 return none_type;
28573 switch (token->keyword)
28575 case RID_CLASS:
28576 return class_type;
28577 case RID_TYPENAME:
28578 return typename_type;
28580 default:
28581 return none_type;
28585 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28587 static void
28588 cp_parser_check_class_key (enum tag_types class_key, tree type)
28590 if (type == error_mark_node)
28591 return;
28592 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28594 if (permerror (input_location, "%qs tag used in naming %q#T",
28595 class_key == union_type ? "union"
28596 : class_key == record_type ? "struct" : "class",
28597 type))
28598 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28599 "%q#T was previously declared here", type);
28603 /* Issue an error message if DECL is redeclared with different
28604 access than its original declaration [class.access.spec/3].
28605 This applies to nested classes, nested class templates and
28606 enumerations [class.mem/1]. */
28608 static void
28609 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28611 if (!decl
28612 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28613 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28614 return;
28616 if ((TREE_PRIVATE (decl)
28617 != (current_access_specifier == access_private_node))
28618 || (TREE_PROTECTED (decl)
28619 != (current_access_specifier == access_protected_node)))
28620 error_at (location, "%qD redeclared with different access", decl);
28623 /* Look for the `template' keyword, as a syntactic disambiguator.
28624 Return TRUE iff it is present, in which case it will be
28625 consumed. */
28627 static bool
28628 cp_parser_optional_template_keyword (cp_parser *parser)
28630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28632 /* In C++98 the `template' keyword can only be used within templates;
28633 outside templates the parser can always figure out what is a
28634 template and what is not. In C++11, per the resolution of DR 468,
28635 `template' is allowed in cases where it is not strictly necessary. */
28636 if (!processing_template_decl
28637 && pedantic && cxx_dialect == cxx98)
28639 cp_token *token = cp_lexer_peek_token (parser->lexer);
28640 pedwarn (token->location, OPT_Wpedantic,
28641 "in C++98 %<template%> (as a disambiguator) is only "
28642 "allowed within templates");
28643 /* If this part of the token stream is rescanned, the same
28644 error message would be generated. So, we purge the token
28645 from the stream. */
28646 cp_lexer_purge_token (parser->lexer);
28647 return false;
28649 else
28651 /* Consume the `template' keyword. */
28652 cp_lexer_consume_token (parser->lexer);
28653 return true;
28656 return false;
28659 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28660 set PARSER->SCOPE, and perform other related actions. */
28662 static void
28663 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28665 struct tree_check *check_value;
28667 /* Get the stored value. */
28668 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28669 /* Set the scope from the stored value. */
28670 parser->scope = saved_checks_value (check_value);
28671 parser->qualifying_scope = check_value->qualifying_scope;
28672 parser->object_scope = NULL_TREE;
28675 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28676 encounter the end of a block before what we were looking for. */
28678 static bool
28679 cp_parser_cache_group (cp_parser *parser,
28680 enum cpp_ttype end,
28681 unsigned depth)
28683 while (true)
28685 cp_token *token = cp_lexer_peek_token (parser->lexer);
28687 /* Abort a parenthesized expression if we encounter a semicolon. */
28688 if ((end == CPP_CLOSE_PAREN || depth == 0)
28689 && token->type == CPP_SEMICOLON)
28690 return true;
28691 /* If we've reached the end of the file, stop. */
28692 if (token->type == CPP_EOF
28693 || (end != CPP_PRAGMA_EOL
28694 && token->type == CPP_PRAGMA_EOL))
28695 return true;
28696 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28697 /* We've hit the end of an enclosing block, so there's been some
28698 kind of syntax error. */
28699 return true;
28701 /* Consume the token. */
28702 cp_lexer_consume_token (parser->lexer);
28703 /* See if it starts a new group. */
28704 if (token->type == CPP_OPEN_BRACE)
28706 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28707 /* In theory this should probably check end == '}', but
28708 cp_parser_save_member_function_body needs it to exit
28709 after either '}' or ')' when called with ')'. */
28710 if (depth == 0)
28711 return false;
28713 else if (token->type == CPP_OPEN_PAREN)
28715 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28716 if (depth == 0 && end == CPP_CLOSE_PAREN)
28717 return false;
28719 else if (token->type == CPP_PRAGMA)
28720 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28721 else if (token->type == end)
28722 return false;
28726 /* Like above, for caching a default argument or NSDMI. Both of these are
28727 terminated by a non-nested comma, but it can be unclear whether or not a
28728 comma is nested in a template argument list unless we do more parsing.
28729 In order to handle this ambiguity, when we encounter a ',' after a '<'
28730 we try to parse what follows as a parameter-declaration-list (in the
28731 case of a default argument) or a member-declarator (in the case of an
28732 NSDMI). If that succeeds, then we stop caching. */
28734 static tree
28735 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28737 unsigned depth = 0;
28738 int maybe_template_id = 0;
28739 cp_token *first_token;
28740 cp_token *token;
28741 tree default_argument;
28743 /* Add tokens until we have processed the entire default
28744 argument. We add the range [first_token, token). */
28745 first_token = cp_lexer_peek_token (parser->lexer);
28746 if (first_token->type == CPP_OPEN_BRACE)
28748 /* For list-initialization, this is straightforward. */
28749 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28750 token = cp_lexer_peek_token (parser->lexer);
28752 else while (true)
28754 bool done = false;
28756 /* Peek at the next token. */
28757 token = cp_lexer_peek_token (parser->lexer);
28758 /* What we do depends on what token we have. */
28759 switch (token->type)
28761 /* In valid code, a default argument must be
28762 immediately followed by a `,' `)', or `...'. */
28763 case CPP_COMMA:
28764 if (depth == 0 && maybe_template_id)
28766 /* If we've seen a '<', we might be in a
28767 template-argument-list. Until Core issue 325 is
28768 resolved, we don't know how this situation ought
28769 to be handled, so try to DTRT. We check whether
28770 what comes after the comma is a valid parameter
28771 declaration list. If it is, then the comma ends
28772 the default argument; otherwise the default
28773 argument continues. */
28774 bool error = false;
28775 cp_token *peek;
28777 /* Set ITALP so cp_parser_parameter_declaration_list
28778 doesn't decide to commit to this parse. */
28779 bool saved_italp = parser->in_template_argument_list_p;
28780 parser->in_template_argument_list_p = true;
28782 cp_parser_parse_tentatively (parser);
28784 if (nsdmi)
28786 /* Parse declarators until we reach a non-comma or
28787 somthing that cannot be an initializer.
28788 Just checking whether we're looking at a single
28789 declarator is insufficient. Consider:
28790 int var = tuple<T,U>::x;
28791 The template parameter 'U' looks exactly like a
28792 declarator. */
28795 int ctor_dtor_or_conv_p;
28796 cp_lexer_consume_token (parser->lexer);
28797 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28798 &ctor_dtor_or_conv_p,
28799 /*parenthesized_p=*/NULL,
28800 /*member_p=*/true,
28801 /*friend_p=*/false);
28802 peek = cp_lexer_peek_token (parser->lexer);
28803 if (cp_parser_error_occurred (parser))
28804 break;
28806 while (peek->type == CPP_COMMA);
28807 /* If we met an '=' or ';' then the original comma
28808 was the end of the NSDMI. Otherwise assume
28809 we're still in the NSDMI. */
28810 error = (peek->type != CPP_EQ
28811 && peek->type != CPP_SEMICOLON);
28813 else
28815 cp_lexer_consume_token (parser->lexer);
28816 begin_scope (sk_function_parms, NULL_TREE);
28817 cp_parser_parameter_declaration_list (parser, &error);
28818 pop_bindings_and_leave_scope ();
28820 if (!cp_parser_error_occurred (parser) && !error)
28821 done = true;
28822 cp_parser_abort_tentative_parse (parser);
28824 parser->in_template_argument_list_p = saved_italp;
28825 break;
28827 /* FALLTHRU */
28828 case CPP_CLOSE_PAREN:
28829 case CPP_ELLIPSIS:
28830 /* If we run into a non-nested `;', `}', or `]',
28831 then the code is invalid -- but the default
28832 argument is certainly over. */
28833 case CPP_SEMICOLON:
28834 case CPP_CLOSE_BRACE:
28835 case CPP_CLOSE_SQUARE:
28836 if (depth == 0
28837 /* Handle correctly int n = sizeof ... ( p ); */
28838 && token->type != CPP_ELLIPSIS)
28839 done = true;
28840 /* Update DEPTH, if necessary. */
28841 else if (token->type == CPP_CLOSE_PAREN
28842 || token->type == CPP_CLOSE_BRACE
28843 || token->type == CPP_CLOSE_SQUARE)
28844 --depth;
28845 break;
28847 case CPP_OPEN_PAREN:
28848 case CPP_OPEN_SQUARE:
28849 case CPP_OPEN_BRACE:
28850 ++depth;
28851 break;
28853 case CPP_LESS:
28854 if (depth == 0)
28855 /* This might be the comparison operator, or it might
28856 start a template argument list. */
28857 ++maybe_template_id;
28858 break;
28860 case CPP_RSHIFT:
28861 if (cxx_dialect == cxx98)
28862 break;
28863 /* Fall through for C++0x, which treats the `>>'
28864 operator like two `>' tokens in certain
28865 cases. */
28866 gcc_fallthrough ();
28868 case CPP_GREATER:
28869 if (depth == 0)
28871 /* This might be an operator, or it might close a
28872 template argument list. But if a previous '<'
28873 started a template argument list, this will have
28874 closed it, so we can't be in one anymore. */
28875 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28876 if (maybe_template_id < 0)
28877 maybe_template_id = 0;
28879 break;
28881 /* If we run out of tokens, issue an error message. */
28882 case CPP_EOF:
28883 case CPP_PRAGMA_EOL:
28884 error_at (token->location, "file ends in default argument");
28885 return error_mark_node;
28887 case CPP_NAME:
28888 case CPP_SCOPE:
28889 /* In these cases, we should look for template-ids.
28890 For example, if the default argument is
28891 `X<int, double>()', we need to do name lookup to
28892 figure out whether or not `X' is a template; if
28893 so, the `,' does not end the default argument.
28895 That is not yet done. */
28896 break;
28898 default:
28899 break;
28902 /* If we've reached the end, stop. */
28903 if (done)
28904 break;
28906 /* Add the token to the token block. */
28907 token = cp_lexer_consume_token (parser->lexer);
28910 /* Create a DEFAULT_ARG to represent the unparsed default
28911 argument. */
28912 default_argument = make_node (DEFAULT_ARG);
28913 DEFARG_TOKENS (default_argument)
28914 = cp_token_cache_new (first_token, token);
28915 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28917 return default_argument;
28920 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28922 location_t
28923 defarg_location (tree default_argument)
28925 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28926 location_t start = tokens->first->location;
28927 location_t end = tokens->last->location;
28928 return make_location (start, start, end);
28931 /* Begin parsing tentatively. We always save tokens while parsing
28932 tentatively so that if the tentative parsing fails we can restore the
28933 tokens. */
28935 static void
28936 cp_parser_parse_tentatively (cp_parser* parser)
28938 /* Enter a new parsing context. */
28939 parser->context = cp_parser_context_new (parser->context);
28940 /* Begin saving tokens. */
28941 cp_lexer_save_tokens (parser->lexer);
28942 /* In order to avoid repetitive access control error messages,
28943 access checks are queued up until we are no longer parsing
28944 tentatively. */
28945 push_deferring_access_checks (dk_deferred);
28948 /* Commit to the currently active tentative parse. */
28950 static void
28951 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28953 cp_parser_context *context;
28954 cp_lexer *lexer;
28956 /* Mark all of the levels as committed. */
28957 lexer = parser->lexer;
28958 for (context = parser->context; context->next; context = context->next)
28960 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28961 break;
28962 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28963 while (!cp_lexer_saving_tokens (lexer))
28964 lexer = lexer->next;
28965 cp_lexer_commit_tokens (lexer);
28969 /* Commit to the topmost currently active tentative parse.
28971 Note that this function shouldn't be called when there are
28972 irreversible side-effects while in a tentative state. For
28973 example, we shouldn't create a permanent entry in the symbol
28974 table, or issue an error message that might not apply if the
28975 tentative parse is aborted. */
28977 static void
28978 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28980 cp_parser_context *context = parser->context;
28981 cp_lexer *lexer = parser->lexer;
28983 if (context)
28985 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28986 return;
28987 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28989 while (!cp_lexer_saving_tokens (lexer))
28990 lexer = lexer->next;
28991 cp_lexer_commit_tokens (lexer);
28995 /* Abort the currently active tentative parse. All consumed tokens
28996 will be rolled back, and no diagnostics will be issued. */
28998 static void
28999 cp_parser_abort_tentative_parse (cp_parser* parser)
29001 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29002 || errorcount > 0);
29003 cp_parser_simulate_error (parser);
29004 /* Now, pretend that we want to see if the construct was
29005 successfully parsed. */
29006 cp_parser_parse_definitely (parser);
29009 /* Stop parsing tentatively. If a parse error has occurred, restore the
29010 token stream. Otherwise, commit to the tokens we have consumed.
29011 Returns true if no error occurred; false otherwise. */
29013 static bool
29014 cp_parser_parse_definitely (cp_parser* parser)
29016 bool error_occurred;
29017 cp_parser_context *context;
29019 /* Remember whether or not an error occurred, since we are about to
29020 destroy that information. */
29021 error_occurred = cp_parser_error_occurred (parser);
29022 /* Remove the topmost context from the stack. */
29023 context = parser->context;
29024 parser->context = context->next;
29025 /* If no parse errors occurred, commit to the tentative parse. */
29026 if (!error_occurred)
29028 /* Commit to the tokens read tentatively, unless that was
29029 already done. */
29030 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29031 cp_lexer_commit_tokens (parser->lexer);
29033 pop_to_parent_deferring_access_checks ();
29035 /* Otherwise, if errors occurred, roll back our state so that things
29036 are just as they were before we began the tentative parse. */
29037 else
29039 cp_lexer_rollback_tokens (parser->lexer);
29040 pop_deferring_access_checks ();
29042 /* Add the context to the front of the free list. */
29043 context->next = cp_parser_context_free_list;
29044 cp_parser_context_free_list = context;
29046 return !error_occurred;
29049 /* Returns true if we are parsing tentatively and are not committed to
29050 this tentative parse. */
29052 static bool
29053 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29055 return (cp_parser_parsing_tentatively (parser)
29056 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29059 /* Returns nonzero iff an error has occurred during the most recent
29060 tentative parse. */
29062 static bool
29063 cp_parser_error_occurred (cp_parser* parser)
29065 return (cp_parser_parsing_tentatively (parser)
29066 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29069 /* Returns nonzero if GNU extensions are allowed. */
29071 static bool
29072 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29074 return parser->allow_gnu_extensions_p;
29077 /* Objective-C++ Productions */
29080 /* Parse an Objective-C expression, which feeds into a primary-expression
29081 above.
29083 objc-expression:
29084 objc-message-expression
29085 objc-string-literal
29086 objc-encode-expression
29087 objc-protocol-expression
29088 objc-selector-expression
29090 Returns a tree representation of the expression. */
29092 static cp_expr
29093 cp_parser_objc_expression (cp_parser* parser)
29095 /* Try to figure out what kind of declaration is present. */
29096 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29098 switch (kwd->type)
29100 case CPP_OPEN_SQUARE:
29101 return cp_parser_objc_message_expression (parser);
29103 case CPP_OBJC_STRING:
29104 kwd = cp_lexer_consume_token (parser->lexer);
29105 return objc_build_string_object (kwd->u.value);
29107 case CPP_KEYWORD:
29108 switch (kwd->keyword)
29110 case RID_AT_ENCODE:
29111 return cp_parser_objc_encode_expression (parser);
29113 case RID_AT_PROTOCOL:
29114 return cp_parser_objc_protocol_expression (parser);
29116 case RID_AT_SELECTOR:
29117 return cp_parser_objc_selector_expression (parser);
29119 default:
29120 break;
29122 /* FALLTHRU */
29123 default:
29124 error_at (kwd->location,
29125 "misplaced %<@%D%> Objective-C++ construct",
29126 kwd->u.value);
29127 cp_parser_skip_to_end_of_block_or_statement (parser);
29130 return error_mark_node;
29133 /* Parse an Objective-C message expression.
29135 objc-message-expression:
29136 [ objc-message-receiver objc-message-args ]
29138 Returns a representation of an Objective-C message. */
29140 static tree
29141 cp_parser_objc_message_expression (cp_parser* parser)
29143 tree receiver, messageargs;
29145 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29146 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29147 receiver = cp_parser_objc_message_receiver (parser);
29148 messageargs = cp_parser_objc_message_args (parser);
29149 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29152 tree result = objc_build_message_expr (receiver, messageargs);
29154 /* Construct a location e.g.
29155 [self func1:5]
29156 ^~~~~~~~~~~~~~
29157 ranging from the '[' to the ']', with the caret at the start. */
29158 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29159 protected_set_expr_location (result, combined_loc);
29161 return result;
29164 /* Parse an objc-message-receiver.
29166 objc-message-receiver:
29167 expression
29168 simple-type-specifier
29170 Returns a representation of the type or expression. */
29172 static tree
29173 cp_parser_objc_message_receiver (cp_parser* parser)
29175 tree rcv;
29177 /* An Objective-C message receiver may be either (1) a type
29178 or (2) an expression. */
29179 cp_parser_parse_tentatively (parser);
29180 rcv = cp_parser_expression (parser);
29182 /* If that worked out, fine. */
29183 if (cp_parser_parse_definitely (parser))
29184 return rcv;
29186 cp_parser_parse_tentatively (parser);
29187 rcv = cp_parser_simple_type_specifier (parser,
29188 /*decl_specs=*/NULL,
29189 CP_PARSER_FLAGS_NONE);
29191 if (cp_parser_parse_definitely (parser))
29192 return objc_get_class_reference (rcv);
29194 cp_parser_error (parser, "objective-c++ message receiver expected");
29195 return error_mark_node;
29198 /* Parse the arguments and selectors comprising an Objective-C message.
29200 objc-message-args:
29201 objc-selector
29202 objc-selector-args
29203 objc-selector-args , objc-comma-args
29205 objc-selector-args:
29206 objc-selector [opt] : assignment-expression
29207 objc-selector-args objc-selector [opt] : assignment-expression
29209 objc-comma-args:
29210 assignment-expression
29211 objc-comma-args , assignment-expression
29213 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29214 selector arguments and TREE_VALUE containing a list of comma
29215 arguments. */
29217 static tree
29218 cp_parser_objc_message_args (cp_parser* parser)
29220 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29221 bool maybe_unary_selector_p = true;
29222 cp_token *token = cp_lexer_peek_token (parser->lexer);
29224 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29226 tree selector = NULL_TREE, arg;
29228 if (token->type != CPP_COLON)
29229 selector = cp_parser_objc_selector (parser);
29231 /* Detect if we have a unary selector. */
29232 if (maybe_unary_selector_p
29233 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29234 return build_tree_list (selector, NULL_TREE);
29236 maybe_unary_selector_p = false;
29237 cp_parser_require (parser, CPP_COLON, RT_COLON);
29238 arg = cp_parser_assignment_expression (parser);
29240 sel_args
29241 = chainon (sel_args,
29242 build_tree_list (selector, arg));
29244 token = cp_lexer_peek_token (parser->lexer);
29247 /* Handle non-selector arguments, if any. */
29248 while (token->type == CPP_COMMA)
29250 tree arg;
29252 cp_lexer_consume_token (parser->lexer);
29253 arg = cp_parser_assignment_expression (parser);
29255 addl_args
29256 = chainon (addl_args,
29257 build_tree_list (NULL_TREE, arg));
29259 token = cp_lexer_peek_token (parser->lexer);
29262 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29264 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29265 return build_tree_list (error_mark_node, error_mark_node);
29268 return build_tree_list (sel_args, addl_args);
29271 /* Parse an Objective-C encode expression.
29273 objc-encode-expression:
29274 @encode objc-typename
29276 Returns an encoded representation of the type argument. */
29278 static cp_expr
29279 cp_parser_objc_encode_expression (cp_parser* parser)
29281 tree type;
29282 cp_token *token;
29283 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29285 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29286 matching_parens parens;
29287 parens.require_open (parser);
29288 token = cp_lexer_peek_token (parser->lexer);
29289 type = complete_type (cp_parser_type_id (parser));
29290 parens.require_close (parser);
29292 if (!type)
29294 error_at (token->location,
29295 "%<@encode%> must specify a type as an argument");
29296 return error_mark_node;
29299 /* This happens if we find @encode(T) (where T is a template
29300 typename or something dependent on a template typename) when
29301 parsing a template. In that case, we can't compile it
29302 immediately, but we rather create an AT_ENCODE_EXPR which will
29303 need to be instantiated when the template is used.
29305 if (dependent_type_p (type))
29307 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29308 TREE_READONLY (value) = 1;
29309 return value;
29313 /* Build a location of the form:
29314 @encode(int)
29315 ^~~~~~~~~~~~
29316 with caret==start at the @ token, finishing at the close paren. */
29317 location_t combined_loc
29318 = make_location (start_loc, start_loc,
29319 cp_lexer_previous_token (parser->lexer)->location);
29321 return cp_expr (objc_build_encode_expr (type), combined_loc);
29324 /* Parse an Objective-C @defs expression. */
29326 static tree
29327 cp_parser_objc_defs_expression (cp_parser *parser)
29329 tree name;
29331 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29332 matching_parens parens;
29333 parens.require_open (parser);
29334 name = cp_parser_identifier (parser);
29335 parens.require_close (parser);
29337 return objc_get_class_ivars (name);
29340 /* Parse an Objective-C protocol expression.
29342 objc-protocol-expression:
29343 @protocol ( identifier )
29345 Returns a representation of the protocol expression. */
29347 static tree
29348 cp_parser_objc_protocol_expression (cp_parser* parser)
29350 tree proto;
29351 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29353 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29354 matching_parens parens;
29355 parens.require_open (parser);
29356 proto = cp_parser_identifier (parser);
29357 parens.require_close (parser);
29359 /* Build a location of the form:
29360 @protocol(prot)
29361 ^~~~~~~~~~~~~~~
29362 with caret==start at the @ token, finishing at the close paren. */
29363 location_t combined_loc
29364 = make_location (start_loc, start_loc,
29365 cp_lexer_previous_token (parser->lexer)->location);
29366 tree result = objc_build_protocol_expr (proto);
29367 protected_set_expr_location (result, combined_loc);
29368 return result;
29371 /* Parse an Objective-C selector expression.
29373 objc-selector-expression:
29374 @selector ( objc-method-signature )
29376 objc-method-signature:
29377 objc-selector
29378 objc-selector-seq
29380 objc-selector-seq:
29381 objc-selector :
29382 objc-selector-seq objc-selector :
29384 Returns a representation of the method selector. */
29386 static tree
29387 cp_parser_objc_selector_expression (cp_parser* parser)
29389 tree sel_seq = NULL_TREE;
29390 bool maybe_unary_selector_p = true;
29391 cp_token *token;
29392 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29394 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29395 matching_parens parens;
29396 parens.require_open (parser);
29397 token = cp_lexer_peek_token (parser->lexer);
29399 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29400 || token->type == CPP_SCOPE)
29402 tree selector = NULL_TREE;
29404 if (token->type != CPP_COLON
29405 || token->type == CPP_SCOPE)
29406 selector = cp_parser_objc_selector (parser);
29408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29409 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29411 /* Detect if we have a unary selector. */
29412 if (maybe_unary_selector_p)
29414 sel_seq = selector;
29415 goto finish_selector;
29417 else
29419 cp_parser_error (parser, "expected %<:%>");
29422 maybe_unary_selector_p = false;
29423 token = cp_lexer_consume_token (parser->lexer);
29425 if (token->type == CPP_SCOPE)
29427 sel_seq
29428 = chainon (sel_seq,
29429 build_tree_list (selector, NULL_TREE));
29430 sel_seq
29431 = chainon (sel_seq,
29432 build_tree_list (NULL_TREE, NULL_TREE));
29434 else
29435 sel_seq
29436 = chainon (sel_seq,
29437 build_tree_list (selector, NULL_TREE));
29439 token = cp_lexer_peek_token (parser->lexer);
29442 finish_selector:
29443 parens.require_close (parser);
29446 /* Build a location of the form:
29447 @selector(func)
29448 ^~~~~~~~~~~~~~~
29449 with caret==start at the @ token, finishing at the close paren. */
29450 location_t combined_loc
29451 = make_location (loc, loc,
29452 cp_lexer_previous_token (parser->lexer)->location);
29453 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29454 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29455 protected_set_expr_location (result, combined_loc);
29456 return result;
29459 /* Parse a list of identifiers.
29461 objc-identifier-list:
29462 identifier
29463 objc-identifier-list , identifier
29465 Returns a TREE_LIST of identifier nodes. */
29467 static tree
29468 cp_parser_objc_identifier_list (cp_parser* parser)
29470 tree identifier;
29471 tree list;
29472 cp_token *sep;
29474 identifier = cp_parser_identifier (parser);
29475 if (identifier == error_mark_node)
29476 return error_mark_node;
29478 list = build_tree_list (NULL_TREE, identifier);
29479 sep = cp_lexer_peek_token (parser->lexer);
29481 while (sep->type == CPP_COMMA)
29483 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29484 identifier = cp_parser_identifier (parser);
29485 if (identifier == error_mark_node)
29486 return list;
29488 list = chainon (list, build_tree_list (NULL_TREE,
29489 identifier));
29490 sep = cp_lexer_peek_token (parser->lexer);
29493 return list;
29496 /* Parse an Objective-C alias declaration.
29498 objc-alias-declaration:
29499 @compatibility_alias identifier identifier ;
29501 This function registers the alias mapping with the Objective-C front end.
29502 It returns nothing. */
29504 static void
29505 cp_parser_objc_alias_declaration (cp_parser* parser)
29507 tree alias, orig;
29509 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29510 alias = cp_parser_identifier (parser);
29511 orig = cp_parser_identifier (parser);
29512 objc_declare_alias (alias, orig);
29513 cp_parser_consume_semicolon_at_end_of_statement (parser);
29516 /* Parse an Objective-C class forward-declaration.
29518 objc-class-declaration:
29519 @class objc-identifier-list ;
29521 The function registers the forward declarations with the Objective-C
29522 front end. It returns nothing. */
29524 static void
29525 cp_parser_objc_class_declaration (cp_parser* parser)
29527 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29528 while (true)
29530 tree id;
29532 id = cp_parser_identifier (parser);
29533 if (id == error_mark_node)
29534 break;
29536 objc_declare_class (id);
29538 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29539 cp_lexer_consume_token (parser->lexer);
29540 else
29541 break;
29543 cp_parser_consume_semicolon_at_end_of_statement (parser);
29546 /* Parse a list of Objective-C protocol references.
29548 objc-protocol-refs-opt:
29549 objc-protocol-refs [opt]
29551 objc-protocol-refs:
29552 < objc-identifier-list >
29554 Returns a TREE_LIST of identifiers, if any. */
29556 static tree
29557 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29559 tree protorefs = NULL_TREE;
29561 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29563 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29564 protorefs = cp_parser_objc_identifier_list (parser);
29565 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29568 return protorefs;
29571 /* Parse a Objective-C visibility specification. */
29573 static void
29574 cp_parser_objc_visibility_spec (cp_parser* parser)
29576 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29578 switch (vis->keyword)
29580 case RID_AT_PRIVATE:
29581 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29582 break;
29583 case RID_AT_PROTECTED:
29584 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29585 break;
29586 case RID_AT_PUBLIC:
29587 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29588 break;
29589 case RID_AT_PACKAGE:
29590 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29591 break;
29592 default:
29593 return;
29596 /* Eat '@private'/'@protected'/'@public'. */
29597 cp_lexer_consume_token (parser->lexer);
29600 /* Parse an Objective-C method type. Return 'true' if it is a class
29601 (+) method, and 'false' if it is an instance (-) method. */
29603 static inline bool
29604 cp_parser_objc_method_type (cp_parser* parser)
29606 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29607 return true;
29608 else
29609 return false;
29612 /* Parse an Objective-C protocol qualifier. */
29614 static tree
29615 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29617 tree quals = NULL_TREE, node;
29618 cp_token *token = cp_lexer_peek_token (parser->lexer);
29620 node = token->u.value;
29622 while (node && identifier_p (node)
29623 && (node == ridpointers [(int) RID_IN]
29624 || node == ridpointers [(int) RID_OUT]
29625 || node == ridpointers [(int) RID_INOUT]
29626 || node == ridpointers [(int) RID_BYCOPY]
29627 || node == ridpointers [(int) RID_BYREF]
29628 || node == ridpointers [(int) RID_ONEWAY]))
29630 quals = tree_cons (NULL_TREE, node, quals);
29631 cp_lexer_consume_token (parser->lexer);
29632 token = cp_lexer_peek_token (parser->lexer);
29633 node = token->u.value;
29636 return quals;
29639 /* Parse an Objective-C typename. */
29641 static tree
29642 cp_parser_objc_typename (cp_parser* parser)
29644 tree type_name = NULL_TREE;
29646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29648 tree proto_quals, cp_type = NULL_TREE;
29650 matching_parens parens;
29651 parens.consume_open (parser); /* Eat '('. */
29652 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29654 /* An ObjC type name may consist of just protocol qualifiers, in which
29655 case the type shall default to 'id'. */
29656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29658 cp_type = cp_parser_type_id (parser);
29660 /* If the type could not be parsed, an error has already
29661 been produced. For error recovery, behave as if it had
29662 not been specified, which will use the default type
29663 'id'. */
29664 if (cp_type == error_mark_node)
29666 cp_type = NULL_TREE;
29667 /* We need to skip to the closing parenthesis as
29668 cp_parser_type_id() does not seem to do it for
29669 us. */
29670 cp_parser_skip_to_closing_parenthesis (parser,
29671 /*recovering=*/true,
29672 /*or_comma=*/false,
29673 /*consume_paren=*/false);
29677 parens.require_close (parser);
29678 type_name = build_tree_list (proto_quals, cp_type);
29681 return type_name;
29684 /* Check to see if TYPE refers to an Objective-C selector name. */
29686 static bool
29687 cp_parser_objc_selector_p (enum cpp_ttype type)
29689 return (type == CPP_NAME || type == CPP_KEYWORD
29690 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29691 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29692 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29693 || type == CPP_XOR || type == CPP_XOR_EQ);
29696 /* Parse an Objective-C selector. */
29698 static tree
29699 cp_parser_objc_selector (cp_parser* parser)
29701 cp_token *token = cp_lexer_consume_token (parser->lexer);
29703 if (!cp_parser_objc_selector_p (token->type))
29705 error_at (token->location, "invalid Objective-C++ selector name");
29706 return error_mark_node;
29709 /* C++ operator names are allowed to appear in ObjC selectors. */
29710 switch (token->type)
29712 case CPP_AND_AND: return get_identifier ("and");
29713 case CPP_AND_EQ: return get_identifier ("and_eq");
29714 case CPP_AND: return get_identifier ("bitand");
29715 case CPP_OR: return get_identifier ("bitor");
29716 case CPP_COMPL: return get_identifier ("compl");
29717 case CPP_NOT: return get_identifier ("not");
29718 case CPP_NOT_EQ: return get_identifier ("not_eq");
29719 case CPP_OR_OR: return get_identifier ("or");
29720 case CPP_OR_EQ: return get_identifier ("or_eq");
29721 case CPP_XOR: return get_identifier ("xor");
29722 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29723 default: return token->u.value;
29727 /* Parse an Objective-C params list. */
29729 static tree
29730 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29732 tree params = NULL_TREE;
29733 bool maybe_unary_selector_p = true;
29734 cp_token *token = cp_lexer_peek_token (parser->lexer);
29736 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29738 tree selector = NULL_TREE, type_name, identifier;
29739 tree parm_attr = NULL_TREE;
29741 if (token->keyword == RID_ATTRIBUTE)
29742 break;
29744 if (token->type != CPP_COLON)
29745 selector = cp_parser_objc_selector (parser);
29747 /* Detect if we have a unary selector. */
29748 if (maybe_unary_selector_p
29749 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29751 params = selector; /* Might be followed by attributes. */
29752 break;
29755 maybe_unary_selector_p = false;
29756 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29758 /* Something went quite wrong. There should be a colon
29759 here, but there is not. Stop parsing parameters. */
29760 break;
29762 type_name = cp_parser_objc_typename (parser);
29763 /* New ObjC allows attributes on parameters too. */
29764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29765 parm_attr = cp_parser_attributes_opt (parser);
29766 identifier = cp_parser_identifier (parser);
29768 params
29769 = chainon (params,
29770 objc_build_keyword_decl (selector,
29771 type_name,
29772 identifier,
29773 parm_attr));
29775 token = cp_lexer_peek_token (parser->lexer);
29778 if (params == NULL_TREE)
29780 cp_parser_error (parser, "objective-c++ method declaration is expected");
29781 return error_mark_node;
29784 /* We allow tail attributes for the method. */
29785 if (token->keyword == RID_ATTRIBUTE)
29787 *attributes = cp_parser_attributes_opt (parser);
29788 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29789 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29790 return params;
29791 cp_parser_error (parser,
29792 "method attributes must be specified at the end");
29793 return error_mark_node;
29796 if (params == NULL_TREE)
29798 cp_parser_error (parser, "objective-c++ method declaration is expected");
29799 return error_mark_node;
29801 return params;
29804 /* Parse the non-keyword Objective-C params. */
29806 static tree
29807 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29808 tree* attributes)
29810 tree params = make_node (TREE_LIST);
29811 cp_token *token = cp_lexer_peek_token (parser->lexer);
29812 *ellipsisp = false; /* Initially, assume no ellipsis. */
29814 while (token->type == CPP_COMMA)
29816 cp_parameter_declarator *parmdecl;
29817 tree parm;
29819 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29820 token = cp_lexer_peek_token (parser->lexer);
29822 if (token->type == CPP_ELLIPSIS)
29824 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29825 *ellipsisp = true;
29826 token = cp_lexer_peek_token (parser->lexer);
29827 break;
29830 /* TODO: parse attributes for tail parameters. */
29831 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29832 parm = grokdeclarator (parmdecl->declarator,
29833 &parmdecl->decl_specifiers,
29834 PARM, /*initialized=*/0,
29835 /*attrlist=*/NULL);
29837 chainon (params, build_tree_list (NULL_TREE, parm));
29838 token = cp_lexer_peek_token (parser->lexer);
29841 /* We allow tail attributes for the method. */
29842 if (token->keyword == RID_ATTRIBUTE)
29844 if (*attributes == NULL_TREE)
29846 *attributes = cp_parser_attributes_opt (parser);
29847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29848 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29849 return params;
29851 else
29852 /* We have an error, but parse the attributes, so that we can
29853 carry on. */
29854 *attributes = cp_parser_attributes_opt (parser);
29856 cp_parser_error (parser,
29857 "method attributes must be specified at the end");
29858 return error_mark_node;
29861 return params;
29864 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29866 static void
29867 cp_parser_objc_interstitial_code (cp_parser* parser)
29869 cp_token *token = cp_lexer_peek_token (parser->lexer);
29871 /* If the next token is `extern' and the following token is a string
29872 literal, then we have a linkage specification. */
29873 if (token->keyword == RID_EXTERN
29874 && cp_parser_is_pure_string_literal
29875 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29876 cp_parser_linkage_specification (parser);
29877 /* Handle #pragma, if any. */
29878 else if (token->type == CPP_PRAGMA)
29879 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29880 /* Allow stray semicolons. */
29881 else if (token->type == CPP_SEMICOLON)
29882 cp_lexer_consume_token (parser->lexer);
29883 /* Mark methods as optional or required, when building protocols. */
29884 else if (token->keyword == RID_AT_OPTIONAL)
29886 cp_lexer_consume_token (parser->lexer);
29887 objc_set_method_opt (true);
29889 else if (token->keyword == RID_AT_REQUIRED)
29891 cp_lexer_consume_token (parser->lexer);
29892 objc_set_method_opt (false);
29894 else if (token->keyword == RID_NAMESPACE)
29895 cp_parser_namespace_definition (parser);
29896 /* Other stray characters must generate errors. */
29897 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29899 cp_lexer_consume_token (parser->lexer);
29900 error ("stray %qs between Objective-C++ methods",
29901 token->type == CPP_OPEN_BRACE ? "{" : "}");
29903 /* Finally, try to parse a block-declaration, or a function-definition. */
29904 else
29905 cp_parser_block_declaration (parser, /*statement_p=*/false);
29908 /* Parse a method signature. */
29910 static tree
29911 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29913 tree rettype, kwdparms, optparms;
29914 bool ellipsis = false;
29915 bool is_class_method;
29917 is_class_method = cp_parser_objc_method_type (parser);
29918 rettype = cp_parser_objc_typename (parser);
29919 *attributes = NULL_TREE;
29920 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29921 if (kwdparms == error_mark_node)
29922 return error_mark_node;
29923 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29924 if (optparms == error_mark_node)
29925 return error_mark_node;
29927 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29930 static bool
29931 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29933 tree tattr;
29934 cp_lexer_save_tokens (parser->lexer);
29935 tattr = cp_parser_attributes_opt (parser);
29936 gcc_assert (tattr) ;
29938 /* If the attributes are followed by a method introducer, this is not allowed.
29939 Dump the attributes and flag the situation. */
29940 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29941 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29942 return true;
29944 /* Otherwise, the attributes introduce some interstitial code, possibly so
29945 rewind to allow that check. */
29946 cp_lexer_rollback_tokens (parser->lexer);
29947 return false;
29950 /* Parse an Objective-C method prototype list. */
29952 static void
29953 cp_parser_objc_method_prototype_list (cp_parser* parser)
29955 cp_token *token = cp_lexer_peek_token (parser->lexer);
29957 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29959 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29961 tree attributes, sig;
29962 bool is_class_method;
29963 if (token->type == CPP_PLUS)
29964 is_class_method = true;
29965 else
29966 is_class_method = false;
29967 sig = cp_parser_objc_method_signature (parser, &attributes);
29968 if (sig == error_mark_node)
29970 cp_parser_skip_to_end_of_block_or_statement (parser);
29971 token = cp_lexer_peek_token (parser->lexer);
29972 continue;
29974 objc_add_method_declaration (is_class_method, sig, attributes);
29975 cp_parser_consume_semicolon_at_end_of_statement (parser);
29977 else if (token->keyword == RID_AT_PROPERTY)
29978 cp_parser_objc_at_property_declaration (parser);
29979 else if (token->keyword == RID_ATTRIBUTE
29980 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29981 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29982 OPT_Wattributes,
29983 "prefix attributes are ignored for methods");
29984 else
29985 /* Allow for interspersed non-ObjC++ code. */
29986 cp_parser_objc_interstitial_code (parser);
29988 token = cp_lexer_peek_token (parser->lexer);
29991 if (token->type != CPP_EOF)
29992 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29993 else
29994 cp_parser_error (parser, "expected %<@end%>");
29996 objc_finish_interface ();
29999 /* Parse an Objective-C method definition list. */
30001 static void
30002 cp_parser_objc_method_definition_list (cp_parser* parser)
30004 cp_token *token = cp_lexer_peek_token (parser->lexer);
30006 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30008 tree meth;
30010 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30012 cp_token *ptk;
30013 tree sig, attribute;
30014 bool is_class_method;
30015 if (token->type == CPP_PLUS)
30016 is_class_method = true;
30017 else
30018 is_class_method = false;
30019 push_deferring_access_checks (dk_deferred);
30020 sig = cp_parser_objc_method_signature (parser, &attribute);
30021 if (sig == error_mark_node)
30023 cp_parser_skip_to_end_of_block_or_statement (parser);
30024 token = cp_lexer_peek_token (parser->lexer);
30025 continue;
30027 objc_start_method_definition (is_class_method, sig, attribute,
30028 NULL_TREE);
30030 /* For historical reasons, we accept an optional semicolon. */
30031 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30032 cp_lexer_consume_token (parser->lexer);
30034 ptk = cp_lexer_peek_token (parser->lexer);
30035 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30036 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30038 perform_deferred_access_checks (tf_warning_or_error);
30039 stop_deferring_access_checks ();
30040 meth = cp_parser_function_definition_after_declarator (parser,
30041 false);
30042 pop_deferring_access_checks ();
30043 objc_finish_method_definition (meth);
30046 /* The following case will be removed once @synthesize is
30047 completely implemented. */
30048 else if (token->keyword == RID_AT_PROPERTY)
30049 cp_parser_objc_at_property_declaration (parser);
30050 else if (token->keyword == RID_AT_SYNTHESIZE)
30051 cp_parser_objc_at_synthesize_declaration (parser);
30052 else if (token->keyword == RID_AT_DYNAMIC)
30053 cp_parser_objc_at_dynamic_declaration (parser);
30054 else if (token->keyword == RID_ATTRIBUTE
30055 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30056 warning_at (token->location, OPT_Wattributes,
30057 "prefix attributes are ignored for methods");
30058 else
30059 /* Allow for interspersed non-ObjC++ code. */
30060 cp_parser_objc_interstitial_code (parser);
30062 token = cp_lexer_peek_token (parser->lexer);
30065 if (token->type != CPP_EOF)
30066 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30067 else
30068 cp_parser_error (parser, "expected %<@end%>");
30070 objc_finish_implementation ();
30073 /* Parse Objective-C ivars. */
30075 static void
30076 cp_parser_objc_class_ivars (cp_parser* parser)
30078 cp_token *token = cp_lexer_peek_token (parser->lexer);
30080 if (token->type != CPP_OPEN_BRACE)
30081 return; /* No ivars specified. */
30083 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30084 token = cp_lexer_peek_token (parser->lexer);
30086 while (token->type != CPP_CLOSE_BRACE
30087 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30089 cp_decl_specifier_seq declspecs;
30090 int decl_class_or_enum_p;
30091 tree prefix_attributes;
30093 cp_parser_objc_visibility_spec (parser);
30095 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30096 break;
30098 cp_parser_decl_specifier_seq (parser,
30099 CP_PARSER_FLAGS_OPTIONAL,
30100 &declspecs,
30101 &decl_class_or_enum_p);
30103 /* auto, register, static, extern, mutable. */
30104 if (declspecs.storage_class != sc_none)
30106 cp_parser_error (parser, "invalid type for instance variable");
30107 declspecs.storage_class = sc_none;
30110 /* thread_local. */
30111 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30113 cp_parser_error (parser, "invalid type for instance variable");
30114 declspecs.locations[ds_thread] = 0;
30117 /* typedef. */
30118 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30120 cp_parser_error (parser, "invalid type for instance variable");
30121 declspecs.locations[ds_typedef] = 0;
30124 prefix_attributes = declspecs.attributes;
30125 declspecs.attributes = NULL_TREE;
30127 /* Keep going until we hit the `;' at the end of the
30128 declaration. */
30129 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30131 tree width = NULL_TREE, attributes, first_attribute, decl;
30132 cp_declarator *declarator = NULL;
30133 int ctor_dtor_or_conv_p;
30135 /* Check for a (possibly unnamed) bitfield declaration. */
30136 token = cp_lexer_peek_token (parser->lexer);
30137 if (token->type == CPP_COLON)
30138 goto eat_colon;
30140 if (token->type == CPP_NAME
30141 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30142 == CPP_COLON))
30144 /* Get the name of the bitfield. */
30145 declarator = make_id_declarator (NULL_TREE,
30146 cp_parser_identifier (parser),
30147 sfk_none);
30149 eat_colon:
30150 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30151 /* Get the width of the bitfield. */
30152 width
30153 = cp_parser_constant_expression (parser);
30155 else
30157 /* Parse the declarator. */
30158 declarator
30159 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30160 &ctor_dtor_or_conv_p,
30161 /*parenthesized_p=*/NULL,
30162 /*member_p=*/false,
30163 /*friend_p=*/false);
30166 /* Look for attributes that apply to the ivar. */
30167 attributes = cp_parser_attributes_opt (parser);
30168 /* Remember which attributes are prefix attributes and
30169 which are not. */
30170 first_attribute = attributes;
30171 /* Combine the attributes. */
30172 attributes = attr_chainon (prefix_attributes, attributes);
30174 if (width)
30175 /* Create the bitfield declaration. */
30176 decl = grokbitfield (declarator, &declspecs,
30177 width, NULL_TREE, attributes);
30178 else
30179 decl = grokfield (declarator, &declspecs,
30180 NULL_TREE, /*init_const_expr_p=*/false,
30181 NULL_TREE, attributes);
30183 /* Add the instance variable. */
30184 if (decl != error_mark_node && decl != NULL_TREE)
30185 objc_add_instance_variable (decl);
30187 /* Reset PREFIX_ATTRIBUTES. */
30188 if (attributes != error_mark_node)
30190 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30191 attributes = TREE_CHAIN (attributes);
30192 if (attributes)
30193 TREE_CHAIN (attributes) = NULL_TREE;
30196 token = cp_lexer_peek_token (parser->lexer);
30198 if (token->type == CPP_COMMA)
30200 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30201 continue;
30203 break;
30206 cp_parser_consume_semicolon_at_end_of_statement (parser);
30207 token = cp_lexer_peek_token (parser->lexer);
30210 if (token->keyword == RID_AT_END)
30211 cp_parser_error (parser, "expected %<}%>");
30213 /* Do not consume the RID_AT_END, so it will be read again as terminating
30214 the @interface of @implementation. */
30215 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30216 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30218 /* For historical reasons, we accept an optional semicolon. */
30219 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30220 cp_lexer_consume_token (parser->lexer);
30223 /* Parse an Objective-C protocol declaration. */
30225 static void
30226 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30228 tree proto, protorefs;
30229 cp_token *tok;
30231 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30234 tok = cp_lexer_peek_token (parser->lexer);
30235 error_at (tok->location, "identifier expected after %<@protocol%>");
30236 cp_parser_consume_semicolon_at_end_of_statement (parser);
30237 return;
30240 /* See if we have a forward declaration or a definition. */
30241 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30243 /* Try a forward declaration first. */
30244 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30246 while (true)
30248 tree id;
30250 id = cp_parser_identifier (parser);
30251 if (id == error_mark_node)
30252 break;
30254 objc_declare_protocol (id, attributes);
30256 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30257 cp_lexer_consume_token (parser->lexer);
30258 else
30259 break;
30261 cp_parser_consume_semicolon_at_end_of_statement (parser);
30264 /* Ok, we got a full-fledged definition (or at least should). */
30265 else
30267 proto = cp_parser_identifier (parser);
30268 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30269 objc_start_protocol (proto, protorefs, attributes);
30270 cp_parser_objc_method_prototype_list (parser);
30274 /* Parse an Objective-C superclass or category. */
30276 static void
30277 cp_parser_objc_superclass_or_category (cp_parser *parser,
30278 bool iface_p,
30279 tree *super,
30280 tree *categ, bool *is_class_extension)
30282 cp_token *next = cp_lexer_peek_token (parser->lexer);
30284 *super = *categ = NULL_TREE;
30285 *is_class_extension = false;
30286 if (next->type == CPP_COLON)
30288 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30289 *super = cp_parser_identifier (parser);
30291 else if (next->type == CPP_OPEN_PAREN)
30293 matching_parens parens;
30294 parens.consume_open (parser); /* Eat '('. */
30296 /* If there is no category name, and this is an @interface, we
30297 have a class extension. */
30298 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30300 *categ = NULL_TREE;
30301 *is_class_extension = true;
30303 else
30304 *categ = cp_parser_identifier (parser);
30306 parens.require_close (parser);
30310 /* Parse an Objective-C class interface. */
30312 static void
30313 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30315 tree name, super, categ, protos;
30316 bool is_class_extension;
30318 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30319 name = cp_parser_identifier (parser);
30320 if (name == error_mark_node)
30322 /* It's hard to recover because even if valid @interface stuff
30323 is to follow, we can't compile it (or validate it) if we
30324 don't even know which class it refers to. Let's assume this
30325 was a stray '@interface' token in the stream and skip it.
30327 return;
30329 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30330 &is_class_extension);
30331 protos = cp_parser_objc_protocol_refs_opt (parser);
30333 /* We have either a class or a category on our hands. */
30334 if (categ || is_class_extension)
30335 objc_start_category_interface (name, categ, protos, attributes);
30336 else
30338 objc_start_class_interface (name, super, protos, attributes);
30339 /* Handle instance variable declarations, if any. */
30340 cp_parser_objc_class_ivars (parser);
30341 objc_continue_interface ();
30344 cp_parser_objc_method_prototype_list (parser);
30347 /* Parse an Objective-C class implementation. */
30349 static void
30350 cp_parser_objc_class_implementation (cp_parser* parser)
30352 tree name, super, categ;
30353 bool is_class_extension;
30355 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30356 name = cp_parser_identifier (parser);
30357 if (name == error_mark_node)
30359 /* It's hard to recover because even if valid @implementation
30360 stuff is to follow, we can't compile it (or validate it) if
30361 we don't even know which class it refers to. Let's assume
30362 this was a stray '@implementation' token in the stream and
30363 skip it.
30365 return;
30367 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30368 &is_class_extension);
30370 /* We have either a class or a category on our hands. */
30371 if (categ)
30372 objc_start_category_implementation (name, categ);
30373 else
30375 objc_start_class_implementation (name, super);
30376 /* Handle instance variable declarations, if any. */
30377 cp_parser_objc_class_ivars (parser);
30378 objc_continue_implementation ();
30381 cp_parser_objc_method_definition_list (parser);
30384 /* Consume the @end token and finish off the implementation. */
30386 static void
30387 cp_parser_objc_end_implementation (cp_parser* parser)
30389 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30390 objc_finish_implementation ();
30393 /* Parse an Objective-C declaration. */
30395 static void
30396 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30398 /* Try to figure out what kind of declaration is present. */
30399 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30401 if (attributes)
30402 switch (kwd->keyword)
30404 case RID_AT_ALIAS:
30405 case RID_AT_CLASS:
30406 case RID_AT_END:
30407 error_at (kwd->location, "attributes may not be specified before"
30408 " the %<@%D%> Objective-C++ keyword",
30409 kwd->u.value);
30410 attributes = NULL;
30411 break;
30412 case RID_AT_IMPLEMENTATION:
30413 warning_at (kwd->location, OPT_Wattributes,
30414 "prefix attributes are ignored before %<@%D%>",
30415 kwd->u.value);
30416 attributes = NULL;
30417 default:
30418 break;
30421 switch (kwd->keyword)
30423 case RID_AT_ALIAS:
30424 cp_parser_objc_alias_declaration (parser);
30425 break;
30426 case RID_AT_CLASS:
30427 cp_parser_objc_class_declaration (parser);
30428 break;
30429 case RID_AT_PROTOCOL:
30430 cp_parser_objc_protocol_declaration (parser, attributes);
30431 break;
30432 case RID_AT_INTERFACE:
30433 cp_parser_objc_class_interface (parser, attributes);
30434 break;
30435 case RID_AT_IMPLEMENTATION:
30436 cp_parser_objc_class_implementation (parser);
30437 break;
30438 case RID_AT_END:
30439 cp_parser_objc_end_implementation (parser);
30440 break;
30441 default:
30442 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30443 kwd->u.value);
30444 cp_parser_skip_to_end_of_block_or_statement (parser);
30448 /* Parse an Objective-C try-catch-finally statement.
30450 objc-try-catch-finally-stmt:
30451 @try compound-statement objc-catch-clause-seq [opt]
30452 objc-finally-clause [opt]
30454 objc-catch-clause-seq:
30455 objc-catch-clause objc-catch-clause-seq [opt]
30457 objc-catch-clause:
30458 @catch ( objc-exception-declaration ) compound-statement
30460 objc-finally-clause:
30461 @finally compound-statement
30463 objc-exception-declaration:
30464 parameter-declaration
30465 '...'
30467 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30469 Returns NULL_TREE.
30471 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30472 for C. Keep them in sync. */
30474 static tree
30475 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30477 location_t location;
30478 tree stmt;
30480 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30481 location = cp_lexer_peek_token (parser->lexer)->location;
30482 objc_maybe_warn_exceptions (location);
30483 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30484 node, lest it get absorbed into the surrounding block. */
30485 stmt = push_stmt_list ();
30486 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30487 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30489 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30491 cp_parameter_declarator *parm;
30492 tree parameter_declaration = error_mark_node;
30493 bool seen_open_paren = false;
30494 matching_parens parens;
30496 cp_lexer_consume_token (parser->lexer);
30497 if (parens.require_open (parser))
30498 seen_open_paren = true;
30499 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30501 /* We have "@catch (...)" (where the '...' are literally
30502 what is in the code). Skip the '...'.
30503 parameter_declaration is set to NULL_TREE, and
30504 objc_being_catch_clauses() knows that that means
30505 '...'. */
30506 cp_lexer_consume_token (parser->lexer);
30507 parameter_declaration = NULL_TREE;
30509 else
30511 /* We have "@catch (NSException *exception)" or something
30512 like that. Parse the parameter declaration. */
30513 parm = cp_parser_parameter_declaration (parser, false, NULL);
30514 if (parm == NULL)
30515 parameter_declaration = error_mark_node;
30516 else
30517 parameter_declaration = grokdeclarator (parm->declarator,
30518 &parm->decl_specifiers,
30519 PARM, /*initialized=*/0,
30520 /*attrlist=*/NULL);
30522 if (seen_open_paren)
30523 parens.require_close (parser);
30524 else
30526 /* If there was no open parenthesis, we are recovering from
30527 an error, and we are trying to figure out what mistake
30528 the user has made. */
30530 /* If there is an immediate closing parenthesis, the user
30531 probably forgot the opening one (ie, they typed "@catch
30532 NSException *e)". Parse the closing parenthesis and keep
30533 going. */
30534 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30535 cp_lexer_consume_token (parser->lexer);
30537 /* If these is no immediate closing parenthesis, the user
30538 probably doesn't know that parenthesis are required at
30539 all (ie, they typed "@catch NSException *e"). So, just
30540 forget about the closing parenthesis and keep going. */
30542 objc_begin_catch_clause (parameter_declaration);
30543 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30544 objc_finish_catch_clause ();
30546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30548 cp_lexer_consume_token (parser->lexer);
30549 location = cp_lexer_peek_token (parser->lexer)->location;
30550 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30551 node, lest it get absorbed into the surrounding block. */
30552 stmt = push_stmt_list ();
30553 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30554 objc_build_finally_clause (location, pop_stmt_list (stmt));
30557 return objc_finish_try_stmt ();
30560 /* Parse an Objective-C synchronized statement.
30562 objc-synchronized-stmt:
30563 @synchronized ( expression ) compound-statement
30565 Returns NULL_TREE. */
30567 static tree
30568 cp_parser_objc_synchronized_statement (cp_parser *parser)
30570 location_t location;
30571 tree lock, stmt;
30573 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30575 location = cp_lexer_peek_token (parser->lexer)->location;
30576 objc_maybe_warn_exceptions (location);
30577 matching_parens parens;
30578 parens.require_open (parser);
30579 lock = cp_parser_expression (parser);
30580 parens.require_close (parser);
30582 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30583 node, lest it get absorbed into the surrounding block. */
30584 stmt = push_stmt_list ();
30585 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30587 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30590 /* Parse an Objective-C throw statement.
30592 objc-throw-stmt:
30593 @throw assignment-expression [opt] ;
30595 Returns a constructed '@throw' statement. */
30597 static tree
30598 cp_parser_objc_throw_statement (cp_parser *parser)
30600 tree expr = NULL_TREE;
30601 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30603 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30606 expr = cp_parser_expression (parser);
30608 cp_parser_consume_semicolon_at_end_of_statement (parser);
30610 return objc_build_throw_stmt (loc, expr);
30613 /* Parse an Objective-C statement. */
30615 static tree
30616 cp_parser_objc_statement (cp_parser * parser)
30618 /* Try to figure out what kind of declaration is present. */
30619 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30621 switch (kwd->keyword)
30623 case RID_AT_TRY:
30624 return cp_parser_objc_try_catch_finally_statement (parser);
30625 case RID_AT_SYNCHRONIZED:
30626 return cp_parser_objc_synchronized_statement (parser);
30627 case RID_AT_THROW:
30628 return cp_parser_objc_throw_statement (parser);
30629 default:
30630 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30631 kwd->u.value);
30632 cp_parser_skip_to_end_of_block_or_statement (parser);
30635 return error_mark_node;
30638 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30639 look ahead to see if an objc keyword follows the attributes. This
30640 is to detect the use of prefix attributes on ObjC @interface and
30641 @protocol. */
30643 static bool
30644 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30646 cp_lexer_save_tokens (parser->lexer);
30647 *attrib = cp_parser_attributes_opt (parser);
30648 gcc_assert (*attrib);
30649 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30651 cp_lexer_commit_tokens (parser->lexer);
30652 return true;
30654 cp_lexer_rollback_tokens (parser->lexer);
30655 return false;
30658 /* This routine is a minimal replacement for
30659 c_parser_struct_declaration () used when parsing the list of
30660 types/names or ObjC++ properties. For example, when parsing the
30661 code
30663 @property (readonly) int a, b, c;
30665 this function is responsible for parsing "int a, int b, int c" and
30666 returning the declarations as CHAIN of DECLs.
30668 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30669 similar parsing. */
30670 static tree
30671 cp_parser_objc_struct_declaration (cp_parser *parser)
30673 tree decls = NULL_TREE;
30674 cp_decl_specifier_seq declspecs;
30675 int decl_class_or_enum_p;
30676 tree prefix_attributes;
30678 cp_parser_decl_specifier_seq (parser,
30679 CP_PARSER_FLAGS_NONE,
30680 &declspecs,
30681 &decl_class_or_enum_p);
30683 if (declspecs.type == error_mark_node)
30684 return error_mark_node;
30686 /* auto, register, static, extern, mutable. */
30687 if (declspecs.storage_class != sc_none)
30689 cp_parser_error (parser, "invalid type for property");
30690 declspecs.storage_class = sc_none;
30693 /* thread_local. */
30694 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30696 cp_parser_error (parser, "invalid type for property");
30697 declspecs.locations[ds_thread] = 0;
30700 /* typedef. */
30701 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30703 cp_parser_error (parser, "invalid type for property");
30704 declspecs.locations[ds_typedef] = 0;
30707 prefix_attributes = declspecs.attributes;
30708 declspecs.attributes = NULL_TREE;
30710 /* Keep going until we hit the `;' at the end of the declaration. */
30711 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30713 tree attributes, first_attribute, decl;
30714 cp_declarator *declarator;
30715 cp_token *token;
30717 /* Parse the declarator. */
30718 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30719 NULL, NULL, false, false);
30721 /* Look for attributes that apply to the ivar. */
30722 attributes = cp_parser_attributes_opt (parser);
30723 /* Remember which attributes are prefix attributes and
30724 which are not. */
30725 first_attribute = attributes;
30726 /* Combine the attributes. */
30727 attributes = attr_chainon (prefix_attributes, attributes);
30729 decl = grokfield (declarator, &declspecs,
30730 NULL_TREE, /*init_const_expr_p=*/false,
30731 NULL_TREE, attributes);
30733 if (decl == error_mark_node || decl == NULL_TREE)
30734 return error_mark_node;
30736 /* Reset PREFIX_ATTRIBUTES. */
30737 if (attributes != error_mark_node)
30739 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30740 attributes = TREE_CHAIN (attributes);
30741 if (attributes)
30742 TREE_CHAIN (attributes) = NULL_TREE;
30745 DECL_CHAIN (decl) = decls;
30746 decls = decl;
30748 token = cp_lexer_peek_token (parser->lexer);
30749 if (token->type == CPP_COMMA)
30751 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30752 continue;
30754 else
30755 break;
30757 return decls;
30760 /* Parse an Objective-C @property declaration. The syntax is:
30762 objc-property-declaration:
30763 '@property' objc-property-attributes[opt] struct-declaration ;
30765 objc-property-attributes:
30766 '(' objc-property-attribute-list ')'
30768 objc-property-attribute-list:
30769 objc-property-attribute
30770 objc-property-attribute-list, objc-property-attribute
30772 objc-property-attribute
30773 'getter' = identifier
30774 'setter' = identifier
30775 'readonly'
30776 'readwrite'
30777 'assign'
30778 'retain'
30779 'copy'
30780 'nonatomic'
30782 For example:
30783 @property NSString *name;
30784 @property (readonly) id object;
30785 @property (retain, nonatomic, getter=getTheName) id name;
30786 @property int a, b, c;
30788 PS: This function is identical to
30789 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30790 static void
30791 cp_parser_objc_at_property_declaration (cp_parser *parser)
30793 /* The following variables hold the attributes of the properties as
30794 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30795 seen. When we see an attribute, we set them to 'true' (if they
30796 are boolean properties) or to the identifier (if they have an
30797 argument, ie, for getter and setter). Note that here we only
30798 parse the list of attributes, check the syntax and accumulate the
30799 attributes that we find. objc_add_property_declaration() will
30800 then process the information. */
30801 bool property_assign = false;
30802 bool property_copy = false;
30803 tree property_getter_ident = NULL_TREE;
30804 bool property_nonatomic = false;
30805 bool property_readonly = false;
30806 bool property_readwrite = false;
30807 bool property_retain = false;
30808 tree property_setter_ident = NULL_TREE;
30810 /* 'properties' is the list of properties that we read. Usually a
30811 single one, but maybe more (eg, in "@property int a, b, c;" there
30812 are three). */
30813 tree properties;
30814 location_t loc;
30816 loc = cp_lexer_peek_token (parser->lexer)->location;
30818 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30820 /* Parse the optional attribute list... */
30821 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30823 /* Eat the '('. */
30824 matching_parens parens;
30825 parens.consume_open (parser);
30827 while (true)
30829 bool syntax_error = false;
30830 cp_token *token = cp_lexer_peek_token (parser->lexer);
30831 enum rid keyword;
30833 if (token->type != CPP_NAME)
30835 cp_parser_error (parser, "expected identifier");
30836 break;
30838 keyword = C_RID_CODE (token->u.value);
30839 cp_lexer_consume_token (parser->lexer);
30840 switch (keyword)
30842 case RID_ASSIGN: property_assign = true; break;
30843 case RID_COPY: property_copy = true; break;
30844 case RID_NONATOMIC: property_nonatomic = true; break;
30845 case RID_READONLY: property_readonly = true; break;
30846 case RID_READWRITE: property_readwrite = true; break;
30847 case RID_RETAIN: property_retain = true; break;
30849 case RID_GETTER:
30850 case RID_SETTER:
30851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30853 if (keyword == RID_GETTER)
30854 cp_parser_error (parser,
30855 "missing %<=%> (after %<getter%> attribute)");
30856 else
30857 cp_parser_error (parser,
30858 "missing %<=%> (after %<setter%> attribute)");
30859 syntax_error = true;
30860 break;
30862 cp_lexer_consume_token (parser->lexer); /* eat the = */
30863 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30865 cp_parser_error (parser, "expected identifier");
30866 syntax_error = true;
30867 break;
30869 if (keyword == RID_SETTER)
30871 if (property_setter_ident != NULL_TREE)
30873 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30874 cp_lexer_consume_token (parser->lexer);
30876 else
30877 property_setter_ident = cp_parser_objc_selector (parser);
30878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30879 cp_parser_error (parser, "setter name must terminate with %<:%>");
30880 else
30881 cp_lexer_consume_token (parser->lexer);
30883 else
30885 if (property_getter_ident != NULL_TREE)
30887 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30888 cp_lexer_consume_token (parser->lexer);
30890 else
30891 property_getter_ident = cp_parser_objc_selector (parser);
30893 break;
30894 default:
30895 cp_parser_error (parser, "unknown property attribute");
30896 syntax_error = true;
30897 break;
30900 if (syntax_error)
30901 break;
30903 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30904 cp_lexer_consume_token (parser->lexer);
30905 else
30906 break;
30909 /* FIXME: "@property (setter, assign);" will generate a spurious
30910 "error: expected ‘)’ before ‘,’ token". This is because
30911 cp_parser_require, unlike the C counterpart, will produce an
30912 error even if we are in error recovery. */
30913 if (!parens.require_close (parser))
30915 cp_parser_skip_to_closing_parenthesis (parser,
30916 /*recovering=*/true,
30917 /*or_comma=*/false,
30918 /*consume_paren=*/true);
30922 /* ... and the property declaration(s). */
30923 properties = cp_parser_objc_struct_declaration (parser);
30925 if (properties == error_mark_node)
30927 cp_parser_skip_to_end_of_statement (parser);
30928 /* If the next token is now a `;', consume it. */
30929 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30930 cp_lexer_consume_token (parser->lexer);
30931 return;
30934 if (properties == NULL_TREE)
30935 cp_parser_error (parser, "expected identifier");
30936 else
30938 /* Comma-separated properties are chained together in
30939 reverse order; add them one by one. */
30940 properties = nreverse (properties);
30942 for (; properties; properties = TREE_CHAIN (properties))
30943 objc_add_property_declaration (loc, copy_node (properties),
30944 property_readonly, property_readwrite,
30945 property_assign, property_retain,
30946 property_copy, property_nonatomic,
30947 property_getter_ident, property_setter_ident);
30950 cp_parser_consume_semicolon_at_end_of_statement (parser);
30953 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30955 objc-synthesize-declaration:
30956 @synthesize objc-synthesize-identifier-list ;
30958 objc-synthesize-identifier-list:
30959 objc-synthesize-identifier
30960 objc-synthesize-identifier-list, objc-synthesize-identifier
30962 objc-synthesize-identifier
30963 identifier
30964 identifier = identifier
30966 For example:
30967 @synthesize MyProperty;
30968 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30970 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30971 for C. Keep them in sync.
30973 static void
30974 cp_parser_objc_at_synthesize_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 '@synthesize'. */
30981 while (true)
30983 tree property, ivar;
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 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30992 cp_lexer_consume_token (parser->lexer);
30993 ivar = cp_parser_identifier (parser);
30994 if (ivar == error_mark_node)
30996 cp_parser_consume_semicolon_at_end_of_statement (parser);
30997 return;
31000 else
31001 ivar = NULL_TREE;
31002 list = chainon (list, build_tree_list (ivar, property));
31003 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31004 cp_lexer_consume_token (parser->lexer);
31005 else
31006 break;
31008 cp_parser_consume_semicolon_at_end_of_statement (parser);
31009 objc_add_synthesize_declaration (loc, list);
31012 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31014 objc-dynamic-declaration:
31015 @dynamic identifier-list ;
31017 For example:
31018 @dynamic MyProperty;
31019 @dynamic MyProperty, AnotherProperty;
31021 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31022 for C. Keep them in sync.
31024 static void
31025 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31027 tree list = NULL_TREE;
31028 location_t loc;
31029 loc = cp_lexer_peek_token (parser->lexer)->location;
31031 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31032 while (true)
31034 tree property;
31035 property = cp_parser_identifier (parser);
31036 if (property == error_mark_node)
31038 cp_parser_consume_semicolon_at_end_of_statement (parser);
31039 return;
31041 list = chainon (list, build_tree_list (NULL, property));
31042 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31043 cp_lexer_consume_token (parser->lexer);
31044 else
31045 break;
31047 cp_parser_consume_semicolon_at_end_of_statement (parser);
31048 objc_add_dynamic_declaration (loc, list);
31052 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31054 /* Returns name of the next clause.
31055 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31056 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31057 returned and the token is consumed. */
31059 static pragma_omp_clause
31060 cp_parser_omp_clause_name (cp_parser *parser)
31062 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31064 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31065 result = PRAGMA_OACC_CLAUSE_AUTO;
31066 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31067 result = PRAGMA_OMP_CLAUSE_IF;
31068 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31069 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31070 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31071 result = PRAGMA_OACC_CLAUSE_DELETE;
31072 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31073 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31074 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31075 result = PRAGMA_OMP_CLAUSE_FOR;
31076 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31078 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31079 const char *p = IDENTIFIER_POINTER (id);
31081 switch (p[0])
31083 case 'a':
31084 if (!strcmp ("aligned", p))
31085 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31086 else if (!strcmp ("async", p))
31087 result = PRAGMA_OACC_CLAUSE_ASYNC;
31088 break;
31089 case 'c':
31090 if (!strcmp ("collapse", p))
31091 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31092 else if (!strcmp ("copy", p))
31093 result = PRAGMA_OACC_CLAUSE_COPY;
31094 else if (!strcmp ("copyin", p))
31095 result = PRAGMA_OMP_CLAUSE_COPYIN;
31096 else if (!strcmp ("copyout", p))
31097 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31098 else if (!strcmp ("copyprivate", p))
31099 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31100 else if (!strcmp ("create", p))
31101 result = PRAGMA_OACC_CLAUSE_CREATE;
31102 break;
31103 case 'd':
31104 if (!strcmp ("defaultmap", p))
31105 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31106 else if (!strcmp ("depend", p))
31107 result = PRAGMA_OMP_CLAUSE_DEPEND;
31108 else if (!strcmp ("device", p))
31109 result = PRAGMA_OMP_CLAUSE_DEVICE;
31110 else if (!strcmp ("deviceptr", p))
31111 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31112 else if (!strcmp ("device_resident", p))
31113 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31114 else if (!strcmp ("dist_schedule", p))
31115 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31116 break;
31117 case 'f':
31118 if (!strcmp ("final", p))
31119 result = PRAGMA_OMP_CLAUSE_FINAL;
31120 else if (!strcmp ("firstprivate", p))
31121 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31122 else if (!strcmp ("from", p))
31123 result = PRAGMA_OMP_CLAUSE_FROM;
31124 break;
31125 case 'g':
31126 if (!strcmp ("gang", p))
31127 result = PRAGMA_OACC_CLAUSE_GANG;
31128 else if (!strcmp ("grainsize", p))
31129 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31130 break;
31131 case 'h':
31132 if (!strcmp ("hint", p))
31133 result = PRAGMA_OMP_CLAUSE_HINT;
31134 else if (!strcmp ("host", p))
31135 result = PRAGMA_OACC_CLAUSE_HOST;
31136 break;
31137 case 'i':
31138 if (!strcmp ("inbranch", p))
31139 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31140 else if (!strcmp ("independent", p))
31141 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31142 else if (!strcmp ("is_device_ptr", p))
31143 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31144 break;
31145 case 'l':
31146 if (!strcmp ("lastprivate", p))
31147 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31148 else if (!strcmp ("linear", p))
31149 result = PRAGMA_OMP_CLAUSE_LINEAR;
31150 else if (!strcmp ("link", p))
31151 result = PRAGMA_OMP_CLAUSE_LINK;
31152 break;
31153 case 'm':
31154 if (!strcmp ("map", p))
31155 result = PRAGMA_OMP_CLAUSE_MAP;
31156 else if (!strcmp ("mergeable", p))
31157 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31158 break;
31159 case 'n':
31160 if (!strcmp ("nogroup", p))
31161 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31162 else if (!strcmp ("notinbranch", p))
31163 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31164 else if (!strcmp ("nowait", p))
31165 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31166 else if (!strcmp ("num_gangs", p))
31167 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31168 else if (!strcmp ("num_tasks", p))
31169 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31170 else if (!strcmp ("num_teams", p))
31171 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31172 else if (!strcmp ("num_threads", p))
31173 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31174 else if (!strcmp ("num_workers", p))
31175 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31176 break;
31177 case 'o':
31178 if (!strcmp ("ordered", p))
31179 result = PRAGMA_OMP_CLAUSE_ORDERED;
31180 break;
31181 case 'p':
31182 if (!strcmp ("parallel", p))
31183 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31184 else if (!strcmp ("present", p))
31185 result = PRAGMA_OACC_CLAUSE_PRESENT;
31186 else if (!strcmp ("present_or_copy", p)
31187 || !strcmp ("pcopy", p))
31188 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31189 else if (!strcmp ("present_or_copyin", p)
31190 || !strcmp ("pcopyin", p))
31191 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31192 else if (!strcmp ("present_or_copyout", p)
31193 || !strcmp ("pcopyout", p))
31194 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31195 else if (!strcmp ("present_or_create", p)
31196 || !strcmp ("pcreate", p))
31197 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31198 else if (!strcmp ("priority", p))
31199 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31200 else if (!strcmp ("proc_bind", p))
31201 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31202 break;
31203 case 'r':
31204 if (!strcmp ("reduction", p))
31205 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31206 break;
31207 case 's':
31208 if (!strcmp ("safelen", p))
31209 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31210 else if (!strcmp ("schedule", p))
31211 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31212 else if (!strcmp ("sections", p))
31213 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31214 else if (!strcmp ("self", p))
31215 result = PRAGMA_OACC_CLAUSE_SELF;
31216 else if (!strcmp ("seq", p))
31217 result = PRAGMA_OACC_CLAUSE_SEQ;
31218 else if (!strcmp ("shared", p))
31219 result = PRAGMA_OMP_CLAUSE_SHARED;
31220 else if (!strcmp ("simd", p))
31221 result = PRAGMA_OMP_CLAUSE_SIMD;
31222 else if (!strcmp ("simdlen", p))
31223 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31224 break;
31225 case 't':
31226 if (!strcmp ("taskgroup", p))
31227 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31228 else if (!strcmp ("thread_limit", p))
31229 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31230 else if (!strcmp ("threads", p))
31231 result = PRAGMA_OMP_CLAUSE_THREADS;
31232 else if (!strcmp ("tile", p))
31233 result = PRAGMA_OACC_CLAUSE_TILE;
31234 else if (!strcmp ("to", p))
31235 result = PRAGMA_OMP_CLAUSE_TO;
31236 break;
31237 case 'u':
31238 if (!strcmp ("uniform", p))
31239 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31240 else if (!strcmp ("untied", p))
31241 result = PRAGMA_OMP_CLAUSE_UNTIED;
31242 else if (!strcmp ("use_device", p))
31243 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31244 else if (!strcmp ("use_device_ptr", p))
31245 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31246 break;
31247 case 'v':
31248 if (!strcmp ("vector", p))
31249 result = PRAGMA_OACC_CLAUSE_VECTOR;
31250 else if (!strcmp ("vector_length", p))
31251 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31252 break;
31253 case 'w':
31254 if (!strcmp ("wait", p))
31255 result = PRAGMA_OACC_CLAUSE_WAIT;
31256 else if (!strcmp ("worker", p))
31257 result = PRAGMA_OACC_CLAUSE_WORKER;
31258 break;
31262 if (result != PRAGMA_OMP_CLAUSE_NONE)
31263 cp_lexer_consume_token (parser->lexer);
31265 return result;
31268 /* Validate that a clause of the given type does not already exist. */
31270 static void
31271 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31272 const char *name, location_t location)
31274 tree c;
31276 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31277 if (OMP_CLAUSE_CODE (c) == code)
31279 error_at (location, "too many %qs clauses", name);
31280 break;
31284 /* OpenMP 2.5:
31285 variable-list:
31286 identifier
31287 variable-list , identifier
31289 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31290 colon). An opening parenthesis will have been consumed by the caller.
31292 If KIND is nonzero, create the appropriate node and install the decl
31293 in OMP_CLAUSE_DECL and add the node to the head of the list.
31295 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31296 return the list created.
31298 COLON can be NULL if only closing parenthesis should end the list,
31299 or pointer to bool which will receive false if the list is terminated
31300 by closing parenthesis or true if the list is terminated by colon. */
31302 static tree
31303 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31304 tree list, bool *colon)
31306 cp_token *token;
31307 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31308 if (colon)
31310 parser->colon_corrects_to_scope_p = false;
31311 *colon = false;
31313 while (1)
31315 tree name, decl;
31317 token = cp_lexer_peek_token (parser->lexer);
31318 if (kind != 0
31319 && current_class_ptr
31320 && cp_parser_is_keyword (token, RID_THIS))
31322 decl = finish_this_expr ();
31323 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31324 || CONVERT_EXPR_P (decl))
31325 decl = TREE_OPERAND (decl, 0);
31326 cp_lexer_consume_token (parser->lexer);
31328 else
31330 name = cp_parser_id_expression (parser, /*template_p=*/false,
31331 /*check_dependency_p=*/true,
31332 /*template_p=*/NULL,
31333 /*declarator_p=*/false,
31334 /*optional_p=*/false);
31335 if (name == error_mark_node)
31336 goto skip_comma;
31338 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31339 if (decl == error_mark_node)
31340 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31341 token->location);
31343 if (decl == error_mark_node)
31345 else if (kind != 0)
31347 switch (kind)
31349 case OMP_CLAUSE__CACHE_:
31350 /* The OpenACC cache directive explicitly only allows "array
31351 elements or subarrays". */
31352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31354 error_at (token->location, "expected %<[%>");
31355 decl = error_mark_node;
31356 break;
31358 /* FALLTHROUGH. */
31359 case OMP_CLAUSE_MAP:
31360 case OMP_CLAUSE_FROM:
31361 case OMP_CLAUSE_TO:
31362 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31364 location_t loc
31365 = cp_lexer_peek_token (parser->lexer)->location;
31366 cp_id_kind idk = CP_ID_KIND_NONE;
31367 cp_lexer_consume_token (parser->lexer);
31368 decl = convert_from_reference (decl);
31369 decl
31370 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31371 decl, false,
31372 &idk, loc);
31374 /* FALLTHROUGH. */
31375 case OMP_CLAUSE_DEPEND:
31376 case OMP_CLAUSE_REDUCTION:
31377 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31379 tree low_bound = NULL_TREE, length = NULL_TREE;
31381 parser->colon_corrects_to_scope_p = false;
31382 cp_lexer_consume_token (parser->lexer);
31383 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31384 low_bound = cp_parser_expression (parser);
31385 if (!colon)
31386 parser->colon_corrects_to_scope_p
31387 = saved_colon_corrects_to_scope_p;
31388 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31389 length = integer_one_node;
31390 else
31392 /* Look for `:'. */
31393 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31394 goto skip_comma;
31395 if (!cp_lexer_next_token_is (parser->lexer,
31396 CPP_CLOSE_SQUARE))
31397 length = cp_parser_expression (parser);
31399 /* Look for the closing `]'. */
31400 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31401 RT_CLOSE_SQUARE))
31402 goto skip_comma;
31404 decl = tree_cons (low_bound, length, decl);
31406 break;
31407 default:
31408 break;
31411 tree u = build_omp_clause (token->location, kind);
31412 OMP_CLAUSE_DECL (u) = decl;
31413 OMP_CLAUSE_CHAIN (u) = list;
31414 list = u;
31416 else
31417 list = tree_cons (decl, NULL_TREE, list);
31419 get_comma:
31420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31421 break;
31422 cp_lexer_consume_token (parser->lexer);
31425 if (colon)
31426 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31428 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31430 *colon = true;
31431 cp_parser_require (parser, CPP_COLON, RT_COLON);
31432 return list;
31435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31437 int ending;
31439 /* Try to resync to an unnested comma. Copied from
31440 cp_parser_parenthesized_expression_list. */
31441 skip_comma:
31442 if (colon)
31443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31444 ending = cp_parser_skip_to_closing_parenthesis (parser,
31445 /*recovering=*/true,
31446 /*or_comma=*/true,
31447 /*consume_paren=*/true);
31448 if (ending < 0)
31449 goto get_comma;
31452 return list;
31455 /* Similarly, but expect leading and trailing parenthesis. This is a very
31456 common case for omp clauses. */
31458 static tree
31459 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31461 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31462 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31463 return list;
31466 /* OpenACC 2.0:
31467 copy ( variable-list )
31468 copyin ( variable-list )
31469 copyout ( variable-list )
31470 create ( variable-list )
31471 delete ( variable-list )
31472 present ( variable-list )
31473 present_or_copy ( variable-list )
31474 pcopy ( variable-list )
31475 present_or_copyin ( variable-list )
31476 pcopyin ( variable-list )
31477 present_or_copyout ( variable-list )
31478 pcopyout ( variable-list )
31479 present_or_create ( variable-list )
31480 pcreate ( variable-list ) */
31482 static tree
31483 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31484 tree list)
31486 enum gomp_map_kind kind;
31487 switch (c_kind)
31489 case PRAGMA_OACC_CLAUSE_COPY:
31490 kind = GOMP_MAP_FORCE_TOFROM;
31491 break;
31492 case PRAGMA_OACC_CLAUSE_COPYIN:
31493 kind = GOMP_MAP_FORCE_TO;
31494 break;
31495 case PRAGMA_OACC_CLAUSE_COPYOUT:
31496 kind = GOMP_MAP_FORCE_FROM;
31497 break;
31498 case PRAGMA_OACC_CLAUSE_CREATE:
31499 kind = GOMP_MAP_FORCE_ALLOC;
31500 break;
31501 case PRAGMA_OACC_CLAUSE_DELETE:
31502 kind = GOMP_MAP_DELETE;
31503 break;
31504 case PRAGMA_OACC_CLAUSE_DEVICE:
31505 kind = GOMP_MAP_FORCE_TO;
31506 break;
31507 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31508 kind = GOMP_MAP_DEVICE_RESIDENT;
31509 break;
31510 case PRAGMA_OACC_CLAUSE_HOST:
31511 case PRAGMA_OACC_CLAUSE_SELF:
31512 kind = GOMP_MAP_FORCE_FROM;
31513 break;
31514 case PRAGMA_OACC_CLAUSE_LINK:
31515 kind = GOMP_MAP_LINK;
31516 break;
31517 case PRAGMA_OACC_CLAUSE_PRESENT:
31518 kind = GOMP_MAP_FORCE_PRESENT;
31519 break;
31520 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31521 kind = GOMP_MAP_TOFROM;
31522 break;
31523 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31524 kind = GOMP_MAP_TO;
31525 break;
31526 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31527 kind = GOMP_MAP_FROM;
31528 break;
31529 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31530 kind = GOMP_MAP_ALLOC;
31531 break;
31532 default:
31533 gcc_unreachable ();
31535 tree nl, c;
31536 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31538 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31539 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31541 return nl;
31544 /* OpenACC 2.0:
31545 deviceptr ( variable-list ) */
31547 static tree
31548 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31550 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31551 tree vars, t;
31553 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31554 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31555 variable-list must only allow for pointer variables. */
31556 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31557 for (t = vars; t; t = TREE_CHAIN (t))
31559 tree v = TREE_PURPOSE (t);
31560 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31561 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31562 OMP_CLAUSE_DECL (u) = v;
31563 OMP_CLAUSE_CHAIN (u) = list;
31564 list = u;
31567 return list;
31570 /* OpenACC 2.0:
31571 auto
31572 independent
31573 nohost
31574 seq */
31576 static tree
31577 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31578 enum omp_clause_code code,
31579 tree list, location_t location)
31581 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31582 tree c = build_omp_clause (location, code);
31583 OMP_CLAUSE_CHAIN (c) = list;
31584 return c;
31587 /* OpenACC:
31588 num_gangs ( expression )
31589 num_workers ( expression )
31590 vector_length ( expression ) */
31592 static tree
31593 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31594 const char *str, tree list)
31596 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31598 matching_parens parens;
31599 if (!parens.require_open (parser))
31600 return list;
31602 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31604 if (t == error_mark_node
31605 || !parens.require_close (parser))
31607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31608 /*or_comma=*/false,
31609 /*consume_paren=*/true);
31610 return list;
31613 check_no_duplicate_clause (list, code, str, loc);
31615 tree c = build_omp_clause (loc, code);
31616 OMP_CLAUSE_OPERAND (c, 0) = t;
31617 OMP_CLAUSE_CHAIN (c) = list;
31618 return c;
31621 /* OpenACC:
31623 gang [( gang-arg-list )]
31624 worker [( [num:] int-expr )]
31625 vector [( [length:] int-expr )]
31627 where gang-arg is one of:
31629 [num:] int-expr
31630 static: size-expr
31632 and size-expr may be:
31635 int-expr
31638 static tree
31639 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31640 const char *str, tree list)
31642 const char *id = "num";
31643 cp_lexer *lexer = parser->lexer;
31644 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31645 location_t loc = cp_lexer_peek_token (lexer)->location;
31647 if (kind == OMP_CLAUSE_VECTOR)
31648 id = "length";
31650 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31652 matching_parens parens;
31653 parens.consume_open (parser);
31657 cp_token *next = cp_lexer_peek_token (lexer);
31658 int idx = 0;
31660 /* Gang static argument. */
31661 if (kind == OMP_CLAUSE_GANG
31662 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31664 cp_lexer_consume_token (lexer);
31666 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31667 goto cleanup_error;
31669 idx = 1;
31670 if (ops[idx] != NULL)
31672 cp_parser_error (parser, "too many %<static%> arguments");
31673 goto cleanup_error;
31676 /* Check for the '*' argument. */
31677 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31678 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31679 || cp_lexer_nth_token_is (parser->lexer, 2,
31680 CPP_CLOSE_PAREN)))
31682 cp_lexer_consume_token (lexer);
31683 ops[idx] = integer_minus_one_node;
31685 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31687 cp_lexer_consume_token (lexer);
31688 continue;
31690 else break;
31693 /* Worker num: argument and vector length: arguments. */
31694 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31695 && id_equal (next->u.value, id)
31696 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31698 cp_lexer_consume_token (lexer); /* id */
31699 cp_lexer_consume_token (lexer); /* ':' */
31702 /* Now collect the actual argument. */
31703 if (ops[idx] != NULL_TREE)
31705 cp_parser_error (parser, "unexpected argument");
31706 goto cleanup_error;
31709 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31710 false);
31711 if (expr == error_mark_node)
31712 goto cleanup_error;
31714 mark_exp_read (expr);
31715 ops[idx] = expr;
31717 if (kind == OMP_CLAUSE_GANG
31718 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31720 cp_lexer_consume_token (lexer);
31721 continue;
31723 break;
31725 while (1);
31727 if (!parens.require_close (parser))
31728 goto cleanup_error;
31731 check_no_duplicate_clause (list, kind, str, loc);
31733 c = build_omp_clause (loc, kind);
31735 if (ops[1])
31736 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31738 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31739 OMP_CLAUSE_CHAIN (c) = list;
31741 return c;
31743 cleanup_error:
31744 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31745 return list;
31748 /* OpenACC 2.0:
31749 tile ( size-expr-list ) */
31751 static tree
31752 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31754 tree c, expr = error_mark_node;
31755 tree tile = NULL_TREE;
31757 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31758 so, but the spec authors never considered such a case and have
31759 differing opinions on what it might mean, including 'not
31760 allowed'.) */
31761 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31762 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31763 clause_loc);
31765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31766 return list;
31770 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31771 return list;
31773 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31774 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31775 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31777 cp_lexer_consume_token (parser->lexer);
31778 expr = integer_zero_node;
31780 else
31781 expr = cp_parser_constant_expression (parser);
31783 tile = tree_cons (NULL_TREE, expr, tile);
31785 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31787 /* Consume the trailing ')'. */
31788 cp_lexer_consume_token (parser->lexer);
31790 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31791 tile = nreverse (tile);
31792 OMP_CLAUSE_TILE_LIST (c) = tile;
31793 OMP_CLAUSE_CHAIN (c) = list;
31794 return c;
31797 /* OpenACC 2.0
31798 Parse wait clause or directive parameters. */
31800 static tree
31801 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31803 vec<tree, va_gc> *args;
31804 tree t, args_tree;
31806 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31807 /*cast_p=*/false,
31808 /*allow_expansion_p=*/true,
31809 /*non_constant_p=*/NULL);
31811 if (args == NULL || args->length () == 0)
31813 cp_parser_error (parser, "expected integer expression before ')'");
31814 if (args != NULL)
31815 release_tree_vector (args);
31816 return list;
31819 args_tree = build_tree_list_vec (args);
31821 release_tree_vector (args);
31823 for (t = args_tree; t; t = TREE_CHAIN (t))
31825 tree targ = TREE_VALUE (t);
31827 if (targ != error_mark_node)
31829 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31830 error ("%<wait%> expression must be integral");
31831 else
31833 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31835 targ = mark_rvalue_use (targ);
31836 OMP_CLAUSE_DECL (c) = targ;
31837 OMP_CLAUSE_CHAIN (c) = list;
31838 list = c;
31843 return list;
31846 /* OpenACC:
31847 wait ( int-expr-list ) */
31849 static tree
31850 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31852 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31854 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31855 return list;
31857 list = cp_parser_oacc_wait_list (parser, location, list);
31859 return list;
31862 /* OpenMP 3.0:
31863 collapse ( constant-expression ) */
31865 static tree
31866 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31868 tree c, num;
31869 location_t loc;
31870 HOST_WIDE_INT n;
31872 loc = cp_lexer_peek_token (parser->lexer)->location;
31873 matching_parens parens;
31874 if (!parens.require_open (parser))
31875 return list;
31877 num = cp_parser_constant_expression (parser);
31879 if (!parens.require_close (parser))
31880 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31881 /*or_comma=*/false,
31882 /*consume_paren=*/true);
31884 if (num == error_mark_node)
31885 return list;
31886 num = fold_non_dependent_expr (num);
31887 if (!tree_fits_shwi_p (num)
31888 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31889 || (n = tree_to_shwi (num)) <= 0
31890 || (int) n != n)
31892 error_at (loc, "collapse argument needs positive constant integer expression");
31893 return list;
31896 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31897 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31898 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31899 OMP_CLAUSE_CHAIN (c) = list;
31900 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31902 return c;
31905 /* OpenMP 2.5:
31906 default ( none | shared )
31908 OpenACC:
31909 default ( none | present ) */
31911 static tree
31912 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31913 location_t location, bool is_oacc)
31915 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31916 tree c;
31918 matching_parens parens;
31919 if (!parens.require_open (parser))
31920 return list;
31921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31923 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31924 const char *p = IDENTIFIER_POINTER (id);
31926 switch (p[0])
31928 case 'n':
31929 if (strcmp ("none", p) != 0)
31930 goto invalid_kind;
31931 kind = OMP_CLAUSE_DEFAULT_NONE;
31932 break;
31934 case 'p':
31935 if (strcmp ("present", p) != 0 || !is_oacc)
31936 goto invalid_kind;
31937 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31938 break;
31940 case 's':
31941 if (strcmp ("shared", p) != 0 || is_oacc)
31942 goto invalid_kind;
31943 kind = OMP_CLAUSE_DEFAULT_SHARED;
31944 break;
31946 default:
31947 goto invalid_kind;
31950 cp_lexer_consume_token (parser->lexer);
31952 else
31954 invalid_kind:
31955 if (is_oacc)
31956 cp_parser_error (parser, "expected %<none%> or %<present%>");
31957 else
31958 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31961 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31962 || !parens.require_close (parser))
31963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31964 /*or_comma=*/false,
31965 /*consume_paren=*/true);
31967 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31968 return list;
31970 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31971 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31972 OMP_CLAUSE_CHAIN (c) = list;
31973 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31975 return c;
31978 /* OpenMP 3.1:
31979 final ( expression ) */
31981 static tree
31982 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31984 tree t, c;
31986 matching_parens parens;
31987 if (!parens.require_open (parser))
31988 return list;
31990 t = cp_parser_condition (parser);
31992 if (t == error_mark_node
31993 || !parens.require_close (parser))
31994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31995 /*or_comma=*/false,
31996 /*consume_paren=*/true);
31998 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32000 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32001 OMP_CLAUSE_FINAL_EXPR (c) = t;
32002 OMP_CLAUSE_CHAIN (c) = list;
32004 return c;
32007 /* OpenMP 2.5:
32008 if ( expression )
32010 OpenMP 4.5:
32011 if ( directive-name-modifier : expression )
32013 directive-name-modifier:
32014 parallel | task | taskloop | target data | target | target update
32015 | target enter data | target exit data */
32017 static tree
32018 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32019 bool is_omp)
32021 tree t, c;
32022 enum tree_code if_modifier = ERROR_MARK;
32024 matching_parens parens;
32025 if (!parens.require_open (parser))
32026 return list;
32028 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32030 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32031 const char *p = IDENTIFIER_POINTER (id);
32032 int n = 2;
32034 if (strcmp ("parallel", p) == 0)
32035 if_modifier = OMP_PARALLEL;
32036 else if (strcmp ("task", p) == 0)
32037 if_modifier = OMP_TASK;
32038 else if (strcmp ("taskloop", p) == 0)
32039 if_modifier = OMP_TASKLOOP;
32040 else if (strcmp ("target", p) == 0)
32042 if_modifier = OMP_TARGET;
32043 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32045 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32046 p = IDENTIFIER_POINTER (id);
32047 if (strcmp ("data", p) == 0)
32048 if_modifier = OMP_TARGET_DATA;
32049 else if (strcmp ("update", p) == 0)
32050 if_modifier = OMP_TARGET_UPDATE;
32051 else if (strcmp ("enter", p) == 0)
32052 if_modifier = OMP_TARGET_ENTER_DATA;
32053 else if (strcmp ("exit", p) == 0)
32054 if_modifier = OMP_TARGET_EXIT_DATA;
32055 if (if_modifier != OMP_TARGET)
32056 n = 3;
32057 else
32059 location_t loc
32060 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32061 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32062 "or %<exit%>");
32063 if_modifier = ERROR_MARK;
32065 if (if_modifier == OMP_TARGET_ENTER_DATA
32066 || if_modifier == OMP_TARGET_EXIT_DATA)
32068 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32070 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32071 p = IDENTIFIER_POINTER (id);
32072 if (strcmp ("data", p) == 0)
32073 n = 4;
32075 if (n != 4)
32077 location_t loc
32078 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32079 error_at (loc, "expected %<data%>");
32080 if_modifier = ERROR_MARK;
32085 if (if_modifier != ERROR_MARK)
32087 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32089 while (n-- > 0)
32090 cp_lexer_consume_token (parser->lexer);
32092 else
32094 if (n > 2)
32096 location_t loc
32097 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32098 error_at (loc, "expected %<:%>");
32100 if_modifier = ERROR_MARK;
32105 t = cp_parser_condition (parser);
32107 if (t == error_mark_node
32108 || !parens.require_close (parser))
32109 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32110 /*or_comma=*/false,
32111 /*consume_paren=*/true);
32113 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32114 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32116 if (if_modifier != ERROR_MARK
32117 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32119 const char *p = NULL;
32120 switch (if_modifier)
32122 case OMP_PARALLEL: p = "parallel"; break;
32123 case OMP_TASK: p = "task"; break;
32124 case OMP_TASKLOOP: p = "taskloop"; break;
32125 case OMP_TARGET_DATA: p = "target data"; break;
32126 case OMP_TARGET: p = "target"; break;
32127 case OMP_TARGET_UPDATE: p = "target update"; break;
32128 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32129 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32130 default: gcc_unreachable ();
32132 error_at (location, "too many %<if%> clauses with %qs modifier",
32134 return list;
32136 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32138 if (!is_omp)
32139 error_at (location, "too many %<if%> clauses");
32140 else
32141 error_at (location, "too many %<if%> clauses without modifier");
32142 return list;
32144 else if (if_modifier == ERROR_MARK
32145 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32147 error_at (location, "if any %<if%> clause has modifier, then all "
32148 "%<if%> clauses have to use modifier");
32149 return list;
32153 c = build_omp_clause (location, OMP_CLAUSE_IF);
32154 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32155 OMP_CLAUSE_IF_EXPR (c) = t;
32156 OMP_CLAUSE_CHAIN (c) = list;
32158 return c;
32161 /* OpenMP 3.1:
32162 mergeable */
32164 static tree
32165 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32166 tree list, location_t location)
32168 tree c;
32170 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32171 location);
32173 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32174 OMP_CLAUSE_CHAIN (c) = list;
32175 return c;
32178 /* OpenMP 2.5:
32179 nowait */
32181 static tree
32182 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32183 tree list, location_t location)
32185 tree c;
32187 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32189 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32190 OMP_CLAUSE_CHAIN (c) = list;
32191 return c;
32194 /* OpenMP 2.5:
32195 num_threads ( expression ) */
32197 static tree
32198 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32199 location_t location)
32201 tree t, c;
32203 matching_parens parens;
32204 if (!parens.require_open (parser))
32205 return list;
32207 t = cp_parser_expression (parser);
32209 if (t == error_mark_node
32210 || !parens.require_close (parser))
32211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32212 /*or_comma=*/false,
32213 /*consume_paren=*/true);
32215 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32216 "num_threads", location);
32218 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32219 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32220 OMP_CLAUSE_CHAIN (c) = list;
32222 return c;
32225 /* OpenMP 4.5:
32226 num_tasks ( expression ) */
32228 static tree
32229 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32230 location_t location)
32232 tree t, c;
32234 matching_parens parens;
32235 if (!parens.require_open (parser))
32236 return list;
32238 t = cp_parser_expression (parser);
32240 if (t == error_mark_node
32241 || !parens.require_close (parser))
32242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32243 /*or_comma=*/false,
32244 /*consume_paren=*/true);
32246 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32247 "num_tasks", location);
32249 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32250 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32251 OMP_CLAUSE_CHAIN (c) = list;
32253 return c;
32256 /* OpenMP 4.5:
32257 grainsize ( expression ) */
32259 static tree
32260 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32261 location_t location)
32263 tree t, c;
32265 matching_parens parens;
32266 if (!parens.require_open (parser))
32267 return list;
32269 t = cp_parser_expression (parser);
32271 if (t == error_mark_node
32272 || !parens.require_close (parser))
32273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32274 /*or_comma=*/false,
32275 /*consume_paren=*/true);
32277 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32278 "grainsize", location);
32280 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32281 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32282 OMP_CLAUSE_CHAIN (c) = list;
32284 return c;
32287 /* OpenMP 4.5:
32288 priority ( expression ) */
32290 static tree
32291 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32292 location_t location)
32294 tree t, c;
32296 matching_parens parens;
32297 if (!parens.require_open (parser))
32298 return list;
32300 t = cp_parser_expression (parser);
32302 if (t == error_mark_node
32303 || !parens.require_close (parser))
32304 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32305 /*or_comma=*/false,
32306 /*consume_paren=*/true);
32308 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32309 "priority", location);
32311 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32312 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32313 OMP_CLAUSE_CHAIN (c) = list;
32315 return c;
32318 /* OpenMP 4.5:
32319 hint ( expression ) */
32321 static tree
32322 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32323 location_t location)
32325 tree t, c;
32327 matching_parens parens;
32328 if (!parens.require_open (parser))
32329 return list;
32331 t = cp_parser_expression (parser);
32333 if (t == error_mark_node
32334 || !parens.require_close (parser))
32335 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32336 /*or_comma=*/false,
32337 /*consume_paren=*/true);
32339 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32341 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32342 OMP_CLAUSE_HINT_EXPR (c) = t;
32343 OMP_CLAUSE_CHAIN (c) = list;
32345 return c;
32348 /* OpenMP 4.5:
32349 defaultmap ( tofrom : scalar ) */
32351 static tree
32352 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32353 location_t location)
32355 tree c, id;
32356 const char *p;
32358 matching_parens parens;
32359 if (!parens.require_open (parser))
32360 return list;
32362 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32364 cp_parser_error (parser, "expected %<tofrom%>");
32365 goto out_err;
32367 id = cp_lexer_peek_token (parser->lexer)->u.value;
32368 p = IDENTIFIER_POINTER (id);
32369 if (strcmp (p, "tofrom") != 0)
32371 cp_parser_error (parser, "expected %<tofrom%>");
32372 goto out_err;
32374 cp_lexer_consume_token (parser->lexer);
32375 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32376 goto out_err;
32378 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32380 cp_parser_error (parser, "expected %<scalar%>");
32381 goto out_err;
32383 id = cp_lexer_peek_token (parser->lexer)->u.value;
32384 p = IDENTIFIER_POINTER (id);
32385 if (strcmp (p, "scalar") != 0)
32387 cp_parser_error (parser, "expected %<scalar%>");
32388 goto out_err;
32390 cp_lexer_consume_token (parser->lexer);
32391 if (!parens.require_close (parser))
32392 goto out_err;
32394 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32395 location);
32397 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32398 OMP_CLAUSE_CHAIN (c) = list;
32399 return c;
32401 out_err:
32402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32403 /*or_comma=*/false,
32404 /*consume_paren=*/true);
32405 return list;
32408 /* OpenMP 2.5:
32409 ordered
32411 OpenMP 4.5:
32412 ordered ( constant-expression ) */
32414 static tree
32415 cp_parser_omp_clause_ordered (cp_parser *parser,
32416 tree list, location_t location)
32418 tree c, num = NULL_TREE;
32419 HOST_WIDE_INT n;
32421 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32422 "ordered", location);
32424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32426 matching_parens parens;
32427 parens.consume_open (parser);
32429 num = cp_parser_constant_expression (parser);
32431 if (!parens.require_close (parser))
32432 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32433 /*or_comma=*/false,
32434 /*consume_paren=*/true);
32436 if (num == error_mark_node)
32437 return list;
32438 num = fold_non_dependent_expr (num);
32439 if (!tree_fits_shwi_p (num)
32440 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32441 || (n = tree_to_shwi (num)) <= 0
32442 || (int) n != n)
32444 error_at (location,
32445 "ordered argument needs positive constant integer "
32446 "expression");
32447 return list;
32451 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32452 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32453 OMP_CLAUSE_CHAIN (c) = list;
32454 return c;
32457 /* OpenMP 2.5:
32458 reduction ( reduction-operator : variable-list )
32460 reduction-operator:
32461 One of: + * - & ^ | && ||
32463 OpenMP 3.1:
32465 reduction-operator:
32466 One of: + * - & ^ | && || min max
32468 OpenMP 4.0:
32470 reduction-operator:
32471 One of: + * - & ^ | && ||
32472 id-expression */
32474 static tree
32475 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32477 enum tree_code code = ERROR_MARK;
32478 tree nlist, c, id = NULL_TREE;
32480 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32481 return list;
32483 switch (cp_lexer_peek_token (parser->lexer)->type)
32485 case CPP_PLUS: code = PLUS_EXPR; break;
32486 case CPP_MULT: code = MULT_EXPR; break;
32487 case CPP_MINUS: code = MINUS_EXPR; break;
32488 case CPP_AND: code = BIT_AND_EXPR; break;
32489 case CPP_XOR: code = BIT_XOR_EXPR; break;
32490 case CPP_OR: code = BIT_IOR_EXPR; break;
32491 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32492 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32493 default: break;
32496 if (code != ERROR_MARK)
32497 cp_lexer_consume_token (parser->lexer);
32498 else
32500 bool saved_colon_corrects_to_scope_p;
32501 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32502 parser->colon_corrects_to_scope_p = false;
32503 id = cp_parser_id_expression (parser, /*template_p=*/false,
32504 /*check_dependency_p=*/true,
32505 /*template_p=*/NULL,
32506 /*declarator_p=*/false,
32507 /*optional_p=*/false);
32508 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32509 if (identifier_p (id))
32511 const char *p = IDENTIFIER_POINTER (id);
32513 if (strcmp (p, "min") == 0)
32514 code = MIN_EXPR;
32515 else if (strcmp (p, "max") == 0)
32516 code = MAX_EXPR;
32517 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32518 code = PLUS_EXPR;
32519 else if (id == ovl_op_identifier (false, MULT_EXPR))
32520 code = MULT_EXPR;
32521 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32522 code = MINUS_EXPR;
32523 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32524 code = BIT_AND_EXPR;
32525 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32526 code = BIT_IOR_EXPR;
32527 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32528 code = BIT_XOR_EXPR;
32529 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32530 code = TRUTH_ANDIF_EXPR;
32531 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32532 code = TRUTH_ORIF_EXPR;
32533 id = omp_reduction_id (code, id, NULL_TREE);
32534 tree scope = parser->scope;
32535 if (scope)
32536 id = build_qualified_name (NULL_TREE, scope, id, false);
32537 parser->scope = NULL_TREE;
32538 parser->qualifying_scope = NULL_TREE;
32539 parser->object_scope = NULL_TREE;
32541 else
32543 error ("invalid reduction-identifier");
32544 resync_fail:
32545 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32546 /*or_comma=*/false,
32547 /*consume_paren=*/true);
32548 return list;
32552 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32553 goto resync_fail;
32555 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32556 NULL);
32557 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32559 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32560 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32563 return nlist;
32566 /* OpenMP 2.5:
32567 schedule ( schedule-kind )
32568 schedule ( schedule-kind , expression )
32570 schedule-kind:
32571 static | dynamic | guided | runtime | auto
32573 OpenMP 4.5:
32574 schedule ( schedule-modifier : schedule-kind )
32575 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32577 schedule-modifier:
32578 simd
32579 monotonic
32580 nonmonotonic */
32582 static tree
32583 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32585 tree c, t;
32586 int modifiers = 0, nmodifiers = 0;
32588 matching_parens parens;
32589 if (!parens.require_open (parser))
32590 return list;
32592 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32594 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32597 const char *p = IDENTIFIER_POINTER (id);
32598 if (strcmp ("simd", p) == 0)
32599 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32600 else if (strcmp ("monotonic", p) == 0)
32601 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32602 else if (strcmp ("nonmonotonic", p) == 0)
32603 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32604 else
32605 break;
32606 cp_lexer_consume_token (parser->lexer);
32607 if (nmodifiers++ == 0
32608 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32609 cp_lexer_consume_token (parser->lexer);
32610 else
32612 cp_parser_require (parser, CPP_COLON, RT_COLON);
32613 break;
32617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32619 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32620 const char *p = IDENTIFIER_POINTER (id);
32622 switch (p[0])
32624 case 'd':
32625 if (strcmp ("dynamic", p) != 0)
32626 goto invalid_kind;
32627 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32628 break;
32630 case 'g':
32631 if (strcmp ("guided", p) != 0)
32632 goto invalid_kind;
32633 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32634 break;
32636 case 'r':
32637 if (strcmp ("runtime", p) != 0)
32638 goto invalid_kind;
32639 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32640 break;
32642 default:
32643 goto invalid_kind;
32646 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32648 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32649 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32650 else
32651 goto invalid_kind;
32652 cp_lexer_consume_token (parser->lexer);
32654 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32655 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32656 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32657 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32659 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32660 "specified");
32661 modifiers = 0;
32664 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32666 cp_token *token;
32667 cp_lexer_consume_token (parser->lexer);
32669 token = cp_lexer_peek_token (parser->lexer);
32670 t = cp_parser_assignment_expression (parser);
32672 if (t == error_mark_node)
32673 goto resync_fail;
32674 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32675 error_at (token->location, "schedule %<runtime%> does not take "
32676 "a %<chunk_size%> parameter");
32677 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32678 error_at (token->location, "schedule %<auto%> does not take "
32679 "a %<chunk_size%> parameter");
32680 else
32681 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32683 if (!parens.require_close (parser))
32684 goto resync_fail;
32686 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32687 goto resync_fail;
32689 OMP_CLAUSE_SCHEDULE_KIND (c)
32690 = (enum omp_clause_schedule_kind)
32691 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32693 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32694 OMP_CLAUSE_CHAIN (c) = list;
32695 return c;
32697 invalid_kind:
32698 cp_parser_error (parser, "invalid schedule kind");
32699 resync_fail:
32700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32701 /*or_comma=*/false,
32702 /*consume_paren=*/true);
32703 return list;
32706 /* OpenMP 3.0:
32707 untied */
32709 static tree
32710 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32711 tree list, location_t location)
32713 tree c;
32715 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32717 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32718 OMP_CLAUSE_CHAIN (c) = list;
32719 return c;
32722 /* OpenMP 4.0:
32723 inbranch
32724 notinbranch */
32726 static tree
32727 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32728 tree list, location_t location)
32730 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32731 tree c = build_omp_clause (location, code);
32732 OMP_CLAUSE_CHAIN (c) = list;
32733 return c;
32736 /* OpenMP 4.0:
32737 parallel
32739 sections
32740 taskgroup */
32742 static tree
32743 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32744 enum omp_clause_code code,
32745 tree list, location_t location)
32747 tree c = build_omp_clause (location, code);
32748 OMP_CLAUSE_CHAIN (c) = list;
32749 return c;
32752 /* OpenMP 4.5:
32753 nogroup */
32755 static tree
32756 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32757 tree list, location_t location)
32759 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32760 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32761 OMP_CLAUSE_CHAIN (c) = list;
32762 return c;
32765 /* OpenMP 4.5:
32766 simd
32767 threads */
32769 static tree
32770 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32771 enum omp_clause_code code,
32772 tree list, location_t location)
32774 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32775 tree c = build_omp_clause (location, code);
32776 OMP_CLAUSE_CHAIN (c) = list;
32777 return c;
32780 /* OpenMP 4.0:
32781 num_teams ( expression ) */
32783 static tree
32784 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32785 location_t location)
32787 tree t, c;
32789 matching_parens parens;
32790 if (!parens.require_open (parser))
32791 return list;
32793 t = cp_parser_expression (parser);
32795 if (t == error_mark_node
32796 || !parens.require_close (parser))
32797 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32798 /*or_comma=*/false,
32799 /*consume_paren=*/true);
32801 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32802 "num_teams", location);
32804 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32805 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32806 OMP_CLAUSE_CHAIN (c) = list;
32808 return c;
32811 /* OpenMP 4.0:
32812 thread_limit ( expression ) */
32814 static tree
32815 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32816 location_t location)
32818 tree t, c;
32820 matching_parens parens;
32821 if (!parens.require_open (parser))
32822 return list;
32824 t = cp_parser_expression (parser);
32826 if (t == error_mark_node
32827 || !parens.require_close (parser))
32828 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32829 /*or_comma=*/false,
32830 /*consume_paren=*/true);
32832 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32833 "thread_limit", location);
32835 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32836 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32837 OMP_CLAUSE_CHAIN (c) = list;
32839 return c;
32842 /* OpenMP 4.0:
32843 aligned ( variable-list )
32844 aligned ( variable-list : constant-expression ) */
32846 static tree
32847 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32849 tree nlist, c, alignment = NULL_TREE;
32850 bool colon;
32852 matching_parens parens;
32853 if (!parens.require_open (parser))
32854 return list;
32856 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32857 &colon);
32859 if (colon)
32861 alignment = cp_parser_constant_expression (parser);
32863 if (!parens.require_close (parser))
32864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32865 /*or_comma=*/false,
32866 /*consume_paren=*/true);
32868 if (alignment == error_mark_node)
32869 alignment = NULL_TREE;
32872 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32873 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32875 return nlist;
32878 /* OpenMP 4.0:
32879 linear ( variable-list )
32880 linear ( variable-list : expression )
32882 OpenMP 4.5:
32883 linear ( modifier ( variable-list ) )
32884 linear ( modifier ( variable-list ) : expression ) */
32886 static tree
32887 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32888 bool declare_simd)
32890 tree nlist, c, step = integer_one_node;
32891 bool colon;
32892 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32894 matching_parens parens;
32895 if (!parens.require_open (parser))
32896 return list;
32898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32900 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32901 const char *p = IDENTIFIER_POINTER (id);
32903 if (strcmp ("ref", p) == 0)
32904 kind = OMP_CLAUSE_LINEAR_REF;
32905 else if (strcmp ("val", p) == 0)
32906 kind = OMP_CLAUSE_LINEAR_VAL;
32907 else if (strcmp ("uval", p) == 0)
32908 kind = OMP_CLAUSE_LINEAR_UVAL;
32909 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32910 cp_lexer_consume_token (parser->lexer);
32911 else
32912 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32915 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32916 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32917 &colon);
32918 else
32920 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32921 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32922 if (colon)
32923 cp_parser_require (parser, CPP_COLON, RT_COLON);
32924 else if (!parens.require_close (parser))
32925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32926 /*or_comma=*/false,
32927 /*consume_paren=*/true);
32930 if (colon)
32932 step = NULL_TREE;
32933 if (declare_simd
32934 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32935 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32937 cp_token *token = cp_lexer_peek_token (parser->lexer);
32938 cp_parser_parse_tentatively (parser);
32939 step = cp_parser_id_expression (parser, /*template_p=*/false,
32940 /*check_dependency_p=*/true,
32941 /*template_p=*/NULL,
32942 /*declarator_p=*/false,
32943 /*optional_p=*/false);
32944 if (step != error_mark_node)
32945 step = cp_parser_lookup_name_simple (parser, step, token->location);
32946 if (step == error_mark_node)
32948 step = NULL_TREE;
32949 cp_parser_abort_tentative_parse (parser);
32951 else if (!cp_parser_parse_definitely (parser))
32952 step = NULL_TREE;
32954 if (!step)
32955 step = cp_parser_expression (parser);
32957 if (!parens.require_close (parser))
32958 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32959 /*or_comma=*/false,
32960 /*consume_paren=*/true);
32962 if (step == error_mark_node)
32963 return list;
32966 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32968 OMP_CLAUSE_LINEAR_STEP (c) = step;
32969 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32972 return nlist;
32975 /* OpenMP 4.0:
32976 safelen ( constant-expression ) */
32978 static tree
32979 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32980 location_t location)
32982 tree t, c;
32984 matching_parens parens;
32985 if (!parens.require_open (parser))
32986 return list;
32988 t = cp_parser_constant_expression (parser);
32990 if (t == error_mark_node
32991 || !parens.require_close (parser))
32992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32993 /*or_comma=*/false,
32994 /*consume_paren=*/true);
32996 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32998 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32999 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33000 OMP_CLAUSE_CHAIN (c) = list;
33002 return c;
33005 /* OpenMP 4.0:
33006 simdlen ( constant-expression ) */
33008 static tree
33009 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33010 location_t location)
33012 tree t, c;
33014 matching_parens parens;
33015 if (!parens.require_open (parser))
33016 return list;
33018 t = cp_parser_constant_expression (parser);
33020 if (t == error_mark_node
33021 || !parens.require_close (parser))
33022 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33023 /*or_comma=*/false,
33024 /*consume_paren=*/true);
33026 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33028 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33029 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33030 OMP_CLAUSE_CHAIN (c) = list;
33032 return c;
33035 /* OpenMP 4.5:
33036 vec:
33037 identifier [+/- integer]
33038 vec , identifier [+/- integer]
33041 static tree
33042 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33043 tree list)
33045 tree vec = NULL;
33047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33049 cp_parser_error (parser, "expected identifier");
33050 return list;
33053 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33055 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33056 tree t, identifier = cp_parser_identifier (parser);
33057 tree addend = NULL;
33059 if (identifier == error_mark_node)
33060 t = error_mark_node;
33061 else
33063 t = cp_parser_lookup_name_simple
33064 (parser, identifier,
33065 cp_lexer_peek_token (parser->lexer)->location);
33066 if (t == error_mark_node)
33067 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33068 id_loc);
33071 bool neg = false;
33072 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33073 neg = true;
33074 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33076 addend = integer_zero_node;
33077 goto add_to_vector;
33079 cp_lexer_consume_token (parser->lexer);
33081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33083 cp_parser_error (parser, "expected integer");
33084 return list;
33087 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33088 if (TREE_CODE (addend) != INTEGER_CST)
33090 cp_parser_error (parser, "expected integer");
33091 return list;
33093 cp_lexer_consume_token (parser->lexer);
33095 add_to_vector:
33096 if (t != error_mark_node)
33098 vec = tree_cons (addend, t, vec);
33099 if (neg)
33100 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33104 break;
33106 cp_lexer_consume_token (parser->lexer);
33109 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33111 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33112 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33113 OMP_CLAUSE_DECL (u) = nreverse (vec);
33114 OMP_CLAUSE_CHAIN (u) = list;
33115 return u;
33117 return list;
33120 /* OpenMP 4.0:
33121 depend ( depend-kind : variable-list )
33123 depend-kind:
33124 in | out | inout
33126 OpenMP 4.5:
33127 depend ( source )
33129 depend ( sink : vec ) */
33131 static tree
33132 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33134 tree nlist, c;
33135 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33137 matching_parens parens;
33138 if (!parens.require_open (parser))
33139 return list;
33141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33144 const char *p = IDENTIFIER_POINTER (id);
33146 if (strcmp ("in", p) == 0)
33147 kind = OMP_CLAUSE_DEPEND_IN;
33148 else if (strcmp ("inout", p) == 0)
33149 kind = OMP_CLAUSE_DEPEND_INOUT;
33150 else if (strcmp ("out", p) == 0)
33151 kind = OMP_CLAUSE_DEPEND_OUT;
33152 else if (strcmp ("source", p) == 0)
33153 kind = OMP_CLAUSE_DEPEND_SOURCE;
33154 else if (strcmp ("sink", p) == 0)
33155 kind = OMP_CLAUSE_DEPEND_SINK;
33156 else
33157 goto invalid_kind;
33159 else
33160 goto invalid_kind;
33162 cp_lexer_consume_token (parser->lexer);
33164 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33166 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33167 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33168 OMP_CLAUSE_DECL (c) = NULL_TREE;
33169 OMP_CLAUSE_CHAIN (c) = list;
33170 if (!parens.require_close (parser))
33171 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33172 /*or_comma=*/false,
33173 /*consume_paren=*/true);
33174 return c;
33177 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33178 goto resync_fail;
33180 if (kind == OMP_CLAUSE_DEPEND_SINK)
33181 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33182 else
33184 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33185 list, NULL);
33187 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33188 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33190 return nlist;
33192 invalid_kind:
33193 cp_parser_error (parser, "invalid depend kind");
33194 resync_fail:
33195 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33196 /*or_comma=*/false,
33197 /*consume_paren=*/true);
33198 return list;
33201 /* OpenMP 4.0:
33202 map ( map-kind : variable-list )
33203 map ( variable-list )
33205 map-kind:
33206 alloc | to | from | tofrom
33208 OpenMP 4.5:
33209 map-kind:
33210 alloc | to | from | tofrom | release | delete
33212 map ( always [,] map-kind: variable-list ) */
33214 static tree
33215 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33217 tree nlist, c;
33218 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33219 bool always = false;
33221 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33222 return list;
33224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33226 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33227 const char *p = IDENTIFIER_POINTER (id);
33229 if (strcmp ("always", p) == 0)
33231 int nth = 2;
33232 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33233 nth++;
33234 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33235 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33236 == RID_DELETE))
33237 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33238 == CPP_COLON))
33240 always = true;
33241 cp_lexer_consume_token (parser->lexer);
33242 if (nth == 3)
33243 cp_lexer_consume_token (parser->lexer);
33248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33249 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33252 const char *p = IDENTIFIER_POINTER (id);
33254 if (strcmp ("alloc", p) == 0)
33255 kind = GOMP_MAP_ALLOC;
33256 else if (strcmp ("to", p) == 0)
33257 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33258 else if (strcmp ("from", p) == 0)
33259 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33260 else if (strcmp ("tofrom", p) == 0)
33261 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33262 else if (strcmp ("release", p) == 0)
33263 kind = GOMP_MAP_RELEASE;
33264 else
33266 cp_parser_error (parser, "invalid map kind");
33267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33268 /*or_comma=*/false,
33269 /*consume_paren=*/true);
33270 return list;
33272 cp_lexer_consume_token (parser->lexer);
33273 cp_lexer_consume_token (parser->lexer);
33275 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33276 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33278 kind = GOMP_MAP_DELETE;
33279 cp_lexer_consume_token (parser->lexer);
33280 cp_lexer_consume_token (parser->lexer);
33283 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33284 NULL);
33286 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33287 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33289 return nlist;
33292 /* OpenMP 4.0:
33293 device ( expression ) */
33295 static tree
33296 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33297 location_t location)
33299 tree t, c;
33301 matching_parens parens;
33302 if (!parens.require_open (parser))
33303 return list;
33305 t = cp_parser_expression (parser);
33307 if (t == error_mark_node
33308 || !parens.require_close (parser))
33309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33310 /*or_comma=*/false,
33311 /*consume_paren=*/true);
33313 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33314 "device", location);
33316 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33317 OMP_CLAUSE_DEVICE_ID (c) = t;
33318 OMP_CLAUSE_CHAIN (c) = list;
33320 return c;
33323 /* OpenMP 4.0:
33324 dist_schedule ( static )
33325 dist_schedule ( static , expression ) */
33327 static tree
33328 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33329 location_t location)
33331 tree c, t;
33333 matching_parens parens;
33334 if (!parens.require_open (parser))
33335 return list;
33337 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33339 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33340 goto invalid_kind;
33341 cp_lexer_consume_token (parser->lexer);
33343 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33345 cp_lexer_consume_token (parser->lexer);
33347 t = cp_parser_assignment_expression (parser);
33349 if (t == error_mark_node)
33350 goto resync_fail;
33351 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33353 if (!parens.require_close (parser))
33354 goto resync_fail;
33356 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33357 goto resync_fail;
33359 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33360 location);
33361 OMP_CLAUSE_CHAIN (c) = list;
33362 return c;
33364 invalid_kind:
33365 cp_parser_error (parser, "invalid dist_schedule kind");
33366 resync_fail:
33367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33370 return list;
33373 /* OpenMP 4.0:
33374 proc_bind ( proc-bind-kind )
33376 proc-bind-kind:
33377 master | close | spread */
33379 static tree
33380 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33381 location_t location)
33383 tree c;
33384 enum omp_clause_proc_bind_kind kind;
33386 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33387 return list;
33389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33392 const char *p = IDENTIFIER_POINTER (id);
33394 if (strcmp ("master", p) == 0)
33395 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33396 else if (strcmp ("close", p) == 0)
33397 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33398 else if (strcmp ("spread", p) == 0)
33399 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33400 else
33401 goto invalid_kind;
33403 else
33404 goto invalid_kind;
33406 cp_lexer_consume_token (parser->lexer);
33407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33408 goto resync_fail;
33410 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33411 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33412 location);
33413 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33414 OMP_CLAUSE_CHAIN (c) = list;
33415 return c;
33417 invalid_kind:
33418 cp_parser_error (parser, "invalid depend kind");
33419 resync_fail:
33420 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33421 /*or_comma=*/false,
33422 /*consume_paren=*/true);
33423 return list;
33426 /* OpenACC:
33427 async [( int-expr )] */
33429 static tree
33430 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33432 tree c, t;
33433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33435 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33437 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33439 matching_parens parens;
33440 parens.consume_open (parser);
33442 t = cp_parser_expression (parser);
33443 if (t == error_mark_node
33444 || !parens.require_close (parser))
33445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33446 /*or_comma=*/false,
33447 /*consume_paren=*/true);
33450 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33452 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33453 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33454 OMP_CLAUSE_CHAIN (c) = list;
33455 list = c;
33457 return list;
33460 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33461 is a bitmask in MASK. Return the list of clauses found. */
33463 static tree
33464 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33465 const char *where, cp_token *pragma_tok,
33466 bool finish_p = true)
33468 tree clauses = NULL;
33469 bool first = true;
33471 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33473 location_t here;
33474 pragma_omp_clause c_kind;
33475 omp_clause_code code;
33476 const char *c_name;
33477 tree prev = clauses;
33479 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33480 cp_lexer_consume_token (parser->lexer);
33482 here = cp_lexer_peek_token (parser->lexer)->location;
33483 c_kind = cp_parser_omp_clause_name (parser);
33485 switch (c_kind)
33487 case PRAGMA_OACC_CLAUSE_ASYNC:
33488 clauses = cp_parser_oacc_clause_async (parser, clauses);
33489 c_name = "async";
33490 break;
33491 case PRAGMA_OACC_CLAUSE_AUTO:
33492 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33493 clauses, here);
33494 c_name = "auto";
33495 break;
33496 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33497 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33498 c_name = "collapse";
33499 break;
33500 case PRAGMA_OACC_CLAUSE_COPY:
33501 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33502 c_name = "copy";
33503 break;
33504 case PRAGMA_OACC_CLAUSE_COPYIN:
33505 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33506 c_name = "copyin";
33507 break;
33508 case PRAGMA_OACC_CLAUSE_COPYOUT:
33509 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33510 c_name = "copyout";
33511 break;
33512 case PRAGMA_OACC_CLAUSE_CREATE:
33513 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33514 c_name = "create";
33515 break;
33516 case PRAGMA_OACC_CLAUSE_DELETE:
33517 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33518 c_name = "delete";
33519 break;
33520 case PRAGMA_OMP_CLAUSE_DEFAULT:
33521 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33522 c_name = "default";
33523 break;
33524 case PRAGMA_OACC_CLAUSE_DEVICE:
33525 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33526 c_name = "device";
33527 break;
33528 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33529 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33530 c_name = "deviceptr";
33531 break;
33532 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33533 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33534 c_name = "device_resident";
33535 break;
33536 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33537 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33538 clauses);
33539 c_name = "firstprivate";
33540 break;
33541 case PRAGMA_OACC_CLAUSE_GANG:
33542 c_name = "gang";
33543 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33544 c_name, clauses);
33545 break;
33546 case PRAGMA_OACC_CLAUSE_HOST:
33547 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33548 c_name = "host";
33549 break;
33550 case PRAGMA_OACC_CLAUSE_IF:
33551 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33552 c_name = "if";
33553 break;
33554 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33555 clauses = cp_parser_oacc_simple_clause (parser,
33556 OMP_CLAUSE_INDEPENDENT,
33557 clauses, here);
33558 c_name = "independent";
33559 break;
33560 case PRAGMA_OACC_CLAUSE_LINK:
33561 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33562 c_name = "link";
33563 break;
33564 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33565 code = OMP_CLAUSE_NUM_GANGS;
33566 c_name = "num_gangs";
33567 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33568 clauses);
33569 break;
33570 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33571 c_name = "num_workers";
33572 code = OMP_CLAUSE_NUM_WORKERS;
33573 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33574 clauses);
33575 break;
33576 case PRAGMA_OACC_CLAUSE_PRESENT:
33577 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33578 c_name = "present";
33579 break;
33580 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33581 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33582 c_name = "present_or_copy";
33583 break;
33584 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33585 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33586 c_name = "present_or_copyin";
33587 break;
33588 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33589 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33590 c_name = "present_or_copyout";
33591 break;
33592 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33593 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33594 c_name = "present_or_create";
33595 break;
33596 case PRAGMA_OACC_CLAUSE_PRIVATE:
33597 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33598 clauses);
33599 c_name = "private";
33600 break;
33601 case PRAGMA_OACC_CLAUSE_REDUCTION:
33602 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33603 c_name = "reduction";
33604 break;
33605 case PRAGMA_OACC_CLAUSE_SELF:
33606 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33607 c_name = "self";
33608 break;
33609 case PRAGMA_OACC_CLAUSE_SEQ:
33610 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33611 clauses, here);
33612 c_name = "seq";
33613 break;
33614 case PRAGMA_OACC_CLAUSE_TILE:
33615 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33616 c_name = "tile";
33617 break;
33618 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33619 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33620 clauses);
33621 c_name = "use_device";
33622 break;
33623 case PRAGMA_OACC_CLAUSE_VECTOR:
33624 c_name = "vector";
33625 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33626 c_name, clauses);
33627 break;
33628 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33629 c_name = "vector_length";
33630 code = OMP_CLAUSE_VECTOR_LENGTH;
33631 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33632 clauses);
33633 break;
33634 case PRAGMA_OACC_CLAUSE_WAIT:
33635 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33636 c_name = "wait";
33637 break;
33638 case PRAGMA_OACC_CLAUSE_WORKER:
33639 c_name = "worker";
33640 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33641 c_name, clauses);
33642 break;
33643 default:
33644 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33645 goto saw_error;
33648 first = false;
33650 if (((mask >> c_kind) & 1) == 0)
33652 /* Remove the invalid clause(s) from the list to avoid
33653 confusing the rest of the compiler. */
33654 clauses = prev;
33655 error_at (here, "%qs is not valid for %qs", c_name, where);
33659 saw_error:
33660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33662 if (finish_p)
33663 return finish_omp_clauses (clauses, C_ORT_ACC);
33665 return clauses;
33668 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33669 is a bitmask in MASK. Return the list of clauses found; the result
33670 of clause default goes in *pdefault. */
33672 static tree
33673 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33674 const char *where, cp_token *pragma_tok,
33675 bool finish_p = true)
33677 tree clauses = NULL;
33678 bool first = true;
33679 cp_token *token = NULL;
33681 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33683 pragma_omp_clause c_kind;
33684 const char *c_name;
33685 tree prev = clauses;
33687 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33688 cp_lexer_consume_token (parser->lexer);
33690 token = cp_lexer_peek_token (parser->lexer);
33691 c_kind = cp_parser_omp_clause_name (parser);
33693 switch (c_kind)
33695 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33696 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33697 token->location);
33698 c_name = "collapse";
33699 break;
33700 case PRAGMA_OMP_CLAUSE_COPYIN:
33701 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33702 c_name = "copyin";
33703 break;
33704 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33705 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33706 clauses);
33707 c_name = "copyprivate";
33708 break;
33709 case PRAGMA_OMP_CLAUSE_DEFAULT:
33710 clauses = cp_parser_omp_clause_default (parser, clauses,
33711 token->location, false);
33712 c_name = "default";
33713 break;
33714 case PRAGMA_OMP_CLAUSE_FINAL:
33715 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33716 c_name = "final";
33717 break;
33718 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33720 clauses);
33721 c_name = "firstprivate";
33722 break;
33723 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33724 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33725 token->location);
33726 c_name = "grainsize";
33727 break;
33728 case PRAGMA_OMP_CLAUSE_HINT:
33729 clauses = cp_parser_omp_clause_hint (parser, clauses,
33730 token->location);
33731 c_name = "hint";
33732 break;
33733 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33734 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33735 token->location);
33736 c_name = "defaultmap";
33737 break;
33738 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33739 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33740 clauses);
33741 c_name = "use_device_ptr";
33742 break;
33743 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33745 clauses);
33746 c_name = "is_device_ptr";
33747 break;
33748 case PRAGMA_OMP_CLAUSE_IF:
33749 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33750 true);
33751 c_name = "if";
33752 break;
33753 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33754 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33755 clauses);
33756 c_name = "lastprivate";
33757 break;
33758 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33759 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33760 token->location);
33761 c_name = "mergeable";
33762 break;
33763 case PRAGMA_OMP_CLAUSE_NOWAIT:
33764 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33765 c_name = "nowait";
33766 break;
33767 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33768 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33769 token->location);
33770 c_name = "num_tasks";
33771 break;
33772 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33773 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33774 token->location);
33775 c_name = "num_threads";
33776 break;
33777 case PRAGMA_OMP_CLAUSE_ORDERED:
33778 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33779 token->location);
33780 c_name = "ordered";
33781 break;
33782 case PRAGMA_OMP_CLAUSE_PRIORITY:
33783 clauses = cp_parser_omp_clause_priority (parser, clauses,
33784 token->location);
33785 c_name = "priority";
33786 break;
33787 case PRAGMA_OMP_CLAUSE_PRIVATE:
33788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33789 clauses);
33790 c_name = "private";
33791 break;
33792 case PRAGMA_OMP_CLAUSE_REDUCTION:
33793 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33794 c_name = "reduction";
33795 break;
33796 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33797 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33798 token->location);
33799 c_name = "schedule";
33800 break;
33801 case PRAGMA_OMP_CLAUSE_SHARED:
33802 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33803 clauses);
33804 c_name = "shared";
33805 break;
33806 case PRAGMA_OMP_CLAUSE_UNTIED:
33807 clauses = cp_parser_omp_clause_untied (parser, clauses,
33808 token->location);
33809 c_name = "untied";
33810 break;
33811 case PRAGMA_OMP_CLAUSE_INBRANCH:
33812 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33813 clauses, token->location);
33814 c_name = "inbranch";
33815 break;
33816 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33817 clauses = cp_parser_omp_clause_branch (parser,
33818 OMP_CLAUSE_NOTINBRANCH,
33819 clauses, token->location);
33820 c_name = "notinbranch";
33821 break;
33822 case PRAGMA_OMP_CLAUSE_PARALLEL:
33823 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33824 clauses, token->location);
33825 c_name = "parallel";
33826 if (!first)
33828 clause_not_first:
33829 error_at (token->location, "%qs must be the first clause of %qs",
33830 c_name, where);
33831 clauses = prev;
33833 break;
33834 case PRAGMA_OMP_CLAUSE_FOR:
33835 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33836 clauses, token->location);
33837 c_name = "for";
33838 if (!first)
33839 goto clause_not_first;
33840 break;
33841 case PRAGMA_OMP_CLAUSE_SECTIONS:
33842 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33843 clauses, token->location);
33844 c_name = "sections";
33845 if (!first)
33846 goto clause_not_first;
33847 break;
33848 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33849 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33850 clauses, token->location);
33851 c_name = "taskgroup";
33852 if (!first)
33853 goto clause_not_first;
33854 break;
33855 case PRAGMA_OMP_CLAUSE_LINK:
33856 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33857 c_name = "to";
33858 break;
33859 case PRAGMA_OMP_CLAUSE_TO:
33860 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33862 clauses);
33863 else
33864 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33865 c_name = "to";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_FROM:
33868 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33869 c_name = "from";
33870 break;
33871 case PRAGMA_OMP_CLAUSE_UNIFORM:
33872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33873 clauses);
33874 c_name = "uniform";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33877 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33878 token->location);
33879 c_name = "num_teams";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33882 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33883 token->location);
33884 c_name = "thread_limit";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_ALIGNED:
33887 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33888 c_name = "aligned";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_LINEAR:
33892 bool declare_simd = false;
33893 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33894 declare_simd = true;
33895 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33897 c_name = "linear";
33898 break;
33899 case PRAGMA_OMP_CLAUSE_DEPEND:
33900 clauses = cp_parser_omp_clause_depend (parser, clauses,
33901 token->location);
33902 c_name = "depend";
33903 break;
33904 case PRAGMA_OMP_CLAUSE_MAP:
33905 clauses = cp_parser_omp_clause_map (parser, clauses);
33906 c_name = "map";
33907 break;
33908 case PRAGMA_OMP_CLAUSE_DEVICE:
33909 clauses = cp_parser_omp_clause_device (parser, clauses,
33910 token->location);
33911 c_name = "device";
33912 break;
33913 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33914 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33915 token->location);
33916 c_name = "dist_schedule";
33917 break;
33918 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33919 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33920 token->location);
33921 c_name = "proc_bind";
33922 break;
33923 case PRAGMA_OMP_CLAUSE_SAFELEN:
33924 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33925 token->location);
33926 c_name = "safelen";
33927 break;
33928 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33929 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33930 token->location);
33931 c_name = "simdlen";
33932 break;
33933 case PRAGMA_OMP_CLAUSE_NOGROUP:
33934 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33935 token->location);
33936 c_name = "nogroup";
33937 break;
33938 case PRAGMA_OMP_CLAUSE_THREADS:
33939 clauses
33940 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33941 clauses, token->location);
33942 c_name = "threads";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_SIMD:
33945 clauses
33946 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33947 clauses, token->location);
33948 c_name = "simd";
33949 break;
33950 default:
33951 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33952 goto saw_error;
33955 first = false;
33957 if (((mask >> c_kind) & 1) == 0)
33959 /* Remove the invalid clause(s) from the list to avoid
33960 confusing the rest of the compiler. */
33961 clauses = prev;
33962 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33965 saw_error:
33966 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33967 if (finish_p)
33969 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33970 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33971 else
33972 return finish_omp_clauses (clauses, C_ORT_OMP);
33974 return clauses;
33977 /* OpenMP 2.5:
33978 structured-block:
33979 statement
33981 In practice, we're also interested in adding the statement to an
33982 outer node. So it is convenient if we work around the fact that
33983 cp_parser_statement calls add_stmt. */
33985 static unsigned
33986 cp_parser_begin_omp_structured_block (cp_parser *parser)
33988 unsigned save = parser->in_statement;
33990 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33991 This preserves the "not within loop or switch" style error messages
33992 for nonsense cases like
33993 void foo() {
33994 #pragma omp single
33995 break;
33998 if (parser->in_statement)
33999 parser->in_statement = IN_OMP_BLOCK;
34001 return save;
34004 static void
34005 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34007 parser->in_statement = save;
34010 static tree
34011 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34013 tree stmt = begin_omp_structured_block ();
34014 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34016 cp_parser_statement (parser, NULL_TREE, false, if_p);
34018 cp_parser_end_omp_structured_block (parser, save);
34019 return finish_omp_structured_block (stmt);
34022 /* OpenMP 2.5:
34023 # pragma omp atomic new-line
34024 expression-stmt
34026 expression-stmt:
34027 x binop= expr | x++ | ++x | x-- | --x
34028 binop:
34029 +, *, -, /, &, ^, |, <<, >>
34031 where x is an lvalue expression with scalar type.
34033 OpenMP 3.1:
34034 # pragma omp atomic new-line
34035 update-stmt
34037 # pragma omp atomic read new-line
34038 read-stmt
34040 # pragma omp atomic write new-line
34041 write-stmt
34043 # pragma omp atomic update new-line
34044 update-stmt
34046 # pragma omp atomic capture new-line
34047 capture-stmt
34049 # pragma omp atomic capture new-line
34050 capture-block
34052 read-stmt:
34053 v = x
34054 write-stmt:
34055 x = expr
34056 update-stmt:
34057 expression-stmt | x = x binop expr
34058 capture-stmt:
34059 v = expression-stmt
34060 capture-block:
34061 { v = x; update-stmt; } | { update-stmt; v = x; }
34063 OpenMP 4.0:
34064 update-stmt:
34065 expression-stmt | x = x binop expr | x = expr binop x
34066 capture-stmt:
34067 v = update-stmt
34068 capture-block:
34069 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34071 where x and v are lvalue expressions with scalar type. */
34073 static void
34074 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34076 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34077 tree rhs1 = NULL_TREE, orig_lhs;
34078 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34079 bool structured_block = false;
34080 bool seq_cst = false;
34082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34084 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34085 const char *p = IDENTIFIER_POINTER (id);
34087 if (!strcmp (p, "seq_cst"))
34089 seq_cst = true;
34090 cp_lexer_consume_token (parser->lexer);
34091 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34092 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34093 cp_lexer_consume_token (parser->lexer);
34096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34098 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34099 const char *p = IDENTIFIER_POINTER (id);
34101 if (!strcmp (p, "read"))
34102 code = OMP_ATOMIC_READ;
34103 else if (!strcmp (p, "write"))
34104 code = NOP_EXPR;
34105 else if (!strcmp (p, "update"))
34106 code = OMP_ATOMIC;
34107 else if (!strcmp (p, "capture"))
34108 code = OMP_ATOMIC_CAPTURE_NEW;
34109 else
34110 p = NULL;
34111 if (p)
34112 cp_lexer_consume_token (parser->lexer);
34114 if (!seq_cst)
34116 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34117 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34118 cp_lexer_consume_token (parser->lexer);
34120 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34123 const char *p = IDENTIFIER_POINTER (id);
34125 if (!strcmp (p, "seq_cst"))
34127 seq_cst = true;
34128 cp_lexer_consume_token (parser->lexer);
34132 cp_parser_require_pragma_eol (parser, pragma_tok);
34134 switch (code)
34136 case OMP_ATOMIC_READ:
34137 case NOP_EXPR: /* atomic write */
34138 v = cp_parser_unary_expression (parser);
34139 if (v == error_mark_node)
34140 goto saw_error;
34141 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34142 goto saw_error;
34143 if (code == NOP_EXPR)
34144 lhs = cp_parser_expression (parser);
34145 else
34146 lhs = cp_parser_unary_expression (parser);
34147 if (lhs == error_mark_node)
34148 goto saw_error;
34149 if (code == NOP_EXPR)
34151 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34152 opcode. */
34153 code = OMP_ATOMIC;
34154 rhs = lhs;
34155 lhs = v;
34156 v = NULL_TREE;
34158 goto done;
34159 case OMP_ATOMIC_CAPTURE_NEW:
34160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34162 cp_lexer_consume_token (parser->lexer);
34163 structured_block = true;
34165 else
34167 v = cp_parser_unary_expression (parser);
34168 if (v == error_mark_node)
34169 goto saw_error;
34170 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34171 goto saw_error;
34173 default:
34174 break;
34177 restart:
34178 lhs = cp_parser_unary_expression (parser);
34179 orig_lhs = lhs;
34180 switch (TREE_CODE (lhs))
34182 case ERROR_MARK:
34183 goto saw_error;
34185 case POSTINCREMENT_EXPR:
34186 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34187 code = OMP_ATOMIC_CAPTURE_OLD;
34188 /* FALLTHROUGH */
34189 case PREINCREMENT_EXPR:
34190 lhs = TREE_OPERAND (lhs, 0);
34191 opcode = PLUS_EXPR;
34192 rhs = integer_one_node;
34193 break;
34195 case POSTDECREMENT_EXPR:
34196 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34197 code = OMP_ATOMIC_CAPTURE_OLD;
34198 /* FALLTHROUGH */
34199 case PREDECREMENT_EXPR:
34200 lhs = TREE_OPERAND (lhs, 0);
34201 opcode = MINUS_EXPR;
34202 rhs = integer_one_node;
34203 break;
34205 case COMPOUND_EXPR:
34206 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34207 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34208 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34209 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34210 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34211 (TREE_OPERAND (lhs, 1), 0), 0)))
34212 == BOOLEAN_TYPE)
34213 /* Undo effects of boolean_increment for post {in,de}crement. */
34214 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34215 /* FALLTHRU */
34216 case MODIFY_EXPR:
34217 if (TREE_CODE (lhs) == MODIFY_EXPR
34218 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34220 /* Undo effects of boolean_increment. */
34221 if (integer_onep (TREE_OPERAND (lhs, 1)))
34223 /* This is pre or post increment. */
34224 rhs = TREE_OPERAND (lhs, 1);
34225 lhs = TREE_OPERAND (lhs, 0);
34226 opcode = NOP_EXPR;
34227 if (code == OMP_ATOMIC_CAPTURE_NEW
34228 && !structured_block
34229 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34230 code = OMP_ATOMIC_CAPTURE_OLD;
34231 break;
34234 /* FALLTHRU */
34235 default:
34236 switch (cp_lexer_peek_token (parser->lexer)->type)
34238 case CPP_MULT_EQ:
34239 opcode = MULT_EXPR;
34240 break;
34241 case CPP_DIV_EQ:
34242 opcode = TRUNC_DIV_EXPR;
34243 break;
34244 case CPP_PLUS_EQ:
34245 opcode = PLUS_EXPR;
34246 break;
34247 case CPP_MINUS_EQ:
34248 opcode = MINUS_EXPR;
34249 break;
34250 case CPP_LSHIFT_EQ:
34251 opcode = LSHIFT_EXPR;
34252 break;
34253 case CPP_RSHIFT_EQ:
34254 opcode = RSHIFT_EXPR;
34255 break;
34256 case CPP_AND_EQ:
34257 opcode = BIT_AND_EXPR;
34258 break;
34259 case CPP_OR_EQ:
34260 opcode = BIT_IOR_EXPR;
34261 break;
34262 case CPP_XOR_EQ:
34263 opcode = BIT_XOR_EXPR;
34264 break;
34265 case CPP_EQ:
34266 enum cp_parser_prec oprec;
34267 cp_token *token;
34268 cp_lexer_consume_token (parser->lexer);
34269 cp_parser_parse_tentatively (parser);
34270 rhs1 = cp_parser_simple_cast_expression (parser);
34271 if (rhs1 == error_mark_node)
34273 cp_parser_abort_tentative_parse (parser);
34274 cp_parser_simple_cast_expression (parser);
34275 goto saw_error;
34277 token = cp_lexer_peek_token (parser->lexer);
34278 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34280 cp_parser_abort_tentative_parse (parser);
34281 cp_parser_parse_tentatively (parser);
34282 rhs = cp_parser_binary_expression (parser, false, true,
34283 PREC_NOT_OPERATOR, NULL);
34284 if (rhs == error_mark_node)
34286 cp_parser_abort_tentative_parse (parser);
34287 cp_parser_binary_expression (parser, false, true,
34288 PREC_NOT_OPERATOR, NULL);
34289 goto saw_error;
34291 switch (TREE_CODE (rhs))
34293 case MULT_EXPR:
34294 case TRUNC_DIV_EXPR:
34295 case RDIV_EXPR:
34296 case PLUS_EXPR:
34297 case MINUS_EXPR:
34298 case LSHIFT_EXPR:
34299 case RSHIFT_EXPR:
34300 case BIT_AND_EXPR:
34301 case BIT_IOR_EXPR:
34302 case BIT_XOR_EXPR:
34303 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34305 if (cp_parser_parse_definitely (parser))
34307 opcode = TREE_CODE (rhs);
34308 rhs1 = TREE_OPERAND (rhs, 0);
34309 rhs = TREE_OPERAND (rhs, 1);
34310 goto stmt_done;
34312 else
34313 goto saw_error;
34315 break;
34316 default:
34317 break;
34319 cp_parser_abort_tentative_parse (parser);
34320 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34322 rhs = cp_parser_expression (parser);
34323 if (rhs == error_mark_node)
34324 goto saw_error;
34325 opcode = NOP_EXPR;
34326 rhs1 = NULL_TREE;
34327 goto stmt_done;
34329 cp_parser_error (parser,
34330 "invalid form of %<#pragma omp atomic%>");
34331 goto saw_error;
34333 if (!cp_parser_parse_definitely (parser))
34334 goto saw_error;
34335 switch (token->type)
34337 case CPP_SEMICOLON:
34338 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34340 code = OMP_ATOMIC_CAPTURE_OLD;
34341 v = lhs;
34342 lhs = NULL_TREE;
34343 lhs1 = rhs1;
34344 rhs1 = NULL_TREE;
34345 cp_lexer_consume_token (parser->lexer);
34346 goto restart;
34348 else if (structured_block)
34350 opcode = NOP_EXPR;
34351 rhs = rhs1;
34352 rhs1 = NULL_TREE;
34353 goto stmt_done;
34355 cp_parser_error (parser,
34356 "invalid form of %<#pragma omp atomic%>");
34357 goto saw_error;
34358 case CPP_MULT:
34359 opcode = MULT_EXPR;
34360 break;
34361 case CPP_DIV:
34362 opcode = TRUNC_DIV_EXPR;
34363 break;
34364 case CPP_PLUS:
34365 opcode = PLUS_EXPR;
34366 break;
34367 case CPP_MINUS:
34368 opcode = MINUS_EXPR;
34369 break;
34370 case CPP_LSHIFT:
34371 opcode = LSHIFT_EXPR;
34372 break;
34373 case CPP_RSHIFT:
34374 opcode = RSHIFT_EXPR;
34375 break;
34376 case CPP_AND:
34377 opcode = BIT_AND_EXPR;
34378 break;
34379 case CPP_OR:
34380 opcode = BIT_IOR_EXPR;
34381 break;
34382 case CPP_XOR:
34383 opcode = BIT_XOR_EXPR;
34384 break;
34385 default:
34386 cp_parser_error (parser,
34387 "invalid operator for %<#pragma omp atomic%>");
34388 goto saw_error;
34390 oprec = TOKEN_PRECEDENCE (token);
34391 gcc_assert (oprec != PREC_NOT_OPERATOR);
34392 if (commutative_tree_code (opcode))
34393 oprec = (enum cp_parser_prec) (oprec - 1);
34394 cp_lexer_consume_token (parser->lexer);
34395 rhs = cp_parser_binary_expression (parser, false, false,
34396 oprec, NULL);
34397 if (rhs == error_mark_node)
34398 goto saw_error;
34399 goto stmt_done;
34400 /* FALLTHROUGH */
34401 default:
34402 cp_parser_error (parser,
34403 "invalid operator for %<#pragma omp atomic%>");
34404 goto saw_error;
34406 cp_lexer_consume_token (parser->lexer);
34408 rhs = cp_parser_expression (parser);
34409 if (rhs == error_mark_node)
34410 goto saw_error;
34411 break;
34413 stmt_done:
34414 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34416 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34417 goto saw_error;
34418 v = cp_parser_unary_expression (parser);
34419 if (v == error_mark_node)
34420 goto saw_error;
34421 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34422 goto saw_error;
34423 lhs1 = cp_parser_unary_expression (parser);
34424 if (lhs1 == error_mark_node)
34425 goto saw_error;
34427 if (structured_block)
34429 cp_parser_consume_semicolon_at_end_of_statement (parser);
34430 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34432 done:
34433 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34434 if (!structured_block)
34435 cp_parser_consume_semicolon_at_end_of_statement (parser);
34436 return;
34438 saw_error:
34439 cp_parser_skip_to_end_of_block_or_statement (parser);
34440 if (structured_block)
34442 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34443 cp_lexer_consume_token (parser->lexer);
34444 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34446 cp_parser_skip_to_end_of_block_or_statement (parser);
34447 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34448 cp_lexer_consume_token (parser->lexer);
34454 /* OpenMP 2.5:
34455 # pragma omp barrier new-line */
34457 static void
34458 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34460 cp_parser_require_pragma_eol (parser, pragma_tok);
34461 finish_omp_barrier ();
34464 /* OpenMP 2.5:
34465 # pragma omp critical [(name)] new-line
34466 structured-block
34468 OpenMP 4.5:
34469 # pragma omp critical [(name) [hint(expression)]] new-line
34470 structured-block */
34472 #define OMP_CRITICAL_CLAUSE_MASK \
34473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34475 static tree
34476 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34478 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34482 matching_parens parens;
34483 parens.consume_open (parser);
34485 name = cp_parser_identifier (parser);
34487 if (name == error_mark_node
34488 || !parens.require_close (parser))
34489 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34490 /*or_comma=*/false,
34491 /*consume_paren=*/true);
34492 if (name == error_mark_node)
34493 name = NULL;
34495 clauses = cp_parser_omp_all_clauses (parser,
34496 OMP_CRITICAL_CLAUSE_MASK,
34497 "#pragma omp critical", pragma_tok);
34499 else
34500 cp_parser_require_pragma_eol (parser, pragma_tok);
34502 stmt = cp_parser_omp_structured_block (parser, if_p);
34503 return c_finish_omp_critical (input_location, stmt, name, clauses);
34506 /* OpenMP 2.5:
34507 # pragma omp flush flush-vars[opt] new-line
34509 flush-vars:
34510 ( variable-list ) */
34512 static void
34513 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34516 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34517 cp_parser_require_pragma_eol (parser, pragma_tok);
34519 finish_omp_flush ();
34522 /* Helper function, to parse omp for increment expression. */
34524 static tree
34525 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34527 tree cond = cp_parser_binary_expression (parser, false, true,
34528 PREC_NOT_OPERATOR, NULL);
34529 if (cond == error_mark_node
34530 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34532 cp_parser_skip_to_end_of_statement (parser);
34533 return error_mark_node;
34536 switch (TREE_CODE (cond))
34538 case GT_EXPR:
34539 case GE_EXPR:
34540 case LT_EXPR:
34541 case LE_EXPR:
34542 break;
34543 case NE_EXPR:
34544 /* Fall through: OpenMP disallows NE_EXPR. */
34545 gcc_fallthrough ();
34546 default:
34547 return error_mark_node;
34550 /* If decl is an iterator, preserve LHS and RHS of the relational
34551 expr until finish_omp_for. */
34552 if (decl
34553 && (type_dependent_expression_p (decl)
34554 || CLASS_TYPE_P (TREE_TYPE (decl))))
34555 return cond;
34557 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34558 TREE_CODE (cond),
34559 TREE_OPERAND (cond, 0), ERROR_MARK,
34560 TREE_OPERAND (cond, 1), ERROR_MARK,
34561 /*overload=*/NULL, tf_warning_or_error);
34564 /* Helper function, to parse omp for increment expression. */
34566 static tree
34567 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34569 cp_token *token = cp_lexer_peek_token (parser->lexer);
34570 enum tree_code op;
34571 tree lhs, rhs;
34572 cp_id_kind idk;
34573 bool decl_first;
34575 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34577 op = (token->type == CPP_PLUS_PLUS
34578 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34579 cp_lexer_consume_token (parser->lexer);
34580 lhs = cp_parser_simple_cast_expression (parser);
34581 if (lhs != decl
34582 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34583 return error_mark_node;
34584 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34587 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34588 if (lhs != decl
34589 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34590 return error_mark_node;
34592 token = cp_lexer_peek_token (parser->lexer);
34593 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34595 op = (token->type == CPP_PLUS_PLUS
34596 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34597 cp_lexer_consume_token (parser->lexer);
34598 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34601 op = cp_parser_assignment_operator_opt (parser);
34602 if (op == ERROR_MARK)
34603 return error_mark_node;
34605 if (op != NOP_EXPR)
34607 rhs = cp_parser_assignment_expression (parser);
34608 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34609 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34612 lhs = cp_parser_binary_expression (parser, false, false,
34613 PREC_ADDITIVE_EXPRESSION, NULL);
34614 token = cp_lexer_peek_token (parser->lexer);
34615 decl_first = (lhs == decl
34616 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34617 if (decl_first)
34618 lhs = NULL_TREE;
34619 if (token->type != CPP_PLUS
34620 && token->type != CPP_MINUS)
34621 return error_mark_node;
34625 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34626 cp_lexer_consume_token (parser->lexer);
34627 rhs = cp_parser_binary_expression (parser, false, false,
34628 PREC_ADDITIVE_EXPRESSION, NULL);
34629 token = cp_lexer_peek_token (parser->lexer);
34630 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34632 if (lhs == NULL_TREE)
34634 if (op == PLUS_EXPR)
34635 lhs = rhs;
34636 else
34637 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34638 tf_warning_or_error);
34640 else
34641 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34642 ERROR_MARK, NULL, tf_warning_or_error);
34645 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34647 if (!decl_first)
34649 if ((rhs != decl
34650 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34651 || op == MINUS_EXPR)
34652 return error_mark_node;
34653 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34655 else
34656 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34658 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34661 /* Parse the initialization statement of an OpenMP for loop.
34663 Return true if the resulting construct should have an
34664 OMP_CLAUSE_PRIVATE added to it. */
34666 static tree
34667 cp_parser_omp_for_loop_init (cp_parser *parser,
34668 tree &this_pre_body,
34669 vec<tree, va_gc> *for_block,
34670 tree &init,
34671 tree &orig_init,
34672 tree &decl,
34673 tree &real_decl)
34675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34676 return NULL_TREE;
34678 tree add_private_clause = NULL_TREE;
34680 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34682 init-expr:
34683 var = lb
34684 integer-type var = lb
34685 random-access-iterator-type var = lb
34686 pointer-type var = lb
34688 cp_decl_specifier_seq type_specifiers;
34690 /* First, try to parse as an initialized declaration. See
34691 cp_parser_condition, from whence the bulk of this is copied. */
34693 cp_parser_parse_tentatively (parser);
34694 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34695 /*is_trailing_return=*/false,
34696 &type_specifiers);
34697 if (cp_parser_parse_definitely (parser))
34699 /* If parsing a type specifier seq succeeded, then this
34700 MUST be a initialized declaration. */
34701 tree asm_specification, attributes;
34702 cp_declarator *declarator;
34704 declarator = cp_parser_declarator (parser,
34705 CP_PARSER_DECLARATOR_NAMED,
34706 /*ctor_dtor_or_conv_p=*/NULL,
34707 /*parenthesized_p=*/NULL,
34708 /*member_p=*/false,
34709 /*friend_p=*/false);
34710 attributes = cp_parser_attributes_opt (parser);
34711 asm_specification = cp_parser_asm_specification_opt (parser);
34713 if (declarator == cp_error_declarator)
34714 cp_parser_skip_to_end_of_statement (parser);
34716 else
34718 tree pushed_scope, auto_node;
34720 decl = start_decl (declarator, &type_specifiers,
34721 SD_INITIALIZED, attributes,
34722 /*prefix_attributes=*/NULL_TREE,
34723 &pushed_scope);
34725 auto_node = type_uses_auto (TREE_TYPE (decl));
34726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34728 if (cp_lexer_next_token_is (parser->lexer,
34729 CPP_OPEN_PAREN))
34730 error ("parenthesized initialization is not allowed in "
34731 "OpenMP %<for%> loop");
34732 else
34733 /* Trigger an error. */
34734 cp_parser_require (parser, CPP_EQ, RT_EQ);
34736 init = error_mark_node;
34737 cp_parser_skip_to_end_of_statement (parser);
34739 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34740 || type_dependent_expression_p (decl)
34741 || auto_node)
34743 bool is_direct_init, is_non_constant_init;
34745 init = cp_parser_initializer (parser,
34746 &is_direct_init,
34747 &is_non_constant_init);
34749 if (auto_node)
34751 TREE_TYPE (decl)
34752 = do_auto_deduction (TREE_TYPE (decl), init,
34753 auto_node);
34755 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34756 && !type_dependent_expression_p (decl))
34757 goto non_class;
34760 cp_finish_decl (decl, init, !is_non_constant_init,
34761 asm_specification,
34762 LOOKUP_ONLYCONVERTING);
34763 orig_init = init;
34764 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34766 vec_safe_push (for_block, this_pre_body);
34767 init = NULL_TREE;
34769 else
34771 init = pop_stmt_list (this_pre_body);
34772 if (init && TREE_CODE (init) == STATEMENT_LIST)
34774 tree_stmt_iterator i = tsi_start (init);
34775 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34776 while (!tsi_end_p (i))
34778 tree t = tsi_stmt (i);
34779 if (TREE_CODE (t) == DECL_EXPR
34780 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34782 tsi_delink (&i);
34783 vec_safe_push (for_block, t);
34784 continue;
34786 break;
34788 if (tsi_one_before_end_p (i))
34790 tree t = tsi_stmt (i);
34791 tsi_delink (&i);
34792 free_stmt_list (init);
34793 init = t;
34797 this_pre_body = NULL_TREE;
34799 else
34801 /* Consume '='. */
34802 cp_lexer_consume_token (parser->lexer);
34803 init = cp_parser_assignment_expression (parser);
34805 non_class:
34806 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34807 init = error_mark_node;
34808 else
34809 cp_finish_decl (decl, NULL_TREE,
34810 /*init_const_expr_p=*/false,
34811 asm_specification,
34812 LOOKUP_ONLYCONVERTING);
34815 if (pushed_scope)
34816 pop_scope (pushed_scope);
34819 else
34821 cp_id_kind idk;
34822 /* If parsing a type specifier sequence failed, then
34823 this MUST be a simple expression. */
34824 cp_parser_parse_tentatively (parser);
34825 decl = cp_parser_primary_expression (parser, false, false,
34826 false, &idk);
34827 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34828 if (!cp_parser_error_occurred (parser)
34829 && decl
34830 && (TREE_CODE (decl) == COMPONENT_REF
34831 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34833 cp_parser_abort_tentative_parse (parser);
34834 cp_parser_parse_tentatively (parser);
34835 cp_token *token = cp_lexer_peek_token (parser->lexer);
34836 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34837 /*check_dependency_p=*/true,
34838 /*template_p=*/NULL,
34839 /*declarator_p=*/false,
34840 /*optional_p=*/false);
34841 if (name != error_mark_node
34842 && last_tok == cp_lexer_peek_token (parser->lexer))
34844 decl = cp_parser_lookup_name_simple (parser, name,
34845 token->location);
34846 if (TREE_CODE (decl) == FIELD_DECL)
34847 add_private_clause = omp_privatize_field (decl, false);
34849 cp_parser_abort_tentative_parse (parser);
34850 cp_parser_parse_tentatively (parser);
34851 decl = cp_parser_primary_expression (parser, false, false,
34852 false, &idk);
34854 if (!cp_parser_error_occurred (parser)
34855 && decl
34856 && DECL_P (decl)
34857 && CLASS_TYPE_P (TREE_TYPE (decl)))
34859 tree rhs;
34861 cp_parser_parse_definitely (parser);
34862 cp_parser_require (parser, CPP_EQ, RT_EQ);
34863 rhs = cp_parser_assignment_expression (parser);
34864 orig_init = rhs;
34865 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34866 decl, NOP_EXPR,
34867 rhs,
34868 tf_warning_or_error));
34869 if (!add_private_clause)
34870 add_private_clause = decl;
34872 else
34874 decl = NULL;
34875 cp_parser_abort_tentative_parse (parser);
34876 init = cp_parser_expression (parser);
34877 if (init)
34879 if (TREE_CODE (init) == MODIFY_EXPR
34880 || TREE_CODE (init) == MODOP_EXPR)
34881 real_decl = TREE_OPERAND (init, 0);
34885 return add_private_clause;
34888 /* Parse the restricted form of the for statement allowed by OpenMP. */
34890 static tree
34891 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34892 tree *cclauses, bool *if_p)
34894 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34895 tree real_decl, initv, condv, incrv, declv;
34896 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34897 location_t loc_first;
34898 bool collapse_err = false;
34899 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34900 vec<tree, va_gc> *for_block = make_tree_vector ();
34901 auto_vec<tree, 4> orig_inits;
34902 bool tiling = false;
34904 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34905 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34906 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34907 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34909 tiling = true;
34910 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34912 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34913 && OMP_CLAUSE_ORDERED_EXPR (cl))
34915 ordered_cl = cl;
34916 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34919 if (ordered && ordered < collapse)
34921 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34922 "%<ordered%> clause parameter is less than %<collapse%>");
34923 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34924 = build_int_cst (NULL_TREE, collapse);
34925 ordered = collapse;
34927 if (ordered)
34929 for (tree *pc = &clauses; *pc; )
34930 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34932 error_at (OMP_CLAUSE_LOCATION (*pc),
34933 "%<linear%> clause may not be specified together "
34934 "with %<ordered%> clause with a parameter");
34935 *pc = OMP_CLAUSE_CHAIN (*pc);
34937 else
34938 pc = &OMP_CLAUSE_CHAIN (*pc);
34941 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34942 count = ordered ? ordered : collapse;
34944 declv = make_tree_vec (count);
34945 initv = make_tree_vec (count);
34946 condv = make_tree_vec (count);
34947 incrv = make_tree_vec (count);
34949 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34951 for (i = 0; i < count; i++)
34953 int bracecount = 0;
34954 tree add_private_clause = NULL_TREE;
34955 location_t loc;
34957 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34959 if (!collapse_err)
34960 cp_parser_error (parser, "for statement expected");
34961 return NULL;
34963 loc = cp_lexer_consume_token (parser->lexer)->location;
34965 matching_parens parens;
34966 if (!parens.require_open (parser))
34967 return NULL;
34969 init = orig_init = decl = real_decl = NULL;
34970 this_pre_body = push_stmt_list ();
34972 add_private_clause
34973 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34974 init, orig_init, decl, real_decl);
34976 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34977 if (this_pre_body)
34979 this_pre_body = pop_stmt_list (this_pre_body);
34980 if (pre_body)
34982 tree t = pre_body;
34983 pre_body = push_stmt_list ();
34984 add_stmt (t);
34985 add_stmt (this_pre_body);
34986 pre_body = pop_stmt_list (pre_body);
34988 else
34989 pre_body = this_pre_body;
34992 if (decl)
34993 real_decl = decl;
34994 if (cclauses != NULL
34995 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34996 && real_decl != NULL_TREE)
34998 tree *c;
34999 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35000 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35001 && OMP_CLAUSE_DECL (*c) == real_decl)
35003 error_at (loc, "iteration variable %qD"
35004 " should not be firstprivate", real_decl);
35005 *c = OMP_CLAUSE_CHAIN (*c);
35007 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35008 && OMP_CLAUSE_DECL (*c) == real_decl)
35010 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35011 tree l = *c;
35012 *c = OMP_CLAUSE_CHAIN (*c);
35013 if (code == OMP_SIMD)
35015 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35016 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35018 else
35020 OMP_CLAUSE_CHAIN (l) = clauses;
35021 clauses = l;
35023 add_private_clause = NULL_TREE;
35025 else
35027 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35028 && OMP_CLAUSE_DECL (*c) == real_decl)
35029 add_private_clause = NULL_TREE;
35030 c = &OMP_CLAUSE_CHAIN (*c);
35034 if (add_private_clause)
35036 tree c;
35037 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35039 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35040 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35041 && OMP_CLAUSE_DECL (c) == decl)
35042 break;
35043 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35044 && OMP_CLAUSE_DECL (c) == decl)
35045 error_at (loc, "iteration variable %qD "
35046 "should not be firstprivate",
35047 decl);
35048 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35049 && OMP_CLAUSE_DECL (c) == decl)
35050 error_at (loc, "iteration variable %qD should not be reduction",
35051 decl);
35053 if (c == NULL)
35055 if (code != OMP_SIMD)
35056 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35057 else if (collapse == 1)
35058 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35059 else
35060 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35061 OMP_CLAUSE_DECL (c) = add_private_clause;
35062 c = finish_omp_clauses (c, C_ORT_OMP);
35063 if (c)
35065 OMP_CLAUSE_CHAIN (c) = clauses;
35066 clauses = c;
35067 /* For linear, signal that we need to fill up
35068 the so far unknown linear step. */
35069 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35070 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35075 cond = NULL;
35076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35077 cond = cp_parser_omp_for_cond (parser, decl);
35078 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35080 incr = NULL;
35081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35083 /* If decl is an iterator, preserve the operator on decl
35084 until finish_omp_for. */
35085 if (real_decl
35086 && ((processing_template_decl
35087 && (TREE_TYPE (real_decl) == NULL_TREE
35088 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35089 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35090 incr = cp_parser_omp_for_incr (parser, real_decl);
35091 else
35092 incr = cp_parser_expression (parser);
35093 if (!EXPR_HAS_LOCATION (incr))
35094 protected_set_expr_location (incr, input_location);
35097 if (!parens.require_close (parser))
35098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35099 /*or_comma=*/false,
35100 /*consume_paren=*/true);
35102 TREE_VEC_ELT (declv, i) = decl;
35103 TREE_VEC_ELT (initv, i) = init;
35104 TREE_VEC_ELT (condv, i) = cond;
35105 TREE_VEC_ELT (incrv, i) = incr;
35106 if (orig_init)
35108 orig_inits.safe_grow_cleared (i + 1);
35109 orig_inits[i] = orig_init;
35112 if (i == count - 1)
35113 break;
35115 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35116 in between the collapsed for loops to be still considered perfectly
35117 nested. Hopefully the final version clarifies this.
35118 For now handle (multiple) {'s and empty statements. */
35119 cp_parser_parse_tentatively (parser);
35120 for (;;)
35122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35123 break;
35124 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35126 cp_lexer_consume_token (parser->lexer);
35127 bracecount++;
35129 else if (bracecount
35130 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35131 cp_lexer_consume_token (parser->lexer);
35132 else
35134 loc = cp_lexer_peek_token (parser->lexer)->location;
35135 error_at (loc, "not enough for loops to collapse");
35136 collapse_err = true;
35137 cp_parser_abort_tentative_parse (parser);
35138 declv = NULL_TREE;
35139 break;
35143 if (declv)
35145 cp_parser_parse_definitely (parser);
35146 nbraces += bracecount;
35150 if (nbraces)
35151 if_p = NULL;
35153 /* Note that we saved the original contents of this flag when we entered
35154 the structured block, and so we don't need to re-save it here. */
35155 parser->in_statement = IN_OMP_FOR;
35157 /* Note that the grammar doesn't call for a structured block here,
35158 though the loop as a whole is a structured block. */
35159 body = push_stmt_list ();
35160 cp_parser_statement (parser, NULL_TREE, false, if_p);
35161 body = pop_stmt_list (body);
35163 if (declv == NULL_TREE)
35164 ret = NULL_TREE;
35165 else
35166 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35167 body, pre_body, &orig_inits, clauses);
35169 while (nbraces)
35171 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35173 cp_lexer_consume_token (parser->lexer);
35174 nbraces--;
35176 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35177 cp_lexer_consume_token (parser->lexer);
35178 else
35180 if (!collapse_err)
35182 error_at (cp_lexer_peek_token (parser->lexer)->location,
35183 "collapsed loops not perfectly nested");
35185 collapse_err = true;
35186 cp_parser_statement_seq_opt (parser, NULL);
35187 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35188 break;
35192 while (!for_block->is_empty ())
35194 tree t = for_block->pop ();
35195 if (TREE_CODE (t) == STATEMENT_LIST)
35196 add_stmt (pop_stmt_list (t));
35197 else
35198 add_stmt (t);
35200 release_tree_vector (for_block);
35202 return ret;
35205 /* Helper function for OpenMP parsing, split clauses and call
35206 finish_omp_clauses on each of the set of clauses afterwards. */
35208 static void
35209 cp_omp_split_clauses (location_t loc, enum tree_code code,
35210 omp_clause_mask mask, tree clauses, tree *cclauses)
35212 int i;
35213 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35214 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35215 if (cclauses[i])
35216 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35219 /* OpenMP 4.0:
35220 #pragma omp simd simd-clause[optseq] new-line
35221 for-loop */
35223 #define OMP_SIMD_CLAUSE_MASK \
35224 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35233 static tree
35234 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35235 char *p_name, omp_clause_mask mask, tree *cclauses,
35236 bool *if_p)
35238 tree clauses, sb, ret;
35239 unsigned int save;
35240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35242 strcat (p_name, " simd");
35243 mask |= OMP_SIMD_CLAUSE_MASK;
35245 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35246 cclauses == NULL);
35247 if (cclauses)
35249 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35250 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35251 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35252 OMP_CLAUSE_ORDERED);
35253 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35255 error_at (OMP_CLAUSE_LOCATION (c),
35256 "%<ordered%> clause with parameter may not be specified "
35257 "on %qs construct", p_name);
35258 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35262 sb = begin_omp_structured_block ();
35263 save = cp_parser_begin_omp_structured_block (parser);
35265 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35267 cp_parser_end_omp_structured_block (parser, save);
35268 add_stmt (finish_omp_structured_block (sb));
35270 return ret;
35273 /* OpenMP 2.5:
35274 #pragma omp for for-clause[optseq] new-line
35275 for-loop
35277 OpenMP 4.0:
35278 #pragma omp for simd for-simd-clause[optseq] new-line
35279 for-loop */
35281 #define OMP_FOR_CLAUSE_MASK \
35282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35292 static tree
35293 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35294 char *p_name, omp_clause_mask mask, tree *cclauses,
35295 bool *if_p)
35297 tree clauses, sb, ret;
35298 unsigned int save;
35299 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35301 strcat (p_name, " for");
35302 mask |= OMP_FOR_CLAUSE_MASK;
35303 /* parallel for{, simd} disallows nowait clause, but for
35304 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35305 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35306 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35307 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35308 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35309 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35311 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35314 const char *p = IDENTIFIER_POINTER (id);
35316 if (strcmp (p, "simd") == 0)
35318 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35319 if (cclauses == NULL)
35320 cclauses = cclauses_buf;
35322 cp_lexer_consume_token (parser->lexer);
35323 if (!flag_openmp) /* flag_openmp_simd */
35324 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35325 cclauses, if_p);
35326 sb = begin_omp_structured_block ();
35327 save = cp_parser_begin_omp_structured_block (parser);
35328 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35329 cclauses, if_p);
35330 cp_parser_end_omp_structured_block (parser, save);
35331 tree body = finish_omp_structured_block (sb);
35332 if (ret == NULL)
35333 return ret;
35334 ret = make_node (OMP_FOR);
35335 TREE_TYPE (ret) = void_type_node;
35336 OMP_FOR_BODY (ret) = body;
35337 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35338 SET_EXPR_LOCATION (ret, loc);
35339 add_stmt (ret);
35340 return ret;
35343 if (!flag_openmp) /* flag_openmp_simd */
35345 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35346 return NULL_TREE;
35349 /* Composite distribute parallel for disallows linear clause. */
35350 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35351 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35353 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35354 cclauses == NULL);
35355 if (cclauses)
35357 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35358 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35361 sb = begin_omp_structured_block ();
35362 save = cp_parser_begin_omp_structured_block (parser);
35364 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35366 cp_parser_end_omp_structured_block (parser, save);
35367 add_stmt (finish_omp_structured_block (sb));
35369 return ret;
35372 /* OpenMP 2.5:
35373 # pragma omp master new-line
35374 structured-block */
35376 static tree
35377 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35379 cp_parser_require_pragma_eol (parser, pragma_tok);
35380 return c_finish_omp_master (input_location,
35381 cp_parser_omp_structured_block (parser, if_p));
35384 /* OpenMP 2.5:
35385 # pragma omp ordered new-line
35386 structured-block
35388 OpenMP 4.5:
35389 # pragma omp ordered ordered-clauses new-line
35390 structured-block */
35392 #define OMP_ORDERED_CLAUSE_MASK \
35393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35396 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35397 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35399 static bool
35400 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35401 enum pragma_context context, bool *if_p)
35403 location_t loc = pragma_tok->location;
35405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35407 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35408 const char *p = IDENTIFIER_POINTER (id);
35410 if (strcmp (p, "depend") == 0)
35412 if (!flag_openmp) /* flag_openmp_simd */
35414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35415 return false;
35417 if (context == pragma_stmt)
35419 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35420 "%<depend%> clause may only be used in compound "
35421 "statements");
35422 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35423 return false;
35425 tree clauses
35426 = cp_parser_omp_all_clauses (parser,
35427 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35428 "#pragma omp ordered", pragma_tok);
35429 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35430 return false;
35434 tree clauses
35435 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35436 "#pragma omp ordered", pragma_tok);
35438 if (!flag_openmp /* flag_openmp_simd */
35439 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35440 return false;
35442 c_finish_omp_ordered (loc, clauses,
35443 cp_parser_omp_structured_block (parser, if_p));
35444 return true;
35447 /* OpenMP 2.5:
35449 section-scope:
35450 { section-sequence }
35452 section-sequence:
35453 section-directive[opt] structured-block
35454 section-sequence section-directive structured-block */
35456 static tree
35457 cp_parser_omp_sections_scope (cp_parser *parser)
35459 tree stmt, substmt;
35460 bool error_suppress = false;
35461 cp_token *tok;
35463 matching_braces braces;
35464 if (!braces.require_open (parser))
35465 return NULL_TREE;
35467 stmt = push_stmt_list ();
35469 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35470 != PRAGMA_OMP_SECTION)
35472 substmt = cp_parser_omp_structured_block (parser, NULL);
35473 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35474 add_stmt (substmt);
35477 while (1)
35479 tok = cp_lexer_peek_token (parser->lexer);
35480 if (tok->type == CPP_CLOSE_BRACE)
35481 break;
35482 if (tok->type == CPP_EOF)
35483 break;
35485 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35487 cp_lexer_consume_token (parser->lexer);
35488 cp_parser_require_pragma_eol (parser, tok);
35489 error_suppress = false;
35491 else if (!error_suppress)
35493 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35494 error_suppress = true;
35497 substmt = cp_parser_omp_structured_block (parser, NULL);
35498 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35499 add_stmt (substmt);
35501 braces.require_close (parser);
35503 substmt = pop_stmt_list (stmt);
35505 stmt = make_node (OMP_SECTIONS);
35506 TREE_TYPE (stmt) = void_type_node;
35507 OMP_SECTIONS_BODY (stmt) = substmt;
35509 add_stmt (stmt);
35510 return stmt;
35513 /* OpenMP 2.5:
35514 # pragma omp sections sections-clause[optseq] newline
35515 sections-scope */
35517 #define OMP_SECTIONS_CLAUSE_MASK \
35518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35524 static tree
35525 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35526 char *p_name, omp_clause_mask mask, tree *cclauses)
35528 tree clauses, ret;
35529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35531 strcat (p_name, " sections");
35532 mask |= OMP_SECTIONS_CLAUSE_MASK;
35533 if (cclauses)
35534 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35536 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35537 cclauses == NULL);
35538 if (cclauses)
35540 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35541 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35544 ret = cp_parser_omp_sections_scope (parser);
35545 if (ret)
35546 OMP_SECTIONS_CLAUSES (ret) = clauses;
35548 return ret;
35551 /* OpenMP 2.5:
35552 # pragma omp parallel parallel-clause[optseq] new-line
35553 structured-block
35554 # pragma omp parallel for parallel-for-clause[optseq] new-line
35555 structured-block
35556 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35557 structured-block
35559 OpenMP 4.0:
35560 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35561 structured-block */
35563 #define OMP_PARALLEL_CLAUSE_MASK \
35564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35574 static tree
35575 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35576 char *p_name, omp_clause_mask mask, tree *cclauses,
35577 bool *if_p)
35579 tree stmt, clauses, block;
35580 unsigned int save;
35581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35583 strcat (p_name, " parallel");
35584 mask |= OMP_PARALLEL_CLAUSE_MASK;
35585 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35586 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35587 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35588 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35592 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35593 if (cclauses == NULL)
35594 cclauses = cclauses_buf;
35596 cp_lexer_consume_token (parser->lexer);
35597 if (!flag_openmp) /* flag_openmp_simd */
35598 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35599 if_p);
35600 block = begin_omp_parallel ();
35601 save = cp_parser_begin_omp_structured_block (parser);
35602 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35603 if_p);
35604 cp_parser_end_omp_structured_block (parser, save);
35605 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35606 block);
35607 if (ret == NULL_TREE)
35608 return ret;
35609 OMP_PARALLEL_COMBINED (stmt) = 1;
35610 return stmt;
35612 /* When combined with distribute, parallel has to be followed by for.
35613 #pragma omp target parallel is allowed though. */
35614 else if (cclauses
35615 && (mask & (OMP_CLAUSE_MASK_1
35616 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35618 error_at (loc, "expected %<for%> after %qs", p_name);
35619 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35620 return NULL_TREE;
35622 else if (!flag_openmp) /* flag_openmp_simd */
35624 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35625 return NULL_TREE;
35627 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35630 const char *p = IDENTIFIER_POINTER (id);
35631 if (strcmp (p, "sections") == 0)
35633 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35634 cclauses = cclauses_buf;
35636 cp_lexer_consume_token (parser->lexer);
35637 block = begin_omp_parallel ();
35638 save = cp_parser_begin_omp_structured_block (parser);
35639 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35640 cp_parser_end_omp_structured_block (parser, save);
35641 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35642 block);
35643 OMP_PARALLEL_COMBINED (stmt) = 1;
35644 return stmt;
35648 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35649 cclauses == NULL);
35650 if (cclauses)
35652 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35653 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35656 block = begin_omp_parallel ();
35657 save = cp_parser_begin_omp_structured_block (parser);
35658 cp_parser_statement (parser, NULL_TREE, false, if_p);
35659 cp_parser_end_omp_structured_block (parser, save);
35660 stmt = finish_omp_parallel (clauses, block);
35661 return stmt;
35664 /* OpenMP 2.5:
35665 # pragma omp single single-clause[optseq] new-line
35666 structured-block */
35668 #define OMP_SINGLE_CLAUSE_MASK \
35669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35674 static tree
35675 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35677 tree stmt = make_node (OMP_SINGLE);
35678 TREE_TYPE (stmt) = void_type_node;
35680 OMP_SINGLE_CLAUSES (stmt)
35681 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35682 "#pragma omp single", pragma_tok);
35683 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35685 return add_stmt (stmt);
35688 /* OpenMP 3.0:
35689 # pragma omp task task-clause[optseq] new-line
35690 structured-block */
35692 #define OMP_TASK_CLAUSE_MASK \
35693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35704 static tree
35705 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35707 tree clauses, block;
35708 unsigned int save;
35710 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35711 "#pragma omp task", pragma_tok);
35712 block = begin_omp_task ();
35713 save = cp_parser_begin_omp_structured_block (parser);
35714 cp_parser_statement (parser, NULL_TREE, false, if_p);
35715 cp_parser_end_omp_structured_block (parser, save);
35716 return finish_omp_task (clauses, block);
35719 /* OpenMP 3.0:
35720 # pragma omp taskwait new-line */
35722 static void
35723 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35725 cp_parser_require_pragma_eol (parser, pragma_tok);
35726 finish_omp_taskwait ();
35729 /* OpenMP 3.1:
35730 # pragma omp taskyield new-line */
35732 static void
35733 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35735 cp_parser_require_pragma_eol (parser, pragma_tok);
35736 finish_omp_taskyield ();
35739 /* OpenMP 4.0:
35740 # pragma omp taskgroup new-line
35741 structured-block */
35743 static tree
35744 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35746 cp_parser_require_pragma_eol (parser, pragma_tok);
35747 return c_finish_omp_taskgroup (input_location,
35748 cp_parser_omp_structured_block (parser,
35749 if_p));
35753 /* OpenMP 2.5:
35754 # pragma omp threadprivate (variable-list) */
35756 static void
35757 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35759 tree vars;
35761 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35762 cp_parser_require_pragma_eol (parser, pragma_tok);
35764 finish_omp_threadprivate (vars);
35767 /* OpenMP 4.0:
35768 # pragma omp cancel cancel-clause[optseq] new-line */
35770 #define OMP_CANCEL_CLAUSE_MASK \
35771 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35777 static void
35778 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35780 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35781 "#pragma omp cancel", pragma_tok);
35782 finish_omp_cancel (clauses);
35785 /* OpenMP 4.0:
35786 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35788 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35794 static void
35795 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35796 enum pragma_context context)
35798 tree clauses;
35799 bool point_seen = false;
35801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35804 const char *p = IDENTIFIER_POINTER (id);
35806 if (strcmp (p, "point") == 0)
35808 cp_lexer_consume_token (parser->lexer);
35809 point_seen = true;
35812 if (!point_seen)
35814 cp_parser_error (parser, "expected %<point%>");
35815 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35816 return;
35819 if (context != pragma_compound)
35821 if (context == pragma_stmt)
35822 error_at (pragma_tok->location,
35823 "%<#pragma %s%> may only be used in compound statements",
35824 "omp cancellation point");
35825 else
35826 cp_parser_error (parser, "expected declaration specifiers");
35827 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35828 return;
35831 clauses = cp_parser_omp_all_clauses (parser,
35832 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35833 "#pragma omp cancellation point",
35834 pragma_tok);
35835 finish_omp_cancellation_point (clauses);
35838 /* OpenMP 4.0:
35839 #pragma omp distribute distribute-clause[optseq] new-line
35840 for-loop */
35842 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35843 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35849 static tree
35850 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35851 char *p_name, omp_clause_mask mask, tree *cclauses,
35852 bool *if_p)
35854 tree clauses, sb, ret;
35855 unsigned int save;
35856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35858 strcat (p_name, " distribute");
35859 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35863 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35864 const char *p = IDENTIFIER_POINTER (id);
35865 bool simd = false;
35866 bool parallel = false;
35868 if (strcmp (p, "simd") == 0)
35869 simd = true;
35870 else
35871 parallel = strcmp (p, "parallel") == 0;
35872 if (parallel || simd)
35874 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35875 if (cclauses == NULL)
35876 cclauses = cclauses_buf;
35877 cp_lexer_consume_token (parser->lexer);
35878 if (!flag_openmp) /* flag_openmp_simd */
35880 if (simd)
35881 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35882 cclauses, if_p);
35883 else
35884 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35885 cclauses, if_p);
35887 sb = begin_omp_structured_block ();
35888 save = cp_parser_begin_omp_structured_block (parser);
35889 if (simd)
35890 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35891 cclauses, if_p);
35892 else
35893 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35894 cclauses, if_p);
35895 cp_parser_end_omp_structured_block (parser, save);
35896 tree body = finish_omp_structured_block (sb);
35897 if (ret == NULL)
35898 return ret;
35899 ret = make_node (OMP_DISTRIBUTE);
35900 TREE_TYPE (ret) = void_type_node;
35901 OMP_FOR_BODY (ret) = body;
35902 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35903 SET_EXPR_LOCATION (ret, loc);
35904 add_stmt (ret);
35905 return ret;
35908 if (!flag_openmp) /* flag_openmp_simd */
35910 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35911 return NULL_TREE;
35914 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35915 cclauses == NULL);
35916 if (cclauses)
35918 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35919 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35922 sb = begin_omp_structured_block ();
35923 save = cp_parser_begin_omp_structured_block (parser);
35925 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35927 cp_parser_end_omp_structured_block (parser, save);
35928 add_stmt (finish_omp_structured_block (sb));
35930 return ret;
35933 /* OpenMP 4.0:
35934 # pragma omp teams teams-clause[optseq] new-line
35935 structured-block */
35937 #define OMP_TEAMS_CLAUSE_MASK \
35938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35946 static tree
35947 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35948 char *p_name, omp_clause_mask mask, tree *cclauses,
35949 bool *if_p)
35951 tree clauses, sb, ret;
35952 unsigned int save;
35953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35955 strcat (p_name, " teams");
35956 mask |= OMP_TEAMS_CLAUSE_MASK;
35958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35961 const char *p = IDENTIFIER_POINTER (id);
35962 if (strcmp (p, "distribute") == 0)
35964 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35965 if (cclauses == NULL)
35966 cclauses = cclauses_buf;
35968 cp_lexer_consume_token (parser->lexer);
35969 if (!flag_openmp) /* flag_openmp_simd */
35970 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35971 cclauses, if_p);
35972 sb = begin_omp_structured_block ();
35973 save = cp_parser_begin_omp_structured_block (parser);
35974 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35975 cclauses, if_p);
35976 cp_parser_end_omp_structured_block (parser, save);
35977 tree body = finish_omp_structured_block (sb);
35978 if (ret == NULL)
35979 return ret;
35980 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35981 ret = make_node (OMP_TEAMS);
35982 TREE_TYPE (ret) = void_type_node;
35983 OMP_TEAMS_CLAUSES (ret) = clauses;
35984 OMP_TEAMS_BODY (ret) = body;
35985 OMP_TEAMS_COMBINED (ret) = 1;
35986 SET_EXPR_LOCATION (ret, loc);
35987 return add_stmt (ret);
35990 if (!flag_openmp) /* flag_openmp_simd */
35992 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35993 return NULL_TREE;
35996 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35997 cclauses == NULL);
35998 if (cclauses)
36000 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36001 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36004 tree stmt = make_node (OMP_TEAMS);
36005 TREE_TYPE (stmt) = void_type_node;
36006 OMP_TEAMS_CLAUSES (stmt) = clauses;
36007 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36008 SET_EXPR_LOCATION (stmt, loc);
36010 return add_stmt (stmt);
36013 /* OpenMP 4.0:
36014 # pragma omp target data target-data-clause[optseq] new-line
36015 structured-block */
36017 #define OMP_TARGET_DATA_CLAUSE_MASK \
36018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36023 static tree
36024 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36026 tree clauses
36027 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36028 "#pragma omp target data", pragma_tok);
36029 int map_seen = 0;
36030 for (tree *pc = &clauses; *pc;)
36032 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36033 switch (OMP_CLAUSE_MAP_KIND (*pc))
36035 case GOMP_MAP_TO:
36036 case GOMP_MAP_ALWAYS_TO:
36037 case GOMP_MAP_FROM:
36038 case GOMP_MAP_ALWAYS_FROM:
36039 case GOMP_MAP_TOFROM:
36040 case GOMP_MAP_ALWAYS_TOFROM:
36041 case GOMP_MAP_ALLOC:
36042 map_seen = 3;
36043 break;
36044 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36045 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36046 case GOMP_MAP_ALWAYS_POINTER:
36047 break;
36048 default:
36049 map_seen |= 1;
36050 error_at (OMP_CLAUSE_LOCATION (*pc),
36051 "%<#pragma omp target data%> with map-type other "
36052 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36053 "on %<map%> clause");
36054 *pc = OMP_CLAUSE_CHAIN (*pc);
36055 continue;
36057 pc = &OMP_CLAUSE_CHAIN (*pc);
36060 if (map_seen != 3)
36062 if (map_seen == 0)
36063 error_at (pragma_tok->location,
36064 "%<#pragma omp target data%> must contain at least "
36065 "one %<map%> clause");
36066 return NULL_TREE;
36069 tree stmt = make_node (OMP_TARGET_DATA);
36070 TREE_TYPE (stmt) = void_type_node;
36071 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36073 keep_next_level (true);
36074 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36076 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36077 return add_stmt (stmt);
36080 /* OpenMP 4.5:
36081 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36082 structured-block */
36084 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36091 static tree
36092 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36093 enum pragma_context context)
36095 bool data_seen = false;
36096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36098 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36099 const char *p = IDENTIFIER_POINTER (id);
36101 if (strcmp (p, "data") == 0)
36103 cp_lexer_consume_token (parser->lexer);
36104 data_seen = true;
36107 if (!data_seen)
36109 cp_parser_error (parser, "expected %<data%>");
36110 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36111 return NULL_TREE;
36114 if (context == pragma_stmt)
36116 error_at (pragma_tok->location,
36117 "%<#pragma %s%> may only be used in compound statements",
36118 "omp target enter data");
36119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36120 return NULL_TREE;
36123 tree clauses
36124 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36125 "#pragma omp target enter data", pragma_tok);
36126 int map_seen = 0;
36127 for (tree *pc = &clauses; *pc;)
36129 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36130 switch (OMP_CLAUSE_MAP_KIND (*pc))
36132 case GOMP_MAP_TO:
36133 case GOMP_MAP_ALWAYS_TO:
36134 case GOMP_MAP_ALLOC:
36135 map_seen = 3;
36136 break;
36137 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36138 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36139 case GOMP_MAP_ALWAYS_POINTER:
36140 break;
36141 default:
36142 map_seen |= 1;
36143 error_at (OMP_CLAUSE_LOCATION (*pc),
36144 "%<#pragma omp target enter data%> with map-type other "
36145 "than %<to%> or %<alloc%> on %<map%> clause");
36146 *pc = OMP_CLAUSE_CHAIN (*pc);
36147 continue;
36149 pc = &OMP_CLAUSE_CHAIN (*pc);
36152 if (map_seen != 3)
36154 if (map_seen == 0)
36155 error_at (pragma_tok->location,
36156 "%<#pragma omp target enter data%> must contain at least "
36157 "one %<map%> clause");
36158 return NULL_TREE;
36161 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36162 TREE_TYPE (stmt) = void_type_node;
36163 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36164 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36165 return add_stmt (stmt);
36168 /* OpenMP 4.5:
36169 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36170 structured-block */
36172 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36179 static tree
36180 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36181 enum pragma_context context)
36183 bool data_seen = false;
36184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36186 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36187 const char *p = IDENTIFIER_POINTER (id);
36189 if (strcmp (p, "data") == 0)
36191 cp_lexer_consume_token (parser->lexer);
36192 data_seen = true;
36195 if (!data_seen)
36197 cp_parser_error (parser, "expected %<data%>");
36198 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36199 return NULL_TREE;
36202 if (context == pragma_stmt)
36204 error_at (pragma_tok->location,
36205 "%<#pragma %s%> may only be used in compound statements",
36206 "omp target exit data");
36207 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36208 return NULL_TREE;
36211 tree clauses
36212 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36213 "#pragma omp target exit data", pragma_tok);
36214 int map_seen = 0;
36215 for (tree *pc = &clauses; *pc;)
36217 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36218 switch (OMP_CLAUSE_MAP_KIND (*pc))
36220 case GOMP_MAP_FROM:
36221 case GOMP_MAP_ALWAYS_FROM:
36222 case GOMP_MAP_RELEASE:
36223 case GOMP_MAP_DELETE:
36224 map_seen = 3;
36225 break;
36226 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36227 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36228 case GOMP_MAP_ALWAYS_POINTER:
36229 break;
36230 default:
36231 map_seen |= 1;
36232 error_at (OMP_CLAUSE_LOCATION (*pc),
36233 "%<#pragma omp target exit data%> with map-type other "
36234 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36235 " clause");
36236 *pc = OMP_CLAUSE_CHAIN (*pc);
36237 continue;
36239 pc = &OMP_CLAUSE_CHAIN (*pc);
36242 if (map_seen != 3)
36244 if (map_seen == 0)
36245 error_at (pragma_tok->location,
36246 "%<#pragma omp target exit data%> must contain at least "
36247 "one %<map%> clause");
36248 return NULL_TREE;
36251 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36252 TREE_TYPE (stmt) = void_type_node;
36253 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36254 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36255 return add_stmt (stmt);
36258 /* OpenMP 4.0:
36259 # pragma omp target update target-update-clause[optseq] new-line */
36261 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36262 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36269 static bool
36270 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36271 enum pragma_context context)
36273 if (context == pragma_stmt)
36275 error_at (pragma_tok->location,
36276 "%<#pragma %s%> may only be used in compound statements",
36277 "omp target update");
36278 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36279 return false;
36282 tree clauses
36283 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36284 "#pragma omp target update", pragma_tok);
36285 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36286 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36288 error_at (pragma_tok->location,
36289 "%<#pragma omp target update%> must contain at least one "
36290 "%<from%> or %<to%> clauses");
36291 return false;
36294 tree stmt = make_node (OMP_TARGET_UPDATE);
36295 TREE_TYPE (stmt) = void_type_node;
36296 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36297 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36298 add_stmt (stmt);
36299 return false;
36302 /* OpenMP 4.0:
36303 # pragma omp target target-clause[optseq] new-line
36304 structured-block */
36306 #define OMP_TARGET_CLAUSE_MASK \
36307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36317 static bool
36318 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36319 enum pragma_context context, bool *if_p)
36321 tree *pc = NULL, stmt;
36323 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36326 const char *p = IDENTIFIER_POINTER (id);
36327 enum tree_code ccode = ERROR_MARK;
36329 if (strcmp (p, "teams") == 0)
36330 ccode = OMP_TEAMS;
36331 else if (strcmp (p, "parallel") == 0)
36332 ccode = OMP_PARALLEL;
36333 else if (strcmp (p, "simd") == 0)
36334 ccode = OMP_SIMD;
36335 if (ccode != ERROR_MARK)
36337 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36338 char p_name[sizeof ("#pragma omp target teams distribute "
36339 "parallel for simd")];
36341 cp_lexer_consume_token (parser->lexer);
36342 strcpy (p_name, "#pragma omp target");
36343 if (!flag_openmp) /* flag_openmp_simd */
36345 tree stmt;
36346 switch (ccode)
36348 case OMP_TEAMS:
36349 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36350 OMP_TARGET_CLAUSE_MASK,
36351 cclauses, if_p);
36352 break;
36353 case OMP_PARALLEL:
36354 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36355 OMP_TARGET_CLAUSE_MASK,
36356 cclauses, if_p);
36357 break;
36358 case OMP_SIMD:
36359 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36360 OMP_TARGET_CLAUSE_MASK,
36361 cclauses, if_p);
36362 break;
36363 default:
36364 gcc_unreachable ();
36366 return stmt != NULL_TREE;
36368 keep_next_level (true);
36369 tree sb = begin_omp_structured_block (), ret;
36370 unsigned save = cp_parser_begin_omp_structured_block (parser);
36371 switch (ccode)
36373 case OMP_TEAMS:
36374 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36375 OMP_TARGET_CLAUSE_MASK, cclauses,
36376 if_p);
36377 break;
36378 case OMP_PARALLEL:
36379 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36380 OMP_TARGET_CLAUSE_MASK, cclauses,
36381 if_p);
36382 break;
36383 case OMP_SIMD:
36384 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36385 OMP_TARGET_CLAUSE_MASK, cclauses,
36386 if_p);
36387 break;
36388 default:
36389 gcc_unreachable ();
36391 cp_parser_end_omp_structured_block (parser, save);
36392 tree body = finish_omp_structured_block (sb);
36393 if (ret == NULL_TREE)
36394 return false;
36395 if (ccode == OMP_TEAMS && !processing_template_decl)
36397 /* For combined target teams, ensure the num_teams and
36398 thread_limit clause expressions are evaluated on the host,
36399 before entering the target construct. */
36400 tree c;
36401 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36402 c; c = OMP_CLAUSE_CHAIN (c))
36403 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36404 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36405 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36407 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36408 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36409 if (expr == error_mark_node)
36410 continue;
36411 tree tmp = TARGET_EXPR_SLOT (expr);
36412 add_stmt (expr);
36413 OMP_CLAUSE_OPERAND (c, 0) = expr;
36414 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36415 OMP_CLAUSE_FIRSTPRIVATE);
36416 OMP_CLAUSE_DECL (tc) = tmp;
36417 OMP_CLAUSE_CHAIN (tc)
36418 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36419 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36422 tree stmt = make_node (OMP_TARGET);
36423 TREE_TYPE (stmt) = void_type_node;
36424 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36425 OMP_TARGET_BODY (stmt) = body;
36426 OMP_TARGET_COMBINED (stmt) = 1;
36427 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36428 add_stmt (stmt);
36429 pc = &OMP_TARGET_CLAUSES (stmt);
36430 goto check_clauses;
36432 else if (!flag_openmp) /* flag_openmp_simd */
36434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36435 return false;
36437 else if (strcmp (p, "data") == 0)
36439 cp_lexer_consume_token (parser->lexer);
36440 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36441 return true;
36443 else if (strcmp (p, "enter") == 0)
36445 cp_lexer_consume_token (parser->lexer);
36446 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36447 return false;
36449 else if (strcmp (p, "exit") == 0)
36451 cp_lexer_consume_token (parser->lexer);
36452 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36453 return false;
36455 else if (strcmp (p, "update") == 0)
36457 cp_lexer_consume_token (parser->lexer);
36458 return cp_parser_omp_target_update (parser, pragma_tok, context);
36461 if (!flag_openmp) /* flag_openmp_simd */
36463 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36464 return false;
36467 stmt = make_node (OMP_TARGET);
36468 TREE_TYPE (stmt) = void_type_node;
36470 OMP_TARGET_CLAUSES (stmt)
36471 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36472 "#pragma omp target", pragma_tok);
36473 pc = &OMP_TARGET_CLAUSES (stmt);
36474 keep_next_level (true);
36475 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36477 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36478 add_stmt (stmt);
36480 check_clauses:
36481 while (*pc)
36483 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36484 switch (OMP_CLAUSE_MAP_KIND (*pc))
36486 case GOMP_MAP_TO:
36487 case GOMP_MAP_ALWAYS_TO:
36488 case GOMP_MAP_FROM:
36489 case GOMP_MAP_ALWAYS_FROM:
36490 case GOMP_MAP_TOFROM:
36491 case GOMP_MAP_ALWAYS_TOFROM:
36492 case GOMP_MAP_ALLOC:
36493 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36494 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36495 case GOMP_MAP_ALWAYS_POINTER:
36496 break;
36497 default:
36498 error_at (OMP_CLAUSE_LOCATION (*pc),
36499 "%<#pragma omp target%> with map-type other "
36500 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36501 "on %<map%> clause");
36502 *pc = OMP_CLAUSE_CHAIN (*pc);
36503 continue;
36505 pc = &OMP_CLAUSE_CHAIN (*pc);
36507 return true;
36510 /* OpenACC 2.0:
36511 # pragma acc cache (variable-list) new-line
36514 static tree
36515 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36517 tree stmt, clauses;
36519 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36520 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36522 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36524 stmt = make_node (OACC_CACHE);
36525 TREE_TYPE (stmt) = void_type_node;
36526 OACC_CACHE_CLAUSES (stmt) = clauses;
36527 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36528 add_stmt (stmt);
36530 return stmt;
36533 /* OpenACC 2.0:
36534 # pragma acc data oacc-data-clause[optseq] new-line
36535 structured-block */
36537 #define OACC_DATA_CLAUSE_MASK \
36538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36550 static tree
36551 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36553 tree stmt, clauses, block;
36554 unsigned int save;
36556 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36557 "#pragma acc data", pragma_tok);
36559 block = begin_omp_parallel ();
36560 save = cp_parser_begin_omp_structured_block (parser);
36561 cp_parser_statement (parser, NULL_TREE, false, if_p);
36562 cp_parser_end_omp_structured_block (parser, save);
36563 stmt = finish_oacc_data (clauses, block);
36564 return stmt;
36567 /* OpenACC 2.0:
36568 # pragma acc host_data <clauses> new-line
36569 structured-block */
36571 #define OACC_HOST_DATA_CLAUSE_MASK \
36572 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36574 static tree
36575 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36577 tree stmt, clauses, block;
36578 unsigned int save;
36580 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36581 "#pragma acc host_data", pragma_tok);
36583 block = begin_omp_parallel ();
36584 save = cp_parser_begin_omp_structured_block (parser);
36585 cp_parser_statement (parser, NULL_TREE, false, if_p);
36586 cp_parser_end_omp_structured_block (parser, save);
36587 stmt = finish_oacc_host_data (clauses, block);
36588 return stmt;
36591 /* OpenACC 2.0:
36592 # pragma acc declare oacc-data-clause[optseq] new-line
36595 #define OACC_DECLARE_CLAUSE_MASK \
36596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36609 static tree
36610 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36612 tree clauses, stmt;
36613 bool error = false;
36615 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36616 "#pragma acc declare", pragma_tok, true);
36619 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36621 error_at (pragma_tok->location,
36622 "no valid clauses specified in %<#pragma acc declare%>");
36623 return NULL_TREE;
36626 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36628 location_t loc = OMP_CLAUSE_LOCATION (t);
36629 tree decl = OMP_CLAUSE_DECL (t);
36630 if (!DECL_P (decl))
36632 error_at (loc, "array section in %<#pragma acc declare%>");
36633 error = true;
36634 continue;
36636 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36637 switch (OMP_CLAUSE_MAP_KIND (t))
36639 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36640 case GOMP_MAP_FORCE_ALLOC:
36641 case GOMP_MAP_FORCE_TO:
36642 case GOMP_MAP_FORCE_DEVICEPTR:
36643 case GOMP_MAP_DEVICE_RESIDENT:
36644 break;
36646 case GOMP_MAP_LINK:
36647 if (!global_bindings_p ()
36648 && (TREE_STATIC (decl)
36649 || !DECL_EXTERNAL (decl)))
36651 error_at (loc,
36652 "%qD must be a global variable in "
36653 "%<#pragma acc declare link%>",
36654 decl);
36655 error = true;
36656 continue;
36658 break;
36660 default:
36661 if (global_bindings_p ())
36663 error_at (loc, "invalid OpenACC clause at file scope");
36664 error = true;
36665 continue;
36667 if (DECL_EXTERNAL (decl))
36669 error_at (loc,
36670 "invalid use of %<extern%> variable %qD "
36671 "in %<#pragma acc declare%>", decl);
36672 error = true;
36673 continue;
36675 else if (TREE_PUBLIC (decl))
36677 error_at (loc,
36678 "invalid use of %<global%> variable %qD "
36679 "in %<#pragma acc declare%>", decl);
36680 error = true;
36681 continue;
36683 break;
36686 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36687 || lookup_attribute ("omp declare target link",
36688 DECL_ATTRIBUTES (decl)))
36690 error_at (loc, "variable %qD used more than once with "
36691 "%<#pragma acc declare%>", decl);
36692 error = true;
36693 continue;
36696 if (!error)
36698 tree id;
36700 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36701 id = get_identifier ("omp declare target link");
36702 else
36703 id = get_identifier ("omp declare target");
36705 DECL_ATTRIBUTES (decl)
36706 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36707 if (global_bindings_p ())
36709 symtab_node *node = symtab_node::get (decl);
36710 if (node != NULL)
36712 node->offloadable = 1;
36713 if (ENABLE_OFFLOADING)
36715 g->have_offload = true;
36716 if (is_a <varpool_node *> (node))
36717 vec_safe_push (offload_vars, decl);
36724 if (error || global_bindings_p ())
36725 return NULL_TREE;
36727 stmt = make_node (OACC_DECLARE);
36728 TREE_TYPE (stmt) = void_type_node;
36729 OACC_DECLARE_CLAUSES (stmt) = clauses;
36730 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36732 add_stmt (stmt);
36734 return NULL_TREE;
36737 /* OpenACC 2.0:
36738 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36742 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36744 LOC is the location of the #pragma token.
36747 #define OACC_ENTER_DATA_CLAUSE_MASK \
36748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36756 #define OACC_EXIT_DATA_CLAUSE_MASK \
36757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36763 static tree
36764 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36765 bool enter)
36767 location_t loc = pragma_tok->location;
36768 tree stmt, clauses;
36769 const char *p = "";
36771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36772 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36774 if (strcmp (p, "data") != 0)
36776 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36777 enter ? "enter" : "exit");
36778 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36779 return NULL_TREE;
36782 cp_lexer_consume_token (parser->lexer);
36784 if (enter)
36785 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36786 "#pragma acc enter data", pragma_tok);
36787 else
36788 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36789 "#pragma acc exit data", pragma_tok);
36791 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36793 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36794 enter ? "enter" : "exit");
36795 return NULL_TREE;
36798 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36799 TREE_TYPE (stmt) = void_type_node;
36800 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36801 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36802 add_stmt (stmt);
36803 return stmt;
36806 /* OpenACC 2.0:
36807 # pragma acc loop oacc-loop-clause[optseq] new-line
36808 structured-block */
36810 #define OACC_LOOP_CLAUSE_MASK \
36811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36822 static tree
36823 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36824 omp_clause_mask mask, tree *cclauses, bool *if_p)
36826 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36828 strcat (p_name, " loop");
36829 mask |= OACC_LOOP_CLAUSE_MASK;
36831 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36832 cclauses == NULL);
36833 if (cclauses)
36835 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36836 if (*cclauses)
36837 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36838 if (clauses)
36839 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36842 tree block = begin_omp_structured_block ();
36843 int save = cp_parser_begin_omp_structured_block (parser);
36844 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36845 cp_parser_end_omp_structured_block (parser, save);
36846 add_stmt (finish_omp_structured_block (block));
36848 return stmt;
36851 /* OpenACC 2.0:
36852 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36853 structured-block
36857 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36858 structured-block
36861 #define OACC_KERNELS_CLAUSE_MASK \
36862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36880 #define OACC_PARALLEL_CLAUSE_MASK \
36881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36902 static tree
36903 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36904 char *p_name, bool *if_p)
36906 omp_clause_mask mask;
36907 enum tree_code code;
36908 switch (cp_parser_pragma_kind (pragma_tok))
36910 case PRAGMA_OACC_KERNELS:
36911 strcat (p_name, " kernels");
36912 mask = OACC_KERNELS_CLAUSE_MASK;
36913 code = OACC_KERNELS;
36914 break;
36915 case PRAGMA_OACC_PARALLEL:
36916 strcat (p_name, " parallel");
36917 mask = OACC_PARALLEL_CLAUSE_MASK;
36918 code = OACC_PARALLEL;
36919 break;
36920 default:
36921 gcc_unreachable ();
36924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36926 const char *p
36927 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36928 if (strcmp (p, "loop") == 0)
36930 cp_lexer_consume_token (parser->lexer);
36931 tree block = begin_omp_parallel ();
36932 tree clauses;
36933 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36934 if_p);
36935 return finish_omp_construct (code, block, clauses);
36939 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36941 tree block = begin_omp_parallel ();
36942 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36943 cp_parser_statement (parser, NULL_TREE, false, if_p);
36944 cp_parser_end_omp_structured_block (parser, save);
36945 return finish_omp_construct (code, block, clauses);
36948 /* OpenACC 2.0:
36949 # pragma acc update oacc-update-clause[optseq] new-line
36952 #define OACC_UPDATE_CLAUSE_MASK \
36953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36960 static tree
36961 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36963 tree stmt, clauses;
36965 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36966 "#pragma acc update", pragma_tok);
36968 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36970 error_at (pragma_tok->location,
36971 "%<#pragma acc update%> must contain at least one "
36972 "%<device%> or %<host%> or %<self%> clause");
36973 return NULL_TREE;
36976 stmt = make_node (OACC_UPDATE);
36977 TREE_TYPE (stmt) = void_type_node;
36978 OACC_UPDATE_CLAUSES (stmt) = clauses;
36979 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36980 add_stmt (stmt);
36981 return stmt;
36984 /* OpenACC 2.0:
36985 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36987 LOC is the location of the #pragma token.
36990 #define OACC_WAIT_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36993 static tree
36994 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36996 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36999 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37000 list = cp_parser_oacc_wait_list (parser, loc, list);
37002 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37003 "#pragma acc wait", pragma_tok);
37005 stmt = c_finish_oacc_wait (loc, list, clauses);
37006 stmt = finish_expr_stmt (stmt);
37008 return stmt;
37011 /* OpenMP 4.0:
37012 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37014 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37022 static void
37023 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37024 enum pragma_context context)
37026 bool first_p = parser->omp_declare_simd == NULL;
37027 cp_omp_declare_simd_data data;
37028 if (first_p)
37030 data.error_seen = false;
37031 data.fndecl_seen = false;
37032 data.tokens = vNULL;
37033 data.clauses = NULL_TREE;
37034 /* It is safe to take the address of a local variable; it will only be
37035 used while this scope is live. */
37036 parser->omp_declare_simd = &data;
37039 /* Store away all pragma tokens. */
37040 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37041 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37042 cp_lexer_consume_token (parser->lexer);
37043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37044 parser->omp_declare_simd->error_seen = true;
37045 cp_parser_require_pragma_eol (parser, pragma_tok);
37046 struct cp_token_cache *cp
37047 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37048 parser->omp_declare_simd->tokens.safe_push (cp);
37050 if (first_p)
37052 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37053 cp_parser_pragma (parser, context, NULL);
37054 switch (context)
37056 case pragma_external:
37057 cp_parser_declaration (parser);
37058 break;
37059 case pragma_member:
37060 cp_parser_member_declaration (parser);
37061 break;
37062 case pragma_objc_icode:
37063 cp_parser_block_declaration (parser, /*statement_p=*/false);
37064 break;
37065 default:
37066 cp_parser_declaration_statement (parser);
37067 break;
37069 if (parser->omp_declare_simd
37070 && !parser->omp_declare_simd->error_seen
37071 && !parser->omp_declare_simd->fndecl_seen)
37072 error_at (pragma_tok->location,
37073 "%<#pragma omp declare simd%> not immediately followed by "
37074 "function declaration or definition");
37075 data.tokens.release ();
37076 parser->omp_declare_simd = NULL;
37080 /* Finalize #pragma omp declare simd clauses after direct declarator has
37081 been parsed, and put that into "omp declare simd" attribute. */
37083 static tree
37084 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37086 struct cp_token_cache *ce;
37087 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37088 int i;
37090 if (!data->error_seen && data->fndecl_seen)
37092 error ("%<#pragma omp declare simd%> not immediately followed by "
37093 "a single function declaration or definition");
37094 data->error_seen = true;
37096 if (data->error_seen)
37097 return attrs;
37099 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37101 tree c, cl;
37103 cp_parser_push_lexer_for_tokens (parser, ce);
37104 parser->lexer->in_pragma = true;
37105 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37106 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37107 cp_lexer_consume_token (parser->lexer);
37108 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37109 "#pragma omp declare simd", pragma_tok);
37110 cp_parser_pop_lexer (parser);
37111 if (cl)
37112 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37113 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37114 TREE_CHAIN (c) = attrs;
37115 if (processing_template_decl)
37116 ATTR_IS_DEPENDENT (c) = 1;
37117 attrs = c;
37120 data->fndecl_seen = true;
37121 return attrs;
37125 /* OpenMP 4.0:
37126 # pragma omp declare target new-line
37127 declarations and definitions
37128 # pragma omp end declare target new-line
37130 OpenMP 4.5:
37131 # pragma omp declare target ( extended-list ) new-line
37133 # pragma omp declare target declare-target-clauses[seq] new-line */
37135 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37139 static void
37140 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37142 tree clauses = NULL_TREE;
37143 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37144 clauses
37145 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37146 "#pragma omp declare target", pragma_tok);
37147 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37149 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37150 clauses);
37151 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37152 cp_parser_require_pragma_eol (parser, pragma_tok);
37154 else
37156 cp_parser_require_pragma_eol (parser, pragma_tok);
37157 scope_chain->omp_declare_target_attribute++;
37158 return;
37160 if (scope_chain->omp_declare_target_attribute)
37161 error_at (pragma_tok->location,
37162 "%<#pragma omp declare target%> with clauses in between "
37163 "%<#pragma omp declare target%> without clauses and "
37164 "%<#pragma omp end declare target%>");
37165 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37167 tree t = OMP_CLAUSE_DECL (c), id;
37168 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37169 tree at2 = lookup_attribute ("omp declare target link",
37170 DECL_ATTRIBUTES (t));
37171 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37173 id = get_identifier ("omp declare target link");
37174 std::swap (at1, at2);
37176 else
37177 id = get_identifier ("omp declare target");
37178 if (at2)
37180 error_at (OMP_CLAUSE_LOCATION (c),
37181 "%qD specified both in declare target %<link%> and %<to%>"
37182 " clauses", t);
37183 continue;
37185 if (!at1)
37187 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37188 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37189 continue;
37191 symtab_node *node = symtab_node::get (t);
37192 if (node != NULL)
37194 node->offloadable = 1;
37195 if (ENABLE_OFFLOADING)
37197 g->have_offload = true;
37198 if (is_a <varpool_node *> (node))
37199 vec_safe_push (offload_vars, t);
37206 static void
37207 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37209 const char *p = "";
37210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37212 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37213 p = IDENTIFIER_POINTER (id);
37215 if (strcmp (p, "declare") == 0)
37217 cp_lexer_consume_token (parser->lexer);
37218 p = "";
37219 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37221 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37222 p = IDENTIFIER_POINTER (id);
37224 if (strcmp (p, "target") == 0)
37225 cp_lexer_consume_token (parser->lexer);
37226 else
37228 cp_parser_error (parser, "expected %<target%>");
37229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37230 return;
37233 else
37235 cp_parser_error (parser, "expected %<declare%>");
37236 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37237 return;
37239 cp_parser_require_pragma_eol (parser, pragma_tok);
37240 if (!scope_chain->omp_declare_target_attribute)
37241 error_at (pragma_tok->location,
37242 "%<#pragma omp end declare target%> without corresponding "
37243 "%<#pragma omp declare target%>");
37244 else
37245 scope_chain->omp_declare_target_attribute--;
37248 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37249 expression and optional initializer clause of
37250 #pragma omp declare reduction. We store the expression(s) as
37251 either 3, 6 or 7 special statements inside of the artificial function's
37252 body. The first two statements are DECL_EXPRs for the artificial
37253 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37254 expression that uses those variables.
37255 If there was any INITIALIZER clause, this is followed by further statements,
37256 the fourth and fifth statements are DECL_EXPRs for the artificial
37257 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37258 constructor variant (first token after open paren is not omp_priv),
37259 then the sixth statement is a statement with the function call expression
37260 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37261 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37262 to initialize the OMP_PRIV artificial variable and there is seventh
37263 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37265 static bool
37266 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37268 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37269 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37270 type = TREE_TYPE (type);
37271 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37272 DECL_ARTIFICIAL (omp_out) = 1;
37273 pushdecl (omp_out);
37274 add_decl_expr (omp_out);
37275 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37276 DECL_ARTIFICIAL (omp_in) = 1;
37277 pushdecl (omp_in);
37278 add_decl_expr (omp_in);
37279 tree combiner;
37280 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37282 keep_next_level (true);
37283 tree block = begin_omp_structured_block ();
37284 combiner = cp_parser_expression (parser);
37285 finish_expr_stmt (combiner);
37286 block = finish_omp_structured_block (block);
37287 add_stmt (block);
37289 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37290 return false;
37292 const char *p = "";
37293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37296 p = IDENTIFIER_POINTER (id);
37299 if (strcmp (p, "initializer") == 0)
37301 cp_lexer_consume_token (parser->lexer);
37302 matching_parens parens;
37303 if (!parens.require_open (parser))
37304 return false;
37306 p = "";
37307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37310 p = IDENTIFIER_POINTER (id);
37313 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37314 DECL_ARTIFICIAL (omp_priv) = 1;
37315 pushdecl (omp_priv);
37316 add_decl_expr (omp_priv);
37317 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37318 DECL_ARTIFICIAL (omp_orig) = 1;
37319 pushdecl (omp_orig);
37320 add_decl_expr (omp_orig);
37322 keep_next_level (true);
37323 block = begin_omp_structured_block ();
37325 bool ctor = false;
37326 if (strcmp (p, "omp_priv") == 0)
37328 bool is_direct_init, is_non_constant_init;
37329 ctor = true;
37330 cp_lexer_consume_token (parser->lexer);
37331 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37332 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37333 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37334 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37335 == CPP_CLOSE_PAREN
37336 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37337 == CPP_CLOSE_PAREN))
37339 finish_omp_structured_block (block);
37340 error ("invalid initializer clause");
37341 return false;
37343 initializer = cp_parser_initializer (parser, &is_direct_init,
37344 &is_non_constant_init);
37345 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37346 NULL_TREE, LOOKUP_ONLYCONVERTING);
37348 else
37350 cp_parser_parse_tentatively (parser);
37351 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37352 /*check_dependency_p=*/true,
37353 /*template_p=*/NULL,
37354 /*declarator_p=*/false,
37355 /*optional_p=*/false);
37356 vec<tree, va_gc> *args;
37357 if (fn_name == error_mark_node
37358 || cp_parser_error_occurred (parser)
37359 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37360 || ((args = cp_parser_parenthesized_expression_list
37361 (parser, non_attr, /*cast_p=*/false,
37362 /*allow_expansion_p=*/true,
37363 /*non_constant_p=*/NULL)),
37364 cp_parser_error_occurred (parser)))
37366 finish_omp_structured_block (block);
37367 cp_parser_abort_tentative_parse (parser);
37368 cp_parser_error (parser, "expected id-expression (arguments)");
37369 return false;
37371 unsigned int i;
37372 tree arg;
37373 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37374 if (arg == omp_priv
37375 || (TREE_CODE (arg) == ADDR_EXPR
37376 && TREE_OPERAND (arg, 0) == omp_priv))
37377 break;
37378 cp_parser_abort_tentative_parse (parser);
37379 if (arg == NULL_TREE)
37380 error ("one of the initializer call arguments should be %<omp_priv%>"
37381 " or %<&omp_priv%>");
37382 initializer = cp_parser_postfix_expression (parser, false, false, false,
37383 false, NULL);
37384 finish_expr_stmt (initializer);
37387 block = finish_omp_structured_block (block);
37388 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37389 add_stmt (block);
37391 if (ctor)
37392 add_decl_expr (omp_orig);
37394 if (!parens.require_close (parser))
37395 return false;
37398 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37399 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37400 UNKNOWN_LOCATION);
37402 return true;
37405 /* OpenMP 4.0
37406 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37407 initializer-clause[opt] new-line
37409 initializer-clause:
37410 initializer (omp_priv initializer)
37411 initializer (function-name (argument-list)) */
37413 static void
37414 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37415 enum pragma_context)
37417 auto_vec<tree> types;
37418 enum tree_code reduc_code = ERROR_MARK;
37419 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37420 unsigned int i;
37421 cp_token *first_token;
37422 cp_token_cache *cp;
37423 int errs;
37424 void *p;
37426 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37427 p = obstack_alloc (&declarator_obstack, 0);
37429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37430 goto fail;
37432 switch (cp_lexer_peek_token (parser->lexer)->type)
37434 case CPP_PLUS:
37435 reduc_code = PLUS_EXPR;
37436 break;
37437 case CPP_MULT:
37438 reduc_code = MULT_EXPR;
37439 break;
37440 case CPP_MINUS:
37441 reduc_code = MINUS_EXPR;
37442 break;
37443 case CPP_AND:
37444 reduc_code = BIT_AND_EXPR;
37445 break;
37446 case CPP_XOR:
37447 reduc_code = BIT_XOR_EXPR;
37448 break;
37449 case CPP_OR:
37450 reduc_code = BIT_IOR_EXPR;
37451 break;
37452 case CPP_AND_AND:
37453 reduc_code = TRUTH_ANDIF_EXPR;
37454 break;
37455 case CPP_OR_OR:
37456 reduc_code = TRUTH_ORIF_EXPR;
37457 break;
37458 case CPP_NAME:
37459 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37460 break;
37461 default:
37462 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37463 "%<|%>, %<&&%>, %<||%> or identifier");
37464 goto fail;
37467 if (reduc_code != ERROR_MARK)
37468 cp_lexer_consume_token (parser->lexer);
37470 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37471 if (reduc_id == error_mark_node)
37472 goto fail;
37474 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37475 goto fail;
37477 /* Types may not be defined in declare reduction type list. */
37478 const char *saved_message;
37479 saved_message = parser->type_definition_forbidden_message;
37480 parser->type_definition_forbidden_message
37481 = G_("types may not be defined in declare reduction type list");
37482 bool saved_colon_corrects_to_scope_p;
37483 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37484 parser->colon_corrects_to_scope_p = false;
37485 bool saved_colon_doesnt_start_class_def_p;
37486 saved_colon_doesnt_start_class_def_p
37487 = parser->colon_doesnt_start_class_def_p;
37488 parser->colon_doesnt_start_class_def_p = true;
37490 while (true)
37492 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37493 type = cp_parser_type_id (parser);
37494 if (type == error_mark_node)
37496 else if (ARITHMETIC_TYPE_P (type)
37497 && (orig_reduc_id == NULL_TREE
37498 || (TREE_CODE (type) != COMPLEX_TYPE
37499 && (id_equal (orig_reduc_id, "min")
37500 || id_equal (orig_reduc_id, "max")))))
37501 error_at (loc, "predeclared arithmetic type %qT in "
37502 "%<#pragma omp declare reduction%>", type);
37503 else if (TREE_CODE (type) == FUNCTION_TYPE
37504 || TREE_CODE (type) == METHOD_TYPE
37505 || TREE_CODE (type) == ARRAY_TYPE)
37506 error_at (loc, "function or array type %qT in "
37507 "%<#pragma omp declare reduction%>", type);
37508 else if (TREE_CODE (type) == REFERENCE_TYPE)
37509 error_at (loc, "reference type %qT in "
37510 "%<#pragma omp declare reduction%>", type);
37511 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37512 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37513 "%<#pragma omp declare reduction%>", type);
37514 else
37515 types.safe_push (type);
37517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37518 cp_lexer_consume_token (parser->lexer);
37519 else
37520 break;
37523 /* Restore the saved message. */
37524 parser->type_definition_forbidden_message = saved_message;
37525 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37526 parser->colon_doesnt_start_class_def_p
37527 = saved_colon_doesnt_start_class_def_p;
37529 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37530 || types.is_empty ())
37532 fail:
37533 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37534 goto done;
37537 first_token = cp_lexer_peek_token (parser->lexer);
37538 cp = NULL;
37539 errs = errorcount;
37540 FOR_EACH_VEC_ELT (types, i, type)
37542 tree fntype
37543 = build_function_type_list (void_type_node,
37544 cp_build_reference_type (type, false),
37545 NULL_TREE);
37546 tree this_reduc_id = reduc_id;
37547 if (!dependent_type_p (type))
37548 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37549 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37550 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37551 DECL_ARTIFICIAL (fndecl) = 1;
37552 DECL_EXTERNAL (fndecl) = 1;
37553 DECL_DECLARED_INLINE_P (fndecl) = 1;
37554 DECL_IGNORED_P (fndecl) = 1;
37555 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37556 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37557 DECL_ATTRIBUTES (fndecl)
37558 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37559 DECL_ATTRIBUTES (fndecl));
37560 if (processing_template_decl)
37561 fndecl = push_template_decl (fndecl);
37562 bool block_scope = false;
37563 tree block = NULL_TREE;
37564 if (current_function_decl)
37566 block_scope = true;
37567 DECL_CONTEXT (fndecl) = global_namespace;
37568 if (!processing_template_decl)
37569 pushdecl (fndecl);
37571 else if (current_class_type)
37573 if (cp == NULL)
37575 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37576 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37577 cp_lexer_consume_token (parser->lexer);
37578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37579 goto fail;
37580 cp = cp_token_cache_new (first_token,
37581 cp_lexer_peek_nth_token (parser->lexer,
37582 2));
37584 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37585 finish_member_declaration (fndecl);
37586 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37587 DECL_PENDING_INLINE_P (fndecl) = 1;
37588 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37589 continue;
37591 else
37593 DECL_CONTEXT (fndecl) = current_namespace;
37594 pushdecl (fndecl);
37596 if (!block_scope)
37597 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37598 else
37599 block = begin_omp_structured_block ();
37600 if (cp)
37602 cp_parser_push_lexer_for_tokens (parser, cp);
37603 parser->lexer->in_pragma = true;
37605 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37607 if (!block_scope)
37608 finish_function (/*inline_p=*/false);
37609 else
37610 DECL_CONTEXT (fndecl) = current_function_decl;
37611 if (cp)
37612 cp_parser_pop_lexer (parser);
37613 goto fail;
37615 if (cp)
37616 cp_parser_pop_lexer (parser);
37617 if (!block_scope)
37618 finish_function (/*inline_p=*/false);
37619 else
37621 DECL_CONTEXT (fndecl) = current_function_decl;
37622 block = finish_omp_structured_block (block);
37623 if (TREE_CODE (block) == BIND_EXPR)
37624 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37625 else if (TREE_CODE (block) == STATEMENT_LIST)
37626 DECL_SAVED_TREE (fndecl) = block;
37627 if (processing_template_decl)
37628 add_decl_expr (fndecl);
37630 cp_check_omp_declare_reduction (fndecl);
37631 if (cp == NULL && types.length () > 1)
37632 cp = cp_token_cache_new (first_token,
37633 cp_lexer_peek_nth_token (parser->lexer, 2));
37634 if (errs != errorcount)
37635 break;
37638 cp_parser_require_pragma_eol (parser, pragma_tok);
37640 done:
37641 /* Free any declarators allocated. */
37642 obstack_free (&declarator_obstack, p);
37645 /* OpenMP 4.0
37646 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37647 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37648 initializer-clause[opt] new-line
37649 #pragma omp declare target new-line */
37651 static bool
37652 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37653 enum pragma_context context)
37655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37658 const char *p = IDENTIFIER_POINTER (id);
37660 if (strcmp (p, "simd") == 0)
37662 cp_lexer_consume_token (parser->lexer);
37663 cp_parser_omp_declare_simd (parser, pragma_tok,
37664 context);
37665 return true;
37667 cp_ensure_no_omp_declare_simd (parser);
37668 if (strcmp (p, "reduction") == 0)
37670 cp_lexer_consume_token (parser->lexer);
37671 cp_parser_omp_declare_reduction (parser, pragma_tok,
37672 context);
37673 return false;
37675 if (!flag_openmp) /* flag_openmp_simd */
37677 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37678 return false;
37680 if (strcmp (p, "target") == 0)
37682 cp_lexer_consume_token (parser->lexer);
37683 cp_parser_omp_declare_target (parser, pragma_tok);
37684 return false;
37687 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37688 "or %<target%>");
37689 cp_parser_require_pragma_eol (parser, pragma_tok);
37690 return false;
37693 /* OpenMP 4.5:
37694 #pragma omp taskloop taskloop-clause[optseq] new-line
37695 for-loop
37697 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37698 for-loop */
37700 #define OMP_TASKLOOP_CLAUSE_MASK \
37701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37716 static tree
37717 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37718 char *p_name, omp_clause_mask mask, tree *cclauses,
37719 bool *if_p)
37721 tree clauses, sb, ret;
37722 unsigned int save;
37723 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37725 strcat (p_name, " taskloop");
37726 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37730 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37731 const char *p = IDENTIFIER_POINTER (id);
37733 if (strcmp (p, "simd") == 0)
37735 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37736 if (cclauses == NULL)
37737 cclauses = cclauses_buf;
37739 cp_lexer_consume_token (parser->lexer);
37740 if (!flag_openmp) /* flag_openmp_simd */
37741 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37742 cclauses, if_p);
37743 sb = begin_omp_structured_block ();
37744 save = cp_parser_begin_omp_structured_block (parser);
37745 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37746 cclauses, if_p);
37747 cp_parser_end_omp_structured_block (parser, save);
37748 tree body = finish_omp_structured_block (sb);
37749 if (ret == NULL)
37750 return ret;
37751 ret = make_node (OMP_TASKLOOP);
37752 TREE_TYPE (ret) = void_type_node;
37753 OMP_FOR_BODY (ret) = body;
37754 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37755 SET_EXPR_LOCATION (ret, loc);
37756 add_stmt (ret);
37757 return ret;
37760 if (!flag_openmp) /* flag_openmp_simd */
37762 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37763 return NULL_TREE;
37766 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37767 cclauses == NULL);
37768 if (cclauses)
37770 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37771 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37774 sb = begin_omp_structured_block ();
37775 save = cp_parser_begin_omp_structured_block (parser);
37777 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37778 if_p);
37780 cp_parser_end_omp_structured_block (parser, save);
37781 add_stmt (finish_omp_structured_block (sb));
37783 return ret;
37787 /* OpenACC 2.0:
37788 # pragma acc routine oacc-routine-clause[optseq] new-line
37789 function-definition
37791 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37794 #define OACC_ROUTINE_CLAUSE_MASK \
37795 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37801 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37802 component, which must resolve to a declared namespace-scope
37803 function. The clauses are either processed directly (for a named
37804 function), or defered until the immediatley following declaration
37805 is parsed. */
37807 static void
37808 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37809 enum pragma_context context)
37811 gcc_checking_assert (context == pragma_external);
37812 /* The checking for "another pragma following this one" in the "no optional
37813 '( name )'" case makes sure that we dont re-enter. */
37814 gcc_checking_assert (parser->oacc_routine == NULL);
37816 cp_oacc_routine_data data;
37817 data.error_seen = false;
37818 data.fndecl_seen = false;
37819 data.tokens = vNULL;
37820 data.clauses = NULL_TREE;
37821 data.loc = pragma_tok->location;
37822 /* It is safe to take the address of a local variable; it will only be
37823 used while this scope is live. */
37824 parser->oacc_routine = &data;
37826 /* Look for optional '( name )'. */
37827 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37829 matching_parens parens;
37830 parens.consume_open (parser); /* '(' */
37832 /* We parse the name as an id-expression. If it resolves to
37833 anything other than a non-overloaded function at namespace
37834 scope, it's an error. */
37835 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37836 tree name = cp_parser_id_expression (parser,
37837 /*template_keyword_p=*/false,
37838 /*check_dependency_p=*/false,
37839 /*template_p=*/NULL,
37840 /*declarator_p=*/false,
37841 /*optional_p=*/false);
37842 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37843 if (name != error_mark_node && decl == error_mark_node)
37844 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37846 if (decl == error_mark_node
37847 || !parens.require_close (parser))
37849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37850 parser->oacc_routine = NULL;
37851 return;
37854 data.clauses
37855 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37856 "#pragma acc routine",
37857 cp_lexer_peek_token (parser->lexer));
37859 if (decl && is_overloaded_fn (decl)
37860 && (TREE_CODE (decl) != FUNCTION_DECL
37861 || DECL_FUNCTION_TEMPLATE_P (decl)))
37863 error_at (name_loc,
37864 "%<#pragma acc routine%> names a set of overloads");
37865 parser->oacc_routine = NULL;
37866 return;
37869 /* Perhaps we should use the same rule as declarations in different
37870 namespaces? */
37871 if (!DECL_NAMESPACE_SCOPE_P (decl))
37873 error_at (name_loc,
37874 "%qD does not refer to a namespace scope function", decl);
37875 parser->oacc_routine = NULL;
37876 return;
37879 if (TREE_CODE (decl) != FUNCTION_DECL)
37881 error_at (name_loc, "%qD does not refer to a function", decl);
37882 parser->oacc_routine = NULL;
37883 return;
37886 cp_finalize_oacc_routine (parser, decl, false);
37887 parser->oacc_routine = NULL;
37889 else /* No optional '( name )'. */
37891 /* Store away all pragma tokens. */
37892 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37893 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37894 cp_lexer_consume_token (parser->lexer);
37895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37896 parser->oacc_routine->error_seen = true;
37897 cp_parser_require_pragma_eol (parser, pragma_tok);
37898 struct cp_token_cache *cp
37899 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37900 parser->oacc_routine->tokens.safe_push (cp);
37902 /* Emit a helpful diagnostic if there's another pragma following this
37903 one. */
37904 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37906 cp_ensure_no_oacc_routine (parser);
37907 data.tokens.release ();
37908 /* ..., and then just keep going. */
37909 return;
37912 /* We only have to consider the pragma_external case here. */
37913 cp_parser_declaration (parser);
37914 if (parser->oacc_routine
37915 && !parser->oacc_routine->fndecl_seen)
37916 cp_ensure_no_oacc_routine (parser);
37917 else
37918 parser->oacc_routine = NULL;
37919 data.tokens.release ();
37923 /* Finalize #pragma acc routine clauses after direct declarator has
37924 been parsed. */
37926 static tree
37927 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37929 struct cp_token_cache *ce;
37930 cp_oacc_routine_data *data = parser->oacc_routine;
37932 if (!data->error_seen && data->fndecl_seen)
37934 error_at (data->loc,
37935 "%<#pragma acc routine%> not immediately followed by "
37936 "a single function declaration or definition");
37937 data->error_seen = true;
37939 if (data->error_seen)
37940 return attrs;
37942 gcc_checking_assert (data->tokens.length () == 1);
37943 ce = data->tokens[0];
37945 cp_parser_push_lexer_for_tokens (parser, ce);
37946 parser->lexer->in_pragma = true;
37947 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37949 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37950 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37951 parser->oacc_routine->clauses
37952 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37953 "#pragma acc routine", pragma_tok);
37954 cp_parser_pop_lexer (parser);
37955 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37956 fndecl_seen. */
37958 return attrs;
37961 /* Apply any saved OpenACC routine clauses to a just-parsed
37962 declaration. */
37964 static void
37965 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37967 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37969 /* Keep going if we're in error reporting mode. */
37970 if (parser->oacc_routine->error_seen
37971 || fndecl == error_mark_node)
37972 return;
37974 if (parser->oacc_routine->fndecl_seen)
37976 error_at (parser->oacc_routine->loc,
37977 "%<#pragma acc routine%> not immediately followed by"
37978 " a single function declaration or definition");
37979 parser->oacc_routine = NULL;
37980 return;
37982 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37984 cp_ensure_no_oacc_routine (parser);
37985 return;
37988 if (oacc_get_fn_attrib (fndecl))
37990 error_at (parser->oacc_routine->loc,
37991 "%<#pragma acc routine%> already applied to %qD", fndecl);
37992 parser->oacc_routine = NULL;
37993 return;
37996 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37998 error_at (parser->oacc_routine->loc,
37999 TREE_USED (fndecl)
38000 ? G_("%<#pragma acc routine%> must be applied before use")
38001 : G_("%<#pragma acc routine%> must be applied before "
38002 "definition"));
38003 parser->oacc_routine = NULL;
38004 return;
38007 /* Process the routine's dimension clauses. */
38008 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38009 oacc_replace_fn_attrib (fndecl, dims);
38011 /* Add an "omp declare target" attribute. */
38012 DECL_ATTRIBUTES (fndecl)
38013 = tree_cons (get_identifier ("omp declare target"),
38014 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38016 /* Don't unset parser->oacc_routine here: we may still need it to
38017 diagnose wrong usage. But, remember that we've used this "#pragma acc
38018 routine". */
38019 parser->oacc_routine->fndecl_seen = true;
38023 /* Main entry point to OpenMP statement pragmas. */
38025 static void
38026 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38028 tree stmt;
38029 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38030 omp_clause_mask mask (0);
38032 switch (cp_parser_pragma_kind (pragma_tok))
38034 case PRAGMA_OACC_ATOMIC:
38035 cp_parser_omp_atomic (parser, pragma_tok);
38036 return;
38037 case PRAGMA_OACC_CACHE:
38038 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38039 break;
38040 case PRAGMA_OACC_DATA:
38041 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38042 break;
38043 case PRAGMA_OACC_ENTER_DATA:
38044 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38045 break;
38046 case PRAGMA_OACC_EXIT_DATA:
38047 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38048 break;
38049 case PRAGMA_OACC_HOST_DATA:
38050 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38051 break;
38052 case PRAGMA_OACC_KERNELS:
38053 case PRAGMA_OACC_PARALLEL:
38054 strcpy (p_name, "#pragma acc");
38055 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38056 if_p);
38057 break;
38058 case PRAGMA_OACC_LOOP:
38059 strcpy (p_name, "#pragma acc");
38060 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38061 if_p);
38062 break;
38063 case PRAGMA_OACC_UPDATE:
38064 stmt = cp_parser_oacc_update (parser, pragma_tok);
38065 break;
38066 case PRAGMA_OACC_WAIT:
38067 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38068 break;
38069 case PRAGMA_OMP_ATOMIC:
38070 cp_parser_omp_atomic (parser, pragma_tok);
38071 return;
38072 case PRAGMA_OMP_CRITICAL:
38073 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38074 break;
38075 case PRAGMA_OMP_DISTRIBUTE:
38076 strcpy (p_name, "#pragma omp");
38077 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38078 if_p);
38079 break;
38080 case PRAGMA_OMP_FOR:
38081 strcpy (p_name, "#pragma omp");
38082 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38083 if_p);
38084 break;
38085 case PRAGMA_OMP_MASTER:
38086 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38087 break;
38088 case PRAGMA_OMP_PARALLEL:
38089 strcpy (p_name, "#pragma omp");
38090 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38091 if_p);
38092 break;
38093 case PRAGMA_OMP_SECTIONS:
38094 strcpy (p_name, "#pragma omp");
38095 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38096 break;
38097 case PRAGMA_OMP_SIMD:
38098 strcpy (p_name, "#pragma omp");
38099 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38100 if_p);
38101 break;
38102 case PRAGMA_OMP_SINGLE:
38103 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38104 break;
38105 case PRAGMA_OMP_TASK:
38106 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38107 break;
38108 case PRAGMA_OMP_TASKGROUP:
38109 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38110 break;
38111 case PRAGMA_OMP_TASKLOOP:
38112 strcpy (p_name, "#pragma omp");
38113 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38114 if_p);
38115 break;
38116 case PRAGMA_OMP_TEAMS:
38117 strcpy (p_name, "#pragma omp");
38118 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38119 if_p);
38120 break;
38121 default:
38122 gcc_unreachable ();
38125 protected_set_expr_location (stmt, pragma_tok->location);
38128 /* Transactional Memory parsing routines. */
38130 /* Parse a transaction attribute.
38132 txn-attribute:
38133 attribute
38134 [ [ identifier ] ]
38136 We use this instead of cp_parser_attributes_opt for transactions to avoid
38137 the pedwarn in C++98 mode. */
38139 static tree
38140 cp_parser_txn_attribute_opt (cp_parser *parser)
38142 cp_token *token;
38143 tree attr_name, attr = NULL;
38145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38146 return cp_parser_attributes_opt (parser);
38148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38149 return NULL_TREE;
38150 cp_lexer_consume_token (parser->lexer);
38151 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38152 goto error1;
38154 token = cp_lexer_peek_token (parser->lexer);
38155 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38157 token = cp_lexer_consume_token (parser->lexer);
38159 attr_name = (token->type == CPP_KEYWORD
38160 /* For keywords, use the canonical spelling,
38161 not the parsed identifier. */
38162 ? ridpointers[(int) token->keyword]
38163 : token->u.value);
38164 attr = build_tree_list (attr_name, NULL_TREE);
38166 else
38167 cp_parser_error (parser, "expected identifier");
38169 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38170 error1:
38171 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38172 return attr;
38175 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38177 transaction-statement:
38178 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38179 compound-statement
38180 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38183 static tree
38184 cp_parser_transaction (cp_parser *parser, cp_token *token)
38186 unsigned char old_in = parser->in_transaction;
38187 unsigned char this_in = 1, new_in;
38188 enum rid keyword = token->keyword;
38189 tree stmt, attrs, noex;
38191 cp_lexer_consume_token (parser->lexer);
38193 if (keyword == RID_TRANSACTION_RELAXED
38194 || keyword == RID_SYNCHRONIZED)
38195 this_in |= TM_STMT_ATTR_RELAXED;
38196 else
38198 attrs = cp_parser_txn_attribute_opt (parser);
38199 if (attrs)
38200 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38203 /* Parse a noexcept specification. */
38204 if (keyword == RID_ATOMIC_NOEXCEPT)
38205 noex = boolean_true_node;
38206 else if (keyword == RID_ATOMIC_CANCEL)
38208 /* cancel-and-throw is unimplemented. */
38209 sorry ("atomic_cancel");
38210 noex = NULL_TREE;
38212 else
38213 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38215 /* Keep track if we're in the lexical scope of an outer transaction. */
38216 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38218 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38220 parser->in_transaction = new_in;
38221 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38222 parser->in_transaction = old_in;
38224 finish_transaction_stmt (stmt, NULL, this_in, noex);
38226 return stmt;
38229 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38231 transaction-expression:
38232 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38233 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38236 static tree
38237 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38239 unsigned char old_in = parser->in_transaction;
38240 unsigned char this_in = 1;
38241 cp_token *token;
38242 tree expr, noex;
38243 bool noex_expr;
38244 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38246 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38247 || keyword == RID_TRANSACTION_RELAXED);
38249 if (!flag_tm)
38250 error_at (loc,
38251 keyword == RID_TRANSACTION_RELAXED
38252 ? G_("%<__transaction_relaxed%> without transactional memory "
38253 "support enabled")
38254 : G_("%<__transaction_atomic%> without transactional memory "
38255 "support enabled"));
38257 token = cp_parser_require_keyword (parser, keyword,
38258 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38259 : RT_TRANSACTION_RELAXED));
38260 gcc_assert (token != NULL);
38262 if (keyword == RID_TRANSACTION_RELAXED)
38263 this_in |= TM_STMT_ATTR_RELAXED;
38265 /* Set this early. This might mean that we allow transaction_cancel in
38266 an expression that we find out later actually has to be a constexpr.
38267 However, we expect that cxx_constant_value will be able to deal with
38268 this; also, if the noexcept has no constexpr, then what we parse next
38269 really is a transaction's body. */
38270 parser->in_transaction = this_in;
38272 /* Parse a noexcept specification. */
38273 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38274 true);
38276 if (!noex || !noex_expr
38277 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38279 matching_parens parens;
38280 parens.require_open (parser);
38282 expr = cp_parser_expression (parser);
38283 expr = finish_parenthesized_expr (expr);
38285 parens.require_close (parser);
38287 else
38289 /* The only expression that is available got parsed for the noexcept
38290 already. noexcept is true then. */
38291 expr = noex;
38292 noex = boolean_true_node;
38295 expr = build_transaction_expr (token->location, expr, this_in, noex);
38296 parser->in_transaction = old_in;
38298 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38299 return error_mark_node;
38301 return (flag_tm ? expr : error_mark_node);
38304 /* Parse a function-transaction-block.
38306 function-transaction-block:
38307 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38308 function-body
38309 __transaction_atomic txn-attribute[opt] function-try-block
38310 __transaction_relaxed ctor-initializer[opt] function-body
38311 __transaction_relaxed function-try-block
38314 static void
38315 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38317 unsigned char old_in = parser->in_transaction;
38318 unsigned char new_in = 1;
38319 tree compound_stmt, stmt, attrs;
38320 cp_token *token;
38322 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38323 || keyword == RID_TRANSACTION_RELAXED);
38324 token = cp_parser_require_keyword (parser, keyword,
38325 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38326 : RT_TRANSACTION_RELAXED));
38327 gcc_assert (token != NULL);
38329 if (keyword == RID_TRANSACTION_RELAXED)
38330 new_in |= TM_STMT_ATTR_RELAXED;
38331 else
38333 attrs = cp_parser_txn_attribute_opt (parser);
38334 if (attrs)
38335 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38338 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38340 parser->in_transaction = new_in;
38342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38343 cp_parser_function_try_block (parser);
38344 else
38345 cp_parser_ctor_initializer_opt_and_function_body
38346 (parser, /*in_function_try_block=*/false);
38348 parser->in_transaction = old_in;
38350 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38353 /* Parse a __transaction_cancel statement.
38355 cancel-statement:
38356 __transaction_cancel txn-attribute[opt] ;
38357 __transaction_cancel txn-attribute[opt] throw-expression ;
38359 ??? Cancel and throw is not yet implemented. */
38361 static tree
38362 cp_parser_transaction_cancel (cp_parser *parser)
38364 cp_token *token;
38365 bool is_outer = false;
38366 tree stmt, attrs;
38368 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38369 RT_TRANSACTION_CANCEL);
38370 gcc_assert (token != NULL);
38372 attrs = cp_parser_txn_attribute_opt (parser);
38373 if (attrs)
38374 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38376 /* ??? Parse cancel-and-throw here. */
38378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38380 if (!flag_tm)
38382 error_at (token->location, "%<__transaction_cancel%> without "
38383 "transactional memory support enabled");
38384 return error_mark_node;
38386 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38388 error_at (token->location, "%<__transaction_cancel%> within a "
38389 "%<__transaction_relaxed%>");
38390 return error_mark_node;
38392 else if (is_outer)
38394 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38395 && !is_tm_may_cancel_outer (current_function_decl))
38397 error_at (token->location, "outer %<__transaction_cancel%> not "
38398 "within outer %<__transaction_atomic%>");
38399 error_at (token->location,
38400 " or a %<transaction_may_cancel_outer%> function");
38401 return error_mark_node;
38404 else if (parser->in_transaction == 0)
38406 error_at (token->location, "%<__transaction_cancel%> not within "
38407 "%<__transaction_atomic%>");
38408 return error_mark_node;
38411 stmt = build_tm_abort_call (token->location, is_outer);
38412 add_stmt (stmt);
38414 return stmt;
38417 /* The parser. */
38419 static GTY (()) cp_parser *the_parser;
38422 /* Special handling for the first token or line in the file. The first
38423 thing in the file might be #pragma GCC pch_preprocess, which loads a
38424 PCH file, which is a GC collection point. So we need to handle this
38425 first pragma without benefit of an existing lexer structure.
38427 Always returns one token to the caller in *FIRST_TOKEN. This is
38428 either the true first token of the file, or the first token after
38429 the initial pragma. */
38431 static void
38432 cp_parser_initial_pragma (cp_token *first_token)
38434 tree name = NULL;
38436 cp_lexer_get_preprocessor_token (NULL, first_token);
38437 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38438 return;
38440 cp_lexer_get_preprocessor_token (NULL, first_token);
38441 if (first_token->type == CPP_STRING)
38443 name = first_token->u.value;
38445 cp_lexer_get_preprocessor_token (NULL, first_token);
38446 if (first_token->type != CPP_PRAGMA_EOL)
38447 error_at (first_token->location,
38448 "junk at end of %<#pragma GCC pch_preprocess%>");
38450 else
38451 error_at (first_token->location, "expected string literal");
38453 /* Skip to the end of the pragma. */
38454 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38455 cp_lexer_get_preprocessor_token (NULL, first_token);
38457 /* Now actually load the PCH file. */
38458 if (name)
38459 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38461 /* Read one more token to return to our caller. We have to do this
38462 after reading the PCH file in, since its pointers have to be
38463 live. */
38464 cp_lexer_get_preprocessor_token (NULL, first_token);
38467 /* Parse a pragma GCC ivdep. */
38469 static bool
38470 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38473 return true;
38476 /* Parse a pragma GCC unroll. */
38478 static unsigned short
38479 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38481 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38482 tree expr = cp_parser_constant_expression (parser);
38483 unsigned short unroll;
38484 expr = maybe_constant_value (expr);
38485 HOST_WIDE_INT lunroll = 0;
38486 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38487 || TREE_CODE (expr) != INTEGER_CST
38488 || (lunroll = tree_to_shwi (expr)) < 0
38489 || lunroll >= USHRT_MAX)
38491 error_at (location, "%<#pragma GCC unroll%> requires an"
38492 " assignment-expression that evaluates to a non-negative"
38493 " integral constant less than %u", USHRT_MAX);
38494 unroll = 0;
38496 else
38498 unroll = (unsigned short)lunroll;
38499 if (unroll == 0)
38500 unroll = 1;
38502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38503 return unroll;
38506 /* Normal parsing of a pragma token. Here we can (and must) use the
38507 regular lexer. */
38509 static bool
38510 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38512 cp_token *pragma_tok;
38513 unsigned int id;
38514 tree stmt;
38515 bool ret;
38517 pragma_tok = cp_lexer_consume_token (parser->lexer);
38518 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38519 parser->lexer->in_pragma = true;
38521 id = cp_parser_pragma_kind (pragma_tok);
38522 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38523 cp_ensure_no_omp_declare_simd (parser);
38524 switch (id)
38526 case PRAGMA_GCC_PCH_PREPROCESS:
38527 error_at (pragma_tok->location,
38528 "%<#pragma GCC pch_preprocess%> must be first");
38529 break;
38531 case PRAGMA_OMP_BARRIER:
38532 switch (context)
38534 case pragma_compound:
38535 cp_parser_omp_barrier (parser, pragma_tok);
38536 return false;
38537 case pragma_stmt:
38538 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38539 "used in compound statements", "omp barrier");
38540 break;
38541 default:
38542 goto bad_stmt;
38544 break;
38546 case PRAGMA_OMP_FLUSH:
38547 switch (context)
38549 case pragma_compound:
38550 cp_parser_omp_flush (parser, pragma_tok);
38551 return false;
38552 case pragma_stmt:
38553 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38554 "used in compound statements", "omp flush");
38555 break;
38556 default:
38557 goto bad_stmt;
38559 break;
38561 case PRAGMA_OMP_TASKWAIT:
38562 switch (context)
38564 case pragma_compound:
38565 cp_parser_omp_taskwait (parser, pragma_tok);
38566 return false;
38567 case pragma_stmt:
38568 error_at (pragma_tok->location,
38569 "%<#pragma %s%> may only be used in compound statements",
38570 "omp taskwait");
38571 break;
38572 default:
38573 goto bad_stmt;
38575 break;
38577 case PRAGMA_OMP_TASKYIELD:
38578 switch (context)
38580 case pragma_compound:
38581 cp_parser_omp_taskyield (parser, pragma_tok);
38582 return false;
38583 case pragma_stmt:
38584 error_at (pragma_tok->location,
38585 "%<#pragma %s%> may only be used in compound statements",
38586 "omp taskyield");
38587 break;
38588 default:
38589 goto bad_stmt;
38591 break;
38593 case PRAGMA_OMP_CANCEL:
38594 switch (context)
38596 case pragma_compound:
38597 cp_parser_omp_cancel (parser, pragma_tok);
38598 return false;
38599 case pragma_stmt:
38600 error_at (pragma_tok->location,
38601 "%<#pragma %s%> may only be used in compound statements",
38602 "omp cancel");
38603 break;
38604 default:
38605 goto bad_stmt;
38607 break;
38609 case PRAGMA_OMP_CANCELLATION_POINT:
38610 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38611 return false;
38613 case PRAGMA_OMP_THREADPRIVATE:
38614 cp_parser_omp_threadprivate (parser, pragma_tok);
38615 return false;
38617 case PRAGMA_OMP_DECLARE:
38618 return cp_parser_omp_declare (parser, pragma_tok, context);
38620 case PRAGMA_OACC_DECLARE:
38621 cp_parser_oacc_declare (parser, pragma_tok);
38622 return false;
38624 case PRAGMA_OACC_ENTER_DATA:
38625 if (context == pragma_stmt)
38627 error_at (pragma_tok->location,
38628 "%<#pragma %s%> may only be used in compound statements",
38629 "acc enter data");
38630 break;
38632 else if (context != pragma_compound)
38633 goto bad_stmt;
38634 cp_parser_omp_construct (parser, pragma_tok, if_p);
38635 return true;
38637 case PRAGMA_OACC_EXIT_DATA:
38638 if (context == pragma_stmt)
38640 error_at (pragma_tok->location,
38641 "%<#pragma %s%> may only be used in compound statements",
38642 "acc exit data");
38643 break;
38645 else if (context != pragma_compound)
38646 goto bad_stmt;
38647 cp_parser_omp_construct (parser, pragma_tok, if_p);
38648 return true;
38650 case PRAGMA_OACC_ROUTINE:
38651 if (context != pragma_external)
38653 error_at (pragma_tok->location,
38654 "%<#pragma acc routine%> must be at file scope");
38655 break;
38657 cp_parser_oacc_routine (parser, pragma_tok, context);
38658 return false;
38660 case PRAGMA_OACC_UPDATE:
38661 if (context == pragma_stmt)
38663 error_at (pragma_tok->location,
38664 "%<#pragma %s%> may only be used in compound statements",
38665 "acc update");
38666 break;
38668 else if (context != pragma_compound)
38669 goto bad_stmt;
38670 cp_parser_omp_construct (parser, pragma_tok, if_p);
38671 return true;
38673 case PRAGMA_OACC_WAIT:
38674 if (context == pragma_stmt)
38676 error_at (pragma_tok->location,
38677 "%<#pragma %s%> may only be used in compound statements",
38678 "acc wait");
38679 break;
38681 else if (context != pragma_compound)
38682 goto bad_stmt;
38683 cp_parser_omp_construct (parser, pragma_tok, if_p);
38684 return true;
38686 case PRAGMA_OACC_ATOMIC:
38687 case PRAGMA_OACC_CACHE:
38688 case PRAGMA_OACC_DATA:
38689 case PRAGMA_OACC_HOST_DATA:
38690 case PRAGMA_OACC_KERNELS:
38691 case PRAGMA_OACC_PARALLEL:
38692 case PRAGMA_OACC_LOOP:
38693 case PRAGMA_OMP_ATOMIC:
38694 case PRAGMA_OMP_CRITICAL:
38695 case PRAGMA_OMP_DISTRIBUTE:
38696 case PRAGMA_OMP_FOR:
38697 case PRAGMA_OMP_MASTER:
38698 case PRAGMA_OMP_PARALLEL:
38699 case PRAGMA_OMP_SECTIONS:
38700 case PRAGMA_OMP_SIMD:
38701 case PRAGMA_OMP_SINGLE:
38702 case PRAGMA_OMP_TASK:
38703 case PRAGMA_OMP_TASKGROUP:
38704 case PRAGMA_OMP_TASKLOOP:
38705 case PRAGMA_OMP_TEAMS:
38706 if (context != pragma_stmt && context != pragma_compound)
38707 goto bad_stmt;
38708 stmt = push_omp_privatization_clauses (false);
38709 cp_parser_omp_construct (parser, pragma_tok, if_p);
38710 pop_omp_privatization_clauses (stmt);
38711 return true;
38713 case PRAGMA_OMP_ORDERED:
38714 if (context != pragma_stmt && context != pragma_compound)
38715 goto bad_stmt;
38716 stmt = push_omp_privatization_clauses (false);
38717 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38718 pop_omp_privatization_clauses (stmt);
38719 return ret;
38721 case PRAGMA_OMP_TARGET:
38722 if (context != pragma_stmt && context != pragma_compound)
38723 goto bad_stmt;
38724 stmt = push_omp_privatization_clauses (false);
38725 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38726 pop_omp_privatization_clauses (stmt);
38727 return ret;
38729 case PRAGMA_OMP_END_DECLARE_TARGET:
38730 cp_parser_omp_end_declare_target (parser, pragma_tok);
38731 return false;
38733 case PRAGMA_OMP_SECTION:
38734 error_at (pragma_tok->location,
38735 "%<#pragma omp section%> may only be used in "
38736 "%<#pragma omp sections%> construct");
38737 break;
38739 case PRAGMA_IVDEP:
38741 if (context == pragma_external)
38743 error_at (pragma_tok->location,
38744 "%<#pragma GCC ivdep%> must be inside a function");
38745 break;
38747 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38748 unsigned short unroll;
38749 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38750 if (tok->type == CPP_PRAGMA
38751 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38753 tok = cp_lexer_consume_token (parser->lexer);
38754 unroll = cp_parser_pragma_unroll (parser, tok);
38755 tok = cp_lexer_peek_token (the_parser->lexer);
38757 else
38758 unroll = 0;
38759 if (tok->type != CPP_KEYWORD
38760 || (tok->keyword != RID_FOR
38761 && tok->keyword != RID_WHILE
38762 && tok->keyword != RID_DO))
38764 cp_parser_error (parser, "for, while or do statement expected");
38765 return false;
38767 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38768 return true;
38771 case PRAGMA_UNROLL:
38773 if (context == pragma_external)
38775 error_at (pragma_tok->location,
38776 "%<#pragma GCC unroll%> must be inside a function");
38777 break;
38779 const unsigned short unroll
38780 = cp_parser_pragma_unroll (parser, pragma_tok);
38781 bool ivdep;
38782 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38783 if (tok->type == CPP_PRAGMA
38784 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38786 tok = cp_lexer_consume_token (parser->lexer);
38787 ivdep = cp_parser_pragma_ivdep (parser, tok);
38788 tok = cp_lexer_peek_token (the_parser->lexer);
38790 else
38791 ivdep = false;
38792 if (tok->type != CPP_KEYWORD
38793 || (tok->keyword != RID_FOR
38794 && tok->keyword != RID_WHILE
38795 && tok->keyword != RID_DO))
38797 cp_parser_error (parser, "for, while or do statement expected");
38798 return false;
38800 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38801 return true;
38804 default:
38805 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38806 c_invoke_pragma_handler (id);
38807 break;
38809 bad_stmt:
38810 cp_parser_error (parser, "expected declaration specifiers");
38811 break;
38814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38815 return false;
38818 /* The interface the pragma parsers have to the lexer. */
38820 enum cpp_ttype
38821 pragma_lex (tree *value, location_t *loc)
38823 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38824 enum cpp_ttype ret = tok->type;
38826 *value = tok->u.value;
38827 if (loc)
38828 *loc = tok->location;
38830 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38831 ret = CPP_EOF;
38832 else if (ret == CPP_STRING)
38833 *value = cp_parser_string_literal (the_parser, false, false);
38834 else
38836 if (ret == CPP_KEYWORD)
38837 ret = CPP_NAME;
38838 cp_lexer_consume_token (the_parser->lexer);
38841 return ret;
38845 /* External interface. */
38847 /* Parse one entire translation unit. */
38849 void
38850 c_parse_file (void)
38852 static bool already_called = false;
38854 if (already_called)
38855 fatal_error (input_location,
38856 "inter-module optimizations not implemented for C++");
38857 already_called = true;
38859 the_parser = cp_parser_new ();
38860 push_deferring_access_checks (flag_access_control
38861 ? dk_no_deferred : dk_no_check);
38862 cp_parser_translation_unit (the_parser);
38863 the_parser = NULL;
38866 /* Create an identifier for a generic parameter type (a synthesized
38867 template parameter implied by `auto' or a concept identifier). */
38869 static GTY(()) int generic_parm_count;
38870 static tree
38871 make_generic_type_name ()
38873 char buf[32];
38874 sprintf (buf, "auto:%d", ++generic_parm_count);
38875 return get_identifier (buf);
38878 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38879 (creating a new template parameter list if necessary). Returns the newly
38880 created template type parm. */
38882 static tree
38883 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38885 gcc_assert (current_binding_level->kind == sk_function_parms);
38887 /* Before committing to modifying any scope, if we're in an
38888 implicit template scope, and we're trying to synthesize a
38889 constrained parameter, try to find a previous parameter with
38890 the same name. This is the same-type rule for abbreviated
38891 function templates.
38893 NOTE: We can generate implicit parameters when tentatively
38894 parsing a nested name specifier, only to reject that parse
38895 later. However, matching the same template-id as part of a
38896 direct-declarator should generate an identical template
38897 parameter, so this rule will merge them. */
38898 if (parser->implicit_template_scope && constr)
38900 tree t = parser->implicit_template_parms;
38901 while (t)
38903 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38905 tree d = TREE_VALUE (t);
38906 if (TREE_CODE (d) == PARM_DECL)
38907 /* Return the TEMPLATE_PARM_INDEX. */
38908 d = DECL_INITIAL (d);
38909 return d;
38911 t = TREE_CHAIN (t);
38915 /* We are either continuing a function template that already contains implicit
38916 template parameters, creating a new fully-implicit function template, or
38917 extending an existing explicit function template with implicit template
38918 parameters. */
38920 cp_binding_level *const entry_scope = current_binding_level;
38922 bool become_template = false;
38923 cp_binding_level *parent_scope = 0;
38925 if (parser->implicit_template_scope)
38927 gcc_assert (parser->implicit_template_parms);
38929 current_binding_level = parser->implicit_template_scope;
38931 else
38933 /* Roll back to the existing template parameter scope (in the case of
38934 extending an explicit function template) or introduce a new template
38935 parameter scope ahead of the function parameter scope (or class scope
38936 in the case of out-of-line member definitions). The function scope is
38937 added back after template parameter synthesis below. */
38939 cp_binding_level *scope = entry_scope;
38941 while (scope->kind == sk_function_parms)
38943 parent_scope = scope;
38944 scope = scope->level_chain;
38946 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38948 /* If not defining a class, then any class scope is a scope level in
38949 an out-of-line member definition. In this case simply wind back
38950 beyond the first such scope to inject the template parameter list.
38951 Otherwise wind back to the class being defined. The latter can
38952 occur in class member friend declarations such as:
38954 class A {
38955 void foo (auto);
38957 class B {
38958 friend void A::foo (auto);
38961 The template parameter list synthesized for the friend declaration
38962 must be injected in the scope of 'B'. This can also occur in
38963 erroneous cases such as:
38965 struct A {
38966 struct B {
38967 void foo (auto);
38969 void B::foo (auto) {}
38972 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38973 but, nevertheless, the template parameter list synthesized for the
38974 declarator should be injected into the scope of 'A' as if the
38975 ill-formed template was specified explicitly. */
38977 while (scope->kind == sk_class && !scope->defining_class_p)
38979 parent_scope = scope;
38980 scope = scope->level_chain;
38984 current_binding_level = scope;
38986 if (scope->kind != sk_template_parms
38987 || !function_being_declared_is_template_p (parser))
38989 /* Introduce a new template parameter list for implicit template
38990 parameters. */
38992 become_template = true;
38994 parser->implicit_template_scope
38995 = begin_scope (sk_template_parms, NULL);
38997 ++processing_template_decl;
38999 parser->fully_implicit_function_template_p = true;
39000 ++parser->num_template_parameter_lists;
39002 else
39004 /* Synthesize implicit template parameters at the end of the explicit
39005 template parameter list. */
39007 gcc_assert (current_template_parms);
39009 parser->implicit_template_scope = scope;
39011 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39012 parser->implicit_template_parms
39013 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39017 /* Synthesize a new template parameter and track the current template
39018 parameter chain with implicit_template_parms. */
39020 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39021 tree synth_id = make_generic_type_name ();
39022 tree synth_tmpl_parm;
39023 bool non_type = false;
39025 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39026 synth_tmpl_parm
39027 = finish_template_type_parm (class_type_node, synth_id);
39028 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39029 synth_tmpl_parm
39030 = finish_constrained_template_template_parm (proto, synth_id);
39031 else
39033 synth_tmpl_parm = copy_decl (proto);
39034 DECL_NAME (synth_tmpl_parm) = synth_id;
39035 non_type = true;
39038 // Attach the constraint to the parm before processing.
39039 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39040 TREE_TYPE (node) = constr;
39041 tree new_parm
39042 = process_template_parm (parser->implicit_template_parms,
39043 input_location,
39044 node,
39045 /*non_type=*/non_type,
39046 /*param_pack=*/false);
39048 // Chain the new parameter to the list of implicit parameters.
39049 if (parser->implicit_template_parms)
39050 parser->implicit_template_parms
39051 = TREE_CHAIN (parser->implicit_template_parms);
39052 else
39053 parser->implicit_template_parms = new_parm;
39055 tree new_decl = get_local_decls ();
39056 if (non_type)
39057 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39058 new_decl = DECL_INITIAL (new_decl);
39060 /* If creating a fully implicit function template, start the new implicit
39061 template parameter list with this synthesized type, otherwise grow the
39062 current template parameter list. */
39064 if (become_template)
39066 parent_scope->level_chain = current_binding_level;
39068 tree new_parms = make_tree_vec (1);
39069 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39070 current_template_parms = tree_cons (size_int (processing_template_decl),
39071 new_parms, current_template_parms);
39073 else
39075 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39076 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39077 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39078 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39081 // If the new parameter was constrained, we need to add that to the
39082 // constraints in the template parameter list.
39083 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39085 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39086 reqs = conjoin_constraints (reqs, req);
39087 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39090 current_binding_level = entry_scope;
39092 return new_decl;
39095 /* Finish the declaration of a fully implicit function template. Such a
39096 template has no explicit template parameter list so has not been through the
39097 normal template head and tail processing. synthesize_implicit_template_parm
39098 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39099 provided if the declaration is a class member such that its template
39100 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39101 form is returned. Otherwise NULL_TREE is returned. */
39103 static tree
39104 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39106 gcc_assert (parser->fully_implicit_function_template_p);
39108 if (member_decl_opt && member_decl_opt != error_mark_node
39109 && DECL_VIRTUAL_P (member_decl_opt))
39111 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39112 "implicit templates may not be %<virtual%>");
39113 DECL_VIRTUAL_P (member_decl_opt) = false;
39116 if (member_decl_opt)
39117 member_decl_opt = finish_member_template_decl (member_decl_opt);
39118 end_template_decl ();
39120 parser->fully_implicit_function_template_p = false;
39121 --parser->num_template_parameter_lists;
39123 return member_decl_opt;
39126 /* Helper function for diagnostics that have complained about things
39127 being used with 'extern "C"' linkage.
39129 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39131 void
39132 maybe_show_extern_c_location (void)
39134 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39135 inform (the_parser->innermost_linkage_specification_location,
39136 "%<extern \"C\"%> linkage started here");
39139 #include "gt-cp-parser.h"