Import GCC-8 to a new vendor branch
[dragonfly.git] / contrib / gcc-8.0 / gcc / cp / parser.c
blobd8ce28a6d61a11b0a9a711218587ffd302ce7cca
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 return;
2869 gcc_rich_location richloc (input_location);
2871 bool added_matching_location = false;
2873 if (missing_token_desc != RT_NONE)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2899 if (missing_token_desc != RT_NONE)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2956 switch (desired)
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2976 else if (parser->scope == global_namespace)
2978 switch (desired)
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2995 else
2997 switch (desired)
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3027 return false;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3045 return true;
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3089 cp_token_position start = 0;
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3132 if (!parser->allow_non_integral_constant_expression_p)
3134 const char *msg = NULL;
3135 switch (thing)
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3241 return false;
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3321 tree b;
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 b = TREE_CHAIN (b))
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3347 if (field)
3348 break;
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3404 else if (TYPE_P (parser->scope))
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 else
3425 gcc_unreachable ();
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3497 if (recovering && or_ttype == CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499 return 0;
3501 while (true)
3503 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token->type == or_ttype && or_ttype != CPP_EOF
3507 && !brace_depth && !paren_depth && !square_depth)
3508 return -1;
3510 switch (token->type)
3512 case CPP_EOF:
3513 case CPP_PRAGMA_EOL:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3515 return 0;
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE:
3519 ++square_depth;
3520 break;
3521 case CPP_CLOSE_SQUARE:
3522 if (!square_depth--)
3523 return 0;
3524 break;
3526 case CPP_SEMICOLON:
3527 /* This matches the processing in skip_to_end_of_statement. */
3528 if (!brace_depth)
3529 return 0;
3530 break;
3532 case CPP_OPEN_BRACE:
3533 ++brace_depth;
3534 break;
3535 case CPP_CLOSE_BRACE:
3536 if (!brace_depth--)
3537 return 0;
3538 break;
3540 case CPP_OPEN_PAREN:
3541 if (!brace_depth)
3542 ++paren_depth;
3543 break;
3545 case CPP_CLOSE_PAREN:
3546 if (!brace_depth && !paren_depth--)
3548 if (consume_paren)
3549 cp_lexer_consume_token (parser->lexer);
3550 return 1;
3552 break;
3554 default:
3555 break;
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser->lexer);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570 bool recovering,
3571 bool or_comma,
3572 bool consume_paren)
3574 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576 ttype, consume_paren);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 unsigned nesting_depth = 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser->fully_implicit_function_template_p)
3590 abort_fully_implicit_template (parser);
3592 while (true)
3594 cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 switch (token->type)
3598 case CPP_EOF:
3599 case CPP_PRAGMA_EOL:
3600 /* If we've run out of tokens, stop. */
3601 return;
3603 case CPP_SEMICOLON:
3604 /* If the next token is a `;', we have reached the end of the
3605 statement. */
3606 if (!nesting_depth)
3607 return;
3608 break;
3610 case CPP_CLOSE_BRACE:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3614 { 3 + }
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth == 0)
3619 return;
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3623 That way given:
3625 void f g () { ... }
3626 typedef int I;
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3630 declaration. */
3631 if (--nesting_depth == 0)
3633 cp_lexer_consume_token (parser->lexer);
3634 return;
3636 break;
3638 case CPP_OPEN_BRACE:
3639 ++nesting_depth;
3640 break;
3642 default:
3643 break;
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser->lexer);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661 /* If there is additional (erroneous) input, skip to the end of
3662 the statement. */
3663 cp_parser_skip_to_end_of_statement (parser);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666 cp_lexer_consume_token (parser->lexer);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 int nesting_depth = 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser->fully_implicit_function_template_p)
3680 abort_fully_implicit_template (parser);
3682 while (nesting_depth >= 0)
3684 cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 switch (token->type)
3688 case CPP_EOF:
3689 case CPP_PRAGMA_EOL:
3690 /* If we've run out of tokens, stop. */
3691 return;
3693 case CPP_SEMICOLON:
3694 /* Stop if this is an unnested ';'. */
3695 if (!nesting_depth)
3696 nesting_depth = -1;
3697 break;
3699 case CPP_CLOSE_BRACE:
3700 /* Stop if this is an unnested '}', or closes the outermost
3701 nesting level. */
3702 nesting_depth--;
3703 if (nesting_depth < 0)
3704 return;
3705 if (!nesting_depth)
3706 nesting_depth = -1;
3707 break;
3709 case CPP_OPEN_BRACE:
3710 /* Nest. */
3711 nesting_depth++;
3712 break;
3714 default:
3715 break;
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser->lexer);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3725 false otherwise. */
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 unsigned nesting_depth = 0;
3732 while (true)
3734 cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 switch (token->type)
3738 case CPP_EOF:
3739 case CPP_PRAGMA_EOL:
3740 /* If we've run out of tokens, stop. */
3741 return false;
3743 case CPP_CLOSE_BRACE:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth-- == 0)
3747 return true;
3748 break;
3750 case CPP_OPEN_BRACE:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3753 ++nesting_depth;
3754 break;
3756 default:
3757 break;
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser->lexer);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3767 sequence. */
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3772 cp_token *token;
3774 parser->lexer->in_pragma = false;
3777 token = cp_lexer_consume_token (parser->lexer);
3778 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 parser->lexer->in_pragma = false;
3791 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801 location_t id_location)
3803 tree result;
3804 if (identifier_p (id))
3806 result = make_typename_type (parser->scope, id, typename_type,
3807 /*complain=*/tf_none);
3808 if (result == error_mark_node)
3809 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810 return result;
3812 return make_typename_type (parser->scope, id, typename_type, tf_error);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824 cp_cv_quals cv_qualifiers,
3825 cp_declarator *target,
3826 tree attributes)
3828 if (code == ERROR_MARK || target == cp_error_declarator)
3829 return cp_error_declarator;
3831 if (code == INDIRECT_REF)
3832 if (class_type == NULL_TREE)
3833 return make_pointer_declarator (cv_qualifiers, target, attributes);
3834 else
3835 return make_ptrmem_declarator (cv_qualifiers, class_type,
3836 target, attributes);
3837 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838 return make_reference_declarator (cv_qualifiers, target,
3839 false, attributes);
3840 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841 return make_reference_declarator (cv_qualifiers, target,
3842 true, attributes);
3843 gcc_unreachable ();
3846 /* Create a new C++ parser. */
3848 static cp_parser *
3849 cp_parser_new (void)
3851 cp_parser *parser;
3852 cp_lexer *lexer;
3853 unsigned i;
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer = cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862 binops_by_token[binops[i].token_type] = binops[i];
3864 parser = ggc_cleared_alloc<cp_parser> ();
3865 parser->lexer = lexer;
3866 parser->context = cp_parser_context_new (NULL);
3868 /* For now, we always accept GNU extensions. */
3869 parser->allow_gnu_extensions_p = 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3872 template-id. */
3873 parser->greater_than_is_operator_p = true;
3875 parser->default_arg_ok_p = true;
3877 /* We are not parsing a constant-expression. */
3878 parser->integral_constant_expression_p = false;
3879 parser->allow_non_integral_constant_expression_p = false;
3880 parser->non_integral_constant_expression_p = false;
3882 /* Local variable names are not forbidden. */
3883 parser->local_variables_forbidden_p = false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser->in_unbraced_linkage_specification_p = false;
3888 /* We are not processing a declarator. */
3889 parser->in_declarator_p = false;
3891 /* We are not processing a template-argument-list. */
3892 parser->in_template_argument_list_p = false;
3894 /* We are not in an iteration statement. */
3895 parser->in_statement = 0;
3897 /* We are not in a switch statement. */
3898 parser->in_switch_statement_p = false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser->in_type_id_in_expr_p = false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser->implicit_extern_c = false;
3906 /* String literals should be translated to the execution character set. */
3907 parser->translate_strings_p = true;
3909 /* We are not parsing a function body. */
3910 parser->in_function_body = false;
3912 /* We can correct until told otherwise. */
3913 parser->colon_corrects_to_scope_p = true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser);
3918 /* There are no classes being defined. */
3919 parser->num_classes_being_defined = 0;
3921 /* No template parameters apply. */
3922 parser->num_template_parameter_lists = 0;
3924 /* Special parsing data structures. */
3925 parser->omp_declare_simd = NULL;
3926 parser->oacc_routine = NULL;
3928 /* Not declaring an implicit function template. */
3929 parser->auto_is_implicit_function_template_parm_p = false;
3930 parser->fully_implicit_function_template_p = false;
3931 parser->implicit_template_parms = 0;
3932 parser->implicit_template_scope = 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser->prevent_constrained_type_specifiers = 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3940 return parser;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951 lexer->next = parser->lexer;
3952 parser->lexer = lexer;
3954 /* Move the current source position to that of the first token in the
3955 new lexer. */
3956 cp_lexer_set_source_position_from_token (lexer->next_token);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3964 cp_lexer *lexer = parser->lexer;
3965 parser->lexer = lexer->next;
3966 cp_lexer_destroy (lexer);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3976 identifier. */
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3981 cp_token *token;
3983 /* Look for the identifier. */
3984 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985 /* Return the value. */
3986 if (token)
3987 return cp_expr (token->u.value, token->location);
3988 else
3989 return error_mark_node;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3996 invalid here.
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008 bool lookup_udlit = true)
4010 tree value;
4011 size_t count;
4012 struct obstack str_ob;
4013 cpp_string str, istr, *strs;
4014 cp_token *tok;
4015 enum cpp_ttype type, curr_type;
4016 int have_suffix_p = 0;
4017 tree string_tree;
4018 tree suffix_id = NULL_TREE;
4019 bool curr_tok_is_userdef_p = false;
4021 tok = cp_lexer_peek_token (parser->lexer);
4022 if (!cp_parser_is_string_literal (tok))
4024 cp_parser_error (parser, "expected string-literal");
4025 return error_mark_node;
4028 location_t loc = tok->location;
4030 if (cpp_userdef_string_p (tok->type))
4032 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033 curr_type = cpp_userdef_string_remove_type (tok->type);
4034 curr_tok_is_userdef_p = true;
4036 else
4038 string_tree = tok->u.value;
4039 curr_type = tok->type;
4041 type = curr_type;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048 cp_lexer_consume_token (parser->lexer);
4050 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051 str.len = TREE_STRING_LENGTH (string_tree);
4052 count = 1;
4054 if (curr_tok_is_userdef_p)
4056 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057 have_suffix_p = 1;
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4060 else
4061 curr_type = tok->type;
4063 strs = &str;
4065 else
4067 location_t last_tok_loc = tok->location;
4068 gcc_obstack_init (&str_ob);
4069 count = 0;
4073 cp_lexer_consume_token (parser->lexer);
4074 count++;
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4078 if (curr_tok_is_userdef_p)
4080 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081 if (have_suffix_p == 0)
4083 suffix_id = curr_suffix_id;
4084 have_suffix_p = 1;
4086 else if (have_suffix_p == 1
4087 && curr_suffix_id != suffix_id)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id, curr_suffix_id);
4092 have_suffix_p = -1;
4094 curr_type = cpp_userdef_string_remove_type (tok->type);
4096 else
4097 curr_type = tok->type;
4099 if (type != curr_type)
4101 if (type == CPP_STRING)
4102 type = curr_type;
4103 else if (curr_type != CPP_STRING)
4105 rich_location rich_loc (line_table, tok->location);
4106 rich_loc.add_range (last_tok_loc, false);
4107 error_at (&rich_loc,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 last_tok_loc = tok->location;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (cpp_userdef_string_p (tok->type))
4120 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121 curr_type = cpp_userdef_string_remove_type (tok->type);
4122 curr_tok_is_userdef_p = true;
4124 else
4126 string_tree = tok->u.value;
4127 curr_type = tok->type;
4128 curr_tok_is_userdef_p = false;
4131 while (cp_parser_is_string_literal (tok));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 strs = (cpp_string *) obstack_finish (&str_ob);
4141 if (type != CPP_STRING && !wide_ok)
4143 cp_parser_error (parser, "a wide string is invalid in this context");
4144 type = CPP_STRING;
4147 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148 (parse_in, strs, count, &istr, type))
4150 value = build_string (istr.len, (const char *)istr.text);
4151 free (CONST_CAST (unsigned char *, istr.text));
4153 switch (type)
4155 default:
4156 case CPP_STRING:
4157 case CPP_UTF8STRING:
4158 TREE_TYPE (value) = char_array_type_node;
4159 break;
4160 case CPP_STRING16:
4161 TREE_TYPE (value) = char16_array_type_node;
4162 break;
4163 case CPP_STRING32:
4164 TREE_TYPE (value) = char32_array_type_node;
4165 break;
4166 case CPP_WSTRING:
4167 TREE_TYPE (value) = wchar_array_type_node;
4168 break;
4171 value = fix_string_type (value);
4173 if (have_suffix_p)
4175 tree literal = build_userdef_literal (suffix_id, value,
4176 OT_NONE, NULL_TREE);
4177 if (lookup_udlit)
4178 value = cp_parser_userdef_string_literal (literal);
4179 else
4180 value = literal;
4183 else
4184 /* cpp_interpret_string has issued an error. */
4185 value = error_mark_node;
4187 if (count > 1)
4188 obstack_free (&str_ob, 0);
4190 return cp_expr (value, loc);
4193 /* Look up a literal operator with the name and the exact arguments. */
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4198 tree decl;
4199 decl = lookup_name (name);
4200 if (!decl || !is_overloaded_fn (decl))
4201 return error_mark_node;
4203 for (lkp_iterator iter (decl); iter; ++iter)
4205 unsigned int ix;
4206 bool found = true;
4207 tree fn = *iter;
4208 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209 if (parmtypes != NULL_TREE)
4211 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 tree tparm = TREE_VALUE (parmtypes);
4215 tree targ = TREE_TYPE ((*args)[ix]);
4216 bool ptr = TYPE_PTR_P (tparm);
4217 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218 if ((ptr || arr || !same_type_p (tparm, targ))
4219 && (!ptr || !arr
4220 || !same_type_p (TREE_TYPE (tparm),
4221 TREE_TYPE (targ))))
4222 found = false;
4224 if (found
4225 && ix == vec_safe_length (args)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes == void_list_node)
4231 return decl;
4235 return error_mark_node;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4244 cp_token *token = cp_lexer_consume_token (parser->lexer);
4245 tree literal = token->u.value;
4246 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree decl, result;
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec<tree, va_gc> *args = make_tree_vector ();
4254 vec_safe_push (args, value);
4255 decl = lookup_literal_operator (name, args);
4256 if (!decl || decl == error_mark_node)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name, TREE_TYPE (value));
4260 release_tree_vector (args);
4261 return error_mark_node;
4263 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4271 static tree
4272 make_char_string_pack (tree value)
4274 tree charvec;
4275 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276 const char *str = TREE_STRING_POINTER (value);
4277 int i, len = TREE_STRING_LENGTH (value) - 1;
4278 tree argvec = make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec = make_tree_vec (len);
4282 for (i = 0; i < len; ++i)
4283 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 TREE_VEC_ELT (argvec, 0) = argpack;
4290 return argvec;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value);
4303 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304 int len = TREE_STRING_LENGTH (value) / sz - 1;
4305 tree argvec = make_tree_vec (2);
4307 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec = make_tree_vec (len);
4315 for (int i = 0; i < len; ++i)
4316 TREE_VEC_ELT (charvec, i)
4317 = double_int_to_tree (str_char_type_node,
4318 double_int::from_buffer (str + i * sz, sz));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 TREE_VEC_ELT (argvec, 1) = argpack;
4325 return argvec;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 cp_token *token = cp_lexer_consume_token (parser->lexer);
4335 tree literal = token->u.value;
4336 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337 tree value = USERDEF_LITERAL_VALUE (literal);
4338 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342 vec<tree, va_gc> *args;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args = make_tree_vector ();
4347 vec_safe_push (args, value);
4348 decl = lookup_literal_operator (name, args);
4349 if (decl && decl != error_mark_node)
4351 result = finish_call_expr (decl, &args, false, true,
4352 tf_warning_or_error);
4354 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 warning_at (token->location, OPT_Woverflow,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node);
4360 else
4362 if (overflow > 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node);
4366 else if (overflow < 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args);
4372 return result;
4374 release_tree_vector (args);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args = make_tree_vector ();
4380 vec_safe_push (args, num_string);
4381 decl = lookup_literal_operator (name, args);
4382 if (decl && decl != error_mark_node)
4384 result = finish_call_expr (decl, &args, false, true,
4385 tf_warning_or_error);
4386 release_tree_vector (args);
4387 return result;
4389 release_tree_vector (args);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args = make_tree_vector ();
4395 decl = lookup_literal_operator (name, args);
4396 if (decl && decl != error_mark_node)
4398 tree tmpl_args = make_char_string_pack (num_string);
4399 decl = lookup_template_function (decl, tmpl_args);
4400 result = finish_call_expr (decl, &args, false, true,
4401 tf_warning_or_error);
4402 release_tree_vector (args);
4403 return result;
4406 release_tree_vector (args);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411 bool i14 = (cxx_dialect > cxx11
4412 && (id_equal (suffix_id, "i")
4413 || id_equal (suffix_id, "if")
4414 || id_equal (suffix_id, "il")));
4415 diagnostic_t kind = DK_ERROR;
4416 int opt = 0;
4418 if (i14 && ext)
4420 tree cxlit = lookup_qualified_name (std_node,
4421 get_identifier ("complex_literals"),
4422 0, false, false);
4423 if (cxlit == error_mark_node)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4426 kind = DK_PEDWARN;
4427 opt = OPT_Wpedantic;
4431 bool complained
4432 = emit_diagnostic (kind, input_location, opt,
4433 "unable to find numeric literal operator %qD", name);
4435 if (!complained)
4436 /* Don't inform either. */;
4437 else if (i14)
4439 inform (token->location, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4441 "suffixes");
4442 if (ext)
4443 inform (token->location, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4446 else if (!ext)
4447 inform (token->location, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind == DK_ERROR)
4451 value = error_mark_node;
4452 else
4454 /* Use the built-in semantics. */
4455 tree type;
4456 if (id_equal (suffix_id, "i"))
4458 if (TREE_CODE (value) == INTEGER_CST)
4459 type = integer_type_node;
4460 else
4461 type = double_type_node;
4463 else if (id_equal (suffix_id, "if"))
4464 type = float_type_node;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type = long_double_type_node;
4468 value = build_complex (build_complex_type (type),
4469 fold_convert (type, integer_zero_node),
4470 fold_convert (type, value));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474 /* Avoid repeated diagnostics. */
4475 token->u.value = value;
4476 return value;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4481 as arguments. */
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4486 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488 tree value = USERDEF_LITERAL_VALUE (literal);
4489 int len = TREE_STRING_LENGTH (value)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491 tree decl, result;
4492 vec<tree, va_gc> *args;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args = make_tree_vector ();
4497 vec_safe_push (args, value);
4498 vec_safe_push (args, build_int_cst (size_type_node, len));
4499 decl = lookup_literal_operator (name, args);
4501 if (decl && decl != error_mark_node)
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 release_tree_vector (args);
4506 return result;
4508 release_tree_vector (args);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args = make_tree_vector ();
4514 decl = lookup_literal_operator (name, args);
4515 if (decl && decl != error_mark_node)
4517 tree tmpl_args = make_string_pack (value);
4518 decl = lookup_template_function (decl, tmpl_args);
4519 result = finish_call_expr (decl, &args, false, true,
4520 tf_warning_or_error);
4521 release_tree_vector (args);
4522 return result;
4524 release_tree_vector (args);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name, TREE_TYPE (value), size_type_node);
4528 return error_mark_node;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4536 translation-unit:
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4544 /* The address of the first non-permanent object on the declarator
4545 obstack. */
4546 static void *declarator_obstack_base;
4548 bool success;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator)
4553 gcc_obstack_init (&declarator_obstack);
4554 /* Create the error declarator. */
4555 cp_error_declarator = make_declarator (cdk_error);
4556 /* Create the empty parameter list. */
4557 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558 UNKNOWN_LOCATION);
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4563 cp_parser_declaration_seq_opt (parser);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser->lexer);
4570 parser->lexer = NULL;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser->implicit_extern_c)
4576 pop_lang_context ();
4577 parser->implicit_extern_c = false;
4580 /* Finish up. */
4581 finish_translation_unit ();
4583 success = true;
4585 else
4587 cp_parser_error (parser, "expected declaration");
4588 success = false;
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack)
4593 == declarator_obstack_base);
4595 /* All went well. */
4596 return success;
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4605 tsubst_flags_t complain = tf_warning_or_error;
4606 if (decltype_p)
4607 complain |= tf_decltype;
4608 return complain;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619 return 0;
4621 cp_parser_parse_tentatively (parser);
4622 cp_parser_commit_to_topmost_tentative_parse (parser);
4623 return cp_lexer_token_position (parser->lexer, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631 tree expr)
4633 if (!start)
4634 return;
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640 token->type = CPP_PREPARSED_EXPR;
4641 token->u.value = expr;
4642 token->keyword = RID_MAX;
4643 cp_lexer_purge_tokens_after (parser->lexer, start);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4653 cp_parser *parser;
4654 bool set;
4656 tentative_firewall (cp_parser *p): parser(p)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662 cp_parser_parse_tentatively (parser);
4663 cp_parser_commit_to_topmost_tentative_parse (parser);
4664 cp_parser_parse_tentatively (parser);
4668 ~tentative_firewall()
4670 if (set)
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err = cp_parser_error_occurred (parser);
4675 cp_parser_parse_definitely (parser);
4676 cp_parser_parse_definitely (parser);
4677 if (err)
4678 cp_parser_simulate_error (parser);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4688 problem. */
4690 template <typename traits_t>
4691 class token_pair
4693 public:
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4698 return true.
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser *parser)
4704 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 return cp_parser_require (parser, traits_t::open_token_type,
4706 traits_t::required_token_open);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token * consume_open (cp_parser *parser)
4714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715 gcc_assert (tok->type == traits_t::open_token_type);
4716 m_open_loc = tok->location;
4717 return tok;
4720 /* If the next token is the closing symbol for this pair, consume it
4721 and return it.
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token *require_close (cp_parser *parser) const
4727 return cp_parser_require (parser, traits_t::close_token_type,
4728 traits_t::required_token_close,
4729 m_open_loc);
4732 private:
4733 location_t m_open_loc;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741 static const enum required_token required_token_open = RT_OPEN_PAREN;
4742 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair<matching_paren_traits> matching_parens;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756 static const enum required_token required_token_open = RT_OPEN_BRACE;
4757 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 pairs of braces. */
4764 typedef token_pair<matching_brace_traits> matching_braces;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4773 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 /* Consume the '('. */
4776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777 matching_parens parens;
4778 parens.consume_open (parser);
4779 /* Start the statement-expression. */
4780 tree expr = begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783 /* Finish up. */
4784 expr = finish_stmt_expr (expr, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787 if (!parens.require_close (parser))
4788 cp_parser_skip_to_end_of_statement (parser);
4790 cp_parser_end_tentative_firewall (parser, start, expr);
4791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792 return cp_expr (expr, combined_loc);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4799 fold-operator:
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4812 switch (token->type)
4814 case CPP_PLUS: return PLUS_EXPR;
4815 case CPP_MINUS: return MINUS_EXPR;
4816 case CPP_MULT: return MULT_EXPR;
4817 case CPP_DIV: return TRUNC_DIV_EXPR;
4818 case CPP_MOD: return TRUNC_MOD_EXPR;
4819 case CPP_XOR: return BIT_XOR_EXPR;
4820 case CPP_AND: return BIT_AND_EXPR;
4821 case CPP_OR: return BIT_IOR_EXPR;
4822 case CPP_LSHIFT: return LSHIFT_EXPR;
4823 case CPP_RSHIFT: return RSHIFT_EXPR;
4825 case CPP_EQ: return -NOP_EXPR;
4826 case CPP_PLUS_EQ: return -PLUS_EXPR;
4827 case CPP_MINUS_EQ: return -MINUS_EXPR;
4828 case CPP_MULT_EQ: return -MULT_EXPR;
4829 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832 case CPP_AND_EQ: return -BIT_AND_EXPR;
4833 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 case CPP_EQ_EQ: return EQ_EXPR;
4838 case CPP_NOT_EQ: return NE_EXPR;
4839 case CPP_LESS: return LT_EXPR;
4840 case CPP_GREATER: return GT_EXPR;
4841 case CPP_LESS_EQ: return LE_EXPR;
4842 case CPP_GREATER_EQ: return GE_EXPR;
4844 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 case CPP_COMMA: return COMPOUND_EXPR;
4849 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850 case CPP_DEREF_STAR: return MEMBER_REF;
4852 default: return ERROR_MARK;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4860 static bool
4861 is_binary_op (tree_code code)
4863 switch (code)
4865 case PLUS_EXPR:
4866 case POINTER_PLUS_EXPR:
4867 case MINUS_EXPR:
4868 case MULT_EXPR:
4869 case TRUNC_DIV_EXPR:
4870 case TRUNC_MOD_EXPR:
4871 case BIT_XOR_EXPR:
4872 case BIT_AND_EXPR:
4873 case BIT_IOR_EXPR:
4874 case LSHIFT_EXPR:
4875 case RSHIFT_EXPR:
4877 case MODOP_EXPR:
4879 case EQ_EXPR:
4880 case NE_EXPR:
4881 case LE_EXPR:
4882 case GE_EXPR:
4883 case LT_EXPR:
4884 case GT_EXPR:
4886 case TRUTH_ANDIF_EXPR:
4887 case TRUTH_ORIF_EXPR:
4889 case COMPOUND_EXPR:
4891 case DOTSTAR_EXPR:
4892 case MEMBER_REF:
4893 return true;
4895 default:
4896 return false;
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4906 cp_token* token = cp_lexer_peek_token (parser->lexer);
4907 int code = cp_parser_fold_operator (token);
4908 if (code != ERROR_MARK)
4909 cp_lexer_consume_token (parser->lexer);
4910 return code;
4913 /* Parse a fold-expression.
4915 fold-expression:
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4925 cp_id_kind pidk;
4927 // Left fold.
4928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930 cp_lexer_consume_token (parser->lexer);
4931 int op = cp_parser_fold_operator (parser);
4932 if (op == ERROR_MARK)
4934 cp_parser_error (parser, "expected binary operator");
4935 return error_mark_node;
4938 tree expr = cp_parser_cast_expression (parser, false, false,
4939 false, &pidk);
4940 if (expr == error_mark_node)
4941 return error_mark_node;
4942 return finish_left_unary_fold_expr (expr, op);
4945 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946 int op = cp_parser_fold_operator (parser);
4947 if (op == ERROR_MARK)
4949 cp_parser_error (parser, "expected binary operator");
4950 return error_mark_node;
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955 cp_parser_error (parser, "expected ...");
4956 return error_mark_node;
4958 cp_lexer_consume_token (parser->lexer);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1)))
4966 error_at (location_of (expr1),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1)
4970 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971 error_at (location_of (expr1),
4972 "conditional expression in operand of fold-expression");
4974 // Right fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976 return finish_right_unary_fold_expr (expr1, op);
4978 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980 cp_parser_error (parser, "mismatched operator in fold-expression");
4981 return error_mark_node;
4983 cp_lexer_consume_token (parser->lexer);
4985 // Binary left or right fold.
4986 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987 if (expr2 == error_mark_node)
4988 return error_mark_node;
4989 return finish_binary_fold_expr (expr1, expr2, op);
4992 /* Parse a primary-expression.
4994 primary-expression:
4995 literal
4996 this
4997 ( expression )
4998 id-expression
4999 lambda-expression (C++11)
5001 GNU Extensions:
5003 primary-expression:
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5008 C++ Extensions:
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5032 primary-expression:
5033 objc-expression
5035 literal:
5036 __null
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048 bool address_p,
5049 bool cast_p,
5050 bool template_arg_p,
5051 bool decltype_p,
5052 cp_id_kind *idk)
5054 cp_token *token = NULL;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk = CP_ID_KIND_NONE;
5059 /* Peek at the next token. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 switch ((int) token->type)
5063 /* literal:
5064 integer-literal
5065 character-literal
5066 floating-literal
5067 string-literal
5068 boolean-literal
5069 pointer-literal
5070 user-defined-literal */
5071 case CPP_CHAR:
5072 case CPP_CHAR16:
5073 case CPP_CHAR32:
5074 case CPP_WCHAR:
5075 case CPP_UTF8CHAR:
5076 case CPP_NUMBER:
5077 case CPP_PREPARSED_EXPR:
5078 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079 return cp_parser_userdef_numeric_literal (parser);
5080 token = cp_lexer_consume_token (parser->lexer);
5081 if (TREE_CODE (token->u.value) == FIXED_CST)
5083 error_at (token->location,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token->u.value) == REAL_CST
5091 && parser->integral_constant_expression_p
5092 && pedantic)
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5097 if (cast_p)
5099 cp_token *next_token;
5101 next_token = cp_lexer_peek_token (parser->lexer);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token->type != CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token->type != CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token->type != CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token->type != CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token->type != CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token->type != CPP_GREATER
5115 || parser->greater_than_is_operator_p)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token->type != CPP_RSHIFT
5119 || (cxx_dialect == cxx98)
5120 || parser->greater_than_is_operator_p))
5121 cast_p = false;
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5128 if (!cast_p)
5129 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 return cp_expr (token->u.value, token->location);
5133 case CPP_CHAR_USERDEF:
5134 case CPP_CHAR16_USERDEF:
5135 case CPP_CHAR32_USERDEF:
5136 case CPP_WCHAR_USERDEF:
5137 case CPP_UTF8CHAR_USERDEF:
5138 return cp_parser_userdef_char_literal (parser);
5140 case CPP_STRING:
5141 case CPP_STRING16:
5142 case CPP_STRING32:
5143 case CPP_WSTRING:
5144 case CPP_UTF8STRING:
5145 case CPP_STRING_USERDEF:
5146 case CPP_STRING16_USERDEF:
5147 case CPP_STRING32_USERDEF:
5148 case CPP_WSTRING_USERDEF:
5149 case CPP_UTF8STRING_USERDEF:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser,
5154 parser->translate_strings_p,
5155 true);
5157 case CPP_OPEN_PAREN:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser)
5161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token->location, OPT_Wpedantic,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser->in_function_body
5174 || parser->in_template_argument_list_p)
5176 error_at (token->location,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser);
5180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181 cp_lexer_consume_token (parser->lexer);
5182 return error_mark_node;
5184 else
5185 return cp_parser_statement_expr (parser);
5187 /* Otherwise it's a normal parenthesized expression. */
5189 cp_expr expr;
5190 bool saved_greater_than_is_operator_p;
5192 location_t open_paren_loc = token->location;
5194 /* Consume the `('. */
5195 matching_parens parens;
5196 parens.consume_open (parser);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser->greater_than_is_operator_p;
5201 parser->greater_than_is_operator_p = true;
5203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204 /* Left fold expression. */
5205 expr = NULL_TREE;
5206 else
5207 /* Parse the parenthesized expression. */
5208 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 expr = cp_parser_fold_expression (parser, expr);
5214 if (expr != error_mark_node
5215 && cxx_dialect < cxx17
5216 && !in_system_header_at (input_location))
5217 pedwarn (input_location, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5220 else
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225 not. */
5226 expr = finish_parenthesized_expr (expr);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk != CP_ID_KIND_QUALIFIED)
5233 *idk = CP_ID_KIND_NONE;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p;
5240 /* Consume the `)'. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 location_t close_paren_loc = token->location;
5243 expr.set_range (open_paren_loc, close_paren_loc);
5244 if (!parens.require_close (parser)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246 cp_parser_skip_to_end_of_statement (parser);
5248 return expr;
5251 case CPP_OPEN_SQUARE:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser);
5257 tree msg = cp_parser_objc_message_expression (parser);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser))
5260 return msg;
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam = cp_parser_lambda_expression (parser);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser))
5266 return error_mark_node;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268 return lam;
5271 case CPP_OBJC_STRING:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser);
5275 cp_parser_error (parser, "expected primary-expression");
5276 return error_mark_node;
5278 case CPP_KEYWORD:
5279 switch (token->keyword)
5281 /* These two are the boolean literals. */
5282 case RID_TRUE:
5283 cp_lexer_consume_token (parser->lexer);
5284 return cp_expr (boolean_true_node, token->location);
5285 case RID_FALSE:
5286 cp_lexer_consume_token (parser->lexer);
5287 return cp_expr (boolean_false_node, token->location);
5289 /* The `__null' literal. */
5290 case RID_NULL:
5291 cp_lexer_consume_token (parser->lexer);
5292 return cp_expr (null_node, token->location);
5294 /* The `nullptr' literal. */
5295 case RID_NULLPTR:
5296 cp_lexer_consume_token (parser->lexer);
5297 return cp_expr (nullptr_node, token->location);
5299 /* Recognize the `this' keyword. */
5300 case RID_THIS:
5301 cp_lexer_consume_token (parser->lexer);
5302 if (parser->local_variables_forbidden_p)
5304 error_at (token->location,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310 return error_mark_node;
5311 return cp_expr (finish_this_expr (), token->location);
5313 /* The `operator' keyword can be the beginning of an
5314 id-expression. */
5315 case RID_OPERATOR:
5316 goto id_expression;
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5322 non_integral_constant name;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token = cp_lexer_consume_token (parser->lexer);
5333 switch (token->keyword)
5335 case RID_FUNCTION_NAME:
5336 name = NIC_FUNC_NAME;
5337 break;
5338 case RID_PRETTY_FUNCTION_NAME:
5339 name = NIC_PRETTY_FUNC;
5340 break;
5341 case RID_C99_FUNCTION_NAME:
5342 name = NIC_C99_FUNC;
5343 break;
5344 default:
5345 gcc_unreachable ();
5348 if (cp_parser_non_integral_constant_expression (parser, name))
5349 return error_mark_node;
5351 /* Look up the name. */
5352 return finish_fname (token->u.value);
5355 case RID_VA_ARG:
5357 tree expression;
5358 tree type;
5359 source_location type_location;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser->lexer)->location;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser->lexer);
5365 /* Look for the opening `('. */
5366 matching_parens parens;
5367 parens.require_open (parser);
5368 /* Now, parse the assignment-expression. */
5369 expression = cp_parser_assignment_expression (parser);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372 type_location = cp_lexer_peek_token (parser->lexer)->location;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel s (parser);
5376 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser->lexer)->location;
5381 parens.require_close (parser);
5382 /* Using `va_arg' in a constant-expression is not
5383 allowed. */
5384 if (cp_parser_non_integral_constant_expression (parser,
5385 NIC_VA_ARG))
5386 return error_mark_node;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location, start_loc, finish_loc);
5394 return build_x_va_arg (combined_loc, expression, type);
5397 case RID_OFFSETOF:
5398 return cp_parser_builtin_offsetof (parser);
5400 case RID_HAS_NOTHROW_ASSIGN:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR:
5402 case RID_HAS_NOTHROW_COPY:
5403 case RID_HAS_TRIVIAL_ASSIGN:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405 case RID_HAS_TRIVIAL_COPY:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR:
5409 case RID_IS_ABSTRACT:
5410 case RID_IS_AGGREGATE:
5411 case RID_IS_BASE_OF:
5412 case RID_IS_CLASS:
5413 case RID_IS_EMPTY:
5414 case RID_IS_ENUM:
5415 case RID_IS_FINAL:
5416 case RID_IS_LITERAL_TYPE:
5417 case RID_IS_POD:
5418 case RID_IS_POLYMORPHIC:
5419 case RID_IS_SAME_AS:
5420 case RID_IS_STD_LAYOUT:
5421 case RID_IS_TRIVIAL:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424 case RID_IS_TRIVIALLY_COPYABLE:
5425 case RID_IS_UNION:
5426 case RID_IS_ASSIGNABLE:
5427 case RID_IS_CONSTRUCTIBLE:
5428 return cp_parser_trait_expr (parser, token->keyword);
5430 // C++ concepts
5431 case RID_REQUIRES:
5432 return cp_parser_requires_expression (parser);
5434 /* Objective-C++ expressions. */
5435 case RID_AT_ENCODE:
5436 case RID_AT_PROTOCOL:
5437 case RID_AT_SELECTOR:
5438 return cp_parser_objc_expression (parser);
5440 case RID_TEMPLATE:
5441 if (parser->in_function_body
5442 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443 == CPP_LESS))
5445 error_at (token->location,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser);
5448 return error_mark_node;
5450 /* FALLTHRU */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5458 keyword. */
5459 case CPP_NAME:
5460 case CPP_SCOPE:
5461 case CPP_TEMPLATE_ID:
5462 case CPP_NESTED_NAME_SPECIFIER:
5464 id_expression:
5465 cp_expr id_expression;
5466 cp_expr decl;
5467 const char *error_msg;
5468 bool template_p;
5469 bool done;
5470 cp_token *id_expr_token;
5472 /* Parse the id-expression. */
5473 id_expression
5474 = cp_parser_id_expression (parser,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5477 &template_p,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression == error_mark_node)
5481 return error_mark_node;
5482 id_expr_token = token;
5483 token = cp_lexer_peek_token (parser->lexer);
5484 done = (token->type != CPP_OPEN_SQUARE
5485 && token->type != CPP_OPEN_PAREN
5486 && token->type != CPP_DOT
5487 && token->type != CPP_DEREF
5488 && token->type != CPP_PLUS_PLUS
5489 && token->type != CPP_MINUS_MINUS);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression) == TYPE_DECL)
5495 decl = id_expression;
5496 /* Look up the name. */
5497 else
5499 tree ambiguous_decls;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5503 to check again. */
5504 if (id_expr_token->type == CPP_NAME
5505 && id_expr_token->error_reported)
5507 cp_parser_simulate_error (parser);
5508 return error_mark_node;
5511 decl = cp_parser_lookup_name (parser, id_expression,
5512 none_type,
5513 template_p,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5516 &ambiguous_decls,
5517 id_expr_token->location);
5518 /* If the lookup was ambiguous, an error will already have
5519 been issued. */
5520 if (ambiguous_decls)
5521 return error_mark_node;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527 && TREE_CODE (decl) == TYPE_DECL
5528 && objc_is_class_name (decl))
5530 tree component;
5531 cp_lexer_consume_token (parser->lexer);
5532 component = cp_parser_identifier (parser);
5533 if (component == error_mark_node)
5534 return error_mark_node;
5536 tree result = objc_build_class_component_ref (id_expression,
5537 component);
5538 /* Build a location of the form:
5539 expr.component
5540 ~~~~~^~~~~~~~~
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location, id_expression.get_start (),
5546 get_finish (input_location));
5547 protected_set_expr_location (result, combined_loc);
5548 return result;
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563 id_expression);
5564 if (decl_tree != decl.get_value ())
5565 decl = cp_expr (decl_tree);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl) == SCOPE_REF)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5576 int a[B<N>::i];
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5581 return decl;
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser->local_variables_forbidden_p
5586 && local_variable_p (decl))
5588 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5592 int i;
5593 void g() {
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl = check_for_out_of_scope_variable (decl);
5602 if (local_variable_p (decl))
5604 error_at (id_expr_token->location,
5605 "local variable %qD may not appear in this context",
5606 decl.get_value ());
5607 return error_mark_node;
5612 decl = (finish_id_expression
5613 (id_expression, decl, parser->scope,
5614 idk,
5615 parser->integral_constant_expression_p,
5616 parser->allow_non_integral_constant_expression_p,
5617 &parser->non_integral_constant_expression_p,
5618 template_p, done, address_p,
5619 template_arg_p,
5620 &error_msg,
5621 id_expression.get_location ()));
5622 if (error_msg)
5623 cp_parser_error (parser, error_msg);
5624 decl.set_location (id_expr_token->location);
5625 return decl;
5628 /* Anything else is an error. */
5629 default:
5630 cp_parser_error (parser, "expected primary-expression");
5631 return error_mark_node;
5635 static inline cp_expr
5636 cp_parser_primary_expression (cp_parser *parser,
5637 bool address_p,
5638 bool cast_p,
5639 bool template_arg_p,
5640 cp_id_kind *idk)
5642 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5643 /*decltype*/false, idk);
5646 /* Parse an id-expression.
5648 id-expression:
5649 unqualified-id
5650 qualified-id
5652 qualified-id:
5653 :: [opt] nested-name-specifier template [opt] unqualified-id
5654 :: identifier
5655 :: operator-function-id
5656 :: template-id
5658 Return a representation of the unqualified portion of the
5659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5660 a `::' or nested-name-specifier.
5662 Often, if the id-expression was a qualified-id, the caller will
5663 want to make a SCOPE_REF to represent the qualified-id. This
5664 function does not do this in order to avoid wastefully creating
5665 SCOPE_REFs when they are not required.
5667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5668 `template' keyword.
5670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5671 uninstantiated templates.
5673 If *TEMPLATE_P is non-NULL, it is set to true iff the
5674 `template' keyword is used to explicitly indicate that the entity
5675 named is a template.
5677 If DECLARATOR_P is true, the id-expression is appearing as part of
5678 a declarator, rather than as part of an expression. */
5680 static cp_expr
5681 cp_parser_id_expression (cp_parser *parser,
5682 bool template_keyword_p,
5683 bool check_dependency_p,
5684 bool *template_p,
5685 bool declarator_p,
5686 bool optional_p)
5688 bool global_scope_p;
5689 bool nested_name_specifier_p;
5691 /* Assume the `template' keyword was not used. */
5692 if (template_p)
5693 *template_p = template_keyword_p;
5695 /* Look for the optional `::' operator. */
5696 global_scope_p
5697 = (!template_keyword_p
5698 && (cp_parser_global_scope_opt (parser,
5699 /*current_scope_valid_p=*/false)
5700 != NULL_TREE));
5702 /* Look for the optional nested-name-specifier. */
5703 nested_name_specifier_p
5704 = (cp_parser_nested_name_specifier_opt (parser,
5705 /*typename_keyword_p=*/false,
5706 check_dependency_p,
5707 /*type_p=*/false,
5708 declarator_p,
5709 template_keyword_p)
5710 != NULL_TREE);
5712 /* If there is a nested-name-specifier, then we are looking at
5713 the first qualified-id production. */
5714 if (nested_name_specifier_p)
5716 tree saved_scope;
5717 tree saved_object_scope;
5718 tree saved_qualifying_scope;
5719 cp_expr unqualified_id;
5720 bool is_template;
5722 /* See if the next token is the `template' keyword. */
5723 if (!template_p)
5724 template_p = &is_template;
5725 *template_p = cp_parser_optional_template_keyword (parser);
5726 /* Name lookup we do during the processing of the
5727 unqualified-id might obliterate SCOPE. */
5728 saved_scope = parser->scope;
5729 saved_object_scope = parser->object_scope;
5730 saved_qualifying_scope = parser->qualifying_scope;
5731 /* Process the final unqualified-id. */
5732 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5733 check_dependency_p,
5734 declarator_p,
5735 /*optional_p=*/false);
5736 /* Restore the SAVED_SCOPE for our caller. */
5737 parser->scope = saved_scope;
5738 parser->object_scope = saved_object_scope;
5739 parser->qualifying_scope = saved_qualifying_scope;
5741 return unqualified_id;
5743 /* Otherwise, if we are in global scope, then we are looking at one
5744 of the other qualified-id productions. */
5745 else if (global_scope_p)
5747 cp_token *token;
5748 tree id;
5750 /* Peek at the next token. */
5751 token = cp_lexer_peek_token (parser->lexer);
5753 /* If it's an identifier, and the next token is not a "<", then
5754 we can avoid the template-id case. This is an optimization
5755 for this common case. */
5756 if (token->type == CPP_NAME
5757 && !cp_parser_nth_token_starts_template_argument_list_p
5758 (parser, 2))
5759 return cp_parser_identifier (parser);
5761 cp_parser_parse_tentatively (parser);
5762 /* Try a template-id. */
5763 id = cp_parser_template_id (parser,
5764 /*template_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5766 none_type,
5767 declarator_p);
5768 /* If that worked, we're done. */
5769 if (cp_parser_parse_definitely (parser))
5770 return id;
5772 /* Peek at the next token. (Changes in the token buffer may
5773 have invalidated the pointer obtained above.) */
5774 token = cp_lexer_peek_token (parser->lexer);
5776 switch (token->type)
5778 case CPP_NAME:
5779 return cp_parser_identifier (parser);
5781 case CPP_KEYWORD:
5782 if (token->keyword == RID_OPERATOR)
5783 return cp_parser_operator_function_id (parser);
5784 /* Fall through. */
5786 default:
5787 cp_parser_error (parser, "expected id-expression");
5788 return error_mark_node;
5791 else
5792 return cp_parser_unqualified_id (parser, template_keyword_p,
5793 /*check_dependency_p=*/true,
5794 declarator_p,
5795 optional_p);
5798 /* Parse an unqualified-id.
5800 unqualified-id:
5801 identifier
5802 operator-function-id
5803 conversion-function-id
5804 ~ class-name
5805 template-id
5807 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5808 keyword, in a construct like `A::template ...'.
5810 Returns a representation of unqualified-id. For the `identifier'
5811 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5812 production a BIT_NOT_EXPR is returned; the operand of the
5813 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5814 other productions, see the documentation accompanying the
5815 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5816 names are looked up in uninstantiated templates. If DECLARATOR_P
5817 is true, the unqualified-id is appearing as part of a declarator,
5818 rather than as part of an expression. */
5820 static cp_expr
5821 cp_parser_unqualified_id (cp_parser* parser,
5822 bool template_keyword_p,
5823 bool check_dependency_p,
5824 bool declarator_p,
5825 bool optional_p)
5827 cp_token *token;
5829 /* Peek at the next token. */
5830 token = cp_lexer_peek_token (parser->lexer);
5832 switch ((int) token->type)
5834 case CPP_NAME:
5836 tree id;
5838 /* We don't know yet whether or not this will be a
5839 template-id. */
5840 cp_parser_parse_tentatively (parser);
5841 /* Try a template-id. */
5842 id = cp_parser_template_id (parser, template_keyword_p,
5843 check_dependency_p,
5844 none_type,
5845 declarator_p);
5846 /* If it worked, we're done. */
5847 if (cp_parser_parse_definitely (parser))
5848 return id;
5849 /* Otherwise, it's an ordinary identifier. */
5850 return cp_parser_identifier (parser);
5853 case CPP_TEMPLATE_ID:
5854 return cp_parser_template_id (parser, template_keyword_p,
5855 check_dependency_p,
5856 none_type,
5857 declarator_p);
5859 case CPP_COMPL:
5861 tree type_decl;
5862 tree qualifying_scope;
5863 tree object_scope;
5864 tree scope;
5865 bool done;
5867 /* Consume the `~' token. */
5868 cp_lexer_consume_token (parser->lexer);
5869 /* Parse the class-name. The standard, as written, seems to
5870 say that:
5872 template <typename T> struct S { ~S (); };
5873 template <typename T> S<T>::~S() {}
5875 is invalid, since `~' must be followed by a class-name, but
5876 `S<T>' is dependent, and so not known to be a class.
5877 That's not right; we need to look in uninstantiated
5878 templates. A further complication arises from:
5880 template <typename T> void f(T t) {
5881 t.T::~T();
5884 Here, it is not possible to look up `T' in the scope of `T'
5885 itself. We must look in both the current scope, and the
5886 scope of the containing complete expression.
5888 Yet another issue is:
5890 struct S {
5891 int S;
5892 ~S();
5895 S::~S() {}
5897 The standard does not seem to say that the `S' in `~S'
5898 should refer to the type `S' and not the data member
5899 `S::S'. */
5901 /* DR 244 says that we look up the name after the "~" in the
5902 same scope as we looked up the qualifying name. That idea
5903 isn't fully worked out; it's more complicated than that. */
5904 scope = parser->scope;
5905 object_scope = parser->object_scope;
5906 qualifying_scope = parser->qualifying_scope;
5908 /* Check for invalid scopes. */
5909 if (scope == error_mark_node)
5911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5912 cp_lexer_consume_token (parser->lexer);
5913 return error_mark_node;
5915 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5917 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5918 error_at (token->location,
5919 "scope %qT before %<~%> is not a class-name",
5920 scope);
5921 cp_parser_simulate_error (parser);
5922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5923 cp_lexer_consume_token (parser->lexer);
5924 return error_mark_node;
5926 gcc_assert (!scope || TYPE_P (scope));
5928 /* If the name is of the form "X::~X" it's OK even if X is a
5929 typedef. */
5930 token = cp_lexer_peek_token (parser->lexer);
5931 if (scope
5932 && token->type == CPP_NAME
5933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5934 != CPP_LESS)
5935 && (token->u.value == TYPE_IDENTIFIER (scope)
5936 || (CLASS_TYPE_P (scope)
5937 && constructor_name_p (token->u.value, scope))))
5939 cp_lexer_consume_token (parser->lexer);
5940 return build_nt (BIT_NOT_EXPR, scope);
5943 /* ~auto means the destructor of whatever the object is. */
5944 if (cp_parser_is_keyword (token, RID_AUTO))
5946 if (cxx_dialect < cxx14)
5947 pedwarn (input_location, 0,
5948 "%<~auto%> only available with "
5949 "-std=c++14 or -std=gnu++14");
5950 cp_lexer_consume_token (parser->lexer);
5951 return build_nt (BIT_NOT_EXPR, make_auto ());
5954 /* If there was an explicit qualification (S::~T), first look
5955 in the scope given by the qualification (i.e., S).
5957 Note: in the calls to cp_parser_class_name below we pass
5958 typename_type so that lookup finds the injected-class-name
5959 rather than the constructor. */
5960 done = false;
5961 type_decl = NULL_TREE;
5962 if (scope)
5964 cp_parser_parse_tentatively (parser);
5965 type_decl = cp_parser_class_name (parser,
5966 /*typename_keyword_p=*/false,
5967 /*template_keyword_p=*/false,
5968 typename_type,
5969 /*check_dependency=*/false,
5970 /*class_head_p=*/false,
5971 declarator_p);
5972 if (cp_parser_parse_definitely (parser))
5973 done = true;
5975 /* In "N::S::~S", look in "N" as well. */
5976 if (!done && scope && qualifying_scope)
5978 cp_parser_parse_tentatively (parser);
5979 parser->scope = qualifying_scope;
5980 parser->object_scope = NULL_TREE;
5981 parser->qualifying_scope = NULL_TREE;
5982 type_decl
5983 = cp_parser_class_name (parser,
5984 /*typename_keyword_p=*/false,
5985 /*template_keyword_p=*/false,
5986 typename_type,
5987 /*check_dependency=*/false,
5988 /*class_head_p=*/false,
5989 declarator_p);
5990 if (cp_parser_parse_definitely (parser))
5991 done = true;
5993 /* In "p->S::~T", look in the scope given by "*p" as well. */
5994 else if (!done && object_scope)
5996 cp_parser_parse_tentatively (parser);
5997 parser->scope = object_scope;
5998 parser->object_scope = NULL_TREE;
5999 parser->qualifying_scope = NULL_TREE;
6000 type_decl
6001 = cp_parser_class_name (parser,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6004 typename_type,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6007 declarator_p);
6008 if (cp_parser_parse_definitely (parser))
6009 done = true;
6011 /* Look in the surrounding context. */
6012 if (!done)
6014 parser->scope = NULL_TREE;
6015 parser->object_scope = NULL_TREE;
6016 parser->qualifying_scope = NULL_TREE;
6017 if (processing_template_decl)
6018 cp_parser_parse_tentatively (parser);
6019 type_decl
6020 = cp_parser_class_name (parser,
6021 /*typename_keyword_p=*/false,
6022 /*template_keyword_p=*/false,
6023 typename_type,
6024 /*check_dependency=*/false,
6025 /*class_head_p=*/false,
6026 declarator_p);
6027 if (processing_template_decl
6028 && ! cp_parser_parse_definitely (parser))
6030 /* We couldn't find a type with this name. If we're parsing
6031 tentatively, fail and try something else. */
6032 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6034 cp_parser_simulate_error (parser);
6035 return error_mark_node;
6037 /* Otherwise, accept it and check for a match at instantiation
6038 time. */
6039 type_decl = cp_parser_identifier (parser);
6040 if (type_decl != error_mark_node)
6041 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6042 return type_decl;
6045 /* If an error occurred, assume that the name of the
6046 destructor is the same as the name of the qualifying
6047 class. That allows us to keep parsing after running
6048 into ill-formed destructor names. */
6049 if (type_decl == error_mark_node && scope)
6050 return build_nt (BIT_NOT_EXPR, scope);
6051 else if (type_decl == error_mark_node)
6052 return error_mark_node;
6054 /* Check that destructor name and scope match. */
6055 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6058 error_at (token->location,
6059 "declaration of %<~%T%> as member of %qT",
6060 type_decl, scope);
6061 cp_parser_simulate_error (parser);
6062 return error_mark_node;
6065 /* [class.dtor]
6067 A typedef-name that names a class shall not be used as the
6068 identifier in the declarator for a destructor declaration. */
6069 if (declarator_p
6070 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6071 && !DECL_SELF_REFERENCE_P (type_decl)
6072 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6073 error_at (token->location,
6074 "typedef-name %qD used as destructor declarator",
6075 type_decl);
6077 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6080 case CPP_KEYWORD:
6081 if (token->keyword == RID_OPERATOR)
6083 cp_expr id;
6085 /* This could be a template-id, so we try that first. */
6086 cp_parser_parse_tentatively (parser);
6087 /* Try a template-id. */
6088 id = cp_parser_template_id (parser, template_keyword_p,
6089 /*check_dependency_p=*/true,
6090 none_type,
6091 declarator_p);
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser))
6094 return id;
6095 /* We still don't know whether we're looking at an
6096 operator-function-id or a conversion-function-id. */
6097 cp_parser_parse_tentatively (parser);
6098 /* Try an operator-function-id. */
6099 id = cp_parser_operator_function_id (parser);
6100 /* If that didn't work, try a conversion-function-id. */
6101 if (!cp_parser_parse_definitely (parser))
6102 id = cp_parser_conversion_function_id (parser);
6104 return id;
6106 /* Fall through. */
6108 default:
6109 if (optional_p)
6110 return NULL_TREE;
6111 cp_parser_error (parser, "expected unqualified-id");
6112 return error_mark_node;
6116 /* Parse an (optional) nested-name-specifier.
6118 nested-name-specifier: [C++98]
6119 class-or-namespace-name :: nested-name-specifier [opt]
6120 class-or-namespace-name :: template nested-name-specifier [opt]
6122 nested-name-specifier: [C++0x]
6123 type-name ::
6124 namespace-name ::
6125 nested-name-specifier identifier ::
6126 nested-name-specifier template [opt] simple-template-id ::
6128 PARSER->SCOPE should be set appropriately before this function is
6129 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6130 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6131 in name lookups.
6133 Sets PARSER->SCOPE to the class (TYPE) or namespace
6134 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6135 it unchanged if there is no nested-name-specifier. Returns the new
6136 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6138 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6139 part of a declaration and/or decl-specifier. */
6141 static tree
6142 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6143 bool typename_keyword_p,
6144 bool check_dependency_p,
6145 bool type_p,
6146 bool is_declaration,
6147 bool template_keyword_p /* = false */)
6149 bool success = false;
6150 cp_token_position start = 0;
6151 cp_token *token;
6153 /* Remember where the nested-name-specifier starts. */
6154 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6156 start = cp_lexer_token_position (parser->lexer, false);
6157 push_deferring_access_checks (dk_deferred);
6160 while (true)
6162 tree new_scope;
6163 tree old_scope;
6164 tree saved_qualifying_scope;
6166 /* Spot cases that cannot be the beginning of a
6167 nested-name-specifier. */
6168 token = cp_lexer_peek_token (parser->lexer);
6170 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6171 the already parsed nested-name-specifier. */
6172 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6174 /* Grab the nested-name-specifier and continue the loop. */
6175 cp_parser_pre_parsed_nested_name_specifier (parser);
6176 /* If we originally encountered this nested-name-specifier
6177 with IS_DECLARATION set to false, we will not have
6178 resolved TYPENAME_TYPEs, so we must do so here. */
6179 if (is_declaration
6180 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6182 new_scope = resolve_typename_type (parser->scope,
6183 /*only_current_p=*/false);
6184 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6185 parser->scope = new_scope;
6187 success = true;
6188 continue;
6191 /* Spot cases that cannot be the beginning of a
6192 nested-name-specifier. On the second and subsequent times
6193 through the loop, we look for the `template' keyword. */
6194 if (success && token->keyword == RID_TEMPLATE)
6196 /* A template-id can start a nested-name-specifier. */
6197 else if (token->type == CPP_TEMPLATE_ID)
6199 /* DR 743: decltype can be used in a nested-name-specifier. */
6200 else if (token_is_decltype (token))
6202 else
6204 /* If the next token is not an identifier, then it is
6205 definitely not a type-name or namespace-name. */
6206 if (token->type != CPP_NAME)
6207 break;
6208 /* If the following token is neither a `<' (to begin a
6209 template-id), nor a `::', then we are not looking at a
6210 nested-name-specifier. */
6211 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6213 if (token->type == CPP_COLON
6214 && parser->colon_corrects_to_scope_p
6215 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6217 gcc_rich_location richloc (token->location);
6218 richloc.add_fixit_replace ("::");
6219 error_at (&richloc,
6220 "found %<:%> in nested-name-specifier, "
6221 "expected %<::%>");
6222 token->type = CPP_SCOPE;
6225 if (token->type != CPP_SCOPE
6226 && !cp_parser_nth_token_starts_template_argument_list_p
6227 (parser, 2))
6228 break;
6231 /* The nested-name-specifier is optional, so we parse
6232 tentatively. */
6233 cp_parser_parse_tentatively (parser);
6235 /* Look for the optional `template' keyword, if this isn't the
6236 first time through the loop. */
6237 if (success)
6238 template_keyword_p = cp_parser_optional_template_keyword (parser);
6240 /* Save the old scope since the name lookup we are about to do
6241 might destroy it. */
6242 old_scope = parser->scope;
6243 saved_qualifying_scope = parser->qualifying_scope;
6244 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6245 look up names in "X<T>::I" in order to determine that "Y" is
6246 a template. So, if we have a typename at this point, we make
6247 an effort to look through it. */
6248 if (is_declaration
6249 && !typename_keyword_p
6250 && parser->scope
6251 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6252 parser->scope = resolve_typename_type (parser->scope,
6253 /*only_current_p=*/false);
6254 /* Parse the qualifying entity. */
6255 new_scope
6256 = cp_parser_qualifying_entity (parser,
6257 typename_keyword_p,
6258 template_keyword_p,
6259 check_dependency_p,
6260 type_p,
6261 is_declaration);
6262 /* Look for the `::' token. */
6263 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6265 /* If we found what we wanted, we keep going; otherwise, we're
6266 done. */
6267 if (!cp_parser_parse_definitely (parser))
6269 bool error_p = false;
6271 /* Restore the OLD_SCOPE since it was valid before the
6272 failed attempt at finding the last
6273 class-or-namespace-name. */
6274 parser->scope = old_scope;
6275 parser->qualifying_scope = saved_qualifying_scope;
6277 /* If the next token is a decltype, and the one after that is a
6278 `::', then the decltype has failed to resolve to a class or
6279 enumeration type. Give this error even when parsing
6280 tentatively since it can't possibly be valid--and we're going
6281 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6282 won't get another chance.*/
6283 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6284 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6285 == CPP_SCOPE))
6287 token = cp_lexer_consume_token (parser->lexer);
6288 error_at (token->location, "decltype evaluates to %qT, "
6289 "which is not a class or enumeration type",
6290 token->u.tree_check_value->value);
6291 parser->scope = error_mark_node;
6292 error_p = true;
6293 /* As below. */
6294 success = true;
6295 cp_lexer_consume_token (parser->lexer);
6298 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6299 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6301 /* If we have a non-type template-id followed by ::, it can't
6302 possibly be valid. */
6303 token = cp_lexer_peek_token (parser->lexer);
6304 tree tid = token->u.tree_check_value->value;
6305 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6306 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6308 tree tmpl = NULL_TREE;
6309 if (is_overloaded_fn (tid))
6311 tree fns = get_fns (tid);
6312 if (OVL_SINGLE_P (fns))
6313 tmpl = OVL_FIRST (fns);
6314 error_at (token->location, "function template-id %qD "
6315 "in nested-name-specifier", tid);
6317 else
6319 /* Variable template. */
6320 tmpl = TREE_OPERAND (tid, 0);
6321 gcc_assert (variable_template_p (tmpl));
6322 error_at (token->location, "variable template-id %qD "
6323 "in nested-name-specifier", tid);
6325 if (tmpl)
6326 inform (DECL_SOURCE_LOCATION (tmpl),
6327 "%qD declared here", tmpl);
6329 parser->scope = error_mark_node;
6330 error_p = true;
6331 /* As below. */
6332 success = true;
6333 cp_lexer_consume_token (parser->lexer);
6334 cp_lexer_consume_token (parser->lexer);
6338 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6339 break;
6340 /* If the next token is an identifier, and the one after
6341 that is a `::', then any valid interpretation would have
6342 found a class-or-namespace-name. */
6343 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6344 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6345 == CPP_SCOPE)
6346 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6347 != CPP_COMPL))
6349 token = cp_lexer_consume_token (parser->lexer);
6350 if (!error_p)
6352 if (!token->error_reported)
6354 tree decl;
6355 tree ambiguous_decls;
6357 decl = cp_parser_lookup_name (parser, token->u.value,
6358 none_type,
6359 /*is_template=*/false,
6360 /*is_namespace=*/false,
6361 /*check_dependency=*/true,
6362 &ambiguous_decls,
6363 token->location);
6364 if (TREE_CODE (decl) == TEMPLATE_DECL)
6365 error_at (token->location,
6366 "%qD used without template parameters",
6367 decl);
6368 else if (ambiguous_decls)
6370 // cp_parser_lookup_name has the same diagnostic,
6371 // thus make sure to emit it at most once.
6372 if (cp_parser_uncommitted_to_tentative_parse_p
6373 (parser))
6375 error_at (token->location,
6376 "reference to %qD is ambiguous",
6377 token->u.value);
6378 print_candidates (ambiguous_decls);
6380 decl = error_mark_node;
6382 else
6384 if (cxx_dialect != cxx98)
6385 cp_parser_name_lookup_error
6386 (parser, token->u.value, decl, NLE_NOT_CXX98,
6387 token->location);
6388 else
6389 cp_parser_name_lookup_error
6390 (parser, token->u.value, decl, NLE_CXX98,
6391 token->location);
6394 parser->scope = error_mark_node;
6395 error_p = true;
6396 /* Treat this as a successful nested-name-specifier
6397 due to:
6399 [basic.lookup.qual]
6401 If the name found is not a class-name (clause
6402 _class_) or namespace-name (_namespace.def_), the
6403 program is ill-formed. */
6404 success = true;
6406 cp_lexer_consume_token (parser->lexer);
6408 break;
6410 /* We've found one valid nested-name-specifier. */
6411 success = true;
6412 /* Name lookup always gives us a DECL. */
6413 if (TREE_CODE (new_scope) == TYPE_DECL)
6414 new_scope = TREE_TYPE (new_scope);
6415 /* Uses of "template" must be followed by actual templates. */
6416 if (template_keyword_p
6417 && !(CLASS_TYPE_P (new_scope)
6418 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6419 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6420 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6421 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6422 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6423 == TEMPLATE_ID_EXPR)))
6424 permerror (input_location, TYPE_P (new_scope)
6425 ? G_("%qT is not a template")
6426 : G_("%qD is not a template"),
6427 new_scope);
6428 /* If it is a class scope, try to complete it; we are about to
6429 be looking up names inside the class. */
6430 if (TYPE_P (new_scope)
6431 /* Since checking types for dependency can be expensive,
6432 avoid doing it if the type is already complete. */
6433 && !COMPLETE_TYPE_P (new_scope)
6434 /* Do not try to complete dependent types. */
6435 && !dependent_type_p (new_scope))
6437 new_scope = complete_type (new_scope);
6438 /* If it is a typedef to current class, use the current
6439 class instead, as the typedef won't have any names inside
6440 it yet. */
6441 if (!COMPLETE_TYPE_P (new_scope)
6442 && currently_open_class (new_scope))
6443 new_scope = TYPE_MAIN_VARIANT (new_scope);
6445 /* Make sure we look in the right scope the next time through
6446 the loop. */
6447 parser->scope = new_scope;
6450 /* If parsing tentatively, replace the sequence of tokens that makes
6451 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6452 token. That way, should we re-parse the token stream, we will
6453 not have to repeat the effort required to do the parse, nor will
6454 we issue duplicate error messages. */
6455 if (success && start)
6457 cp_token *token;
6459 token = cp_lexer_token_at (parser->lexer, start);
6460 /* Reset the contents of the START token. */
6461 token->type = CPP_NESTED_NAME_SPECIFIER;
6462 /* Retrieve any deferred checks. Do not pop this access checks yet
6463 so the memory will not be reclaimed during token replacing below. */
6464 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6465 token->u.tree_check_value->value = parser->scope;
6466 token->u.tree_check_value->checks = get_deferred_access_checks ();
6467 token->u.tree_check_value->qualifying_scope =
6468 parser->qualifying_scope;
6469 token->keyword = RID_MAX;
6471 /* Purge all subsequent tokens. */
6472 cp_lexer_purge_tokens_after (parser->lexer, start);
6475 if (start)
6476 pop_to_parent_deferring_access_checks ();
6478 return success ? parser->scope : NULL_TREE;
6481 /* Parse a nested-name-specifier. See
6482 cp_parser_nested_name_specifier_opt for details. This function
6483 behaves identically, except that it will an issue an error if no
6484 nested-name-specifier is present. */
6486 static tree
6487 cp_parser_nested_name_specifier (cp_parser *parser,
6488 bool typename_keyword_p,
6489 bool check_dependency_p,
6490 bool type_p,
6491 bool is_declaration)
6493 tree scope;
6495 /* Look for the nested-name-specifier. */
6496 scope = cp_parser_nested_name_specifier_opt (parser,
6497 typename_keyword_p,
6498 check_dependency_p,
6499 type_p,
6500 is_declaration);
6501 /* If it was not present, issue an error message. */
6502 if (!scope)
6504 cp_parser_error (parser, "expected nested-name-specifier");
6505 parser->scope = NULL_TREE;
6508 return scope;
6511 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6512 this is either a class-name or a namespace-name (which corresponds
6513 to the class-or-namespace-name production in the grammar). For
6514 C++0x, it can also be a type-name that refers to an enumeration
6515 type or a simple-template-id.
6517 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6518 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6519 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6520 TYPE_P is TRUE iff the next name should be taken as a class-name,
6521 even the same name is declared to be another entity in the same
6522 scope.
6524 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6525 specified by the class-or-namespace-name. If neither is found the
6526 ERROR_MARK_NODE is returned. */
6528 static tree
6529 cp_parser_qualifying_entity (cp_parser *parser,
6530 bool typename_keyword_p,
6531 bool template_keyword_p,
6532 bool check_dependency_p,
6533 bool type_p,
6534 bool is_declaration)
6536 tree saved_scope;
6537 tree saved_qualifying_scope;
6538 tree saved_object_scope;
6539 tree scope;
6540 bool only_class_p;
6541 bool successful_parse_p;
6543 /* DR 743: decltype can appear in a nested-name-specifier. */
6544 if (cp_lexer_next_token_is_decltype (parser->lexer))
6546 scope = cp_parser_decltype (parser);
6547 if (TREE_CODE (scope) != ENUMERAL_TYPE
6548 && !MAYBE_CLASS_TYPE_P (scope))
6550 cp_parser_simulate_error (parser);
6551 return error_mark_node;
6553 if (TYPE_NAME (scope))
6554 scope = TYPE_NAME (scope);
6555 return scope;
6558 /* Before we try to parse the class-name, we must save away the
6559 current PARSER->SCOPE since cp_parser_class_name will destroy
6560 it. */
6561 saved_scope = parser->scope;
6562 saved_qualifying_scope = parser->qualifying_scope;
6563 saved_object_scope = parser->object_scope;
6564 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6565 there is no need to look for a namespace-name. */
6566 only_class_p = template_keyword_p
6567 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6568 if (!only_class_p)
6569 cp_parser_parse_tentatively (parser);
6570 scope = cp_parser_class_name (parser,
6571 typename_keyword_p,
6572 template_keyword_p,
6573 type_p ? class_type : none_type,
6574 check_dependency_p,
6575 /*class_head_p=*/false,
6576 is_declaration,
6577 /*enum_ok=*/cxx_dialect > cxx98);
6578 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6579 /* If that didn't work, try for a namespace-name. */
6580 if (!only_class_p && !successful_parse_p)
6582 /* Restore the saved scope. */
6583 parser->scope = saved_scope;
6584 parser->qualifying_scope = saved_qualifying_scope;
6585 parser->object_scope = saved_object_scope;
6586 /* If we are not looking at an identifier followed by the scope
6587 resolution operator, then this is not part of a
6588 nested-name-specifier. (Note that this function is only used
6589 to parse the components of a nested-name-specifier.) */
6590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6591 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6592 return error_mark_node;
6593 scope = cp_parser_namespace_name (parser);
6596 return scope;
6599 /* Return true if we are looking at a compound-literal, false otherwise. */
6601 static bool
6602 cp_parser_compound_literal_p (cp_parser *parser)
6604 cp_lexer_save_tokens (parser->lexer);
6606 /* Skip tokens until the next token is a closing parenthesis.
6607 If we find the closing `)', and the next token is a `{', then
6608 we are looking at a compound-literal. */
6609 bool compound_literal_p
6610 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6611 /*consume_paren=*/true)
6612 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6614 /* Roll back the tokens we skipped. */
6615 cp_lexer_rollback_tokens (parser->lexer);
6617 return compound_literal_p;
6620 /* Return true if EXPR is the integer constant zero or a complex constant
6621 of zero, without any folding, but ignoring location wrappers. */
6623 static bool
6624 literal_integer_zerop (const_tree expr)
6626 STRIP_ANY_LOCATION_WRAPPER (expr);
6627 return integer_zerop (expr);
6630 /* Parse a postfix-expression.
6632 postfix-expression:
6633 primary-expression
6634 postfix-expression [ expression ]
6635 postfix-expression ( expression-list [opt] )
6636 simple-type-specifier ( expression-list [opt] )
6637 typename :: [opt] nested-name-specifier identifier
6638 ( expression-list [opt] )
6639 typename :: [opt] nested-name-specifier template [opt] template-id
6640 ( expression-list [opt] )
6641 postfix-expression . template [opt] id-expression
6642 postfix-expression -> template [opt] id-expression
6643 postfix-expression . pseudo-destructor-name
6644 postfix-expression -> pseudo-destructor-name
6645 postfix-expression ++
6646 postfix-expression --
6647 dynamic_cast < type-id > ( expression )
6648 static_cast < type-id > ( expression )
6649 reinterpret_cast < type-id > ( expression )
6650 const_cast < type-id > ( expression )
6651 typeid ( expression )
6652 typeid ( type-id )
6654 GNU Extension:
6656 postfix-expression:
6657 ( type-id ) { initializer-list , [opt] }
6659 This extension is a GNU version of the C99 compound-literal
6660 construct. (The C99 grammar uses `type-name' instead of `type-id',
6661 but they are essentially the same concept.)
6663 If ADDRESS_P is true, the postfix expression is the operand of the
6664 `&' operator. CAST_P is true if this expression is the target of a
6665 cast.
6667 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6668 class member access expressions [expr.ref].
6670 Returns a representation of the expression. */
6672 static cp_expr
6673 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6674 bool member_access_only_p, bool decltype_p,
6675 cp_id_kind * pidk_return)
6677 cp_token *token;
6678 location_t loc;
6679 enum rid keyword;
6680 cp_id_kind idk = CP_ID_KIND_NONE;
6681 cp_expr postfix_expression = NULL_TREE;
6682 bool is_member_access = false;
6684 /* Peek at the next token. */
6685 token = cp_lexer_peek_token (parser->lexer);
6686 loc = token->location;
6687 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6689 /* Some of the productions are determined by keywords. */
6690 keyword = token->keyword;
6691 switch (keyword)
6693 case RID_DYNCAST:
6694 case RID_STATCAST:
6695 case RID_REINTCAST:
6696 case RID_CONSTCAST:
6698 tree type;
6699 cp_expr expression;
6700 const char *saved_message;
6701 bool saved_in_type_id_in_expr_p;
6703 /* All of these can be handled in the same way from the point
6704 of view of parsing. Begin by consuming the token
6705 identifying the cast. */
6706 cp_lexer_consume_token (parser->lexer);
6708 /* New types cannot be defined in the cast. */
6709 saved_message = parser->type_definition_forbidden_message;
6710 parser->type_definition_forbidden_message
6711 = G_("types may not be defined in casts");
6713 /* Look for the opening `<'. */
6714 cp_parser_require (parser, CPP_LESS, RT_LESS);
6715 /* Parse the type to which we are casting. */
6716 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6717 parser->in_type_id_in_expr_p = true;
6718 type = cp_parser_type_id (parser);
6719 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6720 /* Look for the closing `>'. */
6721 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6722 /* Restore the old message. */
6723 parser->type_definition_forbidden_message = saved_message;
6725 bool saved_greater_than_is_operator_p
6726 = parser->greater_than_is_operator_p;
6727 parser->greater_than_is_operator_p = true;
6729 /* And the expression which is being cast. */
6730 matching_parens parens;
6731 parens.require_open (parser);
6732 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6733 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6734 RT_CLOSE_PAREN);
6735 location_t end_loc = close_paren ?
6736 close_paren->location : UNKNOWN_LOCATION;
6738 parser->greater_than_is_operator_p
6739 = saved_greater_than_is_operator_p;
6741 /* Only type conversions to integral or enumeration types
6742 can be used in constant-expressions. */
6743 if (!cast_valid_in_integral_constant_expression_p (type)
6744 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6746 postfix_expression = error_mark_node;
6747 break;
6750 switch (keyword)
6752 case RID_DYNCAST:
6753 postfix_expression
6754 = build_dynamic_cast (type, expression, tf_warning_or_error);
6755 break;
6756 case RID_STATCAST:
6757 postfix_expression
6758 = build_static_cast (type, expression, tf_warning_or_error);
6759 break;
6760 case RID_REINTCAST:
6761 postfix_expression
6762 = build_reinterpret_cast (type, expression,
6763 tf_warning_or_error);
6764 break;
6765 case RID_CONSTCAST:
6766 postfix_expression
6767 = build_const_cast (type, expression, tf_warning_or_error);
6768 break;
6769 default:
6770 gcc_unreachable ();
6773 /* Construct a location e.g. :
6774 reinterpret_cast <int *> (expr)
6775 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6776 ranging from the start of the "*_cast" token to the final closing
6777 paren, with the caret at the start. */
6778 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6779 postfix_expression.set_location (cp_cast_loc);
6781 break;
6783 case RID_TYPEID:
6785 tree type;
6786 const char *saved_message;
6787 bool saved_in_type_id_in_expr_p;
6789 /* Consume the `typeid' token. */
6790 cp_lexer_consume_token (parser->lexer);
6791 /* Look for the `(' token. */
6792 matching_parens parens;
6793 parens.require_open (parser);
6794 /* Types cannot be defined in a `typeid' expression. */
6795 saved_message = parser->type_definition_forbidden_message;
6796 parser->type_definition_forbidden_message
6797 = G_("types may not be defined in a %<typeid%> expression");
6798 /* We can't be sure yet whether we're looking at a type-id or an
6799 expression. */
6800 cp_parser_parse_tentatively (parser);
6801 /* Try a type-id first. */
6802 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6803 parser->in_type_id_in_expr_p = true;
6804 type = cp_parser_type_id (parser);
6805 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6806 /* Look for the `)' token. Otherwise, we can't be sure that
6807 we're not looking at an expression: consider `typeid (int
6808 (3))', for example. */
6809 cp_token *close_paren = parens.require_close (parser);
6810 /* If all went well, simply lookup the type-id. */
6811 if (cp_parser_parse_definitely (parser))
6812 postfix_expression = get_typeid (type, tf_warning_or_error);
6813 /* Otherwise, fall back to the expression variant. */
6814 else
6816 tree expression;
6818 /* Look for an expression. */
6819 expression = cp_parser_expression (parser, & idk);
6820 /* Compute its typeid. */
6821 postfix_expression = build_typeid (expression, tf_warning_or_error);
6822 /* Look for the `)' token. */
6823 close_paren = parens.require_close (parser);
6825 /* Restore the saved message. */
6826 parser->type_definition_forbidden_message = saved_message;
6827 /* `typeid' may not appear in an integral constant expression. */
6828 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6829 postfix_expression = error_mark_node;
6831 /* Construct a location e.g. :
6832 typeid (expr)
6833 ^~~~~~~~~~~~~
6834 ranging from the start of the "typeid" token to the final closing
6835 paren, with the caret at the start. */
6836 if (close_paren)
6838 location_t typeid_loc
6839 = make_location (start_loc, start_loc, close_paren->location);
6840 postfix_expression.set_location (typeid_loc);
6841 postfix_expression.maybe_add_location_wrapper ();
6844 break;
6846 case RID_TYPENAME:
6848 tree type;
6849 /* The syntax permitted here is the same permitted for an
6850 elaborated-type-specifier. */
6851 ++parser->prevent_constrained_type_specifiers;
6852 type = cp_parser_elaborated_type_specifier (parser,
6853 /*is_friend=*/false,
6854 /*is_declaration=*/false);
6855 --parser->prevent_constrained_type_specifiers;
6856 postfix_expression = cp_parser_functional_cast (parser, type);
6858 break;
6860 case RID_ADDRESSOF:
6861 case RID_BUILTIN_SHUFFLE:
6862 case RID_BUILTIN_LAUNDER:
6864 vec<tree, va_gc> *vec;
6865 unsigned int i;
6866 tree p;
6868 cp_lexer_consume_token (parser->lexer);
6869 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL);
6872 if (vec == NULL)
6874 postfix_expression = error_mark_node;
6875 break;
6878 FOR_EACH_VEC_ELT (*vec, i, p)
6879 mark_exp_read (p);
6881 switch (keyword)
6883 case RID_ADDRESSOF:
6884 if (vec->length () == 1)
6885 postfix_expression
6886 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6887 else
6889 error_at (loc, "wrong number of arguments to "
6890 "%<__builtin_addressof%>");
6891 postfix_expression = error_mark_node;
6893 break;
6895 case RID_BUILTIN_LAUNDER:
6896 if (vec->length () == 1)
6897 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6898 tf_warning_or_error);
6899 else
6901 error_at (loc, "wrong number of arguments to "
6902 "%<__builtin_launder%>");
6903 postfix_expression = error_mark_node;
6905 break;
6907 case RID_BUILTIN_SHUFFLE:
6908 if (vec->length () == 2)
6909 postfix_expression
6910 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6911 (*vec)[1], tf_warning_or_error);
6912 else if (vec->length () == 3)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6915 (*vec)[2], tf_warning_or_error);
6916 else
6918 error_at (loc, "wrong number of arguments to "
6919 "%<__builtin_shuffle%>");
6920 postfix_expression = error_mark_node;
6922 break;
6924 default:
6925 gcc_unreachable ();
6927 break;
6930 default:
6932 tree type;
6934 /* If the next thing is a simple-type-specifier, we may be
6935 looking at a functional cast. We could also be looking at
6936 an id-expression. So, we try the functional cast, and if
6937 that doesn't work we fall back to the primary-expression. */
6938 cp_parser_parse_tentatively (parser);
6939 /* Look for the simple-type-specifier. */
6940 ++parser->prevent_constrained_type_specifiers;
6941 type = cp_parser_simple_type_specifier (parser,
6942 /*decl_specs=*/NULL,
6943 CP_PARSER_FLAGS_NONE);
6944 --parser->prevent_constrained_type_specifiers;
6945 /* Parse the cast itself. */
6946 if (!cp_parser_error_occurred (parser))
6947 postfix_expression
6948 = cp_parser_functional_cast (parser, type);
6949 /* If that worked, we're done. */
6950 if (cp_parser_parse_definitely (parser))
6951 break;
6953 /* If the functional-cast didn't work out, try a
6954 compound-literal. */
6955 if (cp_parser_allow_gnu_extensions_p (parser)
6956 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6958 cp_expr initializer = NULL_TREE;
6960 cp_parser_parse_tentatively (parser);
6962 matching_parens parens;
6963 parens.consume_open (parser);
6965 /* Avoid calling cp_parser_type_id pointlessly, see comment
6966 in cp_parser_cast_expression about c++/29234. */
6967 if (!cp_parser_compound_literal_p (parser))
6968 cp_parser_simulate_error (parser);
6969 else
6971 /* Parse the type. */
6972 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6973 parser->in_type_id_in_expr_p = true;
6974 type = cp_parser_type_id (parser);
6975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6976 parens.require_close (parser);
6979 /* If things aren't going well, there's no need to
6980 keep going. */
6981 if (!cp_parser_error_occurred (parser))
6983 bool non_constant_p;
6984 /* Parse the brace-enclosed initializer list. */
6985 initializer = cp_parser_braced_list (parser,
6986 &non_constant_p);
6988 /* If that worked, we're definitely looking at a
6989 compound-literal expression. */
6990 if (cp_parser_parse_definitely (parser))
6992 /* Warn the user that a compound literal is not
6993 allowed in standard C++. */
6994 pedwarn (input_location, OPT_Wpedantic,
6995 "ISO C++ forbids compound-literals");
6996 /* For simplicity, we disallow compound literals in
6997 constant-expressions. We could
6998 allow compound literals of integer type, whose
6999 initializer was a constant, in constant
7000 expressions. Permitting that usage, as a further
7001 extension, would not change the meaning of any
7002 currently accepted programs. (Of course, as
7003 compound literals are not part of ISO C++, the
7004 standard has nothing to say.) */
7005 if (cp_parser_non_integral_constant_expression (parser,
7006 NIC_NCC))
7008 postfix_expression = error_mark_node;
7009 break;
7011 /* Form the representation of the compound-literal. */
7012 postfix_expression
7013 = finish_compound_literal (type, initializer,
7014 tf_warning_or_error, fcl_c99);
7015 postfix_expression.set_location (initializer.get_location ());
7016 break;
7020 /* It must be a primary-expression. */
7021 postfix_expression
7022 = cp_parser_primary_expression (parser, address_p, cast_p,
7023 /*template_arg_p=*/false,
7024 decltype_p,
7025 &idk);
7027 break;
7030 /* Note that we don't need to worry about calling build_cplus_new on a
7031 class-valued CALL_EXPR in decltype when it isn't the end of the
7032 postfix-expression; unary_complex_lvalue will take care of that for
7033 all these cases. */
7035 /* Keep looping until the postfix-expression is complete. */
7036 while (true)
7038 if (idk == CP_ID_KIND_UNQUALIFIED
7039 && identifier_p (postfix_expression)
7040 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7041 /* It is not a Koenig lookup function call. */
7042 postfix_expression
7043 = unqualified_name_lookup_error (postfix_expression);
7045 /* Peek at the next token. */
7046 token = cp_lexer_peek_token (parser->lexer);
7048 switch (token->type)
7050 case CPP_OPEN_SQUARE:
7051 if (cp_next_tokens_can_be_std_attribute_p (parser))
7053 cp_parser_error (parser,
7054 "two consecutive %<[%> shall "
7055 "only introduce an attribute");
7056 return error_mark_node;
7058 postfix_expression
7059 = cp_parser_postfix_open_square_expression (parser,
7060 postfix_expression,
7061 false,
7062 decltype_p);
7063 postfix_expression.set_range (start_loc,
7064 postfix_expression.get_location ());
7066 idk = CP_ID_KIND_NONE;
7067 is_member_access = false;
7068 break;
7070 case CPP_OPEN_PAREN:
7071 /* postfix-expression ( expression-list [opt] ) */
7073 bool koenig_p;
7074 bool is_builtin_constant_p;
7075 bool saved_integral_constant_expression_p = false;
7076 bool saved_non_integral_constant_expression_p = false;
7077 tsubst_flags_t complain = complain_flags (decltype_p);
7078 vec<tree, va_gc> *args;
7079 location_t close_paren_loc = UNKNOWN_LOCATION;
7081 is_member_access = false;
7083 is_builtin_constant_p
7084 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7085 if (is_builtin_constant_p)
7087 /* The whole point of __builtin_constant_p is to allow
7088 non-constant expressions to appear as arguments. */
7089 saved_integral_constant_expression_p
7090 = parser->integral_constant_expression_p;
7091 saved_non_integral_constant_expression_p
7092 = parser->non_integral_constant_expression_p;
7093 parser->integral_constant_expression_p = false;
7095 args = (cp_parser_parenthesized_expression_list
7096 (parser, non_attr,
7097 /*cast_p=*/false, /*allow_expansion_p=*/true,
7098 /*non_constant_p=*/NULL,
7099 /*close_paren_loc=*/&close_paren_loc,
7100 /*wrap_locations_p=*/true));
7101 if (is_builtin_constant_p)
7103 parser->integral_constant_expression_p
7104 = saved_integral_constant_expression_p;
7105 parser->non_integral_constant_expression_p
7106 = saved_non_integral_constant_expression_p;
7109 if (args == NULL)
7111 postfix_expression = error_mark_node;
7112 break;
7115 /* Function calls are not permitted in
7116 constant-expressions. */
7117 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7118 && cp_parser_non_integral_constant_expression (parser,
7119 NIC_FUNC_CALL))
7121 postfix_expression = error_mark_node;
7122 release_tree_vector (args);
7123 break;
7126 koenig_p = false;
7127 if (idk == CP_ID_KIND_UNQUALIFIED
7128 || idk == CP_ID_KIND_TEMPLATE_ID)
7130 if (identifier_p (postfix_expression))
7132 if (!args->is_empty ())
7134 koenig_p = true;
7135 if (!any_type_dependent_arguments_p (args))
7136 postfix_expression
7137 = perform_koenig_lookup (postfix_expression, args,
7138 complain);
7140 else
7141 postfix_expression
7142 = unqualified_fn_lookup_error (postfix_expression);
7144 /* We do not perform argument-dependent lookup if
7145 normal lookup finds a non-function, in accordance
7146 with the expected resolution of DR 218. */
7147 else if (!args->is_empty ()
7148 && is_overloaded_fn (postfix_expression))
7150 tree fn = get_first_fn (postfix_expression);
7151 fn = STRIP_TEMPLATE (fn);
7153 /* Do not do argument dependent lookup if regular
7154 lookup finds a member function or a block-scope
7155 function declaration. [basic.lookup.argdep]/3 */
7156 if (!DECL_FUNCTION_MEMBER_P (fn)
7157 && !DECL_LOCAL_FUNCTION_P (fn))
7159 koenig_p = true;
7160 if (!any_type_dependent_arguments_p (args))
7161 postfix_expression
7162 = perform_koenig_lookup (postfix_expression, args,
7163 complain);
7168 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7169 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7170 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7171 && vec_safe_length (args) == 3)
7173 tree arg0 = (*args)[0];
7174 tree arg1 = (*args)[1];
7175 tree arg2 = (*args)[2];
7176 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7177 | (literal_integer_zerop (arg2) << 2));
7178 warn_for_memset (input_location, arg0, arg2, literal_mask);
7181 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7183 tree instance = TREE_OPERAND (postfix_expression, 0);
7184 tree fn = TREE_OPERAND (postfix_expression, 1);
7186 if (processing_template_decl
7187 && (type_dependent_object_expression_p (instance)
7188 || (!BASELINK_P (fn)
7189 && TREE_CODE (fn) != FIELD_DECL)
7190 || type_dependent_expression_p (fn)
7191 || any_type_dependent_arguments_p (args)))
7193 maybe_generic_this_capture (instance, fn);
7194 postfix_expression
7195 = build_min_nt_call_vec (postfix_expression, args);
7196 release_tree_vector (args);
7197 break;
7200 if (BASELINK_P (fn))
7202 postfix_expression
7203 = (build_new_method_call
7204 (instance, fn, &args, NULL_TREE,
7205 (idk == CP_ID_KIND_QUALIFIED
7206 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7207 : LOOKUP_NORMAL),
7208 /*fn_p=*/NULL,
7209 complain));
7211 else
7212 postfix_expression
7213 = finish_call_expr (postfix_expression, &args,
7214 /*disallow_virtual=*/false,
7215 /*koenig_p=*/false,
7216 complain);
7218 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7219 || TREE_CODE (postfix_expression) == MEMBER_REF
7220 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7221 postfix_expression = (build_offset_ref_call_from_tree
7222 (postfix_expression, &args,
7223 complain));
7224 else if (idk == CP_ID_KIND_QUALIFIED)
7225 /* A call to a static class member, or a namespace-scope
7226 function. */
7227 postfix_expression
7228 = finish_call_expr (postfix_expression, &args,
7229 /*disallow_virtual=*/true,
7230 koenig_p,
7231 complain);
7232 else
7233 /* All other function calls. */
7234 postfix_expression
7235 = finish_call_expr (postfix_expression, &args,
7236 /*disallow_virtual=*/false,
7237 koenig_p,
7238 complain);
7240 if (close_paren_loc != UNKNOWN_LOCATION)
7242 location_t combined_loc = make_location (token->location,
7243 start_loc,
7244 close_paren_loc);
7245 postfix_expression.set_location (combined_loc);
7248 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7249 idk = CP_ID_KIND_NONE;
7251 release_tree_vector (args);
7253 break;
7255 case CPP_DOT:
7256 case CPP_DEREF:
7257 /* postfix-expression . template [opt] id-expression
7258 postfix-expression . pseudo-destructor-name
7259 postfix-expression -> template [opt] id-expression
7260 postfix-expression -> pseudo-destructor-name */
7262 /* Consume the `.' or `->' operator. */
7263 cp_lexer_consume_token (parser->lexer);
7265 postfix_expression
7266 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7267 postfix_expression,
7268 false, &idk, loc);
7270 is_member_access = true;
7271 break;
7273 case CPP_PLUS_PLUS:
7274 /* postfix-expression ++ */
7275 /* Consume the `++' token. */
7276 cp_lexer_consume_token (parser->lexer);
7277 /* Generate a representation for the complete expression. */
7278 postfix_expression
7279 = finish_increment_expr (postfix_expression,
7280 POSTINCREMENT_EXPR);
7281 /* Increments may not appear in constant-expressions. */
7282 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7283 postfix_expression = error_mark_node;
7284 idk = CP_ID_KIND_NONE;
7285 is_member_access = false;
7286 break;
7288 case CPP_MINUS_MINUS:
7289 /* postfix-expression -- */
7290 /* Consume the `--' token. */
7291 cp_lexer_consume_token (parser->lexer);
7292 /* Generate a representation for the complete expression. */
7293 postfix_expression
7294 = finish_increment_expr (postfix_expression,
7295 POSTDECREMENT_EXPR);
7296 /* Decrements may not appear in constant-expressions. */
7297 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7298 postfix_expression = error_mark_node;
7299 idk = CP_ID_KIND_NONE;
7300 is_member_access = false;
7301 break;
7303 default:
7304 if (pidk_return != NULL)
7305 * pidk_return = idk;
7306 if (member_access_only_p)
7307 return is_member_access
7308 ? postfix_expression
7309 : cp_expr (error_mark_node);
7310 else
7311 return postfix_expression;
7315 /* We should never get here. */
7316 gcc_unreachable ();
7317 return error_mark_node;
7320 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7321 by cp_parser_builtin_offsetof. We're looking for
7323 postfix-expression [ expression ]
7324 postfix-expression [ braced-init-list ] (C++11)
7326 FOR_OFFSETOF is set if we're being called in that context, which
7327 changes how we deal with integer constant expressions. */
7329 static tree
7330 cp_parser_postfix_open_square_expression (cp_parser *parser,
7331 tree postfix_expression,
7332 bool for_offsetof,
7333 bool decltype_p)
7335 tree index = NULL_TREE;
7336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7337 bool saved_greater_than_is_operator_p;
7339 /* Consume the `[' token. */
7340 cp_lexer_consume_token (parser->lexer);
7342 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7343 parser->greater_than_is_operator_p = true;
7345 /* Parse the index expression. */
7346 /* ??? For offsetof, there is a question of what to allow here. If
7347 offsetof is not being used in an integral constant expression context,
7348 then we *could* get the right answer by computing the value at runtime.
7349 If we are in an integral constant expression context, then we might
7350 could accept any constant expression; hard to say without analysis.
7351 Rather than open the barn door too wide right away, allow only integer
7352 constant expressions here. */
7353 if (for_offsetof)
7354 index = cp_parser_constant_expression (parser);
7355 else
7357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7359 bool expr_nonconst_p;
7360 cp_lexer_set_source_position (parser->lexer);
7361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7362 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7364 else
7365 index = cp_parser_expression (parser);
7368 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7370 /* Look for the closing `]'. */
7371 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7373 /* Build the ARRAY_REF. */
7374 postfix_expression = grok_array_decl (loc, postfix_expression,
7375 index, decltype_p);
7377 /* When not doing offsetof, array references are not permitted in
7378 constant-expressions. */
7379 if (!for_offsetof
7380 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7381 postfix_expression = error_mark_node;
7383 return postfix_expression;
7386 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7387 dereference of incomplete type, returns true if error_mark_node should
7388 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7389 and *DEPENDENT_P. */
7391 bool
7392 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7393 bool *dependent_p)
7395 /* In a template, be permissive by treating an object expression
7396 of incomplete type as dependent (after a pedwarn). */
7397 diagnostic_t kind = (processing_template_decl
7398 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7400 switch (TREE_CODE (*postfix_expression))
7402 case CAST_EXPR:
7403 case REINTERPRET_CAST_EXPR:
7404 case CONST_CAST_EXPR:
7405 case STATIC_CAST_EXPR:
7406 case DYNAMIC_CAST_EXPR:
7407 case IMPLICIT_CONV_EXPR:
7408 case VIEW_CONVERT_EXPR:
7409 case NON_LVALUE_EXPR:
7410 kind = DK_ERROR;
7411 break;
7412 case OVERLOAD:
7413 /* Don't emit any diagnostic for OVERLOADs. */
7414 kind = DK_IGNORED;
7415 break;
7416 default:
7417 /* Avoid clobbering e.g. DECLs. */
7418 if (!EXPR_P (*postfix_expression))
7419 kind = DK_ERROR;
7420 break;
7423 if (kind == DK_IGNORED)
7424 return false;
7426 location_t exploc = location_of (*postfix_expression);
7427 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7428 if (!MAYBE_CLASS_TYPE_P (*scope))
7429 return true;
7430 if (kind == DK_ERROR)
7431 *scope = *postfix_expression = error_mark_node;
7432 else if (processing_template_decl)
7434 *dependent_p = true;
7435 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7437 return false;
7440 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7441 by cp_parser_builtin_offsetof. We're looking for
7443 postfix-expression . template [opt] id-expression
7444 postfix-expression . pseudo-destructor-name
7445 postfix-expression -> template [opt] id-expression
7446 postfix-expression -> pseudo-destructor-name
7448 FOR_OFFSETOF is set if we're being called in that context. That sorta
7449 limits what of the above we'll actually accept, but nevermind.
7450 TOKEN_TYPE is the "." or "->" token, which will already have been
7451 removed from the stream. */
7453 static tree
7454 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7455 enum cpp_ttype token_type,
7456 cp_expr postfix_expression,
7457 bool for_offsetof, cp_id_kind *idk,
7458 location_t location)
7460 tree name;
7461 bool dependent_p;
7462 bool pseudo_destructor_p;
7463 tree scope = NULL_TREE;
7464 location_t start_loc = postfix_expression.get_start ();
7466 /* If this is a `->' operator, dereference the pointer. */
7467 if (token_type == CPP_DEREF)
7468 postfix_expression = build_x_arrow (location, postfix_expression,
7469 tf_warning_or_error);
7470 /* Check to see whether or not the expression is type-dependent and
7471 not the current instantiation. */
7472 dependent_p = type_dependent_object_expression_p (postfix_expression);
7473 /* The identifier following the `->' or `.' is not qualified. */
7474 parser->scope = NULL_TREE;
7475 parser->qualifying_scope = NULL_TREE;
7476 parser->object_scope = NULL_TREE;
7477 *idk = CP_ID_KIND_NONE;
7479 /* Enter the scope corresponding to the type of the object
7480 given by the POSTFIX_EXPRESSION. */
7481 if (!dependent_p)
7483 scope = TREE_TYPE (postfix_expression);
7484 /* According to the standard, no expression should ever have
7485 reference type. Unfortunately, we do not currently match
7486 the standard in this respect in that our internal representation
7487 of an expression may have reference type even when the standard
7488 says it does not. Therefore, we have to manually obtain the
7489 underlying type here. */
7490 scope = non_reference (scope);
7491 /* The type of the POSTFIX_EXPRESSION must be complete. */
7492 /* Unlike the object expression in other contexts, *this is not
7493 required to be of complete type for purposes of class member
7494 access (5.2.5) outside the member function body. */
7495 if (postfix_expression != current_class_ref
7496 && scope != error_mark_node
7497 && !(processing_template_decl
7498 && current_class_type
7499 && (same_type_ignoring_top_level_qualifiers_p
7500 (scope, current_class_type))))
7502 scope = complete_type (scope);
7503 if (!COMPLETE_TYPE_P (scope)
7504 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7505 &dependent_p))
7506 return error_mark_node;
7509 if (!dependent_p)
7511 /* Let the name lookup machinery know that we are processing a
7512 class member access expression. */
7513 parser->context->object_type = scope;
7514 /* If something went wrong, we want to be able to discern that case,
7515 as opposed to the case where there was no SCOPE due to the type
7516 of expression being dependent. */
7517 if (!scope)
7518 scope = error_mark_node;
7519 /* If the SCOPE was erroneous, make the various semantic analysis
7520 functions exit quickly -- and without issuing additional error
7521 messages. */
7522 if (scope == error_mark_node)
7523 postfix_expression = error_mark_node;
7527 if (dependent_p)
7528 /* Tell cp_parser_lookup_name that there was an object, even though it's
7529 type-dependent. */
7530 parser->context->object_type = unknown_type_node;
7532 /* Assume this expression is not a pseudo-destructor access. */
7533 pseudo_destructor_p = false;
7535 /* If the SCOPE is a scalar type, then, if this is a valid program,
7536 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7537 is type dependent, it can be pseudo-destructor-name or something else.
7538 Try to parse it as pseudo-destructor-name first. */
7539 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7541 tree s;
7542 tree type;
7544 cp_parser_parse_tentatively (parser);
7545 /* Parse the pseudo-destructor-name. */
7546 s = NULL_TREE;
7547 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7548 &s, &type);
7549 if (dependent_p
7550 && (cp_parser_error_occurred (parser)
7551 || !SCALAR_TYPE_P (type)))
7552 cp_parser_abort_tentative_parse (parser);
7553 else if (cp_parser_parse_definitely (parser))
7555 pseudo_destructor_p = true;
7556 postfix_expression
7557 = finish_pseudo_destructor_expr (postfix_expression,
7558 s, type, location);
7562 if (!pseudo_destructor_p)
7564 /* If the SCOPE is not a scalar type, we are looking at an
7565 ordinary class member access expression, rather than a
7566 pseudo-destructor-name. */
7567 bool template_p;
7568 cp_token *token = cp_lexer_peek_token (parser->lexer);
7569 /* Parse the id-expression. */
7570 name = (cp_parser_id_expression
7571 (parser,
7572 cp_parser_optional_template_keyword (parser),
7573 /*check_dependency_p=*/true,
7574 &template_p,
7575 /*declarator_p=*/false,
7576 /*optional_p=*/false));
7577 /* In general, build a SCOPE_REF if the member name is qualified.
7578 However, if the name was not dependent and has already been
7579 resolved; there is no need to build the SCOPE_REF. For example;
7581 struct X { void f(); };
7582 template <typename T> void f(T* t) { t->X::f(); }
7584 Even though "t" is dependent, "X::f" is not and has been resolved
7585 to a BASELINK; there is no need to include scope information. */
7587 /* But we do need to remember that there was an explicit scope for
7588 virtual function calls. */
7589 if (parser->scope)
7590 *idk = CP_ID_KIND_QUALIFIED;
7592 /* If the name is a template-id that names a type, we will get a
7593 TYPE_DECL here. That is invalid code. */
7594 if (TREE_CODE (name) == TYPE_DECL)
7596 error_at (token->location, "invalid use of %qD", name);
7597 postfix_expression = error_mark_node;
7599 else
7601 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7603 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7605 error_at (token->location, "%<%D::%D%> is not a class member",
7606 parser->scope, name);
7607 postfix_expression = error_mark_node;
7609 else
7610 name = build_qualified_name (/*type=*/NULL_TREE,
7611 parser->scope,
7612 name,
7613 template_p);
7614 parser->scope = NULL_TREE;
7615 parser->qualifying_scope = NULL_TREE;
7616 parser->object_scope = NULL_TREE;
7618 if (parser->scope && name && BASELINK_P (name))
7619 adjust_result_of_qualified_name_lookup
7620 (name, parser->scope, scope);
7621 postfix_expression
7622 = finish_class_member_access_expr (postfix_expression, name,
7623 template_p,
7624 tf_warning_or_error);
7625 /* Build a location e.g.:
7626 ptr->access_expr
7627 ~~~^~~~~~~~~~~~~
7628 where the caret is at the deref token, ranging from
7629 the start of postfix_expression to the end of the access expr. */
7630 location_t end_loc
7631 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7632 location_t combined_loc
7633 = make_location (input_location, start_loc, end_loc);
7634 protected_set_expr_location (postfix_expression, combined_loc);
7638 /* We no longer need to look up names in the scope of the object on
7639 the left-hand side of the `.' or `->' operator. */
7640 parser->context->object_type = NULL_TREE;
7642 /* Outside of offsetof, these operators may not appear in
7643 constant-expressions. */
7644 if (!for_offsetof
7645 && (cp_parser_non_integral_constant_expression
7646 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7647 postfix_expression = error_mark_node;
7649 return postfix_expression;
7652 /* Parse a parenthesized expression-list.
7654 expression-list:
7655 assignment-expression
7656 expression-list, assignment-expression
7658 attribute-list:
7659 expression-list
7660 identifier
7661 identifier, expression-list
7663 CAST_P is true if this expression is the target of a cast.
7665 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7666 argument pack.
7668 WRAP_LOCATIONS_P is true if expressions within this list for which
7669 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7670 their source locations.
7672 Returns a vector of trees. Each element is a representation of an
7673 assignment-expression. NULL is returned if the ( and or ) are
7674 missing. An empty, but allocated, vector is returned on no
7675 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7676 if we are parsing an attribute list for an attribute that wants a
7677 plain identifier argument, normal_attr for an attribute that wants
7678 an expression, or non_attr if we aren't parsing an attribute list. If
7679 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7680 not all of the expressions in the list were constant.
7681 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7682 will be written to with the location of the closing parenthesis. If
7683 an error occurs, it may or may not be written to. */
7685 static vec<tree, va_gc> *
7686 cp_parser_parenthesized_expression_list (cp_parser* parser,
7687 int is_attribute_list,
7688 bool cast_p,
7689 bool allow_expansion_p,
7690 bool *non_constant_p,
7691 location_t *close_paren_loc,
7692 bool wrap_locations_p)
7694 vec<tree, va_gc> *expression_list;
7695 bool fold_expr_p = is_attribute_list != non_attr;
7696 tree identifier = NULL_TREE;
7697 bool saved_greater_than_is_operator_p;
7699 /* Assume all the expressions will be constant. */
7700 if (non_constant_p)
7701 *non_constant_p = false;
7703 matching_parens parens;
7704 if (!parens.require_open (parser))
7705 return NULL;
7707 expression_list = make_tree_vector ();
7709 /* Within a parenthesized expression, a `>' token is always
7710 the greater-than operator. */
7711 saved_greater_than_is_operator_p
7712 = parser->greater_than_is_operator_p;
7713 parser->greater_than_is_operator_p = true;
7715 cp_expr expr (NULL_TREE);
7717 /* Consume expressions until there are no more. */
7718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7719 while (true)
7721 /* At the beginning of attribute lists, check to see if the
7722 next token is an identifier. */
7723 if (is_attribute_list == id_attr
7724 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7726 cp_token *token;
7728 /* Consume the identifier. */
7729 token = cp_lexer_consume_token (parser->lexer);
7730 /* Save the identifier. */
7731 identifier = token->u.value;
7733 else
7735 bool expr_non_constant_p;
7737 /* Parse the next assignment-expression. */
7738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7740 /* A braced-init-list. */
7741 cp_lexer_set_source_position (parser->lexer);
7742 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7743 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7744 if (non_constant_p && expr_non_constant_p)
7745 *non_constant_p = true;
7747 else if (non_constant_p)
7749 expr = (cp_parser_constant_expression
7750 (parser, /*allow_non_constant_p=*/true,
7751 &expr_non_constant_p));
7752 if (expr_non_constant_p)
7753 *non_constant_p = true;
7755 else
7756 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7757 cast_p);
7759 if (fold_expr_p)
7760 expr = instantiate_non_dependent_expr (expr);
7762 /* If we have an ellipsis, then this is an expression
7763 expansion. */
7764 if (allow_expansion_p
7765 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7767 /* Consume the `...'. */
7768 cp_lexer_consume_token (parser->lexer);
7770 /* Build the argument pack. */
7771 expr = make_pack_expansion (expr);
7774 if (wrap_locations_p)
7775 expr.maybe_add_location_wrapper ();
7777 /* Add it to the list. We add error_mark_node
7778 expressions to the list, so that we can still tell if
7779 the correct form for a parenthesized expression-list
7780 is found. That gives better errors. */
7781 vec_safe_push (expression_list, expr.get_value ());
7783 if (expr == error_mark_node)
7784 goto skip_comma;
7787 /* After the first item, attribute lists look the same as
7788 expression lists. */
7789 is_attribute_list = non_attr;
7791 get_comma:;
7792 /* If the next token isn't a `,', then we are done. */
7793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7794 break;
7796 /* Otherwise, consume the `,' and keep going. */
7797 cp_lexer_consume_token (parser->lexer);
7800 if (close_paren_loc)
7801 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7803 if (!parens.require_close (parser))
7805 int ending;
7807 skip_comma:;
7808 /* We try and resync to an unnested comma, as that will give the
7809 user better diagnostics. */
7810 ending = cp_parser_skip_to_closing_parenthesis (parser,
7811 /*recovering=*/true,
7812 /*or_comma=*/true,
7813 /*consume_paren=*/true);
7814 if (ending < 0)
7815 goto get_comma;
7816 if (!ending)
7818 parser->greater_than_is_operator_p
7819 = saved_greater_than_is_operator_p;
7820 return NULL;
7824 parser->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p;
7827 if (identifier)
7828 vec_safe_insert (expression_list, 0, identifier);
7830 return expression_list;
7833 /* Parse a pseudo-destructor-name.
7835 pseudo-destructor-name:
7836 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7837 :: [opt] nested-name-specifier template template-id :: ~ type-name
7838 :: [opt] nested-name-specifier [opt] ~ type-name
7840 If either of the first two productions is used, sets *SCOPE to the
7841 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7842 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7843 or ERROR_MARK_NODE if the parse fails. */
7845 static void
7846 cp_parser_pseudo_destructor_name (cp_parser* parser,
7847 tree object,
7848 tree* scope,
7849 tree* type)
7851 bool nested_name_specifier_p;
7853 /* Handle ~auto. */
7854 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7855 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7856 && !type_dependent_expression_p (object))
7858 if (cxx_dialect < cxx14)
7859 pedwarn (input_location, 0,
7860 "%<~auto%> only available with "
7861 "-std=c++14 or -std=gnu++14");
7862 cp_lexer_consume_token (parser->lexer);
7863 cp_lexer_consume_token (parser->lexer);
7864 *scope = NULL_TREE;
7865 *type = TREE_TYPE (object);
7866 return;
7869 /* Assume that things will not work out. */
7870 *type = error_mark_node;
7872 /* Look for the optional `::' operator. */
7873 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7874 /* Look for the optional nested-name-specifier. */
7875 nested_name_specifier_p
7876 = (cp_parser_nested_name_specifier_opt (parser,
7877 /*typename_keyword_p=*/false,
7878 /*check_dependency_p=*/true,
7879 /*type_p=*/false,
7880 /*is_declaration=*/false)
7881 != NULL_TREE);
7882 /* Now, if we saw a nested-name-specifier, we might be doing the
7883 second production. */
7884 if (nested_name_specifier_p
7885 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7887 /* Consume the `template' keyword. */
7888 cp_lexer_consume_token (parser->lexer);
7889 /* Parse the template-id. */
7890 cp_parser_template_id (parser,
7891 /*template_keyword_p=*/true,
7892 /*check_dependency_p=*/false,
7893 class_type,
7894 /*is_declaration=*/true);
7895 /* Look for the `::' token. */
7896 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7898 /* If the next token is not a `~', then there might be some
7899 additional qualification. */
7900 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7902 /* At this point, we're looking for "type-name :: ~". The type-name
7903 must not be a class-name, since this is a pseudo-destructor. So,
7904 it must be either an enum-name, or a typedef-name -- both of which
7905 are just identifiers. So, we peek ahead to check that the "::"
7906 and "~" tokens are present; if they are not, then we can avoid
7907 calling type_name. */
7908 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7909 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7910 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7912 cp_parser_error (parser, "non-scalar type");
7913 return;
7916 /* Look for the type-name. */
7917 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7918 if (*scope == error_mark_node)
7919 return;
7921 /* Look for the `::' token. */
7922 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7924 else
7925 *scope = NULL_TREE;
7927 /* Look for the `~'. */
7928 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7930 /* Once we see the ~, this has to be a pseudo-destructor. */
7931 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7932 cp_parser_commit_to_topmost_tentative_parse (parser);
7934 /* Look for the type-name again. We are not responsible for
7935 checking that it matches the first type-name. */
7936 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7939 /* Parse a unary-expression.
7941 unary-expression:
7942 postfix-expression
7943 ++ cast-expression
7944 -- cast-expression
7945 unary-operator cast-expression
7946 sizeof unary-expression
7947 sizeof ( type-id )
7948 alignof ( type-id ) [C++0x]
7949 new-expression
7950 delete-expression
7952 GNU Extensions:
7954 unary-expression:
7955 __extension__ cast-expression
7956 __alignof__ unary-expression
7957 __alignof__ ( type-id )
7958 alignof unary-expression [C++0x]
7959 __real__ cast-expression
7960 __imag__ cast-expression
7961 && identifier
7962 sizeof ( type-id ) { initializer-list , [opt] }
7963 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7964 __alignof__ ( type-id ) { initializer-list , [opt] }
7966 ADDRESS_P is true iff the unary-expression is appearing as the
7967 operand of the `&' operator. CAST_P is true if this expression is
7968 the target of a cast.
7970 Returns a representation of the expression. */
7972 static cp_expr
7973 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7974 bool address_p, bool cast_p, bool decltype_p)
7976 cp_token *token;
7977 enum tree_code unary_operator;
7979 /* Peek at the next token. */
7980 token = cp_lexer_peek_token (parser->lexer);
7981 /* Some keywords give away the kind of expression. */
7982 if (token->type == CPP_KEYWORD)
7984 enum rid keyword = token->keyword;
7986 switch (keyword)
7988 case RID_ALIGNOF:
7989 case RID_SIZEOF:
7991 tree operand, ret;
7992 enum tree_code op;
7993 location_t start_loc = token->location;
7995 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7996 bool std_alignof = id_equal (token->u.value, "alignof");
7998 /* Consume the token. */
7999 cp_lexer_consume_token (parser->lexer);
8000 /* Parse the operand. */
8001 operand = cp_parser_sizeof_operand (parser, keyword);
8003 if (TYPE_P (operand))
8004 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8005 true);
8006 else
8008 /* ISO C++ defines alignof only with types, not with
8009 expressions. So pedwarn if alignof is used with a non-
8010 type expression. However, __alignof__ is ok. */
8011 if (std_alignof)
8012 pedwarn (token->location, OPT_Wpedantic,
8013 "ISO C++ does not allow %<alignof%> "
8014 "with a non-type");
8016 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8018 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8019 SIZEOF_EXPR with the original operand. */
8020 if (op == SIZEOF_EXPR && ret != error_mark_node)
8022 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8024 if (!processing_template_decl && TYPE_P (operand))
8026 ret = build_min (SIZEOF_EXPR, size_type_node,
8027 build1 (NOP_EXPR, operand,
8028 error_mark_node));
8029 SIZEOF_EXPR_TYPE_P (ret) = 1;
8031 else
8032 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8033 TREE_SIDE_EFFECTS (ret) = 0;
8034 TREE_READONLY (ret) = 1;
8038 /* Construct a location e.g. :
8039 alignof (expr)
8040 ^~~~~~~~~~~~~~
8041 with start == caret at the start of the "alignof"/"sizeof"
8042 token, with the endpoint at the final closing paren. */
8043 location_t finish_loc
8044 = cp_lexer_previous_token (parser->lexer)->location;
8045 location_t compound_loc
8046 = make_location (start_loc, start_loc, finish_loc);
8048 cp_expr ret_expr (ret);
8049 ret_expr.set_location (compound_loc);
8050 ret_expr = ret_expr.maybe_add_location_wrapper ();
8051 return ret_expr;
8054 case RID_NEW:
8055 return cp_parser_new_expression (parser);
8057 case RID_DELETE:
8058 return cp_parser_delete_expression (parser);
8060 case RID_EXTENSION:
8062 /* The saved value of the PEDANTIC flag. */
8063 int saved_pedantic;
8064 tree expr;
8066 /* Save away the PEDANTIC flag. */
8067 cp_parser_extension_opt (parser, &saved_pedantic);
8068 /* Parse the cast-expression. */
8069 expr = cp_parser_simple_cast_expression (parser);
8070 /* Restore the PEDANTIC flag. */
8071 pedantic = saved_pedantic;
8073 return expr;
8076 case RID_REALPART:
8077 case RID_IMAGPART:
8079 tree expression;
8081 /* Consume the `__real__' or `__imag__' token. */
8082 cp_lexer_consume_token (parser->lexer);
8083 /* Parse the cast-expression. */
8084 expression = cp_parser_simple_cast_expression (parser);
8085 /* Create the complete representation. */
8086 return build_x_unary_op (token->location,
8087 (keyword == RID_REALPART
8088 ? REALPART_EXPR : IMAGPART_EXPR),
8089 expression,
8090 tf_warning_or_error);
8092 break;
8094 case RID_TRANSACTION_ATOMIC:
8095 case RID_TRANSACTION_RELAXED:
8096 return cp_parser_transaction_expression (parser, keyword);
8098 case RID_NOEXCEPT:
8100 tree expr;
8101 const char *saved_message;
8102 bool saved_integral_constant_expression_p;
8103 bool saved_non_integral_constant_expression_p;
8104 bool saved_greater_than_is_operator_p;
8106 location_t start_loc = token->location;
8108 cp_lexer_consume_token (parser->lexer);
8109 matching_parens parens;
8110 parens.require_open (parser);
8112 saved_message = parser->type_definition_forbidden_message;
8113 parser->type_definition_forbidden_message
8114 = G_("types may not be defined in %<noexcept%> expressions");
8116 saved_integral_constant_expression_p
8117 = parser->integral_constant_expression_p;
8118 saved_non_integral_constant_expression_p
8119 = parser->non_integral_constant_expression_p;
8120 parser->integral_constant_expression_p = false;
8122 saved_greater_than_is_operator_p
8123 = parser->greater_than_is_operator_p;
8124 parser->greater_than_is_operator_p = true;
8126 ++cp_unevaluated_operand;
8127 ++c_inhibit_evaluation_warnings;
8128 ++cp_noexcept_operand;
8129 expr = cp_parser_expression (parser);
8130 --cp_noexcept_operand;
8131 --c_inhibit_evaluation_warnings;
8132 --cp_unevaluated_operand;
8134 parser->greater_than_is_operator_p
8135 = saved_greater_than_is_operator_p;
8137 parser->integral_constant_expression_p
8138 = saved_integral_constant_expression_p;
8139 parser->non_integral_constant_expression_p
8140 = saved_non_integral_constant_expression_p;
8142 parser->type_definition_forbidden_message = saved_message;
8144 location_t finish_loc
8145 = cp_lexer_peek_token (parser->lexer)->location;
8146 parens.require_close (parser);
8148 /* Construct a location of the form:
8149 noexcept (expr)
8150 ^~~~~~~~~~~~~~~
8151 with start == caret, finishing at the close-paren. */
8152 location_t noexcept_loc
8153 = make_location (start_loc, start_loc, finish_loc);
8155 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8156 noexcept_loc);
8159 default:
8160 break;
8164 /* Look for the `:: new' and `:: delete', which also signal the
8165 beginning of a new-expression, or delete-expression,
8166 respectively. If the next token is `::', then it might be one of
8167 these. */
8168 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8170 enum rid keyword;
8172 /* See if the token after the `::' is one of the keywords in
8173 which we're interested. */
8174 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8175 /* If it's `new', we have a new-expression. */
8176 if (keyword == RID_NEW)
8177 return cp_parser_new_expression (parser);
8178 /* Similarly, for `delete'. */
8179 else if (keyword == RID_DELETE)
8180 return cp_parser_delete_expression (parser);
8183 /* Look for a unary operator. */
8184 unary_operator = cp_parser_unary_operator (token);
8185 /* The `++' and `--' operators can be handled similarly, even though
8186 they are not technically unary-operators in the grammar. */
8187 if (unary_operator == ERROR_MARK)
8189 if (token->type == CPP_PLUS_PLUS)
8190 unary_operator = PREINCREMENT_EXPR;
8191 else if (token->type == CPP_MINUS_MINUS)
8192 unary_operator = PREDECREMENT_EXPR;
8193 /* Handle the GNU address-of-label extension. */
8194 else if (cp_parser_allow_gnu_extensions_p (parser)
8195 && token->type == CPP_AND_AND)
8197 tree identifier;
8198 tree expression;
8199 location_t start_loc = token->location;
8201 /* Consume the '&&' token. */
8202 cp_lexer_consume_token (parser->lexer);
8203 /* Look for the identifier. */
8204 location_t finish_loc
8205 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8206 identifier = cp_parser_identifier (parser);
8207 /* Construct a location of the form:
8208 &&label
8209 ^~~~~~~
8210 with caret==start at the "&&", finish at the end of the label. */
8211 location_t combined_loc
8212 = make_location (start_loc, start_loc, finish_loc);
8213 /* Create an expression representing the address. */
8214 expression = finish_label_address_expr (identifier, combined_loc);
8215 if (cp_parser_non_integral_constant_expression (parser,
8216 NIC_ADDR_LABEL))
8217 expression = error_mark_node;
8218 return expression;
8221 if (unary_operator != ERROR_MARK)
8223 cp_expr cast_expression;
8224 cp_expr expression = error_mark_node;
8225 non_integral_constant non_constant_p = NIC_NONE;
8226 location_t loc = token->location;
8227 tsubst_flags_t complain = complain_flags (decltype_p);
8229 /* Consume the operator token. */
8230 token = cp_lexer_consume_token (parser->lexer);
8231 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8233 /* Parse the cast-expression. */
8234 cast_expression
8235 = cp_parser_cast_expression (parser,
8236 unary_operator == ADDR_EXPR,
8237 /*cast_p=*/false,
8238 /*decltype*/false,
8239 pidk);
8241 /* Make a location:
8242 OP_TOKEN CAST_EXPRESSION
8243 ^~~~~~~~~~~~~~~~~~~~~~~~~
8244 with start==caret at the operator token, and
8245 extending to the end of the cast_expression. */
8246 loc = make_location (loc, loc, cast_expression.get_finish ());
8248 /* Now, build an appropriate representation. */
8249 switch (unary_operator)
8251 case INDIRECT_REF:
8252 non_constant_p = NIC_STAR;
8253 expression = build_x_indirect_ref (loc, cast_expression,
8254 RO_UNARY_STAR,
8255 complain);
8256 /* TODO: build_x_indirect_ref does not always honor the
8257 location, so ensure it is set. */
8258 expression.set_location (loc);
8259 break;
8261 case ADDR_EXPR:
8262 non_constant_p = NIC_ADDR;
8263 /* Fall through. */
8264 case BIT_NOT_EXPR:
8265 expression = build_x_unary_op (loc, unary_operator,
8266 cast_expression,
8267 complain);
8268 /* TODO: build_x_unary_op does not always honor the location,
8269 so ensure it is set. */
8270 expression.set_location (loc);
8271 break;
8273 case PREINCREMENT_EXPR:
8274 case PREDECREMENT_EXPR:
8275 non_constant_p = unary_operator == PREINCREMENT_EXPR
8276 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8277 /* Fall through. */
8278 case NEGATE_EXPR:
8279 /* Immediately fold negation of a constant, unless the constant is 0
8280 (since -0 == 0) or it would overflow. */
8281 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8282 && CONSTANT_CLASS_P (cast_expression)
8283 && !integer_zerop (cast_expression)
8284 && !TREE_OVERFLOW (cast_expression))
8286 tree folded = fold_build1 (unary_operator,
8287 TREE_TYPE (cast_expression),
8288 cast_expression);
8289 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8291 expression = cp_expr (folded, loc);
8292 break;
8295 /* Fall through. */
8296 case UNARY_PLUS_EXPR:
8297 case TRUTH_NOT_EXPR:
8298 expression = finish_unary_op_expr (loc, unary_operator,
8299 cast_expression, complain);
8300 break;
8302 default:
8303 gcc_unreachable ();
8306 if (non_constant_p != NIC_NONE
8307 && cp_parser_non_integral_constant_expression (parser,
8308 non_constant_p))
8309 expression = error_mark_node;
8311 return expression;
8314 return cp_parser_postfix_expression (parser, address_p, cast_p,
8315 /*member_access_only_p=*/false,
8316 decltype_p,
8317 pidk);
8320 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8321 unary-operator, the corresponding tree code is returned. */
8323 static enum tree_code
8324 cp_parser_unary_operator (cp_token* token)
8326 switch (token->type)
8328 case CPP_MULT:
8329 return INDIRECT_REF;
8331 case CPP_AND:
8332 return ADDR_EXPR;
8334 case CPP_PLUS:
8335 return UNARY_PLUS_EXPR;
8337 case CPP_MINUS:
8338 return NEGATE_EXPR;
8340 case CPP_NOT:
8341 return TRUTH_NOT_EXPR;
8343 case CPP_COMPL:
8344 return BIT_NOT_EXPR;
8346 default:
8347 return ERROR_MARK;
8351 /* Parse a new-expression.
8353 new-expression:
8354 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8355 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8357 Returns a representation of the expression. */
8359 static tree
8360 cp_parser_new_expression (cp_parser* parser)
8362 bool global_scope_p;
8363 vec<tree, va_gc> *placement;
8364 tree type;
8365 vec<tree, va_gc> *initializer;
8366 tree nelts = NULL_TREE;
8367 tree ret;
8369 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8371 /* Look for the optional `::' operator. */
8372 global_scope_p
8373 = (cp_parser_global_scope_opt (parser,
8374 /*current_scope_valid_p=*/false)
8375 != NULL_TREE);
8376 /* Look for the `new' operator. */
8377 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8378 /* There's no easy way to tell a new-placement from the
8379 `( type-id )' construct. */
8380 cp_parser_parse_tentatively (parser);
8381 /* Look for a new-placement. */
8382 placement = cp_parser_new_placement (parser);
8383 /* If that didn't work out, there's no new-placement. */
8384 if (!cp_parser_parse_definitely (parser))
8386 if (placement != NULL)
8387 release_tree_vector (placement);
8388 placement = NULL;
8391 /* If the next token is a `(', then we have a parenthesized
8392 type-id. */
8393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8395 cp_token *token;
8396 const char *saved_message = parser->type_definition_forbidden_message;
8398 /* Consume the `('. */
8399 matching_parens parens;
8400 parens.consume_open (parser);
8402 /* Parse the type-id. */
8403 parser->type_definition_forbidden_message
8404 = G_("types may not be defined in a new-expression");
8406 type_id_in_expr_sentinel s (parser);
8407 type = cp_parser_type_id (parser);
8409 parser->type_definition_forbidden_message = saved_message;
8411 /* Look for the closing `)'. */
8412 parens.require_close (parser);
8413 token = cp_lexer_peek_token (parser->lexer);
8414 /* There should not be a direct-new-declarator in this production,
8415 but GCC used to allowed this, so we check and emit a sensible error
8416 message for this case. */
8417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8419 error_at (token->location,
8420 "array bound forbidden after parenthesized type-id");
8421 inform (token->location,
8422 "try removing the parentheses around the type-id");
8423 cp_parser_direct_new_declarator (parser);
8426 /* Otherwise, there must be a new-type-id. */
8427 else
8428 type = cp_parser_new_type_id (parser, &nelts);
8430 /* If the next token is a `(' or '{', then we have a new-initializer. */
8431 cp_token *token = cp_lexer_peek_token (parser->lexer);
8432 if (token->type == CPP_OPEN_PAREN
8433 || token->type == CPP_OPEN_BRACE)
8434 initializer = cp_parser_new_initializer (parser);
8435 else
8436 initializer = NULL;
8438 /* A new-expression may not appear in an integral constant
8439 expression. */
8440 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8441 ret = error_mark_node;
8442 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8443 of a new-type-id or type-id of a new-expression, the new-expression shall
8444 contain a new-initializer of the form ( assignment-expression )".
8445 Additionally, consistently with the spirit of DR 1467, we want to accept
8446 'new auto { 2 }' too. */
8447 else if ((ret = type_uses_auto (type))
8448 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8449 && (vec_safe_length (initializer) != 1
8450 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8451 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8453 error_at (token->location,
8454 "initialization of new-expression for type %<auto%> "
8455 "requires exactly one element");
8456 ret = error_mark_node;
8458 else
8460 /* Construct a location e.g.:
8461 ptr = new int[100]
8462 ^~~~~~~~~~~~
8463 with caret == start at the start of the "new" token, and the end
8464 at the end of the final token we consumed. */
8465 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8466 location_t end_loc = get_finish (end_tok->location);
8467 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8469 /* Create a representation of the new-expression. */
8470 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8471 tf_warning_or_error);
8472 protected_set_expr_location (ret, combined_loc);
8475 if (placement != NULL)
8476 release_tree_vector (placement);
8477 if (initializer != NULL)
8478 release_tree_vector (initializer);
8480 return ret;
8483 /* Parse a new-placement.
8485 new-placement:
8486 ( expression-list )
8488 Returns the same representation as for an expression-list. */
8490 static vec<tree, va_gc> *
8491 cp_parser_new_placement (cp_parser* parser)
8493 vec<tree, va_gc> *expression_list;
8495 /* Parse the expression-list. */
8496 expression_list = (cp_parser_parenthesized_expression_list
8497 (parser, non_attr, /*cast_p=*/false,
8498 /*allow_expansion_p=*/true,
8499 /*non_constant_p=*/NULL));
8501 if (expression_list && expression_list->is_empty ())
8502 error ("expected expression-list or type-id");
8504 return expression_list;
8507 /* Parse a new-type-id.
8509 new-type-id:
8510 type-specifier-seq new-declarator [opt]
8512 Returns the TYPE allocated. If the new-type-id indicates an array
8513 type, *NELTS is set to the number of elements in the last array
8514 bound; the TYPE will not include the last array bound. */
8516 static tree
8517 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8519 cp_decl_specifier_seq type_specifier_seq;
8520 cp_declarator *new_declarator;
8521 cp_declarator *declarator;
8522 cp_declarator *outer_declarator;
8523 const char *saved_message;
8525 /* The type-specifier sequence must not contain type definitions.
8526 (It cannot contain declarations of new types either, but if they
8527 are not definitions we will catch that because they are not
8528 complete.) */
8529 saved_message = parser->type_definition_forbidden_message;
8530 parser->type_definition_forbidden_message
8531 = G_("types may not be defined in a new-type-id");
8532 /* Parse the type-specifier-seq. */
8533 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8534 /*is_trailing_return=*/false,
8535 &type_specifier_seq);
8536 /* Restore the old message. */
8537 parser->type_definition_forbidden_message = saved_message;
8539 if (type_specifier_seq.type == error_mark_node)
8540 return error_mark_node;
8542 /* Parse the new-declarator. */
8543 new_declarator = cp_parser_new_declarator_opt (parser);
8545 /* Determine the number of elements in the last array dimension, if
8546 any. */
8547 *nelts = NULL_TREE;
8548 /* Skip down to the last array dimension. */
8549 declarator = new_declarator;
8550 outer_declarator = NULL;
8551 while (declarator && (declarator->kind == cdk_pointer
8552 || declarator->kind == cdk_ptrmem))
8554 outer_declarator = declarator;
8555 declarator = declarator->declarator;
8557 while (declarator
8558 && declarator->kind == cdk_array
8559 && declarator->declarator
8560 && declarator->declarator->kind == cdk_array)
8562 outer_declarator = declarator;
8563 declarator = declarator->declarator;
8566 if (declarator && declarator->kind == cdk_array)
8568 *nelts = declarator->u.array.bounds;
8569 if (*nelts == error_mark_node)
8570 *nelts = integer_one_node;
8572 if (outer_declarator)
8573 outer_declarator->declarator = declarator->declarator;
8574 else
8575 new_declarator = NULL;
8578 return groktypename (&type_specifier_seq, new_declarator, false);
8581 /* Parse an (optional) new-declarator.
8583 new-declarator:
8584 ptr-operator new-declarator [opt]
8585 direct-new-declarator
8587 Returns the declarator. */
8589 static cp_declarator *
8590 cp_parser_new_declarator_opt (cp_parser* parser)
8592 enum tree_code code;
8593 tree type, std_attributes = NULL_TREE;
8594 cp_cv_quals cv_quals;
8596 /* We don't know if there's a ptr-operator next, or not. */
8597 cp_parser_parse_tentatively (parser);
8598 /* Look for a ptr-operator. */
8599 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8600 /* If that worked, look for more new-declarators. */
8601 if (cp_parser_parse_definitely (parser))
8603 cp_declarator *declarator;
8605 /* Parse another optional declarator. */
8606 declarator = cp_parser_new_declarator_opt (parser);
8608 declarator = cp_parser_make_indirect_declarator
8609 (code, type, cv_quals, declarator, std_attributes);
8611 return declarator;
8614 /* If the next token is a `[', there is a direct-new-declarator. */
8615 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8616 return cp_parser_direct_new_declarator (parser);
8618 return NULL;
8621 /* Parse a direct-new-declarator.
8623 direct-new-declarator:
8624 [ expression ]
8625 direct-new-declarator [constant-expression]
8629 static cp_declarator *
8630 cp_parser_direct_new_declarator (cp_parser* parser)
8632 cp_declarator *declarator = NULL;
8634 while (true)
8636 tree expression;
8637 cp_token *token;
8639 /* Look for the opening `['. */
8640 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8642 token = cp_lexer_peek_token (parser->lexer);
8643 expression = cp_parser_expression (parser);
8644 /* The standard requires that the expression have integral
8645 type. DR 74 adds enumeration types. We believe that the
8646 real intent is that these expressions be handled like the
8647 expression in a `switch' condition, which also allows
8648 classes with a single conversion to integral or
8649 enumeration type. */
8650 if (!processing_template_decl)
8652 expression
8653 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8654 expression,
8655 /*complain=*/true);
8656 if (!expression)
8658 error_at (token->location,
8659 "expression in new-declarator must have integral "
8660 "or enumeration type");
8661 expression = error_mark_node;
8665 /* Look for the closing `]'. */
8666 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8668 /* Add this bound to the declarator. */
8669 declarator = make_array_declarator (declarator, expression);
8671 /* If the next token is not a `[', then there are no more
8672 bounds. */
8673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8674 break;
8677 return declarator;
8680 /* Parse a new-initializer.
8682 new-initializer:
8683 ( expression-list [opt] )
8684 braced-init-list
8686 Returns a representation of the expression-list. */
8688 static vec<tree, va_gc> *
8689 cp_parser_new_initializer (cp_parser* parser)
8691 vec<tree, va_gc> *expression_list;
8693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8695 tree t;
8696 bool expr_non_constant_p;
8697 cp_lexer_set_source_position (parser->lexer);
8698 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8699 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8700 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8701 expression_list = make_tree_vector_single (t);
8703 else
8704 expression_list = (cp_parser_parenthesized_expression_list
8705 (parser, non_attr, /*cast_p=*/false,
8706 /*allow_expansion_p=*/true,
8707 /*non_constant_p=*/NULL));
8709 return expression_list;
8712 /* Parse a delete-expression.
8714 delete-expression:
8715 :: [opt] delete cast-expression
8716 :: [opt] delete [ ] cast-expression
8718 Returns a representation of the expression. */
8720 static tree
8721 cp_parser_delete_expression (cp_parser* parser)
8723 bool global_scope_p;
8724 bool array_p;
8725 tree expression;
8727 /* Look for the optional `::' operator. */
8728 global_scope_p
8729 = (cp_parser_global_scope_opt (parser,
8730 /*current_scope_valid_p=*/false)
8731 != NULL_TREE);
8732 /* Look for the `delete' keyword. */
8733 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8734 /* See if the array syntax is in use. */
8735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8737 /* Consume the `[' token. */
8738 cp_lexer_consume_token (parser->lexer);
8739 /* Look for the `]' token. */
8740 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8741 /* Remember that this is the `[]' construct. */
8742 array_p = true;
8744 else
8745 array_p = false;
8747 /* Parse the cast-expression. */
8748 expression = cp_parser_simple_cast_expression (parser);
8750 /* A delete-expression may not appear in an integral constant
8751 expression. */
8752 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8753 return error_mark_node;
8755 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8756 tf_warning_or_error);
8759 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8760 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8761 0 otherwise. */
8763 static int
8764 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8766 cp_token *token = cp_lexer_peek_token (parser->lexer);
8767 switch (token->type)
8769 case CPP_COMMA:
8770 case CPP_SEMICOLON:
8771 case CPP_QUERY:
8772 case CPP_COLON:
8773 case CPP_CLOSE_SQUARE:
8774 case CPP_CLOSE_PAREN:
8775 case CPP_CLOSE_BRACE:
8776 case CPP_OPEN_BRACE:
8777 case CPP_DOT:
8778 case CPP_DOT_STAR:
8779 case CPP_DEREF:
8780 case CPP_DEREF_STAR:
8781 case CPP_DIV:
8782 case CPP_MOD:
8783 case CPP_LSHIFT:
8784 case CPP_RSHIFT:
8785 case CPP_LESS:
8786 case CPP_GREATER:
8787 case CPP_LESS_EQ:
8788 case CPP_GREATER_EQ:
8789 case CPP_EQ_EQ:
8790 case CPP_NOT_EQ:
8791 case CPP_EQ:
8792 case CPP_MULT_EQ:
8793 case CPP_DIV_EQ:
8794 case CPP_MOD_EQ:
8795 case CPP_PLUS_EQ:
8796 case CPP_MINUS_EQ:
8797 case CPP_RSHIFT_EQ:
8798 case CPP_LSHIFT_EQ:
8799 case CPP_AND_EQ:
8800 case CPP_XOR_EQ:
8801 case CPP_OR_EQ:
8802 case CPP_XOR:
8803 case CPP_OR:
8804 case CPP_OR_OR:
8805 case CPP_EOF:
8806 case CPP_ELLIPSIS:
8807 return 0;
8809 case CPP_OPEN_PAREN:
8810 /* In ((type ()) () the last () isn't a valid cast-expression,
8811 so the whole must be parsed as postfix-expression. */
8812 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8813 != CPP_CLOSE_PAREN;
8815 case CPP_OPEN_SQUARE:
8816 /* '[' may start a primary-expression in obj-c++ and in C++11,
8817 as a lambda-expression, eg, '(void)[]{}'. */
8818 if (cxx_dialect >= cxx11)
8819 return -1;
8820 return c_dialect_objc ();
8822 case CPP_PLUS_PLUS:
8823 case CPP_MINUS_MINUS:
8824 /* '++' and '--' may or may not start a cast-expression:
8826 struct T { void operator++(int); };
8827 void f() { (T())++; }
8831 int a;
8832 (int)++a; */
8833 return -1;
8835 default:
8836 return 1;
8840 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8841 in the order: const_cast, static_cast, reinterpret_cast.
8843 Don't suggest dynamic_cast.
8845 Return the first legal cast kind found, or NULL otherwise. */
8847 static const char *
8848 get_cast_suggestion (tree dst_type, tree orig_expr)
8850 tree trial;
8852 /* Reuse the parser logic by attempting to build the various kinds of
8853 cast, with "complain" disabled.
8854 Identify the first such cast that is valid. */
8856 /* Don't attempt to run such logic within template processing. */
8857 if (processing_template_decl)
8858 return NULL;
8860 /* First try const_cast. */
8861 trial = build_const_cast (dst_type, orig_expr, tf_none);
8862 if (trial != error_mark_node)
8863 return "const_cast";
8865 /* If that fails, try static_cast. */
8866 trial = build_static_cast (dst_type, orig_expr, tf_none);
8867 if (trial != error_mark_node)
8868 return "static_cast";
8870 /* Finally, try reinterpret_cast. */
8871 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8872 if (trial != error_mark_node)
8873 return "reinterpret_cast";
8875 /* No such cast possible. */
8876 return NULL;
8879 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8880 suggesting how to convert a C-style cast of the form:
8882 (DST_TYPE)ORIG_EXPR
8884 to a C++-style cast.
8886 The primary range of RICHLOC is asssumed to be that of the original
8887 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8888 of the parens in the C-style cast. */
8890 static void
8891 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8892 location_t close_paren_loc, tree orig_expr,
8893 tree dst_type)
8895 /* This function is non-trivial, so bail out now if the warning isn't
8896 going to be emitted. */
8897 if (!warn_old_style_cast)
8898 return;
8900 /* Try to find a legal C++ cast, trying them in order:
8901 const_cast, static_cast, reinterpret_cast. */
8902 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8903 if (!cast_suggestion)
8904 return;
8906 /* Replace the open paren with "CAST_SUGGESTION<". */
8907 pretty_printer pp;
8908 pp_printf (&pp, "%s<", cast_suggestion);
8909 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8911 /* Replace the close paren with "> (". */
8912 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8914 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8915 rich_loc->add_fixit_insert_after (")");
8919 /* Parse a cast-expression.
8921 cast-expression:
8922 unary-expression
8923 ( type-id ) cast-expression
8925 ADDRESS_P is true iff the unary-expression is appearing as the
8926 operand of the `&' operator. CAST_P is true if this expression is
8927 the target of a cast.
8929 Returns a representation of the expression. */
8931 static cp_expr
8932 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8933 bool decltype_p, cp_id_kind * pidk)
8935 /* If it's a `(', then we might be looking at a cast. */
8936 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8938 tree type = NULL_TREE;
8939 cp_expr expr (NULL_TREE);
8940 int cast_expression = 0;
8941 const char *saved_message;
8943 /* There's no way to know yet whether or not this is a cast.
8944 For example, `(int (3))' is a unary-expression, while `(int)
8945 3' is a cast. So, we resort to parsing tentatively. */
8946 cp_parser_parse_tentatively (parser);
8947 /* Types may not be defined in a cast. */
8948 saved_message = parser->type_definition_forbidden_message;
8949 parser->type_definition_forbidden_message
8950 = G_("types may not be defined in casts");
8951 /* Consume the `('. */
8952 matching_parens parens;
8953 cp_token *open_paren = parens.consume_open (parser);
8954 location_t open_paren_loc = open_paren->location;
8955 location_t close_paren_loc = UNKNOWN_LOCATION;
8957 /* A very tricky bit is that `(struct S) { 3 }' is a
8958 compound-literal (which we permit in C++ as an extension).
8959 But, that construct is not a cast-expression -- it is a
8960 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8961 is legal; if the compound-literal were a cast-expression,
8962 you'd need an extra set of parentheses.) But, if we parse
8963 the type-id, and it happens to be a class-specifier, then we
8964 will commit to the parse at that point, because we cannot
8965 undo the action that is done when creating a new class. So,
8966 then we cannot back up and do a postfix-expression.
8968 Another tricky case is the following (c++/29234):
8970 struct S { void operator () (); };
8972 void foo ()
8974 ( S()() );
8977 As a type-id we parse the parenthesized S()() as a function
8978 returning a function, groktypename complains and we cannot
8979 back up in this case either.
8981 Therefore, we scan ahead to the closing `)', and check to see
8982 if the tokens after the `)' can start a cast-expression. Otherwise
8983 we are dealing with an unary-expression, a postfix-expression
8984 or something else.
8986 Yet another tricky case, in C++11, is the following (c++/54891):
8988 (void)[]{};
8990 The issue is that usually, besides the case of lambda-expressions,
8991 the parenthesized type-id cannot be followed by '[', and, eg, we
8992 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8993 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8994 we don't commit, we try a cast-expression, then an unary-expression.
8996 Save tokens so that we can put them back. */
8997 cp_lexer_save_tokens (parser->lexer);
8999 /* We may be looking at a cast-expression. */
9000 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9001 /*consume_paren=*/true))
9002 cast_expression
9003 = cp_parser_tokens_start_cast_expression (parser);
9005 /* Roll back the tokens we skipped. */
9006 cp_lexer_rollback_tokens (parser->lexer);
9007 /* If we aren't looking at a cast-expression, simulate an error so
9008 that the call to cp_parser_error_occurred below returns true. */
9009 if (!cast_expression)
9010 cp_parser_simulate_error (parser);
9011 else
9013 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9014 parser->in_type_id_in_expr_p = true;
9015 /* Look for the type-id. */
9016 type = cp_parser_type_id (parser);
9017 /* Look for the closing `)'. */
9018 cp_token *close_paren = parens.require_close (parser);
9019 if (close_paren)
9020 close_paren_loc = close_paren->location;
9021 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9024 /* Restore the saved message. */
9025 parser->type_definition_forbidden_message = saved_message;
9027 /* At this point this can only be either a cast or a
9028 parenthesized ctor such as `(T ())' that looks like a cast to
9029 function returning T. */
9030 if (!cp_parser_error_occurred (parser))
9032 /* Only commit if the cast-expression doesn't start with
9033 '++', '--', or '[' in C++11. */
9034 if (cast_expression > 0)
9035 cp_parser_commit_to_topmost_tentative_parse (parser);
9037 expr = cp_parser_cast_expression (parser,
9038 /*address_p=*/false,
9039 /*cast_p=*/true,
9040 /*decltype_p=*/false,
9041 pidk);
9043 if (cp_parser_parse_definitely (parser))
9045 /* Warn about old-style casts, if so requested. */
9046 if (warn_old_style_cast
9047 && !in_system_header_at (input_location)
9048 && !VOID_TYPE_P (type)
9049 && current_lang_name != lang_name_c)
9051 gcc_rich_location rich_loc (input_location);
9052 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9053 expr, type);
9054 warning_at (&rich_loc, OPT_Wold_style_cast,
9055 "use of old-style cast to %q#T", type);
9058 /* Only type conversions to integral or enumeration types
9059 can be used in constant-expressions. */
9060 if (!cast_valid_in_integral_constant_expression_p (type)
9061 && cp_parser_non_integral_constant_expression (parser,
9062 NIC_CAST))
9063 return error_mark_node;
9065 /* Perform the cast. */
9066 /* Make a location:
9067 (TYPE) EXPR
9068 ^~~~~~~~~~~
9069 with start==caret at the open paren, extending to the
9070 end of "expr". */
9071 location_t cast_loc = make_location (open_paren_loc,
9072 open_paren_loc,
9073 expr.get_finish ());
9074 expr = build_c_cast (cast_loc, type, expr);
9075 return expr;
9078 else
9079 cp_parser_abort_tentative_parse (parser);
9082 /* If we get here, then it's not a cast, so it must be a
9083 unary-expression. */
9084 return cp_parser_unary_expression (parser, pidk, address_p,
9085 cast_p, decltype_p);
9088 /* Parse a binary expression of the general form:
9090 pm-expression:
9091 cast-expression
9092 pm-expression .* cast-expression
9093 pm-expression ->* cast-expression
9095 multiplicative-expression:
9096 pm-expression
9097 multiplicative-expression * pm-expression
9098 multiplicative-expression / pm-expression
9099 multiplicative-expression % pm-expression
9101 additive-expression:
9102 multiplicative-expression
9103 additive-expression + multiplicative-expression
9104 additive-expression - multiplicative-expression
9106 shift-expression:
9107 additive-expression
9108 shift-expression << additive-expression
9109 shift-expression >> additive-expression
9111 relational-expression:
9112 shift-expression
9113 relational-expression < shift-expression
9114 relational-expression > shift-expression
9115 relational-expression <= shift-expression
9116 relational-expression >= shift-expression
9118 GNU Extension:
9120 relational-expression:
9121 relational-expression <? shift-expression
9122 relational-expression >? shift-expression
9124 equality-expression:
9125 relational-expression
9126 equality-expression == relational-expression
9127 equality-expression != relational-expression
9129 and-expression:
9130 equality-expression
9131 and-expression & equality-expression
9133 exclusive-or-expression:
9134 and-expression
9135 exclusive-or-expression ^ and-expression
9137 inclusive-or-expression:
9138 exclusive-or-expression
9139 inclusive-or-expression | exclusive-or-expression
9141 logical-and-expression:
9142 inclusive-or-expression
9143 logical-and-expression && inclusive-or-expression
9145 logical-or-expression:
9146 logical-and-expression
9147 logical-or-expression || logical-and-expression
9149 All these are implemented with a single function like:
9151 binary-expression:
9152 simple-cast-expression
9153 binary-expression <token> binary-expression
9155 CAST_P is true if this expression is the target of a cast.
9157 The binops_by_token map is used to get the tree codes for each <token> type.
9158 binary-expressions are associated according to a precedence table. */
9160 #define TOKEN_PRECEDENCE(token) \
9161 (((token->type == CPP_GREATER \
9162 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9163 && !parser->greater_than_is_operator_p) \
9164 ? PREC_NOT_OPERATOR \
9165 : binops_by_token[token->type].prec)
9167 static cp_expr
9168 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9169 bool no_toplevel_fold_p,
9170 bool decltype_p,
9171 enum cp_parser_prec prec,
9172 cp_id_kind * pidk)
9174 cp_parser_expression_stack stack;
9175 cp_parser_expression_stack_entry *sp = &stack[0];
9176 cp_parser_expression_stack_entry current;
9177 cp_expr rhs;
9178 cp_token *token;
9179 enum tree_code rhs_type;
9180 enum cp_parser_prec new_prec, lookahead_prec;
9181 tree overload;
9183 /* Parse the first expression. */
9184 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9185 ? TRUTH_NOT_EXPR : ERROR_MARK);
9186 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9187 cast_p, decltype_p, pidk);
9188 current.prec = prec;
9190 if (cp_parser_error_occurred (parser))
9191 return error_mark_node;
9193 for (;;)
9195 /* Get an operator token. */
9196 token = cp_lexer_peek_token (parser->lexer);
9198 if (warn_cxx11_compat
9199 && token->type == CPP_RSHIFT
9200 && !parser->greater_than_is_operator_p)
9202 if (warning_at (token->location, OPT_Wc__11_compat,
9203 "%<>>%> operator is treated"
9204 " as two right angle brackets in C++11"))
9205 inform (token->location,
9206 "suggest parentheses around %<>>%> expression");
9209 new_prec = TOKEN_PRECEDENCE (token);
9210 if (new_prec != PREC_NOT_OPERATOR
9211 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9212 /* This is a fold-expression; handle it later. */
9213 new_prec = PREC_NOT_OPERATOR;
9215 /* Popping an entry off the stack means we completed a subexpression:
9216 - either we found a token which is not an operator (`>' where it is not
9217 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9218 will happen repeatedly;
9219 - or, we found an operator which has lower priority. This is the case
9220 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9221 parsing `3 * 4'. */
9222 if (new_prec <= current.prec)
9224 if (sp == stack)
9225 break;
9226 else
9227 goto pop;
9230 get_rhs:
9231 current.tree_type = binops_by_token[token->type].tree_type;
9232 current.loc = token->location;
9234 /* We used the operator token. */
9235 cp_lexer_consume_token (parser->lexer);
9237 /* For "false && x" or "true || x", x will never be executed;
9238 disable warnings while evaluating it. */
9239 if (current.tree_type == TRUTH_ANDIF_EXPR)
9240 c_inhibit_evaluation_warnings +=
9241 cp_fully_fold (current.lhs) == truthvalue_false_node;
9242 else if (current.tree_type == TRUTH_ORIF_EXPR)
9243 c_inhibit_evaluation_warnings +=
9244 cp_fully_fold (current.lhs) == truthvalue_true_node;
9246 /* Extract another operand. It may be the RHS of this expression
9247 or the LHS of a new, higher priority expression. */
9248 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9249 ? TRUTH_NOT_EXPR : ERROR_MARK);
9250 rhs = cp_parser_simple_cast_expression (parser);
9252 /* Get another operator token. Look up its precedence to avoid
9253 building a useless (immediately popped) stack entry for common
9254 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9255 token = cp_lexer_peek_token (parser->lexer);
9256 lookahead_prec = TOKEN_PRECEDENCE (token);
9257 if (lookahead_prec != PREC_NOT_OPERATOR
9258 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9259 lookahead_prec = PREC_NOT_OPERATOR;
9260 if (lookahead_prec > new_prec)
9262 /* ... and prepare to parse the RHS of the new, higher priority
9263 expression. Since precedence levels on the stack are
9264 monotonically increasing, we do not have to care about
9265 stack overflows. */
9266 *sp = current;
9267 ++sp;
9268 current.lhs = rhs;
9269 current.lhs_type = rhs_type;
9270 current.prec = new_prec;
9271 new_prec = lookahead_prec;
9272 goto get_rhs;
9274 pop:
9275 lookahead_prec = new_prec;
9276 /* If the stack is not empty, we have parsed into LHS the right side
9277 (`4' in the example above) of an expression we had suspended.
9278 We can use the information on the stack to recover the LHS (`3')
9279 from the stack together with the tree code (`MULT_EXPR'), and
9280 the precedence of the higher level subexpression
9281 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9282 which will be used to actually build the additive expression. */
9283 rhs = current.lhs;
9284 rhs_type = current.lhs_type;
9285 --sp;
9286 current = *sp;
9289 /* Undo the disabling of warnings done above. */
9290 if (current.tree_type == TRUTH_ANDIF_EXPR)
9291 c_inhibit_evaluation_warnings -=
9292 cp_fully_fold (current.lhs) == truthvalue_false_node;
9293 else if (current.tree_type == TRUTH_ORIF_EXPR)
9294 c_inhibit_evaluation_warnings -=
9295 cp_fully_fold (current.lhs) == truthvalue_true_node;
9297 if (warn_logical_not_paren
9298 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9299 && current.lhs_type == TRUTH_NOT_EXPR
9300 /* Avoid warning for !!x == y. */
9301 && (TREE_CODE (current.lhs) != NE_EXPR
9302 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9303 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9304 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9305 /* Avoid warning for !b == y where b is boolean. */
9306 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9307 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9308 != BOOLEAN_TYPE))))
9309 /* Avoid warning for !!b == y where b is boolean. */
9310 && (!DECL_P (current.lhs)
9311 || TREE_TYPE (current.lhs) == NULL_TREE
9312 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9313 warn_logical_not_parentheses (current.loc, current.tree_type,
9314 current.lhs, maybe_constant_value (rhs));
9316 overload = NULL;
9318 location_t combined_loc = make_location (current.loc,
9319 current.lhs.get_start (),
9320 rhs.get_finish ());
9322 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9323 ERROR_MARK for everything that is not a binary expression.
9324 This makes warn_about_parentheses miss some warnings that
9325 involve unary operators. For unary expressions we should
9326 pass the correct tree_code unless the unary expression was
9327 surrounded by parentheses.
9329 if (no_toplevel_fold_p
9330 && lookahead_prec <= current.prec
9331 && sp == stack)
9333 if (current.lhs == error_mark_node || rhs == error_mark_node)
9334 current.lhs = error_mark_node;
9335 else
9337 current.lhs
9338 = build_min (current.tree_type,
9339 TREE_CODE_CLASS (current.tree_type)
9340 == tcc_comparison
9341 ? boolean_type_node : TREE_TYPE (current.lhs),
9342 current.lhs.get_value (), rhs.get_value ());
9343 SET_EXPR_LOCATION (current.lhs, combined_loc);
9346 else
9348 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9349 current.lhs, current.lhs_type,
9350 rhs, rhs_type, &overload,
9351 complain_flags (decltype_p));
9352 /* TODO: build_x_binary_op doesn't always honor the location. */
9353 current.lhs.set_location (combined_loc);
9355 current.lhs_type = current.tree_type;
9357 /* If the binary operator required the use of an overloaded operator,
9358 then this expression cannot be an integral constant-expression.
9359 An overloaded operator can be used even if both operands are
9360 otherwise permissible in an integral constant-expression if at
9361 least one of the operands is of enumeration type. */
9363 if (overload
9364 && cp_parser_non_integral_constant_expression (parser,
9365 NIC_OVERLOADED))
9366 return error_mark_node;
9369 return current.lhs;
9372 static cp_expr
9373 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9374 bool no_toplevel_fold_p,
9375 enum cp_parser_prec prec,
9376 cp_id_kind * pidk)
9378 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9379 /*decltype*/false, prec, pidk);
9382 /* Parse the `? expression : assignment-expression' part of a
9383 conditional-expression. The LOGICAL_OR_EXPR is the
9384 logical-or-expression that started the conditional-expression.
9385 Returns a representation of the entire conditional-expression.
9387 This routine is used by cp_parser_assignment_expression.
9389 ? expression : assignment-expression
9391 GNU Extensions:
9393 ? : assignment-expression */
9395 static tree
9396 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9398 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9399 cp_expr assignment_expr;
9400 struct cp_token *token;
9401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9403 /* Consume the `?' token. */
9404 cp_lexer_consume_token (parser->lexer);
9405 token = cp_lexer_peek_token (parser->lexer);
9406 if (cp_parser_allow_gnu_extensions_p (parser)
9407 && token->type == CPP_COLON)
9409 pedwarn (token->location, OPT_Wpedantic,
9410 "ISO C++ does not allow ?: with omitted middle operand");
9411 /* Implicit true clause. */
9412 expr = NULL_TREE;
9413 c_inhibit_evaluation_warnings +=
9414 folded_logical_or_expr == truthvalue_true_node;
9415 warn_for_omitted_condop (token->location, logical_or_expr);
9417 else
9419 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9420 parser->colon_corrects_to_scope_p = false;
9421 /* Parse the expression. */
9422 c_inhibit_evaluation_warnings +=
9423 folded_logical_or_expr == truthvalue_false_node;
9424 expr = cp_parser_expression (parser);
9425 c_inhibit_evaluation_warnings +=
9426 ((folded_logical_or_expr == truthvalue_true_node)
9427 - (folded_logical_or_expr == truthvalue_false_node));
9428 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9431 /* The next token should be a `:'. */
9432 cp_parser_require (parser, CPP_COLON, RT_COLON);
9433 /* Parse the assignment-expression. */
9434 assignment_expr = cp_parser_assignment_expression (parser);
9435 c_inhibit_evaluation_warnings -=
9436 folded_logical_or_expr == truthvalue_true_node;
9438 /* Make a location:
9439 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9440 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9441 with the caret at the "?", ranging from the start of
9442 the logical_or_expr to the end of the assignment_expr. */
9443 loc = make_location (loc,
9444 logical_or_expr.get_start (),
9445 assignment_expr.get_finish ());
9447 /* Build the conditional-expression. */
9448 return build_x_conditional_expr (loc, logical_or_expr,
9449 expr,
9450 assignment_expr,
9451 tf_warning_or_error);
9454 /* Parse an assignment-expression.
9456 assignment-expression:
9457 conditional-expression
9458 logical-or-expression assignment-operator assignment_expression
9459 throw-expression
9461 CAST_P is true if this expression is the target of a cast.
9462 DECLTYPE_P is true if this expression is the operand of decltype.
9464 Returns a representation for the expression. */
9466 static cp_expr
9467 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9468 bool cast_p, bool decltype_p)
9470 cp_expr expr;
9472 /* If the next token is the `throw' keyword, then we're looking at
9473 a throw-expression. */
9474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9475 expr = cp_parser_throw_expression (parser);
9476 /* Otherwise, it must be that we are looking at a
9477 logical-or-expression. */
9478 else
9480 /* Parse the binary expressions (logical-or-expression). */
9481 expr = cp_parser_binary_expression (parser, cast_p, false,
9482 decltype_p,
9483 PREC_NOT_OPERATOR, pidk);
9484 /* If the next token is a `?' then we're actually looking at a
9485 conditional-expression. */
9486 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9487 return cp_parser_question_colon_clause (parser, expr);
9488 else
9490 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9492 /* If it's an assignment-operator, we're using the second
9493 production. */
9494 enum tree_code assignment_operator
9495 = cp_parser_assignment_operator_opt (parser);
9496 if (assignment_operator != ERROR_MARK)
9498 bool non_constant_p;
9500 /* Parse the right-hand side of the assignment. */
9501 cp_expr rhs = cp_parser_initializer_clause (parser,
9502 &non_constant_p);
9504 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9507 /* An assignment may not appear in a
9508 constant-expression. */
9509 if (cp_parser_non_integral_constant_expression (parser,
9510 NIC_ASSIGNMENT))
9511 return error_mark_node;
9512 /* Build the assignment expression. Its default
9513 location:
9514 LHS = RHS
9515 ~~~~^~~~~
9516 is the location of the '=' token as the
9517 caret, ranging from the start of the lhs to the
9518 end of the rhs. */
9519 loc = make_location (loc,
9520 expr.get_start (),
9521 rhs.get_finish ());
9522 expr = build_x_modify_expr (loc, expr,
9523 assignment_operator,
9524 rhs,
9525 complain_flags (decltype_p));
9526 /* TODO: build_x_modify_expr doesn't honor the location,
9527 so we must set it here. */
9528 expr.set_location (loc);
9533 return expr;
9536 /* Parse an (optional) assignment-operator.
9538 assignment-operator: one of
9539 = *= /= %= += -= >>= <<= &= ^= |=
9541 GNU Extension:
9543 assignment-operator: one of
9544 <?= >?=
9546 If the next token is an assignment operator, the corresponding tree
9547 code is returned, and the token is consumed. For example, for
9548 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9549 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9550 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9551 operator, ERROR_MARK is returned. */
9553 static enum tree_code
9554 cp_parser_assignment_operator_opt (cp_parser* parser)
9556 enum tree_code op;
9557 cp_token *token;
9559 /* Peek at the next token. */
9560 token = cp_lexer_peek_token (parser->lexer);
9562 switch (token->type)
9564 case CPP_EQ:
9565 op = NOP_EXPR;
9566 break;
9568 case CPP_MULT_EQ:
9569 op = MULT_EXPR;
9570 break;
9572 case CPP_DIV_EQ:
9573 op = TRUNC_DIV_EXPR;
9574 break;
9576 case CPP_MOD_EQ:
9577 op = TRUNC_MOD_EXPR;
9578 break;
9580 case CPP_PLUS_EQ:
9581 op = PLUS_EXPR;
9582 break;
9584 case CPP_MINUS_EQ:
9585 op = MINUS_EXPR;
9586 break;
9588 case CPP_RSHIFT_EQ:
9589 op = RSHIFT_EXPR;
9590 break;
9592 case CPP_LSHIFT_EQ:
9593 op = LSHIFT_EXPR;
9594 break;
9596 case CPP_AND_EQ:
9597 op = BIT_AND_EXPR;
9598 break;
9600 case CPP_XOR_EQ:
9601 op = BIT_XOR_EXPR;
9602 break;
9604 case CPP_OR_EQ:
9605 op = BIT_IOR_EXPR;
9606 break;
9608 default:
9609 /* Nothing else is an assignment operator. */
9610 op = ERROR_MARK;
9613 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9614 if (op != ERROR_MARK
9615 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9616 op = ERROR_MARK;
9618 /* If it was an assignment operator, consume it. */
9619 if (op != ERROR_MARK)
9620 cp_lexer_consume_token (parser->lexer);
9622 return op;
9625 /* Parse an expression.
9627 expression:
9628 assignment-expression
9629 expression , assignment-expression
9631 CAST_P is true if this expression is the target of a cast.
9632 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9633 except possibly parenthesized or on the RHS of a comma (N3276).
9635 Returns a representation of the expression. */
9637 static cp_expr
9638 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9639 bool cast_p, bool decltype_p)
9641 cp_expr expression = NULL_TREE;
9642 location_t loc = UNKNOWN_LOCATION;
9644 while (true)
9646 cp_expr assignment_expression;
9648 /* Parse the next assignment-expression. */
9649 assignment_expression
9650 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9652 /* We don't create a temporary for a call that is the immediate operand
9653 of decltype or on the RHS of a comma. But when we see a comma, we
9654 need to create a temporary for a call on the LHS. */
9655 if (decltype_p && !processing_template_decl
9656 && TREE_CODE (assignment_expression) == CALL_EXPR
9657 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9658 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9659 assignment_expression
9660 = build_cplus_new (TREE_TYPE (assignment_expression),
9661 assignment_expression, tf_warning_or_error);
9663 /* If this is the first assignment-expression, we can just
9664 save it away. */
9665 if (!expression)
9666 expression = assignment_expression;
9667 else
9669 /* Create a location with caret at the comma, ranging
9670 from the start of the LHS to the end of the RHS. */
9671 loc = make_location (loc,
9672 expression.get_start (),
9673 assignment_expression.get_finish ());
9674 expression = build_x_compound_expr (loc, expression,
9675 assignment_expression,
9676 complain_flags (decltype_p));
9677 expression.set_location (loc);
9679 /* If the next token is not a comma, or we're in a fold-expression, then
9680 we are done with the expression. */
9681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9682 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9683 break;
9684 /* Consume the `,'. */
9685 loc = cp_lexer_peek_token (parser->lexer)->location;
9686 cp_lexer_consume_token (parser->lexer);
9687 /* A comma operator cannot appear in a constant-expression. */
9688 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9689 expression = error_mark_node;
9692 return expression;
9695 /* Parse a constant-expression.
9697 constant-expression:
9698 conditional-expression
9700 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9701 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9702 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9703 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9704 only parse a conditional-expression, otherwise parse an
9705 assignment-expression. See below for rationale. */
9707 static cp_expr
9708 cp_parser_constant_expression (cp_parser* parser,
9709 bool allow_non_constant_p,
9710 bool *non_constant_p,
9711 bool strict_p)
9713 bool saved_integral_constant_expression_p;
9714 bool saved_allow_non_integral_constant_expression_p;
9715 bool saved_non_integral_constant_expression_p;
9716 cp_expr expression;
9718 /* It might seem that we could simply parse the
9719 conditional-expression, and then check to see if it were
9720 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9721 one that the compiler can figure out is constant, possibly after
9722 doing some simplifications or optimizations. The standard has a
9723 precise definition of constant-expression, and we must honor
9724 that, even though it is somewhat more restrictive.
9726 For example:
9728 int i[(2, 3)];
9730 is not a legal declaration, because `(2, 3)' is not a
9731 constant-expression. The `,' operator is forbidden in a
9732 constant-expression. However, GCC's constant-folding machinery
9733 will fold this operation to an INTEGER_CST for `3'. */
9735 /* Save the old settings. */
9736 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9737 saved_allow_non_integral_constant_expression_p
9738 = parser->allow_non_integral_constant_expression_p;
9739 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9740 /* We are now parsing a constant-expression. */
9741 parser->integral_constant_expression_p = true;
9742 parser->allow_non_integral_constant_expression_p
9743 = (allow_non_constant_p || cxx_dialect >= cxx11);
9744 parser->non_integral_constant_expression_p = false;
9745 /* Although the grammar says "conditional-expression", when not STRICT_P,
9746 we parse an "assignment-expression", which also permits
9747 "throw-expression" and the use of assignment operators. In the case
9748 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9749 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9750 actually essential that we look for an assignment-expression.
9751 For example, cp_parser_initializer_clauses uses this function to
9752 determine whether a particular assignment-expression is in fact
9753 constant. */
9754 if (strict_p)
9756 /* Parse the binary expressions (logical-or-expression). */
9757 expression = cp_parser_binary_expression (parser, false, false, false,
9758 PREC_NOT_OPERATOR, NULL);
9759 /* If the next token is a `?' then we're actually looking at
9760 a conditional-expression; otherwise we're done. */
9761 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9762 expression = cp_parser_question_colon_clause (parser, expression);
9764 else
9765 expression = cp_parser_assignment_expression (parser);
9766 /* Restore the old settings. */
9767 parser->integral_constant_expression_p
9768 = saved_integral_constant_expression_p;
9769 parser->allow_non_integral_constant_expression_p
9770 = saved_allow_non_integral_constant_expression_p;
9771 if (cxx_dialect >= cxx11)
9773 /* Require an rvalue constant expression here; that's what our
9774 callers expect. Reference constant expressions are handled
9775 separately in e.g. cp_parser_template_argument. */
9776 tree decay = expression;
9777 if (TREE_TYPE (expression)
9778 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9779 decay = build_address (expression);
9780 bool is_const = potential_rvalue_constant_expression (decay);
9781 parser->non_integral_constant_expression_p = !is_const;
9782 if (!is_const && !allow_non_constant_p)
9783 require_potential_rvalue_constant_expression (decay);
9785 if (allow_non_constant_p)
9786 *non_constant_p = parser->non_integral_constant_expression_p;
9787 parser->non_integral_constant_expression_p
9788 = saved_non_integral_constant_expression_p;
9790 return expression;
9793 /* Parse __builtin_offsetof.
9795 offsetof-expression:
9796 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9798 offsetof-member-designator:
9799 id-expression
9800 | offsetof-member-designator "." id-expression
9801 | offsetof-member-designator "[" expression "]"
9802 | offsetof-member-designator "->" id-expression */
9804 static cp_expr
9805 cp_parser_builtin_offsetof (cp_parser *parser)
9807 int save_ice_p, save_non_ice_p;
9808 tree type;
9809 cp_expr expr;
9810 cp_id_kind dummy;
9811 cp_token *token;
9812 location_t finish_loc;
9814 /* We're about to accept non-integral-constant things, but will
9815 definitely yield an integral constant expression. Save and
9816 restore these values around our local parsing. */
9817 save_ice_p = parser->integral_constant_expression_p;
9818 save_non_ice_p = parser->non_integral_constant_expression_p;
9820 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9822 /* Consume the "__builtin_offsetof" token. */
9823 cp_lexer_consume_token (parser->lexer);
9824 /* Consume the opening `('. */
9825 matching_parens parens;
9826 parens.require_open (parser);
9827 /* Parse the type-id. */
9828 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9830 const char *saved_message = parser->type_definition_forbidden_message;
9831 parser->type_definition_forbidden_message
9832 = G_("types may not be defined within __builtin_offsetof");
9833 type = cp_parser_type_id (parser);
9834 parser->type_definition_forbidden_message = saved_message;
9836 /* Look for the `,'. */
9837 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9838 token = cp_lexer_peek_token (parser->lexer);
9840 /* Build the (type *)null that begins the traditional offsetof macro. */
9841 tree object_ptr
9842 = build_static_cast (build_pointer_type (type), null_pointer_node,
9843 tf_warning_or_error);
9845 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9846 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9847 true, &dummy, token->location);
9848 while (true)
9850 token = cp_lexer_peek_token (parser->lexer);
9851 switch (token->type)
9853 case CPP_OPEN_SQUARE:
9854 /* offsetof-member-designator "[" expression "]" */
9855 expr = cp_parser_postfix_open_square_expression (parser, expr,
9856 true, false);
9857 break;
9859 case CPP_DEREF:
9860 /* offsetof-member-designator "->" identifier */
9861 expr = grok_array_decl (token->location, expr,
9862 integer_zero_node, false);
9863 /* FALLTHRU */
9865 case CPP_DOT:
9866 /* offsetof-member-designator "." identifier */
9867 cp_lexer_consume_token (parser->lexer);
9868 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9869 expr, true, &dummy,
9870 token->location);
9871 break;
9873 case CPP_CLOSE_PAREN:
9874 /* Consume the ")" token. */
9875 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9876 cp_lexer_consume_token (parser->lexer);
9877 goto success;
9879 default:
9880 /* Error. We know the following require will fail, but
9881 that gives the proper error message. */
9882 parens.require_close (parser);
9883 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9884 expr = error_mark_node;
9885 goto failure;
9889 success:
9890 /* Make a location of the form:
9891 __builtin_offsetof (struct s, f)
9892 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9893 with caret at the type-id, ranging from the start of the
9894 "_builtin_offsetof" token to the close paren. */
9895 loc = make_location (loc, start_loc, finish_loc);
9896 /* The result will be an INTEGER_CST, so we need to explicitly
9897 preserve the location. */
9898 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9900 failure:
9901 parser->integral_constant_expression_p = save_ice_p;
9902 parser->non_integral_constant_expression_p = save_non_ice_p;
9904 expr = expr.maybe_add_location_wrapper ();
9905 return expr;
9908 /* Parse a trait expression.
9910 Returns a representation of the expression, the underlying type
9911 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9913 static cp_expr
9914 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9916 cp_trait_kind kind;
9917 tree type1, type2 = NULL_TREE;
9918 bool binary = false;
9919 bool variadic = false;
9921 switch (keyword)
9923 case RID_HAS_NOTHROW_ASSIGN:
9924 kind = CPTK_HAS_NOTHROW_ASSIGN;
9925 break;
9926 case RID_HAS_NOTHROW_CONSTRUCTOR:
9927 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9928 break;
9929 case RID_HAS_NOTHROW_COPY:
9930 kind = CPTK_HAS_NOTHROW_COPY;
9931 break;
9932 case RID_HAS_TRIVIAL_ASSIGN:
9933 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9934 break;
9935 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9936 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9937 break;
9938 case RID_HAS_TRIVIAL_COPY:
9939 kind = CPTK_HAS_TRIVIAL_COPY;
9940 break;
9941 case RID_HAS_TRIVIAL_DESTRUCTOR:
9942 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9943 break;
9944 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9945 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9946 break;
9947 case RID_HAS_VIRTUAL_DESTRUCTOR:
9948 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9949 break;
9950 case RID_IS_ABSTRACT:
9951 kind = CPTK_IS_ABSTRACT;
9952 break;
9953 case RID_IS_AGGREGATE:
9954 kind = CPTK_IS_AGGREGATE;
9955 break;
9956 case RID_IS_BASE_OF:
9957 kind = CPTK_IS_BASE_OF;
9958 binary = true;
9959 break;
9960 case RID_IS_CLASS:
9961 kind = CPTK_IS_CLASS;
9962 break;
9963 case RID_IS_EMPTY:
9964 kind = CPTK_IS_EMPTY;
9965 break;
9966 case RID_IS_ENUM:
9967 kind = CPTK_IS_ENUM;
9968 break;
9969 case RID_IS_FINAL:
9970 kind = CPTK_IS_FINAL;
9971 break;
9972 case RID_IS_LITERAL_TYPE:
9973 kind = CPTK_IS_LITERAL_TYPE;
9974 break;
9975 case RID_IS_POD:
9976 kind = CPTK_IS_POD;
9977 break;
9978 case RID_IS_POLYMORPHIC:
9979 kind = CPTK_IS_POLYMORPHIC;
9980 break;
9981 case RID_IS_SAME_AS:
9982 kind = CPTK_IS_SAME_AS;
9983 binary = true;
9984 break;
9985 case RID_IS_STD_LAYOUT:
9986 kind = CPTK_IS_STD_LAYOUT;
9987 break;
9988 case RID_IS_TRIVIAL:
9989 kind = CPTK_IS_TRIVIAL;
9990 break;
9991 case RID_IS_TRIVIALLY_ASSIGNABLE:
9992 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9993 binary = true;
9994 break;
9995 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9996 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9997 variadic = true;
9998 break;
9999 case RID_IS_TRIVIALLY_COPYABLE:
10000 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10001 break;
10002 case RID_IS_UNION:
10003 kind = CPTK_IS_UNION;
10004 break;
10005 case RID_UNDERLYING_TYPE:
10006 kind = CPTK_UNDERLYING_TYPE;
10007 break;
10008 case RID_BASES:
10009 kind = CPTK_BASES;
10010 break;
10011 case RID_DIRECT_BASES:
10012 kind = CPTK_DIRECT_BASES;
10013 break;
10014 case RID_IS_ASSIGNABLE:
10015 kind = CPTK_IS_ASSIGNABLE;
10016 binary = true;
10017 break;
10018 case RID_IS_CONSTRUCTIBLE:
10019 kind = CPTK_IS_CONSTRUCTIBLE;
10020 variadic = true;
10021 break;
10022 default:
10023 gcc_unreachable ();
10026 /* Get location of initial token. */
10027 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10029 /* Consume the token. */
10030 cp_lexer_consume_token (parser->lexer);
10032 matching_parens parens;
10033 parens.require_open (parser);
10036 type_id_in_expr_sentinel s (parser);
10037 type1 = cp_parser_type_id (parser);
10040 if (type1 == error_mark_node)
10041 return error_mark_node;
10043 if (binary)
10045 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10048 type_id_in_expr_sentinel s (parser);
10049 type2 = cp_parser_type_id (parser);
10052 if (type2 == error_mark_node)
10053 return error_mark_node;
10055 else if (variadic)
10057 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10059 cp_lexer_consume_token (parser->lexer);
10060 tree elt = cp_parser_type_id (parser);
10061 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10063 cp_lexer_consume_token (parser->lexer);
10064 elt = make_pack_expansion (elt);
10066 if (elt == error_mark_node)
10067 return error_mark_node;
10068 type2 = tree_cons (NULL_TREE, elt, type2);
10072 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10073 parens.require_close (parser);
10075 /* Construct a location of the form:
10076 __is_trivially_copyable(_Tp)
10077 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10078 with start == caret, finishing at the close-paren. */
10079 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10081 /* Complete the trait expression, which may mean either processing
10082 the trait expr now or saving it for template instantiation. */
10083 switch (kind)
10085 case CPTK_UNDERLYING_TYPE:
10086 return cp_expr (finish_underlying_type (type1), trait_loc);
10087 case CPTK_BASES:
10088 return cp_expr (finish_bases (type1, false), trait_loc);
10089 case CPTK_DIRECT_BASES:
10090 return cp_expr (finish_bases (type1, true), trait_loc);
10091 default:
10092 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10096 /* Parse a lambda expression.
10098 lambda-expression:
10099 lambda-introducer lambda-declarator [opt] compound-statement
10101 Returns a representation of the expression. */
10103 static cp_expr
10104 cp_parser_lambda_expression (cp_parser* parser)
10106 tree lambda_expr = build_lambda_expr ();
10107 tree type;
10108 bool ok = true;
10109 cp_token *token = cp_lexer_peek_token (parser->lexer);
10110 cp_token_position start = 0;
10112 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10114 if (cp_unevaluated_operand)
10116 if (!token->error_reported)
10118 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10119 "lambda-expression in unevaluated context");
10120 token->error_reported = true;
10122 ok = false;
10124 else if (parser->in_template_argument_list_p)
10126 if (!token->error_reported)
10128 error_at (token->location, "lambda-expression in template-argument");
10129 token->error_reported = true;
10131 ok = false;
10134 /* We may be in the middle of deferred access check. Disable
10135 it now. */
10136 push_deferring_access_checks (dk_no_deferred);
10138 cp_parser_lambda_introducer (parser, lambda_expr);
10140 type = begin_lambda_type (lambda_expr);
10141 if (type == error_mark_node)
10142 return error_mark_node;
10144 record_lambda_scope (lambda_expr);
10146 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10147 determine_visibility (TYPE_NAME (type));
10149 /* Now that we've started the type, add the capture fields for any
10150 explicit captures. */
10151 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10154 /* Inside the class, surrounding template-parameter-lists do not apply. */
10155 unsigned int saved_num_template_parameter_lists
10156 = parser->num_template_parameter_lists;
10157 unsigned char in_statement = parser->in_statement;
10158 bool in_switch_statement_p = parser->in_switch_statement_p;
10159 bool fully_implicit_function_template_p
10160 = parser->fully_implicit_function_template_p;
10161 tree implicit_template_parms = parser->implicit_template_parms;
10162 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10163 bool auto_is_implicit_function_template_parm_p
10164 = parser->auto_is_implicit_function_template_parm_p;
10166 parser->num_template_parameter_lists = 0;
10167 parser->in_statement = 0;
10168 parser->in_switch_statement_p = false;
10169 parser->fully_implicit_function_template_p = false;
10170 parser->implicit_template_parms = 0;
10171 parser->implicit_template_scope = 0;
10172 parser->auto_is_implicit_function_template_parm_p = false;
10174 /* By virtue of defining a local class, a lambda expression has access to
10175 the private variables of enclosing classes. */
10177 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10179 if (ok && cp_parser_error_occurred (parser))
10180 ok = false;
10182 if (ok)
10184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10185 && cp_parser_start_tentative_firewall (parser))
10186 start = token;
10187 cp_parser_lambda_body (parser, lambda_expr);
10189 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10191 if (cp_parser_skip_to_closing_brace (parser))
10192 cp_lexer_consume_token (parser->lexer);
10195 /* The capture list was built up in reverse order; fix that now. */
10196 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10197 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10199 if (ok)
10200 maybe_add_lambda_conv_op (type);
10202 type = finish_struct (type, /*attributes=*/NULL_TREE);
10204 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10205 parser->in_statement = in_statement;
10206 parser->in_switch_statement_p = in_switch_statement_p;
10207 parser->fully_implicit_function_template_p
10208 = fully_implicit_function_template_p;
10209 parser->implicit_template_parms = implicit_template_parms;
10210 parser->implicit_template_scope = implicit_template_scope;
10211 parser->auto_is_implicit_function_template_parm_p
10212 = auto_is_implicit_function_template_parm_p;
10215 /* This field is only used during parsing of the lambda. */
10216 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10218 /* This lambda shouldn't have any proxies left at this point. */
10219 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10220 /* And now that we're done, push proxies for an enclosing lambda. */
10221 insert_pending_capture_proxies ();
10223 if (ok)
10224 lambda_expr = build_lambda_object (lambda_expr);
10225 else
10226 lambda_expr = error_mark_node;
10228 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10230 pop_deferring_access_checks ();
10232 return lambda_expr;
10235 /* Parse the beginning of a lambda expression.
10237 lambda-introducer:
10238 [ lambda-capture [opt] ]
10240 LAMBDA_EXPR is the current representation of the lambda expression. */
10242 static void
10243 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10245 /* Need commas after the first capture. */
10246 bool first = true;
10248 /* Eat the leading `['. */
10249 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10251 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10252 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10253 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10254 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10255 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10256 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10258 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10260 cp_lexer_consume_token (parser->lexer);
10261 first = false;
10264 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10266 cp_token* capture_token;
10267 tree capture_id;
10268 tree capture_init_expr;
10269 cp_id_kind idk = CP_ID_KIND_NONE;
10270 bool explicit_init_p = false;
10272 enum capture_kind_type
10274 BY_COPY,
10275 BY_REFERENCE
10277 enum capture_kind_type capture_kind = BY_COPY;
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10281 error ("expected end of capture-list");
10282 return;
10285 if (first)
10286 first = false;
10287 else
10288 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10290 /* Possibly capture `this'. */
10291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10293 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10294 if (cxx_dialect < cxx2a
10295 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10296 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10297 "with by-copy capture default");
10298 cp_lexer_consume_token (parser->lexer);
10299 add_capture (lambda_expr,
10300 /*id=*/this_identifier,
10301 /*initializer=*/finish_this_expr (),
10302 /*by_reference_p=*/true,
10303 explicit_init_p);
10304 continue;
10307 /* Possibly capture `*this'. */
10308 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10309 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10311 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10312 if (cxx_dialect < cxx17)
10313 pedwarn (loc, 0, "%<*this%> capture only available with "
10314 "-std=c++17 or -std=gnu++17");
10315 cp_lexer_consume_token (parser->lexer);
10316 cp_lexer_consume_token (parser->lexer);
10317 add_capture (lambda_expr,
10318 /*id=*/this_identifier,
10319 /*initializer=*/finish_this_expr (),
10320 /*by_reference_p=*/false,
10321 explicit_init_p);
10322 continue;
10325 /* Remember whether we want to capture as a reference or not. */
10326 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10328 capture_kind = BY_REFERENCE;
10329 cp_lexer_consume_token (parser->lexer);
10332 /* Get the identifier. */
10333 capture_token = cp_lexer_peek_token (parser->lexer);
10334 capture_id = cp_parser_identifier (parser);
10336 if (capture_id == error_mark_node)
10337 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10338 delimiters, but I modified this to stop on unnested ']' as well. It
10339 was already changed to stop on unnested '}', so the
10340 "closing_parenthesis" name is no more misleading with my change. */
10342 cp_parser_skip_to_closing_parenthesis (parser,
10343 /*recovering=*/true,
10344 /*or_comma=*/true,
10345 /*consume_paren=*/true);
10346 break;
10349 /* Find the initializer for this capture. */
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10351 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10352 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10354 bool direct, non_constant;
10355 /* An explicit initializer exists. */
10356 if (cxx_dialect < cxx14)
10357 pedwarn (input_location, 0,
10358 "lambda capture initializers "
10359 "only available with -std=c++14 or -std=gnu++14");
10360 capture_init_expr = cp_parser_initializer (parser, &direct,
10361 &non_constant);
10362 explicit_init_p = true;
10363 if (capture_init_expr == NULL_TREE)
10365 error ("empty initializer for lambda init-capture");
10366 capture_init_expr = error_mark_node;
10369 else
10371 const char* error_msg;
10373 /* Turn the identifier into an id-expression. */
10374 capture_init_expr
10375 = cp_parser_lookup_name_simple (parser, capture_id,
10376 capture_token->location);
10378 if (capture_init_expr == error_mark_node)
10380 unqualified_name_lookup_error (capture_id);
10381 continue;
10383 else if (!VAR_P (capture_init_expr)
10384 && TREE_CODE (capture_init_expr) != PARM_DECL)
10386 error_at (capture_token->location,
10387 "capture of non-variable %qE",
10388 capture_init_expr);
10389 if (DECL_P (capture_init_expr))
10390 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10391 "%q#D declared here", capture_init_expr);
10392 continue;
10394 if (VAR_P (capture_init_expr)
10395 && decl_storage_duration (capture_init_expr) != dk_auto)
10397 if (pedwarn (capture_token->location, 0, "capture of variable "
10398 "%qD with non-automatic storage duration",
10399 capture_init_expr))
10400 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10401 "%q#D declared here", capture_init_expr);
10402 continue;
10405 capture_init_expr
10406 = finish_id_expression
10407 (capture_id,
10408 capture_init_expr,
10409 parser->scope,
10410 &idk,
10411 /*integral_constant_expression_p=*/false,
10412 /*allow_non_integral_constant_expression_p=*/false,
10413 /*non_integral_constant_expression_p=*/NULL,
10414 /*template_p=*/false,
10415 /*done=*/true,
10416 /*address_p=*/false,
10417 /*template_arg_p=*/false,
10418 &error_msg,
10419 capture_token->location);
10421 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10423 cp_lexer_consume_token (parser->lexer);
10424 capture_init_expr = make_pack_expansion (capture_init_expr);
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10429 && !explicit_init_p)
10431 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10432 && capture_kind == BY_COPY)
10433 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10434 "of %qD redundant with by-copy capture default",
10435 capture_id);
10436 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10437 && capture_kind == BY_REFERENCE)
10438 pedwarn (capture_token->location, 0, "explicit by-reference "
10439 "capture of %qD redundant with by-reference capture "
10440 "default", capture_id);
10443 add_capture (lambda_expr,
10444 capture_id,
10445 capture_init_expr,
10446 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10447 explicit_init_p);
10449 /* If there is any qualification still in effect, clear it
10450 now; we will be starting fresh with the next capture. */
10451 parser->scope = NULL_TREE;
10452 parser->qualifying_scope = NULL_TREE;
10453 parser->object_scope = NULL_TREE;
10456 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10459 /* Parse the (optional) middle of a lambda expression.
10461 lambda-declarator:
10462 < template-parameter-list [opt] >
10463 ( parameter-declaration-clause [opt] )
10464 attribute-specifier [opt]
10465 decl-specifier-seq [opt]
10466 exception-specification [opt]
10467 lambda-return-type-clause [opt]
10469 LAMBDA_EXPR is the current representation of the lambda expression. */
10471 static bool
10472 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10474 /* 5.1.1.4 of the standard says:
10475 If a lambda-expression does not include a lambda-declarator, it is as if
10476 the lambda-declarator were ().
10477 This means an empty parameter list, no attributes, and no exception
10478 specification. */
10479 tree param_list = void_list_node;
10480 tree attributes = NULL_TREE;
10481 tree exception_spec = NULL_TREE;
10482 tree template_param_list = NULL_TREE;
10483 tree tx_qual = NULL_TREE;
10484 tree return_type = NULL_TREE;
10485 cp_decl_specifier_seq lambda_specs;
10486 clear_decl_specs (&lambda_specs);
10488 /* The template-parameter-list is optional, but must begin with
10489 an opening angle if present. */
10490 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10492 if (cxx_dialect < cxx14)
10493 pedwarn (parser->lexer->next_token->location, 0,
10494 "lambda templates are only available with "
10495 "-std=c++14 or -std=gnu++14");
10496 else if (cxx_dialect < cxx2a)
10497 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10498 "lambda templates are only available with "
10499 "-std=c++2a or -std=gnu++2a");
10501 cp_lexer_consume_token (parser->lexer);
10503 template_param_list = cp_parser_template_parameter_list (parser);
10505 cp_parser_skip_to_end_of_template_parameter_list (parser);
10507 /* We just processed one more parameter list. */
10508 ++parser->num_template_parameter_lists;
10511 /* The parameter-declaration-clause is optional (unless
10512 template-parameter-list was given), but must begin with an
10513 opening parenthesis if present. */
10514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10516 matching_parens parens;
10517 parens.consume_open (parser);
10519 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10521 /* Parse parameters. */
10522 param_list = cp_parser_parameter_declaration_clause (parser);
10524 /* Default arguments shall not be specified in the
10525 parameter-declaration-clause of a lambda-declarator. */
10526 if (cxx_dialect < cxx14)
10527 for (tree t = param_list; t; t = TREE_CHAIN (t))
10528 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10529 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10530 "default argument specified for lambda parameter");
10532 parens.require_close (parser);
10534 attributes = cp_parser_attributes_opt (parser);
10536 /* In the decl-specifier-seq of the lambda-declarator, each
10537 decl-specifier shall either be mutable or constexpr. */
10538 int declares_class_or_enum;
10539 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10540 cp_parser_decl_specifier_seq (parser,
10541 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10542 &lambda_specs, &declares_class_or_enum);
10543 if (lambda_specs.storage_class == sc_mutable)
10545 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10546 if (lambda_specs.conflicting_specifiers_p)
10547 error_at (lambda_specs.locations[ds_storage_class],
10548 "duplicate %<mutable%>");
10551 tx_qual = cp_parser_tx_qualifier_opt (parser);
10553 /* Parse optional exception specification. */
10554 exception_spec = cp_parser_exception_specification_opt (parser);
10556 /* Parse optional trailing return type. */
10557 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10559 cp_lexer_consume_token (parser->lexer);
10560 return_type = cp_parser_trailing_type_id (parser);
10563 /* The function parameters must be in scope all the way until after the
10564 trailing-return-type in case of decltype. */
10565 pop_bindings_and_leave_scope ();
10567 else if (template_param_list != NULL_TREE) // generate diagnostic
10568 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10570 /* Create the function call operator.
10572 Messing with declarators like this is no uglier than building up the
10573 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10574 other code. */
10576 cp_decl_specifier_seq return_type_specs;
10577 cp_declarator* declarator;
10578 tree fco;
10579 int quals;
10580 void *p;
10582 clear_decl_specs (&return_type_specs);
10583 if (return_type)
10584 return_type_specs.type = return_type;
10585 else
10586 /* Maybe we will deduce the return type later. */
10587 return_type_specs.type = make_auto ();
10589 if (lambda_specs.locations[ds_constexpr])
10591 if (cxx_dialect >= cxx17)
10592 return_type_specs.locations[ds_constexpr]
10593 = lambda_specs.locations[ds_constexpr];
10594 else
10595 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10596 "lambda only available with -std=c++17 or -std=gnu++17");
10599 p = obstack_alloc (&declarator_obstack, 0);
10601 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10603 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10604 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10605 declarator = make_call_declarator (declarator, param_list, quals,
10606 VIRT_SPEC_UNSPECIFIED,
10607 REF_QUAL_NONE,
10608 tx_qual,
10609 exception_spec,
10610 /*late_return_type=*/NULL_TREE,
10611 /*requires_clause*/NULL_TREE);
10612 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10614 fco = grokmethod (&return_type_specs,
10615 declarator,
10616 attributes);
10617 if (fco != error_mark_node)
10619 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10620 DECL_ARTIFICIAL (fco) = 1;
10621 /* Give the object parameter a different name. */
10622 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10623 if (return_type)
10624 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10626 if (template_param_list)
10628 fco = finish_member_template_decl (fco);
10629 finish_template_decl (template_param_list);
10630 --parser->num_template_parameter_lists;
10632 else if (parser->fully_implicit_function_template_p)
10633 fco = finish_fully_implicit_template (parser, fco);
10635 finish_member_declaration (fco);
10637 obstack_free (&declarator_obstack, p);
10639 return (fco != error_mark_node);
10643 /* Parse the body of a lambda expression, which is simply
10645 compound-statement
10647 but which requires special handling.
10648 LAMBDA_EXPR is the current representation of the lambda expression. */
10650 static void
10651 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10653 bool nested = (current_function_decl != NULL_TREE);
10654 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10655 bool in_function_body = parser->in_function_body;
10657 if (nested)
10658 push_function_context ();
10659 else
10660 /* Still increment function_depth so that we don't GC in the
10661 middle of an expression. */
10662 ++function_depth;
10664 vec<tree> omp_privatization_save;
10665 save_omp_privatization_clauses (omp_privatization_save);
10666 /* Clear this in case we're in the middle of a default argument. */
10667 parser->local_variables_forbidden_p = false;
10668 parser->in_function_body = true;
10671 local_specialization_stack s (lss_copy);
10672 tree fco = lambda_function (lambda_expr);
10673 tree body = start_lambda_function (fco, lambda_expr);
10674 matching_braces braces;
10676 if (braces.require_open (parser))
10678 tree compound_stmt = begin_compound_stmt (0);
10680 /* Originally C++11 required us to peek for 'return expr'; and
10681 process it specially here to deduce the return type. N3638
10682 removed the need for that. */
10684 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10685 cp_parser_label_declaration (parser);
10686 cp_parser_statement_seq_opt (parser, NULL_TREE);
10687 braces.require_close (parser);
10689 finish_compound_stmt (compound_stmt);
10692 finish_lambda_function (body);
10695 restore_omp_privatization_clauses (omp_privatization_save);
10696 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10697 parser->in_function_body = in_function_body;
10698 if (nested)
10699 pop_function_context();
10700 else
10701 --function_depth;
10704 /* Statements [gram.stmt.stmt] */
10706 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10708 static void
10709 add_debug_begin_stmt (location_t loc)
10711 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10712 return;
10713 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10714 /* A concept is never expanded normally. */
10715 return;
10717 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10718 SET_EXPR_LOCATION (stmt, loc);
10719 add_stmt (stmt);
10722 /* Parse a statement.
10724 statement:
10725 labeled-statement
10726 expression-statement
10727 compound-statement
10728 selection-statement
10729 iteration-statement
10730 jump-statement
10731 declaration-statement
10732 try-block
10734 C++11:
10736 statement:
10737 labeled-statement
10738 attribute-specifier-seq (opt) expression-statement
10739 attribute-specifier-seq (opt) compound-statement
10740 attribute-specifier-seq (opt) selection-statement
10741 attribute-specifier-seq (opt) iteration-statement
10742 attribute-specifier-seq (opt) jump-statement
10743 declaration-statement
10744 attribute-specifier-seq (opt) try-block
10746 init-statement:
10747 expression-statement
10748 simple-declaration
10750 TM Extension:
10752 statement:
10753 atomic-statement
10755 IN_COMPOUND is true when the statement is nested inside a
10756 cp_parser_compound_statement; this matters for certain pragmas.
10758 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10759 is a (possibly labeled) if statement which is not enclosed in braces
10760 and has an else clause. This is used to implement -Wparentheses.
10762 CHAIN is a vector of if-else-if conditions. */
10764 static void
10765 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10766 bool in_compound, bool *if_p, vec<tree> *chain,
10767 location_t *loc_after_labels)
10769 tree statement, std_attrs = NULL_TREE;
10770 cp_token *token;
10771 location_t statement_location, attrs_location;
10773 restart:
10774 if (if_p != NULL)
10775 *if_p = false;
10776 /* There is no statement yet. */
10777 statement = NULL_TREE;
10779 saved_token_sentinel saved_tokens (parser->lexer);
10780 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10781 if (c_dialect_objc ())
10782 /* In obj-c++, seeing '[[' might be the either the beginning of
10783 c++11 attributes, or a nested objc-message-expression. So
10784 let's parse the c++11 attributes tentatively. */
10785 cp_parser_parse_tentatively (parser);
10786 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10787 if (c_dialect_objc ())
10789 if (!cp_parser_parse_definitely (parser))
10790 std_attrs = NULL_TREE;
10793 /* Peek at the next token. */
10794 token = cp_lexer_peek_token (parser->lexer);
10795 /* Remember the location of the first token in the statement. */
10796 statement_location = token->location;
10797 add_debug_begin_stmt (statement_location);
10798 /* If this is a keyword, then that will often determine what kind of
10799 statement we have. */
10800 if (token->type == CPP_KEYWORD)
10802 enum rid keyword = token->keyword;
10804 switch (keyword)
10806 case RID_CASE:
10807 case RID_DEFAULT:
10808 /* Looks like a labeled-statement with a case label.
10809 Parse the label, and then use tail recursion to parse
10810 the statement. */
10811 cp_parser_label_for_labeled_statement (parser, std_attrs);
10812 in_compound = false;
10813 goto restart;
10815 case RID_IF:
10816 case RID_SWITCH:
10817 statement = cp_parser_selection_statement (parser, if_p, chain);
10818 break;
10820 case RID_WHILE:
10821 case RID_DO:
10822 case RID_FOR:
10823 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10824 break;
10826 case RID_BREAK:
10827 case RID_CONTINUE:
10828 case RID_RETURN:
10829 case RID_GOTO:
10830 statement = cp_parser_jump_statement (parser);
10831 break;
10833 /* Objective-C++ exception-handling constructs. */
10834 case RID_AT_TRY:
10835 case RID_AT_CATCH:
10836 case RID_AT_FINALLY:
10837 case RID_AT_SYNCHRONIZED:
10838 case RID_AT_THROW:
10839 statement = cp_parser_objc_statement (parser);
10840 break;
10842 case RID_TRY:
10843 statement = cp_parser_try_block (parser);
10844 break;
10846 case RID_NAMESPACE:
10847 /* This must be a namespace alias definition. */
10848 cp_parser_declaration_statement (parser);
10849 return;
10851 case RID_TRANSACTION_ATOMIC:
10852 case RID_TRANSACTION_RELAXED:
10853 case RID_SYNCHRONIZED:
10854 case RID_ATOMIC_NOEXCEPT:
10855 case RID_ATOMIC_CANCEL:
10856 statement = cp_parser_transaction (parser, token);
10857 break;
10858 case RID_TRANSACTION_CANCEL:
10859 statement = cp_parser_transaction_cancel (parser);
10860 break;
10862 default:
10863 /* It might be a keyword like `int' that can start a
10864 declaration-statement. */
10865 break;
10868 else if (token->type == CPP_NAME)
10870 /* If the next token is a `:', then we are looking at a
10871 labeled-statement. */
10872 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10873 if (token->type == CPP_COLON)
10875 /* Looks like a labeled-statement with an ordinary label.
10876 Parse the label, and then use tail recursion to parse
10877 the statement. */
10879 cp_parser_label_for_labeled_statement (parser, std_attrs);
10880 in_compound = false;
10881 goto restart;
10884 /* Anything that starts with a `{' must be a compound-statement. */
10885 else if (token->type == CPP_OPEN_BRACE)
10886 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10887 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10888 a statement all its own. */
10889 else if (token->type == CPP_PRAGMA)
10891 /* Only certain OpenMP pragmas are attached to statements, and thus
10892 are considered statements themselves. All others are not. In
10893 the context of a compound, accept the pragma as a "statement" and
10894 return so that we can check for a close brace. Otherwise we
10895 require a real statement and must go back and read one. */
10896 if (in_compound)
10897 cp_parser_pragma (parser, pragma_compound, if_p);
10898 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10899 goto restart;
10900 return;
10902 else if (token->type == CPP_EOF)
10904 cp_parser_error (parser, "expected statement");
10905 return;
10908 /* Everything else must be a declaration-statement or an
10909 expression-statement. Try for the declaration-statement
10910 first, unless we are looking at a `;', in which case we know that
10911 we have an expression-statement. */
10912 if (!statement)
10914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10916 if (std_attrs != NULL_TREE)
10918 /* Attributes should be parsed as part of the the
10919 declaration, so let's un-parse them. */
10920 saved_tokens.rollback();
10921 std_attrs = NULL_TREE;
10924 cp_parser_parse_tentatively (parser);
10925 /* Try to parse the declaration-statement. */
10926 cp_parser_declaration_statement (parser);
10927 /* If that worked, we're done. */
10928 if (cp_parser_parse_definitely (parser))
10929 return;
10931 /* All preceding labels have been parsed at this point. */
10932 if (loc_after_labels != NULL)
10933 *loc_after_labels = statement_location;
10935 /* Look for an expression-statement instead. */
10936 statement = cp_parser_expression_statement (parser, in_statement_expr);
10938 /* Handle [[fallthrough]];. */
10939 if (attribute_fallthrough_p (std_attrs))
10941 /* The next token after the fallthrough attribute is ';'. */
10942 if (statement == NULL_TREE)
10944 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10945 statement = build_call_expr_internal_loc (statement_location,
10946 IFN_FALLTHROUGH,
10947 void_type_node, 0);
10948 finish_expr_stmt (statement);
10950 else
10951 warning_at (statement_location, OPT_Wattributes,
10952 "%<fallthrough%> attribute not followed by %<;%>");
10953 std_attrs = NULL_TREE;
10957 /* Set the line number for the statement. */
10958 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10959 SET_EXPR_LOCATION (statement, statement_location);
10961 /* Allow "[[fallthrough]];", but warn otherwise. */
10962 if (std_attrs != NULL_TREE)
10963 warning_at (attrs_location,
10964 OPT_Wattributes,
10965 "attributes at the beginning of statement are ignored");
10968 /* Append ATTR to attribute list ATTRS. */
10970 static tree
10971 attr_chainon (tree attrs, tree attr)
10973 if (attrs == error_mark_node)
10974 return error_mark_node;
10975 if (attr == error_mark_node)
10976 return error_mark_node;
10977 return chainon (attrs, attr);
10980 /* Parse the label for a labeled-statement, i.e.
10982 identifier :
10983 case constant-expression :
10984 default :
10986 GNU Extension:
10987 case constant-expression ... constant-expression : statement
10989 When a label is parsed without errors, the label is added to the
10990 parse tree by the finish_* functions, so this function doesn't
10991 have to return the label. */
10993 static void
10994 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10996 cp_token *token;
10997 tree label = NULL_TREE;
10998 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11000 /* The next token should be an identifier. */
11001 token = cp_lexer_peek_token (parser->lexer);
11002 if (token->type != CPP_NAME
11003 && token->type != CPP_KEYWORD)
11005 cp_parser_error (parser, "expected labeled-statement");
11006 return;
11009 /* Remember whether this case or a user-defined label is allowed to fall
11010 through to. */
11011 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11013 parser->colon_corrects_to_scope_p = false;
11014 switch (token->keyword)
11016 case RID_CASE:
11018 tree expr, expr_hi;
11019 cp_token *ellipsis;
11021 /* Consume the `case' token. */
11022 cp_lexer_consume_token (parser->lexer);
11023 /* Parse the constant-expression. */
11024 expr = cp_parser_constant_expression (parser);
11025 if (check_for_bare_parameter_packs (expr))
11026 expr = error_mark_node;
11028 ellipsis = cp_lexer_peek_token (parser->lexer);
11029 if (ellipsis->type == CPP_ELLIPSIS)
11031 /* Consume the `...' token. */
11032 cp_lexer_consume_token (parser->lexer);
11033 expr_hi = cp_parser_constant_expression (parser);
11034 if (check_for_bare_parameter_packs (expr_hi))
11035 expr_hi = error_mark_node;
11037 /* We don't need to emit warnings here, as the common code
11038 will do this for us. */
11040 else
11041 expr_hi = NULL_TREE;
11043 if (parser->in_switch_statement_p)
11045 tree l = finish_case_label (token->location, expr, expr_hi);
11046 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11047 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11049 else
11050 error_at (token->location,
11051 "case label %qE not within a switch statement",
11052 expr);
11054 break;
11056 case RID_DEFAULT:
11057 /* Consume the `default' token. */
11058 cp_lexer_consume_token (parser->lexer);
11060 if (parser->in_switch_statement_p)
11062 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11063 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11064 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11066 else
11067 error_at (token->location, "case label not within a switch statement");
11068 break;
11070 default:
11071 /* Anything else must be an ordinary label. */
11072 label = finish_label_stmt (cp_parser_identifier (parser));
11073 if (label && TREE_CODE (label) == LABEL_DECL)
11074 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11075 break;
11078 /* Require the `:' token. */
11079 cp_parser_require (parser, CPP_COLON, RT_COLON);
11081 /* An ordinary label may optionally be followed by attributes.
11082 However, this is only permitted if the attributes are then
11083 followed by a semicolon. This is because, for backward
11084 compatibility, when parsing
11085 lab: __attribute__ ((unused)) int i;
11086 we want the attribute to attach to "i", not "lab". */
11087 if (label != NULL_TREE
11088 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11090 tree attrs;
11091 cp_parser_parse_tentatively (parser);
11092 attrs = cp_parser_gnu_attributes_opt (parser);
11093 if (attrs == NULL_TREE
11094 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11095 cp_parser_abort_tentative_parse (parser);
11096 else if (!cp_parser_parse_definitely (parser))
11098 else
11099 attributes = attr_chainon (attributes, attrs);
11102 if (attributes != NULL_TREE)
11103 cplus_decl_attributes (&label, attributes, 0);
11105 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11108 /* Parse an expression-statement.
11110 expression-statement:
11111 expression [opt] ;
11113 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11114 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11115 indicates whether this expression-statement is part of an
11116 expression statement. */
11118 static tree
11119 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11121 tree statement = NULL_TREE;
11122 cp_token *token = cp_lexer_peek_token (parser->lexer);
11123 location_t loc = token->location;
11125 /* There might be attribute fallthrough. */
11126 tree attr = cp_parser_gnu_attributes_opt (parser);
11128 /* If the next token is a ';', then there is no expression
11129 statement. */
11130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11132 statement = cp_parser_expression (parser);
11133 if (statement == error_mark_node
11134 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11136 cp_parser_skip_to_end_of_block_or_statement (parser);
11137 return error_mark_node;
11141 /* Handle [[fallthrough]];. */
11142 if (attribute_fallthrough_p (attr))
11144 /* The next token after the fallthrough attribute is ';'. */
11145 if (statement == NULL_TREE)
11146 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11147 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11148 void_type_node, 0);
11149 else
11150 warning_at (loc, OPT_Wattributes,
11151 "%<fallthrough%> attribute not followed by %<;%>");
11152 attr = NULL_TREE;
11155 /* Allow "[[fallthrough]];", but warn otherwise. */
11156 if (attr != NULL_TREE)
11157 warning_at (loc, OPT_Wattributes,
11158 "attributes at the beginning of statement are ignored");
11160 /* Give a helpful message for "A<T>::type t;" and the like. */
11161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11162 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11164 if (TREE_CODE (statement) == SCOPE_REF)
11165 error_at (token->location, "need %<typename%> before %qE because "
11166 "%qT is a dependent scope",
11167 statement, TREE_OPERAND (statement, 0));
11168 else if (is_overloaded_fn (statement)
11169 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11171 /* A::A a; */
11172 tree fn = get_first_fn (statement);
11173 error_at (token->location,
11174 "%<%T::%D%> names the constructor, not the type",
11175 DECL_CONTEXT (fn), DECL_NAME (fn));
11179 /* Consume the final `;'. */
11180 cp_parser_consume_semicolon_at_end_of_statement (parser);
11182 if (in_statement_expr
11183 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11184 /* This is the final expression statement of a statement
11185 expression. */
11186 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11187 else if (statement)
11188 statement = finish_expr_stmt (statement);
11190 return statement;
11193 /* Parse a compound-statement.
11195 compound-statement:
11196 { statement-seq [opt] }
11198 GNU extension:
11200 compound-statement:
11201 { label-declaration-seq [opt] statement-seq [opt] }
11203 label-declaration-seq:
11204 label-declaration
11205 label-declaration-seq label-declaration
11207 Returns a tree representing the statement. */
11209 static tree
11210 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11211 int bcs_flags, bool function_body)
11213 tree compound_stmt;
11214 matching_braces braces;
11216 /* Consume the `{'. */
11217 if (!braces.require_open (parser))
11218 return error_mark_node;
11219 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11220 && !function_body && cxx_dialect < cxx14)
11221 pedwarn (input_location, OPT_Wpedantic,
11222 "compound-statement in %<constexpr%> function");
11223 /* Begin the compound-statement. */
11224 compound_stmt = begin_compound_stmt (bcs_flags);
11225 /* If the next keyword is `__label__' we have a label declaration. */
11226 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11227 cp_parser_label_declaration (parser);
11228 /* Parse an (optional) statement-seq. */
11229 cp_parser_statement_seq_opt (parser, in_statement_expr);
11230 /* Finish the compound-statement. */
11231 finish_compound_stmt (compound_stmt);
11232 /* Consume the `}'. */
11233 braces.require_close (parser);
11235 return compound_stmt;
11238 /* Parse an (optional) statement-seq.
11240 statement-seq:
11241 statement
11242 statement-seq [opt] statement */
11244 static void
11245 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11247 /* Scan statements until there aren't any more. */
11248 while (true)
11250 cp_token *token = cp_lexer_peek_token (parser->lexer);
11252 /* If we are looking at a `}', then we have run out of
11253 statements; the same is true if we have reached the end
11254 of file, or have stumbled upon a stray '@end'. */
11255 if (token->type == CPP_CLOSE_BRACE
11256 || token->type == CPP_EOF
11257 || token->type == CPP_PRAGMA_EOL
11258 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11259 break;
11261 /* If we are in a compound statement and find 'else' then
11262 something went wrong. */
11263 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11265 if (parser->in_statement & IN_IF_STMT)
11266 break;
11267 else
11269 token = cp_lexer_consume_token (parser->lexer);
11270 error_at (token->location, "%<else%> without a previous %<if%>");
11274 /* Parse the statement. */
11275 cp_parser_statement (parser, in_statement_expr, true, NULL);
11279 /* Return true if we're looking at (init; cond), false otherwise. */
11281 static bool
11282 cp_parser_init_statement_p (cp_parser *parser)
11284 /* Save tokens so that we can put them back. */
11285 cp_lexer_save_tokens (parser->lexer);
11287 /* Look for ';' that is not nested in () or {}. */
11288 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11289 /*recovering=*/false,
11290 CPP_SEMICOLON,
11291 /*consume_paren=*/false);
11293 /* Roll back the tokens we skipped. */
11294 cp_lexer_rollback_tokens (parser->lexer);
11296 return ret == -1;
11299 /* Parse a selection-statement.
11301 selection-statement:
11302 if ( init-statement [opt] condition ) statement
11303 if ( init-statement [opt] condition ) statement else statement
11304 switch ( init-statement [opt] condition ) statement
11306 Returns the new IF_STMT or SWITCH_STMT.
11308 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11309 is a (possibly labeled) if statement which is not enclosed in
11310 braces and has an else clause. This is used to implement
11311 -Wparentheses.
11313 CHAIN is a vector of if-else-if conditions. This is used to implement
11314 -Wduplicated-cond. */
11316 static tree
11317 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11318 vec<tree> *chain)
11320 cp_token *token;
11321 enum rid keyword;
11322 token_indent_info guard_tinfo;
11324 if (if_p != NULL)
11325 *if_p = false;
11327 /* Peek at the next token. */
11328 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11329 guard_tinfo = get_token_indent_info (token);
11331 /* See what kind of keyword it is. */
11332 keyword = token->keyword;
11333 switch (keyword)
11335 case RID_IF:
11336 case RID_SWITCH:
11338 tree statement;
11339 tree condition;
11341 bool cx = false;
11342 if (keyword == RID_IF
11343 && cp_lexer_next_token_is_keyword (parser->lexer,
11344 RID_CONSTEXPR))
11346 cx = true;
11347 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11348 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11349 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11350 "with -std=c++17 or -std=gnu++17");
11353 /* Look for the `('. */
11354 matching_parens parens;
11355 if (!parens.require_open (parser))
11357 cp_parser_skip_to_end_of_statement (parser);
11358 return error_mark_node;
11361 /* Begin the selection-statement. */
11362 if (keyword == RID_IF)
11364 statement = begin_if_stmt ();
11365 IF_STMT_CONSTEXPR_P (statement) = cx;
11367 else
11368 statement = begin_switch_stmt ();
11370 /* Parse the optional init-statement. */
11371 if (cp_parser_init_statement_p (parser))
11373 tree decl;
11374 if (cxx_dialect < cxx17)
11375 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11376 "init-statement in selection statements only available "
11377 "with -std=c++17 or -std=gnu++17");
11378 cp_parser_init_statement (parser, &decl);
11381 /* Parse the condition. */
11382 condition = cp_parser_condition (parser);
11383 /* Look for the `)'. */
11384 if (!parens.require_close (parser))
11385 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11386 /*consume_paren=*/true);
11388 if (keyword == RID_IF)
11390 bool nested_if;
11391 unsigned char in_statement;
11393 /* Add the condition. */
11394 condition = finish_if_stmt_cond (condition, statement);
11396 if (warn_duplicated_cond)
11397 warn_duplicated_cond_add_or_warn (token->location, condition,
11398 &chain);
11400 /* Parse the then-clause. */
11401 in_statement = parser->in_statement;
11402 parser->in_statement |= IN_IF_STMT;
11404 /* Outside a template, the non-selected branch of a constexpr
11405 if is a 'discarded statement', i.e. unevaluated. */
11406 bool was_discarded = in_discarded_stmt;
11407 bool discard_then = (cx && !processing_template_decl
11408 && integer_zerop (condition));
11409 if (discard_then)
11411 in_discarded_stmt = true;
11412 ++c_inhibit_evaluation_warnings;
11415 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11416 guard_tinfo);
11418 parser->in_statement = in_statement;
11420 finish_then_clause (statement);
11422 if (discard_then)
11424 THEN_CLAUSE (statement) = NULL_TREE;
11425 in_discarded_stmt = was_discarded;
11426 --c_inhibit_evaluation_warnings;
11429 /* If the next token is `else', parse the else-clause. */
11430 if (cp_lexer_next_token_is_keyword (parser->lexer,
11431 RID_ELSE))
11433 bool discard_else = (cx && !processing_template_decl
11434 && integer_nonzerop (condition));
11435 if (discard_else)
11437 in_discarded_stmt = true;
11438 ++c_inhibit_evaluation_warnings;
11441 guard_tinfo
11442 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11443 /* Consume the `else' keyword. */
11444 cp_lexer_consume_token (parser->lexer);
11445 if (warn_duplicated_cond)
11447 if (cp_lexer_next_token_is_keyword (parser->lexer,
11448 RID_IF)
11449 && chain == NULL)
11451 /* We've got "if (COND) else if (COND2)". Start
11452 the condition chain and add COND as the first
11453 element. */
11454 chain = new vec<tree> ();
11455 if (!CONSTANT_CLASS_P (condition)
11456 && !TREE_SIDE_EFFECTS (condition))
11458 /* Wrap it in a NOP_EXPR so that we can set the
11459 location of the condition. */
11460 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11461 condition);
11462 SET_EXPR_LOCATION (e, token->location);
11463 chain->safe_push (e);
11466 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11467 RID_IF))
11469 /* This is if-else without subsequent if. Zap the
11470 condition chain; we would have already warned at
11471 this point. */
11472 delete chain;
11473 chain = NULL;
11476 begin_else_clause (statement);
11477 /* Parse the else-clause. */
11478 cp_parser_implicitly_scoped_statement (parser, NULL,
11479 guard_tinfo, chain);
11481 finish_else_clause (statement);
11483 /* If we are currently parsing a then-clause, then
11484 IF_P will not be NULL. We set it to true to
11485 indicate that this if statement has an else clause.
11486 This may trigger the Wparentheses warning below
11487 when we get back up to the parent if statement. */
11488 if (if_p != NULL)
11489 *if_p = true;
11491 if (discard_else)
11493 ELSE_CLAUSE (statement) = NULL_TREE;
11494 in_discarded_stmt = was_discarded;
11495 --c_inhibit_evaluation_warnings;
11498 else
11500 /* This if statement does not have an else clause. If
11501 NESTED_IF is true, then the then-clause has an if
11502 statement which does have an else clause. We warn
11503 about the potential ambiguity. */
11504 if (nested_if)
11505 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11506 "suggest explicit braces to avoid ambiguous"
11507 " %<else%>");
11508 if (warn_duplicated_cond)
11510 /* We don't need the condition chain anymore. */
11511 delete chain;
11512 chain = NULL;
11516 /* Now we're all done with the if-statement. */
11517 finish_if_stmt (statement);
11519 else
11521 bool in_switch_statement_p;
11522 unsigned char in_statement;
11524 /* Add the condition. */
11525 finish_switch_cond (condition, statement);
11527 /* Parse the body of the switch-statement. */
11528 in_switch_statement_p = parser->in_switch_statement_p;
11529 in_statement = parser->in_statement;
11530 parser->in_switch_statement_p = true;
11531 parser->in_statement |= IN_SWITCH_STMT;
11532 cp_parser_implicitly_scoped_statement (parser, if_p,
11533 guard_tinfo);
11534 parser->in_switch_statement_p = in_switch_statement_p;
11535 parser->in_statement = in_statement;
11537 /* Now we're all done with the switch-statement. */
11538 finish_switch_stmt (statement);
11541 return statement;
11543 break;
11545 default:
11546 cp_parser_error (parser, "expected selection-statement");
11547 return error_mark_node;
11551 /* Parse a condition.
11553 condition:
11554 expression
11555 type-specifier-seq declarator = initializer-clause
11556 type-specifier-seq declarator braced-init-list
11558 GNU Extension:
11560 condition:
11561 type-specifier-seq declarator asm-specification [opt]
11562 attributes [opt] = assignment-expression
11564 Returns the expression that should be tested. */
11566 static tree
11567 cp_parser_condition (cp_parser* parser)
11569 cp_decl_specifier_seq type_specifiers;
11570 const char *saved_message;
11571 int declares_class_or_enum;
11573 /* Try the declaration first. */
11574 cp_parser_parse_tentatively (parser);
11575 /* New types are not allowed in the type-specifier-seq for a
11576 condition. */
11577 saved_message = parser->type_definition_forbidden_message;
11578 parser->type_definition_forbidden_message
11579 = G_("types may not be defined in conditions");
11580 /* Parse the type-specifier-seq. */
11581 cp_parser_decl_specifier_seq (parser,
11582 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11583 &type_specifiers,
11584 &declares_class_or_enum);
11585 /* Restore the saved message. */
11586 parser->type_definition_forbidden_message = saved_message;
11587 /* If all is well, we might be looking at a declaration. */
11588 if (!cp_parser_error_occurred (parser))
11590 tree decl;
11591 tree asm_specification;
11592 tree attributes;
11593 cp_declarator *declarator;
11594 tree initializer = NULL_TREE;
11596 /* Parse the declarator. */
11597 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11598 /*ctor_dtor_or_conv_p=*/NULL,
11599 /*parenthesized_p=*/NULL,
11600 /*member_p=*/false,
11601 /*friend_p=*/false);
11602 /* Parse the attributes. */
11603 attributes = cp_parser_attributes_opt (parser);
11604 /* Parse the asm-specification. */
11605 asm_specification = cp_parser_asm_specification_opt (parser);
11606 /* If the next token is not an `=' or '{', then we might still be
11607 looking at an expression. For example:
11609 if (A(a).x)
11611 looks like a decl-specifier-seq and a declarator -- but then
11612 there is no `=', so this is an expression. */
11613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11614 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11615 cp_parser_simulate_error (parser);
11617 /* If we did see an `=' or '{', then we are looking at a declaration
11618 for sure. */
11619 if (cp_parser_parse_definitely (parser))
11621 tree pushed_scope;
11622 bool non_constant_p;
11623 int flags = LOOKUP_ONLYCONVERTING;
11625 /* Create the declaration. */
11626 decl = start_decl (declarator, &type_specifiers,
11627 /*initialized_p=*/true,
11628 attributes, /*prefix_attributes=*/NULL_TREE,
11629 &pushed_scope);
11631 /* Parse the initializer. */
11632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11634 initializer = cp_parser_braced_list (parser, &non_constant_p);
11635 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11636 flags = 0;
11638 else
11640 /* Consume the `='. */
11641 cp_parser_require (parser, CPP_EQ, RT_EQ);
11642 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11644 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11647 /* Process the initializer. */
11648 cp_finish_decl (decl,
11649 initializer, !non_constant_p,
11650 asm_specification,
11651 flags);
11653 if (pushed_scope)
11654 pop_scope (pushed_scope);
11656 return convert_from_reference (decl);
11659 /* If we didn't even get past the declarator successfully, we are
11660 definitely not looking at a declaration. */
11661 else
11662 cp_parser_abort_tentative_parse (parser);
11664 /* Otherwise, we are looking at an expression. */
11665 return cp_parser_expression (parser);
11668 /* Parses a for-statement or range-for-statement until the closing ')',
11669 not included. */
11671 static tree
11672 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11674 tree init, scope, decl;
11675 bool is_range_for;
11677 /* Begin the for-statement. */
11678 scope = begin_for_scope (&init);
11680 /* Parse the initialization. */
11681 is_range_for = cp_parser_init_statement (parser, &decl);
11683 if (is_range_for)
11684 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11685 else
11686 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11689 static tree
11690 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11691 unsigned short unroll)
11693 /* Normal for loop */
11694 tree condition = NULL_TREE;
11695 tree expression = NULL_TREE;
11696 tree stmt;
11698 stmt = begin_for_stmt (scope, init);
11699 /* The init-statement has already been parsed in
11700 cp_parser_init_statement, so no work is needed here. */
11701 finish_init_stmt (stmt);
11703 /* If there's a condition, process it. */
11704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11705 condition = cp_parser_condition (parser);
11706 else if (ivdep)
11708 cp_parser_error (parser, "missing loop condition in loop with "
11709 "%<GCC ivdep%> pragma");
11710 condition = error_mark_node;
11712 else if (unroll)
11714 cp_parser_error (parser, "missing loop condition in loop with "
11715 "%<GCC unroll%> pragma");
11716 condition = error_mark_node;
11718 finish_for_cond (condition, stmt, ivdep, unroll);
11719 /* Look for the `;'. */
11720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11722 /* If there's an expression, process it. */
11723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11724 expression = cp_parser_expression (parser);
11725 finish_for_expr (expression, stmt);
11727 return stmt;
11730 /* Tries to parse a range-based for-statement:
11732 range-based-for:
11733 decl-specifier-seq declarator : expression
11735 The decl-specifier-seq declarator and the `:' are already parsed by
11736 cp_parser_init_statement. If processing_template_decl it returns a
11737 newly created RANGE_FOR_STMT; if not, it is converted to a
11738 regular FOR_STMT. */
11740 static tree
11741 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11742 bool ivdep, unsigned short unroll)
11744 tree stmt, range_expr;
11745 auto_vec <cxx_binding *, 16> bindings;
11746 auto_vec <tree, 16> names;
11747 tree decomp_first_name = NULL_TREE;
11748 unsigned int decomp_cnt = 0;
11750 /* Get the range declaration momentarily out of the way so that
11751 the range expression doesn't clash with it. */
11752 if (range_decl != error_mark_node)
11754 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11756 tree v = DECL_VALUE_EXPR (range_decl);
11757 /* For decomposition declaration get all of the corresponding
11758 declarations out of the way. */
11759 if (TREE_CODE (v) == ARRAY_REF
11760 && VAR_P (TREE_OPERAND (v, 0))
11761 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11763 tree d = range_decl;
11764 range_decl = TREE_OPERAND (v, 0);
11765 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11766 decomp_first_name = d;
11767 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11769 tree name = DECL_NAME (d);
11770 names.safe_push (name);
11771 bindings.safe_push (IDENTIFIER_BINDING (name));
11772 IDENTIFIER_BINDING (name)
11773 = IDENTIFIER_BINDING (name)->previous;
11777 if (names.is_empty ())
11779 tree name = DECL_NAME (range_decl);
11780 names.safe_push (name);
11781 bindings.safe_push (IDENTIFIER_BINDING (name));
11782 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11788 bool expr_non_constant_p;
11789 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11791 else
11792 range_expr = cp_parser_expression (parser);
11794 /* Put the range declaration(s) back into scope. */
11795 for (unsigned int i = 0; i < names.length (); i++)
11797 cxx_binding *binding = bindings[i];
11798 binding->previous = IDENTIFIER_BINDING (names[i]);
11799 IDENTIFIER_BINDING (names[i]) = binding;
11802 /* If in template, STMT is converted to a normal for-statement
11803 at instantiation. If not, it is done just ahead. */
11804 if (processing_template_decl)
11806 if (check_for_bare_parameter_packs (range_expr))
11807 range_expr = error_mark_node;
11808 stmt = begin_range_for_stmt (scope, init);
11809 if (ivdep)
11810 RANGE_FOR_IVDEP (stmt) = 1;
11811 if (unroll)
11812 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11813 finish_range_for_decl (stmt, range_decl, range_expr);
11814 if (!type_dependent_expression_p (range_expr)
11815 /* do_auto_deduction doesn't mess with template init-lists. */
11816 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11817 do_range_for_auto_deduction (range_decl, range_expr);
11819 else
11821 stmt = begin_for_stmt (scope, init);
11822 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11823 decomp_first_name, decomp_cnt, ivdep,
11824 unroll);
11826 return stmt;
11829 /* Subroutine of cp_convert_range_for: given the initializer expression,
11830 builds up the range temporary. */
11832 static tree
11833 build_range_temp (tree range_expr)
11835 tree range_type, range_temp;
11837 /* Find out the type deduced by the declaration
11838 `auto &&__range = range_expr'. */
11839 range_type = cp_build_reference_type (make_auto (), true);
11840 range_type = do_auto_deduction (range_type, range_expr,
11841 type_uses_auto (range_type));
11843 /* Create the __range variable. */
11844 range_temp = build_decl (input_location, VAR_DECL,
11845 get_identifier ("__for_range"), range_type);
11846 TREE_USED (range_temp) = 1;
11847 DECL_ARTIFICIAL (range_temp) = 1;
11849 return range_temp;
11852 /* Used by cp_parser_range_for in template context: we aren't going to
11853 do a full conversion yet, but we still need to resolve auto in the
11854 type of the for-range-declaration if present. This is basically
11855 a shortcut version of cp_convert_range_for. */
11857 static void
11858 do_range_for_auto_deduction (tree decl, tree range_expr)
11860 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11861 if (auto_node)
11863 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11864 range_temp = convert_from_reference (build_range_temp (range_expr));
11865 iter_type = (cp_parser_perform_range_for_lookup
11866 (range_temp, &begin_dummy, &end_dummy));
11867 if (iter_type)
11869 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11870 iter_type);
11871 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11872 RO_UNARY_STAR,
11873 tf_warning_or_error);
11874 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11875 iter_decl, auto_node);
11880 /* Converts a range-based for-statement into a normal
11881 for-statement, as per the definition.
11883 for (RANGE_DECL : RANGE_EXPR)
11884 BLOCK
11886 should be equivalent to:
11889 auto &&__range = RANGE_EXPR;
11890 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11891 __begin != __end;
11892 ++__begin)
11894 RANGE_DECL = *__begin;
11895 BLOCK
11899 If RANGE_EXPR is an array:
11900 BEGIN_EXPR = __range
11901 END_EXPR = __range + ARRAY_SIZE(__range)
11902 Else if RANGE_EXPR has a member 'begin' or 'end':
11903 BEGIN_EXPR = __range.begin()
11904 END_EXPR = __range.end()
11905 Else:
11906 BEGIN_EXPR = begin(__range)
11907 END_EXPR = end(__range);
11909 If __range has a member 'begin' but not 'end', or vice versa, we must
11910 still use the second alternative (it will surely fail, however).
11911 When calling begin()/end() in the third alternative we must use
11912 argument dependent lookup, but always considering 'std' as an associated
11913 namespace. */
11915 tree
11916 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11917 tree decomp_first_name, unsigned int decomp_cnt,
11918 bool ivdep, unsigned short unroll)
11920 tree begin, end;
11921 tree iter_type, begin_expr, end_expr;
11922 tree condition, expression;
11924 range_expr = mark_lvalue_use (range_expr);
11926 if (range_decl == error_mark_node || range_expr == error_mark_node)
11927 /* If an error happened previously do nothing or else a lot of
11928 unhelpful errors would be issued. */
11929 begin_expr = end_expr = iter_type = error_mark_node;
11930 else
11932 tree range_temp;
11934 if (VAR_P (range_expr)
11935 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11936 /* Can't bind a reference to an array of runtime bound. */
11937 range_temp = range_expr;
11938 else
11940 range_temp = build_range_temp (range_expr);
11941 pushdecl (range_temp);
11942 cp_finish_decl (range_temp, range_expr,
11943 /*is_constant_init*/false, NULL_TREE,
11944 LOOKUP_ONLYCONVERTING);
11945 range_temp = convert_from_reference (range_temp);
11947 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11948 &begin_expr, &end_expr);
11951 /* The new for initialization statement. */
11952 begin = build_decl (input_location, VAR_DECL,
11953 get_identifier ("__for_begin"), iter_type);
11954 TREE_USED (begin) = 1;
11955 DECL_ARTIFICIAL (begin) = 1;
11956 pushdecl (begin);
11957 cp_finish_decl (begin, begin_expr,
11958 /*is_constant_init*/false, NULL_TREE,
11959 LOOKUP_ONLYCONVERTING);
11961 if (cxx_dialect >= cxx17)
11962 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11963 end = build_decl (input_location, VAR_DECL,
11964 get_identifier ("__for_end"), iter_type);
11965 TREE_USED (end) = 1;
11966 DECL_ARTIFICIAL (end) = 1;
11967 pushdecl (end);
11968 cp_finish_decl (end, end_expr,
11969 /*is_constant_init*/false, NULL_TREE,
11970 LOOKUP_ONLYCONVERTING);
11972 finish_init_stmt (statement);
11974 /* The new for condition. */
11975 condition = build_x_binary_op (input_location, NE_EXPR,
11976 begin, ERROR_MARK,
11977 end, ERROR_MARK,
11978 NULL, tf_warning_or_error);
11979 finish_for_cond (condition, statement, ivdep, unroll);
11981 /* The new increment expression. */
11982 expression = finish_unary_op_expr (input_location,
11983 PREINCREMENT_EXPR, begin,
11984 tf_warning_or_error);
11985 finish_for_expr (expression, statement);
11987 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11988 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11990 /* The declaration is initialized with *__begin inside the loop body. */
11991 cp_finish_decl (range_decl,
11992 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11993 tf_warning_or_error),
11994 /*is_constant_init*/false, NULL_TREE,
11995 LOOKUP_ONLYCONVERTING);
11996 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11997 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11999 return statement;
12002 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12003 We need to solve both at the same time because the method used
12004 depends on the existence of members begin or end.
12005 Returns the type deduced for the iterator expression. */
12007 static tree
12008 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12010 if (error_operand_p (range))
12012 *begin = *end = error_mark_node;
12013 return error_mark_node;
12016 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12018 error ("range-based %<for%> expression of type %qT "
12019 "has incomplete type", TREE_TYPE (range));
12020 *begin = *end = error_mark_node;
12021 return error_mark_node;
12023 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12025 /* If RANGE is an array, we will use pointer arithmetic. */
12026 *begin = decay_conversion (range, tf_warning_or_error);
12027 *end = build_binary_op (input_location, PLUS_EXPR,
12028 range,
12029 array_type_nelts_top (TREE_TYPE (range)),
12030 false);
12031 return TREE_TYPE (*begin);
12033 else
12035 /* If it is not an array, we must do a bit of magic. */
12036 tree id_begin, id_end;
12037 tree member_begin, member_end;
12039 *begin = *end = error_mark_node;
12041 id_begin = get_identifier ("begin");
12042 id_end = get_identifier ("end");
12043 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12044 /*protect=*/2, /*want_type=*/false,
12045 tf_warning_or_error);
12046 member_end = lookup_member (TREE_TYPE (range), id_end,
12047 /*protect=*/2, /*want_type=*/false,
12048 tf_warning_or_error);
12050 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12052 /* Use the member functions. */
12053 *begin = cp_parser_range_for_member_function (range, id_begin);
12054 *end = cp_parser_range_for_member_function (range, id_end);
12056 else
12058 /* Use global functions with ADL. */
12059 vec<tree, va_gc> *vec;
12060 vec = make_tree_vector ();
12062 vec_safe_push (vec, range);
12064 member_begin = perform_koenig_lookup (id_begin, vec,
12065 tf_warning_or_error);
12066 *begin = finish_call_expr (member_begin, &vec, false, true,
12067 tf_warning_or_error);
12068 member_end = perform_koenig_lookup (id_end, vec,
12069 tf_warning_or_error);
12070 *end = finish_call_expr (member_end, &vec, false, true,
12071 tf_warning_or_error);
12073 release_tree_vector (vec);
12076 /* Last common checks. */
12077 if (*begin == error_mark_node || *end == error_mark_node)
12079 /* If one of the expressions is an error do no more checks. */
12080 *begin = *end = error_mark_node;
12081 return error_mark_node;
12083 else if (type_dependent_expression_p (*begin)
12084 || type_dependent_expression_p (*end))
12085 /* Can happen, when, eg, in a template context, Koenig lookup
12086 can't resolve begin/end (c++/58503). */
12087 return NULL_TREE;
12088 else
12090 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12091 /* The unqualified type of the __begin and __end temporaries should
12092 be the same, as required by the multiple auto declaration. */
12093 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12095 if (cxx_dialect >= cxx17
12096 && (build_x_binary_op (input_location, NE_EXPR,
12097 *begin, ERROR_MARK,
12098 *end, ERROR_MARK,
12099 NULL, tf_none)
12100 != error_mark_node))
12101 /* P0184R0 allows __begin and __end to have different types,
12102 but make sure they are comparable so we can give a better
12103 diagnostic. */;
12104 else
12105 error ("inconsistent begin/end types in range-based %<for%> "
12106 "statement: %qT and %qT",
12107 TREE_TYPE (*begin), TREE_TYPE (*end));
12109 return iter_type;
12114 /* Helper function for cp_parser_perform_range_for_lookup.
12115 Builds a tree for RANGE.IDENTIFIER(). */
12117 static tree
12118 cp_parser_range_for_member_function (tree range, tree identifier)
12120 tree member, res;
12121 vec<tree, va_gc> *vec;
12123 member = finish_class_member_access_expr (range, identifier,
12124 false, tf_warning_or_error);
12125 if (member == error_mark_node)
12126 return error_mark_node;
12128 vec = make_tree_vector ();
12129 res = finish_call_expr (member, &vec,
12130 /*disallow_virtual=*/false,
12131 /*koenig_p=*/false,
12132 tf_warning_or_error);
12133 release_tree_vector (vec);
12134 return res;
12137 /* Parse an iteration-statement.
12139 iteration-statement:
12140 while ( condition ) statement
12141 do statement while ( expression ) ;
12142 for ( init-statement condition [opt] ; expression [opt] )
12143 statement
12145 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12147 static tree
12148 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12149 unsigned short unroll)
12151 cp_token *token;
12152 enum rid keyword;
12153 tree statement;
12154 unsigned char in_statement;
12155 token_indent_info guard_tinfo;
12157 /* Peek at the next token. */
12158 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12159 if (!token)
12160 return error_mark_node;
12162 guard_tinfo = get_token_indent_info (token);
12164 /* Remember whether or not we are already within an iteration
12165 statement. */
12166 in_statement = parser->in_statement;
12168 /* See what kind of keyword it is. */
12169 keyword = token->keyword;
12170 switch (keyword)
12172 case RID_WHILE:
12174 tree condition;
12176 /* Begin the while-statement. */
12177 statement = begin_while_stmt ();
12178 /* Look for the `('. */
12179 matching_parens parens;
12180 parens.require_open (parser);
12181 /* Parse the condition. */
12182 condition = cp_parser_condition (parser);
12183 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12184 /* Look for the `)'. */
12185 parens.require_close (parser);
12186 /* Parse the dependent statement. */
12187 parser->in_statement = IN_ITERATION_STMT;
12188 bool prev = note_iteration_stmt_body_start ();
12189 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12190 note_iteration_stmt_body_end (prev);
12191 parser->in_statement = in_statement;
12192 /* We're done with the while-statement. */
12193 finish_while_stmt (statement);
12195 break;
12197 case RID_DO:
12199 tree expression;
12201 /* Begin the do-statement. */
12202 statement = begin_do_stmt ();
12203 /* Parse the body of the do-statement. */
12204 parser->in_statement = IN_ITERATION_STMT;
12205 bool prev = note_iteration_stmt_body_start ();
12206 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12207 note_iteration_stmt_body_end (prev);
12208 parser->in_statement = in_statement;
12209 finish_do_body (statement);
12210 /* Look for the `while' keyword. */
12211 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12212 /* Look for the `('. */
12213 matching_parens parens;
12214 parens.require_open (parser);
12215 /* Parse the expression. */
12216 expression = cp_parser_expression (parser);
12217 /* We're done with the do-statement. */
12218 finish_do_stmt (expression, statement, ivdep, unroll);
12219 /* Look for the `)'. */
12220 parens.require_close (parser);
12221 /* Look for the `;'. */
12222 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12224 break;
12226 case RID_FOR:
12228 /* Look for the `('. */
12229 matching_parens parens;
12230 parens.require_open (parser);
12232 statement = cp_parser_for (parser, ivdep, unroll);
12234 /* Look for the `)'. */
12235 parens.require_close (parser);
12237 /* Parse the body of the for-statement. */
12238 parser->in_statement = IN_ITERATION_STMT;
12239 bool prev = note_iteration_stmt_body_start ();
12240 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12241 note_iteration_stmt_body_end (prev);
12242 parser->in_statement = in_statement;
12244 /* We're done with the for-statement. */
12245 finish_for_stmt (statement);
12247 break;
12249 default:
12250 cp_parser_error (parser, "expected iteration-statement");
12251 statement = error_mark_node;
12252 break;
12255 return statement;
12258 /* Parse a init-statement or the declarator of a range-based-for.
12259 Returns true if a range-based-for declaration is seen.
12261 init-statement:
12262 expression-statement
12263 simple-declaration */
12265 static bool
12266 cp_parser_init_statement (cp_parser* parser, tree *decl)
12268 /* If the next token is a `;', then we have an empty
12269 expression-statement. Grammatically, this is also a
12270 simple-declaration, but an invalid one, because it does not
12271 declare anything. Therefore, if we did not handle this case
12272 specially, we would issue an error message about an invalid
12273 declaration. */
12274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12276 bool is_range_for = false;
12277 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12279 /* A colon is used in range-based for. */
12280 parser->colon_corrects_to_scope_p = false;
12282 /* We're going to speculatively look for a declaration, falling back
12283 to an expression, if necessary. */
12284 cp_parser_parse_tentatively (parser);
12285 /* Parse the declaration. */
12286 cp_parser_simple_declaration (parser,
12287 /*function_definition_allowed_p=*/false,
12288 decl);
12289 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12290 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12292 /* It is a range-for, consume the ':' */
12293 cp_lexer_consume_token (parser->lexer);
12294 is_range_for = true;
12295 if (cxx_dialect < cxx11)
12297 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12298 "range-based %<for%> loops only available with "
12299 "-std=c++11 or -std=gnu++11");
12300 *decl = error_mark_node;
12303 else
12304 /* The ';' is not consumed yet because we told
12305 cp_parser_simple_declaration not to. */
12306 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12308 if (cp_parser_parse_definitely (parser))
12309 return is_range_for;
12310 /* If the tentative parse failed, then we shall need to look for an
12311 expression-statement. */
12313 /* If we are here, it is an expression-statement. */
12314 cp_parser_expression_statement (parser, NULL_TREE);
12315 return false;
12318 /* Parse a jump-statement.
12320 jump-statement:
12321 break ;
12322 continue ;
12323 return expression [opt] ;
12324 return braced-init-list ;
12325 goto identifier ;
12327 GNU extension:
12329 jump-statement:
12330 goto * expression ;
12332 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12334 static tree
12335 cp_parser_jump_statement (cp_parser* parser)
12337 tree statement = error_mark_node;
12338 cp_token *token;
12339 enum rid keyword;
12340 unsigned char in_statement;
12342 /* Peek at the next token. */
12343 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12344 if (!token)
12345 return error_mark_node;
12347 /* See what kind of keyword it is. */
12348 keyword = token->keyword;
12349 switch (keyword)
12351 case RID_BREAK:
12352 in_statement = parser->in_statement & ~IN_IF_STMT;
12353 switch (in_statement)
12355 case 0:
12356 error_at (token->location, "break statement not within loop or switch");
12357 break;
12358 default:
12359 gcc_assert ((in_statement & IN_SWITCH_STMT)
12360 || in_statement == IN_ITERATION_STMT);
12361 statement = finish_break_stmt ();
12362 if (in_statement == IN_ITERATION_STMT)
12363 break_maybe_infinite_loop ();
12364 break;
12365 case IN_OMP_BLOCK:
12366 error_at (token->location, "invalid exit from OpenMP structured block");
12367 break;
12368 case IN_OMP_FOR:
12369 error_at (token->location, "break statement used with OpenMP for loop");
12370 break;
12372 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12373 break;
12375 case RID_CONTINUE:
12376 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12378 case 0:
12379 error_at (token->location, "continue statement not within a loop");
12380 break;
12381 /* Fall through. */
12382 case IN_ITERATION_STMT:
12383 case IN_OMP_FOR:
12384 statement = finish_continue_stmt ();
12385 break;
12386 case IN_OMP_BLOCK:
12387 error_at (token->location, "invalid exit from OpenMP structured block");
12388 break;
12389 default:
12390 gcc_unreachable ();
12392 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12393 break;
12395 case RID_RETURN:
12397 tree expr;
12398 bool expr_non_constant_p;
12400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12402 cp_lexer_set_source_position (parser->lexer);
12403 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12404 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12406 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12407 expr = cp_parser_expression (parser);
12408 else
12409 /* If the next token is a `;', then there is no
12410 expression. */
12411 expr = NULL_TREE;
12412 /* Build the return-statement. */
12413 if (current_function_auto_return_pattern && in_discarded_stmt)
12414 /* Don't deduce from a discarded return statement. */;
12415 else
12416 statement = finish_return_stmt (expr);
12417 /* Look for the final `;'. */
12418 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12420 break;
12422 case RID_GOTO:
12423 if (parser->in_function_body
12424 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12426 error ("%<goto%> in %<constexpr%> function");
12427 cp_function_chain->invalid_constexpr = true;
12430 /* Create the goto-statement. */
12431 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12433 /* Issue a warning about this use of a GNU extension. */
12434 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12435 /* Consume the '*' token. */
12436 cp_lexer_consume_token (parser->lexer);
12437 /* Parse the dependent expression. */
12438 finish_goto_stmt (cp_parser_expression (parser));
12440 else
12441 finish_goto_stmt (cp_parser_identifier (parser));
12442 /* Look for the final `;'. */
12443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12444 break;
12446 default:
12447 cp_parser_error (parser, "expected jump-statement");
12448 break;
12451 return statement;
12454 /* Parse a declaration-statement.
12456 declaration-statement:
12457 block-declaration */
12459 static void
12460 cp_parser_declaration_statement (cp_parser* parser)
12462 void *p;
12464 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12465 p = obstack_alloc (&declarator_obstack, 0);
12467 /* Parse the block-declaration. */
12468 cp_parser_block_declaration (parser, /*statement_p=*/true);
12470 /* Free any declarators allocated. */
12471 obstack_free (&declarator_obstack, p);
12474 /* Some dependent statements (like `if (cond) statement'), are
12475 implicitly in their own scope. In other words, if the statement is
12476 a single statement (as opposed to a compound-statement), it is
12477 none-the-less treated as if it were enclosed in braces. Any
12478 declarations appearing in the dependent statement are out of scope
12479 after control passes that point. This function parses a statement,
12480 but ensures that is in its own scope, even if it is not a
12481 compound-statement.
12483 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12484 is a (possibly labeled) if statement which is not enclosed in
12485 braces and has an else clause. This is used to implement
12486 -Wparentheses.
12488 CHAIN is a vector of if-else-if conditions. This is used to implement
12489 -Wduplicated-cond.
12491 Returns the new statement. */
12493 static tree
12494 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12495 const token_indent_info &guard_tinfo,
12496 vec<tree> *chain)
12498 tree statement;
12499 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12500 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12501 token_indent_info body_tinfo
12502 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12504 if (if_p != NULL)
12505 *if_p = false;
12507 /* Mark if () ; with a special NOP_EXPR. */
12508 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12510 cp_lexer_consume_token (parser->lexer);
12511 statement = add_stmt (build_empty_stmt (body_loc));
12513 if (guard_tinfo.keyword == RID_IF
12514 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12515 warning_at (body_loc, OPT_Wempty_body,
12516 "suggest braces around empty body in an %<if%> statement");
12517 else if (guard_tinfo.keyword == RID_ELSE)
12518 warning_at (body_loc, OPT_Wempty_body,
12519 "suggest braces around empty body in an %<else%> statement");
12521 /* if a compound is opened, we simply parse the statement directly. */
12522 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12523 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12524 /* If the token is not a `{', then we must take special action. */
12525 else
12527 /* Create a compound-statement. */
12528 statement = begin_compound_stmt (0);
12529 /* Parse the dependent-statement. */
12530 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12531 &body_loc_after_labels);
12532 /* Finish the dummy compound-statement. */
12533 finish_compound_stmt (statement);
12536 token_indent_info next_tinfo
12537 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12538 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12540 if (body_loc_after_labels != UNKNOWN_LOCATION
12541 && next_tinfo.type != CPP_SEMICOLON)
12542 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12543 guard_tinfo.location, guard_tinfo.keyword);
12545 /* Return the statement. */
12546 return statement;
12549 /* For some dependent statements (like `while (cond) statement'), we
12550 have already created a scope. Therefore, even if the dependent
12551 statement is a compound-statement, we do not want to create another
12552 scope. */
12554 static void
12555 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12556 const token_indent_info &guard_tinfo)
12558 /* If the token is a `{', then we must take special action. */
12559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12561 token_indent_info body_tinfo
12562 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12563 location_t loc_after_labels = UNKNOWN_LOCATION;
12565 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12566 &loc_after_labels);
12567 token_indent_info next_tinfo
12568 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12569 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12571 if (loc_after_labels != UNKNOWN_LOCATION
12572 && next_tinfo.type != CPP_SEMICOLON)
12573 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12574 guard_tinfo.location,
12575 guard_tinfo.keyword);
12577 else
12579 /* Avoid calling cp_parser_compound_statement, so that we
12580 don't create a new scope. Do everything else by hand. */
12581 matching_braces braces;
12582 braces.require_open (parser);
12583 /* If the next keyword is `__label__' we have a label declaration. */
12584 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12585 cp_parser_label_declaration (parser);
12586 /* Parse an (optional) statement-seq. */
12587 cp_parser_statement_seq_opt (parser, NULL_TREE);
12588 braces.require_close (parser);
12592 /* Declarations [gram.dcl.dcl] */
12594 /* Parse an optional declaration-sequence.
12596 declaration-seq:
12597 declaration
12598 declaration-seq declaration */
12600 static void
12601 cp_parser_declaration_seq_opt (cp_parser* parser)
12603 while (true)
12605 cp_token *token;
12607 token = cp_lexer_peek_token (parser->lexer);
12609 if (token->type == CPP_CLOSE_BRACE
12610 || token->type == CPP_EOF
12611 || token->type == CPP_PRAGMA_EOL)
12612 break;
12614 if (token->type == CPP_SEMICOLON)
12616 /* A declaration consisting of a single semicolon is
12617 invalid. Allow it unless we're being pedantic. */
12618 cp_lexer_consume_token (parser->lexer);
12619 if (!in_system_header_at (input_location))
12620 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12621 continue;
12624 /* If we're entering or exiting a region that's implicitly
12625 extern "C", modify the lang context appropriately. */
12626 if (!parser->implicit_extern_c && token->implicit_extern_c)
12628 push_lang_context (lang_name_c);
12629 parser->implicit_extern_c = true;
12631 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12633 pop_lang_context ();
12634 parser->implicit_extern_c = false;
12637 if (token->type == CPP_PRAGMA)
12639 /* A top-level declaration can consist solely of a #pragma.
12640 A nested declaration cannot, so this is done here and not
12641 in cp_parser_declaration. (A #pragma at block scope is
12642 handled in cp_parser_statement.) */
12643 cp_parser_pragma (parser, pragma_external, NULL);
12644 continue;
12647 /* Parse the declaration itself. */
12648 cp_parser_declaration (parser);
12652 /* Parse a declaration.
12654 declaration:
12655 block-declaration
12656 function-definition
12657 template-declaration
12658 explicit-instantiation
12659 explicit-specialization
12660 linkage-specification
12661 namespace-definition
12663 C++17:
12664 deduction-guide
12666 GNU extension:
12668 declaration:
12669 __extension__ declaration */
12671 static void
12672 cp_parser_declaration (cp_parser* parser)
12674 cp_token token1;
12675 cp_token token2;
12676 int saved_pedantic;
12677 void *p;
12678 tree attributes = NULL_TREE;
12680 /* Check for the `__extension__' keyword. */
12681 if (cp_parser_extension_opt (parser, &saved_pedantic))
12683 /* Parse the qualified declaration. */
12684 cp_parser_declaration (parser);
12685 /* Restore the PEDANTIC flag. */
12686 pedantic = saved_pedantic;
12688 return;
12691 /* Try to figure out what kind of declaration is present. */
12692 token1 = *cp_lexer_peek_token (parser->lexer);
12694 if (token1.type != CPP_EOF)
12695 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12696 else
12698 token2.type = CPP_EOF;
12699 token2.keyword = RID_MAX;
12702 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12703 p = obstack_alloc (&declarator_obstack, 0);
12705 /* If the next token is `extern' and the following token is a string
12706 literal, then we have a linkage specification. */
12707 if (token1.keyword == RID_EXTERN
12708 && cp_parser_is_pure_string_literal (&token2))
12709 cp_parser_linkage_specification (parser);
12710 /* If the next token is `template', then we have either a template
12711 declaration, an explicit instantiation, or an explicit
12712 specialization. */
12713 else if (token1.keyword == RID_TEMPLATE)
12715 /* `template <>' indicates a template specialization. */
12716 if (token2.type == CPP_LESS
12717 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12718 cp_parser_explicit_specialization (parser);
12719 /* `template <' indicates a template declaration. */
12720 else if (token2.type == CPP_LESS)
12721 cp_parser_template_declaration (parser, /*member_p=*/false);
12722 /* Anything else must be an explicit instantiation. */
12723 else
12724 cp_parser_explicit_instantiation (parser);
12726 /* If the next token is `export', then we have a template
12727 declaration. */
12728 else if (token1.keyword == RID_EXPORT)
12729 cp_parser_template_declaration (parser, /*member_p=*/false);
12730 /* If the next token is `extern', 'static' or 'inline' and the one
12731 after that is `template', we have a GNU extended explicit
12732 instantiation directive. */
12733 else if (cp_parser_allow_gnu_extensions_p (parser)
12734 && (token1.keyword == RID_EXTERN
12735 || token1.keyword == RID_STATIC
12736 || token1.keyword == RID_INLINE)
12737 && token2.keyword == RID_TEMPLATE)
12738 cp_parser_explicit_instantiation (parser);
12739 /* If the next token is `namespace', check for a named or unnamed
12740 namespace definition. */
12741 else if (token1.keyword == RID_NAMESPACE
12742 && (/* A named namespace definition. */
12743 (token2.type == CPP_NAME
12744 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12745 != CPP_EQ))
12746 || (token2.type == CPP_OPEN_SQUARE
12747 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12748 == CPP_OPEN_SQUARE)
12749 /* An unnamed namespace definition. */
12750 || token2.type == CPP_OPEN_BRACE
12751 || token2.keyword == RID_ATTRIBUTE))
12752 cp_parser_namespace_definition (parser);
12753 /* An inline (associated) namespace definition. */
12754 else if (token1.keyword == RID_INLINE
12755 && token2.keyword == RID_NAMESPACE)
12756 cp_parser_namespace_definition (parser);
12757 /* Objective-C++ declaration/definition. */
12758 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12759 cp_parser_objc_declaration (parser, NULL_TREE);
12760 else if (c_dialect_objc ()
12761 && token1.keyword == RID_ATTRIBUTE
12762 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12763 cp_parser_objc_declaration (parser, attributes);
12764 /* At this point we may have a template declared by a concept
12765 introduction. */
12766 else if (flag_concepts
12767 && cp_parser_template_declaration_after_export (parser,
12768 /*member_p=*/false))
12769 /* We did. */;
12770 else
12771 /* Try to parse a block-declaration, or a function-definition. */
12772 cp_parser_block_declaration (parser, /*statement_p=*/false);
12774 /* Free any declarators allocated. */
12775 obstack_free (&declarator_obstack, p);
12778 /* Parse a block-declaration.
12780 block-declaration:
12781 simple-declaration
12782 asm-definition
12783 namespace-alias-definition
12784 using-declaration
12785 using-directive
12787 GNU Extension:
12789 block-declaration:
12790 __extension__ block-declaration
12792 C++0x Extension:
12794 block-declaration:
12795 static_assert-declaration
12797 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12798 part of a declaration-statement. */
12800 static void
12801 cp_parser_block_declaration (cp_parser *parser,
12802 bool statement_p)
12804 cp_token *token1;
12805 int saved_pedantic;
12807 /* Check for the `__extension__' keyword. */
12808 if (cp_parser_extension_opt (parser, &saved_pedantic))
12810 /* Parse the qualified declaration. */
12811 cp_parser_block_declaration (parser, statement_p);
12812 /* Restore the PEDANTIC flag. */
12813 pedantic = saved_pedantic;
12815 return;
12818 /* Peek at the next token to figure out which kind of declaration is
12819 present. */
12820 token1 = cp_lexer_peek_token (parser->lexer);
12822 /* If the next keyword is `asm', we have an asm-definition. */
12823 if (token1->keyword == RID_ASM)
12825 if (statement_p)
12826 cp_parser_commit_to_tentative_parse (parser);
12827 cp_parser_asm_definition (parser);
12829 /* If the next keyword is `namespace', we have a
12830 namespace-alias-definition. */
12831 else if (token1->keyword == RID_NAMESPACE)
12832 cp_parser_namespace_alias_definition (parser);
12833 /* If the next keyword is `using', we have a
12834 using-declaration, a using-directive, or an alias-declaration. */
12835 else if (token1->keyword == RID_USING)
12837 cp_token *token2;
12839 if (statement_p)
12840 cp_parser_commit_to_tentative_parse (parser);
12841 /* If the token after `using' is `namespace', then we have a
12842 using-directive. */
12843 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12844 if (token2->keyword == RID_NAMESPACE)
12845 cp_parser_using_directive (parser);
12846 /* If the second token after 'using' is '=', then we have an
12847 alias-declaration. */
12848 else if (cxx_dialect >= cxx11
12849 && token2->type == CPP_NAME
12850 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12851 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12852 cp_parser_alias_declaration (parser);
12853 /* Otherwise, it's a using-declaration. */
12854 else
12855 cp_parser_using_declaration (parser,
12856 /*access_declaration_p=*/false);
12858 /* If the next keyword is `__label__' we have a misplaced label
12859 declaration. */
12860 else if (token1->keyword == RID_LABEL)
12862 cp_lexer_consume_token (parser->lexer);
12863 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12864 cp_parser_skip_to_end_of_statement (parser);
12865 /* If the next token is now a `;', consume it. */
12866 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12867 cp_lexer_consume_token (parser->lexer);
12869 /* If the next token is `static_assert' we have a static assertion. */
12870 else if (token1->keyword == RID_STATIC_ASSERT)
12871 cp_parser_static_assert (parser, /*member_p=*/false);
12872 /* Anything else must be a simple-declaration. */
12873 else
12874 cp_parser_simple_declaration (parser, !statement_p,
12875 /*maybe_range_for_decl*/NULL);
12878 /* Parse a simple-declaration.
12880 simple-declaration:
12881 decl-specifier-seq [opt] init-declarator-list [opt] ;
12882 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12883 brace-or-equal-initializer ;
12885 init-declarator-list:
12886 init-declarator
12887 init-declarator-list , init-declarator
12889 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12890 function-definition as a simple-declaration.
12892 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12893 parsed declaration if it is an uninitialized single declarator not followed
12894 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12895 if present, will not be consumed. */
12897 static void
12898 cp_parser_simple_declaration (cp_parser* parser,
12899 bool function_definition_allowed_p,
12900 tree *maybe_range_for_decl)
12902 cp_decl_specifier_seq decl_specifiers;
12903 int declares_class_or_enum;
12904 bool saw_declarator;
12905 location_t comma_loc = UNKNOWN_LOCATION;
12906 location_t init_loc = UNKNOWN_LOCATION;
12908 if (maybe_range_for_decl)
12909 *maybe_range_for_decl = NULL_TREE;
12911 /* Defer access checks until we know what is being declared; the
12912 checks for names appearing in the decl-specifier-seq should be
12913 done as if we were in the scope of the thing being declared. */
12914 push_deferring_access_checks (dk_deferred);
12916 /* Parse the decl-specifier-seq. We have to keep track of whether
12917 or not the decl-specifier-seq declares a named class or
12918 enumeration type, since that is the only case in which the
12919 init-declarator-list is allowed to be empty.
12921 [dcl.dcl]
12923 In a simple-declaration, the optional init-declarator-list can be
12924 omitted only when declaring a class or enumeration, that is when
12925 the decl-specifier-seq contains either a class-specifier, an
12926 elaborated-type-specifier, or an enum-specifier. */
12927 cp_parser_decl_specifier_seq (parser,
12928 CP_PARSER_FLAGS_OPTIONAL,
12929 &decl_specifiers,
12930 &declares_class_or_enum);
12931 /* We no longer need to defer access checks. */
12932 stop_deferring_access_checks ();
12934 /* In a block scope, a valid declaration must always have a
12935 decl-specifier-seq. By not trying to parse declarators, we can
12936 resolve the declaration/expression ambiguity more quickly. */
12937 if (!function_definition_allowed_p
12938 && !decl_specifiers.any_specifiers_p)
12940 cp_parser_error (parser, "expected declaration");
12941 goto done;
12944 /* If the next two tokens are both identifiers, the code is
12945 erroneous. The usual cause of this situation is code like:
12947 T t;
12949 where "T" should name a type -- but does not. */
12950 if (!decl_specifiers.any_type_specifiers_p
12951 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12953 /* If parsing tentatively, we should commit; we really are
12954 looking at a declaration. */
12955 cp_parser_commit_to_tentative_parse (parser);
12956 /* Give up. */
12957 goto done;
12960 /* If we have seen at least one decl-specifier, and the next token
12961 is not a parenthesis, then we must be looking at a declaration.
12962 (After "int (" we might be looking at a functional cast.) */
12963 if (decl_specifiers.any_specifiers_p
12964 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12965 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12966 && !cp_parser_error_occurred (parser))
12967 cp_parser_commit_to_tentative_parse (parser);
12969 /* Look for C++17 decomposition declaration. */
12970 for (size_t n = 1; ; n++)
12971 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12972 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12973 continue;
12974 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12975 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12976 && decl_specifiers.any_specifiers_p)
12978 tree decl
12979 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12980 maybe_range_for_decl,
12981 &init_loc);
12983 /* The next token should be either a `,' or a `;'. */
12984 cp_token *token = cp_lexer_peek_token (parser->lexer);
12985 /* If it's a `;', we are done. */
12986 if (token->type == CPP_SEMICOLON)
12987 goto finish;
12988 else if (maybe_range_for_decl)
12990 if (*maybe_range_for_decl == NULL_TREE)
12991 *maybe_range_for_decl = error_mark_node;
12992 goto finish;
12994 /* Anything else is an error. */
12995 else
12997 /* If we have already issued an error message we don't need
12998 to issue another one. */
12999 if ((decl != error_mark_node
13000 && DECL_INITIAL (decl) != error_mark_node)
13001 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13002 cp_parser_error (parser, "expected %<,%> or %<;%>");
13003 /* Skip tokens until we reach the end of the statement. */
13004 cp_parser_skip_to_end_of_statement (parser);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13007 cp_lexer_consume_token (parser->lexer);
13008 goto done;
13011 else
13012 break;
13014 tree last_type;
13015 bool auto_specifier_p;
13016 /* NULL_TREE if both variable and function declaration are allowed,
13017 error_mark_node if function declaration are not allowed and
13018 a FUNCTION_DECL that should be diagnosed if it is followed by
13019 variable declarations. */
13020 tree auto_function_declaration;
13022 last_type = NULL_TREE;
13023 auto_specifier_p
13024 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13025 auto_function_declaration = NULL_TREE;
13027 /* Keep going until we hit the `;' at the end of the simple
13028 declaration. */
13029 saw_declarator = false;
13030 while (cp_lexer_next_token_is_not (parser->lexer,
13031 CPP_SEMICOLON))
13033 cp_token *token;
13034 bool function_definition_p;
13035 tree decl;
13036 tree auto_result = NULL_TREE;
13038 if (saw_declarator)
13040 /* If we are processing next declarator, comma is expected */
13041 token = cp_lexer_peek_token (parser->lexer);
13042 gcc_assert (token->type == CPP_COMMA);
13043 cp_lexer_consume_token (parser->lexer);
13044 if (maybe_range_for_decl)
13046 *maybe_range_for_decl = error_mark_node;
13047 if (comma_loc == UNKNOWN_LOCATION)
13048 comma_loc = token->location;
13051 else
13052 saw_declarator = true;
13054 /* Parse the init-declarator. */
13055 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13056 /*checks=*/NULL,
13057 function_definition_allowed_p,
13058 /*member_p=*/false,
13059 declares_class_or_enum,
13060 &function_definition_p,
13061 maybe_range_for_decl,
13062 &init_loc,
13063 &auto_result);
13064 /* If an error occurred while parsing tentatively, exit quickly.
13065 (That usually happens when in the body of a function; each
13066 statement is treated as a declaration-statement until proven
13067 otherwise.) */
13068 if (cp_parser_error_occurred (parser))
13069 goto done;
13071 if (auto_specifier_p && cxx_dialect >= cxx14)
13073 /* If the init-declarator-list contains more than one
13074 init-declarator, they shall all form declarations of
13075 variables. */
13076 if (auto_function_declaration == NULL_TREE)
13077 auto_function_declaration
13078 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13079 else if (TREE_CODE (decl) == FUNCTION_DECL
13080 || auto_function_declaration != error_mark_node)
13082 error_at (decl_specifiers.locations[ds_type_spec],
13083 "non-variable %qD in declaration with more than one "
13084 "declarator with placeholder type",
13085 TREE_CODE (decl) == FUNCTION_DECL
13086 ? decl : auto_function_declaration);
13087 auto_function_declaration = error_mark_node;
13091 if (auto_result
13092 && (!processing_template_decl || !type_uses_auto (auto_result)))
13094 if (last_type
13095 && last_type != error_mark_node
13096 && !same_type_p (auto_result, last_type))
13098 /* If the list of declarators contains more than one declarator,
13099 the type of each declared variable is determined as described
13100 above. If the type deduced for the template parameter U is not
13101 the same in each deduction, the program is ill-formed. */
13102 error_at (decl_specifiers.locations[ds_type_spec],
13103 "inconsistent deduction for %qT: %qT and then %qT",
13104 decl_specifiers.type, last_type, auto_result);
13105 last_type = error_mark_node;
13107 else
13108 last_type = auto_result;
13111 /* Handle function definitions specially. */
13112 if (function_definition_p)
13114 /* If the next token is a `,', then we are probably
13115 processing something like:
13117 void f() {}, *p;
13119 which is erroneous. */
13120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13122 cp_token *token = cp_lexer_peek_token (parser->lexer);
13123 error_at (token->location,
13124 "mixing"
13125 " declarations and function-definitions is forbidden");
13127 /* Otherwise, we're done with the list of declarators. */
13128 else
13130 pop_deferring_access_checks ();
13131 return;
13134 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13135 *maybe_range_for_decl = decl;
13136 /* The next token should be either a `,' or a `;'. */
13137 token = cp_lexer_peek_token (parser->lexer);
13138 /* If it's a `,', there are more declarators to come. */
13139 if (token->type == CPP_COMMA)
13140 /* will be consumed next time around */;
13141 /* If it's a `;', we are done. */
13142 else if (token->type == CPP_SEMICOLON)
13143 break;
13144 else if (maybe_range_for_decl)
13146 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13147 permerror (decl_specifiers.locations[ds_type_spec],
13148 "types may not be defined in a for-range-declaration");
13149 break;
13151 /* Anything else is an error. */
13152 else
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl != error_mark_node
13157 && DECL_INITIAL (decl) != error_mark_node)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13159 cp_parser_error (parser, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13164 cp_lexer_consume_token (parser->lexer);
13165 goto done;
13167 /* After the first time around, a function-definition is not
13168 allowed -- even if it was OK at first. For example:
13170 int i, f() {}
13172 is not valid. */
13173 function_definition_allowed_p = false;
13176 /* Issue an error message if no declarators are present, and the
13177 decl-specifier-seq does not itself declare a class or
13178 enumeration: [dcl.dcl]/3. */
13179 if (!saw_declarator)
13181 if (cp_parser_declares_only_class_p (parser))
13183 if (!declares_class_or_enum
13184 && decl_specifiers.type
13185 && OVERLOAD_TYPE_P (decl_specifiers.type))
13186 /* Ensure an error is issued anyway when finish_decltype_type,
13187 called via cp_parser_decl_specifier_seq, returns a class or
13188 an enumeration (c++/51786). */
13189 decl_specifiers.type = NULL_TREE;
13190 shadow_tag (&decl_specifiers);
13192 /* Perform any deferred access checks. */
13193 perform_deferred_access_checks (tf_warning_or_error);
13196 /* Consume the `;'. */
13197 finish:
13198 if (!maybe_range_for_decl)
13199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13200 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13202 if (init_loc != UNKNOWN_LOCATION)
13203 error_at (init_loc, "initializer in range-based %<for%> loop");
13204 if (comma_loc != UNKNOWN_LOCATION)
13205 error_at (comma_loc,
13206 "multiple declarations in range-based %<for%> loop");
13209 done:
13210 pop_deferring_access_checks ();
13213 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13214 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13215 initializer ; */
13217 static tree
13218 cp_parser_decomposition_declaration (cp_parser *parser,
13219 cp_decl_specifier_seq *decl_specifiers,
13220 tree *maybe_range_for_decl,
13221 location_t *init_loc)
13223 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13225 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13227 /* Parse the identifier-list. */
13228 auto_vec<cp_expr, 10> v;
13229 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13230 while (true)
13232 cp_expr e = cp_parser_identifier (parser);
13233 if (e.get_value () == error_mark_node)
13234 break;
13235 v.safe_push (e);
13236 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13237 break;
13238 cp_lexer_consume_token (parser->lexer);
13241 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13242 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13244 end_loc = UNKNOWN_LOCATION;
13245 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13246 false);
13247 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13248 cp_lexer_consume_token (parser->lexer);
13249 else
13251 cp_parser_skip_to_end_of_statement (parser);
13252 return error_mark_node;
13256 if (cxx_dialect < cxx17)
13257 pedwarn (loc, 0, "structured bindings only available with "
13258 "-std=c++17 or -std=gnu++17");
13260 tree pushed_scope;
13261 cp_declarator *declarator = make_declarator (cdk_decomp);
13262 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13263 declarator->id_loc = loc;
13264 if (ref_qual != REF_QUAL_NONE)
13265 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13266 ref_qual == REF_QUAL_RVALUE,
13267 NULL_TREE);
13268 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13269 NULL_TREE, decl_specifiers->attributes,
13270 &pushed_scope);
13271 tree orig_decl = decl;
13273 unsigned int i;
13274 cp_expr e;
13275 cp_decl_specifier_seq decl_specs;
13276 clear_decl_specs (&decl_specs);
13277 decl_specs.type = make_auto ();
13278 tree prev = decl;
13279 FOR_EACH_VEC_ELT (v, i, e)
13281 if (i == 0)
13282 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13283 else
13284 declarator->u.id.unqualified_name = e.get_value ();
13285 declarator->id_loc = e.get_location ();
13286 tree elt_pushed_scope;
13287 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13288 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13289 if (decl2 == error_mark_node)
13290 decl = error_mark_node;
13291 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13293 /* Ensure we've diagnosed redeclaration if we aren't creating
13294 a new VAR_DECL. */
13295 gcc_assert (errorcount);
13296 decl = error_mark_node;
13298 else
13299 prev = decl2;
13300 if (elt_pushed_scope)
13301 pop_scope (elt_pushed_scope);
13304 if (v.is_empty ())
13306 error_at (loc, "empty structured binding declaration");
13307 decl = error_mark_node;
13310 if (maybe_range_for_decl == NULL
13311 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13313 bool non_constant_p = false, is_direct_init = false;
13314 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13315 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13316 &non_constant_p);
13317 if (initializer == NULL_TREE
13318 || (TREE_CODE (initializer) == TREE_LIST
13319 && TREE_CHAIN (initializer))
13320 || (is_direct_init
13321 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13322 && CONSTRUCTOR_NELTS (initializer) != 1))
13324 error_at (loc, "invalid initializer for structured binding "
13325 "declaration");
13326 initializer = error_mark_node;
13329 if (decl != error_mark_node)
13331 cp_maybe_mangle_decomp (decl, prev, v.length ());
13332 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13333 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13334 cp_finish_decomp (decl, prev, v.length ());
13337 else if (decl != error_mark_node)
13339 *maybe_range_for_decl = prev;
13340 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13341 the underlying DECL. */
13342 cp_finish_decomp (decl, prev, v.length ());
13345 if (pushed_scope)
13346 pop_scope (pushed_scope);
13348 if (decl == error_mark_node && DECL_P (orig_decl))
13350 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13351 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13354 return decl;
13357 /* Parse a decl-specifier-seq.
13359 decl-specifier-seq:
13360 decl-specifier-seq [opt] decl-specifier
13361 decl-specifier attribute-specifier-seq [opt] (C++11)
13363 decl-specifier:
13364 storage-class-specifier
13365 type-specifier
13366 function-specifier
13367 friend
13368 typedef
13370 GNU Extension:
13372 decl-specifier:
13373 attributes
13375 Concepts Extension:
13377 decl-specifier:
13378 concept
13380 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13382 The parser flags FLAGS is used to control type-specifier parsing.
13384 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13385 flags:
13387 1: one of the decl-specifiers is an elaborated-type-specifier
13388 (i.e., a type declaration)
13389 2: one of the decl-specifiers is an enum-specifier or a
13390 class-specifier (i.e., a type definition)
13394 static void
13395 cp_parser_decl_specifier_seq (cp_parser* parser,
13396 cp_parser_flags flags,
13397 cp_decl_specifier_seq *decl_specs,
13398 int* declares_class_or_enum)
13400 bool constructor_possible_p = !parser->in_declarator_p;
13401 bool found_decl_spec = false;
13402 cp_token *start_token = NULL;
13403 cp_decl_spec ds;
13405 /* Clear DECL_SPECS. */
13406 clear_decl_specs (decl_specs);
13408 /* Assume no class or enumeration type is declared. */
13409 *declares_class_or_enum = 0;
13411 /* Keep reading specifiers until there are no more to read. */
13412 while (true)
13414 bool constructor_p;
13415 cp_token *token;
13416 ds = ds_last;
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13421 /* Save the first token of the decl spec list for error
13422 reporting. */
13423 if (!start_token)
13424 start_token = token;
13425 /* Handle attributes. */
13426 if (cp_next_tokens_can_be_attribute_p (parser))
13428 /* Parse the attributes. */
13429 tree attrs = cp_parser_attributes_opt (parser);
13431 /* In a sequence of declaration specifiers, c++11 attributes
13432 appertain to the type that precede them. In that case
13433 [dcl.spec]/1 says:
13435 The attribute-specifier-seq affects the type only for
13436 the declaration it appears in, not other declarations
13437 involving the same type.
13439 But for now let's force the user to position the
13440 attribute either at the beginning of the declaration or
13441 after the declarator-id, which would clearly mean that it
13442 applies to the declarator. */
13443 if (cxx11_attribute_p (attrs))
13445 if (!found_decl_spec)
13446 /* The c++11 attribute is at the beginning of the
13447 declaration. It appertains to the entity being
13448 declared. */;
13449 else
13451 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13453 /* This is an attribute following a
13454 class-specifier. */
13455 if (decl_specs->type_definition_p)
13456 warn_misplaced_attr_for_class_type (token->location,
13457 decl_specs->type);
13458 attrs = NULL_TREE;
13460 else
13462 decl_specs->std_attributes
13463 = attr_chainon (decl_specs->std_attributes, attrs);
13464 if (decl_specs->locations[ds_std_attribute] == 0)
13465 decl_specs->locations[ds_std_attribute] = token->location;
13467 continue;
13471 decl_specs->attributes
13472 = attr_chainon (decl_specs->attributes, attrs);
13473 if (decl_specs->locations[ds_attribute] == 0)
13474 decl_specs->locations[ds_attribute] = token->location;
13475 continue;
13477 /* Assume we will find a decl-specifier keyword. */
13478 found_decl_spec = true;
13479 /* If the next token is an appropriate keyword, we can simply
13480 add it to the list. */
13481 switch (token->keyword)
13483 /* decl-specifier:
13484 friend
13485 constexpr */
13486 case RID_FRIEND:
13487 if (!at_class_scope_p ())
13489 gcc_rich_location richloc (token->location);
13490 richloc.add_fixit_remove ();
13491 error_at (&richloc, "%<friend%> used outside of class");
13492 cp_lexer_purge_token (parser->lexer);
13494 else
13496 ds = ds_friend;
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser->lexer);
13500 break;
13502 case RID_CONSTEXPR:
13503 ds = ds_constexpr;
13504 cp_lexer_consume_token (parser->lexer);
13505 break;
13507 case RID_CONCEPT:
13508 ds = ds_concept;
13509 cp_lexer_consume_token (parser->lexer);
13510 break;
13512 /* function-specifier:
13513 inline
13514 virtual
13515 explicit */
13516 case RID_INLINE:
13517 case RID_VIRTUAL:
13518 case RID_EXPLICIT:
13519 cp_parser_function_specifier_opt (parser, decl_specs);
13520 break;
13522 /* decl-specifier:
13523 typedef */
13524 case RID_TYPEDEF:
13525 ds = ds_typedef;
13526 /* Consume the token. */
13527 cp_lexer_consume_token (parser->lexer);
13528 /* A constructor declarator cannot appear in a typedef. */
13529 constructor_possible_p = false;
13530 /* The "typedef" keyword can only occur in a declaration; we
13531 may as well commit at this point. */
13532 cp_parser_commit_to_tentative_parse (parser);
13534 if (decl_specs->storage_class != sc_none)
13535 decl_specs->conflicting_specifiers_p = true;
13536 break;
13538 /* storage-class-specifier:
13539 auto
13540 register
13541 static
13542 extern
13543 mutable
13545 GNU Extension:
13546 thread */
13547 case RID_AUTO:
13548 if (cxx_dialect == cxx98)
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser->lexer);
13553 /* Complain about `auto' as a storage specifier, if
13554 we're complaining about C++0x compatibility. */
13555 gcc_rich_location richloc (token->location);
13556 richloc.add_fixit_remove ();
13557 warning_at (&richloc, OPT_Wc__11_compat,
13558 "%<auto%> changes meaning in C++11; "
13559 "please remove it");
13561 /* Set the storage class anyway. */
13562 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13563 token);
13565 else
13566 /* C++0x auto type-specifier. */
13567 found_decl_spec = false;
13568 break;
13570 case RID_REGISTER:
13571 case RID_STATIC:
13572 case RID_EXTERN:
13573 case RID_MUTABLE:
13574 /* Consume the token. */
13575 cp_lexer_consume_token (parser->lexer);
13576 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13577 token);
13578 break;
13579 case RID_THREAD:
13580 /* Consume the token. */
13581 ds = ds_thread;
13582 cp_lexer_consume_token (parser->lexer);
13583 break;
13585 default:
13586 /* We did not yet find a decl-specifier yet. */
13587 found_decl_spec = false;
13588 break;
13591 if (found_decl_spec
13592 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13593 && token->keyword != RID_CONSTEXPR)
13594 error ("decl-specifier invalid in condition");
13596 if (found_decl_spec
13597 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13598 && token->keyword != RID_MUTABLE
13599 && token->keyword != RID_CONSTEXPR)
13600 error_at (token->location, "%qD invalid in lambda",
13601 ridpointers[token->keyword]);
13603 if (ds != ds_last)
13604 set_and_check_decl_spec_loc (decl_specs, ds, token);
13606 /* Constructors are a special case. The `S' in `S()' is not a
13607 decl-specifier; it is the beginning of the declarator. */
13608 constructor_p
13609 = (!found_decl_spec
13610 && constructor_possible_p
13611 && (cp_parser_constructor_declarator_p
13612 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13614 /* If we don't have a DECL_SPEC yet, then we must be looking at
13615 a type-specifier. */
13616 if (!found_decl_spec && !constructor_p)
13618 int decl_spec_declares_class_or_enum;
13619 bool is_cv_qualifier;
13620 tree type_spec;
13622 type_spec
13623 = cp_parser_type_specifier (parser, flags,
13624 decl_specs,
13625 /*is_declaration=*/true,
13626 &decl_spec_declares_class_or_enum,
13627 &is_cv_qualifier);
13628 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13630 /* If this type-specifier referenced a user-defined type
13631 (a typedef, class-name, etc.), then we can't allow any
13632 more such type-specifiers henceforth.
13634 [dcl.spec]
13636 The longest sequence of decl-specifiers that could
13637 possibly be a type name is taken as the
13638 decl-specifier-seq of a declaration. The sequence shall
13639 be self-consistent as described below.
13641 [dcl.type]
13643 As a general rule, at most one type-specifier is allowed
13644 in the complete decl-specifier-seq of a declaration. The
13645 only exceptions are the following:
13647 -- const or volatile can be combined with any other
13648 type-specifier.
13650 -- signed or unsigned can be combined with char, long,
13651 short, or int.
13653 -- ..
13655 Example:
13657 typedef char* Pc;
13658 void g (const int Pc);
13660 Here, Pc is *not* part of the decl-specifier seq; it's
13661 the declarator. Therefore, once we see a type-specifier
13662 (other than a cv-qualifier), we forbid any additional
13663 user-defined types. We *do* still allow things like `int
13664 int' to be considered a decl-specifier-seq, and issue the
13665 error message later. */
13666 if (type_spec && !is_cv_qualifier)
13667 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13668 /* A constructor declarator cannot follow a type-specifier. */
13669 if (type_spec)
13671 constructor_possible_p = false;
13672 found_decl_spec = true;
13673 if (!is_cv_qualifier)
13674 decl_specs->any_type_specifiers_p = true;
13678 /* If we still do not have a DECL_SPEC, then there are no more
13679 decl-specifiers. */
13680 if (!found_decl_spec)
13681 break;
13683 decl_specs->any_specifiers_p = true;
13684 /* After we see one decl-specifier, further decl-specifiers are
13685 always optional. */
13686 flags |= CP_PARSER_FLAGS_OPTIONAL;
13689 /* Don't allow a friend specifier with a class definition. */
13690 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13691 && (*declares_class_or_enum & 2))
13692 error_at (decl_specs->locations[ds_friend],
13693 "class definition may not be declared a friend");
13696 /* Parse an (optional) storage-class-specifier.
13698 storage-class-specifier:
13699 auto
13700 register
13701 static
13702 extern
13703 mutable
13705 GNU Extension:
13707 storage-class-specifier:
13708 thread
13710 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13712 static tree
13713 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13715 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13717 case RID_AUTO:
13718 if (cxx_dialect != cxx98)
13719 return NULL_TREE;
13720 /* Fall through for C++98. */
13721 gcc_fallthrough ();
13723 case RID_REGISTER:
13724 case RID_STATIC:
13725 case RID_EXTERN:
13726 case RID_MUTABLE:
13727 case RID_THREAD:
13728 /* Consume the token. */
13729 return cp_lexer_consume_token (parser->lexer)->u.value;
13731 default:
13732 return NULL_TREE;
13736 /* Parse an (optional) function-specifier.
13738 function-specifier:
13739 inline
13740 virtual
13741 explicit
13743 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13744 Updates DECL_SPECS, if it is non-NULL. */
13746 static tree
13747 cp_parser_function_specifier_opt (cp_parser* parser,
13748 cp_decl_specifier_seq *decl_specs)
13750 cp_token *token = cp_lexer_peek_token (parser->lexer);
13751 switch (token->keyword)
13753 case RID_INLINE:
13754 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13755 break;
13757 case RID_VIRTUAL:
13758 /* 14.5.2.3 [temp.mem]
13760 A member function template shall not be virtual. */
13761 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13762 && current_class_type)
13763 error_at (token->location, "templates may not be %<virtual%>");
13764 else
13765 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13766 break;
13768 case RID_EXPLICIT:
13769 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13770 break;
13772 default:
13773 return NULL_TREE;
13776 /* Consume the token. */
13777 return cp_lexer_consume_token (parser->lexer)->u.value;
13780 /* Parse a linkage-specification.
13782 linkage-specification:
13783 extern string-literal { declaration-seq [opt] }
13784 extern string-literal declaration */
13786 static void
13787 cp_parser_linkage_specification (cp_parser* parser)
13789 tree linkage;
13791 /* Look for the `extern' keyword. */
13792 cp_token *extern_token
13793 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13795 /* Look for the string-literal. */
13796 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13797 linkage = cp_parser_string_literal (parser, false, false);
13799 /* Transform the literal into an identifier. If the literal is a
13800 wide-character string, or contains embedded NULs, then we can't
13801 handle it as the user wants. */
13802 if (strlen (TREE_STRING_POINTER (linkage))
13803 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13805 cp_parser_error (parser, "invalid linkage-specification");
13806 /* Assume C++ linkage. */
13807 linkage = lang_name_cplusplus;
13809 else
13810 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13812 /* We're now using the new linkage. */
13813 push_lang_context (linkage);
13815 /* Preserve the location of the the innermost linkage specification,
13816 tracking the locations of nested specifications via a local. */
13817 location_t saved_location
13818 = parser->innermost_linkage_specification_location;
13819 /* Construct a location ranging from the start of the "extern" to
13820 the end of the string-literal, with the caret at the start, e.g.:
13821 extern "C" {
13822 ^~~~~~~~~~
13824 parser->innermost_linkage_specification_location
13825 = make_location (extern_token->location,
13826 extern_token->location,
13827 get_finish (string_token->location));
13829 /* If the next token is a `{', then we're using the first
13830 production. */
13831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13833 cp_ensure_no_omp_declare_simd (parser);
13834 cp_ensure_no_oacc_routine (parser);
13836 /* Consume the `{' token. */
13837 matching_braces braces;
13838 braces.consume_open (parser)->location;
13839 /* Parse the declarations. */
13840 cp_parser_declaration_seq_opt (parser);
13841 /* Look for the closing `}'. */
13842 braces.require_close (parser);
13844 /* Otherwise, there's just one declaration. */
13845 else
13847 bool saved_in_unbraced_linkage_specification_p;
13849 saved_in_unbraced_linkage_specification_p
13850 = parser->in_unbraced_linkage_specification_p;
13851 parser->in_unbraced_linkage_specification_p = true;
13852 cp_parser_declaration (parser);
13853 parser->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p;
13857 /* We're done with the linkage-specification. */
13858 pop_lang_context ();
13860 /* Restore location of parent linkage specification, if any. */
13861 parser->innermost_linkage_specification_location = saved_location;
13864 /* Parse a static_assert-declaration.
13866 static_assert-declaration:
13867 static_assert ( constant-expression , string-literal ) ;
13868 static_assert ( constant-expression ) ; (C++17)
13870 If MEMBER_P, this static_assert is a class member. */
13872 static void
13873 cp_parser_static_assert(cp_parser *parser, bool member_p)
13875 cp_expr condition;
13876 location_t token_loc;
13877 tree message;
13878 bool dummy;
13880 /* Peek at the `static_assert' token so we can keep track of exactly
13881 where the static assertion started. */
13882 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13884 /* Look for the `static_assert' keyword. */
13885 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13886 RT_STATIC_ASSERT))
13887 return;
13889 /* We know we are in a static assertion; commit to any tentative
13890 parse. */
13891 if (cp_parser_parsing_tentatively (parser))
13892 cp_parser_commit_to_tentative_parse (parser);
13894 /* Parse the `(' starting the static assertion condition. */
13895 matching_parens parens;
13896 parens.require_open (parser);
13898 /* Parse the constant-expression. Allow a non-constant expression
13899 here in order to give better diagnostics in finish_static_assert. */
13900 condition =
13901 cp_parser_constant_expression (parser,
13902 /*allow_non_constant_p=*/true,
13903 /*non_constant_p=*/&dummy);
13905 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13907 if (cxx_dialect < cxx17)
13908 pedwarn (input_location, OPT_Wpedantic,
13909 "static_assert without a message "
13910 "only available with -std=c++17 or -std=gnu++17");
13911 /* Eat the ')' */
13912 cp_lexer_consume_token (parser->lexer);
13913 message = build_string (1, "");
13914 TREE_TYPE (message) = char_array_type_node;
13915 fix_string_type (message);
13917 else
13919 /* Parse the separating `,'. */
13920 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13922 /* Parse the string-literal message. */
13923 message = cp_parser_string_literal (parser,
13924 /*translate=*/false,
13925 /*wide_ok=*/true);
13927 /* A `)' completes the static assertion. */
13928 if (!parens.require_close (parser))
13929 cp_parser_skip_to_closing_parenthesis (parser,
13930 /*recovering=*/true,
13931 /*or_comma=*/false,
13932 /*consume_paren=*/true);
13935 /* A semicolon terminates the declaration. */
13936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13938 /* Get the location for the static assertion. Use that of the
13939 condition if available, otherwise, use that of the "static_assert"
13940 token. */
13941 location_t assert_loc = condition.get_location ();
13942 if (assert_loc == UNKNOWN_LOCATION)
13943 assert_loc = token_loc;
13945 /* Complete the static assertion, which may mean either processing
13946 the static assert now or saving it for template instantiation. */
13947 finish_static_assert (condition, message, assert_loc, member_p);
13950 /* Parse the expression in decltype ( expression ). */
13952 static tree
13953 cp_parser_decltype_expr (cp_parser *parser,
13954 bool &id_expression_or_member_access_p)
13956 cp_token *id_expr_start_token;
13957 tree expr;
13959 /* Since we're going to preserve any side-effects from this parse, set up a
13960 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13961 in the expression. */
13962 tentative_firewall firewall (parser);
13964 /* First, try parsing an id-expression. */
13965 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13966 cp_parser_parse_tentatively (parser);
13967 expr = cp_parser_id_expression (parser,
13968 /*template_keyword_p=*/false,
13969 /*check_dependency_p=*/true,
13970 /*template_p=*/NULL,
13971 /*declarator_p=*/false,
13972 /*optional_p=*/false);
13974 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13976 bool non_integral_constant_expression_p = false;
13977 tree id_expression = expr;
13978 cp_id_kind idk;
13979 const char *error_msg;
13981 if (identifier_p (expr))
13982 /* Lookup the name we got back from the id-expression. */
13983 expr = cp_parser_lookup_name_simple (parser, expr,
13984 id_expr_start_token->location);
13986 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13987 /* A template without args is not a complete id-expression. */
13988 expr = error_mark_node;
13990 if (expr
13991 && expr != error_mark_node
13992 && TREE_CODE (expr) != TYPE_DECL
13993 && (TREE_CODE (expr) != BIT_NOT_EXPR
13994 || !TYPE_P (TREE_OPERAND (expr, 0)))
13995 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13997 /* Complete lookup of the id-expression. */
13998 expr = (finish_id_expression
13999 (id_expression, expr, parser->scope, &idk,
14000 /*integral_constant_expression_p=*/false,
14001 /*allow_non_integral_constant_expression_p=*/true,
14002 &non_integral_constant_expression_p,
14003 /*template_p=*/false,
14004 /*done=*/true,
14005 /*address_p=*/false,
14006 /*template_arg_p=*/false,
14007 &error_msg,
14008 id_expr_start_token->location));
14010 if (expr == error_mark_node)
14011 /* We found an id-expression, but it was something that we
14012 should not have found. This is an error, not something
14013 we can recover from, so note that we found an
14014 id-expression and we'll recover as gracefully as
14015 possible. */
14016 id_expression_or_member_access_p = true;
14019 if (expr
14020 && expr != error_mark_node
14021 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14022 /* We have an id-expression. */
14023 id_expression_or_member_access_p = true;
14026 if (!id_expression_or_member_access_p)
14028 /* Abort the id-expression parse. */
14029 cp_parser_abort_tentative_parse (parser);
14031 /* Parsing tentatively, again. */
14032 cp_parser_parse_tentatively (parser);
14034 /* Parse a class member access. */
14035 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14036 /*cast_p=*/false, /*decltype*/true,
14037 /*member_access_only_p=*/true, NULL);
14039 if (expr
14040 && expr != error_mark_node
14041 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14042 /* We have an id-expression. */
14043 id_expression_or_member_access_p = true;
14046 if (id_expression_or_member_access_p)
14047 /* We have parsed the complete id-expression or member access. */
14048 cp_parser_parse_definitely (parser);
14049 else
14051 /* Abort our attempt to parse an id-expression or member access
14052 expression. */
14053 cp_parser_abort_tentative_parse (parser);
14055 /* Commit to the tentative_firewall so we get syntax errors. */
14056 cp_parser_commit_to_tentative_parse (parser);
14058 /* Parse a full expression. */
14059 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14060 /*decltype_p=*/true);
14063 return expr;
14066 /* Parse a `decltype' type. Returns the type.
14068 simple-type-specifier:
14069 decltype ( expression )
14070 C++14 proposal:
14071 decltype ( auto ) */
14073 static tree
14074 cp_parser_decltype (cp_parser *parser)
14076 bool id_expression_or_member_access_p = false;
14077 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14079 if (start_token->type == CPP_DECLTYPE)
14081 /* Already parsed. */
14082 cp_lexer_consume_token (parser->lexer);
14083 return saved_checks_value (start_token->u.tree_check_value);
14086 /* Look for the `decltype' token. */
14087 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14088 return error_mark_node;
14090 /* Parse the opening `('. */
14091 matching_parens parens;
14092 if (!parens.require_open (parser))
14093 return error_mark_node;
14095 push_deferring_access_checks (dk_deferred);
14097 tree expr = NULL_TREE;
14099 if (cxx_dialect >= cxx14
14100 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14101 /* decltype (auto) */
14102 cp_lexer_consume_token (parser->lexer);
14103 else
14105 /* decltype (expression) */
14107 /* Types cannot be defined in a `decltype' expression. Save away the
14108 old message and set the new one. */
14109 const char *saved_message = parser->type_definition_forbidden_message;
14110 parser->type_definition_forbidden_message
14111 = G_("types may not be defined in %<decltype%> expressions");
14113 /* The restrictions on constant-expressions do not apply inside
14114 decltype expressions. */
14115 bool saved_integral_constant_expression_p
14116 = parser->integral_constant_expression_p;
14117 bool saved_non_integral_constant_expression_p
14118 = parser->non_integral_constant_expression_p;
14119 parser->integral_constant_expression_p = false;
14121 /* Within a parenthesized expression, a `>' token is always
14122 the greater-than operator. */
14123 bool saved_greater_than_is_operator_p
14124 = parser->greater_than_is_operator_p;
14125 parser->greater_than_is_operator_p = true;
14127 /* Do not actually evaluate the expression. */
14128 ++cp_unevaluated_operand;
14130 /* Do not warn about problems with the expression. */
14131 ++c_inhibit_evaluation_warnings;
14133 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14135 /* Go back to evaluating expressions. */
14136 --cp_unevaluated_operand;
14137 --c_inhibit_evaluation_warnings;
14139 /* The `>' token might be the end of a template-id or
14140 template-parameter-list now. */
14141 parser->greater_than_is_operator_p
14142 = saved_greater_than_is_operator_p;
14144 /* Restore the old message and the integral constant expression
14145 flags. */
14146 parser->type_definition_forbidden_message = saved_message;
14147 parser->integral_constant_expression_p
14148 = saved_integral_constant_expression_p;
14149 parser->non_integral_constant_expression_p
14150 = saved_non_integral_constant_expression_p;
14153 /* Parse to the closing `)'. */
14154 if (!parens.require_close (parser))
14156 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14157 /*consume_paren=*/true);
14158 pop_deferring_access_checks ();
14159 return error_mark_node;
14162 if (!expr)
14164 /* Build auto. */
14165 expr = make_decltype_auto ();
14166 AUTO_IS_DECLTYPE (expr) = true;
14168 else
14169 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14170 tf_warning_or_error);
14172 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14173 it again. */
14174 start_token->type = CPP_DECLTYPE;
14175 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14176 start_token->u.tree_check_value->value = expr;
14177 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14178 start_token->keyword = RID_MAX;
14179 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14181 pop_to_parent_deferring_access_checks ();
14183 return expr;
14186 /* Special member functions [gram.special] */
14188 /* Parse a conversion-function-id.
14190 conversion-function-id:
14191 operator conversion-type-id
14193 Returns an IDENTIFIER_NODE representing the operator. */
14195 static tree
14196 cp_parser_conversion_function_id (cp_parser* parser)
14198 tree type;
14199 tree saved_scope;
14200 tree saved_qualifying_scope;
14201 tree saved_object_scope;
14202 tree pushed_scope = NULL_TREE;
14204 /* Look for the `operator' token. */
14205 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14206 return error_mark_node;
14207 /* When we parse the conversion-type-id, the current scope will be
14208 reset. However, we need that information in able to look up the
14209 conversion function later, so we save it here. */
14210 saved_scope = parser->scope;
14211 saved_qualifying_scope = parser->qualifying_scope;
14212 saved_object_scope = parser->object_scope;
14213 /* We must enter the scope of the class so that the names of
14214 entities declared within the class are available in the
14215 conversion-type-id. For example, consider:
14217 struct S {
14218 typedef int I;
14219 operator I();
14222 S::operator I() { ... }
14224 In order to see that `I' is a type-name in the definition, we
14225 must be in the scope of `S'. */
14226 if (saved_scope)
14227 pushed_scope = push_scope (saved_scope);
14228 /* Parse the conversion-type-id. */
14229 type = cp_parser_conversion_type_id (parser);
14230 /* Leave the scope of the class, if any. */
14231 if (pushed_scope)
14232 pop_scope (pushed_scope);
14233 /* Restore the saved scope. */
14234 parser->scope = saved_scope;
14235 parser->qualifying_scope = saved_qualifying_scope;
14236 parser->object_scope = saved_object_scope;
14237 /* If the TYPE is invalid, indicate failure. */
14238 if (type == error_mark_node)
14239 return error_mark_node;
14240 return make_conv_op_name (type);
14243 /* Parse a conversion-type-id:
14245 conversion-type-id:
14246 type-specifier-seq conversion-declarator [opt]
14248 Returns the TYPE specified. */
14250 static tree
14251 cp_parser_conversion_type_id (cp_parser* parser)
14253 tree attributes;
14254 cp_decl_specifier_seq type_specifiers;
14255 cp_declarator *declarator;
14256 tree type_specified;
14257 const char *saved_message;
14259 /* Parse the attributes. */
14260 attributes = cp_parser_attributes_opt (parser);
14262 saved_message = parser->type_definition_forbidden_message;
14263 parser->type_definition_forbidden_message
14264 = G_("types may not be defined in a conversion-type-id");
14266 /* Parse the type-specifiers. */
14267 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14268 /*is_trailing_return=*/false,
14269 &type_specifiers);
14271 parser->type_definition_forbidden_message = saved_message;
14273 /* If that didn't work, stop. */
14274 if (type_specifiers.type == error_mark_node)
14275 return error_mark_node;
14276 /* Parse the conversion-declarator. */
14277 declarator = cp_parser_conversion_declarator_opt (parser);
14279 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14280 /*initialized=*/0, &attributes);
14281 if (attributes)
14282 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14284 /* Don't give this error when parsing tentatively. This happens to
14285 work because we always parse this definitively once. */
14286 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14287 && type_uses_auto (type_specified))
14289 if (cxx_dialect < cxx14)
14291 error ("invalid use of %<auto%> in conversion operator");
14292 return error_mark_node;
14294 else if (template_parm_scope_p ())
14295 warning (0, "use of %<auto%> in member template "
14296 "conversion operator can never be deduced");
14299 return type_specified;
14302 /* Parse an (optional) conversion-declarator.
14304 conversion-declarator:
14305 ptr-operator conversion-declarator [opt]
14309 static cp_declarator *
14310 cp_parser_conversion_declarator_opt (cp_parser* parser)
14312 enum tree_code code;
14313 tree class_type, std_attributes = NULL_TREE;
14314 cp_cv_quals cv_quals;
14316 /* We don't know if there's a ptr-operator next, or not. */
14317 cp_parser_parse_tentatively (parser);
14318 /* Try the ptr-operator. */
14319 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14320 &std_attributes);
14321 /* If it worked, look for more conversion-declarators. */
14322 if (cp_parser_parse_definitely (parser))
14324 cp_declarator *declarator;
14326 /* Parse another optional declarator. */
14327 declarator = cp_parser_conversion_declarator_opt (parser);
14329 declarator = cp_parser_make_indirect_declarator
14330 (code, class_type, cv_quals, declarator, std_attributes);
14332 return declarator;
14335 return NULL;
14338 /* Parse an (optional) ctor-initializer.
14340 ctor-initializer:
14341 : mem-initializer-list */
14343 static void
14344 cp_parser_ctor_initializer_opt (cp_parser* parser)
14346 /* If the next token is not a `:', then there is no
14347 ctor-initializer. */
14348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14350 /* Do default initialization of any bases and members. */
14351 if (DECL_CONSTRUCTOR_P (current_function_decl))
14352 finish_mem_initializers (NULL_TREE);
14353 return;
14356 /* Consume the `:' token. */
14357 cp_lexer_consume_token (parser->lexer);
14358 /* And the mem-initializer-list. */
14359 cp_parser_mem_initializer_list (parser);
14362 /* Parse a mem-initializer-list.
14364 mem-initializer-list:
14365 mem-initializer ... [opt]
14366 mem-initializer ... [opt] , mem-initializer-list */
14368 static void
14369 cp_parser_mem_initializer_list (cp_parser* parser)
14371 tree mem_initializer_list = NULL_TREE;
14372 tree target_ctor = error_mark_node;
14373 cp_token *token = cp_lexer_peek_token (parser->lexer);
14375 /* Let the semantic analysis code know that we are starting the
14376 mem-initializer-list. */
14377 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14378 error_at (token->location,
14379 "only constructors take member initializers");
14381 /* Loop through the list. */
14382 while (true)
14384 tree mem_initializer;
14386 token = cp_lexer_peek_token (parser->lexer);
14387 /* Parse the mem-initializer. */
14388 mem_initializer = cp_parser_mem_initializer (parser);
14389 /* If the next token is a `...', we're expanding member initializers. */
14390 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14391 if (ellipsis
14392 || (mem_initializer != error_mark_node
14393 && check_for_bare_parameter_packs (TREE_PURPOSE
14394 (mem_initializer))))
14396 /* Consume the `...'. */
14397 if (ellipsis)
14398 cp_lexer_consume_token (parser->lexer);
14400 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14401 can be expanded but members cannot. */
14402 if (mem_initializer != error_mark_node
14403 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14405 error_at (token->location,
14406 "cannot expand initializer for member %qD",
14407 TREE_PURPOSE (mem_initializer));
14408 mem_initializer = error_mark_node;
14411 /* Construct the pack expansion type. */
14412 if (mem_initializer != error_mark_node)
14413 mem_initializer = make_pack_expansion (mem_initializer);
14415 if (target_ctor != error_mark_node
14416 && mem_initializer != error_mark_node)
14418 error ("mem-initializer for %qD follows constructor delegation",
14419 TREE_PURPOSE (mem_initializer));
14420 mem_initializer = error_mark_node;
14422 /* Look for a target constructor. */
14423 if (mem_initializer != error_mark_node
14424 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14425 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14427 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14428 if (mem_initializer_list)
14430 error ("constructor delegation follows mem-initializer for %qD",
14431 TREE_PURPOSE (mem_initializer_list));
14432 mem_initializer = error_mark_node;
14434 target_ctor = mem_initializer;
14436 /* Add it to the list, unless it was erroneous. */
14437 if (mem_initializer != error_mark_node)
14439 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14440 mem_initializer_list = mem_initializer;
14442 /* If the next token is not a `,', we're done. */
14443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14444 break;
14445 /* Consume the `,' token. */
14446 cp_lexer_consume_token (parser->lexer);
14449 /* Perform semantic analysis. */
14450 if (DECL_CONSTRUCTOR_P (current_function_decl))
14451 finish_mem_initializers (mem_initializer_list);
14454 /* Parse a mem-initializer.
14456 mem-initializer:
14457 mem-initializer-id ( expression-list [opt] )
14458 mem-initializer-id braced-init-list
14460 GNU extension:
14462 mem-initializer:
14463 ( expression-list [opt] )
14465 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14466 class) or FIELD_DECL (for a non-static data member) to initialize;
14467 the TREE_VALUE is the expression-list. An empty initialization
14468 list is represented by void_list_node. */
14470 static tree
14471 cp_parser_mem_initializer (cp_parser* parser)
14473 tree mem_initializer_id;
14474 tree expression_list;
14475 tree member;
14476 cp_token *token = cp_lexer_peek_token (parser->lexer);
14478 /* Find out what is being initialized. */
14479 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14481 permerror (token->location,
14482 "anachronistic old-style base class initializer");
14483 mem_initializer_id = NULL_TREE;
14485 else
14487 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14488 if (mem_initializer_id == error_mark_node)
14489 return mem_initializer_id;
14491 member = expand_member_init (mem_initializer_id);
14492 if (member && !DECL_P (member))
14493 in_base_initializer = 1;
14495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14497 bool expr_non_constant_p;
14498 cp_lexer_set_source_position (parser->lexer);
14499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14500 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14501 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14502 expression_list = build_tree_list (NULL_TREE, expression_list);
14504 else
14506 vec<tree, va_gc> *vec;
14507 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14508 /*cast_p=*/false,
14509 /*allow_expansion_p=*/true,
14510 /*non_constant_p=*/NULL);
14511 if (vec == NULL)
14512 return error_mark_node;
14513 expression_list = build_tree_list_vec (vec);
14514 release_tree_vector (vec);
14517 if (expression_list == error_mark_node)
14518 return error_mark_node;
14519 if (!expression_list)
14520 expression_list = void_type_node;
14522 in_base_initializer = 0;
14524 return member ? build_tree_list (member, expression_list) : error_mark_node;
14527 /* Parse a mem-initializer-id.
14529 mem-initializer-id:
14530 :: [opt] nested-name-specifier [opt] class-name
14531 decltype-specifier (C++11)
14532 identifier
14534 Returns a TYPE indicating the class to be initialized for the first
14535 production (and the second in C++11). Returns an IDENTIFIER_NODE
14536 indicating the data member to be initialized for the last production. */
14538 static tree
14539 cp_parser_mem_initializer_id (cp_parser* parser)
14541 bool global_scope_p;
14542 bool nested_name_specifier_p;
14543 bool template_p = false;
14544 tree id;
14546 cp_token *token = cp_lexer_peek_token (parser->lexer);
14548 /* `typename' is not allowed in this context ([temp.res]). */
14549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14551 error_at (token->location,
14552 "keyword %<typename%> not allowed in this context (a qualified "
14553 "member initializer is implicitly a type)");
14554 cp_lexer_consume_token (parser->lexer);
14556 /* Look for the optional `::' operator. */
14557 global_scope_p
14558 = (cp_parser_global_scope_opt (parser,
14559 /*current_scope_valid_p=*/false)
14560 != NULL_TREE);
14561 /* Look for the optional nested-name-specifier. The simplest way to
14562 implement:
14564 [temp.res]
14566 The keyword `typename' is not permitted in a base-specifier or
14567 mem-initializer; in these contexts a qualified name that
14568 depends on a template-parameter is implicitly assumed to be a
14569 type name.
14571 is to assume that we have seen the `typename' keyword at this
14572 point. */
14573 nested_name_specifier_p
14574 = (cp_parser_nested_name_specifier_opt (parser,
14575 /*typename_keyword_p=*/true,
14576 /*check_dependency_p=*/true,
14577 /*type_p=*/true,
14578 /*is_declaration=*/true)
14579 != NULL_TREE);
14580 if (nested_name_specifier_p)
14581 template_p = cp_parser_optional_template_keyword (parser);
14582 /* If there is a `::' operator or a nested-name-specifier, then we
14583 are definitely looking for a class-name. */
14584 if (global_scope_p || nested_name_specifier_p)
14585 return cp_parser_class_name (parser,
14586 /*typename_keyword_p=*/true,
14587 /*template_keyword_p=*/template_p,
14588 typename_type,
14589 /*check_dependency_p=*/true,
14590 /*class_head_p=*/false,
14591 /*is_declaration=*/true);
14592 /* Otherwise, we could also be looking for an ordinary identifier. */
14593 cp_parser_parse_tentatively (parser);
14594 if (cp_lexer_next_token_is_decltype (parser->lexer))
14595 /* Try a decltype-specifier. */
14596 id = cp_parser_decltype (parser);
14597 else
14598 /* Otherwise, try a class-name. */
14599 id = cp_parser_class_name (parser,
14600 /*typename_keyword_p=*/true,
14601 /*template_keyword_p=*/false,
14602 none_type,
14603 /*check_dependency_p=*/true,
14604 /*class_head_p=*/false,
14605 /*is_declaration=*/true);
14606 /* If we found one, we're done. */
14607 if (cp_parser_parse_definitely (parser))
14608 return id;
14609 /* Otherwise, look for an ordinary identifier. */
14610 return cp_parser_identifier (parser);
14613 /* Overloading [gram.over] */
14615 /* Parse an operator-function-id.
14617 operator-function-id:
14618 operator operator
14620 Returns an IDENTIFIER_NODE for the operator which is a
14621 human-readable spelling of the identifier, e.g., `operator +'. */
14623 static cp_expr
14624 cp_parser_operator_function_id (cp_parser* parser)
14626 /* Look for the `operator' keyword. */
14627 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14628 return error_mark_node;
14629 /* And then the name of the operator itself. */
14630 return cp_parser_operator (parser);
14633 /* Return an identifier node for a user-defined literal operator.
14634 The suffix identifier is chained to the operator name identifier. */
14636 tree
14637 cp_literal_operator_id (const char* name)
14639 tree identifier;
14640 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14641 + strlen (name) + 10);
14642 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14643 identifier = get_identifier (buffer);
14645 return identifier;
14648 /* Parse an operator.
14650 operator:
14651 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14652 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14653 || ++ -- , ->* -> () []
14655 GNU Extensions:
14657 operator:
14658 <? >? <?= >?=
14660 Returns an IDENTIFIER_NODE for the operator which is a
14661 human-readable spelling of the identifier, e.g., `operator +'. */
14663 static cp_expr
14664 cp_parser_operator (cp_parser* parser)
14666 tree id = NULL_TREE;
14667 cp_token *token;
14668 bool utf8 = false;
14670 /* Peek at the next token. */
14671 token = cp_lexer_peek_token (parser->lexer);
14673 location_t start_loc = token->location;
14675 /* Figure out which operator we have. */
14676 enum tree_code op = ERROR_MARK;
14677 bool assop = false;
14678 bool consumed = false;
14679 switch (token->type)
14681 case CPP_KEYWORD:
14683 /* The keyword should be either `new' or `delete'. */
14684 if (token->keyword == RID_NEW)
14685 op = NEW_EXPR;
14686 else if (token->keyword == RID_DELETE)
14687 op = DELETE_EXPR;
14688 else
14689 break;
14691 /* Consume the `new' or `delete' token. */
14692 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14694 /* Peek at the next token. */
14695 token = cp_lexer_peek_token (parser->lexer);
14696 /* If it's a `[' token then this is the array variant of the
14697 operator. */
14698 if (token->type == CPP_OPEN_SQUARE)
14700 /* Consume the `[' token. */
14701 cp_lexer_consume_token (parser->lexer);
14702 /* Look for the `]' token. */
14703 if (cp_token *close_token
14704 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14705 end_loc = close_token->location;
14706 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14708 start_loc = make_location (start_loc, start_loc, end_loc);
14709 consumed = true;
14710 break;
14713 case CPP_PLUS:
14714 op = PLUS_EXPR;
14715 break;
14717 case CPP_MINUS:
14718 op = MINUS_EXPR;
14719 break;
14721 case CPP_MULT:
14722 op = MULT_EXPR;
14723 break;
14725 case CPP_DIV:
14726 op = TRUNC_DIV_EXPR;
14727 break;
14729 case CPP_MOD:
14730 op = TRUNC_MOD_EXPR;
14731 break;
14733 case CPP_XOR:
14734 op = BIT_XOR_EXPR;
14735 break;
14737 case CPP_AND:
14738 op = BIT_AND_EXPR;
14739 break;
14741 case CPP_OR:
14742 op = BIT_IOR_EXPR;
14743 break;
14745 case CPP_COMPL:
14746 op = BIT_NOT_EXPR;
14747 break;
14749 case CPP_NOT:
14750 op = TRUTH_NOT_EXPR;
14751 break;
14753 case CPP_EQ:
14754 assop = true;
14755 op = NOP_EXPR;
14756 break;
14758 case CPP_LESS:
14759 op = LT_EXPR;
14760 break;
14762 case CPP_GREATER:
14763 op = GT_EXPR;
14764 break;
14766 case CPP_PLUS_EQ:
14767 assop = true;
14768 op = PLUS_EXPR;
14769 break;
14771 case CPP_MINUS_EQ:
14772 assop = true;
14773 op = MINUS_EXPR;
14774 break;
14776 case CPP_MULT_EQ:
14777 assop = true;
14778 op = MULT_EXPR;
14779 break;
14781 case CPP_DIV_EQ:
14782 assop = true;
14783 op = TRUNC_DIV_EXPR;
14784 break;
14786 case CPP_MOD_EQ:
14787 assop = true;
14788 op = TRUNC_MOD_EXPR;
14789 break;
14791 case CPP_XOR_EQ:
14792 assop = true;
14793 op = BIT_XOR_EXPR;
14794 break;
14796 case CPP_AND_EQ:
14797 assop = true;
14798 op = BIT_AND_EXPR;
14799 break;
14801 case CPP_OR_EQ:
14802 assop = true;
14803 op = BIT_IOR_EXPR;
14804 break;
14806 case CPP_LSHIFT:
14807 op = LSHIFT_EXPR;
14808 break;
14810 case CPP_RSHIFT:
14811 op = RSHIFT_EXPR;
14812 break;
14814 case CPP_LSHIFT_EQ:
14815 assop = true;
14816 op = LSHIFT_EXPR;
14817 break;
14819 case CPP_RSHIFT_EQ:
14820 assop = true;
14821 op = RSHIFT_EXPR;
14822 break;
14824 case CPP_EQ_EQ:
14825 op = EQ_EXPR;
14826 break;
14828 case CPP_NOT_EQ:
14829 op = NE_EXPR;
14830 break;
14832 case CPP_LESS_EQ:
14833 op = LE_EXPR;
14834 break;
14836 case CPP_GREATER_EQ:
14837 op = GE_EXPR;
14838 break;
14840 case CPP_AND_AND:
14841 op = TRUTH_ANDIF_EXPR;
14842 break;
14844 case CPP_OR_OR:
14845 op = TRUTH_ORIF_EXPR;
14846 break;
14848 case CPP_PLUS_PLUS:
14849 op = POSTINCREMENT_EXPR;
14850 break;
14852 case CPP_MINUS_MINUS:
14853 op = PREDECREMENT_EXPR;
14854 break;
14856 case CPP_COMMA:
14857 op = COMPOUND_EXPR;
14858 break;
14860 case CPP_DEREF_STAR:
14861 op = MEMBER_REF;
14862 break;
14864 case CPP_DEREF:
14865 op = COMPONENT_REF;
14866 break;
14868 case CPP_OPEN_PAREN:
14870 /* Consume the `('. */
14871 matching_parens parens;
14872 parens.consume_open (parser);
14873 /* Look for the matching `)'. */
14874 parens.require_close (parser);
14875 op = CALL_EXPR;
14876 consumed = true;
14877 break;
14880 case CPP_OPEN_SQUARE:
14881 /* Consume the `['. */
14882 cp_lexer_consume_token (parser->lexer);
14883 /* Look for the matching `]'. */
14884 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14885 op = ARRAY_REF;
14886 consumed = true;
14887 break;
14889 case CPP_UTF8STRING:
14890 case CPP_UTF8STRING_USERDEF:
14891 utf8 = true;
14892 /* FALLTHRU */
14893 case CPP_STRING:
14894 case CPP_WSTRING:
14895 case CPP_STRING16:
14896 case CPP_STRING32:
14897 case CPP_STRING_USERDEF:
14898 case CPP_WSTRING_USERDEF:
14899 case CPP_STRING16_USERDEF:
14900 case CPP_STRING32_USERDEF:
14902 tree str, string_tree;
14903 int sz, len;
14905 if (cxx_dialect == cxx98)
14906 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14908 /* Consume the string. */
14909 str = cp_parser_string_literal (parser, /*translate=*/true,
14910 /*wide_ok=*/true, /*lookup_udlit=*/false);
14911 if (str == error_mark_node)
14912 return error_mark_node;
14913 else if (TREE_CODE (str) == USERDEF_LITERAL)
14915 string_tree = USERDEF_LITERAL_VALUE (str);
14916 id = USERDEF_LITERAL_SUFFIX_ID (str);
14918 else
14920 string_tree = str;
14921 /* Look for the suffix identifier. */
14922 token = cp_lexer_peek_token (parser->lexer);
14923 if (token->type == CPP_NAME)
14924 id = cp_parser_identifier (parser);
14925 else if (token->type == CPP_KEYWORD)
14927 error ("unexpected keyword;"
14928 " remove space between quotes and suffix identifier");
14929 return error_mark_node;
14931 else
14933 error ("expected suffix identifier");
14934 return error_mark_node;
14937 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14938 (TREE_TYPE (TREE_TYPE (string_tree))));
14939 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14940 if (len != 0)
14942 error ("expected empty string after %<operator%> keyword");
14943 return error_mark_node;
14945 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14946 != char_type_node)
14948 error ("invalid encoding prefix in literal operator");
14949 return error_mark_node;
14951 if (id != error_mark_node)
14953 const char *name = IDENTIFIER_POINTER (id);
14954 id = cp_literal_operator_id (name);
14956 return id;
14959 default:
14960 /* Anything else is an error. */
14961 break;
14964 /* If we have selected an identifier, we need to consume the
14965 operator token. */
14966 if (op != ERROR_MARK)
14968 id = ovl_op_identifier (assop, op);
14969 if (!consumed)
14970 cp_lexer_consume_token (parser->lexer);
14972 /* Otherwise, no valid operator name was present. */
14973 else
14975 cp_parser_error (parser, "expected operator");
14976 id = error_mark_node;
14979 return cp_expr (id, start_loc);
14982 /* Parse a template-declaration.
14984 template-declaration:
14985 export [opt] template < template-parameter-list > declaration
14987 If MEMBER_P is TRUE, this template-declaration occurs within a
14988 class-specifier.
14990 The grammar rule given by the standard isn't correct. What
14991 is really meant is:
14993 template-declaration:
14994 export [opt] template-parameter-list-seq
14995 decl-specifier-seq [opt] init-declarator [opt] ;
14996 export [opt] template-parameter-list-seq
14997 function-definition
14999 template-parameter-list-seq:
15000 template-parameter-list-seq [opt]
15001 template < template-parameter-list >
15003 Concept Extensions:
15005 template-parameter-list-seq:
15006 template < template-parameter-list > requires-clause [opt]
15008 requires-clause:
15009 requires logical-or-expression */
15011 static void
15012 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15014 /* Check for `export'. */
15015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15017 /* Consume the `export' token. */
15018 cp_lexer_consume_token (parser->lexer);
15019 /* Warn that we do not support `export'. */
15020 warning (0, "keyword %<export%> not implemented, and will be ignored");
15023 cp_parser_template_declaration_after_export (parser, member_p);
15026 /* Parse a template-parameter-list.
15028 template-parameter-list:
15029 template-parameter
15030 template-parameter-list , template-parameter
15032 Returns a TREE_LIST. Each node represents a template parameter.
15033 The nodes are connected via their TREE_CHAINs. */
15035 static tree
15036 cp_parser_template_parameter_list (cp_parser* parser)
15038 tree parameter_list = NULL_TREE;
15040 begin_template_parm_list ();
15042 /* The loop below parses the template parms. We first need to know
15043 the total number of template parms to be able to compute proper
15044 canonical types of each dependent type. So after the loop, when
15045 we know the total number of template parms,
15046 end_template_parm_list computes the proper canonical types and
15047 fixes up the dependent types accordingly. */
15048 while (true)
15050 tree parameter;
15051 bool is_non_type;
15052 bool is_parameter_pack;
15053 location_t parm_loc;
15055 /* Parse the template-parameter. */
15056 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15057 parameter = cp_parser_template_parameter (parser,
15058 &is_non_type,
15059 &is_parameter_pack);
15060 /* Add it to the list. */
15061 if (parameter != error_mark_node)
15062 parameter_list = process_template_parm (parameter_list,
15063 parm_loc,
15064 parameter,
15065 is_non_type,
15066 is_parameter_pack);
15067 else
15069 tree err_parm = build_tree_list (parameter, parameter);
15070 parameter_list = chainon (parameter_list, err_parm);
15073 /* If the next token is not a `,', we're done. */
15074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15075 break;
15076 /* Otherwise, consume the `,' token. */
15077 cp_lexer_consume_token (parser->lexer);
15080 return end_template_parm_list (parameter_list);
15083 /* Parse a introduction-list.
15085 introduction-list:
15086 introduced-parameter
15087 introduction-list , introduced-parameter
15089 introduced-parameter:
15090 ...[opt] identifier
15092 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15093 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15094 WILDCARD_DECL will also have DECL_NAME set and token location in
15095 DECL_SOURCE_LOCATION. */
15097 static tree
15098 cp_parser_introduction_list (cp_parser *parser)
15100 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15102 while (true)
15104 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15105 if (is_pack)
15106 cp_lexer_consume_token (parser->lexer);
15108 /* Build placeholder. */
15109 tree parm = build_nt (WILDCARD_DECL);
15110 DECL_SOURCE_LOCATION (parm)
15111 = cp_lexer_peek_token (parser->lexer)->location;
15112 DECL_NAME (parm) = cp_parser_identifier (parser);
15113 WILDCARD_PACK_P (parm) = is_pack;
15114 vec_safe_push (introduction_vec, parm);
15116 /* If the next token is not a `,', we're done. */
15117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15118 break;
15119 /* Otherwise, consume the `,' token. */
15120 cp_lexer_consume_token (parser->lexer);
15123 /* Convert the vec into a TREE_VEC. */
15124 tree introduction_list = make_tree_vec (introduction_vec->length ());
15125 unsigned int n;
15126 tree parm;
15127 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15128 TREE_VEC_ELT (introduction_list, n) = parm;
15130 release_tree_vector (introduction_vec);
15131 return introduction_list;
15134 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15135 is an abstract declarator. */
15137 static inline cp_declarator*
15138 get_id_declarator (cp_declarator *declarator)
15140 cp_declarator *d = declarator;
15141 while (d && d->kind != cdk_id)
15142 d = d->declarator;
15143 return d;
15146 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15147 is an abstract declarator. */
15149 static inline tree
15150 get_unqualified_id (cp_declarator *declarator)
15152 declarator = get_id_declarator (declarator);
15153 if (declarator)
15154 return declarator->u.id.unqualified_name;
15155 else
15156 return NULL_TREE;
15159 /* Returns true if DECL represents a constrained-parameter. */
15161 static inline bool
15162 is_constrained_parameter (tree decl)
15164 return (decl
15165 && TREE_CODE (decl) == TYPE_DECL
15166 && CONSTRAINED_PARM_CONCEPT (decl)
15167 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15170 /* Returns true if PARM declares a constrained-parameter. */
15172 static inline bool
15173 is_constrained_parameter (cp_parameter_declarator *parm)
15175 return is_constrained_parameter (parm->decl_specifiers.type);
15178 /* Check that the type parameter is only a declarator-id, and that its
15179 type is not cv-qualified. */
15181 bool
15182 cp_parser_check_constrained_type_parm (cp_parser *parser,
15183 cp_parameter_declarator *parm)
15185 if (!parm->declarator)
15186 return true;
15188 if (parm->declarator->kind != cdk_id)
15190 cp_parser_error (parser, "invalid constrained type parameter");
15191 return false;
15194 /* Don't allow cv-qualified type parameters. */
15195 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15196 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15198 cp_parser_error (parser, "cv-qualified type parameter");
15199 return false;
15202 return true;
15205 /* Finish parsing/processing a template type parameter and checking
15206 various restrictions. */
15208 static inline tree
15209 cp_parser_constrained_type_template_parm (cp_parser *parser,
15210 tree id,
15211 cp_parameter_declarator* parmdecl)
15213 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15214 return finish_template_type_parm (class_type_node, id);
15215 else
15216 return error_mark_node;
15219 static tree
15220 finish_constrained_template_template_parm (tree proto, tree id)
15222 /* FIXME: This should probably be copied, and we may need to adjust
15223 the template parameter depths. */
15224 tree saved_parms = current_template_parms;
15225 begin_template_parm_list ();
15226 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15227 end_template_parm_list ();
15229 tree parm = finish_template_template_parm (class_type_node, id);
15230 current_template_parms = saved_parms;
15232 return parm;
15235 /* Finish parsing/processing a template template parameter by borrowing
15236 the template parameter list from the prototype parameter. */
15238 static tree
15239 cp_parser_constrained_template_template_parm (cp_parser *parser,
15240 tree proto,
15241 tree id,
15242 cp_parameter_declarator *parmdecl)
15244 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15245 return error_mark_node;
15246 return finish_constrained_template_template_parm (proto, id);
15249 /* Create a new non-type template parameter from the given PARM
15250 declarator. */
15252 static tree
15253 constrained_non_type_template_parm (bool *is_non_type,
15254 cp_parameter_declarator *parm)
15256 *is_non_type = true;
15257 cp_declarator *decl = parm->declarator;
15258 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15259 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15260 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15263 /* Build a constrained template parameter based on the PARMDECL
15264 declarator. The type of PARMDECL is the constrained type, which
15265 refers to the prototype template parameter that ultimately
15266 specifies the type of the declared parameter. */
15268 static tree
15269 finish_constrained_parameter (cp_parser *parser,
15270 cp_parameter_declarator *parmdecl,
15271 bool *is_non_type,
15272 bool *is_parameter_pack)
15274 tree decl = parmdecl->decl_specifiers.type;
15275 tree id = get_unqualified_id (parmdecl->declarator);
15276 tree def = parmdecl->default_argument;
15277 tree proto = DECL_INITIAL (decl);
15279 /* A template parameter constrained by a variadic concept shall also
15280 be declared as a template parameter pack. */
15281 bool is_variadic = template_parameter_pack_p (proto);
15282 if (is_variadic && !*is_parameter_pack)
15283 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15285 /* Build the parameter. Return an error if the declarator was invalid. */
15286 tree parm;
15287 if (TREE_CODE (proto) == TYPE_DECL)
15288 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15289 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15290 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15291 parmdecl);
15292 else
15293 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15294 if (parm == error_mark_node)
15295 return error_mark_node;
15297 /* Finish the parameter decl and create a node attaching the
15298 default argument and constraint. */
15299 parm = build_tree_list (def, parm);
15300 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15302 return parm;
15305 /* Returns true if the parsed type actually represents the declaration
15306 of a type template-parameter. */
15308 static inline bool
15309 declares_constrained_type_template_parameter (tree type)
15311 return (is_constrained_parameter (type)
15312 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15316 /* Returns true if the parsed type actually represents the declaration of
15317 a template template-parameter. */
15319 static bool
15320 declares_constrained_template_template_parameter (tree type)
15322 return (is_constrained_parameter (type)
15323 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15326 /* Parse a default argument for a type template-parameter.
15327 Note that diagnostics are handled in cp_parser_template_parameter. */
15329 static tree
15330 cp_parser_default_type_template_argument (cp_parser *parser)
15332 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15334 /* Consume the `=' token. */
15335 cp_lexer_consume_token (parser->lexer);
15337 cp_token *token = cp_lexer_peek_token (parser->lexer);
15339 /* Parse the default-argument. */
15340 push_deferring_access_checks (dk_no_deferred);
15341 tree default_argument = cp_parser_type_id (parser);
15342 pop_deferring_access_checks ();
15344 if (flag_concepts && type_uses_auto (default_argument))
15346 error_at (token->location,
15347 "invalid use of %<auto%> in default template argument");
15348 return error_mark_node;
15351 return default_argument;
15354 /* Parse a default argument for a template template-parameter. */
15356 static tree
15357 cp_parser_default_template_template_argument (cp_parser *parser)
15359 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15361 bool is_template;
15363 /* Consume the `='. */
15364 cp_lexer_consume_token (parser->lexer);
15365 /* Parse the id-expression. */
15366 push_deferring_access_checks (dk_no_deferred);
15367 /* save token before parsing the id-expression, for error
15368 reporting */
15369 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15370 tree default_argument
15371 = cp_parser_id_expression (parser,
15372 /*template_keyword_p=*/false,
15373 /*check_dependency_p=*/true,
15374 /*template_p=*/&is_template,
15375 /*declarator_p=*/false,
15376 /*optional_p=*/false);
15377 if (TREE_CODE (default_argument) == TYPE_DECL)
15378 /* If the id-expression was a template-id that refers to
15379 a template-class, we already have the declaration here,
15380 so no further lookup is needed. */
15382 else
15383 /* Look up the name. */
15384 default_argument
15385 = cp_parser_lookup_name (parser, default_argument,
15386 none_type,
15387 /*is_template=*/is_template,
15388 /*is_namespace=*/false,
15389 /*check_dependency=*/true,
15390 /*ambiguous_decls=*/NULL,
15391 token->location);
15392 /* See if the default argument is valid. */
15393 default_argument = check_template_template_default_arg (default_argument);
15394 pop_deferring_access_checks ();
15395 return default_argument;
15398 /* Parse a template-parameter.
15400 template-parameter:
15401 type-parameter
15402 parameter-declaration
15404 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15405 the parameter. The TREE_PURPOSE is the default value, if any.
15406 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15407 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15408 set to true iff this parameter is a parameter pack. */
15410 static tree
15411 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15412 bool *is_parameter_pack)
15414 cp_token *token;
15415 cp_parameter_declarator *parameter_declarator;
15416 tree parm;
15418 /* Assume it is a type parameter or a template parameter. */
15419 *is_non_type = false;
15420 /* Assume it not a parameter pack. */
15421 *is_parameter_pack = false;
15422 /* Peek at the next token. */
15423 token = cp_lexer_peek_token (parser->lexer);
15424 /* If it is `template', we have a type-parameter. */
15425 if (token->keyword == RID_TEMPLATE)
15426 return cp_parser_type_parameter (parser, is_parameter_pack);
15427 /* If it is `class' or `typename' we do not know yet whether it is a
15428 type parameter or a non-type parameter. Consider:
15430 template <typename T, typename T::X X> ...
15434 template <class C, class D*> ...
15436 Here, the first parameter is a type parameter, and the second is
15437 a non-type parameter. We can tell by looking at the token after
15438 the identifier -- if it is a `,', `=', or `>' then we have a type
15439 parameter. */
15440 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15442 /* Peek at the token after `class' or `typename'. */
15443 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15444 /* If it's an ellipsis, we have a template type parameter
15445 pack. */
15446 if (token->type == CPP_ELLIPSIS)
15447 return cp_parser_type_parameter (parser, is_parameter_pack);
15448 /* If it's an identifier, skip it. */
15449 if (token->type == CPP_NAME)
15450 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15451 /* Now, see if the token looks like the end of a template
15452 parameter. */
15453 if (token->type == CPP_COMMA
15454 || token->type == CPP_EQ
15455 || token->type == CPP_GREATER)
15456 return cp_parser_type_parameter (parser, is_parameter_pack);
15459 /* Otherwise, it is a non-type parameter or a constrained parameter.
15461 [temp.param]
15463 When parsing a default template-argument for a non-type
15464 template-parameter, the first non-nested `>' is taken as the end
15465 of the template parameter-list rather than a greater-than
15466 operator. */
15467 parameter_declarator
15468 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15469 /*parenthesized_p=*/NULL);
15471 if (!parameter_declarator)
15472 return error_mark_node;
15474 /* If the parameter declaration is marked as a parameter pack, set
15475 *IS_PARAMETER_PACK to notify the caller. */
15476 if (parameter_declarator->template_parameter_pack_p)
15477 *is_parameter_pack = true;
15479 if (parameter_declarator->default_argument)
15481 /* Can happen in some cases of erroneous input (c++/34892). */
15482 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15483 /* Consume the `...' for better error recovery. */
15484 cp_lexer_consume_token (parser->lexer);
15487 // The parameter may have been constrained.
15488 if (is_constrained_parameter (parameter_declarator))
15489 return finish_constrained_parameter (parser,
15490 parameter_declarator,
15491 is_non_type,
15492 is_parameter_pack);
15494 // Now we're sure that the parameter is a non-type parameter.
15495 *is_non_type = true;
15497 parm = grokdeclarator (parameter_declarator->declarator,
15498 &parameter_declarator->decl_specifiers,
15499 TPARM, /*initialized=*/0,
15500 /*attrlist=*/NULL);
15501 if (parm == error_mark_node)
15502 return error_mark_node;
15504 return build_tree_list (parameter_declarator->default_argument, parm);
15507 /* Parse a type-parameter.
15509 type-parameter:
15510 class identifier [opt]
15511 class identifier [opt] = type-id
15512 typename identifier [opt]
15513 typename identifier [opt] = type-id
15514 template < template-parameter-list > class identifier [opt]
15515 template < template-parameter-list > class identifier [opt]
15516 = id-expression
15518 GNU Extension (variadic templates):
15520 type-parameter:
15521 class ... identifier [opt]
15522 typename ... identifier [opt]
15524 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15525 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15526 the declaration of the parameter.
15528 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15530 static tree
15531 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15533 cp_token *token;
15534 tree parameter;
15536 /* Look for a keyword to tell us what kind of parameter this is. */
15537 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15538 if (!token)
15539 return error_mark_node;
15541 switch (token->keyword)
15543 case RID_CLASS:
15544 case RID_TYPENAME:
15546 tree identifier;
15547 tree default_argument;
15549 /* If the next token is an ellipsis, we have a template
15550 argument pack. */
15551 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15553 /* Consume the `...' token. */
15554 cp_lexer_consume_token (parser->lexer);
15555 maybe_warn_variadic_templates ();
15557 *is_parameter_pack = true;
15560 /* If the next token is an identifier, then it names the
15561 parameter. */
15562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15563 identifier = cp_parser_identifier (parser);
15564 else
15565 identifier = NULL_TREE;
15567 /* Create the parameter. */
15568 parameter = finish_template_type_parm (class_type_node, identifier);
15570 /* If the next token is an `=', we have a default argument. */
15571 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15573 default_argument
15574 = cp_parser_default_type_template_argument (parser);
15576 /* Template parameter packs cannot have default
15577 arguments. */
15578 if (*is_parameter_pack)
15580 if (identifier)
15581 error_at (token->location,
15582 "template parameter pack %qD cannot have a "
15583 "default argument", identifier);
15584 else
15585 error_at (token->location,
15586 "template parameter packs cannot have "
15587 "default arguments");
15588 default_argument = NULL_TREE;
15590 else if (check_for_bare_parameter_packs (default_argument))
15591 default_argument = error_mark_node;
15593 else
15594 default_argument = NULL_TREE;
15596 /* Create the combined representation of the parameter and the
15597 default argument. */
15598 parameter = build_tree_list (default_argument, parameter);
15600 break;
15602 case RID_TEMPLATE:
15604 tree identifier;
15605 tree default_argument;
15607 /* Look for the `<'. */
15608 cp_parser_require (parser, CPP_LESS, RT_LESS);
15609 /* Parse the template-parameter-list. */
15610 cp_parser_template_parameter_list (parser);
15611 /* Look for the `>'. */
15612 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15614 // If template requirements are present, parse them.
15615 if (flag_concepts)
15617 tree reqs = get_shorthand_constraints (current_template_parms);
15618 if (tree r = cp_parser_requires_clause_opt (parser))
15619 reqs = conjoin_constraints (reqs, normalize_expression (r));
15620 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15623 /* Look for the `class' or 'typename' keywords. */
15624 cp_parser_type_parameter_key (parser);
15625 /* If the next token is an ellipsis, we have a template
15626 argument pack. */
15627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15629 /* Consume the `...' token. */
15630 cp_lexer_consume_token (parser->lexer);
15631 maybe_warn_variadic_templates ();
15633 *is_parameter_pack = true;
15635 /* If the next token is an `=', then there is a
15636 default-argument. If the next token is a `>', we are at
15637 the end of the parameter-list. If the next token is a `,',
15638 then we are at the end of this parameter. */
15639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15640 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15641 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15643 identifier = cp_parser_identifier (parser);
15644 /* Treat invalid names as if the parameter were nameless. */
15645 if (identifier == error_mark_node)
15646 identifier = NULL_TREE;
15648 else
15649 identifier = NULL_TREE;
15651 /* Create the template parameter. */
15652 parameter = finish_template_template_parm (class_type_node,
15653 identifier);
15655 /* If the next token is an `=', then there is a
15656 default-argument. */
15657 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15659 default_argument
15660 = cp_parser_default_template_template_argument (parser);
15662 /* Template parameter packs cannot have default
15663 arguments. */
15664 if (*is_parameter_pack)
15666 if (identifier)
15667 error_at (token->location,
15668 "template parameter pack %qD cannot "
15669 "have a default argument",
15670 identifier);
15671 else
15672 error_at (token->location, "template parameter packs cannot "
15673 "have default arguments");
15674 default_argument = NULL_TREE;
15677 else
15678 default_argument = NULL_TREE;
15680 /* Create the combined representation of the parameter and the
15681 default argument. */
15682 parameter = build_tree_list (default_argument, parameter);
15684 break;
15686 default:
15687 gcc_unreachable ();
15688 break;
15691 return parameter;
15694 /* Parse a template-id.
15696 template-id:
15697 template-name < template-argument-list [opt] >
15699 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15700 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15701 returned. Otherwise, if the template-name names a function, or set
15702 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15703 names a class, returns a TYPE_DECL for the specialization.
15705 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15706 uninstantiated templates. */
15708 static tree
15709 cp_parser_template_id (cp_parser *parser,
15710 bool template_keyword_p,
15711 bool check_dependency_p,
15712 enum tag_types tag_type,
15713 bool is_declaration)
15715 tree templ;
15716 tree arguments;
15717 tree template_id;
15718 cp_token_position start_of_id = 0;
15719 cp_token *next_token = NULL, *next_token_2 = NULL;
15720 bool is_identifier;
15722 /* If the next token corresponds to a template-id, there is no need
15723 to reparse it. */
15724 cp_token *token = cp_lexer_peek_token (parser->lexer);
15725 if (token->type == CPP_TEMPLATE_ID)
15727 cp_lexer_consume_token (parser->lexer);
15728 return saved_checks_value (token->u.tree_check_value);
15731 /* Avoid performing name lookup if there is no possibility of
15732 finding a template-id. */
15733 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15734 || (token->type == CPP_NAME
15735 && !cp_parser_nth_token_starts_template_argument_list_p
15736 (parser, 2)))
15738 cp_parser_error (parser, "expected template-id");
15739 return error_mark_node;
15742 /* Remember where the template-id starts. */
15743 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15744 start_of_id = cp_lexer_token_position (parser->lexer, false);
15746 push_deferring_access_checks (dk_deferred);
15748 /* Parse the template-name. */
15749 is_identifier = false;
15750 templ = cp_parser_template_name (parser, template_keyword_p,
15751 check_dependency_p,
15752 is_declaration,
15753 tag_type,
15754 &is_identifier);
15755 if (templ == error_mark_node || is_identifier)
15757 pop_deferring_access_checks ();
15758 return templ;
15761 /* Since we're going to preserve any side-effects from this parse, set up a
15762 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15763 in the template arguments. */
15764 tentative_firewall firewall (parser);
15766 /* If we find the sequence `[:' after a template-name, it's probably
15767 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15768 parse correctly the argument list. */
15769 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15770 == CPP_OPEN_SQUARE)
15771 && next_token->flags & DIGRAPH
15772 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15773 == CPP_COLON)
15774 && !(next_token_2->flags & PREV_WHITE))
15776 cp_parser_parse_tentatively (parser);
15777 /* Change `:' into `::'. */
15778 next_token_2->type = CPP_SCOPE;
15779 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15780 CPP_LESS. */
15781 cp_lexer_consume_token (parser->lexer);
15783 /* Parse the arguments. */
15784 arguments = cp_parser_enclosed_template_argument_list (parser);
15785 if (!cp_parser_parse_definitely (parser))
15787 /* If we couldn't parse an argument list, then we revert our changes
15788 and return simply an error. Maybe this is not a template-id
15789 after all. */
15790 next_token_2->type = CPP_COLON;
15791 cp_parser_error (parser, "expected %<<%>");
15792 pop_deferring_access_checks ();
15793 return error_mark_node;
15795 /* Otherwise, emit an error about the invalid digraph, but continue
15796 parsing because we got our argument list. */
15797 if (permerror (next_token->location,
15798 "%<<::%> cannot begin a template-argument list"))
15800 static bool hint = false;
15801 inform (next_token->location,
15802 "%<<:%> is an alternate spelling for %<[%>."
15803 " Insert whitespace between %<<%> and %<::%>");
15804 if (!hint && !flag_permissive)
15806 inform (next_token->location, "(if you use %<-fpermissive%> "
15807 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15808 "accept your code)");
15809 hint = true;
15813 else
15815 /* Look for the `<' that starts the template-argument-list. */
15816 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15818 pop_deferring_access_checks ();
15819 return error_mark_node;
15821 /* Parse the arguments. */
15822 arguments = cp_parser_enclosed_template_argument_list (parser);
15825 /* Set the location to be of the form:
15826 template-name < template-argument-list [opt] >
15827 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15828 with caret == start at the start of the template-name,
15829 ranging until the closing '>'. */
15830 location_t finish_loc
15831 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15832 location_t combined_loc
15833 = make_location (token->location, token->location, finish_loc);
15835 /* Check for concepts autos where they don't belong. We could
15836 identify types in some cases of idnetifier TEMPL, looking ahead
15837 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15838 types. We reject them in functions, but if what we have is an
15839 identifier, even with none_type we can't conclude it's NOT a
15840 type, we have to wait for template substitution. */
15841 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15842 template_id = error_mark_node;
15843 /* Build a representation of the specialization. */
15844 else if (identifier_p (templ))
15845 template_id = build_min_nt_loc (combined_loc,
15846 TEMPLATE_ID_EXPR,
15847 templ, arguments);
15848 else if (DECL_TYPE_TEMPLATE_P (templ)
15849 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15851 bool entering_scope;
15852 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15853 template (rather than some instantiation thereof) only if
15854 is not nested within some other construct. For example, in
15855 "template <typename T> void f(T) { A<T>::", A<T> is just an
15856 instantiation of A. */
15857 entering_scope = (template_parm_scope_p ()
15858 && cp_lexer_next_token_is (parser->lexer,
15859 CPP_SCOPE));
15860 template_id
15861 = finish_template_type (templ, arguments, entering_scope);
15863 /* A template-like identifier may be a partial concept id. */
15864 else if (flag_concepts
15865 && (template_id = (cp_parser_maybe_partial_concept_id
15866 (parser, templ, arguments))))
15867 return template_id;
15868 else if (variable_template_p (templ))
15870 template_id = lookup_template_variable (templ, arguments);
15871 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15872 SET_EXPR_LOCATION (template_id, combined_loc);
15874 else
15876 /* If it's not a class-template or a template-template, it should be
15877 a function-template. */
15878 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15879 || TREE_CODE (templ) == OVERLOAD
15880 || BASELINK_P (templ)));
15882 template_id = lookup_template_function (templ, arguments);
15883 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15884 SET_EXPR_LOCATION (template_id, combined_loc);
15887 /* If parsing tentatively, replace the sequence of tokens that makes
15888 up the template-id with a CPP_TEMPLATE_ID token. That way,
15889 should we re-parse the token stream, we will not have to repeat
15890 the effort required to do the parse, nor will we issue duplicate
15891 error messages about problems during instantiation of the
15892 template. */
15893 if (start_of_id
15894 /* Don't do this if we had a parse error in a declarator; re-parsing
15895 might succeed if a name changes meaning (60361). */
15896 && !(cp_parser_error_occurred (parser)
15897 && cp_parser_parsing_tentatively (parser)
15898 && parser->in_declarator_p))
15900 /* Reset the contents of the START_OF_ID token. */
15901 token->type = CPP_TEMPLATE_ID;
15902 token->location = combined_loc;
15904 /* We must mark the lookup as kept, so we don't throw it away on
15905 the first parse. */
15906 if (is_overloaded_fn (template_id))
15907 lookup_keep (get_fns (template_id), true);
15909 /* Retrieve any deferred checks. Do not pop this access checks yet
15910 so the memory will not be reclaimed during token replacing below. */
15911 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15912 token->u.tree_check_value->value = template_id;
15913 token->u.tree_check_value->checks = get_deferred_access_checks ();
15914 token->keyword = RID_MAX;
15916 /* Purge all subsequent tokens. */
15917 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15919 /* ??? Can we actually assume that, if template_id ==
15920 error_mark_node, we will have issued a diagnostic to the
15921 user, as opposed to simply marking the tentative parse as
15922 failed? */
15923 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15924 error_at (token->location, "parse error in template argument list");
15927 pop_to_parent_deferring_access_checks ();
15928 return template_id;
15931 /* Parse a template-name.
15933 template-name:
15934 identifier
15936 The standard should actually say:
15938 template-name:
15939 identifier
15940 operator-function-id
15942 A defect report has been filed about this issue.
15944 A conversion-function-id cannot be a template name because they cannot
15945 be part of a template-id. In fact, looking at this code:
15947 a.operator K<int>()
15949 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15950 It is impossible to call a templated conversion-function-id with an
15951 explicit argument list, since the only allowed template parameter is
15952 the type to which it is converting.
15954 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15955 `template' keyword, in a construction like:
15957 T::template f<3>()
15959 In that case `f' is taken to be a template-name, even though there
15960 is no way of knowing for sure.
15962 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15963 name refers to a set of overloaded functions, at least one of which
15964 is a template, or an IDENTIFIER_NODE with the name of the template,
15965 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15966 names are looked up inside uninstantiated templates. */
15968 static tree
15969 cp_parser_template_name (cp_parser* parser,
15970 bool template_keyword_p,
15971 bool check_dependency_p,
15972 bool is_declaration,
15973 enum tag_types tag_type,
15974 bool *is_identifier)
15976 tree identifier;
15977 tree decl;
15978 cp_token *token = cp_lexer_peek_token (parser->lexer);
15980 /* If the next token is `operator', then we have either an
15981 operator-function-id or a conversion-function-id. */
15982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15984 /* We don't know whether we're looking at an
15985 operator-function-id or a conversion-function-id. */
15986 cp_parser_parse_tentatively (parser);
15987 /* Try an operator-function-id. */
15988 identifier = cp_parser_operator_function_id (parser);
15989 /* If that didn't work, try a conversion-function-id. */
15990 if (!cp_parser_parse_definitely (parser))
15992 cp_parser_error (parser, "expected template-name");
15993 return error_mark_node;
15996 /* Look for the identifier. */
15997 else
15998 identifier = cp_parser_identifier (parser);
16000 /* If we didn't find an identifier, we don't have a template-id. */
16001 if (identifier == error_mark_node)
16002 return error_mark_node;
16004 /* If the name immediately followed the `template' keyword, then it
16005 is a template-name. However, if the next token is not `<', then
16006 we do not treat it as a template-name, since it is not being used
16007 as part of a template-id. This enables us to handle constructs
16008 like:
16010 template <typename T> struct S { S(); };
16011 template <typename T> S<T>::S();
16013 correctly. We would treat `S' as a template -- if it were `S<T>'
16014 -- but we do not if there is no `<'. */
16016 if (processing_template_decl
16017 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16019 /* In a declaration, in a dependent context, we pretend that the
16020 "template" keyword was present in order to improve error
16021 recovery. For example, given:
16023 template <typename T> void f(T::X<int>);
16025 we want to treat "X<int>" as a template-id. */
16026 if (is_declaration
16027 && !template_keyword_p
16028 && parser->scope && TYPE_P (parser->scope)
16029 && check_dependency_p
16030 && dependent_scope_p (parser->scope)
16031 /* Do not do this for dtors (or ctors), since they never
16032 need the template keyword before their name. */
16033 && !constructor_name_p (identifier, parser->scope))
16035 cp_token_position start = 0;
16037 /* Explain what went wrong. */
16038 error_at (token->location, "non-template %qD used as template",
16039 identifier);
16040 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16041 parser->scope, identifier);
16042 /* If parsing tentatively, find the location of the "<" token. */
16043 if (cp_parser_simulate_error (parser))
16044 start = cp_lexer_token_position (parser->lexer, true);
16045 /* Parse the template arguments so that we can issue error
16046 messages about them. */
16047 cp_lexer_consume_token (parser->lexer);
16048 cp_parser_enclosed_template_argument_list (parser);
16049 /* Skip tokens until we find a good place from which to
16050 continue parsing. */
16051 cp_parser_skip_to_closing_parenthesis (parser,
16052 /*recovering=*/true,
16053 /*or_comma=*/true,
16054 /*consume_paren=*/false);
16055 /* If parsing tentatively, permanently remove the
16056 template argument list. That will prevent duplicate
16057 error messages from being issued about the missing
16058 "template" keyword. */
16059 if (start)
16060 cp_lexer_purge_tokens_after (parser->lexer, start);
16061 if (is_identifier)
16062 *is_identifier = true;
16063 parser->context->object_type = NULL_TREE;
16064 return identifier;
16067 /* If the "template" keyword is present, then there is generally
16068 no point in doing name-lookup, so we just return IDENTIFIER.
16069 But, if the qualifying scope is non-dependent then we can
16070 (and must) do name-lookup normally. */
16071 if (template_keyword_p)
16073 tree scope = (parser->scope ? parser->scope
16074 : parser->context->object_type);
16075 if (scope && TYPE_P (scope)
16076 && (!CLASS_TYPE_P (scope)
16077 || (check_dependency_p && dependent_type_p (scope))))
16079 /* We're optimizing away the call to cp_parser_lookup_name, but
16080 we still need to do this. */
16081 parser->context->object_type = NULL_TREE;
16082 return identifier;
16087 /* Look up the name. */
16088 decl = cp_parser_lookup_name (parser, identifier,
16089 tag_type,
16090 /*is_template=*/true,
16091 /*is_namespace=*/false,
16092 check_dependency_p,
16093 /*ambiguous_decls=*/NULL,
16094 token->location);
16096 decl = strip_using_decl (decl);
16098 /* If DECL is a template, then the name was a template-name. */
16099 if (TREE_CODE (decl) == TEMPLATE_DECL)
16101 if (TREE_DEPRECATED (decl)
16102 && deprecated_state != DEPRECATED_SUPPRESS)
16103 warn_deprecated_use (decl, NULL_TREE);
16105 else
16107 /* The standard does not explicitly indicate whether a name that
16108 names a set of overloaded declarations, some of which are
16109 templates, is a template-name. However, such a name should
16110 be a template-name; otherwise, there is no way to form a
16111 template-id for the overloaded templates. */
16112 bool found = false;
16114 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16115 !found && iter; ++iter)
16116 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16117 found = true;
16119 if (!found)
16121 /* The name does not name a template. */
16122 cp_parser_error (parser, "expected template-name");
16123 return error_mark_node;
16127 /* If DECL is dependent, and refers to a function, then just return
16128 its name; we will look it up again during template instantiation. */
16129 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16131 tree scope = ovl_scope (decl);
16132 if (TYPE_P (scope) && dependent_type_p (scope))
16133 return identifier;
16136 return decl;
16139 /* Parse a template-argument-list.
16141 template-argument-list:
16142 template-argument ... [opt]
16143 template-argument-list , template-argument ... [opt]
16145 Returns a TREE_VEC containing the arguments. */
16147 static tree
16148 cp_parser_template_argument_list (cp_parser* parser)
16150 tree fixed_args[10];
16151 unsigned n_args = 0;
16152 unsigned alloced = 10;
16153 tree *arg_ary = fixed_args;
16154 tree vec;
16155 bool saved_in_template_argument_list_p;
16156 bool saved_ice_p;
16157 bool saved_non_ice_p;
16159 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16160 parser->in_template_argument_list_p = true;
16161 /* Even if the template-id appears in an integral
16162 constant-expression, the contents of the argument list do
16163 not. */
16164 saved_ice_p = parser->integral_constant_expression_p;
16165 parser->integral_constant_expression_p = false;
16166 saved_non_ice_p = parser->non_integral_constant_expression_p;
16167 parser->non_integral_constant_expression_p = false;
16169 /* Parse the arguments. */
16172 tree argument;
16174 if (n_args)
16175 /* Consume the comma. */
16176 cp_lexer_consume_token (parser->lexer);
16178 /* Parse the template-argument. */
16179 argument = cp_parser_template_argument (parser);
16181 /* If the next token is an ellipsis, we're expanding a template
16182 argument pack. */
16183 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16185 if (argument == error_mark_node)
16187 cp_token *token = cp_lexer_peek_token (parser->lexer);
16188 error_at (token->location,
16189 "expected parameter pack before %<...%>");
16191 /* Consume the `...' token. */
16192 cp_lexer_consume_token (parser->lexer);
16194 /* Make the argument into a TYPE_PACK_EXPANSION or
16195 EXPR_PACK_EXPANSION. */
16196 argument = make_pack_expansion (argument);
16199 if (n_args == alloced)
16201 alloced *= 2;
16203 if (arg_ary == fixed_args)
16205 arg_ary = XNEWVEC (tree, alloced);
16206 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16208 else
16209 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16211 arg_ary[n_args++] = argument;
16213 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16215 vec = make_tree_vec (n_args);
16217 while (n_args--)
16218 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16220 if (arg_ary != fixed_args)
16221 free (arg_ary);
16222 parser->non_integral_constant_expression_p = saved_non_ice_p;
16223 parser->integral_constant_expression_p = saved_ice_p;
16224 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16225 if (CHECKING_P)
16226 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16227 return vec;
16230 /* Parse a template-argument.
16232 template-argument:
16233 assignment-expression
16234 type-id
16235 id-expression
16237 The representation is that of an assignment-expression, type-id, or
16238 id-expression -- except that the qualified id-expression is
16239 evaluated, so that the value returned is either a DECL or an
16240 OVERLOAD.
16242 Although the standard says "assignment-expression", it forbids
16243 throw-expressions or assignments in the template argument.
16244 Therefore, we use "conditional-expression" instead. */
16246 static tree
16247 cp_parser_template_argument (cp_parser* parser)
16249 tree argument;
16250 bool template_p;
16251 bool address_p;
16252 bool maybe_type_id = false;
16253 cp_token *token = NULL, *argument_start_token = NULL;
16254 location_t loc = 0;
16255 cp_id_kind idk;
16257 /* There's really no way to know what we're looking at, so we just
16258 try each alternative in order.
16260 [temp.arg]
16262 In a template-argument, an ambiguity between a type-id and an
16263 expression is resolved to a type-id, regardless of the form of
16264 the corresponding template-parameter.
16266 Therefore, we try a type-id first. */
16267 cp_parser_parse_tentatively (parser);
16268 argument = cp_parser_template_type_arg (parser);
16269 /* If there was no error parsing the type-id but the next token is a
16270 '>>', our behavior depends on which dialect of C++ we're
16271 parsing. In C++98, we probably found a typo for '> >'. But there
16272 are type-id which are also valid expressions. For instance:
16274 struct X { int operator >> (int); };
16275 template <int V> struct Foo {};
16276 Foo<X () >> 5> r;
16278 Here 'X()' is a valid type-id of a function type, but the user just
16279 wanted to write the expression "X() >> 5". Thus, we remember that we
16280 found a valid type-id, but we still try to parse the argument as an
16281 expression to see what happens.
16283 In C++0x, the '>>' will be considered two separate '>'
16284 tokens. */
16285 if (!cp_parser_error_occurred (parser)
16286 && cxx_dialect == cxx98
16287 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16289 maybe_type_id = true;
16290 cp_parser_abort_tentative_parse (parser);
16292 else
16294 /* If the next token isn't a `,' or a `>', then this argument wasn't
16295 really finished. This means that the argument is not a valid
16296 type-id. */
16297 if (!cp_parser_next_token_ends_template_argument_p (parser))
16298 cp_parser_error (parser, "expected template-argument");
16299 /* If that worked, we're done. */
16300 if (cp_parser_parse_definitely (parser))
16301 return argument;
16303 /* We're still not sure what the argument will be. */
16304 cp_parser_parse_tentatively (parser);
16305 /* Try a template. */
16306 argument_start_token = cp_lexer_peek_token (parser->lexer);
16307 argument = cp_parser_id_expression (parser,
16308 /*template_keyword_p=*/false,
16309 /*check_dependency_p=*/true,
16310 &template_p,
16311 /*declarator_p=*/false,
16312 /*optional_p=*/false);
16313 /* If the next token isn't a `,' or a `>', then this argument wasn't
16314 really finished. */
16315 if (!cp_parser_next_token_ends_template_argument_p (parser))
16316 cp_parser_error (parser, "expected template-argument");
16317 if (!cp_parser_error_occurred (parser))
16319 /* Figure out what is being referred to. If the id-expression
16320 was for a class template specialization, then we will have a
16321 TYPE_DECL at this point. There is no need to do name lookup
16322 at this point in that case. */
16323 if (TREE_CODE (argument) != TYPE_DECL)
16324 argument = cp_parser_lookup_name (parser, argument,
16325 none_type,
16326 /*is_template=*/template_p,
16327 /*is_namespace=*/false,
16328 /*check_dependency=*/true,
16329 /*ambiguous_decls=*/NULL,
16330 argument_start_token->location);
16331 /* Handle a constrained-type-specifier for a non-type template
16332 parameter. */
16333 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16334 argument = decl;
16335 else if (TREE_CODE (argument) != TEMPLATE_DECL
16336 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16337 cp_parser_error (parser, "expected template-name");
16339 if (cp_parser_parse_definitely (parser))
16341 if (TREE_DEPRECATED (argument))
16342 warn_deprecated_use (argument, NULL_TREE);
16343 return argument;
16345 /* It must be a non-type argument. In C++17 any constant-expression is
16346 allowed. */
16347 if (cxx_dialect > cxx14)
16348 goto general_expr;
16350 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16352 -- an integral constant-expression of integral or enumeration
16353 type; or
16355 -- the name of a non-type template-parameter; or
16357 -- the name of an object or function with external linkage...
16359 -- the address of an object or function with external linkage...
16361 -- a pointer to member... */
16362 /* Look for a non-type template parameter. */
16363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16365 cp_parser_parse_tentatively (parser);
16366 argument = cp_parser_primary_expression (parser,
16367 /*address_p=*/false,
16368 /*cast_p=*/false,
16369 /*template_arg_p=*/true,
16370 &idk);
16371 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16372 || !cp_parser_next_token_ends_template_argument_p (parser))
16373 cp_parser_simulate_error (parser);
16374 if (cp_parser_parse_definitely (parser))
16375 return argument;
16378 /* If the next token is "&", the argument must be the address of an
16379 object or function with external linkage. */
16380 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16381 if (address_p)
16383 loc = cp_lexer_peek_token (parser->lexer)->location;
16384 cp_lexer_consume_token (parser->lexer);
16386 /* See if we might have an id-expression. */
16387 token = cp_lexer_peek_token (parser->lexer);
16388 if (token->type == CPP_NAME
16389 || token->keyword == RID_OPERATOR
16390 || token->type == CPP_SCOPE
16391 || token->type == CPP_TEMPLATE_ID
16392 || token->type == CPP_NESTED_NAME_SPECIFIER)
16394 cp_parser_parse_tentatively (parser);
16395 argument = cp_parser_primary_expression (parser,
16396 address_p,
16397 /*cast_p=*/false,
16398 /*template_arg_p=*/true,
16399 &idk);
16400 if (cp_parser_error_occurred (parser)
16401 || !cp_parser_next_token_ends_template_argument_p (parser))
16402 cp_parser_abort_tentative_parse (parser);
16403 else
16405 tree probe;
16407 if (INDIRECT_REF_P (argument))
16409 /* Strip the dereference temporarily. */
16410 gcc_assert (REFERENCE_REF_P (argument));
16411 argument = TREE_OPERAND (argument, 0);
16414 /* If we're in a template, we represent a qualified-id referring
16415 to a static data member as a SCOPE_REF even if the scope isn't
16416 dependent so that we can check access control later. */
16417 probe = argument;
16418 if (TREE_CODE (probe) == SCOPE_REF)
16419 probe = TREE_OPERAND (probe, 1);
16420 if (VAR_P (probe))
16422 /* A variable without external linkage might still be a
16423 valid constant-expression, so no error is issued here
16424 if the external-linkage check fails. */
16425 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16426 cp_parser_simulate_error (parser);
16428 else if (is_overloaded_fn (argument))
16429 /* All overloaded functions are allowed; if the external
16430 linkage test does not pass, an error will be issued
16431 later. */
16433 else if (address_p
16434 && (TREE_CODE (argument) == OFFSET_REF
16435 || TREE_CODE (argument) == SCOPE_REF))
16436 /* A pointer-to-member. */
16438 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16440 else
16441 cp_parser_simulate_error (parser);
16443 if (cp_parser_parse_definitely (parser))
16445 if (address_p)
16446 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16447 tf_warning_or_error);
16448 else
16449 argument = convert_from_reference (argument);
16450 return argument;
16454 /* If the argument started with "&", there are no other valid
16455 alternatives at this point. */
16456 if (address_p)
16458 cp_parser_error (parser, "invalid non-type template argument");
16459 return error_mark_node;
16462 general_expr:
16463 /* If the argument wasn't successfully parsed as a type-id followed
16464 by '>>', the argument can only be a constant expression now.
16465 Otherwise, we try parsing the constant-expression tentatively,
16466 because the argument could really be a type-id. */
16467 if (maybe_type_id)
16468 cp_parser_parse_tentatively (parser);
16470 if (cxx_dialect <= cxx14)
16471 argument = cp_parser_constant_expression (parser);
16472 else
16474 /* With C++17 generalized non-type template arguments we need to handle
16475 lvalue constant expressions, too. */
16476 argument = cp_parser_assignment_expression (parser);
16477 require_potential_constant_expression (argument);
16480 if (!maybe_type_id)
16481 return argument;
16482 if (!cp_parser_next_token_ends_template_argument_p (parser))
16483 cp_parser_error (parser, "expected template-argument");
16484 if (cp_parser_parse_definitely (parser))
16485 return argument;
16486 /* We did our best to parse the argument as a non type-id, but that
16487 was the only alternative that matched (albeit with a '>' after
16488 it). We can assume it's just a typo from the user, and a
16489 diagnostic will then be issued. */
16490 return cp_parser_template_type_arg (parser);
16493 /* Parse an explicit-instantiation.
16495 explicit-instantiation:
16496 template declaration
16498 Although the standard says `declaration', what it really means is:
16500 explicit-instantiation:
16501 template decl-specifier-seq [opt] declarator [opt] ;
16503 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16504 supposed to be allowed. A defect report has been filed about this
16505 issue.
16507 GNU Extension:
16509 explicit-instantiation:
16510 storage-class-specifier template
16511 decl-specifier-seq [opt] declarator [opt] ;
16512 function-specifier template
16513 decl-specifier-seq [opt] declarator [opt] ; */
16515 static void
16516 cp_parser_explicit_instantiation (cp_parser* parser)
16518 int declares_class_or_enum;
16519 cp_decl_specifier_seq decl_specifiers;
16520 tree extension_specifier = NULL_TREE;
16522 timevar_push (TV_TEMPLATE_INST);
16524 /* Look for an (optional) storage-class-specifier or
16525 function-specifier. */
16526 if (cp_parser_allow_gnu_extensions_p (parser))
16528 extension_specifier
16529 = cp_parser_storage_class_specifier_opt (parser);
16530 if (!extension_specifier)
16531 extension_specifier
16532 = cp_parser_function_specifier_opt (parser,
16533 /*decl_specs=*/NULL);
16536 /* Look for the `template' keyword. */
16537 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16538 /* Let the front end know that we are processing an explicit
16539 instantiation. */
16540 begin_explicit_instantiation ();
16541 /* [temp.explicit] says that we are supposed to ignore access
16542 control while processing explicit instantiation directives. */
16543 push_deferring_access_checks (dk_no_check);
16544 /* Parse a decl-specifier-seq. */
16545 cp_parser_decl_specifier_seq (parser,
16546 CP_PARSER_FLAGS_OPTIONAL,
16547 &decl_specifiers,
16548 &declares_class_or_enum);
16549 /* If there was exactly one decl-specifier, and it declared a class,
16550 and there's no declarator, then we have an explicit type
16551 instantiation. */
16552 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16554 tree type;
16556 type = check_tag_decl (&decl_specifiers,
16557 /*explicit_type_instantiation_p=*/true);
16558 /* Turn access control back on for names used during
16559 template instantiation. */
16560 pop_deferring_access_checks ();
16561 if (type)
16562 do_type_instantiation (type, extension_specifier,
16563 /*complain=*/tf_error);
16565 else
16567 cp_declarator *declarator;
16568 tree decl;
16570 /* Parse the declarator. */
16571 declarator
16572 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16573 /*ctor_dtor_or_conv_p=*/NULL,
16574 /*parenthesized_p=*/NULL,
16575 /*member_p=*/false,
16576 /*friend_p=*/false);
16577 if (declares_class_or_enum & 2)
16578 cp_parser_check_for_definition_in_return_type (declarator,
16579 decl_specifiers.type,
16580 decl_specifiers.locations[ds_type_spec]);
16581 if (declarator != cp_error_declarator)
16583 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16584 permerror (decl_specifiers.locations[ds_inline],
16585 "explicit instantiation shall not use"
16586 " %<inline%> specifier");
16587 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16588 permerror (decl_specifiers.locations[ds_constexpr],
16589 "explicit instantiation shall not use"
16590 " %<constexpr%> specifier");
16592 decl = grokdeclarator (declarator, &decl_specifiers,
16593 NORMAL, 0, &decl_specifiers.attributes);
16594 /* Turn access control back on for names used during
16595 template instantiation. */
16596 pop_deferring_access_checks ();
16597 /* Do the explicit instantiation. */
16598 do_decl_instantiation (decl, extension_specifier);
16600 else
16602 pop_deferring_access_checks ();
16603 /* Skip the body of the explicit instantiation. */
16604 cp_parser_skip_to_end_of_statement (parser);
16607 /* We're done with the instantiation. */
16608 end_explicit_instantiation ();
16610 cp_parser_consume_semicolon_at_end_of_statement (parser);
16612 timevar_pop (TV_TEMPLATE_INST);
16615 /* Parse an explicit-specialization.
16617 explicit-specialization:
16618 template < > declaration
16620 Although the standard says `declaration', what it really means is:
16622 explicit-specialization:
16623 template <> decl-specifier [opt] init-declarator [opt] ;
16624 template <> function-definition
16625 template <> explicit-specialization
16626 template <> template-declaration */
16628 static void
16629 cp_parser_explicit_specialization (cp_parser* parser)
16631 bool need_lang_pop;
16632 cp_token *token = cp_lexer_peek_token (parser->lexer);
16634 /* Look for the `template' keyword. */
16635 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16636 /* Look for the `<'. */
16637 cp_parser_require (parser, CPP_LESS, RT_LESS);
16638 /* Look for the `>'. */
16639 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16640 /* We have processed another parameter list. */
16641 ++parser->num_template_parameter_lists;
16642 /* [temp]
16644 A template ... explicit specialization ... shall not have C
16645 linkage. */
16646 if (current_lang_name == lang_name_c)
16648 error_at (token->location, "template specialization with C linkage");
16649 maybe_show_extern_c_location ();
16650 /* Give it C++ linkage to avoid confusing other parts of the
16651 front end. */
16652 push_lang_context (lang_name_cplusplus);
16653 need_lang_pop = true;
16655 else
16656 need_lang_pop = false;
16657 /* Let the front end know that we are beginning a specialization. */
16658 if (!begin_specialization ())
16660 end_specialization ();
16661 return;
16664 /* If the next keyword is `template', we need to figure out whether
16665 or not we're looking a template-declaration. */
16666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16668 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16669 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16670 cp_parser_template_declaration_after_export (parser,
16671 /*member_p=*/false);
16672 else
16673 cp_parser_explicit_specialization (parser);
16675 else
16676 /* Parse the dependent declaration. */
16677 cp_parser_single_declaration (parser,
16678 /*checks=*/NULL,
16679 /*member_p=*/false,
16680 /*explicit_specialization_p=*/true,
16681 /*friend_p=*/NULL);
16682 /* We're done with the specialization. */
16683 end_specialization ();
16684 /* For the erroneous case of a template with C linkage, we pushed an
16685 implicit C++ linkage scope; exit that scope now. */
16686 if (need_lang_pop)
16687 pop_lang_context ();
16688 /* We're done with this parameter list. */
16689 --parser->num_template_parameter_lists;
16692 /* Parse a type-specifier.
16694 type-specifier:
16695 simple-type-specifier
16696 class-specifier
16697 enum-specifier
16698 elaborated-type-specifier
16699 cv-qualifier
16701 GNU Extension:
16703 type-specifier:
16704 __complex__
16706 Returns a representation of the type-specifier. For a
16707 class-specifier, enum-specifier, or elaborated-type-specifier, a
16708 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16710 The parser flags FLAGS is used to control type-specifier parsing.
16712 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16713 in a decl-specifier-seq.
16715 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16716 class-specifier, enum-specifier, or elaborated-type-specifier, then
16717 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16718 if a type is declared; 2 if it is defined. Otherwise, it is set to
16719 zero.
16721 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16722 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16723 is set to FALSE. */
16725 static tree
16726 cp_parser_type_specifier (cp_parser* parser,
16727 cp_parser_flags flags,
16728 cp_decl_specifier_seq *decl_specs,
16729 bool is_declaration,
16730 int* declares_class_or_enum,
16731 bool* is_cv_qualifier)
16733 tree type_spec = NULL_TREE;
16734 cp_token *token;
16735 enum rid keyword;
16736 cp_decl_spec ds = ds_last;
16738 /* Assume this type-specifier does not declare a new type. */
16739 if (declares_class_or_enum)
16740 *declares_class_or_enum = 0;
16741 /* And that it does not specify a cv-qualifier. */
16742 if (is_cv_qualifier)
16743 *is_cv_qualifier = false;
16744 /* Peek at the next token. */
16745 token = cp_lexer_peek_token (parser->lexer);
16747 /* If we're looking at a keyword, we can use that to guide the
16748 production we choose. */
16749 keyword = token->keyword;
16750 switch (keyword)
16752 case RID_ENUM:
16753 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16754 goto elaborated_type_specifier;
16756 /* Look for the enum-specifier. */
16757 type_spec = cp_parser_enum_specifier (parser);
16758 /* If that worked, we're done. */
16759 if (type_spec)
16761 if (declares_class_or_enum)
16762 *declares_class_or_enum = 2;
16763 if (decl_specs)
16764 cp_parser_set_decl_spec_type (decl_specs,
16765 type_spec,
16766 token,
16767 /*type_definition_p=*/true);
16768 return type_spec;
16770 else
16771 goto elaborated_type_specifier;
16773 /* Any of these indicate either a class-specifier, or an
16774 elaborated-type-specifier. */
16775 case RID_CLASS:
16776 case RID_STRUCT:
16777 case RID_UNION:
16778 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16779 goto elaborated_type_specifier;
16781 /* Parse tentatively so that we can back up if we don't find a
16782 class-specifier. */
16783 cp_parser_parse_tentatively (parser);
16784 /* Look for the class-specifier. */
16785 type_spec = cp_parser_class_specifier (parser);
16786 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16787 /* If that worked, we're done. */
16788 if (cp_parser_parse_definitely (parser))
16790 if (declares_class_or_enum)
16791 *declares_class_or_enum = 2;
16792 if (decl_specs)
16793 cp_parser_set_decl_spec_type (decl_specs,
16794 type_spec,
16795 token,
16796 /*type_definition_p=*/true);
16797 return type_spec;
16800 /* Fall through. */
16801 elaborated_type_specifier:
16802 /* We're declaring (not defining) a class or enum. */
16803 if (declares_class_or_enum)
16804 *declares_class_or_enum = 1;
16806 /* Fall through. */
16807 case RID_TYPENAME:
16808 /* Look for an elaborated-type-specifier. */
16809 type_spec
16810 = (cp_parser_elaborated_type_specifier
16811 (parser,
16812 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16813 is_declaration));
16814 if (decl_specs)
16815 cp_parser_set_decl_spec_type (decl_specs,
16816 type_spec,
16817 token,
16818 /*type_definition_p=*/false);
16819 return type_spec;
16821 case RID_CONST:
16822 ds = ds_const;
16823 if (is_cv_qualifier)
16824 *is_cv_qualifier = true;
16825 break;
16827 case RID_VOLATILE:
16828 ds = ds_volatile;
16829 if (is_cv_qualifier)
16830 *is_cv_qualifier = true;
16831 break;
16833 case RID_RESTRICT:
16834 ds = ds_restrict;
16835 if (is_cv_qualifier)
16836 *is_cv_qualifier = true;
16837 break;
16839 case RID_COMPLEX:
16840 /* The `__complex__' keyword is a GNU extension. */
16841 ds = ds_complex;
16842 break;
16844 default:
16845 break;
16848 /* Handle simple keywords. */
16849 if (ds != ds_last)
16851 if (decl_specs)
16853 set_and_check_decl_spec_loc (decl_specs, ds, token);
16854 decl_specs->any_specifiers_p = true;
16856 return cp_lexer_consume_token (parser->lexer)->u.value;
16859 /* If we do not already have a type-specifier, assume we are looking
16860 at a simple-type-specifier. */
16861 type_spec = cp_parser_simple_type_specifier (parser,
16862 decl_specs,
16863 flags);
16865 /* If we didn't find a type-specifier, and a type-specifier was not
16866 optional in this context, issue an error message. */
16867 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16869 cp_parser_error (parser, "expected type specifier");
16870 return error_mark_node;
16873 return type_spec;
16876 /* Parse a simple-type-specifier.
16878 simple-type-specifier:
16879 :: [opt] nested-name-specifier [opt] type-name
16880 :: [opt] nested-name-specifier template template-id
16881 char
16882 wchar_t
16883 bool
16884 short
16886 long
16887 signed
16888 unsigned
16889 float
16890 double
16891 void
16893 C++11 Extension:
16895 simple-type-specifier:
16896 auto
16897 decltype ( expression )
16898 char16_t
16899 char32_t
16900 __underlying_type ( type-id )
16902 C++17 extension:
16904 nested-name-specifier(opt) template-name
16906 GNU Extension:
16908 simple-type-specifier:
16909 __int128
16910 __typeof__ unary-expression
16911 __typeof__ ( type-id )
16912 __typeof__ ( type-id ) { initializer-list , [opt] }
16914 Concepts Extension:
16916 simple-type-specifier:
16917 constrained-type-specifier
16919 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16920 appropriately updated. */
16922 static tree
16923 cp_parser_simple_type_specifier (cp_parser* parser,
16924 cp_decl_specifier_seq *decl_specs,
16925 cp_parser_flags flags)
16927 tree type = NULL_TREE;
16928 cp_token *token;
16929 int idx;
16931 /* Peek at the next token. */
16932 token = cp_lexer_peek_token (parser->lexer);
16934 /* If we're looking at a keyword, things are easy. */
16935 switch (token->keyword)
16937 case RID_CHAR:
16938 if (decl_specs)
16939 decl_specs->explicit_char_p = true;
16940 type = char_type_node;
16941 break;
16942 case RID_CHAR16:
16943 type = char16_type_node;
16944 break;
16945 case RID_CHAR32:
16946 type = char32_type_node;
16947 break;
16948 case RID_WCHAR:
16949 type = wchar_type_node;
16950 break;
16951 case RID_BOOL:
16952 type = boolean_type_node;
16953 break;
16954 case RID_SHORT:
16955 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16956 type = short_integer_type_node;
16957 break;
16958 case RID_INT:
16959 if (decl_specs)
16960 decl_specs->explicit_int_p = true;
16961 type = integer_type_node;
16962 break;
16963 case RID_INT_N_0:
16964 case RID_INT_N_1:
16965 case RID_INT_N_2:
16966 case RID_INT_N_3:
16967 idx = token->keyword - RID_INT_N_0;
16968 if (! int_n_enabled_p [idx])
16969 break;
16970 if (decl_specs)
16972 decl_specs->explicit_intN_p = true;
16973 decl_specs->int_n_idx = idx;
16975 type = int_n_trees [idx].signed_type;
16976 break;
16977 case RID_LONG:
16978 if (decl_specs)
16979 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16980 type = long_integer_type_node;
16981 break;
16982 case RID_SIGNED:
16983 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16984 type = integer_type_node;
16985 break;
16986 case RID_UNSIGNED:
16987 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16988 type = unsigned_type_node;
16989 break;
16990 case RID_FLOAT:
16991 type = float_type_node;
16992 break;
16993 case RID_DOUBLE:
16994 type = double_type_node;
16995 break;
16996 case RID_VOID:
16997 type = void_type_node;
16998 break;
17000 case RID_AUTO:
17001 maybe_warn_cpp0x (CPP0X_AUTO);
17002 if (parser->auto_is_implicit_function_template_parm_p)
17004 /* The 'auto' might be the placeholder return type for a function decl
17005 with trailing return type. */
17006 bool have_trailing_return_fn_decl = false;
17008 cp_parser_parse_tentatively (parser);
17009 cp_lexer_consume_token (parser->lexer);
17010 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17011 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17012 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17013 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17015 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17017 cp_lexer_consume_token (parser->lexer);
17018 cp_parser_skip_to_closing_parenthesis (parser,
17019 /*recovering*/false,
17020 /*or_comma*/false,
17021 /*consume_paren*/true);
17022 continue;
17025 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17027 have_trailing_return_fn_decl = true;
17028 break;
17031 cp_lexer_consume_token (parser->lexer);
17033 cp_parser_abort_tentative_parse (parser);
17035 if (have_trailing_return_fn_decl)
17037 type = make_auto ();
17038 break;
17041 if (cxx_dialect >= cxx14)
17043 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17044 type = TREE_TYPE (type);
17046 else
17047 type = error_mark_node;
17049 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17051 if (cxx_dialect < cxx14)
17052 error_at (token->location,
17053 "use of %<auto%> in lambda parameter declaration "
17054 "only available with "
17055 "-std=c++14 or -std=gnu++14");
17057 else if (cxx_dialect < cxx14)
17058 error_at (token->location,
17059 "use of %<auto%> in parameter declaration "
17060 "only available with "
17061 "-std=c++14 or -std=gnu++14");
17062 else if (!flag_concepts)
17063 pedwarn (token->location, 0,
17064 "use of %<auto%> in parameter declaration "
17065 "only available with -fconcepts");
17067 else
17068 type = make_auto ();
17069 break;
17071 case RID_DECLTYPE:
17072 /* Since DR 743, decltype can either be a simple-type-specifier by
17073 itself or begin a nested-name-specifier. Parsing it will replace
17074 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17075 handling below decide what to do. */
17076 cp_parser_decltype (parser);
17077 cp_lexer_set_token_position (parser->lexer, token);
17078 break;
17080 case RID_TYPEOF:
17081 /* Consume the `typeof' token. */
17082 cp_lexer_consume_token (parser->lexer);
17083 /* Parse the operand to `typeof'. */
17084 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17085 /* If it is not already a TYPE, take its type. */
17086 if (!TYPE_P (type))
17087 type = finish_typeof (type);
17089 if (decl_specs)
17090 cp_parser_set_decl_spec_type (decl_specs, type,
17091 token,
17092 /*type_definition_p=*/false);
17094 return type;
17096 case RID_UNDERLYING_TYPE:
17097 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17098 if (decl_specs)
17099 cp_parser_set_decl_spec_type (decl_specs, type,
17100 token,
17101 /*type_definition_p=*/false);
17103 return type;
17105 case RID_BASES:
17106 case RID_DIRECT_BASES:
17107 type = cp_parser_trait_expr (parser, token->keyword);
17108 if (decl_specs)
17109 cp_parser_set_decl_spec_type (decl_specs, type,
17110 token,
17111 /*type_definition_p=*/false);
17112 return type;
17113 default:
17114 break;
17117 /* If token is an already-parsed decltype not followed by ::,
17118 it's a simple-type-specifier. */
17119 if (token->type == CPP_DECLTYPE
17120 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17122 type = saved_checks_value (token->u.tree_check_value);
17123 if (decl_specs)
17125 cp_parser_set_decl_spec_type (decl_specs, type,
17126 token,
17127 /*type_definition_p=*/false);
17128 /* Remember that we are handling a decltype in order to
17129 implement the resolution of DR 1510 when the argument
17130 isn't instantiation dependent. */
17131 decl_specs->decltype_p = true;
17133 cp_lexer_consume_token (parser->lexer);
17134 return type;
17137 /* If the type-specifier was for a built-in type, we're done. */
17138 if (type)
17140 /* Record the type. */
17141 if (decl_specs
17142 && (token->keyword != RID_SIGNED
17143 && token->keyword != RID_UNSIGNED
17144 && token->keyword != RID_SHORT
17145 && token->keyword != RID_LONG))
17146 cp_parser_set_decl_spec_type (decl_specs,
17147 type,
17148 token,
17149 /*type_definition_p=*/false);
17150 if (decl_specs)
17151 decl_specs->any_specifiers_p = true;
17153 /* Consume the token. */
17154 cp_lexer_consume_token (parser->lexer);
17156 if (type == error_mark_node)
17157 return error_mark_node;
17159 /* There is no valid C++ program where a non-template type is
17160 followed by a "<". That usually indicates that the user thought
17161 that the type was a template. */
17162 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17163 token->location);
17165 return TYPE_NAME (type);
17168 /* The type-specifier must be a user-defined type. */
17169 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17171 bool qualified_p;
17172 bool global_p;
17174 /* Don't gobble tokens or issue error messages if this is an
17175 optional type-specifier. */
17176 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17177 cp_parser_parse_tentatively (parser);
17179 token = cp_lexer_peek_token (parser->lexer);
17181 /* Look for the optional `::' operator. */
17182 global_p
17183 = (cp_parser_global_scope_opt (parser,
17184 /*current_scope_valid_p=*/false)
17185 != NULL_TREE);
17186 /* Look for the nested-name specifier. */
17187 qualified_p
17188 = (cp_parser_nested_name_specifier_opt (parser,
17189 /*typename_keyword_p=*/false,
17190 /*check_dependency_p=*/true,
17191 /*type_p=*/false,
17192 /*is_declaration=*/false)
17193 != NULL_TREE);
17194 /* If we have seen a nested-name-specifier, and the next token
17195 is `template', then we are using the template-id production. */
17196 if (parser->scope
17197 && cp_parser_optional_template_keyword (parser))
17199 /* Look for the template-id. */
17200 type = cp_parser_template_id (parser,
17201 /*template_keyword_p=*/true,
17202 /*check_dependency_p=*/true,
17203 none_type,
17204 /*is_declaration=*/false);
17205 /* If the template-id did not name a type, we are out of
17206 luck. */
17207 if (TREE_CODE (type) != TYPE_DECL)
17209 cp_parser_error (parser, "expected template-id for type");
17210 type = NULL_TREE;
17213 /* Otherwise, look for a type-name. */
17214 else
17215 type = cp_parser_type_name (parser);
17216 /* Keep track of all name-lookups performed in class scopes. */
17217 if (type
17218 && !global_p
17219 && !qualified_p
17220 && TREE_CODE (type) == TYPE_DECL
17221 && identifier_p (DECL_NAME (type)))
17222 maybe_note_name_used_in_class (DECL_NAME (type), type);
17223 /* If it didn't work out, we don't have a TYPE. */
17224 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17225 && !cp_parser_parse_definitely (parser))
17226 type = NULL_TREE;
17227 if (!type && cxx_dialect >= cxx17)
17229 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17230 cp_parser_parse_tentatively (parser);
17232 cp_parser_global_scope_opt (parser,
17233 /*current_scope_valid_p=*/false);
17234 cp_parser_nested_name_specifier_opt (parser,
17235 /*typename_keyword_p=*/false,
17236 /*check_dependency_p=*/true,
17237 /*type_p=*/false,
17238 /*is_declaration=*/false);
17239 tree name = cp_parser_identifier (parser);
17240 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17241 && parser->scope != error_mark_node)
17243 tree tmpl = cp_parser_lookup_name (parser, name,
17244 none_type,
17245 /*is_template=*/false,
17246 /*is_namespace=*/false,
17247 /*check_dependency=*/true,
17248 /*ambiguous_decls=*/NULL,
17249 token->location);
17250 if (tmpl && tmpl != error_mark_node
17251 && (DECL_CLASS_TEMPLATE_P (tmpl)
17252 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17253 type = make_template_placeholder (tmpl);
17254 else
17256 type = error_mark_node;
17257 if (!cp_parser_simulate_error (parser))
17258 cp_parser_name_lookup_error (parser, name, tmpl,
17259 NLE_TYPE, token->location);
17262 else
17263 type = error_mark_node;
17265 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17266 && !cp_parser_parse_definitely (parser))
17267 type = NULL_TREE;
17269 if (type && decl_specs)
17270 cp_parser_set_decl_spec_type (decl_specs, type,
17271 token,
17272 /*type_definition_p=*/false);
17275 /* If we didn't get a type-name, issue an error message. */
17276 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17278 cp_parser_error (parser, "expected type-name");
17279 return error_mark_node;
17282 if (type && type != error_mark_node)
17284 /* See if TYPE is an Objective-C type, and if so, parse and
17285 accept any protocol references following it. Do this before
17286 the cp_parser_check_for_invalid_template_id() call, because
17287 Objective-C types can be followed by '<...>' which would
17288 enclose protocol names rather than template arguments, and so
17289 everything is fine. */
17290 if (c_dialect_objc () && !parser->scope
17291 && (objc_is_id (type) || objc_is_class_name (type)))
17293 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17294 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17296 /* Clobber the "unqualified" type previously entered into
17297 DECL_SPECS with the new, improved protocol-qualified version. */
17298 if (decl_specs)
17299 decl_specs->type = qual_type;
17301 return qual_type;
17304 /* There is no valid C++ program where a non-template type is
17305 followed by a "<". That usually indicates that the user
17306 thought that the type was a template. */
17307 cp_parser_check_for_invalid_template_id (parser, type,
17308 none_type,
17309 token->location);
17312 return type;
17315 /* Parse a type-name.
17317 type-name:
17318 class-name
17319 enum-name
17320 typedef-name
17321 simple-template-id [in c++0x]
17323 enum-name:
17324 identifier
17326 typedef-name:
17327 identifier
17329 Concepts:
17331 type-name:
17332 concept-name
17333 partial-concept-id
17335 concept-name:
17336 identifier
17338 Returns a TYPE_DECL for the type. */
17340 static tree
17341 cp_parser_type_name (cp_parser* parser)
17343 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17346 /* See above. */
17347 static tree
17348 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17350 tree type_decl;
17352 /* We can't know yet whether it is a class-name or not. */
17353 cp_parser_parse_tentatively (parser);
17354 /* Try a class-name. */
17355 type_decl = cp_parser_class_name (parser,
17356 typename_keyword_p,
17357 /*template_keyword_p=*/false,
17358 none_type,
17359 /*check_dependency_p=*/true,
17360 /*class_head_p=*/false,
17361 /*is_declaration=*/false);
17362 /* If it's not a class-name, keep looking. */
17363 if (!cp_parser_parse_definitely (parser))
17365 if (cxx_dialect < cxx11)
17366 /* It must be a typedef-name or an enum-name. */
17367 return cp_parser_nonclass_name (parser);
17369 cp_parser_parse_tentatively (parser);
17370 /* It is either a simple-template-id representing an
17371 instantiation of an alias template... */
17372 type_decl = cp_parser_template_id (parser,
17373 /*template_keyword_p=*/false,
17374 /*check_dependency_p=*/true,
17375 none_type,
17376 /*is_declaration=*/false);
17377 /* Note that this must be an instantiation of an alias template
17378 because [temp.names]/6 says:
17380 A template-id that names an alias template specialization
17381 is a type-name.
17383 Whereas [temp.names]/7 says:
17385 A simple-template-id that names a class template
17386 specialization is a class-name.
17388 With concepts, this could also be a partial-concept-id that
17389 declares a non-type template parameter. */
17390 if (type_decl != NULL_TREE
17391 && TREE_CODE (type_decl) == TYPE_DECL
17392 && TYPE_DECL_ALIAS_P (type_decl))
17393 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17394 else if (is_constrained_parameter (type_decl))
17395 /* Don't do anything. */ ;
17396 else
17397 cp_parser_simulate_error (parser);
17399 if (!cp_parser_parse_definitely (parser))
17400 /* ... Or a typedef-name or an enum-name. */
17401 return cp_parser_nonclass_name (parser);
17404 return type_decl;
17407 /* Check if DECL and ARGS can form a constrained-type-specifier.
17408 If ARGS is non-null, we try to form a concept check of the
17409 form DECL<?, ARGS> where ? is a wildcard that matches any
17410 kind of template argument. If ARGS is NULL, then we try to
17411 form a concept check of the form DECL<?>. */
17413 static tree
17414 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17415 tree decl, tree args)
17417 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17419 /* If we a constrained-type-specifier cannot be deduced. */
17420 if (parser->prevent_constrained_type_specifiers)
17421 return NULL_TREE;
17423 /* A constrained type specifier can only be found in an
17424 overload set or as a reference to a template declaration.
17426 FIXME: This might be masking a bug. It's possible that
17427 that the deduction below is causing template specializations
17428 to be formed with the wildcard as an argument. */
17429 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17430 return NULL_TREE;
17432 /* Try to build a call expression that evaluates the
17433 concept. This can fail if the overload set refers
17434 only to non-templates. */
17435 tree placeholder = build_nt (WILDCARD_DECL);
17436 tree check = build_concept_check (decl, placeholder, args);
17437 if (check == error_mark_node)
17438 return NULL_TREE;
17440 /* Deduce the checked constraint and the prototype parameter.
17442 FIXME: In certain cases, failure to deduce should be a
17443 diagnosable error. */
17444 tree conc;
17445 tree proto;
17446 if (!deduce_constrained_parameter (check, conc, proto))
17447 return NULL_TREE;
17449 /* In template parameter scope, this results in a constrained
17450 parameter. Return a descriptor of that parm. */
17451 if (processing_template_parmlist)
17452 return build_constrained_parameter (conc, proto, args);
17454 /* In a parameter-declaration-clause, constrained-type
17455 specifiers result in invented template parameters. */
17456 if (parser->auto_is_implicit_function_template_parm_p)
17458 tree x = build_constrained_parameter (conc, proto, args);
17459 return synthesize_implicit_template_parm (parser, x);
17461 else
17463 /* Otherwise, we're in a context where the constrained
17464 type name is deduced and the constraint applies
17465 after deduction. */
17466 return make_constrained_auto (conc, args);
17469 return NULL_TREE;
17472 /* If DECL refers to a concept, return a TYPE_DECL representing
17473 the result of using the constrained type specifier in the
17474 current context. DECL refers to a concept if
17476 - it is an overload set containing a function concept taking a single
17477 type argument, or
17479 - it is a variable concept taking a single type argument. */
17481 static tree
17482 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17484 if (flag_concepts
17485 && (TREE_CODE (decl) == OVERLOAD
17486 || BASELINK_P (decl)
17487 || variable_concept_p (decl)))
17488 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17489 else
17490 return NULL_TREE;
17493 /* Check if DECL and ARGS form a partial-concept-id. If so,
17494 assign ID to the resulting constrained placeholder.
17496 Returns true if the partial-concept-id designates a placeholder
17497 and false otherwise. Note that *id is set to NULL_TREE in
17498 this case. */
17500 static tree
17501 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17503 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17506 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17507 or a concept-name.
17509 enum-name:
17510 identifier
17512 typedef-name:
17513 identifier
17515 concept-name:
17516 identifier
17518 Returns a TYPE_DECL for the type. */
17520 static tree
17521 cp_parser_nonclass_name (cp_parser* parser)
17523 tree type_decl;
17524 tree identifier;
17526 cp_token *token = cp_lexer_peek_token (parser->lexer);
17527 identifier = cp_parser_identifier (parser);
17528 if (identifier == error_mark_node)
17529 return error_mark_node;
17531 /* Look up the type-name. */
17532 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17534 type_decl = strip_using_decl (type_decl);
17536 /* If we found an overload set, then it may refer to a concept-name. */
17537 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17538 type_decl = decl;
17540 if (TREE_CODE (type_decl) != TYPE_DECL
17541 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17543 /* See if this is an Objective-C type. */
17544 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17545 tree type = objc_get_protocol_qualified_type (identifier, protos);
17546 if (type)
17547 type_decl = TYPE_NAME (type);
17550 /* Issue an error if we did not find a type-name. */
17551 if (TREE_CODE (type_decl) != TYPE_DECL
17552 /* In Objective-C, we have the complication that class names are
17553 normally type names and start declarations (eg, the
17554 "NSObject" in "NSObject *object;"), but can be used in an
17555 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17556 is an expression. So, a classname followed by a dot is not a
17557 valid type-name. */
17558 || (objc_is_class_name (TREE_TYPE (type_decl))
17559 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17561 if (!cp_parser_simulate_error (parser))
17562 cp_parser_name_lookup_error (parser, identifier, type_decl,
17563 NLE_TYPE, token->location);
17564 return error_mark_node;
17566 /* Remember that the name was used in the definition of the
17567 current class so that we can check later to see if the
17568 meaning would have been different after the class was
17569 entirely defined. */
17570 else if (type_decl != error_mark_node
17571 && !parser->scope)
17572 maybe_note_name_used_in_class (identifier, type_decl);
17574 return type_decl;
17577 /* Parse an elaborated-type-specifier. Note that the grammar given
17578 here incorporates the resolution to DR68.
17580 elaborated-type-specifier:
17581 class-key :: [opt] nested-name-specifier [opt] identifier
17582 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17583 enum-key :: [opt] nested-name-specifier [opt] identifier
17584 typename :: [opt] nested-name-specifier identifier
17585 typename :: [opt] nested-name-specifier template [opt]
17586 template-id
17588 GNU extension:
17590 elaborated-type-specifier:
17591 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17592 class-key attributes :: [opt] nested-name-specifier [opt]
17593 template [opt] template-id
17594 enum attributes :: [opt] nested-name-specifier [opt] identifier
17596 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17597 declared `friend'. If IS_DECLARATION is TRUE, then this
17598 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17599 something is being declared.
17601 Returns the TYPE specified. */
17603 static tree
17604 cp_parser_elaborated_type_specifier (cp_parser* parser,
17605 bool is_friend,
17606 bool is_declaration)
17608 enum tag_types tag_type;
17609 tree identifier;
17610 tree type = NULL_TREE;
17611 tree attributes = NULL_TREE;
17612 tree globalscope;
17613 cp_token *token = NULL;
17615 /* See if we're looking at the `enum' keyword. */
17616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17618 /* Consume the `enum' token. */
17619 cp_lexer_consume_token (parser->lexer);
17620 /* Remember that it's an enumeration type. */
17621 tag_type = enum_type;
17622 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17623 enums) is used here. */
17624 cp_token *token = cp_lexer_peek_token (parser->lexer);
17625 if (cp_parser_is_keyword (token, RID_CLASS)
17626 || cp_parser_is_keyword (token, RID_STRUCT))
17628 gcc_rich_location richloc (token->location);
17629 richloc.add_range (input_location, false);
17630 richloc.add_fixit_remove ();
17631 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17632 "a scoped enum must not use the %qD keyword",
17633 token->u.value);
17634 /* Consume the `struct' or `class' and parse it anyway. */
17635 cp_lexer_consume_token (parser->lexer);
17637 /* Parse the attributes. */
17638 attributes = cp_parser_attributes_opt (parser);
17640 /* Or, it might be `typename'. */
17641 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17642 RID_TYPENAME))
17644 /* Consume the `typename' token. */
17645 cp_lexer_consume_token (parser->lexer);
17646 /* Remember that it's a `typename' type. */
17647 tag_type = typename_type;
17649 /* Otherwise it must be a class-key. */
17650 else
17652 tag_type = cp_parser_class_key (parser);
17653 if (tag_type == none_type)
17654 return error_mark_node;
17655 /* Parse the attributes. */
17656 attributes = cp_parser_attributes_opt (parser);
17659 /* Look for the `::' operator. */
17660 globalscope = cp_parser_global_scope_opt (parser,
17661 /*current_scope_valid_p=*/false);
17662 /* Look for the nested-name-specifier. */
17663 tree nested_name_specifier;
17664 if (tag_type == typename_type && !globalscope)
17666 nested_name_specifier
17667 = cp_parser_nested_name_specifier (parser,
17668 /*typename_keyword_p=*/true,
17669 /*check_dependency_p=*/true,
17670 /*type_p=*/true,
17671 is_declaration);
17672 if (!nested_name_specifier)
17673 return error_mark_node;
17675 else
17676 /* Even though `typename' is not present, the proposed resolution
17677 to Core Issue 180 says that in `class A<T>::B', `B' should be
17678 considered a type-name, even if `A<T>' is dependent. */
17679 nested_name_specifier
17680 = cp_parser_nested_name_specifier_opt (parser,
17681 /*typename_keyword_p=*/true,
17682 /*check_dependency_p=*/true,
17683 /*type_p=*/true,
17684 is_declaration);
17685 /* For everything but enumeration types, consider a template-id.
17686 For an enumeration type, consider only a plain identifier. */
17687 if (tag_type != enum_type)
17689 bool template_p = false;
17690 tree decl;
17692 /* Allow the `template' keyword. */
17693 template_p = cp_parser_optional_template_keyword (parser);
17694 /* If we didn't see `template', we don't know if there's a
17695 template-id or not. */
17696 if (!template_p)
17697 cp_parser_parse_tentatively (parser);
17698 /* Parse the template-id. */
17699 token = cp_lexer_peek_token (parser->lexer);
17700 decl = cp_parser_template_id (parser, template_p,
17701 /*check_dependency_p=*/true,
17702 tag_type,
17703 is_declaration);
17704 /* If we didn't find a template-id, look for an ordinary
17705 identifier. */
17706 if (!template_p && !cp_parser_parse_definitely (parser))
17708 /* We can get here when cp_parser_template_id, called by
17709 cp_parser_class_name with tag_type == none_type, succeeds
17710 and caches a BASELINK. Then, when called again here,
17711 instead of failing and returning an error_mark_node
17712 returns it (see template/typename17.C in C++11).
17713 ??? Could we diagnose this earlier? */
17714 else if (tag_type == typename_type && BASELINK_P (decl))
17716 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17717 type = error_mark_node;
17719 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17720 in effect, then we must assume that, upon instantiation, the
17721 template will correspond to a class. */
17722 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17723 && tag_type == typename_type)
17724 type = make_typename_type (parser->scope, decl,
17725 typename_type,
17726 /*complain=*/tf_error);
17727 /* If the `typename' keyword is in effect and DECL is not a type
17728 decl, then type is non existent. */
17729 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17731 else if (TREE_CODE (decl) == TYPE_DECL)
17733 type = check_elaborated_type_specifier (tag_type, decl,
17734 /*allow_template_p=*/true);
17736 /* If the next token is a semicolon, this must be a specialization,
17737 instantiation, or friend declaration. Check the scope while we
17738 still know whether or not we had a nested-name-specifier. */
17739 if (type != error_mark_node
17740 && !nested_name_specifier && !is_friend
17741 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17742 check_unqualified_spec_or_inst (type, token->location);
17744 else if (decl == error_mark_node)
17745 type = error_mark_node;
17748 if (!type)
17750 token = cp_lexer_peek_token (parser->lexer);
17751 identifier = cp_parser_identifier (parser);
17753 if (identifier == error_mark_node)
17755 parser->scope = NULL_TREE;
17756 return error_mark_node;
17759 /* For a `typename', we needn't call xref_tag. */
17760 if (tag_type == typename_type
17761 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17762 return cp_parser_make_typename_type (parser, identifier,
17763 token->location);
17765 /* Template parameter lists apply only if we are not within a
17766 function parameter list. */
17767 bool template_parm_lists_apply
17768 = parser->num_template_parameter_lists;
17769 if (template_parm_lists_apply)
17770 for (cp_binding_level *s = current_binding_level;
17771 s && s->kind != sk_template_parms;
17772 s = s->level_chain)
17773 if (s->kind == sk_function_parms)
17774 template_parm_lists_apply = false;
17776 /* Look up a qualified name in the usual way. */
17777 if (parser->scope)
17779 tree decl;
17780 tree ambiguous_decls;
17782 decl = cp_parser_lookup_name (parser, identifier,
17783 tag_type,
17784 /*is_template=*/false,
17785 /*is_namespace=*/false,
17786 /*check_dependency=*/true,
17787 &ambiguous_decls,
17788 token->location);
17790 /* If the lookup was ambiguous, an error will already have been
17791 issued. */
17792 if (ambiguous_decls)
17793 return error_mark_node;
17795 /* If we are parsing friend declaration, DECL may be a
17796 TEMPLATE_DECL tree node here. However, we need to check
17797 whether this TEMPLATE_DECL results in valid code. Consider
17798 the following example:
17800 namespace N {
17801 template <class T> class C {};
17803 class X {
17804 template <class T> friend class N::C; // #1, valid code
17806 template <class T> class Y {
17807 friend class N::C; // #2, invalid code
17810 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17811 name lookup of `N::C'. We see that friend declaration must
17812 be template for the code to be valid. Note that
17813 processing_template_decl does not work here since it is
17814 always 1 for the above two cases. */
17816 decl = (cp_parser_maybe_treat_template_as_class
17817 (decl, /*tag_name_p=*/is_friend
17818 && template_parm_lists_apply));
17820 if (TREE_CODE (decl) != TYPE_DECL)
17822 cp_parser_diagnose_invalid_type_name (parser,
17823 identifier,
17824 token->location);
17825 return error_mark_node;
17828 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17830 bool allow_template = (template_parm_lists_apply
17831 || DECL_SELF_REFERENCE_P (decl));
17832 type = check_elaborated_type_specifier (tag_type, decl,
17833 allow_template);
17835 if (type == error_mark_node)
17836 return error_mark_node;
17839 /* Forward declarations of nested types, such as
17841 class C1::C2;
17842 class C1::C2::C3;
17844 are invalid unless all components preceding the final '::'
17845 are complete. If all enclosing types are complete, these
17846 declarations become merely pointless.
17848 Invalid forward declarations of nested types are errors
17849 caught elsewhere in parsing. Those that are pointless arrive
17850 here. */
17852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17853 && !is_friend && !processing_explicit_instantiation)
17854 warning (0, "declaration %qD does not declare anything", decl);
17856 type = TREE_TYPE (decl);
17858 else
17860 /* An elaborated-type-specifier sometimes introduces a new type and
17861 sometimes names an existing type. Normally, the rule is that it
17862 introduces a new type only if there is not an existing type of
17863 the same name already in scope. For example, given:
17865 struct S {};
17866 void f() { struct S s; }
17868 the `struct S' in the body of `f' is the same `struct S' as in
17869 the global scope; the existing definition is used. However, if
17870 there were no global declaration, this would introduce a new
17871 local class named `S'.
17873 An exception to this rule applies to the following code:
17875 namespace N { struct S; }
17877 Here, the elaborated-type-specifier names a new type
17878 unconditionally; even if there is already an `S' in the
17879 containing scope this declaration names a new type.
17880 This exception only applies if the elaborated-type-specifier
17881 forms the complete declaration:
17883 [class.name]
17885 A declaration consisting solely of `class-key identifier ;' is
17886 either a redeclaration of the name in the current scope or a
17887 forward declaration of the identifier as a class name. It
17888 introduces the name into the current scope.
17890 We are in this situation precisely when the next token is a `;'.
17892 An exception to the exception is that a `friend' declaration does
17893 *not* name a new type; i.e., given:
17895 struct S { friend struct T; };
17897 `T' is not a new type in the scope of `S'.
17899 Also, `new struct S' or `sizeof (struct S)' never results in the
17900 definition of a new type; a new type can only be declared in a
17901 declaration context. */
17903 tag_scope ts;
17904 bool template_p;
17906 if (is_friend)
17907 /* Friends have special name lookup rules. */
17908 ts = ts_within_enclosing_non_class;
17909 else if (is_declaration
17910 && cp_lexer_next_token_is (parser->lexer,
17911 CPP_SEMICOLON))
17912 /* This is a `class-key identifier ;' */
17913 ts = ts_current;
17914 else
17915 ts = ts_global;
17917 template_p =
17918 (template_parm_lists_apply
17919 && (cp_parser_next_token_starts_class_definition_p (parser)
17920 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17921 /* An unqualified name was used to reference this type, so
17922 there were no qualifying templates. */
17923 if (template_parm_lists_apply
17924 && !cp_parser_check_template_parameters (parser,
17925 /*num_templates=*/0,
17926 /*template_id*/false,
17927 token->location,
17928 /*declarator=*/NULL))
17929 return error_mark_node;
17930 type = xref_tag (tag_type, identifier, ts, template_p);
17934 if (type == error_mark_node)
17935 return error_mark_node;
17937 /* Allow attributes on forward declarations of classes. */
17938 if (attributes)
17940 if (TREE_CODE (type) == TYPENAME_TYPE)
17941 warning (OPT_Wattributes,
17942 "attributes ignored on uninstantiated type");
17943 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17944 && ! processing_explicit_instantiation)
17945 warning (OPT_Wattributes,
17946 "attributes ignored on template instantiation");
17947 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17948 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17949 else
17950 warning (OPT_Wattributes,
17951 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17954 if (tag_type != enum_type)
17956 /* Indicate whether this class was declared as a `class' or as a
17957 `struct'. */
17958 if (CLASS_TYPE_P (type))
17959 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17960 cp_parser_check_class_key (tag_type, type);
17963 /* A "<" cannot follow an elaborated type specifier. If that
17964 happens, the user was probably trying to form a template-id. */
17965 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17966 token->location);
17968 return type;
17971 /* Parse an enum-specifier.
17973 enum-specifier:
17974 enum-head { enumerator-list [opt] }
17975 enum-head { enumerator-list , } [C++0x]
17977 enum-head:
17978 enum-key identifier [opt] enum-base [opt]
17979 enum-key nested-name-specifier identifier enum-base [opt]
17981 enum-key:
17982 enum
17983 enum class [C++0x]
17984 enum struct [C++0x]
17986 enum-base: [C++0x]
17987 : type-specifier-seq
17989 opaque-enum-specifier:
17990 enum-key identifier enum-base [opt] ;
17992 GNU Extensions:
17993 enum-key attributes[opt] identifier [opt] enum-base [opt]
17994 { enumerator-list [opt] }attributes[opt]
17995 enum-key attributes[opt] identifier [opt] enum-base [opt]
17996 { enumerator-list, }attributes[opt] [C++0x]
17998 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17999 if the token stream isn't an enum-specifier after all. */
18001 static tree
18002 cp_parser_enum_specifier (cp_parser* parser)
18004 tree identifier;
18005 tree type = NULL_TREE;
18006 tree prev_scope;
18007 tree nested_name_specifier = NULL_TREE;
18008 tree attributes;
18009 bool scoped_enum_p = false;
18010 bool has_underlying_type = false;
18011 bool nested_being_defined = false;
18012 bool new_value_list = false;
18013 bool is_new_type = false;
18014 bool is_unnamed = false;
18015 tree underlying_type = NULL_TREE;
18016 cp_token *type_start_token = NULL;
18017 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18019 parser->colon_corrects_to_scope_p = false;
18021 /* Parse tentatively so that we can back up if we don't find a
18022 enum-specifier. */
18023 cp_parser_parse_tentatively (parser);
18025 /* Caller guarantees that the current token is 'enum', an identifier
18026 possibly follows, and the token after that is an opening brace.
18027 If we don't have an identifier, fabricate an anonymous name for
18028 the enumeration being defined. */
18029 cp_lexer_consume_token (parser->lexer);
18031 /* Parse the "class" or "struct", which indicates a scoped
18032 enumeration type in C++0x. */
18033 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18034 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18036 if (cxx_dialect < cxx11)
18037 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18039 /* Consume the `struct' or `class' token. */
18040 cp_lexer_consume_token (parser->lexer);
18042 scoped_enum_p = true;
18045 attributes = cp_parser_attributes_opt (parser);
18047 /* Clear the qualification. */
18048 parser->scope = NULL_TREE;
18049 parser->qualifying_scope = NULL_TREE;
18050 parser->object_scope = NULL_TREE;
18052 /* Figure out in what scope the declaration is being placed. */
18053 prev_scope = current_scope ();
18055 type_start_token = cp_lexer_peek_token (parser->lexer);
18057 push_deferring_access_checks (dk_no_check);
18058 nested_name_specifier
18059 = cp_parser_nested_name_specifier_opt (parser,
18060 /*typename_keyword_p=*/true,
18061 /*check_dependency_p=*/false,
18062 /*type_p=*/false,
18063 /*is_declaration=*/false);
18065 if (nested_name_specifier)
18067 tree name;
18069 identifier = cp_parser_identifier (parser);
18070 name = cp_parser_lookup_name (parser, identifier,
18071 enum_type,
18072 /*is_template=*/false,
18073 /*is_namespace=*/false,
18074 /*check_dependency=*/true,
18075 /*ambiguous_decls=*/NULL,
18076 input_location);
18077 if (name && name != error_mark_node)
18079 type = TREE_TYPE (name);
18080 if (TREE_CODE (type) == TYPENAME_TYPE)
18082 /* Are template enums allowed in ISO? */
18083 if (template_parm_scope_p ())
18084 pedwarn (type_start_token->location, OPT_Wpedantic,
18085 "%qD is an enumeration template", name);
18086 /* ignore a typename reference, for it will be solved by name
18087 in start_enum. */
18088 type = NULL_TREE;
18091 else if (nested_name_specifier == error_mark_node)
18092 /* We already issued an error. */;
18093 else
18095 error_at (type_start_token->location,
18096 "%qD does not name an enumeration in %qT",
18097 identifier, nested_name_specifier);
18098 nested_name_specifier = error_mark_node;
18101 else
18103 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18104 identifier = cp_parser_identifier (parser);
18105 else
18107 identifier = make_anon_name ();
18108 is_unnamed = true;
18109 if (scoped_enum_p)
18110 error_at (type_start_token->location,
18111 "unnamed scoped enum is not allowed");
18114 pop_deferring_access_checks ();
18116 /* Check for the `:' that denotes a specified underlying type in C++0x.
18117 Note that a ':' could also indicate a bitfield width, however. */
18118 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18120 cp_decl_specifier_seq type_specifiers;
18122 /* Consume the `:'. */
18123 cp_lexer_consume_token (parser->lexer);
18125 /* Parse the type-specifier-seq. */
18126 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18127 /*is_trailing_return=*/false,
18128 &type_specifiers);
18130 /* At this point this is surely not elaborated type specifier. */
18131 if (!cp_parser_parse_definitely (parser))
18132 return NULL_TREE;
18134 if (cxx_dialect < cxx11)
18135 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18137 has_underlying_type = true;
18139 /* If that didn't work, stop. */
18140 if (type_specifiers.type != error_mark_node)
18142 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18143 /*initialized=*/0, NULL);
18144 if (underlying_type == error_mark_node
18145 || check_for_bare_parameter_packs (underlying_type))
18146 underlying_type = NULL_TREE;
18150 /* Look for the `{' but don't consume it yet. */
18151 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18153 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18155 cp_parser_error (parser, "expected %<{%>");
18156 if (has_underlying_type)
18158 type = NULL_TREE;
18159 goto out;
18162 /* An opaque-enum-specifier must have a ';' here. */
18163 if ((scoped_enum_p || underlying_type)
18164 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18166 cp_parser_error (parser, "expected %<;%> or %<{%>");
18167 if (has_underlying_type)
18169 type = NULL_TREE;
18170 goto out;
18175 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18176 return NULL_TREE;
18178 if (nested_name_specifier)
18180 if (CLASS_TYPE_P (nested_name_specifier))
18182 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18183 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18184 push_scope (nested_name_specifier);
18186 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18188 push_nested_namespace (nested_name_specifier);
18192 /* Issue an error message if type-definitions are forbidden here. */
18193 if (!cp_parser_check_type_definition (parser))
18194 type = error_mark_node;
18195 else
18196 /* Create the new type. We do this before consuming the opening
18197 brace so the enum will be recorded as being on the line of its
18198 tag (or the 'enum' keyword, if there is no tag). */
18199 type = start_enum (identifier, type, underlying_type,
18200 attributes, scoped_enum_p, &is_new_type);
18202 /* If the next token is not '{' it is an opaque-enum-specifier or an
18203 elaborated-type-specifier. */
18204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18206 timevar_push (TV_PARSE_ENUM);
18207 if (nested_name_specifier
18208 && nested_name_specifier != error_mark_node)
18210 /* The following catches invalid code such as:
18211 enum class S<int>::E { A, B, C }; */
18212 if (!processing_specialization
18213 && CLASS_TYPE_P (nested_name_specifier)
18214 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18215 error_at (type_start_token->location, "cannot add an enumerator "
18216 "list to a template instantiation");
18218 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18220 error_at (type_start_token->location,
18221 "%<%T::%E%> has not been declared",
18222 TYPE_CONTEXT (nested_name_specifier),
18223 nested_name_specifier);
18224 type = error_mark_node;
18226 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18227 && !CLASS_TYPE_P (nested_name_specifier))
18229 error_at (type_start_token->location, "nested name specifier "
18230 "%qT for enum declaration does not name a class "
18231 "or namespace", nested_name_specifier);
18232 type = error_mark_node;
18234 /* If that scope does not contain the scope in which the
18235 class was originally declared, the program is invalid. */
18236 else if (prev_scope && !is_ancestor (prev_scope,
18237 nested_name_specifier))
18239 if (at_namespace_scope_p ())
18240 error_at (type_start_token->location,
18241 "declaration of %qD in namespace %qD which does not "
18242 "enclose %qD",
18243 type, prev_scope, nested_name_specifier);
18244 else
18245 error_at (type_start_token->location,
18246 "declaration of %qD in %qD which does not "
18247 "enclose %qD",
18248 type, prev_scope, nested_name_specifier);
18249 type = error_mark_node;
18251 /* If that scope is the scope where the declaration is being placed
18252 the program is invalid. */
18253 else if (CLASS_TYPE_P (nested_name_specifier)
18254 && CLASS_TYPE_P (prev_scope)
18255 && same_type_p (nested_name_specifier, prev_scope))
18257 permerror (type_start_token->location,
18258 "extra qualification not allowed");
18259 nested_name_specifier = NULL_TREE;
18263 if (scoped_enum_p)
18264 begin_scope (sk_scoped_enum, type);
18266 /* Consume the opening brace. */
18267 matching_braces braces;
18268 braces.consume_open (parser);
18270 if (type == error_mark_node)
18271 ; /* Nothing to add */
18272 else if (OPAQUE_ENUM_P (type)
18273 || (cxx_dialect > cxx98 && processing_specialization))
18275 new_value_list = true;
18276 SET_OPAQUE_ENUM_P (type, false);
18277 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18279 else
18281 error_at (type_start_token->location,
18282 "multiple definition of %q#T", type);
18283 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18284 "previous definition here");
18285 type = error_mark_node;
18288 if (type == error_mark_node)
18289 cp_parser_skip_to_end_of_block_or_statement (parser);
18290 /* If the next token is not '}', then there are some enumerators. */
18291 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18293 if (is_unnamed && !scoped_enum_p)
18294 pedwarn (type_start_token->location, OPT_Wpedantic,
18295 "ISO C++ forbids empty unnamed enum");
18297 else
18298 cp_parser_enumerator_list (parser, type);
18300 /* Consume the final '}'. */
18301 braces.require_close (parser);
18303 if (scoped_enum_p)
18304 finish_scope ();
18305 timevar_pop (TV_PARSE_ENUM);
18307 else
18309 /* If a ';' follows, then it is an opaque-enum-specifier
18310 and additional restrictions apply. */
18311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18313 if (is_unnamed)
18314 error_at (type_start_token->location,
18315 "opaque-enum-specifier without name");
18316 else if (nested_name_specifier)
18317 error_at (type_start_token->location,
18318 "opaque-enum-specifier must use a simple identifier");
18322 /* Look for trailing attributes to apply to this enumeration, and
18323 apply them if appropriate. */
18324 if (cp_parser_allow_gnu_extensions_p (parser))
18326 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18327 cplus_decl_attributes (&type,
18328 trailing_attr,
18329 (int) ATTR_FLAG_TYPE_IN_PLACE);
18332 /* Finish up the enumeration. */
18333 if (type != error_mark_node)
18335 if (new_value_list)
18336 finish_enum_value_list (type);
18337 if (is_new_type)
18338 finish_enum (type);
18341 if (nested_name_specifier)
18343 if (CLASS_TYPE_P (nested_name_specifier))
18345 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18346 pop_scope (nested_name_specifier);
18348 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18350 pop_nested_namespace (nested_name_specifier);
18353 out:
18354 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18355 return type;
18358 /* Parse an enumerator-list. The enumerators all have the indicated
18359 TYPE.
18361 enumerator-list:
18362 enumerator-definition
18363 enumerator-list , enumerator-definition */
18365 static void
18366 cp_parser_enumerator_list (cp_parser* parser, tree type)
18368 while (true)
18370 /* Parse an enumerator-definition. */
18371 cp_parser_enumerator_definition (parser, type);
18373 /* If the next token is not a ',', we've reached the end of
18374 the list. */
18375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18376 break;
18377 /* Otherwise, consume the `,' and keep going. */
18378 cp_lexer_consume_token (parser->lexer);
18379 /* If the next token is a `}', there is a trailing comma. */
18380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18382 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18383 pedwarn (input_location, OPT_Wpedantic,
18384 "comma at end of enumerator list");
18385 break;
18390 /* Parse an enumerator-definition. The enumerator has the indicated
18391 TYPE.
18393 enumerator-definition:
18394 enumerator
18395 enumerator = constant-expression
18397 enumerator:
18398 identifier
18400 GNU Extensions:
18402 enumerator-definition:
18403 enumerator attributes [opt]
18404 enumerator attributes [opt] = constant-expression */
18406 static void
18407 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18409 tree identifier;
18410 tree value;
18411 location_t loc;
18413 /* Save the input location because we are interested in the location
18414 of the identifier and not the location of the explicit value. */
18415 loc = cp_lexer_peek_token (parser->lexer)->location;
18417 /* Look for the identifier. */
18418 identifier = cp_parser_identifier (parser);
18419 if (identifier == error_mark_node)
18420 return;
18422 /* Parse any specified attributes. */
18423 tree attrs = cp_parser_attributes_opt (parser);
18425 /* If the next token is an '=', then there is an explicit value. */
18426 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18428 /* Consume the `=' token. */
18429 cp_lexer_consume_token (parser->lexer);
18430 /* Parse the value. */
18431 value = cp_parser_constant_expression (parser);
18433 else
18434 value = NULL_TREE;
18436 /* If we are processing a template, make sure the initializer of the
18437 enumerator doesn't contain any bare template parameter pack. */
18438 if (check_for_bare_parameter_packs (value))
18439 value = error_mark_node;
18441 /* Create the enumerator. */
18442 build_enumerator (identifier, value, type, attrs, loc);
18445 /* Parse a namespace-name.
18447 namespace-name:
18448 original-namespace-name
18449 namespace-alias
18451 Returns the NAMESPACE_DECL for the namespace. */
18453 static tree
18454 cp_parser_namespace_name (cp_parser* parser)
18456 tree identifier;
18457 tree namespace_decl;
18459 cp_token *token = cp_lexer_peek_token (parser->lexer);
18461 /* Get the name of the namespace. */
18462 identifier = cp_parser_identifier (parser);
18463 if (identifier == error_mark_node)
18464 return error_mark_node;
18466 /* Look up the identifier in the currently active scope. Look only
18467 for namespaces, due to:
18469 [basic.lookup.udir]
18471 When looking up a namespace-name in a using-directive or alias
18472 definition, only namespace names are considered.
18474 And:
18476 [basic.lookup.qual]
18478 During the lookup of a name preceding the :: scope resolution
18479 operator, object, function, and enumerator names are ignored.
18481 (Note that cp_parser_qualifying_entity only calls this
18482 function if the token after the name is the scope resolution
18483 operator.) */
18484 namespace_decl = cp_parser_lookup_name (parser, identifier,
18485 none_type,
18486 /*is_template=*/false,
18487 /*is_namespace=*/true,
18488 /*check_dependency=*/true,
18489 /*ambiguous_decls=*/NULL,
18490 token->location);
18491 /* If it's not a namespace, issue an error. */
18492 if (namespace_decl == error_mark_node
18493 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18495 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18497 error_at (token->location, "%qD is not a namespace-name", identifier);
18498 if (namespace_decl == error_mark_node
18499 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18500 suggest_alternative_in_explicit_scope (token->location, identifier,
18501 parser->scope);
18503 cp_parser_error (parser, "expected namespace-name");
18504 namespace_decl = error_mark_node;
18507 return namespace_decl;
18510 /* Parse a namespace-definition.
18512 namespace-definition:
18513 named-namespace-definition
18514 unnamed-namespace-definition
18516 named-namespace-definition:
18517 original-namespace-definition
18518 extension-namespace-definition
18520 original-namespace-definition:
18521 namespace identifier { namespace-body }
18523 extension-namespace-definition:
18524 namespace original-namespace-name { namespace-body }
18526 unnamed-namespace-definition:
18527 namespace { namespace-body } */
18529 static void
18530 cp_parser_namespace_definition (cp_parser* parser)
18532 tree identifier;
18533 int nested_definition_count = 0;
18535 cp_ensure_no_omp_declare_simd (parser);
18536 cp_ensure_no_oacc_routine (parser);
18538 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18540 if (is_inline)
18542 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18543 cp_lexer_consume_token (parser->lexer);
18546 /* Look for the `namespace' keyword. */
18547 cp_token* token
18548 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18550 /* Parse any specified attributes before the identifier. */
18551 tree attribs = cp_parser_attributes_opt (parser);
18553 for (;;)
18555 identifier = NULL_TREE;
18557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18559 identifier = cp_parser_identifier (parser);
18561 /* Parse any attributes specified after the identifier. */
18562 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18566 break;
18568 if (!nested_definition_count && cxx_dialect < cxx17)
18569 pedwarn (input_location, OPT_Wpedantic,
18570 "nested namespace definitions only available with "
18571 "-std=c++17 or -std=gnu++17");
18573 /* Nested namespace names can create new namespaces (unlike
18574 other qualified-ids). */
18575 if (int count = identifier ? push_namespace (identifier) : 0)
18576 nested_definition_count += count;
18577 else
18578 cp_parser_error (parser, "nested namespace name required");
18579 cp_lexer_consume_token (parser->lexer);
18582 if (nested_definition_count && !identifier)
18583 cp_parser_error (parser, "namespace name required");
18585 if (nested_definition_count && attribs)
18586 error_at (token->location,
18587 "a nested namespace definition cannot have attributes");
18588 if (nested_definition_count && is_inline)
18589 error_at (token->location,
18590 "a nested namespace definition cannot be inline");
18592 /* Start the namespace. */
18593 nested_definition_count += push_namespace (identifier, is_inline);
18595 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18597 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18599 /* Look for the `{' to validate starting the namespace. */
18600 matching_braces braces;
18601 if (braces.require_open (parser))
18603 /* Parse the body of the namespace. */
18604 cp_parser_namespace_body (parser);
18606 /* Look for the final `}'. */
18607 braces.require_close (parser);
18610 if (has_visibility)
18611 pop_visibility (1);
18613 /* Pop the nested namespace definitions. */
18614 while (nested_definition_count--)
18615 pop_namespace ();
18618 /* Parse a namespace-body.
18620 namespace-body:
18621 declaration-seq [opt] */
18623 static void
18624 cp_parser_namespace_body (cp_parser* parser)
18626 cp_parser_declaration_seq_opt (parser);
18629 /* Parse a namespace-alias-definition.
18631 namespace-alias-definition:
18632 namespace identifier = qualified-namespace-specifier ; */
18634 static void
18635 cp_parser_namespace_alias_definition (cp_parser* parser)
18637 tree identifier;
18638 tree namespace_specifier;
18640 cp_token *token = cp_lexer_peek_token (parser->lexer);
18642 /* Look for the `namespace' keyword. */
18643 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18644 /* Look for the identifier. */
18645 identifier = cp_parser_identifier (parser);
18646 if (identifier == error_mark_node)
18647 return;
18648 /* Look for the `=' token. */
18649 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18652 error_at (token->location, "%<namespace%> definition is not allowed here");
18653 /* Skip the definition. */
18654 cp_lexer_consume_token (parser->lexer);
18655 if (cp_parser_skip_to_closing_brace (parser))
18656 cp_lexer_consume_token (parser->lexer);
18657 return;
18659 cp_parser_require (parser, CPP_EQ, RT_EQ);
18660 /* Look for the qualified-namespace-specifier. */
18661 namespace_specifier
18662 = cp_parser_qualified_namespace_specifier (parser);
18663 /* Look for the `;' token. */
18664 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18666 /* Register the alias in the symbol table. */
18667 do_namespace_alias (identifier, namespace_specifier);
18670 /* Parse a qualified-namespace-specifier.
18672 qualified-namespace-specifier:
18673 :: [opt] nested-name-specifier [opt] namespace-name
18675 Returns a NAMESPACE_DECL corresponding to the specified
18676 namespace. */
18678 static tree
18679 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18681 /* Look for the optional `::'. */
18682 cp_parser_global_scope_opt (parser,
18683 /*current_scope_valid_p=*/false);
18685 /* Look for the optional nested-name-specifier. */
18686 cp_parser_nested_name_specifier_opt (parser,
18687 /*typename_keyword_p=*/false,
18688 /*check_dependency_p=*/true,
18689 /*type_p=*/false,
18690 /*is_declaration=*/true);
18692 return cp_parser_namespace_name (parser);
18695 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18696 access declaration.
18698 using-declaration:
18699 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18700 using :: unqualified-id ;
18702 access-declaration:
18703 qualified-id ;
18707 static bool
18708 cp_parser_using_declaration (cp_parser* parser,
18709 bool access_declaration_p)
18711 cp_token *token;
18712 bool typename_p = false;
18713 bool global_scope_p;
18714 tree decl;
18715 tree identifier;
18716 tree qscope;
18717 int oldcount = errorcount;
18718 cp_token *diag_token = NULL;
18720 if (access_declaration_p)
18722 diag_token = cp_lexer_peek_token (parser->lexer);
18723 cp_parser_parse_tentatively (parser);
18725 else
18727 /* Look for the `using' keyword. */
18728 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18730 again:
18731 /* Peek at the next token. */
18732 token = cp_lexer_peek_token (parser->lexer);
18733 /* See if it's `typename'. */
18734 if (token->keyword == RID_TYPENAME)
18736 /* Remember that we've seen it. */
18737 typename_p = true;
18738 /* Consume the `typename' token. */
18739 cp_lexer_consume_token (parser->lexer);
18743 /* Look for the optional global scope qualification. */
18744 global_scope_p
18745 = (cp_parser_global_scope_opt (parser,
18746 /*current_scope_valid_p=*/false)
18747 != NULL_TREE);
18749 /* If we saw `typename', or didn't see `::', then there must be a
18750 nested-name-specifier present. */
18751 if (typename_p || !global_scope_p)
18753 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18754 /*check_dependency_p=*/true,
18755 /*type_p=*/false,
18756 /*is_declaration=*/true);
18757 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18759 cp_parser_skip_to_end_of_block_or_statement (parser);
18760 return false;
18763 /* Otherwise, we could be in either of the two productions. In that
18764 case, treat the nested-name-specifier as optional. */
18765 else
18766 qscope = cp_parser_nested_name_specifier_opt (parser,
18767 /*typename_keyword_p=*/false,
18768 /*check_dependency_p=*/true,
18769 /*type_p=*/false,
18770 /*is_declaration=*/true);
18771 if (!qscope)
18772 qscope = global_namespace;
18773 else if (UNSCOPED_ENUM_P (qscope))
18774 qscope = CP_TYPE_CONTEXT (qscope);
18776 if (access_declaration_p && cp_parser_error_occurred (parser))
18777 /* Something has already gone wrong; there's no need to parse
18778 further. Since an error has occurred, the return value of
18779 cp_parser_parse_definitely will be false, as required. */
18780 return cp_parser_parse_definitely (parser);
18782 token = cp_lexer_peek_token (parser->lexer);
18783 /* Parse the unqualified-id. */
18784 identifier = cp_parser_unqualified_id (parser,
18785 /*template_keyword_p=*/false,
18786 /*check_dependency_p=*/true,
18787 /*declarator_p=*/true,
18788 /*optional_p=*/false);
18790 if (access_declaration_p)
18792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18793 cp_parser_simulate_error (parser);
18794 if (!cp_parser_parse_definitely (parser))
18795 return false;
18797 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18799 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18800 if (cxx_dialect < cxx17
18801 && !in_system_header_at (ell->location))
18802 pedwarn (ell->location, 0,
18803 "pack expansion in using-declaration only available "
18804 "with -std=c++17 or -std=gnu++17");
18805 qscope = make_pack_expansion (qscope);
18808 /* The function we call to handle a using-declaration is different
18809 depending on what scope we are in. */
18810 if (qscope == error_mark_node || identifier == error_mark_node)
18812 else if (!identifier_p (identifier)
18813 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18814 /* [namespace.udecl]
18816 A using declaration shall not name a template-id. */
18817 error_at (token->location,
18818 "a template-id may not appear in a using-declaration");
18819 else
18821 if (at_class_scope_p ())
18823 /* Create the USING_DECL. */
18824 decl = do_class_using_decl (qscope, identifier);
18826 if (decl && typename_p)
18827 USING_DECL_TYPENAME_P (decl) = 1;
18829 if (check_for_bare_parameter_packs (decl))
18831 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18832 return false;
18834 else
18835 /* Add it to the list of members in this class. */
18836 finish_member_declaration (decl);
18838 else
18840 decl = cp_parser_lookup_name_simple (parser,
18841 identifier,
18842 token->location);
18843 if (decl == error_mark_node)
18844 cp_parser_name_lookup_error (parser, identifier,
18845 decl, NLE_NULL,
18846 token->location);
18847 else if (check_for_bare_parameter_packs (decl))
18849 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18850 return false;
18852 else if (!at_namespace_scope_p ())
18853 finish_local_using_decl (decl, qscope, identifier);
18854 else
18855 finish_namespace_using_decl (decl, qscope, identifier);
18859 if (!access_declaration_p
18860 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18862 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18863 if (cxx_dialect < cxx17)
18864 pedwarn (comma->location, 0,
18865 "comma-separated list in using-declaration only available "
18866 "with -std=c++17 or -std=gnu++17");
18867 goto again;
18870 /* Look for the final `;'. */
18871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18873 if (access_declaration_p && errorcount == oldcount)
18874 warning_at (diag_token->location, OPT_Wdeprecated,
18875 "access declarations are deprecated "
18876 "in favour of using-declarations; "
18877 "suggestion: add the %<using%> keyword");
18879 return true;
18882 /* Parse an alias-declaration.
18884 alias-declaration:
18885 using identifier attribute-specifier-seq [opt] = type-id */
18887 static tree
18888 cp_parser_alias_declaration (cp_parser* parser)
18890 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18891 location_t id_location;
18892 cp_declarator *declarator;
18893 cp_decl_specifier_seq decl_specs;
18894 bool member_p;
18895 const char *saved_message = NULL;
18897 /* Look for the `using' keyword. */
18898 cp_token *using_token
18899 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18900 if (using_token == NULL)
18901 return error_mark_node;
18903 id_location = cp_lexer_peek_token (parser->lexer)->location;
18904 id = cp_parser_identifier (parser);
18905 if (id == error_mark_node)
18906 return error_mark_node;
18908 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18909 attributes = cp_parser_attributes_opt (parser);
18910 if (attributes == error_mark_node)
18911 return error_mark_node;
18913 cp_parser_require (parser, CPP_EQ, RT_EQ);
18915 if (cp_parser_error_occurred (parser))
18916 return error_mark_node;
18918 cp_parser_commit_to_tentative_parse (parser);
18920 /* Now we are going to parse the type-id of the declaration. */
18923 [dcl.type]/3 says:
18925 "A type-specifier-seq shall not define a class or enumeration
18926 unless it appears in the type-id of an alias-declaration (7.1.3) that
18927 is not the declaration of a template-declaration."
18929 In other words, if we currently are in an alias template, the
18930 type-id should not define a type.
18932 So let's set parser->type_definition_forbidden_message in that
18933 case; cp_parser_check_type_definition (called by
18934 cp_parser_class_specifier) will then emit an error if a type is
18935 defined in the type-id. */
18936 if (parser->num_template_parameter_lists)
18938 saved_message = parser->type_definition_forbidden_message;
18939 parser->type_definition_forbidden_message =
18940 G_("types may not be defined in alias template declarations");
18943 type = cp_parser_type_id (parser);
18945 /* Restore the error message if need be. */
18946 if (parser->num_template_parameter_lists)
18947 parser->type_definition_forbidden_message = saved_message;
18949 if (type == error_mark_node
18950 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18952 cp_parser_skip_to_end_of_block_or_statement (parser);
18953 return error_mark_node;
18956 /* A typedef-name can also be introduced by an alias-declaration. The
18957 identifier following the using keyword becomes a typedef-name. It has
18958 the same semantics as if it were introduced by the typedef
18959 specifier. In particular, it does not define a new type and it shall
18960 not appear in the type-id. */
18962 clear_decl_specs (&decl_specs);
18963 decl_specs.type = type;
18964 if (attributes != NULL_TREE)
18966 decl_specs.attributes = attributes;
18967 set_and_check_decl_spec_loc (&decl_specs,
18968 ds_attribute,
18969 attrs_token);
18971 set_and_check_decl_spec_loc (&decl_specs,
18972 ds_typedef,
18973 using_token);
18974 set_and_check_decl_spec_loc (&decl_specs,
18975 ds_alias,
18976 using_token);
18978 if (parser->num_template_parameter_lists
18979 && !cp_parser_check_template_parameters (parser,
18980 /*num_templates=*/0,
18981 /*template_id*/false,
18982 id_location,
18983 /*declarator=*/NULL))
18984 return error_mark_node;
18986 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18987 declarator->id_loc = id_location;
18989 member_p = at_class_scope_p ();
18990 if (member_p)
18991 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18992 NULL_TREE, attributes);
18993 else
18994 decl = start_decl (declarator, &decl_specs, 0,
18995 attributes, NULL_TREE, &pushed_scope);
18996 if (decl == error_mark_node)
18997 return decl;
18999 // Attach constraints to the alias declaration.
19000 if (flag_concepts && current_template_parms)
19002 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19003 tree constr = build_constraints (reqs, NULL_TREE);
19004 set_constraints (decl, constr);
19007 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19009 if (pushed_scope)
19010 pop_scope (pushed_scope);
19012 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19013 added into the symbol table; otherwise, return the TYPE_DECL. */
19014 if (DECL_LANG_SPECIFIC (decl)
19015 && DECL_TEMPLATE_INFO (decl)
19016 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19018 decl = DECL_TI_TEMPLATE (decl);
19019 if (member_p)
19020 check_member_template (decl);
19023 return decl;
19026 /* Parse a using-directive.
19028 using-directive:
19029 using namespace :: [opt] nested-name-specifier [opt]
19030 namespace-name ; */
19032 static void
19033 cp_parser_using_directive (cp_parser* parser)
19035 tree namespace_decl;
19036 tree attribs;
19038 /* Look for the `using' keyword. */
19039 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19040 /* And the `namespace' keyword. */
19041 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19042 /* Look for the optional `::' operator. */
19043 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19044 /* And the optional nested-name-specifier. */
19045 cp_parser_nested_name_specifier_opt (parser,
19046 /*typename_keyword_p=*/false,
19047 /*check_dependency_p=*/true,
19048 /*type_p=*/false,
19049 /*is_declaration=*/true);
19050 /* Get the namespace being used. */
19051 namespace_decl = cp_parser_namespace_name (parser);
19052 /* And any specified attributes. */
19053 attribs = cp_parser_attributes_opt (parser);
19055 /* Update the symbol table. */
19056 if (namespace_bindings_p ())
19057 finish_namespace_using_directive (namespace_decl, attribs);
19058 else
19059 finish_local_using_directive (namespace_decl, attribs);
19061 /* Look for the final `;'. */
19062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19065 /* Parse an asm-definition.
19067 asm-definition:
19068 asm ( string-literal ) ;
19070 GNU Extension:
19072 asm-definition:
19073 asm volatile [opt] ( string-literal ) ;
19074 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19075 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19076 : asm-operand-list [opt] ) ;
19077 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19078 : asm-operand-list [opt]
19079 : asm-clobber-list [opt] ) ;
19080 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19081 : asm-clobber-list [opt]
19082 : asm-goto-list ) ; */
19084 static void
19085 cp_parser_asm_definition (cp_parser* parser)
19087 tree string;
19088 tree outputs = NULL_TREE;
19089 tree inputs = NULL_TREE;
19090 tree clobbers = NULL_TREE;
19091 tree labels = NULL_TREE;
19092 tree asm_stmt;
19093 bool volatile_p = false;
19094 bool extended_p = false;
19095 bool invalid_inputs_p = false;
19096 bool invalid_outputs_p = false;
19097 bool goto_p = false;
19098 required_token missing = RT_NONE;
19100 /* Look for the `asm' keyword. */
19101 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19103 if (parser->in_function_body
19104 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19106 error ("%<asm%> in %<constexpr%> function");
19107 cp_function_chain->invalid_constexpr = true;
19110 /* See if the next token is `volatile'. */
19111 if (cp_parser_allow_gnu_extensions_p (parser)
19112 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19114 /* Remember that we saw the `volatile' keyword. */
19115 volatile_p = true;
19116 /* Consume the token. */
19117 cp_lexer_consume_token (parser->lexer);
19119 if (cp_parser_allow_gnu_extensions_p (parser)
19120 && parser->in_function_body
19121 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19123 /* Remember that we saw the `goto' keyword. */
19124 goto_p = true;
19125 /* Consume the token. */
19126 cp_lexer_consume_token (parser->lexer);
19128 /* Look for the opening `('. */
19129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19130 return;
19131 /* Look for the string. */
19132 string = cp_parser_string_literal (parser, false, false);
19133 if (string == error_mark_node)
19135 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19136 /*consume_paren=*/true);
19137 return;
19140 /* If we're allowing GNU extensions, check for the extended assembly
19141 syntax. Unfortunately, the `:' tokens need not be separated by
19142 a space in C, and so, for compatibility, we tolerate that here
19143 too. Doing that means that we have to treat the `::' operator as
19144 two `:' tokens. */
19145 if (cp_parser_allow_gnu_extensions_p (parser)
19146 && parser->in_function_body
19147 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19148 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19150 bool inputs_p = false;
19151 bool clobbers_p = false;
19152 bool labels_p = false;
19154 /* The extended syntax was used. */
19155 extended_p = true;
19157 /* Look for outputs. */
19158 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19160 /* Consume the `:'. */
19161 cp_lexer_consume_token (parser->lexer);
19162 /* Parse the output-operands. */
19163 if (cp_lexer_next_token_is_not (parser->lexer,
19164 CPP_COLON)
19165 && cp_lexer_next_token_is_not (parser->lexer,
19166 CPP_SCOPE)
19167 && cp_lexer_next_token_is_not (parser->lexer,
19168 CPP_CLOSE_PAREN)
19169 && !goto_p)
19171 outputs = cp_parser_asm_operand_list (parser);
19172 if (outputs == error_mark_node)
19173 invalid_outputs_p = true;
19176 /* If the next token is `::', there are no outputs, and the
19177 next token is the beginning of the inputs. */
19178 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19179 /* The inputs are coming next. */
19180 inputs_p = true;
19182 /* Look for inputs. */
19183 if (inputs_p
19184 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19186 /* Consume the `:' or `::'. */
19187 cp_lexer_consume_token (parser->lexer);
19188 /* Parse the output-operands. */
19189 if (cp_lexer_next_token_is_not (parser->lexer,
19190 CPP_COLON)
19191 && cp_lexer_next_token_is_not (parser->lexer,
19192 CPP_SCOPE)
19193 && cp_lexer_next_token_is_not (parser->lexer,
19194 CPP_CLOSE_PAREN))
19196 inputs = cp_parser_asm_operand_list (parser);
19197 if (inputs == error_mark_node)
19198 invalid_inputs_p = true;
19201 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19202 /* The clobbers are coming next. */
19203 clobbers_p = true;
19205 /* Look for clobbers. */
19206 if (clobbers_p
19207 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19209 clobbers_p = true;
19210 /* Consume the `:' or `::'. */
19211 cp_lexer_consume_token (parser->lexer);
19212 /* Parse the clobbers. */
19213 if (cp_lexer_next_token_is_not (parser->lexer,
19214 CPP_COLON)
19215 && cp_lexer_next_token_is_not (parser->lexer,
19216 CPP_CLOSE_PAREN))
19217 clobbers = cp_parser_asm_clobber_list (parser);
19219 else if (goto_p
19220 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19221 /* The labels are coming next. */
19222 labels_p = true;
19224 /* Look for labels. */
19225 if (labels_p
19226 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19228 labels_p = true;
19229 /* Consume the `:' or `::'. */
19230 cp_lexer_consume_token (parser->lexer);
19231 /* Parse the labels. */
19232 labels = cp_parser_asm_label_list (parser);
19235 if (goto_p && !labels_p)
19236 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19238 else if (goto_p)
19239 missing = RT_COLON_SCOPE;
19241 /* Look for the closing `)'. */
19242 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19243 missing ? missing : RT_CLOSE_PAREN))
19244 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19245 /*consume_paren=*/true);
19246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19248 if (!invalid_inputs_p && !invalid_outputs_p)
19250 /* Create the ASM_EXPR. */
19251 if (parser->in_function_body)
19253 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19254 inputs, clobbers, labels);
19255 /* If the extended syntax was not used, mark the ASM_EXPR. */
19256 if (!extended_p)
19258 tree temp = asm_stmt;
19259 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19260 temp = TREE_OPERAND (temp, 0);
19262 ASM_INPUT_P (temp) = 1;
19265 else
19266 symtab->finalize_toplevel_asm (string);
19270 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19271 type that comes from the decl-specifier-seq. */
19273 static tree
19274 strip_declarator_types (tree type, cp_declarator *declarator)
19276 for (cp_declarator *d = declarator; d;)
19277 switch (d->kind)
19279 case cdk_id:
19280 case cdk_decomp:
19281 case cdk_error:
19282 d = NULL;
19283 break;
19285 default:
19286 if (TYPE_PTRMEMFUNC_P (type))
19287 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19288 type = TREE_TYPE (type);
19289 d = d->declarator;
19290 break;
19293 return type;
19296 /* Declarators [gram.dcl.decl] */
19298 /* Parse an init-declarator.
19300 init-declarator:
19301 declarator initializer [opt]
19303 GNU Extension:
19305 init-declarator:
19306 declarator asm-specification [opt] attributes [opt] initializer [opt]
19308 function-definition:
19309 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19310 function-body
19311 decl-specifier-seq [opt] declarator function-try-block
19313 GNU Extension:
19315 function-definition:
19316 __extension__ function-definition
19318 TM Extension:
19320 function-definition:
19321 decl-specifier-seq [opt] declarator function-transaction-block
19323 The DECL_SPECIFIERS apply to this declarator. Returns a
19324 representation of the entity declared. If MEMBER_P is TRUE, then
19325 this declarator appears in a class scope. The new DECL created by
19326 this declarator is returned.
19328 The CHECKS are access checks that should be performed once we know
19329 what entity is being declared (and, therefore, what classes have
19330 befriended it).
19332 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19333 for a function-definition here as well. If the declarator is a
19334 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19335 be TRUE upon return. By that point, the function-definition will
19336 have been completely parsed.
19338 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19339 is FALSE.
19341 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19342 parsed declaration if it is an uninitialized single declarator not followed
19343 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19344 if present, will not be consumed. If returned, this declarator will be
19345 created with SD_INITIALIZED but will not call cp_finish_decl.
19347 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19348 and there is an initializer, the pointed location_t is set to the
19349 location of the '=' or `(', or '{' in C++11 token introducing the
19350 initializer. */
19352 static tree
19353 cp_parser_init_declarator (cp_parser* parser,
19354 cp_decl_specifier_seq *decl_specifiers,
19355 vec<deferred_access_check, va_gc> *checks,
19356 bool function_definition_allowed_p,
19357 bool member_p,
19358 int declares_class_or_enum,
19359 bool* function_definition_p,
19360 tree* maybe_range_for_decl,
19361 location_t* init_loc,
19362 tree* auto_result)
19364 cp_token *token = NULL, *asm_spec_start_token = NULL,
19365 *attributes_start_token = NULL;
19366 cp_declarator *declarator;
19367 tree prefix_attributes;
19368 tree attributes = NULL;
19369 tree asm_specification;
19370 tree initializer;
19371 tree decl = NULL_TREE;
19372 tree scope;
19373 int is_initialized;
19374 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19375 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19376 "(...)". */
19377 enum cpp_ttype initialization_kind;
19378 bool is_direct_init = false;
19379 bool is_non_constant_init;
19380 int ctor_dtor_or_conv_p;
19381 bool friend_p = cp_parser_friend_p (decl_specifiers);
19382 tree pushed_scope = NULL_TREE;
19383 bool range_for_decl_p = false;
19384 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19385 location_t tmp_init_loc = UNKNOWN_LOCATION;
19387 /* Gather the attributes that were provided with the
19388 decl-specifiers. */
19389 prefix_attributes = decl_specifiers->attributes;
19391 /* Assume that this is not the declarator for a function
19392 definition. */
19393 if (function_definition_p)
19394 *function_definition_p = false;
19396 /* Default arguments are only permitted for function parameters. */
19397 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19398 parser->default_arg_ok_p = false;
19400 /* Defer access checks while parsing the declarator; we cannot know
19401 what names are accessible until we know what is being
19402 declared. */
19403 resume_deferring_access_checks ();
19405 token = cp_lexer_peek_token (parser->lexer);
19407 /* Parse the declarator. */
19408 declarator
19409 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19410 &ctor_dtor_or_conv_p,
19411 /*parenthesized_p=*/NULL,
19412 member_p, friend_p);
19413 /* Gather up the deferred checks. */
19414 stop_deferring_access_checks ();
19416 parser->default_arg_ok_p = saved_default_arg_ok_p;
19418 /* If the DECLARATOR was erroneous, there's no need to go
19419 further. */
19420 if (declarator == cp_error_declarator)
19421 return error_mark_node;
19423 /* Check that the number of template-parameter-lists is OK. */
19424 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19425 token->location))
19426 return error_mark_node;
19428 if (declares_class_or_enum & 2)
19429 cp_parser_check_for_definition_in_return_type (declarator,
19430 decl_specifiers->type,
19431 decl_specifiers->locations[ds_type_spec]);
19433 /* Figure out what scope the entity declared by the DECLARATOR is
19434 located in. `grokdeclarator' sometimes changes the scope, so
19435 we compute it now. */
19436 scope = get_scope_of_declarator (declarator);
19438 /* Perform any lookups in the declared type which were thought to be
19439 dependent, but are not in the scope of the declarator. */
19440 decl_specifiers->type
19441 = maybe_update_decl_type (decl_specifiers->type, scope);
19443 /* If we're allowing GNU extensions, look for an
19444 asm-specification. */
19445 if (cp_parser_allow_gnu_extensions_p (parser))
19447 /* Look for an asm-specification. */
19448 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19449 asm_specification = cp_parser_asm_specification_opt (parser);
19451 else
19452 asm_specification = NULL_TREE;
19454 /* Look for attributes. */
19455 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19456 attributes = cp_parser_attributes_opt (parser);
19458 /* Peek at the next token. */
19459 token = cp_lexer_peek_token (parser->lexer);
19461 bool bogus_implicit_tmpl = false;
19463 if (function_declarator_p (declarator))
19465 /* Handle C++17 deduction guides. */
19466 if (!decl_specifiers->type
19467 && ctor_dtor_or_conv_p <= 0
19468 && cxx_dialect >= cxx17)
19470 cp_declarator *id = get_id_declarator (declarator);
19471 tree name = id->u.id.unqualified_name;
19472 parser->scope = id->u.id.qualifying_scope;
19473 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19474 if (tmpl
19475 && (DECL_CLASS_TEMPLATE_P (tmpl)
19476 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19478 id->u.id.unqualified_name = dguide_name (tmpl);
19479 id->u.id.sfk = sfk_deduction_guide;
19480 ctor_dtor_or_conv_p = 1;
19484 /* Check to see if the token indicates the start of a
19485 function-definition. */
19486 if (cp_parser_token_starts_function_definition_p (token))
19488 if (!function_definition_allowed_p)
19490 /* If a function-definition should not appear here, issue an
19491 error message. */
19492 cp_parser_error (parser,
19493 "a function-definition is not allowed here");
19494 return error_mark_node;
19497 location_t func_brace_location
19498 = cp_lexer_peek_token (parser->lexer)->location;
19500 /* Neither attributes nor an asm-specification are allowed
19501 on a function-definition. */
19502 if (asm_specification)
19503 error_at (asm_spec_start_token->location,
19504 "an asm-specification is not allowed "
19505 "on a function-definition");
19506 if (attributes)
19507 error_at (attributes_start_token->location,
19508 "attributes are not allowed "
19509 "on a function-definition");
19510 /* This is a function-definition. */
19511 *function_definition_p = true;
19513 /* Parse the function definition. */
19514 if (member_p)
19515 decl = cp_parser_save_member_function_body (parser,
19516 decl_specifiers,
19517 declarator,
19518 prefix_attributes);
19519 else
19520 decl =
19521 (cp_parser_function_definition_from_specifiers_and_declarator
19522 (parser, decl_specifiers, prefix_attributes, declarator));
19524 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19526 /* This is where the prologue starts... */
19527 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19528 = func_brace_location;
19531 return decl;
19534 else if (parser->fully_implicit_function_template_p)
19536 /* A non-template declaration involving a function parameter list
19537 containing an implicit template parameter will be made into a
19538 template. If the resulting declaration is not going to be an
19539 actual function then finish the template scope here to prevent it.
19540 An error message will be issued once we have a decl to talk about.
19542 FIXME probably we should do type deduction rather than create an
19543 implicit template, but the standard currently doesn't allow it. */
19544 bogus_implicit_tmpl = true;
19545 finish_fully_implicit_template (parser, NULL_TREE);
19548 /* [dcl.dcl]
19550 Only in function declarations for constructors, destructors, type
19551 conversions, and deduction guides can the decl-specifier-seq be omitted.
19553 We explicitly postpone this check past the point where we handle
19554 function-definitions because we tolerate function-definitions
19555 that are missing their return types in some modes. */
19556 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19558 cp_parser_error (parser,
19559 "expected constructor, destructor, or type conversion");
19560 return error_mark_node;
19563 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19564 if (token->type == CPP_EQ
19565 || token->type == CPP_OPEN_PAREN
19566 || token->type == CPP_OPEN_BRACE)
19568 is_initialized = SD_INITIALIZED;
19569 initialization_kind = token->type;
19570 if (maybe_range_for_decl)
19571 *maybe_range_for_decl = error_mark_node;
19572 tmp_init_loc = token->location;
19573 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19574 *init_loc = tmp_init_loc;
19576 if (token->type == CPP_EQ
19577 && function_declarator_p (declarator))
19579 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19580 if (t2->keyword == RID_DEFAULT)
19581 is_initialized = SD_DEFAULTED;
19582 else if (t2->keyword == RID_DELETE)
19583 is_initialized = SD_DELETED;
19586 else
19588 /* If the init-declarator isn't initialized and isn't followed by a
19589 `,' or `;', it's not a valid init-declarator. */
19590 if (token->type != CPP_COMMA
19591 && token->type != CPP_SEMICOLON)
19593 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19594 range_for_decl_p = true;
19595 else
19597 if (!maybe_range_for_decl)
19598 cp_parser_error (parser, "expected initializer");
19599 return error_mark_node;
19602 is_initialized = SD_UNINITIALIZED;
19603 initialization_kind = CPP_EOF;
19606 /* Because start_decl has side-effects, we should only call it if we
19607 know we're going ahead. By this point, we know that we cannot
19608 possibly be looking at any other construct. */
19609 cp_parser_commit_to_tentative_parse (parser);
19611 /* Enter the newly declared entry in the symbol table. If we're
19612 processing a declaration in a class-specifier, we wait until
19613 after processing the initializer. */
19614 if (!member_p)
19616 if (parser->in_unbraced_linkage_specification_p)
19617 decl_specifiers->storage_class = sc_extern;
19618 decl = start_decl (declarator, decl_specifiers,
19619 range_for_decl_p? SD_INITIALIZED : is_initialized,
19620 attributes, prefix_attributes, &pushed_scope);
19621 cp_finalize_omp_declare_simd (parser, decl);
19622 cp_finalize_oacc_routine (parser, decl, false);
19623 /* Adjust location of decl if declarator->id_loc is more appropriate:
19624 set, and decl wasn't merged with another decl, in which case its
19625 location would be different from input_location, and more accurate. */
19626 if (DECL_P (decl)
19627 && declarator->id_loc != UNKNOWN_LOCATION
19628 && DECL_SOURCE_LOCATION (decl) == input_location)
19629 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19631 else if (scope)
19632 /* Enter the SCOPE. That way unqualified names appearing in the
19633 initializer will be looked up in SCOPE. */
19634 pushed_scope = push_scope (scope);
19636 /* Perform deferred access control checks, now that we know in which
19637 SCOPE the declared entity resides. */
19638 if (!member_p && decl)
19640 tree saved_current_function_decl = NULL_TREE;
19642 /* If the entity being declared is a function, pretend that we
19643 are in its scope. If it is a `friend', it may have access to
19644 things that would not otherwise be accessible. */
19645 if (TREE_CODE (decl) == FUNCTION_DECL)
19647 saved_current_function_decl = current_function_decl;
19648 current_function_decl = decl;
19651 /* Perform access checks for template parameters. */
19652 cp_parser_perform_template_parameter_access_checks (checks);
19654 /* Perform the access control checks for the declarator and the
19655 decl-specifiers. */
19656 perform_deferred_access_checks (tf_warning_or_error);
19658 /* Restore the saved value. */
19659 if (TREE_CODE (decl) == FUNCTION_DECL)
19660 current_function_decl = saved_current_function_decl;
19663 /* Parse the initializer. */
19664 initializer = NULL_TREE;
19665 is_direct_init = false;
19666 is_non_constant_init = true;
19667 if (is_initialized)
19669 if (function_declarator_p (declarator))
19671 if (initialization_kind == CPP_EQ)
19672 initializer = cp_parser_pure_specifier (parser);
19673 else
19675 /* If the declaration was erroneous, we don't really
19676 know what the user intended, so just silently
19677 consume the initializer. */
19678 if (decl != error_mark_node)
19679 error_at (tmp_init_loc, "initializer provided for function");
19680 cp_parser_skip_to_closing_parenthesis (parser,
19681 /*recovering=*/true,
19682 /*or_comma=*/false,
19683 /*consume_paren=*/true);
19686 else
19688 /* We want to record the extra mangling scope for in-class
19689 initializers of class members and initializers of static data
19690 member templates. The former involves deferring
19691 parsing of the initializer until end of class as with default
19692 arguments. So right here we only handle the latter. */
19693 if (!member_p && processing_template_decl && decl != error_mark_node)
19694 start_lambda_scope (decl);
19695 initializer = cp_parser_initializer (parser,
19696 &is_direct_init,
19697 &is_non_constant_init);
19698 if (!member_p && processing_template_decl && decl != error_mark_node)
19699 finish_lambda_scope ();
19700 if (initializer == error_mark_node)
19701 cp_parser_skip_to_end_of_statement (parser);
19705 /* The old parser allows attributes to appear after a parenthesized
19706 initializer. Mark Mitchell proposed removing this functionality
19707 on the GCC mailing lists on 2002-08-13. This parser accepts the
19708 attributes -- but ignores them. Made a permerror in GCC 8. */
19709 if (cp_parser_allow_gnu_extensions_p (parser)
19710 && initialization_kind == CPP_OPEN_PAREN
19711 && cp_parser_attributes_opt (parser)
19712 && permerror (input_location,
19713 "attributes after parenthesized initializer ignored"))
19715 static bool hint;
19716 if (flag_permissive && !hint)
19718 hint = true;
19719 inform (input_location,
19720 "this flexibility is deprecated and will be removed");
19724 /* And now complain about a non-function implicit template. */
19725 if (bogus_implicit_tmpl && decl != error_mark_node)
19726 error_at (DECL_SOURCE_LOCATION (decl),
19727 "non-function %qD declared as implicit template", decl);
19729 /* For an in-class declaration, use `grokfield' to create the
19730 declaration. */
19731 if (member_p)
19733 if (pushed_scope)
19735 pop_scope (pushed_scope);
19736 pushed_scope = NULL_TREE;
19738 decl = grokfield (declarator, decl_specifiers,
19739 initializer, !is_non_constant_init,
19740 /*asmspec=*/NULL_TREE,
19741 attr_chainon (attributes, prefix_attributes));
19742 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19743 cp_parser_save_default_args (parser, decl);
19744 cp_finalize_omp_declare_simd (parser, decl);
19745 cp_finalize_oacc_routine (parser, decl, false);
19748 /* Finish processing the declaration. But, skip member
19749 declarations. */
19750 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19752 cp_finish_decl (decl,
19753 initializer, !is_non_constant_init,
19754 asm_specification,
19755 /* If the initializer is in parentheses, then this is
19756 a direct-initialization, which means that an
19757 `explicit' constructor is OK. Otherwise, an
19758 `explicit' constructor cannot be used. */
19759 ((is_direct_init || !is_initialized)
19760 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19762 else if ((cxx_dialect != cxx98) && friend_p
19763 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19764 /* Core issue #226 (C++0x only): A default template-argument
19765 shall not be specified in a friend class template
19766 declaration. */
19767 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19768 /*is_partial=*/false, /*is_friend_decl=*/1);
19770 if (!friend_p && pushed_scope)
19771 pop_scope (pushed_scope);
19773 if (function_declarator_p (declarator)
19774 && parser->fully_implicit_function_template_p)
19776 if (member_p)
19777 decl = finish_fully_implicit_template (parser, decl);
19778 else
19779 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19782 if (auto_result && is_initialized && decl_specifiers->type
19783 && type_uses_auto (decl_specifiers->type))
19784 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19786 return decl;
19789 /* Parse a declarator.
19791 declarator:
19792 direct-declarator
19793 ptr-operator declarator
19795 abstract-declarator:
19796 ptr-operator abstract-declarator [opt]
19797 direct-abstract-declarator
19799 GNU Extensions:
19801 declarator:
19802 attributes [opt] direct-declarator
19803 attributes [opt] ptr-operator declarator
19805 abstract-declarator:
19806 attributes [opt] ptr-operator abstract-declarator [opt]
19807 attributes [opt] direct-abstract-declarator
19809 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19810 detect constructors, destructors, deduction guides, or conversion operators.
19811 It is set to -1 if the declarator is a name, and +1 if it is a
19812 function. Otherwise it is set to zero. Usually you just want to
19813 test for >0, but internally the negative value is used.
19815 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19816 a decl-specifier-seq unless it declares a constructor, destructor,
19817 or conversion. It might seem that we could check this condition in
19818 semantic analysis, rather than parsing, but that makes it difficult
19819 to handle something like `f()'. We want to notice that there are
19820 no decl-specifiers, and therefore realize that this is an
19821 expression, not a declaration.)
19823 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19824 the declarator is a direct-declarator of the form "(...)".
19826 MEMBER_P is true iff this declarator is a member-declarator.
19828 FRIEND_P is true iff this declarator is a friend. */
19830 static cp_declarator *
19831 cp_parser_declarator (cp_parser* parser,
19832 cp_parser_declarator_kind dcl_kind,
19833 int* ctor_dtor_or_conv_p,
19834 bool* parenthesized_p,
19835 bool member_p, bool friend_p)
19837 cp_declarator *declarator;
19838 enum tree_code code;
19839 cp_cv_quals cv_quals;
19840 tree class_type;
19841 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19843 /* Assume this is not a constructor, destructor, or type-conversion
19844 operator. */
19845 if (ctor_dtor_or_conv_p)
19846 *ctor_dtor_or_conv_p = 0;
19848 if (cp_parser_allow_gnu_extensions_p (parser))
19849 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19851 /* Check for the ptr-operator production. */
19852 cp_parser_parse_tentatively (parser);
19853 /* Parse the ptr-operator. */
19854 code = cp_parser_ptr_operator (parser,
19855 &class_type,
19856 &cv_quals,
19857 &std_attributes);
19859 /* If that worked, then we have a ptr-operator. */
19860 if (cp_parser_parse_definitely (parser))
19862 /* If a ptr-operator was found, then this declarator was not
19863 parenthesized. */
19864 if (parenthesized_p)
19865 *parenthesized_p = true;
19866 /* The dependent declarator is optional if we are parsing an
19867 abstract-declarator. */
19868 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19869 cp_parser_parse_tentatively (parser);
19871 /* Parse the dependent declarator. */
19872 declarator = cp_parser_declarator (parser, dcl_kind,
19873 /*ctor_dtor_or_conv_p=*/NULL,
19874 /*parenthesized_p=*/NULL,
19875 /*member_p=*/false,
19876 friend_p);
19878 /* If we are parsing an abstract-declarator, we must handle the
19879 case where the dependent declarator is absent. */
19880 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19881 && !cp_parser_parse_definitely (parser))
19882 declarator = NULL;
19884 declarator = cp_parser_make_indirect_declarator
19885 (code, class_type, cv_quals, declarator, std_attributes);
19887 /* Everything else is a direct-declarator. */
19888 else
19890 if (parenthesized_p)
19891 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19892 CPP_OPEN_PAREN);
19893 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19894 ctor_dtor_or_conv_p,
19895 member_p, friend_p);
19898 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19899 declarator->attributes = gnu_attributes;
19900 return declarator;
19903 /* Parse a direct-declarator or direct-abstract-declarator.
19905 direct-declarator:
19906 declarator-id
19907 direct-declarator ( parameter-declaration-clause )
19908 cv-qualifier-seq [opt]
19909 ref-qualifier [opt]
19910 exception-specification [opt]
19911 direct-declarator [ constant-expression [opt] ]
19912 ( declarator )
19914 direct-abstract-declarator:
19915 direct-abstract-declarator [opt]
19916 ( parameter-declaration-clause )
19917 cv-qualifier-seq [opt]
19918 ref-qualifier [opt]
19919 exception-specification [opt]
19920 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19921 ( abstract-declarator )
19923 Returns a representation of the declarator. DCL_KIND is
19924 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19925 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19926 we are parsing a direct-declarator. It is
19927 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19928 of ambiguity we prefer an abstract declarator, as per
19929 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19930 as for cp_parser_declarator. */
19932 static cp_declarator *
19933 cp_parser_direct_declarator (cp_parser* parser,
19934 cp_parser_declarator_kind dcl_kind,
19935 int* ctor_dtor_or_conv_p,
19936 bool member_p, bool friend_p)
19938 cp_token *token;
19939 cp_declarator *declarator = NULL;
19940 tree scope = NULL_TREE;
19941 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19942 bool saved_in_declarator_p = parser->in_declarator_p;
19943 bool first = true;
19944 tree pushed_scope = NULL_TREE;
19945 cp_token *open_paren = NULL, *close_paren = NULL;
19947 while (true)
19949 /* Peek at the next token. */
19950 token = cp_lexer_peek_token (parser->lexer);
19951 if (token->type == CPP_OPEN_PAREN)
19953 /* This is either a parameter-declaration-clause, or a
19954 parenthesized declarator. When we know we are parsing a
19955 named declarator, it must be a parenthesized declarator
19956 if FIRST is true. For instance, `(int)' is a
19957 parameter-declaration-clause, with an omitted
19958 direct-abstract-declarator. But `((*))', is a
19959 parenthesized abstract declarator. Finally, when T is a
19960 template parameter `(T)' is a
19961 parameter-declaration-clause, and not a parenthesized
19962 named declarator.
19964 We first try and parse a parameter-declaration-clause,
19965 and then try a nested declarator (if FIRST is true).
19967 It is not an error for it not to be a
19968 parameter-declaration-clause, even when FIRST is
19969 false. Consider,
19971 int i (int);
19972 int i (3);
19974 The first is the declaration of a function while the
19975 second is the definition of a variable, including its
19976 initializer.
19978 Having seen only the parenthesis, we cannot know which of
19979 these two alternatives should be selected. Even more
19980 complex are examples like:
19982 int i (int (a));
19983 int i (int (3));
19985 The former is a function-declaration; the latter is a
19986 variable initialization.
19988 Thus again, we try a parameter-declaration-clause, and if
19989 that fails, we back out and return. */
19991 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19993 tree params;
19994 bool is_declarator = false;
19996 open_paren = NULL;
19998 /* In a member-declarator, the only valid interpretation
19999 of a parenthesis is the start of a
20000 parameter-declaration-clause. (It is invalid to
20001 initialize a static data member with a parenthesized
20002 initializer; only the "=" form of initialization is
20003 permitted.) */
20004 if (!member_p)
20005 cp_parser_parse_tentatively (parser);
20007 /* Consume the `('. */
20008 matching_parens parens;
20009 parens.consume_open (parser);
20010 if (first)
20012 /* If this is going to be an abstract declarator, we're
20013 in a declarator and we can't have default args. */
20014 parser->default_arg_ok_p = false;
20015 parser->in_declarator_p = true;
20018 begin_scope (sk_function_parms, NULL_TREE);
20020 /* Parse the parameter-declaration-clause. */
20021 params = cp_parser_parameter_declaration_clause (parser);
20023 /* Consume the `)'. */
20024 parens.require_close (parser);
20026 /* If all went well, parse the cv-qualifier-seq,
20027 ref-qualifier and the exception-specification. */
20028 if (member_p || cp_parser_parse_definitely (parser))
20030 cp_cv_quals cv_quals;
20031 cp_virt_specifiers virt_specifiers;
20032 cp_ref_qualifier ref_qual;
20033 tree exception_specification;
20034 tree late_return;
20035 tree attrs;
20036 bool memfn = (member_p || (pushed_scope
20037 && CLASS_TYPE_P (pushed_scope)));
20039 is_declarator = true;
20041 if (ctor_dtor_or_conv_p)
20042 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20043 first = false;
20045 /* Parse the cv-qualifier-seq. */
20046 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20047 /* Parse the ref-qualifier. */
20048 ref_qual = cp_parser_ref_qualifier_opt (parser);
20049 /* Parse the tx-qualifier. */
20050 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20051 /* And the exception-specification. */
20052 exception_specification
20053 = cp_parser_exception_specification_opt (parser);
20055 attrs = cp_parser_std_attribute_spec_seq (parser);
20057 /* In here, we handle cases where attribute is used after
20058 the function declaration. For example:
20059 void func (int x) __attribute__((vector(..))); */
20060 tree gnu_attrs = NULL_TREE;
20061 tree requires_clause = NULL_TREE;
20062 late_return = (cp_parser_late_return_type_opt
20063 (parser, declarator, requires_clause,
20064 memfn ? cv_quals : -1));
20066 /* Parse the virt-specifier-seq. */
20067 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20069 /* Create the function-declarator. */
20070 declarator = make_call_declarator (declarator,
20071 params,
20072 cv_quals,
20073 virt_specifiers,
20074 ref_qual,
20075 tx_qual,
20076 exception_specification,
20077 late_return,
20078 requires_clause);
20079 declarator->std_attributes = attrs;
20080 declarator->attributes = gnu_attrs;
20081 /* Any subsequent parameter lists are to do with
20082 return type, so are not those of the declared
20083 function. */
20084 parser->default_arg_ok_p = false;
20087 /* Remove the function parms from scope. */
20088 pop_bindings_and_leave_scope ();
20090 if (is_declarator)
20091 /* Repeat the main loop. */
20092 continue;
20095 /* If this is the first, we can try a parenthesized
20096 declarator. */
20097 if (first)
20099 bool saved_in_type_id_in_expr_p;
20101 parser->default_arg_ok_p = saved_default_arg_ok_p;
20102 parser->in_declarator_p = saved_in_declarator_p;
20104 open_paren = token;
20105 /* Consume the `('. */
20106 matching_parens parens;
20107 parens.consume_open (parser);
20108 /* Parse the nested declarator. */
20109 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20110 parser->in_type_id_in_expr_p = true;
20111 declarator
20112 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20113 /*parenthesized_p=*/NULL,
20114 member_p, friend_p);
20115 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20116 first = false;
20117 /* Expect a `)'. */
20118 close_paren = cp_lexer_peek_token (parser->lexer);
20119 if (!parens.require_close (parser))
20120 declarator = cp_error_declarator;
20121 if (declarator == cp_error_declarator)
20122 break;
20124 goto handle_declarator;
20126 /* Otherwise, we must be done. */
20127 else
20128 break;
20130 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20131 && token->type == CPP_OPEN_SQUARE
20132 && !cp_next_tokens_can_be_attribute_p (parser))
20134 /* Parse an array-declarator. */
20135 tree bounds, attrs;
20137 if (ctor_dtor_or_conv_p)
20138 *ctor_dtor_or_conv_p = 0;
20140 open_paren = NULL;
20141 first = false;
20142 parser->default_arg_ok_p = false;
20143 parser->in_declarator_p = true;
20144 /* Consume the `['. */
20145 cp_lexer_consume_token (parser->lexer);
20146 /* Peek at the next token. */
20147 token = cp_lexer_peek_token (parser->lexer);
20148 /* If the next token is `]', then there is no
20149 constant-expression. */
20150 if (token->type != CPP_CLOSE_SQUARE)
20152 bool non_constant_p;
20153 bounds
20154 = cp_parser_constant_expression (parser,
20155 /*allow_non_constant=*/true,
20156 &non_constant_p);
20157 if (!non_constant_p)
20158 /* OK */;
20159 else if (error_operand_p (bounds))
20160 /* Already gave an error. */;
20161 else if (!parser->in_function_body
20162 || current_binding_level->kind == sk_function_parms)
20164 /* Normally, the array bound must be an integral constant
20165 expression. However, as an extension, we allow VLAs
20166 in function scopes as long as they aren't part of a
20167 parameter declaration. */
20168 cp_parser_error (parser,
20169 "array bound is not an integer constant");
20170 bounds = error_mark_node;
20172 else if (processing_template_decl
20173 && !type_dependent_expression_p (bounds))
20175 /* Remember this wasn't a constant-expression. */
20176 bounds = build_nop (TREE_TYPE (bounds), bounds);
20177 TREE_SIDE_EFFECTS (bounds) = 1;
20180 else
20181 bounds = NULL_TREE;
20182 /* Look for the closing `]'. */
20183 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20185 declarator = cp_error_declarator;
20186 break;
20189 attrs = cp_parser_std_attribute_spec_seq (parser);
20190 declarator = make_array_declarator (declarator, bounds);
20191 declarator->std_attributes = attrs;
20193 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20196 tree qualifying_scope;
20197 tree unqualified_name;
20198 tree attrs;
20199 special_function_kind sfk;
20200 bool abstract_ok;
20201 bool pack_expansion_p = false;
20202 cp_token *declarator_id_start_token;
20204 /* Parse a declarator-id */
20205 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20206 if (abstract_ok)
20208 cp_parser_parse_tentatively (parser);
20210 /* If we see an ellipsis, we should be looking at a
20211 parameter pack. */
20212 if (token->type == CPP_ELLIPSIS)
20214 /* Consume the `...' */
20215 cp_lexer_consume_token (parser->lexer);
20217 pack_expansion_p = true;
20221 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20222 unqualified_name
20223 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20224 qualifying_scope = parser->scope;
20225 if (abstract_ok)
20227 bool okay = false;
20229 if (!unqualified_name && pack_expansion_p)
20231 /* Check whether an error occurred. */
20232 okay = !cp_parser_error_occurred (parser);
20234 /* We already consumed the ellipsis to mark a
20235 parameter pack, but we have no way to report it,
20236 so abort the tentative parse. We will be exiting
20237 immediately anyway. */
20238 cp_parser_abort_tentative_parse (parser);
20240 else
20241 okay = cp_parser_parse_definitely (parser);
20243 if (!okay)
20244 unqualified_name = error_mark_node;
20245 else if (unqualified_name
20246 && (qualifying_scope
20247 || (!identifier_p (unqualified_name))))
20249 cp_parser_error (parser, "expected unqualified-id");
20250 unqualified_name = error_mark_node;
20254 if (!unqualified_name)
20255 return NULL;
20256 if (unqualified_name == error_mark_node)
20258 declarator = cp_error_declarator;
20259 pack_expansion_p = false;
20260 declarator->parameter_pack_p = false;
20261 break;
20264 attrs = cp_parser_std_attribute_spec_seq (parser);
20266 if (qualifying_scope && at_namespace_scope_p ()
20267 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20269 /* In the declaration of a member of a template class
20270 outside of the class itself, the SCOPE will sometimes
20271 be a TYPENAME_TYPE. For example, given:
20273 template <typename T>
20274 int S<T>::R::i = 3;
20276 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20277 this context, we must resolve S<T>::R to an ordinary
20278 type, rather than a typename type.
20280 The reason we normally avoid resolving TYPENAME_TYPEs
20281 is that a specialization of `S' might render
20282 `S<T>::R' not a type. However, if `S' is
20283 specialized, then this `i' will not be used, so there
20284 is no harm in resolving the types here. */
20285 tree type;
20287 /* Resolve the TYPENAME_TYPE. */
20288 type = resolve_typename_type (qualifying_scope,
20289 /*only_current_p=*/false);
20290 /* If that failed, the declarator is invalid. */
20291 if (TREE_CODE (type) == TYPENAME_TYPE)
20293 if (typedef_variant_p (type))
20294 error_at (declarator_id_start_token->location,
20295 "cannot define member of dependent typedef "
20296 "%qT", type);
20297 else
20298 error_at (declarator_id_start_token->location,
20299 "%<%T::%E%> is not a type",
20300 TYPE_CONTEXT (qualifying_scope),
20301 TYPE_IDENTIFIER (qualifying_scope));
20303 qualifying_scope = type;
20306 sfk = sfk_none;
20308 if (unqualified_name)
20310 tree class_type;
20312 if (qualifying_scope
20313 && CLASS_TYPE_P (qualifying_scope))
20314 class_type = qualifying_scope;
20315 else
20316 class_type = current_class_type;
20318 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20320 tree name_type = TREE_TYPE (unqualified_name);
20322 if (!class_type || !same_type_p (name_type, class_type))
20324 /* We do not attempt to print the declarator
20325 here because we do not have enough
20326 information about its original syntactic
20327 form. */
20328 cp_parser_error (parser, "invalid declarator");
20329 declarator = cp_error_declarator;
20330 break;
20332 else if (qualifying_scope
20333 && CLASSTYPE_USE_TEMPLATE (name_type))
20335 error_at (declarator_id_start_token->location,
20336 "invalid use of constructor as a template");
20337 inform (declarator_id_start_token->location,
20338 "use %<%T::%D%> instead of %<%T::%D%> to "
20339 "name the constructor in a qualified name",
20340 class_type,
20341 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20342 class_type, name_type);
20343 declarator = cp_error_declarator;
20344 break;
20346 unqualified_name = constructor_name (class_type);
20349 if (class_type)
20351 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20352 sfk = sfk_destructor;
20353 else if (identifier_p (unqualified_name)
20354 && IDENTIFIER_CONV_OP_P (unqualified_name))
20355 sfk = sfk_conversion;
20356 else if (/* There's no way to declare a constructor
20357 for an unnamed type, even if the type
20358 got a name for linkage purposes. */
20359 !TYPE_WAS_UNNAMED (class_type)
20360 /* Handle correctly (c++/19200):
20362 struct S {
20363 struct T{};
20364 friend void S(T);
20367 and also:
20369 namespace N {
20370 void S();
20373 struct S {
20374 friend void N::S();
20375 }; */
20376 && (!friend_p || class_type == qualifying_scope)
20377 && constructor_name_p (unqualified_name,
20378 class_type))
20379 sfk = sfk_constructor;
20380 else if (is_overloaded_fn (unqualified_name)
20381 && DECL_CONSTRUCTOR_P (get_first_fn
20382 (unqualified_name)))
20383 sfk = sfk_constructor;
20385 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20386 *ctor_dtor_or_conv_p = -1;
20389 declarator = make_id_declarator (qualifying_scope,
20390 unqualified_name,
20391 sfk);
20392 declarator->std_attributes = attrs;
20393 declarator->id_loc = token->location;
20394 declarator->parameter_pack_p = pack_expansion_p;
20396 if (pack_expansion_p)
20397 maybe_warn_variadic_templates ();
20400 handle_declarator:;
20401 scope = get_scope_of_declarator (declarator);
20402 if (scope)
20404 /* Any names that appear after the declarator-id for a
20405 member are looked up in the containing scope. */
20406 if (at_function_scope_p ())
20408 /* But declarations with qualified-ids can't appear in a
20409 function. */
20410 cp_parser_error (parser, "qualified-id in declaration");
20411 declarator = cp_error_declarator;
20412 break;
20414 pushed_scope = push_scope (scope);
20416 parser->in_declarator_p = true;
20417 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20418 || (declarator && declarator->kind == cdk_id))
20419 /* Default args are only allowed on function
20420 declarations. */
20421 parser->default_arg_ok_p = saved_default_arg_ok_p;
20422 else
20423 parser->default_arg_ok_p = false;
20425 first = false;
20427 /* We're done. */
20428 else
20429 break;
20432 /* For an abstract declarator, we might wind up with nothing at this
20433 point. That's an error; the declarator is not optional. */
20434 if (!declarator)
20435 cp_parser_error (parser, "expected declarator");
20436 else if (open_paren)
20438 /* Record overly parenthesized declarator so we can give a
20439 diagnostic about confusing decl/expr disambiguation. */
20440 if (declarator->kind == cdk_array)
20442 /* If the open and close parens are on different lines, this
20443 is probably a formatting thing, so ignore. */
20444 expanded_location open = expand_location (open_paren->location);
20445 expanded_location close = expand_location (close_paren->location);
20446 if (open.line != close.line || open.file != close.file)
20447 open_paren = NULL;
20449 if (open_paren)
20450 declarator->parenthesized = open_paren->location;
20453 /* If we entered a scope, we must exit it now. */
20454 if (pushed_scope)
20455 pop_scope (pushed_scope);
20457 parser->default_arg_ok_p = saved_default_arg_ok_p;
20458 parser->in_declarator_p = saved_in_declarator_p;
20460 return declarator;
20463 /* Parse a ptr-operator.
20465 ptr-operator:
20466 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20467 * cv-qualifier-seq [opt]
20469 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20470 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20472 GNU Extension:
20474 ptr-operator:
20475 & cv-qualifier-seq [opt]
20477 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20478 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20479 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20480 filled in with the TYPE containing the member. *CV_QUALS is
20481 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20482 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20483 Note that the tree codes returned by this function have nothing
20484 to do with the types of trees that will be eventually be created
20485 to represent the pointer or reference type being parsed. They are
20486 just constants with suggestive names. */
20487 static enum tree_code
20488 cp_parser_ptr_operator (cp_parser* parser,
20489 tree* type,
20490 cp_cv_quals *cv_quals,
20491 tree *attributes)
20493 enum tree_code code = ERROR_MARK;
20494 cp_token *token;
20495 tree attrs = NULL_TREE;
20497 /* Assume that it's not a pointer-to-member. */
20498 *type = NULL_TREE;
20499 /* And that there are no cv-qualifiers. */
20500 *cv_quals = TYPE_UNQUALIFIED;
20502 /* Peek at the next token. */
20503 token = cp_lexer_peek_token (parser->lexer);
20505 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20506 if (token->type == CPP_MULT)
20507 code = INDIRECT_REF;
20508 else if (token->type == CPP_AND)
20509 code = ADDR_EXPR;
20510 else if ((cxx_dialect != cxx98) &&
20511 token->type == CPP_AND_AND) /* C++0x only */
20512 code = NON_LVALUE_EXPR;
20514 if (code != ERROR_MARK)
20516 /* Consume the `*', `&' or `&&'. */
20517 cp_lexer_consume_token (parser->lexer);
20519 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20520 `&', if we are allowing GNU extensions. (The only qualifier
20521 that can legally appear after `&' is `restrict', but that is
20522 enforced during semantic analysis. */
20523 if (code == INDIRECT_REF
20524 || cp_parser_allow_gnu_extensions_p (parser))
20525 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20527 attrs = cp_parser_std_attribute_spec_seq (parser);
20528 if (attributes != NULL)
20529 *attributes = attrs;
20531 else
20533 /* Try the pointer-to-member case. */
20534 cp_parser_parse_tentatively (parser);
20535 /* Look for the optional `::' operator. */
20536 cp_parser_global_scope_opt (parser,
20537 /*current_scope_valid_p=*/false);
20538 /* Look for the nested-name specifier. */
20539 token = cp_lexer_peek_token (parser->lexer);
20540 cp_parser_nested_name_specifier (parser,
20541 /*typename_keyword_p=*/false,
20542 /*check_dependency_p=*/true,
20543 /*type_p=*/false,
20544 /*is_declaration=*/false);
20545 /* If we found it, and the next token is a `*', then we are
20546 indeed looking at a pointer-to-member operator. */
20547 if (!cp_parser_error_occurred (parser)
20548 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20550 /* Indicate that the `*' operator was used. */
20551 code = INDIRECT_REF;
20553 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20554 error_at (token->location, "%qD is a namespace", parser->scope);
20555 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20556 error_at (token->location, "cannot form pointer to member of "
20557 "non-class %q#T", parser->scope);
20558 else
20560 /* The type of which the member is a member is given by the
20561 current SCOPE. */
20562 *type = parser->scope;
20563 /* The next name will not be qualified. */
20564 parser->scope = NULL_TREE;
20565 parser->qualifying_scope = NULL_TREE;
20566 parser->object_scope = NULL_TREE;
20567 /* Look for optional c++11 attributes. */
20568 attrs = cp_parser_std_attribute_spec_seq (parser);
20569 if (attributes != NULL)
20570 *attributes = attrs;
20571 /* Look for the optional cv-qualifier-seq. */
20572 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20575 /* If that didn't work we don't have a ptr-operator. */
20576 if (!cp_parser_parse_definitely (parser))
20577 cp_parser_error (parser, "expected ptr-operator");
20580 return code;
20583 /* Parse an (optional) cv-qualifier-seq.
20585 cv-qualifier-seq:
20586 cv-qualifier cv-qualifier-seq [opt]
20588 cv-qualifier:
20589 const
20590 volatile
20592 GNU Extension:
20594 cv-qualifier:
20595 __restrict__
20597 Returns a bitmask representing the cv-qualifiers. */
20599 static cp_cv_quals
20600 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20602 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20604 while (true)
20606 cp_token *token;
20607 cp_cv_quals cv_qualifier;
20609 /* Peek at the next token. */
20610 token = cp_lexer_peek_token (parser->lexer);
20611 /* See if it's a cv-qualifier. */
20612 switch (token->keyword)
20614 case RID_CONST:
20615 cv_qualifier = TYPE_QUAL_CONST;
20616 break;
20618 case RID_VOLATILE:
20619 cv_qualifier = TYPE_QUAL_VOLATILE;
20620 break;
20622 case RID_RESTRICT:
20623 cv_qualifier = TYPE_QUAL_RESTRICT;
20624 break;
20626 default:
20627 cv_qualifier = TYPE_UNQUALIFIED;
20628 break;
20631 if (!cv_qualifier)
20632 break;
20634 if (cv_quals & cv_qualifier)
20636 gcc_rich_location richloc (token->location);
20637 richloc.add_fixit_remove ();
20638 error_at (&richloc, "duplicate cv-qualifier");
20639 cp_lexer_purge_token (parser->lexer);
20641 else
20643 cp_lexer_consume_token (parser->lexer);
20644 cv_quals |= cv_qualifier;
20648 return cv_quals;
20651 /* Parse an (optional) ref-qualifier
20653 ref-qualifier:
20657 Returns cp_ref_qualifier representing ref-qualifier. */
20659 static cp_ref_qualifier
20660 cp_parser_ref_qualifier_opt (cp_parser* parser)
20662 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20664 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20665 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20666 return ref_qual;
20668 while (true)
20670 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20671 cp_token *token = cp_lexer_peek_token (parser->lexer);
20673 switch (token->type)
20675 case CPP_AND:
20676 curr_ref_qual = REF_QUAL_LVALUE;
20677 break;
20679 case CPP_AND_AND:
20680 curr_ref_qual = REF_QUAL_RVALUE;
20681 break;
20683 default:
20684 curr_ref_qual = REF_QUAL_NONE;
20685 break;
20688 if (!curr_ref_qual)
20689 break;
20690 else if (ref_qual)
20692 error_at (token->location, "multiple ref-qualifiers");
20693 cp_lexer_purge_token (parser->lexer);
20695 else
20697 ref_qual = curr_ref_qual;
20698 cp_lexer_consume_token (parser->lexer);
20702 return ref_qual;
20705 /* Parse an optional tx-qualifier.
20707 tx-qualifier:
20708 transaction_safe
20709 transaction_safe_dynamic */
20711 static tree
20712 cp_parser_tx_qualifier_opt (cp_parser *parser)
20714 cp_token *token = cp_lexer_peek_token (parser->lexer);
20715 if (token->type == CPP_NAME)
20717 tree name = token->u.value;
20718 const char *p = IDENTIFIER_POINTER (name);
20719 const int len = strlen ("transaction_safe");
20720 if (!strncmp (p, "transaction_safe", len))
20722 p += len;
20723 if (*p == '\0'
20724 || !strcmp (p, "_dynamic"))
20726 cp_lexer_consume_token (parser->lexer);
20727 if (!flag_tm)
20729 error ("%qE requires %<-fgnu-tm%>", name);
20730 return NULL_TREE;
20732 else
20733 return name;
20737 return NULL_TREE;
20740 /* Parse an (optional) virt-specifier-seq.
20742 virt-specifier-seq:
20743 virt-specifier virt-specifier-seq [opt]
20745 virt-specifier:
20746 override
20747 final
20749 Returns a bitmask representing the virt-specifiers. */
20751 static cp_virt_specifiers
20752 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20754 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20756 while (true)
20758 cp_token *token;
20759 cp_virt_specifiers virt_specifier;
20761 /* Peek at the next token. */
20762 token = cp_lexer_peek_token (parser->lexer);
20763 /* See if it's a virt-specifier-qualifier. */
20764 if (token->type != CPP_NAME)
20765 break;
20766 if (id_equal (token->u.value, "override"))
20768 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20769 virt_specifier = VIRT_SPEC_OVERRIDE;
20771 else if (id_equal (token->u.value, "final"))
20773 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20774 virt_specifier = VIRT_SPEC_FINAL;
20776 else if (id_equal (token->u.value, "__final"))
20778 virt_specifier = VIRT_SPEC_FINAL;
20780 else
20781 break;
20783 if (virt_specifiers & virt_specifier)
20785 gcc_rich_location richloc (token->location);
20786 richloc.add_fixit_remove ();
20787 error_at (&richloc, "duplicate virt-specifier");
20788 cp_lexer_purge_token (parser->lexer);
20790 else
20792 cp_lexer_consume_token (parser->lexer);
20793 virt_specifiers |= virt_specifier;
20796 return virt_specifiers;
20799 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20800 is in scope even though it isn't real. */
20802 void
20803 inject_this_parameter (tree ctype, cp_cv_quals quals)
20805 tree this_parm;
20807 if (current_class_ptr)
20809 /* We don't clear this between NSDMIs. Is it already what we want? */
20810 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20811 if (DECL_P (current_class_ptr)
20812 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20813 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20814 && cp_type_quals (type) == quals)
20815 return;
20818 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20819 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20820 current_class_ptr = NULL_TREE;
20821 current_class_ref
20822 = cp_build_fold_indirect_ref (this_parm);
20823 current_class_ptr = this_parm;
20826 /* Return true iff our current scope is a non-static data member
20827 initializer. */
20829 bool
20830 parsing_nsdmi (void)
20832 /* We recognize NSDMI context by the context-less 'this' pointer set up
20833 by the function above. */
20834 if (current_class_ptr
20835 && TREE_CODE (current_class_ptr) == PARM_DECL
20836 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20837 return true;
20838 return false;
20841 /* Parse a late-specified return type, if any. This is not a separate
20842 non-terminal, but part of a function declarator, which looks like
20844 -> trailing-type-specifier-seq abstract-declarator(opt)
20846 Returns the type indicated by the type-id.
20848 In addition to this, parse any queued up #pragma omp declare simd
20849 clauses, and #pragma acc routine clauses.
20851 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20852 function. */
20854 static tree
20855 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20856 tree& requires_clause, cp_cv_quals quals)
20858 cp_token *token;
20859 tree type = NULL_TREE;
20860 bool declare_simd_p = (parser->omp_declare_simd
20861 && declarator
20862 && declarator->kind == cdk_id);
20864 bool oacc_routine_p = (parser->oacc_routine
20865 && declarator
20866 && declarator->kind == cdk_id);
20868 /* Peek at the next token. */
20869 token = cp_lexer_peek_token (parser->lexer);
20870 /* A late-specified return type is indicated by an initial '->'. */
20871 if (token->type != CPP_DEREF
20872 && token->keyword != RID_REQUIRES
20873 && !(token->type == CPP_NAME
20874 && token->u.value == ridpointers[RID_REQUIRES])
20875 && !(declare_simd_p || oacc_routine_p))
20876 return NULL_TREE;
20878 tree save_ccp = current_class_ptr;
20879 tree save_ccr = current_class_ref;
20880 if (quals >= 0)
20882 /* DR 1207: 'this' is in scope in the trailing return type. */
20883 inject_this_parameter (current_class_type, quals);
20886 if (token->type == CPP_DEREF)
20888 /* Consume the ->. */
20889 cp_lexer_consume_token (parser->lexer);
20891 type = cp_parser_trailing_type_id (parser);
20894 /* Function declarations may be followed by a trailing
20895 requires-clause. */
20896 requires_clause = cp_parser_requires_clause_opt (parser);
20898 if (declare_simd_p)
20899 declarator->attributes
20900 = cp_parser_late_parsing_omp_declare_simd (parser,
20901 declarator->attributes);
20902 if (oacc_routine_p)
20903 declarator->attributes
20904 = cp_parser_late_parsing_oacc_routine (parser,
20905 declarator->attributes);
20907 if (quals >= 0)
20909 current_class_ptr = save_ccp;
20910 current_class_ref = save_ccr;
20913 return type;
20916 /* Parse a declarator-id.
20918 declarator-id:
20919 id-expression
20920 :: [opt] nested-name-specifier [opt] type-name
20922 In the `id-expression' case, the value returned is as for
20923 cp_parser_id_expression if the id-expression was an unqualified-id.
20924 If the id-expression was a qualified-id, then a SCOPE_REF is
20925 returned. The first operand is the scope (either a NAMESPACE_DECL
20926 or TREE_TYPE), but the second is still just a representation of an
20927 unqualified-id. */
20929 static tree
20930 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20932 tree id;
20933 /* The expression must be an id-expression. Assume that qualified
20934 names are the names of types so that:
20936 template <class T>
20937 int S<T>::R::i = 3;
20939 will work; we must treat `S<T>::R' as the name of a type.
20940 Similarly, assume that qualified names are templates, where
20941 required, so that:
20943 template <class T>
20944 int S<T>::R<T>::i = 3;
20946 will work, too. */
20947 id = cp_parser_id_expression (parser,
20948 /*template_keyword_p=*/false,
20949 /*check_dependency_p=*/false,
20950 /*template_p=*/NULL,
20951 /*declarator_p=*/true,
20952 optional_p);
20953 if (id && BASELINK_P (id))
20954 id = BASELINK_FUNCTIONS (id);
20955 return id;
20958 /* Parse a type-id.
20960 type-id:
20961 type-specifier-seq abstract-declarator [opt]
20963 Returns the TYPE specified. */
20965 static tree
20966 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20967 bool is_trailing_return)
20969 cp_decl_specifier_seq type_specifier_seq;
20970 cp_declarator *abstract_declarator;
20972 /* Parse the type-specifier-seq. */
20973 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20974 is_trailing_return,
20975 &type_specifier_seq);
20976 if (is_template_arg && type_specifier_seq.type
20977 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20978 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20979 /* A bare template name as a template argument is a template template
20980 argument, not a placeholder, so fail parsing it as a type argument. */
20982 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20983 cp_parser_simulate_error (parser);
20984 return error_mark_node;
20986 if (type_specifier_seq.type == error_mark_node)
20987 return error_mark_node;
20989 /* There might or might not be an abstract declarator. */
20990 cp_parser_parse_tentatively (parser);
20991 /* Look for the declarator. */
20992 abstract_declarator
20993 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20994 /*parenthesized_p=*/NULL,
20995 /*member_p=*/false,
20996 /*friend_p=*/false);
20997 /* Check to see if there really was a declarator. */
20998 if (!cp_parser_parse_definitely (parser))
20999 abstract_declarator = NULL;
21001 if (type_specifier_seq.type
21002 /* The concepts TS allows 'auto' as a type-id. */
21003 && (!flag_concepts || parser->in_type_id_in_expr_p)
21004 /* None of the valid uses of 'auto' in C++14 involve the type-id
21005 nonterminal, but it is valid in a trailing-return-type. */
21006 && !(cxx_dialect >= cxx14 && is_trailing_return))
21007 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21009 /* A type-id with type 'auto' is only ok if the abstract declarator
21010 is a function declarator with a late-specified return type.
21012 A type-id with 'auto' is also valid in a trailing-return-type
21013 in a compound-requirement. */
21014 if (abstract_declarator
21015 && abstract_declarator->kind == cdk_function
21016 && abstract_declarator->u.function.late_return_type)
21017 /* OK */;
21018 else if (parser->in_result_type_constraint_p)
21019 /* OK */;
21020 else
21022 location_t loc = type_specifier_seq.locations[ds_type_spec];
21023 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21025 error_at (loc, "missing template arguments after %qT",
21026 auto_node);
21027 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21028 tmpl);
21030 else
21031 error_at (loc, "invalid use of %qT", auto_node);
21032 return error_mark_node;
21036 return groktypename (&type_specifier_seq, abstract_declarator,
21037 is_template_arg);
21040 static tree
21041 cp_parser_type_id (cp_parser *parser)
21043 return cp_parser_type_id_1 (parser, false, false);
21046 static tree
21047 cp_parser_template_type_arg (cp_parser *parser)
21049 tree r;
21050 const char *saved_message = parser->type_definition_forbidden_message;
21051 parser->type_definition_forbidden_message
21052 = G_("types may not be defined in template arguments");
21053 r = cp_parser_type_id_1 (parser, true, false);
21054 parser->type_definition_forbidden_message = saved_message;
21055 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21057 error ("invalid use of %<auto%> in template argument");
21058 r = error_mark_node;
21060 return r;
21063 static tree
21064 cp_parser_trailing_type_id (cp_parser *parser)
21066 return cp_parser_type_id_1 (parser, false, true);
21069 /* Parse a type-specifier-seq.
21071 type-specifier-seq:
21072 type-specifier type-specifier-seq [opt]
21074 GNU extension:
21076 type-specifier-seq:
21077 attributes type-specifier-seq [opt]
21079 If IS_DECLARATION is true, we are at the start of a "condition" or
21080 exception-declaration, so we might be followed by a declarator-id.
21082 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21083 i.e. we've just seen "->".
21085 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21087 static void
21088 cp_parser_type_specifier_seq (cp_parser* parser,
21089 bool is_declaration,
21090 bool is_trailing_return,
21091 cp_decl_specifier_seq *type_specifier_seq)
21093 bool seen_type_specifier = false;
21094 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21095 cp_token *start_token = NULL;
21097 /* Clear the TYPE_SPECIFIER_SEQ. */
21098 clear_decl_specs (type_specifier_seq);
21100 /* In the context of a trailing return type, enum E { } is an
21101 elaborated-type-specifier followed by a function-body, not an
21102 enum-specifier. */
21103 if (is_trailing_return)
21104 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21106 /* Parse the type-specifiers and attributes. */
21107 while (true)
21109 tree type_specifier;
21110 bool is_cv_qualifier;
21112 /* Check for attributes first. */
21113 if (cp_next_tokens_can_be_attribute_p (parser))
21115 type_specifier_seq->attributes
21116 = attr_chainon (type_specifier_seq->attributes,
21117 cp_parser_attributes_opt (parser));
21118 continue;
21121 /* record the token of the beginning of the type specifier seq,
21122 for error reporting purposes*/
21123 if (!start_token)
21124 start_token = cp_lexer_peek_token (parser->lexer);
21126 /* Look for the type-specifier. */
21127 type_specifier = cp_parser_type_specifier (parser,
21128 flags,
21129 type_specifier_seq,
21130 /*is_declaration=*/false,
21131 NULL,
21132 &is_cv_qualifier);
21133 if (!type_specifier)
21135 /* If the first type-specifier could not be found, this is not a
21136 type-specifier-seq at all. */
21137 if (!seen_type_specifier)
21139 /* Set in_declarator_p to avoid skipping to the semicolon. */
21140 int in_decl = parser->in_declarator_p;
21141 parser->in_declarator_p = true;
21143 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21144 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21145 cp_parser_error (parser, "expected type-specifier");
21147 parser->in_declarator_p = in_decl;
21149 type_specifier_seq->type = error_mark_node;
21150 return;
21152 /* If subsequent type-specifiers could not be found, the
21153 type-specifier-seq is complete. */
21154 break;
21157 seen_type_specifier = true;
21158 /* The standard says that a condition can be:
21160 type-specifier-seq declarator = assignment-expression
21162 However, given:
21164 struct S {};
21165 if (int S = ...)
21167 we should treat the "S" as a declarator, not as a
21168 type-specifier. The standard doesn't say that explicitly for
21169 type-specifier-seq, but it does say that for
21170 decl-specifier-seq in an ordinary declaration. Perhaps it
21171 would be clearer just to allow a decl-specifier-seq here, and
21172 then add a semantic restriction that if any decl-specifiers
21173 that are not type-specifiers appear, the program is invalid. */
21174 if (is_declaration && !is_cv_qualifier)
21175 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21179 /* Return whether the function currently being declared has an associated
21180 template parameter list. */
21182 static bool
21183 function_being_declared_is_template_p (cp_parser* parser)
21185 if (!current_template_parms || processing_template_parmlist)
21186 return false;
21188 if (parser->implicit_template_scope)
21189 return true;
21191 if (at_class_scope_p ()
21192 && TYPE_BEING_DEFINED (current_class_type))
21193 return parser->num_template_parameter_lists != 0;
21195 return ((int) parser->num_template_parameter_lists > template_class_depth
21196 (current_class_type));
21199 /* Parse a parameter-declaration-clause.
21201 parameter-declaration-clause:
21202 parameter-declaration-list [opt] ... [opt]
21203 parameter-declaration-list , ...
21205 Returns a representation for the parameter declarations. A return
21206 value of NULL indicates a parameter-declaration-clause consisting
21207 only of an ellipsis. */
21209 static tree
21210 cp_parser_parameter_declaration_clause (cp_parser* parser)
21212 tree parameters;
21213 cp_token *token;
21214 bool ellipsis_p;
21215 bool is_error;
21217 temp_override<bool> cleanup
21218 (parser->auto_is_implicit_function_template_parm_p);
21220 if (!processing_specialization
21221 && !processing_template_parmlist
21222 && !processing_explicit_instantiation
21223 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21224 actual function or a random abstract declarator. */
21225 && parser->default_arg_ok_p)
21226 if (!current_function_decl
21227 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21228 parser->auto_is_implicit_function_template_parm_p = true;
21230 /* Peek at the next token. */
21231 token = cp_lexer_peek_token (parser->lexer);
21232 /* Check for trivial parameter-declaration-clauses. */
21233 if (token->type == CPP_ELLIPSIS)
21235 /* Consume the `...' token. */
21236 cp_lexer_consume_token (parser->lexer);
21237 return NULL_TREE;
21239 else if (token->type == CPP_CLOSE_PAREN)
21240 /* There are no parameters. */
21242 #ifndef NO_IMPLICIT_EXTERN_C
21243 if (in_system_header_at (input_location)
21244 && current_class_type == NULL
21245 && current_lang_name == lang_name_c)
21246 return NULL_TREE;
21247 else
21248 #endif
21249 return void_list_node;
21251 /* Check for `(void)', too, which is a special case. */
21252 else if (token->keyword == RID_VOID
21253 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21254 == CPP_CLOSE_PAREN))
21256 /* Consume the `void' token. */
21257 cp_lexer_consume_token (parser->lexer);
21258 /* There are no parameters. */
21259 return void_list_node;
21262 /* Parse the parameter-declaration-list. */
21263 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21264 /* If a parse error occurred while parsing the
21265 parameter-declaration-list, then the entire
21266 parameter-declaration-clause is erroneous. */
21267 if (is_error)
21268 return NULL;
21270 /* Peek at the next token. */
21271 token = cp_lexer_peek_token (parser->lexer);
21272 /* If it's a `,', the clause should terminate with an ellipsis. */
21273 if (token->type == CPP_COMMA)
21275 /* Consume the `,'. */
21276 cp_lexer_consume_token (parser->lexer);
21277 /* Expect an ellipsis. */
21278 ellipsis_p
21279 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21281 /* It might also be `...' if the optional trailing `,' was
21282 omitted. */
21283 else if (token->type == CPP_ELLIPSIS)
21285 /* Consume the `...' token. */
21286 cp_lexer_consume_token (parser->lexer);
21287 /* And remember that we saw it. */
21288 ellipsis_p = true;
21290 else
21291 ellipsis_p = false;
21293 /* Finish the parameter list. */
21294 if (!ellipsis_p)
21295 parameters = chainon (parameters, void_list_node);
21297 return parameters;
21300 /* Parse a parameter-declaration-list.
21302 parameter-declaration-list:
21303 parameter-declaration
21304 parameter-declaration-list , parameter-declaration
21306 Returns a representation of the parameter-declaration-list, as for
21307 cp_parser_parameter_declaration_clause. However, the
21308 `void_list_node' is never appended to the list. Upon return,
21309 *IS_ERROR will be true iff an error occurred. */
21311 static tree
21312 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21314 tree parameters = NULL_TREE;
21315 tree *tail = &parameters;
21316 bool saved_in_unbraced_linkage_specification_p;
21317 int index = 0;
21319 /* Assume all will go well. */
21320 *is_error = false;
21321 /* The special considerations that apply to a function within an
21322 unbraced linkage specifications do not apply to the parameters
21323 to the function. */
21324 saved_in_unbraced_linkage_specification_p
21325 = parser->in_unbraced_linkage_specification_p;
21326 parser->in_unbraced_linkage_specification_p = false;
21328 /* Look for more parameters. */
21329 while (true)
21331 cp_parameter_declarator *parameter;
21332 tree decl = error_mark_node;
21333 bool parenthesized_p = false;
21335 /* Parse the parameter. */
21336 parameter
21337 = cp_parser_parameter_declaration (parser,
21338 /*template_parm_p=*/false,
21339 &parenthesized_p);
21341 /* We don't know yet if the enclosing context is deprecated, so wait
21342 and warn in grokparms if appropriate. */
21343 deprecated_state = DEPRECATED_SUPPRESS;
21345 if (parameter)
21347 decl = grokdeclarator (parameter->declarator,
21348 &parameter->decl_specifiers,
21349 PARM,
21350 parameter->default_argument != NULL_TREE,
21351 &parameter->decl_specifiers.attributes);
21352 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21353 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21356 deprecated_state = DEPRECATED_NORMAL;
21358 /* If a parse error occurred parsing the parameter declaration,
21359 then the entire parameter-declaration-list is erroneous. */
21360 if (decl == error_mark_node)
21362 *is_error = true;
21363 parameters = error_mark_node;
21364 break;
21367 if (parameter->decl_specifiers.attributes)
21368 cplus_decl_attributes (&decl,
21369 parameter->decl_specifiers.attributes,
21371 if (DECL_NAME (decl))
21372 decl = pushdecl (decl);
21374 if (decl != error_mark_node)
21376 retrofit_lang_decl (decl);
21377 DECL_PARM_INDEX (decl) = ++index;
21378 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21381 /* Add the new parameter to the list. */
21382 *tail = build_tree_list (parameter->default_argument, decl);
21383 tail = &TREE_CHAIN (*tail);
21385 /* Peek at the next token. */
21386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21387 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21388 /* These are for Objective-C++ */
21389 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21390 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21391 /* The parameter-declaration-list is complete. */
21392 break;
21393 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21395 cp_token *token;
21397 /* Peek at the next token. */
21398 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21399 /* If it's an ellipsis, then the list is complete. */
21400 if (token->type == CPP_ELLIPSIS)
21401 break;
21402 /* Otherwise, there must be more parameters. Consume the
21403 `,'. */
21404 cp_lexer_consume_token (parser->lexer);
21405 /* When parsing something like:
21407 int i(float f, double d)
21409 we can tell after seeing the declaration for "f" that we
21410 are not looking at an initialization of a variable "i",
21411 but rather at the declaration of a function "i".
21413 Due to the fact that the parsing of template arguments
21414 (as specified to a template-id) requires backtracking we
21415 cannot use this technique when inside a template argument
21416 list. */
21417 if (!parser->in_template_argument_list_p
21418 && !parser->in_type_id_in_expr_p
21419 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21420 /* However, a parameter-declaration of the form
21421 "float(f)" (which is a valid declaration of a
21422 parameter "f") can also be interpreted as an
21423 expression (the conversion of "f" to "float"). */
21424 && !parenthesized_p)
21425 cp_parser_commit_to_tentative_parse (parser);
21427 else
21429 cp_parser_error (parser, "expected %<,%> or %<...%>");
21430 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21431 cp_parser_skip_to_closing_parenthesis (parser,
21432 /*recovering=*/true,
21433 /*or_comma=*/false,
21434 /*consume_paren=*/false);
21435 break;
21439 parser->in_unbraced_linkage_specification_p
21440 = saved_in_unbraced_linkage_specification_p;
21442 /* Reset implicit_template_scope if we are about to leave the function
21443 parameter list that introduced it. Note that for out-of-line member
21444 definitions, there will be one or more class scopes before we get to
21445 the template parameter scope. */
21447 if (cp_binding_level *its = parser->implicit_template_scope)
21448 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21450 while (maybe_its->kind == sk_class)
21451 maybe_its = maybe_its->level_chain;
21452 if (maybe_its == its)
21454 parser->implicit_template_parms = 0;
21455 parser->implicit_template_scope = 0;
21459 return parameters;
21462 /* Parse a parameter declaration.
21464 parameter-declaration:
21465 decl-specifier-seq ... [opt] declarator
21466 decl-specifier-seq declarator = assignment-expression
21467 decl-specifier-seq ... [opt] abstract-declarator [opt]
21468 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21470 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21471 declares a template parameter. (In that case, a non-nested `>'
21472 token encountered during the parsing of the assignment-expression
21473 is not interpreted as a greater-than operator.)
21475 Returns a representation of the parameter, or NULL if an error
21476 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21477 true iff the declarator is of the form "(p)". */
21479 static cp_parameter_declarator *
21480 cp_parser_parameter_declaration (cp_parser *parser,
21481 bool template_parm_p,
21482 bool *parenthesized_p)
21484 int declares_class_or_enum;
21485 cp_decl_specifier_seq decl_specifiers;
21486 cp_declarator *declarator;
21487 tree default_argument;
21488 cp_token *token = NULL, *declarator_token_start = NULL;
21489 const char *saved_message;
21490 bool template_parameter_pack_p = false;
21492 /* In a template parameter, `>' is not an operator.
21494 [temp.param]
21496 When parsing a default template-argument for a non-type
21497 template-parameter, the first non-nested `>' is taken as the end
21498 of the template parameter-list rather than a greater-than
21499 operator. */
21501 /* Type definitions may not appear in parameter types. */
21502 saved_message = parser->type_definition_forbidden_message;
21503 parser->type_definition_forbidden_message
21504 = G_("types may not be defined in parameter types");
21506 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21507 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21508 (current_template_parms)) : 0);
21510 /* Parse the declaration-specifiers. */
21511 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21512 cp_parser_decl_specifier_seq (parser,
21513 CP_PARSER_FLAGS_NONE,
21514 &decl_specifiers,
21515 &declares_class_or_enum);
21517 /* Complain about missing 'typename' or other invalid type names. */
21518 if (!decl_specifiers.any_type_specifiers_p
21519 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21520 decl_specifiers.type = error_mark_node;
21522 /* If an error occurred, there's no reason to attempt to parse the
21523 rest of the declaration. */
21524 if (cp_parser_error_occurred (parser))
21526 parser->type_definition_forbidden_message = saved_message;
21527 return NULL;
21530 /* Peek at the next token. */
21531 token = cp_lexer_peek_token (parser->lexer);
21533 /* If the next token is a `)', `,', `=', `>', or `...', then there
21534 is no declarator. However, when variadic templates are enabled,
21535 there may be a declarator following `...'. */
21536 if (token->type == CPP_CLOSE_PAREN
21537 || token->type == CPP_COMMA
21538 || token->type == CPP_EQ
21539 || token->type == CPP_GREATER)
21541 declarator = NULL;
21542 if (parenthesized_p)
21543 *parenthesized_p = false;
21545 /* Otherwise, there should be a declarator. */
21546 else
21548 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21549 parser->default_arg_ok_p = false;
21551 /* After seeing a decl-specifier-seq, if the next token is not a
21552 "(", there is no possibility that the code is a valid
21553 expression. Therefore, if parsing tentatively, we commit at
21554 this point. */
21555 if (!parser->in_template_argument_list_p
21556 /* In an expression context, having seen:
21558 (int((char ...
21560 we cannot be sure whether we are looking at a
21561 function-type (taking a "char" as a parameter) or a cast
21562 of some object of type "char" to "int". */
21563 && !parser->in_type_id_in_expr_p
21564 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21565 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21566 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21567 cp_parser_commit_to_tentative_parse (parser);
21568 /* Parse the declarator. */
21569 declarator_token_start = token;
21570 declarator = cp_parser_declarator (parser,
21571 CP_PARSER_DECLARATOR_EITHER,
21572 /*ctor_dtor_or_conv_p=*/NULL,
21573 parenthesized_p,
21574 /*member_p=*/false,
21575 /*friend_p=*/false);
21576 parser->default_arg_ok_p = saved_default_arg_ok_p;
21577 /* After the declarator, allow more attributes. */
21578 decl_specifiers.attributes
21579 = attr_chainon (decl_specifiers.attributes,
21580 cp_parser_attributes_opt (parser));
21582 /* If the declarator is a template parameter pack, remember that and
21583 clear the flag in the declarator itself so we don't get errors
21584 from grokdeclarator. */
21585 if (template_parm_p && declarator && declarator->parameter_pack_p)
21587 declarator->parameter_pack_p = false;
21588 template_parameter_pack_p = true;
21592 /* If the next token is an ellipsis, and we have not seen a declarator
21593 name, and if either the type of the declarator contains parameter
21594 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21595 for, eg, abbreviated integral type names), then we actually have a
21596 parameter pack expansion expression. Otherwise, leave the ellipsis
21597 for a C-style variadic function. */
21598 token = cp_lexer_peek_token (parser->lexer);
21600 /* If a function parameter pack was specified and an implicit template
21601 parameter was introduced during cp_parser_parameter_declaration,
21602 change any implicit parameters introduced into packs. */
21603 if (parser->implicit_template_parms
21604 && (token->type == CPP_ELLIPSIS
21605 || (declarator && declarator->parameter_pack_p)))
21607 int latest_template_parm_idx = TREE_VEC_LENGTH
21608 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21610 if (latest_template_parm_idx != template_parm_idx)
21611 decl_specifiers.type = convert_generic_types_to_packs
21612 (decl_specifiers.type,
21613 template_parm_idx, latest_template_parm_idx);
21616 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21618 tree type = decl_specifiers.type;
21620 if (type && DECL_P (type))
21621 type = TREE_TYPE (type);
21623 if (((type
21624 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21625 && (template_parm_p || uses_parameter_packs (type)))
21626 || (!type && template_parm_p))
21627 && declarator_can_be_parameter_pack (declarator))
21629 /* Consume the `...'. */
21630 cp_lexer_consume_token (parser->lexer);
21631 maybe_warn_variadic_templates ();
21633 /* Build a pack expansion type */
21634 if (template_parm_p)
21635 template_parameter_pack_p = true;
21636 else if (declarator)
21637 declarator->parameter_pack_p = true;
21638 else
21639 decl_specifiers.type = make_pack_expansion (type);
21643 /* The restriction on defining new types applies only to the type
21644 of the parameter, not to the default argument. */
21645 parser->type_definition_forbidden_message = saved_message;
21647 /* If the next token is `=', then process a default argument. */
21648 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21650 tree type = decl_specifiers.type;
21651 token = cp_lexer_peek_token (parser->lexer);
21652 /* If we are defining a class, then the tokens that make up the
21653 default argument must be saved and processed later. */
21654 if (!template_parm_p && at_class_scope_p ()
21655 && TYPE_BEING_DEFINED (current_class_type)
21656 && !LAMBDA_TYPE_P (current_class_type))
21657 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21659 // A constrained-type-specifier may declare a type template-parameter.
21660 else if (declares_constrained_type_template_parameter (type))
21661 default_argument
21662 = cp_parser_default_type_template_argument (parser);
21664 // A constrained-type-specifier may declare a template-template-parameter.
21665 else if (declares_constrained_template_template_parameter (type))
21666 default_argument
21667 = cp_parser_default_template_template_argument (parser);
21669 /* Outside of a class definition, we can just parse the
21670 assignment-expression. */
21671 else
21672 default_argument
21673 = cp_parser_default_argument (parser, template_parm_p);
21675 if (!parser->default_arg_ok_p)
21677 permerror (token->location,
21678 "default arguments are only "
21679 "permitted for function parameters");
21681 else if ((declarator && declarator->parameter_pack_p)
21682 || template_parameter_pack_p
21683 || (decl_specifiers.type
21684 && PACK_EXPANSION_P (decl_specifiers.type)))
21686 /* Find the name of the parameter pack. */
21687 cp_declarator *id_declarator = declarator;
21688 while (id_declarator && id_declarator->kind != cdk_id)
21689 id_declarator = id_declarator->declarator;
21691 if (id_declarator && id_declarator->kind == cdk_id)
21692 error_at (declarator_token_start->location,
21693 template_parm_p
21694 ? G_("template parameter pack %qD "
21695 "cannot have a default argument")
21696 : G_("parameter pack %qD cannot have "
21697 "a default argument"),
21698 id_declarator->u.id.unqualified_name);
21699 else
21700 error_at (declarator_token_start->location,
21701 template_parm_p
21702 ? G_("template parameter pack cannot have "
21703 "a default argument")
21704 : G_("parameter pack cannot have a "
21705 "default argument"));
21707 default_argument = NULL_TREE;
21710 else
21711 default_argument = NULL_TREE;
21713 /* Generate a location for the parameter, ranging from the start of the
21714 initial token to the end of the final token (using input_location for
21715 the latter, set up by cp_lexer_set_source_position_from_token when
21716 consuming tokens).
21718 If we have a identifier, then use it for the caret location, e.g.
21720 extern int callee (int one, int (*two)(int, int), float three);
21721 ~~~~~~^~~~~~~~~~~~~~
21723 otherwise, reuse the start location for the caret location e.g.:
21725 extern int callee (int one, int (*)(int, int), float three);
21726 ^~~~~~~~~~~~~~~~~
21729 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21730 ? declarator->id_loc
21731 : decl_spec_token_start->location);
21732 location_t param_loc = make_location (caret_loc,
21733 decl_spec_token_start->location,
21734 input_location);
21736 return make_parameter_declarator (&decl_specifiers,
21737 declarator,
21738 default_argument,
21739 param_loc,
21740 template_parameter_pack_p);
21743 /* Parse a default argument and return it.
21745 TEMPLATE_PARM_P is true if this is a default argument for a
21746 non-type template parameter. */
21747 static tree
21748 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21750 tree default_argument = NULL_TREE;
21751 bool saved_greater_than_is_operator_p;
21752 bool saved_local_variables_forbidden_p;
21753 bool non_constant_p, is_direct_init;
21755 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21756 set correctly. */
21757 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21758 parser->greater_than_is_operator_p = !template_parm_p;
21759 /* Local variable names (and the `this' keyword) may not
21760 appear in a default argument. */
21761 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21762 parser->local_variables_forbidden_p = true;
21763 /* Parse the assignment-expression. */
21764 if (template_parm_p)
21765 push_deferring_access_checks (dk_no_deferred);
21766 tree saved_class_ptr = NULL_TREE;
21767 tree saved_class_ref = NULL_TREE;
21768 /* The "this" pointer is not valid in a default argument. */
21769 if (cfun)
21771 saved_class_ptr = current_class_ptr;
21772 cp_function_chain->x_current_class_ptr = NULL_TREE;
21773 saved_class_ref = current_class_ref;
21774 cp_function_chain->x_current_class_ref = NULL_TREE;
21776 default_argument
21777 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21778 /* Restore the "this" pointer. */
21779 if (cfun)
21781 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21782 cp_function_chain->x_current_class_ref = saved_class_ref;
21784 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21785 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21786 if (template_parm_p)
21787 pop_deferring_access_checks ();
21788 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21789 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21791 return default_argument;
21794 /* Parse a function-body.
21796 function-body:
21797 compound_statement */
21799 static void
21800 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21802 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21803 ? BCS_TRY_BLOCK : BCS_NORMAL),
21804 true);
21807 /* Parse a ctor-initializer-opt followed by a function-body. Return
21808 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21809 is true we are parsing a function-try-block. */
21811 static void
21812 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21813 bool in_function_try_block)
21815 tree body, list;
21816 const bool check_body_p =
21817 DECL_CONSTRUCTOR_P (current_function_decl)
21818 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21819 tree last = NULL;
21821 /* Begin the function body. */
21822 body = begin_function_body ();
21823 /* Parse the optional ctor-initializer. */
21824 cp_parser_ctor_initializer_opt (parser);
21826 /* If we're parsing a constexpr constructor definition, we need
21827 to check that the constructor body is indeed empty. However,
21828 before we get to cp_parser_function_body lot of junk has been
21829 generated, so we can't just check that we have an empty block.
21830 Rather we take a snapshot of the outermost block, and check whether
21831 cp_parser_function_body changed its state. */
21832 if (check_body_p)
21834 list = cur_stmt_list;
21835 if (STATEMENT_LIST_TAIL (list))
21836 last = STATEMENT_LIST_TAIL (list)->stmt;
21838 /* Parse the function-body. */
21839 cp_parser_function_body (parser, in_function_try_block);
21840 if (check_body_p)
21841 check_constexpr_ctor_body (last, list, /*complain=*/true);
21842 /* Finish the function body. */
21843 finish_function_body (body);
21846 /* Parse an initializer.
21848 initializer:
21849 = initializer-clause
21850 ( expression-list )
21852 Returns an expression representing the initializer. If no
21853 initializer is present, NULL_TREE is returned.
21855 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21856 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21857 set to TRUE if there is no initializer present. If there is an
21858 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21859 is set to true; otherwise it is set to false. */
21861 static tree
21862 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21863 bool* non_constant_p)
21865 cp_token *token;
21866 tree init;
21868 /* Peek at the next token. */
21869 token = cp_lexer_peek_token (parser->lexer);
21871 /* Let our caller know whether or not this initializer was
21872 parenthesized. */
21873 *is_direct_init = (token->type != CPP_EQ);
21874 /* Assume that the initializer is constant. */
21875 *non_constant_p = false;
21877 if (token->type == CPP_EQ)
21879 /* Consume the `='. */
21880 cp_lexer_consume_token (parser->lexer);
21881 /* Parse the initializer-clause. */
21882 init = cp_parser_initializer_clause (parser, non_constant_p);
21884 else if (token->type == CPP_OPEN_PAREN)
21886 vec<tree, va_gc> *vec;
21887 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21888 /*cast_p=*/false,
21889 /*allow_expansion_p=*/true,
21890 non_constant_p);
21891 if (vec == NULL)
21892 return error_mark_node;
21893 init = build_tree_list_vec (vec);
21894 release_tree_vector (vec);
21896 else if (token->type == CPP_OPEN_BRACE)
21898 cp_lexer_set_source_position (parser->lexer);
21899 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21900 init = cp_parser_braced_list (parser, non_constant_p);
21901 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21903 else
21905 /* Anything else is an error. */
21906 cp_parser_error (parser, "expected initializer");
21907 init = error_mark_node;
21910 if (check_for_bare_parameter_packs (init))
21911 init = error_mark_node;
21913 return init;
21916 /* Parse an initializer-clause.
21918 initializer-clause:
21919 assignment-expression
21920 braced-init-list
21922 Returns an expression representing the initializer.
21924 If the `assignment-expression' production is used the value
21925 returned is simply a representation for the expression.
21927 Otherwise, calls cp_parser_braced_list. */
21929 static cp_expr
21930 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21932 cp_expr initializer;
21934 /* Assume the expression is constant. */
21935 *non_constant_p = false;
21937 /* If it is not a `{', then we are looking at an
21938 assignment-expression. */
21939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21941 initializer
21942 = cp_parser_constant_expression (parser,
21943 /*allow_non_constant_p=*/true,
21944 non_constant_p);
21946 else
21947 initializer = cp_parser_braced_list (parser, non_constant_p);
21949 return initializer;
21952 /* Parse a brace-enclosed initializer list.
21954 braced-init-list:
21955 { initializer-list , [opt] }
21956 { designated-initializer-list , [opt] }
21959 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21960 the elements of the initializer-list (or NULL, if the last
21961 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21962 NULL_TREE. There is no way to detect whether or not the optional
21963 trailing `,' was provided. NON_CONSTANT_P is as for
21964 cp_parser_initializer. */
21966 static cp_expr
21967 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21969 tree initializer;
21970 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21972 /* Consume the `{' token. */
21973 matching_braces braces;
21974 braces.require_open (parser);
21975 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21976 initializer = make_node (CONSTRUCTOR);
21977 /* If it's not a `}', then there is a non-trivial initializer. */
21978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21980 /* Parse the initializer list. */
21981 CONSTRUCTOR_ELTS (initializer)
21982 = cp_parser_initializer_list (parser, non_constant_p);
21983 /* A trailing `,' token is allowed. */
21984 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21985 cp_lexer_consume_token (parser->lexer);
21987 else
21988 *non_constant_p = false;
21989 /* Now, there should be a trailing `}'. */
21990 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21991 braces.require_close (parser);
21992 TREE_TYPE (initializer) = init_list_type_node;
21994 cp_expr result (initializer);
21995 /* Build a location of the form:
21996 { ... }
21997 ^~~~~~~
21998 with caret==start at the open brace, finish at the close brace. */
21999 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22000 result.set_location (combined_loc);
22001 return result;
22004 /* Consume tokens up to, and including, the next non-nested closing `]'.
22005 Returns true iff we found a closing `]'. */
22007 static bool
22008 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22010 unsigned square_depth = 0;
22012 while (true)
22014 cp_token * token = cp_lexer_peek_token (parser->lexer);
22016 switch (token->type)
22018 case CPP_EOF:
22019 case CPP_PRAGMA_EOL:
22020 /* If we've run out of tokens, then there is no closing `]'. */
22021 return false;
22023 case CPP_OPEN_SQUARE:
22024 ++square_depth;
22025 break;
22027 case CPP_CLOSE_SQUARE:
22028 if (!square_depth--)
22030 cp_lexer_consume_token (parser->lexer);
22031 return true;
22033 break;
22035 default:
22036 break;
22039 /* Consume the token. */
22040 cp_lexer_consume_token (parser->lexer);
22044 /* Return true if we are looking at an array-designator, false otherwise. */
22046 static bool
22047 cp_parser_array_designator_p (cp_parser *parser)
22049 /* Consume the `['. */
22050 cp_lexer_consume_token (parser->lexer);
22052 cp_lexer_save_tokens (parser->lexer);
22054 /* Skip tokens until the next token is a closing square bracket.
22055 If we find the closing `]', and the next token is a `=', then
22056 we are looking at an array designator. */
22057 bool array_designator_p
22058 = (cp_parser_skip_to_closing_square_bracket (parser)
22059 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22061 /* Roll back the tokens we skipped. */
22062 cp_lexer_rollback_tokens (parser->lexer);
22064 return array_designator_p;
22067 /* Parse an initializer-list.
22069 initializer-list:
22070 initializer-clause ... [opt]
22071 initializer-list , initializer-clause ... [opt]
22073 C++2A Extension:
22075 designated-initializer-list:
22076 designated-initializer-clause
22077 designated-initializer-list , designated-initializer-clause
22079 designated-initializer-clause:
22080 designator brace-or-equal-initializer
22082 designator:
22083 . identifier
22085 GNU Extension:
22087 initializer-list:
22088 designation initializer-clause ...[opt]
22089 initializer-list , designation initializer-clause ...[opt]
22091 designation:
22092 . identifier =
22093 identifier :
22094 [ constant-expression ] =
22096 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22097 for the initializer. If the INDEX of the elt is non-NULL, it is the
22098 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22099 as for cp_parser_initializer. */
22101 static vec<constructor_elt, va_gc> *
22102 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22104 vec<constructor_elt, va_gc> *v = NULL;
22105 bool first_p = true;
22106 tree first_designator = NULL_TREE;
22108 /* Assume all of the expressions are constant. */
22109 *non_constant_p = false;
22111 /* Parse the rest of the list. */
22112 while (true)
22114 cp_token *token;
22115 tree designator;
22116 tree initializer;
22117 bool clause_non_constant_p;
22118 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22120 /* Handle the C++2A syntax, '. id ='. */
22121 if ((cxx_dialect >= cxx2a
22122 || cp_parser_allow_gnu_extensions_p (parser))
22123 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22124 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22125 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22126 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22127 == CPP_OPEN_BRACE)))
22129 if (cxx_dialect < cxx2a)
22130 pedwarn (loc, OPT_Wpedantic,
22131 "C++ designated initializers only available with "
22132 "-std=c++2a or -std=gnu++2a");
22133 /* Consume the `.'. */
22134 cp_lexer_consume_token (parser->lexer);
22135 /* Consume the identifier. */
22136 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22137 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22138 /* Consume the `='. */
22139 cp_lexer_consume_token (parser->lexer);
22141 /* Also, if the next token is an identifier and the following one is a
22142 colon, we are looking at the GNU designated-initializer
22143 syntax. */
22144 else if (cp_parser_allow_gnu_extensions_p (parser)
22145 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22146 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22147 == CPP_COLON))
22149 /* Warn the user that they are using an extension. */
22150 pedwarn (loc, OPT_Wpedantic,
22151 "ISO C++ does not allow GNU designated initializers");
22152 /* Consume the identifier. */
22153 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22154 /* Consume the `:'. */
22155 cp_lexer_consume_token (parser->lexer);
22157 /* Also handle C99 array designators, '[ const ] ='. */
22158 else if (cp_parser_allow_gnu_extensions_p (parser)
22159 && !c_dialect_objc ()
22160 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22162 /* In C++11, [ could start a lambda-introducer. */
22163 bool non_const = false;
22165 cp_parser_parse_tentatively (parser);
22167 if (!cp_parser_array_designator_p (parser))
22169 cp_parser_simulate_error (parser);
22170 designator = NULL_TREE;
22172 else
22174 designator = cp_parser_constant_expression (parser, true,
22175 &non_const);
22176 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22177 cp_parser_require (parser, CPP_EQ, RT_EQ);
22180 if (!cp_parser_parse_definitely (parser))
22181 designator = NULL_TREE;
22182 else if (non_const
22183 && (!require_potential_rvalue_constant_expression
22184 (designator)))
22185 designator = NULL_TREE;
22186 if (designator)
22187 /* Warn the user that they are using an extension. */
22188 pedwarn (loc, OPT_Wpedantic,
22189 "ISO C++ does not allow C99 designated initializers");
22191 else
22192 designator = NULL_TREE;
22194 if (first_p)
22196 first_designator = designator;
22197 first_p = false;
22199 else if (cxx_dialect >= cxx2a
22200 && first_designator != error_mark_node
22201 && (!first_designator != !designator))
22203 error_at (loc, "either all initializer clauses should be designated "
22204 "or none of them should be");
22205 first_designator = error_mark_node;
22207 else if (cxx_dialect < cxx2a && !first_designator)
22208 first_designator = designator;
22210 /* Parse the initializer. */
22211 initializer = cp_parser_initializer_clause (parser,
22212 &clause_non_constant_p);
22213 /* If any clause is non-constant, so is the entire initializer. */
22214 if (clause_non_constant_p)
22215 *non_constant_p = true;
22217 /* If we have an ellipsis, this is an initializer pack
22218 expansion. */
22219 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22223 /* Consume the `...'. */
22224 cp_lexer_consume_token (parser->lexer);
22226 if (designator && cxx_dialect >= cxx2a)
22227 error_at (loc,
22228 "%<...%> not allowed in designated initializer list");
22230 /* Turn the initializer into an initializer expansion. */
22231 initializer = make_pack_expansion (initializer);
22234 /* Add it to the vector. */
22235 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22237 /* If the next token is not a comma, we have reached the end of
22238 the list. */
22239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22240 break;
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22244 /* If the next token is a `}', then we're still done. An
22245 initializer-clause can have a trailing `,' after the
22246 initializer-list and before the closing `}'. */
22247 if (token->type == CPP_CLOSE_BRACE)
22248 break;
22250 /* Consume the `,' token. */
22251 cp_lexer_consume_token (parser->lexer);
22254 /* The same identifier shall not appear in multiple designators
22255 of a designated-initializer-list. */
22256 if (first_designator)
22258 unsigned int i;
22259 tree designator, val;
22260 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22261 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22263 if (IDENTIFIER_MARKED (designator))
22265 error_at (EXPR_LOC_OR_LOC (val, input_location),
22266 "%<.%s%> designator used multiple times in "
22267 "the same initializer list",
22268 IDENTIFIER_POINTER (designator));
22269 (*v)[i].index = NULL_TREE;
22271 else
22272 IDENTIFIER_MARKED (designator) = 1;
22274 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22275 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22276 IDENTIFIER_MARKED (designator) = 0;
22279 return v;
22282 /* Classes [gram.class] */
22284 /* Parse a class-name.
22286 class-name:
22287 identifier
22288 template-id
22290 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22291 to indicate that names looked up in dependent types should be
22292 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22293 keyword has been used to indicate that the name that appears next
22294 is a template. TAG_TYPE indicates the explicit tag given before
22295 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22296 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22297 is the class being defined in a class-head. If ENUM_OK is TRUE,
22298 enum-names are also accepted.
22300 Returns the TYPE_DECL representing the class. */
22302 static tree
22303 cp_parser_class_name (cp_parser *parser,
22304 bool typename_keyword_p,
22305 bool template_keyword_p,
22306 enum tag_types tag_type,
22307 bool check_dependency_p,
22308 bool class_head_p,
22309 bool is_declaration,
22310 bool enum_ok)
22312 tree decl;
22313 tree scope;
22314 bool typename_p;
22315 cp_token *token;
22316 tree identifier = NULL_TREE;
22318 /* All class-names start with an identifier. */
22319 token = cp_lexer_peek_token (parser->lexer);
22320 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22322 cp_parser_error (parser, "expected class-name");
22323 return error_mark_node;
22326 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22327 to a template-id, so we save it here. */
22328 scope = parser->scope;
22329 if (scope == error_mark_node)
22330 return error_mark_node;
22332 /* Any name names a type if we're following the `typename' keyword
22333 in a qualified name where the enclosing scope is type-dependent. */
22334 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22335 && dependent_type_p (scope));
22336 /* Handle the common case (an identifier, but not a template-id)
22337 efficiently. */
22338 if (token->type == CPP_NAME
22339 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22341 cp_token *identifier_token;
22342 bool ambiguous_p;
22344 /* Look for the identifier. */
22345 identifier_token = cp_lexer_peek_token (parser->lexer);
22346 ambiguous_p = identifier_token->error_reported;
22347 identifier = cp_parser_identifier (parser);
22348 /* If the next token isn't an identifier, we are certainly not
22349 looking at a class-name. */
22350 if (identifier == error_mark_node)
22351 decl = error_mark_node;
22352 /* If we know this is a type-name, there's no need to look it
22353 up. */
22354 else if (typename_p)
22355 decl = identifier;
22356 else
22358 tree ambiguous_decls;
22359 /* If we already know that this lookup is ambiguous, then
22360 we've already issued an error message; there's no reason
22361 to check again. */
22362 if (ambiguous_p)
22364 cp_parser_simulate_error (parser);
22365 return error_mark_node;
22367 /* If the next token is a `::', then the name must be a type
22368 name.
22370 [basic.lookup.qual]
22372 During the lookup for a name preceding the :: scope
22373 resolution operator, object, function, and enumerator
22374 names are ignored. */
22375 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22376 tag_type = scope_type;
22377 /* Look up the name. */
22378 decl = cp_parser_lookup_name (parser, identifier,
22379 tag_type,
22380 /*is_template=*/false,
22381 /*is_namespace=*/false,
22382 check_dependency_p,
22383 &ambiguous_decls,
22384 identifier_token->location);
22385 if (ambiguous_decls)
22387 if (cp_parser_parsing_tentatively (parser))
22388 cp_parser_simulate_error (parser);
22389 return error_mark_node;
22393 else
22395 /* Try a template-id. */
22396 decl = cp_parser_template_id (parser, template_keyword_p,
22397 check_dependency_p,
22398 tag_type,
22399 is_declaration);
22400 if (decl == error_mark_node)
22401 return error_mark_node;
22404 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22406 /* If this is a typename, create a TYPENAME_TYPE. */
22407 if (typename_p && decl != error_mark_node)
22409 decl = make_typename_type (scope, decl, typename_type,
22410 /*complain=*/tf_error);
22411 if (decl != error_mark_node)
22412 decl = TYPE_NAME (decl);
22415 decl = strip_using_decl (decl);
22417 /* Check to see that it is really the name of a class. */
22418 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22419 && identifier_p (TREE_OPERAND (decl, 0))
22420 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22421 /* Situations like this:
22423 template <typename T> struct A {
22424 typename T::template X<int>::I i;
22427 are problematic. Is `T::template X<int>' a class-name? The
22428 standard does not seem to be definitive, but there is no other
22429 valid interpretation of the following `::'. Therefore, those
22430 names are considered class-names. */
22432 decl = make_typename_type (scope, decl, tag_type, tf_error);
22433 if (decl != error_mark_node)
22434 decl = TYPE_NAME (decl);
22436 else if (TREE_CODE (decl) != TYPE_DECL
22437 || TREE_TYPE (decl) == error_mark_node
22438 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22439 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22440 /* In Objective-C 2.0, a classname followed by '.' starts a
22441 dot-syntax expression, and it's not a type-name. */
22442 || (c_dialect_objc ()
22443 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22444 && objc_is_class_name (decl)))
22445 decl = error_mark_node;
22447 if (decl == error_mark_node)
22448 cp_parser_error (parser, "expected class-name");
22449 else if (identifier && !parser->scope)
22450 maybe_note_name_used_in_class (identifier, decl);
22452 return decl;
22455 /* Parse a class-specifier.
22457 class-specifier:
22458 class-head { member-specification [opt] }
22460 Returns the TREE_TYPE representing the class. */
22462 static tree
22463 cp_parser_class_specifier_1 (cp_parser* parser)
22465 tree type;
22466 tree attributes = NULL_TREE;
22467 bool nested_name_specifier_p;
22468 unsigned saved_num_template_parameter_lists;
22469 bool saved_in_function_body;
22470 unsigned char in_statement;
22471 bool in_switch_statement_p;
22472 bool saved_in_unbraced_linkage_specification_p;
22473 tree old_scope = NULL_TREE;
22474 tree scope = NULL_TREE;
22475 cp_token *closing_brace;
22477 push_deferring_access_checks (dk_no_deferred);
22479 /* Parse the class-head. */
22480 type = cp_parser_class_head (parser,
22481 &nested_name_specifier_p);
22482 /* If the class-head was a semantic disaster, skip the entire body
22483 of the class. */
22484 if (!type)
22486 cp_parser_skip_to_end_of_block_or_statement (parser);
22487 pop_deferring_access_checks ();
22488 return error_mark_node;
22491 /* Look for the `{'. */
22492 matching_braces braces;
22493 if (!braces.require_open (parser))
22495 pop_deferring_access_checks ();
22496 return error_mark_node;
22499 cp_ensure_no_omp_declare_simd (parser);
22500 cp_ensure_no_oacc_routine (parser);
22502 /* Issue an error message if type-definitions are forbidden here. */
22503 cp_parser_check_type_definition (parser);
22504 /* Remember that we are defining one more class. */
22505 ++parser->num_classes_being_defined;
22506 /* Inside the class, surrounding template-parameter-lists do not
22507 apply. */
22508 saved_num_template_parameter_lists
22509 = parser->num_template_parameter_lists;
22510 parser->num_template_parameter_lists = 0;
22511 /* We are not in a function body. */
22512 saved_in_function_body = parser->in_function_body;
22513 parser->in_function_body = false;
22514 /* Or in a loop. */
22515 in_statement = parser->in_statement;
22516 parser->in_statement = 0;
22517 /* Or in a switch. */
22518 in_switch_statement_p = parser->in_switch_statement_p;
22519 parser->in_switch_statement_p = false;
22520 /* We are not immediately inside an extern "lang" block. */
22521 saved_in_unbraced_linkage_specification_p
22522 = parser->in_unbraced_linkage_specification_p;
22523 parser->in_unbraced_linkage_specification_p = false;
22525 // Associate constraints with the type.
22526 if (flag_concepts)
22527 type = associate_classtype_constraints (type);
22529 /* Start the class. */
22530 if (nested_name_specifier_p)
22532 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22533 old_scope = push_inner_scope (scope);
22535 type = begin_class_definition (type);
22537 if (type == error_mark_node)
22538 /* If the type is erroneous, skip the entire body of the class. */
22539 cp_parser_skip_to_closing_brace (parser);
22540 else
22541 /* Parse the member-specification. */
22542 cp_parser_member_specification_opt (parser);
22544 /* Look for the trailing `}'. */
22545 closing_brace = braces.require_close (parser);
22546 /* Look for trailing attributes to apply to this class. */
22547 if (cp_parser_allow_gnu_extensions_p (parser))
22548 attributes = cp_parser_gnu_attributes_opt (parser);
22549 if (type != error_mark_node)
22550 type = finish_struct (type, attributes);
22551 if (nested_name_specifier_p)
22552 pop_inner_scope (old_scope, scope);
22554 /* We've finished a type definition. Check for the common syntax
22555 error of forgetting a semicolon after the definition. We need to
22556 be careful, as we can't just check for not-a-semicolon and be done
22557 with it; the user might have typed:
22559 class X { } c = ...;
22560 class X { } *p = ...;
22562 and so forth. Instead, enumerate all the possible tokens that
22563 might follow this production; if we don't see one of them, then
22564 complain and silently insert the semicolon. */
22566 cp_token *token = cp_lexer_peek_token (parser->lexer);
22567 bool want_semicolon = true;
22569 if (cp_next_tokens_can_be_std_attribute_p (parser))
22570 /* Don't try to parse c++11 attributes here. As per the
22571 grammar, that should be a task for
22572 cp_parser_decl_specifier_seq. */
22573 want_semicolon = false;
22575 switch (token->type)
22577 case CPP_NAME:
22578 case CPP_SEMICOLON:
22579 case CPP_MULT:
22580 case CPP_AND:
22581 case CPP_OPEN_PAREN:
22582 case CPP_CLOSE_PAREN:
22583 case CPP_COMMA:
22584 want_semicolon = false;
22585 break;
22587 /* While it's legal for type qualifiers and storage class
22588 specifiers to follow type definitions in the grammar, only
22589 compiler testsuites contain code like that. Assume that if
22590 we see such code, then what we're really seeing is a case
22591 like:
22593 class X { }
22594 const <type> var = ...;
22598 class Y { }
22599 static <type> func (...) ...
22601 i.e. the qualifier or specifier applies to the next
22602 declaration. To do so, however, we need to look ahead one
22603 more token to see if *that* token is a type specifier.
22605 This code could be improved to handle:
22607 class Z { }
22608 static const <type> var = ...; */
22609 case CPP_KEYWORD:
22610 if (keyword_is_decl_specifier (token->keyword))
22612 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22614 /* Handling user-defined types here would be nice, but very
22615 tricky. */
22616 want_semicolon
22617 = (lookahead->type == CPP_KEYWORD
22618 && keyword_begins_type_specifier (lookahead->keyword));
22620 break;
22621 default:
22622 break;
22625 /* If we don't have a type, then something is very wrong and we
22626 shouldn't try to do anything clever. Likewise for not seeing the
22627 closing brace. */
22628 if (closing_brace && TYPE_P (type) && want_semicolon)
22630 /* Locate the closing brace. */
22631 cp_token_position prev
22632 = cp_lexer_previous_token_position (parser->lexer);
22633 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22634 location_t loc = prev_token->location;
22636 /* We want to suggest insertion of a ';' immediately *after* the
22637 closing brace, so, if we can, offset the location by 1 column. */
22638 location_t next_loc = loc;
22639 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22640 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22642 rich_location richloc (line_table, next_loc);
22644 /* If we successfully offset the location, suggest the fix-it. */
22645 if (next_loc != loc)
22646 richloc.add_fixit_insert_before (next_loc, ";");
22648 if (CLASSTYPE_DECLARED_CLASS (type))
22649 error_at (&richloc,
22650 "expected %<;%> after class definition");
22651 else if (TREE_CODE (type) == RECORD_TYPE)
22652 error_at (&richloc,
22653 "expected %<;%> after struct definition");
22654 else if (TREE_CODE (type) == UNION_TYPE)
22655 error_at (&richloc,
22656 "expected %<;%> after union definition");
22657 else
22658 gcc_unreachable ();
22660 /* Unget one token and smash it to look as though we encountered
22661 a semicolon in the input stream. */
22662 cp_lexer_set_token_position (parser->lexer, prev);
22663 token = cp_lexer_peek_token (parser->lexer);
22664 token->type = CPP_SEMICOLON;
22665 token->keyword = RID_MAX;
22669 /* If this class is not itself within the scope of another class,
22670 then we need to parse the bodies of all of the queued function
22671 definitions. Note that the queued functions defined in a class
22672 are not always processed immediately following the
22673 class-specifier for that class. Consider:
22675 struct A {
22676 struct B { void f() { sizeof (A); } };
22679 If `f' were processed before the processing of `A' were
22680 completed, there would be no way to compute the size of `A'.
22681 Note that the nesting we are interested in here is lexical --
22682 not the semantic nesting given by TYPE_CONTEXT. In particular,
22683 for:
22685 struct A { struct B; };
22686 struct A::B { void f() { } };
22688 there is no need to delay the parsing of `A::B::f'. */
22689 if (--parser->num_classes_being_defined == 0)
22691 tree decl;
22692 tree class_type = NULL_TREE;
22693 tree pushed_scope = NULL_TREE;
22694 unsigned ix;
22695 cp_default_arg_entry *e;
22696 tree save_ccp, save_ccr;
22698 if (any_erroneous_template_args_p (type))
22700 /* Skip default arguments, NSDMIs, etc, in order to improve
22701 error recovery (c++/71169, c++/71832). */
22702 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22703 vec_safe_truncate (unparsed_nsdmis, 0);
22704 vec_safe_truncate (unparsed_classes, 0);
22705 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22708 /* In a first pass, parse default arguments to the functions.
22709 Then, in a second pass, parse the bodies of the functions.
22710 This two-phased approach handles cases like:
22712 struct S {
22713 void f() { g(); }
22714 void g(int i = 3);
22718 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22720 decl = e->decl;
22721 /* If there are default arguments that have not yet been processed,
22722 take care of them now. */
22723 if (class_type != e->class_type)
22725 if (pushed_scope)
22726 pop_scope (pushed_scope);
22727 class_type = e->class_type;
22728 pushed_scope = push_scope (class_type);
22730 /* Make sure that any template parameters are in scope. */
22731 maybe_begin_member_template_processing (decl);
22732 /* Parse the default argument expressions. */
22733 cp_parser_late_parsing_default_args (parser, decl);
22734 /* Remove any template parameters from the symbol table. */
22735 maybe_end_member_template_processing ();
22737 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22738 /* Now parse any NSDMIs. */
22739 save_ccp = current_class_ptr;
22740 save_ccr = current_class_ref;
22741 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22743 if (class_type != DECL_CONTEXT (decl))
22745 if (pushed_scope)
22746 pop_scope (pushed_scope);
22747 class_type = DECL_CONTEXT (decl);
22748 pushed_scope = push_scope (class_type);
22750 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22751 cp_parser_late_parsing_nsdmi (parser, decl);
22753 vec_safe_truncate (unparsed_nsdmis, 0);
22754 current_class_ptr = save_ccp;
22755 current_class_ref = save_ccr;
22756 if (pushed_scope)
22757 pop_scope (pushed_scope);
22759 /* Now do some post-NSDMI bookkeeping. */
22760 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22761 after_nsdmi_defaulted_late_checks (class_type);
22762 vec_safe_truncate (unparsed_classes, 0);
22763 after_nsdmi_defaulted_late_checks (type);
22765 /* Now parse the body of the functions. */
22766 if (flag_openmp)
22768 /* OpenMP UDRs need to be parsed before all other functions. */
22769 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22770 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22771 cp_parser_late_parsing_for_member (parser, decl);
22772 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22773 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22774 cp_parser_late_parsing_for_member (parser, decl);
22776 else
22777 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22778 cp_parser_late_parsing_for_member (parser, decl);
22779 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22781 else
22782 vec_safe_push (unparsed_classes, type);
22784 /* Put back any saved access checks. */
22785 pop_deferring_access_checks ();
22787 /* Restore saved state. */
22788 parser->in_switch_statement_p = in_switch_statement_p;
22789 parser->in_statement = in_statement;
22790 parser->in_function_body = saved_in_function_body;
22791 parser->num_template_parameter_lists
22792 = saved_num_template_parameter_lists;
22793 parser->in_unbraced_linkage_specification_p
22794 = saved_in_unbraced_linkage_specification_p;
22796 return type;
22799 static tree
22800 cp_parser_class_specifier (cp_parser* parser)
22802 tree ret;
22803 timevar_push (TV_PARSE_STRUCT);
22804 ret = cp_parser_class_specifier_1 (parser);
22805 timevar_pop (TV_PARSE_STRUCT);
22806 return ret;
22809 /* Parse a class-head.
22811 class-head:
22812 class-key identifier [opt] base-clause [opt]
22813 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22814 class-key nested-name-specifier [opt] template-id
22815 base-clause [opt]
22817 class-virt-specifier:
22818 final
22820 GNU Extensions:
22821 class-key attributes identifier [opt] base-clause [opt]
22822 class-key attributes nested-name-specifier identifier base-clause [opt]
22823 class-key attributes nested-name-specifier [opt] template-id
22824 base-clause [opt]
22826 Upon return BASES is initialized to the list of base classes (or
22827 NULL, if there are none) in the same form returned by
22828 cp_parser_base_clause.
22830 Returns the TYPE of the indicated class. Sets
22831 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22832 involving a nested-name-specifier was used, and FALSE otherwise.
22834 Returns error_mark_node if this is not a class-head.
22836 Returns NULL_TREE if the class-head is syntactically valid, but
22837 semantically invalid in a way that means we should skip the entire
22838 body of the class. */
22840 static tree
22841 cp_parser_class_head (cp_parser* parser,
22842 bool* nested_name_specifier_p)
22844 tree nested_name_specifier;
22845 enum tag_types class_key;
22846 tree id = NULL_TREE;
22847 tree type = NULL_TREE;
22848 tree attributes;
22849 tree bases;
22850 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22851 bool template_id_p = false;
22852 bool qualified_p = false;
22853 bool invalid_nested_name_p = false;
22854 bool invalid_explicit_specialization_p = false;
22855 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22856 tree pushed_scope = NULL_TREE;
22857 unsigned num_templates;
22858 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22859 /* Assume no nested-name-specifier will be present. */
22860 *nested_name_specifier_p = false;
22861 /* Assume no template parameter lists will be used in defining the
22862 type. */
22863 num_templates = 0;
22864 parser->colon_corrects_to_scope_p = false;
22866 /* Look for the class-key. */
22867 class_key = cp_parser_class_key (parser);
22868 if (class_key == none_type)
22869 return error_mark_node;
22871 location_t class_head_start_location = input_location;
22873 /* Parse the attributes. */
22874 attributes = cp_parser_attributes_opt (parser);
22876 /* If the next token is `::', that is invalid -- but sometimes
22877 people do try to write:
22879 struct ::S {};
22881 Handle this gracefully by accepting the extra qualifier, and then
22882 issuing an error about it later if this really is a
22883 class-head. If it turns out just to be an elaborated type
22884 specifier, remain silent. */
22885 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22886 qualified_p = true;
22888 push_deferring_access_checks (dk_no_check);
22890 /* Determine the name of the class. Begin by looking for an
22891 optional nested-name-specifier. */
22892 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22893 nested_name_specifier
22894 = cp_parser_nested_name_specifier_opt (parser,
22895 /*typename_keyword_p=*/false,
22896 /*check_dependency_p=*/false,
22897 /*type_p=*/true,
22898 /*is_declaration=*/false);
22899 /* If there was a nested-name-specifier, then there *must* be an
22900 identifier. */
22902 cp_token *bad_template_keyword = NULL;
22904 if (nested_name_specifier)
22906 type_start_token = cp_lexer_peek_token (parser->lexer);
22907 /* Although the grammar says `identifier', it really means
22908 `class-name' or `template-name'. You are only allowed to
22909 define a class that has already been declared with this
22910 syntax.
22912 The proposed resolution for Core Issue 180 says that wherever
22913 you see `class T::X' you should treat `X' as a type-name.
22915 It is OK to define an inaccessible class; for example:
22917 class A { class B; };
22918 class A::B {};
22920 We do not know if we will see a class-name, or a
22921 template-name. We look for a class-name first, in case the
22922 class-name is a template-id; if we looked for the
22923 template-name first we would stop after the template-name. */
22924 cp_parser_parse_tentatively (parser);
22925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22926 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22927 type = cp_parser_class_name (parser,
22928 /*typename_keyword_p=*/false,
22929 /*template_keyword_p=*/false,
22930 class_type,
22931 /*check_dependency_p=*/false,
22932 /*class_head_p=*/true,
22933 /*is_declaration=*/false);
22934 /* If that didn't work, ignore the nested-name-specifier. */
22935 if (!cp_parser_parse_definitely (parser))
22937 invalid_nested_name_p = true;
22938 type_start_token = cp_lexer_peek_token (parser->lexer);
22939 id = cp_parser_identifier (parser);
22940 if (id == error_mark_node)
22941 id = NULL_TREE;
22943 /* If we could not find a corresponding TYPE, treat this
22944 declaration like an unqualified declaration. */
22945 if (type == error_mark_node)
22946 nested_name_specifier = NULL_TREE;
22947 /* Otherwise, count the number of templates used in TYPE and its
22948 containing scopes. */
22949 else
22951 tree scope;
22953 for (scope = TREE_TYPE (type);
22954 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22955 scope = get_containing_scope (scope))
22956 if (TYPE_P (scope)
22957 && CLASS_TYPE_P (scope)
22958 && CLASSTYPE_TEMPLATE_INFO (scope)
22959 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22960 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22961 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22962 ++num_templates;
22965 /* Otherwise, the identifier is optional. */
22966 else
22968 /* We don't know whether what comes next is a template-id,
22969 an identifier, or nothing at all. */
22970 cp_parser_parse_tentatively (parser);
22971 /* Check for a template-id. */
22972 type_start_token = cp_lexer_peek_token (parser->lexer);
22973 id = cp_parser_template_id (parser,
22974 /*template_keyword_p=*/false,
22975 /*check_dependency_p=*/true,
22976 class_key,
22977 /*is_declaration=*/true);
22978 /* If that didn't work, it could still be an identifier. */
22979 if (!cp_parser_parse_definitely (parser))
22981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22983 type_start_token = cp_lexer_peek_token (parser->lexer);
22984 id = cp_parser_identifier (parser);
22986 else
22987 id = NULL_TREE;
22989 else
22991 template_id_p = true;
22992 ++num_templates;
22996 pop_deferring_access_checks ();
22998 if (id)
23000 cp_parser_check_for_invalid_template_id (parser, id,
23001 class_key,
23002 type_start_token->location);
23004 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23006 /* If it's not a `:' or a `{' then we can't really be looking at a
23007 class-head, since a class-head only appears as part of a
23008 class-specifier. We have to detect this situation before calling
23009 xref_tag, since that has irreversible side-effects. */
23010 if (!cp_parser_next_token_starts_class_definition_p (parser))
23012 cp_parser_error (parser, "expected %<{%> or %<:%>");
23013 type = error_mark_node;
23014 goto out;
23017 /* At this point, we're going ahead with the class-specifier, even
23018 if some other problem occurs. */
23019 cp_parser_commit_to_tentative_parse (parser);
23020 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23022 cp_parser_error (parser,
23023 "cannot specify %<override%> for a class");
23024 type = error_mark_node;
23025 goto out;
23027 /* Issue the error about the overly-qualified name now. */
23028 if (qualified_p)
23030 cp_parser_error (parser,
23031 "global qualification of class name is invalid");
23032 type = error_mark_node;
23033 goto out;
23035 else if (invalid_nested_name_p)
23037 cp_parser_error (parser,
23038 "qualified name does not name a class");
23039 type = error_mark_node;
23040 goto out;
23042 else if (nested_name_specifier)
23044 tree scope;
23046 if (bad_template_keyword)
23047 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23048 keyword template shall not appear at the top level. */
23049 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23050 "keyword %<template%> not allowed in class-head-name");
23052 /* Reject typedef-names in class heads. */
23053 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23055 error_at (type_start_token->location,
23056 "invalid class name in declaration of %qD",
23057 type);
23058 type = NULL_TREE;
23059 goto done;
23062 /* Figure out in what scope the declaration is being placed. */
23063 scope = current_scope ();
23064 /* If that scope does not contain the scope in which the
23065 class was originally declared, the program is invalid. */
23066 if (scope && !is_ancestor (scope, nested_name_specifier))
23068 if (at_namespace_scope_p ())
23069 error_at (type_start_token->location,
23070 "declaration of %qD in namespace %qD which does not "
23071 "enclose %qD",
23072 type, scope, nested_name_specifier);
23073 else
23074 error_at (type_start_token->location,
23075 "declaration of %qD in %qD which does not enclose %qD",
23076 type, scope, nested_name_specifier);
23077 type = NULL_TREE;
23078 goto done;
23080 /* [dcl.meaning]
23082 A declarator-id shall not be qualified except for the
23083 definition of a ... nested class outside of its class
23084 ... [or] the definition or explicit instantiation of a
23085 class member of a namespace outside of its namespace. */
23086 if (scope == nested_name_specifier)
23088 permerror (nested_name_specifier_token_start->location,
23089 "extra qualification not allowed");
23090 nested_name_specifier = NULL_TREE;
23091 num_templates = 0;
23094 /* An explicit-specialization must be preceded by "template <>". If
23095 it is not, try to recover gracefully. */
23096 if (at_namespace_scope_p ()
23097 && parser->num_template_parameter_lists == 0
23098 && !processing_template_parmlist
23099 && template_id_p)
23101 /* Build a location of this form:
23102 struct typename <ARGS>
23103 ^~~~~~~~~~~~~~~~~~~~~~
23104 with caret==start at the start token, and
23105 finishing at the end of the type. */
23106 location_t reported_loc
23107 = make_location (class_head_start_location,
23108 class_head_start_location,
23109 get_finish (type_start_token->location));
23110 rich_location richloc (line_table, reported_loc);
23111 richloc.add_fixit_insert_before (class_head_start_location,
23112 "template <> ");
23113 error_at (&richloc,
23114 "an explicit specialization must be preceded by"
23115 " %<template <>%>");
23116 invalid_explicit_specialization_p = true;
23117 /* Take the same action that would have been taken by
23118 cp_parser_explicit_specialization. */
23119 ++parser->num_template_parameter_lists;
23120 begin_specialization ();
23122 /* There must be no "return" statements between this point and the
23123 end of this function; set "type "to the correct return value and
23124 use "goto done;" to return. */
23125 /* Make sure that the right number of template parameters were
23126 present. */
23127 if (!cp_parser_check_template_parameters (parser, num_templates,
23128 template_id_p,
23129 type_start_token->location,
23130 /*declarator=*/NULL))
23132 /* If something went wrong, there is no point in even trying to
23133 process the class-definition. */
23134 type = NULL_TREE;
23135 goto done;
23138 /* Look up the type. */
23139 if (template_id_p)
23141 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23142 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23143 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23145 error_at (type_start_token->location,
23146 "function template %qD redeclared as a class template", id);
23147 type = error_mark_node;
23149 else
23151 type = TREE_TYPE (id);
23152 type = maybe_process_partial_specialization (type);
23154 /* Check the scope while we still know whether or not we had a
23155 nested-name-specifier. */
23156 if (type != error_mark_node)
23157 check_unqualified_spec_or_inst (type, type_start_token->location);
23159 if (nested_name_specifier)
23160 pushed_scope = push_scope (nested_name_specifier);
23162 else if (nested_name_specifier)
23164 tree class_type;
23166 /* Given:
23168 template <typename T> struct S { struct T };
23169 template <typename T> struct S<T>::T { };
23171 we will get a TYPENAME_TYPE when processing the definition of
23172 `S::T'. We need to resolve it to the actual type before we
23173 try to define it. */
23174 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23176 class_type = resolve_typename_type (TREE_TYPE (type),
23177 /*only_current_p=*/false);
23178 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23179 type = TYPE_NAME (class_type);
23180 else
23182 cp_parser_error (parser, "could not resolve typename type");
23183 type = error_mark_node;
23187 if (maybe_process_partial_specialization (TREE_TYPE (type))
23188 == error_mark_node)
23190 type = NULL_TREE;
23191 goto done;
23194 class_type = current_class_type;
23195 /* Enter the scope indicated by the nested-name-specifier. */
23196 pushed_scope = push_scope (nested_name_specifier);
23197 /* Get the canonical version of this type. */
23198 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23199 /* Call push_template_decl if it seems like we should be defining a
23200 template either from the template headers or the type we're
23201 defining, so that we diagnose both extra and missing headers. */
23202 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23203 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23204 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23206 type = push_template_decl (type);
23207 if (type == error_mark_node)
23209 type = NULL_TREE;
23210 goto done;
23214 type = TREE_TYPE (type);
23215 *nested_name_specifier_p = true;
23217 else /* The name is not a nested name. */
23219 /* If the class was unnamed, create a dummy name. */
23220 if (!id)
23221 id = make_anon_name ();
23222 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23223 ? ts_within_enclosing_non_class
23224 : ts_current);
23225 type = xref_tag (class_key, id, tag_scope,
23226 parser->num_template_parameter_lists);
23229 /* Indicate whether this class was declared as a `class' or as a
23230 `struct'. */
23231 if (TREE_CODE (type) == RECORD_TYPE)
23232 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23233 cp_parser_check_class_key (class_key, type);
23235 /* If this type was already complete, and we see another definition,
23236 that's an error. */
23237 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23239 error_at (type_start_token->location, "redefinition of %q#T",
23240 type);
23241 inform (location_of (type), "previous definition of %q#T",
23242 type);
23243 type = NULL_TREE;
23244 goto done;
23246 else if (type == error_mark_node)
23247 type = NULL_TREE;
23249 if (type)
23251 /* Apply attributes now, before any use of the class as a template
23252 argument in its base list. */
23253 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23254 fixup_attribute_variants (type);
23257 /* We will have entered the scope containing the class; the names of
23258 base classes should be looked up in that context. For example:
23260 struct A { struct B {}; struct C; };
23261 struct A::C : B {};
23263 is valid. */
23265 /* Get the list of base-classes, if there is one. */
23266 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23268 /* PR59482: enter the class scope so that base-specifiers are looked
23269 up correctly. */
23270 if (type)
23271 pushclass (type);
23272 bases = cp_parser_base_clause (parser);
23273 /* PR59482: get out of the previously pushed class scope so that the
23274 subsequent pops pop the right thing. */
23275 if (type)
23276 popclass ();
23278 else
23279 bases = NULL_TREE;
23281 /* If we're really defining a class, process the base classes.
23282 If they're invalid, fail. */
23283 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23284 xref_basetypes (type, bases);
23286 done:
23287 /* Leave the scope given by the nested-name-specifier. We will
23288 enter the class scope itself while processing the members. */
23289 if (pushed_scope)
23290 pop_scope (pushed_scope);
23292 if (invalid_explicit_specialization_p)
23294 end_specialization ();
23295 --parser->num_template_parameter_lists;
23298 if (type)
23299 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23300 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23301 CLASSTYPE_FINAL (type) = 1;
23302 out:
23303 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23304 return type;
23307 /* Parse a class-key.
23309 class-key:
23310 class
23311 struct
23312 union
23314 Returns the kind of class-key specified, or none_type to indicate
23315 error. */
23317 static enum tag_types
23318 cp_parser_class_key (cp_parser* parser)
23320 cp_token *token;
23321 enum tag_types tag_type;
23323 /* Look for the class-key. */
23324 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23325 if (!token)
23326 return none_type;
23328 /* Check to see if the TOKEN is a class-key. */
23329 tag_type = cp_parser_token_is_class_key (token);
23330 if (!tag_type)
23331 cp_parser_error (parser, "expected class-key");
23332 return tag_type;
23335 /* Parse a type-parameter-key.
23337 type-parameter-key:
23338 class
23339 typename
23342 static void
23343 cp_parser_type_parameter_key (cp_parser* parser)
23345 /* Look for the type-parameter-key. */
23346 enum tag_types tag_type = none_type;
23347 cp_token *token = cp_lexer_peek_token (parser->lexer);
23348 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23350 cp_lexer_consume_token (parser->lexer);
23351 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23352 /* typename is not allowed in a template template parameter
23353 by the standard until C++17. */
23354 pedwarn (token->location, OPT_Wpedantic,
23355 "ISO C++ forbids typename key in template template parameter;"
23356 " use -std=c++17 or -std=gnu++17");
23358 else
23359 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23361 return;
23364 /* Parse an (optional) member-specification.
23366 member-specification:
23367 member-declaration member-specification [opt]
23368 access-specifier : member-specification [opt] */
23370 static void
23371 cp_parser_member_specification_opt (cp_parser* parser)
23373 while (true)
23375 cp_token *token;
23376 enum rid keyword;
23378 /* Peek at the next token. */
23379 token = cp_lexer_peek_token (parser->lexer);
23380 /* If it's a `}', or EOF then we've seen all the members. */
23381 if (token->type == CPP_CLOSE_BRACE
23382 || token->type == CPP_EOF
23383 || token->type == CPP_PRAGMA_EOL)
23384 break;
23386 /* See if this token is a keyword. */
23387 keyword = token->keyword;
23388 switch (keyword)
23390 case RID_PUBLIC:
23391 case RID_PROTECTED:
23392 case RID_PRIVATE:
23393 /* Consume the access-specifier. */
23394 cp_lexer_consume_token (parser->lexer);
23395 /* Remember which access-specifier is active. */
23396 current_access_specifier = token->u.value;
23397 /* Look for the `:'. */
23398 cp_parser_require (parser, CPP_COLON, RT_COLON);
23399 break;
23401 default:
23402 /* Accept #pragmas at class scope. */
23403 if (token->type == CPP_PRAGMA)
23405 cp_parser_pragma (parser, pragma_member, NULL);
23406 break;
23409 /* Otherwise, the next construction must be a
23410 member-declaration. */
23411 cp_parser_member_declaration (parser);
23416 /* Parse a member-declaration.
23418 member-declaration:
23419 decl-specifier-seq [opt] member-declarator-list [opt] ;
23420 function-definition ; [opt]
23421 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23422 using-declaration
23423 template-declaration
23424 alias-declaration
23426 member-declarator-list:
23427 member-declarator
23428 member-declarator-list , member-declarator
23430 member-declarator:
23431 declarator pure-specifier [opt]
23432 declarator constant-initializer [opt]
23433 identifier [opt] : constant-expression
23435 GNU Extensions:
23437 member-declaration:
23438 __extension__ member-declaration
23440 member-declarator:
23441 declarator attributes [opt] pure-specifier [opt]
23442 declarator attributes [opt] constant-initializer [opt]
23443 identifier [opt] attributes [opt] : constant-expression
23445 C++0x Extensions:
23447 member-declaration:
23448 static_assert-declaration */
23450 static void
23451 cp_parser_member_declaration (cp_parser* parser)
23453 cp_decl_specifier_seq decl_specifiers;
23454 tree prefix_attributes;
23455 tree decl;
23456 int declares_class_or_enum;
23457 bool friend_p;
23458 cp_token *token = NULL;
23459 cp_token *decl_spec_token_start = NULL;
23460 cp_token *initializer_token_start = NULL;
23461 int saved_pedantic;
23462 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23464 /* Check for the `__extension__' keyword. */
23465 if (cp_parser_extension_opt (parser, &saved_pedantic))
23467 /* Recurse. */
23468 cp_parser_member_declaration (parser);
23469 /* Restore the old value of the PEDANTIC flag. */
23470 pedantic = saved_pedantic;
23472 return;
23475 /* Check for a template-declaration. */
23476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23478 /* An explicit specialization here is an error condition, and we
23479 expect the specialization handler to detect and report this. */
23480 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23481 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23482 cp_parser_explicit_specialization (parser);
23483 else
23484 cp_parser_template_declaration (parser, /*member_p=*/true);
23486 return;
23488 /* Check for a template introduction. */
23489 else if (cp_parser_template_declaration_after_export (parser, true))
23490 return;
23492 /* Check for a using-declaration. */
23493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23495 if (cxx_dialect < cxx11)
23497 /* Parse the using-declaration. */
23498 cp_parser_using_declaration (parser,
23499 /*access_declaration_p=*/false);
23500 return;
23502 else
23504 tree decl;
23505 bool alias_decl_expected;
23506 cp_parser_parse_tentatively (parser);
23507 decl = cp_parser_alias_declaration (parser);
23508 /* Note that if we actually see the '=' token after the
23509 identifier, cp_parser_alias_declaration commits the
23510 tentative parse. In that case, we really expect an
23511 alias-declaration. Otherwise, we expect a using
23512 declaration. */
23513 alias_decl_expected =
23514 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23515 cp_parser_parse_definitely (parser);
23517 if (alias_decl_expected)
23518 finish_member_declaration (decl);
23519 else
23520 cp_parser_using_declaration (parser,
23521 /*access_declaration_p=*/false);
23522 return;
23526 /* Check for @defs. */
23527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23529 tree ivar, member;
23530 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23531 ivar = ivar_chains;
23532 while (ivar)
23534 member = ivar;
23535 ivar = TREE_CHAIN (member);
23536 TREE_CHAIN (member) = NULL_TREE;
23537 finish_member_declaration (member);
23539 return;
23542 /* If the next token is `static_assert' we have a static assertion. */
23543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23545 cp_parser_static_assert (parser, /*member_p=*/true);
23546 return;
23549 parser->colon_corrects_to_scope_p = false;
23551 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23552 goto out;
23554 /* Parse the decl-specifier-seq. */
23555 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23556 cp_parser_decl_specifier_seq (parser,
23557 CP_PARSER_FLAGS_OPTIONAL,
23558 &decl_specifiers,
23559 &declares_class_or_enum);
23560 /* Check for an invalid type-name. */
23561 if (!decl_specifiers.any_type_specifiers_p
23562 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23563 goto out;
23564 /* If there is no declarator, then the decl-specifier-seq should
23565 specify a type. */
23566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23568 /* If there was no decl-specifier-seq, and the next token is a
23569 `;', then we have something like:
23571 struct S { ; };
23573 [class.mem]
23575 Each member-declaration shall declare at least one member
23576 name of the class. */
23577 if (!decl_specifiers.any_specifiers_p)
23579 cp_token *token = cp_lexer_peek_token (parser->lexer);
23580 if (!in_system_header_at (token->location))
23582 gcc_rich_location richloc (token->location);
23583 richloc.add_fixit_remove ();
23584 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23587 else
23589 tree type;
23591 /* See if this declaration is a friend. */
23592 friend_p = cp_parser_friend_p (&decl_specifiers);
23593 /* If there were decl-specifiers, check to see if there was
23594 a class-declaration. */
23595 type = check_tag_decl (&decl_specifiers,
23596 /*explicit_type_instantiation_p=*/false);
23597 /* Nested classes have already been added to the class, but
23598 a `friend' needs to be explicitly registered. */
23599 if (friend_p)
23601 /* If the `friend' keyword was present, the friend must
23602 be introduced with a class-key. */
23603 if (!declares_class_or_enum && cxx_dialect < cxx11)
23604 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23605 "in C++03 a class-key must be used "
23606 "when declaring a friend");
23607 /* In this case:
23609 template <typename T> struct A {
23610 friend struct A<T>::B;
23613 A<T>::B will be represented by a TYPENAME_TYPE, and
23614 therefore not recognized by check_tag_decl. */
23615 if (!type)
23617 type = decl_specifiers.type;
23618 if (type && TREE_CODE (type) == TYPE_DECL)
23619 type = TREE_TYPE (type);
23621 if (!type || !TYPE_P (type))
23622 error_at (decl_spec_token_start->location,
23623 "friend declaration does not name a class or "
23624 "function");
23625 else
23626 make_friend_class (current_class_type, type,
23627 /*complain=*/true);
23629 /* If there is no TYPE, an error message will already have
23630 been issued. */
23631 else if (!type || type == error_mark_node)
23633 /* An anonymous aggregate has to be handled specially; such
23634 a declaration really declares a data member (with a
23635 particular type), as opposed to a nested class. */
23636 else if (ANON_AGGR_TYPE_P (type))
23638 /* C++11 9.5/6. */
23639 if (decl_specifiers.storage_class != sc_none)
23640 error_at (decl_spec_token_start->location,
23641 "a storage class on an anonymous aggregate "
23642 "in class scope is not allowed");
23644 /* Remove constructors and such from TYPE, now that we
23645 know it is an anonymous aggregate. */
23646 fixup_anonymous_aggr (type);
23647 /* And make the corresponding data member. */
23648 decl = build_decl (decl_spec_token_start->location,
23649 FIELD_DECL, NULL_TREE, type);
23650 /* Add it to the class. */
23651 finish_member_declaration (decl);
23653 else
23654 cp_parser_check_access_in_redeclaration
23655 (TYPE_NAME (type),
23656 decl_spec_token_start->location);
23659 else
23661 bool assume_semicolon = false;
23663 /* Clear attributes from the decl_specifiers but keep them
23664 around as prefix attributes that apply them to the entity
23665 being declared. */
23666 prefix_attributes = decl_specifiers.attributes;
23667 decl_specifiers.attributes = NULL_TREE;
23669 /* See if these declarations will be friends. */
23670 friend_p = cp_parser_friend_p (&decl_specifiers);
23672 /* Keep going until we hit the `;' at the end of the
23673 declaration. */
23674 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23676 tree attributes = NULL_TREE;
23677 tree first_attribute;
23678 tree initializer;
23679 bool named_bitfld = false;
23681 /* Peek at the next token. */
23682 token = cp_lexer_peek_token (parser->lexer);
23684 /* The following code wants to know early if it is a bit-field
23685 or some other declaration. Attributes can appear before
23686 the `:' token. Skip over them without consuming any tokens
23687 to peek if they are followed by `:'. */
23688 if (cp_next_tokens_can_be_attribute_p (parser)
23689 || (token->type == CPP_NAME
23690 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23691 && (named_bitfld = true)))
23693 size_t n
23694 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23695 token = cp_lexer_peek_nth_token (parser->lexer, n);
23698 /* Check for a bitfield declaration. */
23699 if (token->type == CPP_COLON
23700 || (token->type == CPP_NAME
23701 && token == cp_lexer_peek_token (parser->lexer)
23702 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23703 && (named_bitfld = true)))
23705 tree identifier;
23706 tree width;
23707 tree late_attributes = NULL_TREE;
23709 if (named_bitfld)
23710 identifier = cp_parser_identifier (parser);
23711 else
23712 identifier = NULL_TREE;
23714 /* Look for attributes that apply to the bitfield. */
23715 attributes = cp_parser_attributes_opt (parser);
23717 /* Consume the `:' token. */
23718 cp_lexer_consume_token (parser->lexer);
23720 /* Get the width of the bitfield. */
23721 width = cp_parser_constant_expression (parser, false, NULL,
23722 cxx_dialect >= cxx11);
23724 /* In C++2A and as extension for C++11 and above we allow
23725 default member initializers for bit-fields. */
23726 initializer = NULL_TREE;
23727 if (cxx_dialect >= cxx11
23728 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23729 || cp_lexer_next_token_is (parser->lexer,
23730 CPP_OPEN_BRACE)))
23732 location_t loc
23733 = cp_lexer_peek_token (parser->lexer)->location;
23734 if (cxx_dialect < cxx2a
23735 && !in_system_header_at (loc)
23736 && identifier != NULL_TREE)
23737 pedwarn (loc, 0,
23738 "default member initializers for bit-fields "
23739 "only available with -std=c++2a or "
23740 "-std=gnu++2a");
23742 initializer = cp_parser_save_nsdmi (parser);
23743 if (identifier == NULL_TREE)
23745 error_at (loc, "default member initializer for "
23746 "unnamed bit-field");
23747 initializer = NULL_TREE;
23750 else
23752 /* Look for attributes that apply to the bitfield after
23753 the `:' token and width. This is where GCC used to
23754 parse attributes in the past, pedwarn if there is
23755 a std attribute. */
23756 if (cp_next_tokens_can_be_std_attribute_p (parser))
23757 pedwarn (input_location, OPT_Wpedantic,
23758 "ISO C++ allows bit-field attributes only "
23759 "before the %<:%> token");
23761 late_attributes = cp_parser_attributes_opt (parser);
23764 attributes = attr_chainon (attributes, late_attributes);
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 /* Create the bitfield declaration. */
23773 decl = grokbitfield (identifier
23774 ? make_id_declarator (NULL_TREE,
23775 identifier,
23776 sfk_none)
23777 : NULL,
23778 &decl_specifiers,
23779 width, initializer,
23780 attributes);
23782 else
23784 cp_declarator *declarator;
23785 tree asm_specification;
23786 int ctor_dtor_or_conv_p;
23788 /* Parse the declarator. */
23789 declarator
23790 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23791 &ctor_dtor_or_conv_p,
23792 /*parenthesized_p=*/NULL,
23793 /*member_p=*/true,
23794 friend_p);
23796 /* If something went wrong parsing the declarator, make sure
23797 that we at least consume some tokens. */
23798 if (declarator == cp_error_declarator)
23800 /* Skip to the end of the statement. */
23801 cp_parser_skip_to_end_of_statement (parser);
23802 /* If the next token is not a semicolon, that is
23803 probably because we just skipped over the body of
23804 a function. So, we consume a semicolon if
23805 present, but do not issue an error message if it
23806 is not present. */
23807 if (cp_lexer_next_token_is (parser->lexer,
23808 CPP_SEMICOLON))
23809 cp_lexer_consume_token (parser->lexer);
23810 goto out;
23813 if (declares_class_or_enum & 2)
23814 cp_parser_check_for_definition_in_return_type
23815 (declarator, decl_specifiers.type,
23816 decl_specifiers.locations[ds_type_spec]);
23818 /* Look for an asm-specification. */
23819 asm_specification = cp_parser_asm_specification_opt (parser);
23820 /* Look for attributes that apply to the declaration. */
23821 attributes = cp_parser_attributes_opt (parser);
23822 /* Remember which attributes are prefix attributes and
23823 which are not. */
23824 first_attribute = attributes;
23825 /* Combine the attributes. */
23826 attributes = attr_chainon (prefix_attributes, attributes);
23828 /* If it's an `=', then we have a constant-initializer or a
23829 pure-specifier. It is not correct to parse the
23830 initializer before registering the member declaration
23831 since the member declaration should be in scope while
23832 its initializer is processed. However, the rest of the
23833 front end does not yet provide an interface that allows
23834 us to handle this correctly. */
23835 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23837 /* In [class.mem]:
23839 A pure-specifier shall be used only in the declaration of
23840 a virtual function.
23842 A member-declarator can contain a constant-initializer
23843 only if it declares a static member of integral or
23844 enumeration type.
23846 Therefore, if the DECLARATOR is for a function, we look
23847 for a pure-specifier; otherwise, we look for a
23848 constant-initializer. When we call `grokfield', it will
23849 perform more stringent semantics checks. */
23850 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23851 if (function_declarator_p (declarator)
23852 || (decl_specifiers.type
23853 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23854 && declarator->kind == cdk_id
23855 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23856 == FUNCTION_TYPE)))
23857 initializer = cp_parser_pure_specifier (parser);
23858 else if (decl_specifiers.storage_class != sc_static)
23859 initializer = cp_parser_save_nsdmi (parser);
23860 else if (cxx_dialect >= cxx11)
23862 bool nonconst;
23863 /* Don't require a constant rvalue in C++11, since we
23864 might want a reference constant. We'll enforce
23865 constancy later. */
23866 cp_lexer_consume_token (parser->lexer);
23867 /* Parse the initializer. */
23868 initializer = cp_parser_initializer_clause (parser,
23869 &nonconst);
23871 else
23872 /* Parse the initializer. */
23873 initializer = cp_parser_constant_initializer (parser);
23875 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23876 && !function_declarator_p (declarator))
23878 bool x;
23879 if (decl_specifiers.storage_class != sc_static)
23880 initializer = cp_parser_save_nsdmi (parser);
23881 else
23882 initializer = cp_parser_initializer (parser, &x, &x);
23884 /* Otherwise, there is no initializer. */
23885 else
23886 initializer = NULL_TREE;
23888 /* See if we are probably looking at a function
23889 definition. We are certainly not looking at a
23890 member-declarator. Calling `grokfield' has
23891 side-effects, so we must not do it unless we are sure
23892 that we are looking at a member-declarator. */
23893 if (cp_parser_token_starts_function_definition_p
23894 (cp_lexer_peek_token (parser->lexer)))
23896 /* The grammar does not allow a pure-specifier to be
23897 used when a member function is defined. (It is
23898 possible that this fact is an oversight in the
23899 standard, since a pure function may be defined
23900 outside of the class-specifier. */
23901 if (initializer && initializer_token_start)
23902 error_at (initializer_token_start->location,
23903 "pure-specifier on function-definition");
23904 decl = cp_parser_save_member_function_body (parser,
23905 &decl_specifiers,
23906 declarator,
23907 attributes);
23908 if (parser->fully_implicit_function_template_p)
23909 decl = finish_fully_implicit_template (parser, decl);
23910 /* If the member was not a friend, declare it here. */
23911 if (!friend_p)
23912 finish_member_declaration (decl);
23913 /* Peek at the next token. */
23914 token = cp_lexer_peek_token (parser->lexer);
23915 /* If the next token is a semicolon, consume it. */
23916 if (token->type == CPP_SEMICOLON)
23918 location_t semicolon_loc
23919 = cp_lexer_consume_token (parser->lexer)->location;
23920 gcc_rich_location richloc (semicolon_loc);
23921 richloc.add_fixit_remove ();
23922 warning_at (&richloc, OPT_Wextra_semi,
23923 "extra %<;%> after in-class "
23924 "function definition");
23926 goto out;
23928 else
23929 if (declarator->kind == cdk_function)
23930 declarator->id_loc = token->location;
23931 /* Create the declaration. */
23932 decl = grokfield (declarator, &decl_specifiers,
23933 initializer, /*init_const_expr_p=*/true,
23934 asm_specification, attributes);
23935 if (parser->fully_implicit_function_template_p)
23937 if (friend_p)
23938 finish_fully_implicit_template (parser, 0);
23939 else
23940 decl = finish_fully_implicit_template (parser, decl);
23944 cp_finalize_omp_declare_simd (parser, decl);
23945 cp_finalize_oacc_routine (parser, decl, false);
23947 /* Reset PREFIX_ATTRIBUTES. */
23948 if (attributes != error_mark_node)
23950 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23951 attributes = TREE_CHAIN (attributes);
23952 if (attributes)
23953 TREE_CHAIN (attributes) = NULL_TREE;
23956 /* If there is any qualification still in effect, clear it
23957 now; we will be starting fresh with the next declarator. */
23958 parser->scope = NULL_TREE;
23959 parser->qualifying_scope = NULL_TREE;
23960 parser->object_scope = NULL_TREE;
23961 /* If it's a `,', then there are more declarators. */
23962 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23964 cp_lexer_consume_token (parser->lexer);
23965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23967 cp_token *token = cp_lexer_previous_token (parser->lexer);
23968 gcc_rich_location richloc (token->location);
23969 richloc.add_fixit_remove ();
23970 error_at (&richloc, "stray %<,%> at end of "
23971 "member declaration");
23974 /* If the next token isn't a `;', then we have a parse error. */
23975 else if (cp_lexer_next_token_is_not (parser->lexer,
23976 CPP_SEMICOLON))
23978 /* The next token might be a ways away from where the
23979 actual semicolon is missing. Find the previous token
23980 and use that for our error position. */
23981 cp_token *token = cp_lexer_previous_token (parser->lexer);
23982 gcc_rich_location richloc (token->location);
23983 richloc.add_fixit_insert_after (";");
23984 error_at (&richloc, "expected %<;%> at end of "
23985 "member declaration");
23987 /* Assume that the user meant to provide a semicolon. If
23988 we were to cp_parser_skip_to_end_of_statement, we might
23989 skip to a semicolon inside a member function definition
23990 and issue nonsensical error messages. */
23991 assume_semicolon = true;
23994 if (decl)
23996 /* Add DECL to the list of members. */
23997 if (!friend_p
23998 /* Explicitly include, eg, NSDMIs, for better error
23999 recovery (c++/58650). */
24000 || !DECL_DECLARES_FUNCTION_P (decl))
24001 finish_member_declaration (decl);
24003 if (TREE_CODE (decl) == FUNCTION_DECL)
24004 cp_parser_save_default_args (parser, decl);
24005 else if (TREE_CODE (decl) == FIELD_DECL
24006 && DECL_INITIAL (decl))
24007 /* Add DECL to the queue of NSDMI to be parsed later. */
24008 vec_safe_push (unparsed_nsdmis, decl);
24011 if (assume_semicolon)
24012 goto out;
24016 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24017 out:
24018 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24021 /* Parse a pure-specifier.
24023 pure-specifier:
24026 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24027 Otherwise, ERROR_MARK_NODE is returned. */
24029 static tree
24030 cp_parser_pure_specifier (cp_parser* parser)
24032 cp_token *token;
24034 /* Look for the `=' token. */
24035 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24036 return error_mark_node;
24037 /* Look for the `0' token. */
24038 token = cp_lexer_peek_token (parser->lexer);
24040 if (token->type == CPP_EOF
24041 || token->type == CPP_PRAGMA_EOL)
24042 return error_mark_node;
24044 cp_lexer_consume_token (parser->lexer);
24046 /* Accept = default or = delete in c++0x mode. */
24047 if (token->keyword == RID_DEFAULT
24048 || token->keyword == RID_DELETE)
24050 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24051 return token->u.value;
24054 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24055 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24057 cp_parser_error (parser,
24058 "invalid pure specifier (only %<= 0%> is allowed)");
24059 cp_parser_skip_to_end_of_statement (parser);
24060 return error_mark_node;
24062 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24064 error_at (token->location, "templates may not be %<virtual%>");
24065 return error_mark_node;
24068 return integer_zero_node;
24071 /* Parse a constant-initializer.
24073 constant-initializer:
24074 = constant-expression
24076 Returns a representation of the constant-expression. */
24078 static tree
24079 cp_parser_constant_initializer (cp_parser* parser)
24081 /* Look for the `=' token. */
24082 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24083 return error_mark_node;
24085 /* It is invalid to write:
24087 struct S { static const int i = { 7 }; };
24090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24092 cp_parser_error (parser,
24093 "a brace-enclosed initializer is not allowed here");
24094 /* Consume the opening brace. */
24095 matching_braces braces;
24096 braces.consume_open (parser);
24097 /* Skip the initializer. */
24098 cp_parser_skip_to_closing_brace (parser);
24099 /* Look for the trailing `}'. */
24100 braces.require_close (parser);
24102 return error_mark_node;
24105 return cp_parser_constant_expression (parser);
24108 /* Derived classes [gram.class.derived] */
24110 /* Parse a base-clause.
24112 base-clause:
24113 : base-specifier-list
24115 base-specifier-list:
24116 base-specifier ... [opt]
24117 base-specifier-list , base-specifier ... [opt]
24119 Returns a TREE_LIST representing the base-classes, in the order in
24120 which they were declared. The representation of each node is as
24121 described by cp_parser_base_specifier.
24123 In the case that no bases are specified, this function will return
24124 NULL_TREE, not ERROR_MARK_NODE. */
24126 static tree
24127 cp_parser_base_clause (cp_parser* parser)
24129 tree bases = NULL_TREE;
24131 /* Look for the `:' that begins the list. */
24132 cp_parser_require (parser, CPP_COLON, RT_COLON);
24134 /* Scan the base-specifier-list. */
24135 while (true)
24137 cp_token *token;
24138 tree base;
24139 bool pack_expansion_p = false;
24141 /* Look for the base-specifier. */
24142 base = cp_parser_base_specifier (parser);
24143 /* Look for the (optional) ellipsis. */
24144 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24146 /* Consume the `...'. */
24147 cp_lexer_consume_token (parser->lexer);
24149 pack_expansion_p = true;
24152 /* Add BASE to the front of the list. */
24153 if (base && base != error_mark_node)
24155 if (pack_expansion_p)
24156 /* Make this a pack expansion type. */
24157 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24159 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24161 TREE_CHAIN (base) = bases;
24162 bases = base;
24165 /* Peek at the next token. */
24166 token = cp_lexer_peek_token (parser->lexer);
24167 /* If it's not a comma, then the list is complete. */
24168 if (token->type != CPP_COMMA)
24169 break;
24170 /* Consume the `,'. */
24171 cp_lexer_consume_token (parser->lexer);
24174 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24175 base class had a qualified name. However, the next name that
24176 appears is certainly not qualified. */
24177 parser->scope = NULL_TREE;
24178 parser->qualifying_scope = NULL_TREE;
24179 parser->object_scope = NULL_TREE;
24181 return nreverse (bases);
24184 /* Parse a base-specifier.
24186 base-specifier:
24187 :: [opt] nested-name-specifier [opt] class-name
24188 virtual access-specifier [opt] :: [opt] nested-name-specifier
24189 [opt] class-name
24190 access-specifier virtual [opt] :: [opt] nested-name-specifier
24191 [opt] class-name
24193 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24194 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24195 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24196 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24198 static tree
24199 cp_parser_base_specifier (cp_parser* parser)
24201 cp_token *token;
24202 bool done = false;
24203 bool virtual_p = false;
24204 bool duplicate_virtual_error_issued_p = false;
24205 bool duplicate_access_error_issued_p = false;
24206 bool class_scope_p, template_p;
24207 tree access = access_default_node;
24208 tree type;
24210 /* Process the optional `virtual' and `access-specifier'. */
24211 while (!done)
24213 /* Peek at the next token. */
24214 token = cp_lexer_peek_token (parser->lexer);
24215 /* Process `virtual'. */
24216 switch (token->keyword)
24218 case RID_VIRTUAL:
24219 /* If `virtual' appears more than once, issue an error. */
24220 if (virtual_p && !duplicate_virtual_error_issued_p)
24222 cp_parser_error (parser,
24223 "%<virtual%> specified more than once in base-specifier");
24224 duplicate_virtual_error_issued_p = true;
24227 virtual_p = true;
24229 /* Consume the `virtual' token. */
24230 cp_lexer_consume_token (parser->lexer);
24232 break;
24234 case RID_PUBLIC:
24235 case RID_PROTECTED:
24236 case RID_PRIVATE:
24237 /* If more than one access specifier appears, issue an
24238 error. */
24239 if (access != access_default_node
24240 && !duplicate_access_error_issued_p)
24242 cp_parser_error (parser,
24243 "more than one access specifier in base-specifier");
24244 duplicate_access_error_issued_p = true;
24247 access = ridpointers[(int) token->keyword];
24249 /* Consume the access-specifier. */
24250 cp_lexer_consume_token (parser->lexer);
24252 break;
24254 default:
24255 done = true;
24256 break;
24259 /* It is not uncommon to see programs mechanically, erroneously, use
24260 the 'typename' keyword to denote (dependent) qualified types
24261 as base classes. */
24262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24264 token = cp_lexer_peek_token (parser->lexer);
24265 if (!processing_template_decl)
24266 error_at (token->location,
24267 "keyword %<typename%> not allowed outside of templates");
24268 else
24269 error_at (token->location,
24270 "keyword %<typename%> not allowed in this context "
24271 "(the base class is implicitly a type)");
24272 cp_lexer_consume_token (parser->lexer);
24275 /* Look for the optional `::' operator. */
24276 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24277 /* Look for the nested-name-specifier. The simplest way to
24278 implement:
24280 [temp.res]
24282 The keyword `typename' is not permitted in a base-specifier or
24283 mem-initializer; in these contexts a qualified name that
24284 depends on a template-parameter is implicitly assumed to be a
24285 type name.
24287 is to pretend that we have seen the `typename' keyword at this
24288 point. */
24289 cp_parser_nested_name_specifier_opt (parser,
24290 /*typename_keyword_p=*/true,
24291 /*check_dependency_p=*/true,
24292 /*type_p=*/true,
24293 /*is_declaration=*/true);
24294 /* If the base class is given by a qualified name, assume that names
24295 we see are type names or templates, as appropriate. */
24296 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24297 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24299 if (!parser->scope
24300 && cp_lexer_next_token_is_decltype (parser->lexer))
24301 /* DR 950 allows decltype as a base-specifier. */
24302 type = cp_parser_decltype (parser);
24303 else
24305 /* Otherwise, look for the class-name. */
24306 type = cp_parser_class_name (parser,
24307 class_scope_p,
24308 template_p,
24309 typename_type,
24310 /*check_dependency_p=*/true,
24311 /*class_head_p=*/false,
24312 /*is_declaration=*/true);
24313 type = TREE_TYPE (type);
24316 if (type == error_mark_node)
24317 return error_mark_node;
24319 return finish_base_specifier (type, access, virtual_p);
24322 /* Exception handling [gram.exception] */
24324 /* Parse an (optional) noexcept-specification.
24326 noexcept-specification:
24327 noexcept ( constant-expression ) [opt]
24329 If no noexcept-specification is present, returns NULL_TREE.
24330 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24331 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24332 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24333 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24334 in which case a boolean condition is returned instead. */
24336 static tree
24337 cp_parser_noexcept_specification_opt (cp_parser* parser,
24338 bool require_constexpr,
24339 bool* consumed_expr,
24340 bool return_cond)
24342 cp_token *token;
24343 const char *saved_message;
24345 /* Peek at the next token. */
24346 token = cp_lexer_peek_token (parser->lexer);
24348 /* Is it a noexcept-specification? */
24349 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24351 tree expr;
24352 cp_lexer_consume_token (parser->lexer);
24354 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24356 matching_parens parens;
24357 parens.consume_open (parser);
24359 if (require_constexpr)
24361 /* Types may not be defined in an exception-specification. */
24362 saved_message = parser->type_definition_forbidden_message;
24363 parser->type_definition_forbidden_message
24364 = G_("types may not be defined in an exception-specification");
24366 expr = cp_parser_constant_expression (parser);
24368 /* Restore the saved message. */
24369 parser->type_definition_forbidden_message = saved_message;
24371 else
24373 expr = cp_parser_expression (parser);
24374 *consumed_expr = true;
24377 parens.require_close (parser);
24379 else
24381 expr = boolean_true_node;
24382 if (!require_constexpr)
24383 *consumed_expr = false;
24386 /* We cannot build a noexcept-spec right away because this will check
24387 that expr is a constexpr. */
24388 if (!return_cond)
24389 return build_noexcept_spec (expr, tf_warning_or_error);
24390 else
24391 return expr;
24393 else
24394 return NULL_TREE;
24397 /* Parse an (optional) exception-specification.
24399 exception-specification:
24400 throw ( type-id-list [opt] )
24402 Returns a TREE_LIST representing the exception-specification. The
24403 TREE_VALUE of each node is a type. */
24405 static tree
24406 cp_parser_exception_specification_opt (cp_parser* parser)
24408 cp_token *token;
24409 tree type_id_list;
24410 const char *saved_message;
24412 /* Peek at the next token. */
24413 token = cp_lexer_peek_token (parser->lexer);
24415 /* Is it a noexcept-specification? */
24416 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24417 false);
24418 if (type_id_list != NULL_TREE)
24419 return type_id_list;
24421 /* If it's not `throw', then there's no exception-specification. */
24422 if (!cp_parser_is_keyword (token, RID_THROW))
24423 return NULL_TREE;
24425 location_t loc = token->location;
24427 /* Consume the `throw'. */
24428 cp_lexer_consume_token (parser->lexer);
24430 /* Look for the `('. */
24431 matching_parens parens;
24432 parens.require_open (parser);
24434 /* Peek at the next token. */
24435 token = cp_lexer_peek_token (parser->lexer);
24436 /* If it's not a `)', then there is a type-id-list. */
24437 if (token->type != CPP_CLOSE_PAREN)
24439 /* Types may not be defined in an exception-specification. */
24440 saved_message = parser->type_definition_forbidden_message;
24441 parser->type_definition_forbidden_message
24442 = G_("types may not be defined in an exception-specification");
24443 /* Parse the type-id-list. */
24444 type_id_list = cp_parser_type_id_list (parser);
24445 /* Restore the saved message. */
24446 parser->type_definition_forbidden_message = saved_message;
24448 if (cxx_dialect >= cxx17)
24450 error_at (loc, "ISO C++17 does not allow dynamic exception "
24451 "specifications");
24452 type_id_list = NULL_TREE;
24454 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24455 warning_at (loc, OPT_Wdeprecated,
24456 "dynamic exception specifications are deprecated in "
24457 "C++11");
24459 /* In C++17, throw() is equivalent to noexcept (true). throw()
24460 is deprecated in C++11 and above as well, but is still widely used,
24461 so don't warn about it yet. */
24462 else if (cxx_dialect >= cxx17)
24463 type_id_list = noexcept_true_spec;
24464 else
24465 type_id_list = empty_except_spec;
24467 /* Look for the `)'. */
24468 parens.require_close (parser);
24470 return type_id_list;
24473 /* Parse an (optional) type-id-list.
24475 type-id-list:
24476 type-id ... [opt]
24477 type-id-list , type-id ... [opt]
24479 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24480 in the order that the types were presented. */
24482 static tree
24483 cp_parser_type_id_list (cp_parser* parser)
24485 tree types = NULL_TREE;
24487 while (true)
24489 cp_token *token;
24490 tree type;
24492 token = cp_lexer_peek_token (parser->lexer);
24494 /* Get the next type-id. */
24495 type = cp_parser_type_id (parser);
24496 /* Check for invalid 'auto'. */
24497 if (flag_concepts && type_uses_auto (type))
24499 error_at (token->location,
24500 "invalid use of %<auto%> in exception-specification");
24501 type = error_mark_node;
24503 /* Parse the optional ellipsis. */
24504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24506 /* Consume the `...'. */
24507 cp_lexer_consume_token (parser->lexer);
24509 /* Turn the type into a pack expansion expression. */
24510 type = make_pack_expansion (type);
24512 /* Add it to the list. */
24513 types = add_exception_specifier (types, type, /*complain=*/1);
24514 /* Peek at the next token. */
24515 token = cp_lexer_peek_token (parser->lexer);
24516 /* If it is not a `,', we are done. */
24517 if (token->type != CPP_COMMA)
24518 break;
24519 /* Consume the `,'. */
24520 cp_lexer_consume_token (parser->lexer);
24523 return nreverse (types);
24526 /* Parse a try-block.
24528 try-block:
24529 try compound-statement handler-seq */
24531 static tree
24532 cp_parser_try_block (cp_parser* parser)
24534 tree try_block;
24536 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24537 if (parser->in_function_body
24538 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24539 error ("%<try%> in %<constexpr%> function");
24541 try_block = begin_try_block ();
24542 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24543 finish_try_block (try_block);
24544 cp_parser_handler_seq (parser);
24545 finish_handler_sequence (try_block);
24547 return try_block;
24550 /* Parse a function-try-block.
24552 function-try-block:
24553 try ctor-initializer [opt] function-body handler-seq */
24555 static void
24556 cp_parser_function_try_block (cp_parser* parser)
24558 tree compound_stmt;
24559 tree try_block;
24561 /* Look for the `try' keyword. */
24562 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24563 return;
24564 /* Let the rest of the front end know where we are. */
24565 try_block = begin_function_try_block (&compound_stmt);
24566 /* Parse the function-body. */
24567 cp_parser_ctor_initializer_opt_and_function_body
24568 (parser, /*in_function_try_block=*/true);
24569 /* We're done with the `try' part. */
24570 finish_function_try_block (try_block);
24571 /* Parse the handlers. */
24572 cp_parser_handler_seq (parser);
24573 /* We're done with the handlers. */
24574 finish_function_handler_sequence (try_block, compound_stmt);
24577 /* Parse a handler-seq.
24579 handler-seq:
24580 handler handler-seq [opt] */
24582 static void
24583 cp_parser_handler_seq (cp_parser* parser)
24585 while (true)
24587 cp_token *token;
24589 /* Parse the handler. */
24590 cp_parser_handler (parser);
24591 /* Peek at the next token. */
24592 token = cp_lexer_peek_token (parser->lexer);
24593 /* If it's not `catch' then there are no more handlers. */
24594 if (!cp_parser_is_keyword (token, RID_CATCH))
24595 break;
24599 /* Parse a handler.
24601 handler:
24602 catch ( exception-declaration ) compound-statement */
24604 static void
24605 cp_parser_handler (cp_parser* parser)
24607 tree handler;
24608 tree declaration;
24610 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24611 handler = begin_handler ();
24612 matching_parens parens;
24613 parens.require_open (parser);
24614 declaration = cp_parser_exception_declaration (parser);
24615 finish_handler_parms (declaration, handler);
24616 parens.require_close (parser);
24617 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24618 finish_handler (handler);
24621 /* Parse an exception-declaration.
24623 exception-declaration:
24624 type-specifier-seq declarator
24625 type-specifier-seq abstract-declarator
24626 type-specifier-seq
24629 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24630 ellipsis variant is used. */
24632 static tree
24633 cp_parser_exception_declaration (cp_parser* parser)
24635 cp_decl_specifier_seq type_specifiers;
24636 cp_declarator *declarator;
24637 const char *saved_message;
24639 /* If it's an ellipsis, it's easy to handle. */
24640 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24642 /* Consume the `...' token. */
24643 cp_lexer_consume_token (parser->lexer);
24644 return NULL_TREE;
24647 /* Types may not be defined in exception-declarations. */
24648 saved_message = parser->type_definition_forbidden_message;
24649 parser->type_definition_forbidden_message
24650 = G_("types may not be defined in exception-declarations");
24652 /* Parse the type-specifier-seq. */
24653 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24654 /*is_trailing_return=*/false,
24655 &type_specifiers);
24656 /* If it's a `)', then there is no declarator. */
24657 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24658 declarator = NULL;
24659 else
24660 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24661 /*ctor_dtor_or_conv_p=*/NULL,
24662 /*parenthesized_p=*/NULL,
24663 /*member_p=*/false,
24664 /*friend_p=*/false);
24666 /* Restore the saved message. */
24667 parser->type_definition_forbidden_message = saved_message;
24669 if (!type_specifiers.any_specifiers_p)
24670 return error_mark_node;
24672 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24675 /* Parse a throw-expression.
24677 throw-expression:
24678 throw assignment-expression [opt]
24680 Returns a THROW_EXPR representing the throw-expression. */
24682 static tree
24683 cp_parser_throw_expression (cp_parser* parser)
24685 tree expression;
24686 cp_token* token;
24688 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24689 token = cp_lexer_peek_token (parser->lexer);
24690 /* Figure out whether or not there is an assignment-expression
24691 following the "throw" keyword. */
24692 if (token->type == CPP_COMMA
24693 || token->type == CPP_SEMICOLON
24694 || token->type == CPP_CLOSE_PAREN
24695 || token->type == CPP_CLOSE_SQUARE
24696 || token->type == CPP_CLOSE_BRACE
24697 || token->type == CPP_COLON)
24698 expression = NULL_TREE;
24699 else
24700 expression = cp_parser_assignment_expression (parser);
24702 return build_throw (expression);
24705 /* GNU Extensions */
24707 /* Parse an (optional) asm-specification.
24709 asm-specification:
24710 asm ( string-literal )
24712 If the asm-specification is present, returns a STRING_CST
24713 corresponding to the string-literal. Otherwise, returns
24714 NULL_TREE. */
24716 static tree
24717 cp_parser_asm_specification_opt (cp_parser* parser)
24719 cp_token *token;
24720 tree asm_specification;
24722 /* Peek at the next token. */
24723 token = cp_lexer_peek_token (parser->lexer);
24724 /* If the next token isn't the `asm' keyword, then there's no
24725 asm-specification. */
24726 if (!cp_parser_is_keyword (token, RID_ASM))
24727 return NULL_TREE;
24729 /* Consume the `asm' token. */
24730 cp_lexer_consume_token (parser->lexer);
24731 /* Look for the `('. */
24732 matching_parens parens;
24733 parens.require_open (parser);
24735 /* Look for the string-literal. */
24736 asm_specification = cp_parser_string_literal (parser, false, false);
24738 /* Look for the `)'. */
24739 parens.require_close (parser);
24741 return asm_specification;
24744 /* Parse an asm-operand-list.
24746 asm-operand-list:
24747 asm-operand
24748 asm-operand-list , asm-operand
24750 asm-operand:
24751 string-literal ( expression )
24752 [ string-literal ] string-literal ( expression )
24754 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24755 each node is the expression. The TREE_PURPOSE is itself a
24756 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24757 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24758 is a STRING_CST for the string literal before the parenthesis. Returns
24759 ERROR_MARK_NODE if any of the operands are invalid. */
24761 static tree
24762 cp_parser_asm_operand_list (cp_parser* parser)
24764 tree asm_operands = NULL_TREE;
24765 bool invalid_operands = false;
24767 while (true)
24769 tree string_literal;
24770 tree expression;
24771 tree name;
24773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24775 /* Consume the `[' token. */
24776 cp_lexer_consume_token (parser->lexer);
24777 /* Read the operand name. */
24778 name = cp_parser_identifier (parser);
24779 if (name != error_mark_node)
24780 name = build_string (IDENTIFIER_LENGTH (name),
24781 IDENTIFIER_POINTER (name));
24782 /* Look for the closing `]'. */
24783 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24785 else
24786 name = NULL_TREE;
24787 /* Look for the string-literal. */
24788 string_literal = cp_parser_string_literal (parser, false, false);
24790 /* Look for the `('. */
24791 matching_parens parens;
24792 parens.require_open (parser);
24793 /* Parse the expression. */
24794 expression = cp_parser_expression (parser);
24795 /* Look for the `)'. */
24796 parens.require_close (parser);
24798 if (name == error_mark_node
24799 || string_literal == error_mark_node
24800 || expression == error_mark_node)
24801 invalid_operands = true;
24803 /* Add this operand to the list. */
24804 asm_operands = tree_cons (build_tree_list (name, string_literal),
24805 expression,
24806 asm_operands);
24807 /* If the next token is not a `,', there are no more
24808 operands. */
24809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24810 break;
24811 /* Consume the `,'. */
24812 cp_lexer_consume_token (parser->lexer);
24815 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24818 /* Parse an asm-clobber-list.
24820 asm-clobber-list:
24821 string-literal
24822 asm-clobber-list , string-literal
24824 Returns a TREE_LIST, indicating the clobbers in the order that they
24825 appeared. The TREE_VALUE of each node is a STRING_CST. */
24827 static tree
24828 cp_parser_asm_clobber_list (cp_parser* parser)
24830 tree clobbers = NULL_TREE;
24832 while (true)
24834 tree string_literal;
24836 /* Look for the string literal. */
24837 string_literal = cp_parser_string_literal (parser, false, false);
24838 /* Add it to the list. */
24839 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24840 /* If the next token is not a `,', then the list is
24841 complete. */
24842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24843 break;
24844 /* Consume the `,' token. */
24845 cp_lexer_consume_token (parser->lexer);
24848 return clobbers;
24851 /* Parse an asm-label-list.
24853 asm-label-list:
24854 identifier
24855 asm-label-list , identifier
24857 Returns a TREE_LIST, indicating the labels in the order that they
24858 appeared. The TREE_VALUE of each node is a label. */
24860 static tree
24861 cp_parser_asm_label_list (cp_parser* parser)
24863 tree labels = NULL_TREE;
24865 while (true)
24867 tree identifier, label, name;
24869 /* Look for the identifier. */
24870 identifier = cp_parser_identifier (parser);
24871 if (!error_operand_p (identifier))
24873 label = lookup_label (identifier);
24874 if (TREE_CODE (label) == LABEL_DECL)
24876 TREE_USED (label) = 1;
24877 check_goto (label);
24878 name = build_string (IDENTIFIER_LENGTH (identifier),
24879 IDENTIFIER_POINTER (identifier));
24880 labels = tree_cons (name, label, labels);
24883 /* If the next token is not a `,', then the list is
24884 complete. */
24885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24886 break;
24887 /* Consume the `,' token. */
24888 cp_lexer_consume_token (parser->lexer);
24891 return nreverse (labels);
24894 /* Return TRUE iff the next tokens in the stream are possibly the
24895 beginning of a GNU extension attribute. */
24897 static bool
24898 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24900 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24903 /* Return TRUE iff the next tokens in the stream are possibly the
24904 beginning of a standard C++-11 attribute specifier. */
24906 static bool
24907 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24909 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24912 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24913 beginning of a standard C++-11 attribute specifier. */
24915 static bool
24916 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24918 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24920 return (cxx_dialect >= cxx11
24921 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24922 || (token->type == CPP_OPEN_SQUARE
24923 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24924 && token->type == CPP_OPEN_SQUARE)));
24927 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24928 beginning of a GNU extension attribute. */
24930 static bool
24931 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24933 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24935 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24938 /* Return true iff the next tokens can be the beginning of either a
24939 GNU attribute list, or a standard C++11 attribute sequence. */
24941 static bool
24942 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24944 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24945 || cp_next_tokens_can_be_std_attribute_p (parser));
24948 /* Return true iff the next Nth tokens can be the beginning of either
24949 a GNU attribute list, or a standard C++11 attribute sequence. */
24951 static bool
24952 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24954 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24955 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24958 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24959 of GNU attributes, or return NULL. */
24961 static tree
24962 cp_parser_attributes_opt (cp_parser *parser)
24964 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24965 return cp_parser_gnu_attributes_opt (parser);
24966 return cp_parser_std_attribute_spec_seq (parser);
24969 /* Parse an (optional) series of attributes.
24971 attributes:
24972 attributes attribute
24974 attribute:
24975 __attribute__ (( attribute-list [opt] ))
24977 The return value is as for cp_parser_gnu_attribute_list. */
24979 static tree
24980 cp_parser_gnu_attributes_opt (cp_parser* parser)
24982 tree attributes = NULL_TREE;
24984 temp_override<bool> cleanup
24985 (parser->auto_is_implicit_function_template_parm_p, false);
24987 while (true)
24989 cp_token *token;
24990 tree attribute_list;
24991 bool ok = true;
24993 /* Peek at the next token. */
24994 token = cp_lexer_peek_token (parser->lexer);
24995 /* If it's not `__attribute__', then we're done. */
24996 if (token->keyword != RID_ATTRIBUTE)
24997 break;
24999 /* Consume the `__attribute__' keyword. */
25000 cp_lexer_consume_token (parser->lexer);
25001 /* Look for the two `(' tokens. */
25002 matching_parens outer_parens;
25003 outer_parens.require_open (parser);
25004 matching_parens inner_parens;
25005 inner_parens.require_open (parser);
25007 /* Peek at the next token. */
25008 token = cp_lexer_peek_token (parser->lexer);
25009 if (token->type != CPP_CLOSE_PAREN)
25010 /* Parse the attribute-list. */
25011 attribute_list = cp_parser_gnu_attribute_list (parser);
25012 else
25013 /* If the next token is a `)', then there is no attribute
25014 list. */
25015 attribute_list = NULL;
25017 /* Look for the two `)' tokens. */
25018 if (!inner_parens.require_close (parser))
25019 ok = false;
25020 if (!outer_parens.require_close (parser))
25021 ok = false;
25022 if (!ok)
25023 cp_parser_skip_to_end_of_statement (parser);
25025 /* Add these new attributes to the list. */
25026 attributes = attr_chainon (attributes, attribute_list);
25029 return attributes;
25032 /* Parse a GNU attribute-list.
25034 attribute-list:
25035 attribute
25036 attribute-list , attribute
25038 attribute:
25039 identifier
25040 identifier ( identifier )
25041 identifier ( identifier , expression-list )
25042 identifier ( expression-list )
25044 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25045 to an attribute. The TREE_PURPOSE of each node is the identifier
25046 indicating which attribute is in use. The TREE_VALUE represents
25047 the arguments, if any. */
25049 static tree
25050 cp_parser_gnu_attribute_list (cp_parser* parser)
25052 tree attribute_list = NULL_TREE;
25053 bool save_translate_strings_p = parser->translate_strings_p;
25055 parser->translate_strings_p = false;
25056 while (true)
25058 cp_token *token;
25059 tree identifier;
25060 tree attribute;
25062 /* Look for the identifier. We also allow keywords here; for
25063 example `__attribute__ ((const))' is legal. */
25064 token = cp_lexer_peek_token (parser->lexer);
25065 if (token->type == CPP_NAME
25066 || token->type == CPP_KEYWORD)
25068 tree arguments = NULL_TREE;
25070 /* Consume the token, but save it since we need it for the
25071 SIMD enabled function parsing. */
25072 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25074 /* Save away the identifier that indicates which attribute
25075 this is. */
25076 identifier = (token->type == CPP_KEYWORD)
25077 /* For keywords, use the canonical spelling, not the
25078 parsed identifier. */
25079 ? ridpointers[(int) token->keyword]
25080 : id_token->u.value;
25082 identifier = canonicalize_attr_name (identifier);
25083 attribute = build_tree_list (identifier, NULL_TREE);
25085 /* Peek at the next token. */
25086 token = cp_lexer_peek_token (parser->lexer);
25087 /* If it's an `(', then parse the attribute arguments. */
25088 if (token->type == CPP_OPEN_PAREN)
25090 vec<tree, va_gc> *vec;
25091 int attr_flag = (attribute_takes_identifier_p (identifier)
25092 ? id_attr : normal_attr);
25093 vec = cp_parser_parenthesized_expression_list
25094 (parser, attr_flag, /*cast_p=*/false,
25095 /*allow_expansion_p=*/false,
25096 /*non_constant_p=*/NULL);
25097 if (vec == NULL)
25098 arguments = error_mark_node;
25099 else
25101 arguments = build_tree_list_vec (vec);
25102 release_tree_vector (vec);
25104 /* Save the arguments away. */
25105 TREE_VALUE (attribute) = arguments;
25108 if (arguments != error_mark_node)
25110 /* Add this attribute to the list. */
25111 TREE_CHAIN (attribute) = attribute_list;
25112 attribute_list = attribute;
25115 token = cp_lexer_peek_token (parser->lexer);
25117 /* Now, look for more attributes. If the next token isn't a
25118 `,', we're done. */
25119 if (token->type != CPP_COMMA)
25120 break;
25122 /* Consume the comma and keep going. */
25123 cp_lexer_consume_token (parser->lexer);
25125 parser->translate_strings_p = save_translate_strings_p;
25127 /* We built up the list in reverse order. */
25128 return nreverse (attribute_list);
25131 /* Parse a standard C++11 attribute.
25133 The returned representation is a TREE_LIST which TREE_PURPOSE is
25134 the scoped name of the attribute, and the TREE_VALUE is its
25135 arguments list.
25137 Note that the scoped name of the attribute is itself a TREE_LIST
25138 which TREE_PURPOSE is the namespace of the attribute, and
25139 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25140 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25141 and which TREE_PURPOSE is directly the attribute name.
25143 Clients of the attribute code should use get_attribute_namespace
25144 and get_attribute_name to get the actual namespace and name of
25145 attributes, regardless of their being GNU or C++11 attributes.
25147 attribute:
25148 attribute-token attribute-argument-clause [opt]
25150 attribute-token:
25151 identifier
25152 attribute-scoped-token
25154 attribute-scoped-token:
25155 attribute-namespace :: identifier
25157 attribute-namespace:
25158 identifier
25160 attribute-argument-clause:
25161 ( balanced-token-seq )
25163 balanced-token-seq:
25164 balanced-token [opt]
25165 balanced-token-seq balanced-token
25167 balanced-token:
25168 ( balanced-token-seq )
25169 [ balanced-token-seq ]
25170 { balanced-token-seq }. */
25172 static tree
25173 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25175 tree attribute, attr_id = NULL_TREE, arguments;
25176 cp_token *token;
25178 temp_override<bool> cleanup
25179 (parser->auto_is_implicit_function_template_parm_p, false);
25181 /* First, parse name of the attribute, a.k.a attribute-token. */
25183 token = cp_lexer_peek_token (parser->lexer);
25184 if (token->type == CPP_NAME)
25185 attr_id = token->u.value;
25186 else if (token->type == CPP_KEYWORD)
25187 attr_id = ridpointers[(int) token->keyword];
25188 else if (token->flags & NAMED_OP)
25189 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25191 if (attr_id == NULL_TREE)
25192 return NULL_TREE;
25194 cp_lexer_consume_token (parser->lexer);
25196 token = cp_lexer_peek_token (parser->lexer);
25197 if (token->type == CPP_SCOPE)
25199 /* We are seeing a scoped attribute token. */
25201 cp_lexer_consume_token (parser->lexer);
25202 if (attr_ns)
25203 error_at (token->location, "attribute using prefix used together "
25204 "with scoped attribute token");
25205 attr_ns = attr_id;
25207 token = cp_lexer_consume_token (parser->lexer);
25208 if (token->type == CPP_NAME)
25209 attr_id = token->u.value;
25210 else if (token->type == CPP_KEYWORD)
25211 attr_id = ridpointers[(int) token->keyword];
25212 else if (token->flags & NAMED_OP)
25213 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25214 else
25216 error_at (token->location,
25217 "expected an identifier for the attribute name");
25218 return error_mark_node;
25221 attr_id = canonicalize_attr_name (attr_id);
25222 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25223 NULL_TREE);
25224 token = cp_lexer_peek_token (parser->lexer);
25226 else if (attr_ns)
25227 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25228 NULL_TREE);
25229 else
25231 attr_id = canonicalize_attr_name (attr_id);
25232 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25233 NULL_TREE);
25234 /* C++11 noreturn attribute is equivalent to GNU's. */
25235 if (is_attribute_p ("noreturn", attr_id))
25236 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25237 /* C++14 deprecated attribute is equivalent to GNU's. */
25238 else if (is_attribute_p ("deprecated", attr_id))
25239 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25240 /* C++17 fallthrough attribute is equivalent to GNU's. */
25241 else if (is_attribute_p ("fallthrough", attr_id))
25242 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25243 /* Transactional Memory TS optimize_for_synchronized attribute is
25244 equivalent to GNU transaction_callable. */
25245 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25246 TREE_PURPOSE (attribute)
25247 = get_identifier ("transaction_callable");
25248 /* Transactional Memory attributes are GNU attributes. */
25249 else if (tm_attr_to_mask (attr_id))
25250 TREE_PURPOSE (attribute) = attr_id;
25253 /* Now parse the optional argument clause of the attribute. */
25255 if (token->type != CPP_OPEN_PAREN)
25256 return attribute;
25259 vec<tree, va_gc> *vec;
25260 int attr_flag = normal_attr;
25262 if (attr_ns == get_identifier ("gnu")
25263 && attribute_takes_identifier_p (attr_id))
25264 /* A GNU attribute that takes an identifier in parameter. */
25265 attr_flag = id_attr;
25267 vec = cp_parser_parenthesized_expression_list
25268 (parser, attr_flag, /*cast_p=*/false,
25269 /*allow_expansion_p=*/true,
25270 /*non_constant_p=*/NULL);
25271 if (vec == NULL)
25272 arguments = error_mark_node;
25273 else
25275 arguments = build_tree_list_vec (vec);
25276 release_tree_vector (vec);
25279 if (arguments == error_mark_node)
25280 attribute = error_mark_node;
25281 else
25282 TREE_VALUE (attribute) = arguments;
25285 return attribute;
25288 /* Check that the attribute ATTRIBUTE appears at most once in the
25289 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25290 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25291 isn't implemented yet in GCC. */
25293 static void
25294 cp_parser_check_std_attribute (tree attributes, tree attribute)
25296 if (attributes)
25298 tree name = get_attribute_name (attribute);
25299 if (is_attribute_p ("noreturn", name)
25300 && lookup_attribute ("noreturn", attributes))
25301 error ("attribute %<noreturn%> can appear at most once "
25302 "in an attribute-list");
25303 else if (is_attribute_p ("deprecated", name)
25304 && lookup_attribute ("deprecated", attributes))
25305 error ("attribute %<deprecated%> can appear at most once "
25306 "in an attribute-list");
25310 /* Parse a list of standard C++-11 attributes.
25312 attribute-list:
25313 attribute [opt]
25314 attribute-list , attribute[opt]
25315 attribute ...
25316 attribute-list , attribute ...
25319 static tree
25320 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25322 tree attributes = NULL_TREE, attribute = NULL_TREE;
25323 cp_token *token = NULL;
25325 while (true)
25327 attribute = cp_parser_std_attribute (parser, attr_ns);
25328 if (attribute == error_mark_node)
25329 break;
25330 if (attribute != NULL_TREE)
25332 cp_parser_check_std_attribute (attributes, attribute);
25333 TREE_CHAIN (attribute) = attributes;
25334 attributes = attribute;
25336 token = cp_lexer_peek_token (parser->lexer);
25337 if (token->type == CPP_ELLIPSIS)
25339 cp_lexer_consume_token (parser->lexer);
25340 if (attribute == NULL_TREE)
25341 error_at (token->location,
25342 "expected attribute before %<...%>");
25343 else
25345 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25346 if (pack == error_mark_node)
25347 return error_mark_node;
25348 TREE_VALUE (attribute) = pack;
25350 token = cp_lexer_peek_token (parser->lexer);
25352 if (token->type != CPP_COMMA)
25353 break;
25354 cp_lexer_consume_token (parser->lexer);
25356 attributes = nreverse (attributes);
25357 return attributes;
25360 /* Parse a standard C++-11 attribute specifier.
25362 attribute-specifier:
25363 [ [ attribute-using-prefix [opt] attribute-list ] ]
25364 alignment-specifier
25366 attribute-using-prefix:
25367 using attribute-namespace :
25369 alignment-specifier:
25370 alignas ( type-id ... [opt] )
25371 alignas ( alignment-expression ... [opt] ). */
25373 static tree
25374 cp_parser_std_attribute_spec (cp_parser *parser)
25376 tree attributes = NULL_TREE;
25377 cp_token *token = cp_lexer_peek_token (parser->lexer);
25379 if (token->type == CPP_OPEN_SQUARE
25380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25382 tree attr_ns = NULL_TREE;
25384 cp_lexer_consume_token (parser->lexer);
25385 cp_lexer_consume_token (parser->lexer);
25387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25389 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25390 if (token->type == CPP_NAME)
25391 attr_ns = token->u.value;
25392 else if (token->type == CPP_KEYWORD)
25393 attr_ns = ridpointers[(int) token->keyword];
25394 else if (token->flags & NAMED_OP)
25395 attr_ns = get_identifier (cpp_type2name (token->type,
25396 token->flags));
25397 if (attr_ns
25398 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25400 if (cxx_dialect < cxx17
25401 && !in_system_header_at (input_location))
25402 pedwarn (input_location, 0,
25403 "attribute using prefix only available "
25404 "with -std=c++17 or -std=gnu++17");
25406 cp_lexer_consume_token (parser->lexer);
25407 cp_lexer_consume_token (parser->lexer);
25408 cp_lexer_consume_token (parser->lexer);
25410 else
25411 attr_ns = NULL_TREE;
25414 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25416 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25417 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25418 cp_parser_skip_to_end_of_statement (parser);
25419 else
25420 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25421 when we are sure that we have actually parsed them. */
25422 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25424 else
25426 tree alignas_expr;
25428 /* Look for an alignment-specifier. */
25430 token = cp_lexer_peek_token (parser->lexer);
25432 if (token->type != CPP_KEYWORD
25433 || token->keyword != RID_ALIGNAS)
25434 return NULL_TREE;
25436 cp_lexer_consume_token (parser->lexer);
25437 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25439 matching_parens parens;
25440 if (!parens.require_open (parser))
25441 return error_mark_node;
25443 cp_parser_parse_tentatively (parser);
25444 alignas_expr = cp_parser_type_id (parser);
25446 if (!cp_parser_parse_definitely (parser))
25448 alignas_expr = cp_parser_assignment_expression (parser);
25449 if (alignas_expr == error_mark_node)
25450 cp_parser_skip_to_end_of_statement (parser);
25451 if (alignas_expr == NULL_TREE
25452 || alignas_expr == error_mark_node)
25453 return alignas_expr;
25456 alignas_expr = cxx_alignas_expr (alignas_expr);
25457 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25459 /* Handle alignas (pack...). */
25460 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25462 cp_lexer_consume_token (parser->lexer);
25463 alignas_expr = make_pack_expansion (alignas_expr);
25466 /* Something went wrong, so don't build the attribute. */
25467 if (alignas_expr == error_mark_node)
25468 return error_mark_node;
25470 if (!parens.require_close (parser))
25471 return error_mark_node;
25473 /* Build the C++-11 representation of an 'aligned'
25474 attribute. */
25475 attributes =
25476 build_tree_list (build_tree_list (get_identifier ("gnu"),
25477 get_identifier ("aligned")),
25478 alignas_expr);
25481 return attributes;
25484 /* Parse a standard C++-11 attribute-specifier-seq.
25486 attribute-specifier-seq:
25487 attribute-specifier-seq [opt] attribute-specifier
25490 static tree
25491 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25493 tree attr_specs = NULL_TREE;
25494 tree attr_last = NULL_TREE;
25496 while (true)
25498 tree attr_spec = cp_parser_std_attribute_spec (parser);
25499 if (attr_spec == NULL_TREE)
25500 break;
25501 if (attr_spec == error_mark_node)
25502 return error_mark_node;
25504 if (attr_last)
25505 TREE_CHAIN (attr_last) = attr_spec;
25506 else
25507 attr_specs = attr_last = attr_spec;
25508 attr_last = tree_last (attr_last);
25511 return attr_specs;
25514 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25515 return index of the first token after balanced-token, or N on failure. */
25517 static size_t
25518 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25520 size_t orig_n = n;
25521 int nparens = 0, nbraces = 0, nsquares = 0;
25523 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25525 case CPP_EOF:
25526 case CPP_PRAGMA_EOL:
25527 /* Ran out of tokens. */
25528 return orig_n;
25529 case CPP_OPEN_PAREN:
25530 ++nparens;
25531 break;
25532 case CPP_OPEN_BRACE:
25533 ++nbraces;
25534 break;
25535 case CPP_OPEN_SQUARE:
25536 ++nsquares;
25537 break;
25538 case CPP_CLOSE_PAREN:
25539 --nparens;
25540 break;
25541 case CPP_CLOSE_BRACE:
25542 --nbraces;
25543 break;
25544 case CPP_CLOSE_SQUARE:
25545 --nsquares;
25546 break;
25547 default:
25548 break;
25550 while (nparens || nbraces || nsquares);
25551 return n;
25554 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25555 return index of the first token after the GNU attribute tokens, or N on
25556 failure. */
25558 static size_t
25559 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25561 while (true)
25563 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25564 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25565 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25566 break;
25568 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25569 if (n2 == n + 2)
25570 break;
25571 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25572 break;
25573 n = n2 + 1;
25575 return n;
25578 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25579 next token), return index of the first token after the standard C++11
25580 attribute tokens, or N on failure. */
25582 static size_t
25583 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25585 while (true)
25587 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25588 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25590 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25591 if (n2 == n + 1)
25592 break;
25593 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25594 break;
25595 n = n2 + 1;
25597 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25598 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25600 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25601 if (n2 == n + 1)
25602 break;
25603 n = n2;
25605 else
25606 break;
25608 return n;
25611 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25612 as the next token), return index of the first token after the attribute
25613 tokens, or N on failure. */
25615 static size_t
25616 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25618 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25619 return cp_parser_skip_gnu_attributes_opt (parser, n);
25620 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25623 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25624 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25625 current value of the PEDANTIC flag, regardless of whether or not
25626 the `__extension__' keyword is present. The caller is responsible
25627 for restoring the value of the PEDANTIC flag. */
25629 static bool
25630 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25632 /* Save the old value of the PEDANTIC flag. */
25633 *saved_pedantic = pedantic;
25635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25637 /* Consume the `__extension__' token. */
25638 cp_lexer_consume_token (parser->lexer);
25639 /* We're not being pedantic while the `__extension__' keyword is
25640 in effect. */
25641 pedantic = 0;
25643 return true;
25646 return false;
25649 /* Parse a label declaration.
25651 label-declaration:
25652 __label__ label-declarator-seq ;
25654 label-declarator-seq:
25655 identifier , label-declarator-seq
25656 identifier */
25658 static void
25659 cp_parser_label_declaration (cp_parser* parser)
25661 /* Look for the `__label__' keyword. */
25662 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25664 while (true)
25666 tree identifier;
25668 /* Look for an identifier. */
25669 identifier = cp_parser_identifier (parser);
25670 /* If we failed, stop. */
25671 if (identifier == error_mark_node)
25672 break;
25673 /* Declare it as a label. */
25674 finish_label_decl (identifier);
25675 /* If the next token is a `;', stop. */
25676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25677 break;
25678 /* Look for the `,' separating the label declarations. */
25679 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25682 /* Look for the final `;'. */
25683 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25686 // -------------------------------------------------------------------------- //
25687 // Requires Clause
25689 // Parse a requires clause.
25691 // requires-clause:
25692 // 'requires' logical-or-expression
25694 // The required logical-or-expression must be a constant expression. Note
25695 // that we don't check that the expression is constepxr here. We defer until
25696 // we analyze constraints and then, we only check atomic constraints.
25697 static tree
25698 cp_parser_requires_clause (cp_parser *parser)
25700 // Parse the requires clause so that it is not automatically folded.
25701 ++processing_template_decl;
25702 tree expr = cp_parser_binary_expression (parser, false, false,
25703 PREC_NOT_OPERATOR, NULL);
25704 if (check_for_bare_parameter_packs (expr))
25705 expr = error_mark_node;
25706 --processing_template_decl;
25707 return expr;
25710 // Optionally parse a requires clause:
25711 static tree
25712 cp_parser_requires_clause_opt (cp_parser *parser)
25714 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25715 if (tok->keyword != RID_REQUIRES)
25717 if (!flag_concepts && tok->type == CPP_NAME
25718 && tok->u.value == ridpointers[RID_REQUIRES])
25720 error_at (cp_lexer_peek_token (parser->lexer)->location,
25721 "%<requires%> only available with -fconcepts");
25722 /* Parse and discard the requires-clause. */
25723 cp_lexer_consume_token (parser->lexer);
25724 cp_parser_requires_clause (parser);
25726 return NULL_TREE;
25728 cp_lexer_consume_token (parser->lexer);
25729 return cp_parser_requires_clause (parser);
25733 /*---------------------------------------------------------------------------
25734 Requires expressions
25735 ---------------------------------------------------------------------------*/
25737 /* Parse a requires expression
25739 requirement-expression:
25740 'requires' requirement-parameter-list [opt] requirement-body */
25741 static tree
25742 cp_parser_requires_expression (cp_parser *parser)
25744 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25745 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25747 /* A requires-expression shall appear only within a concept
25748 definition or a requires-clause.
25750 TODO: Implement this diagnostic correctly. */
25751 if (!processing_template_decl)
25753 error_at (loc, "a requires expression cannot appear outside a template");
25754 cp_parser_skip_to_end_of_statement (parser);
25755 return error_mark_node;
25758 tree parms, reqs;
25760 /* Local parameters are delared as variables within the scope
25761 of the expression. They are not visible past the end of
25762 the expression. Expressions within the requires-expression
25763 are unevaluated. */
25764 struct scope_sentinel
25766 scope_sentinel ()
25768 ++cp_unevaluated_operand;
25769 begin_scope (sk_block, NULL_TREE);
25772 ~scope_sentinel ()
25774 pop_bindings_and_leave_scope ();
25775 --cp_unevaluated_operand;
25777 } s;
25779 /* Parse the optional parameter list. */
25780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25782 parms = cp_parser_requirement_parameter_list (parser);
25783 if (parms == error_mark_node)
25784 return error_mark_node;
25786 else
25787 parms = NULL_TREE;
25789 /* Parse the requirement body. */
25790 reqs = cp_parser_requirement_body (parser);
25791 if (reqs == error_mark_node)
25792 return error_mark_node;
25795 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25796 the parm chain. */
25797 grokparms (parms, &parms);
25798 return finish_requires_expr (parms, reqs);
25801 /* Parse a parameterized requirement.
25803 requirement-parameter-list:
25804 '(' parameter-declaration-clause ')' */
25805 static tree
25806 cp_parser_requirement_parameter_list (cp_parser *parser)
25808 matching_parens parens;
25809 if (!parens.require_open (parser))
25810 return error_mark_node;
25812 tree parms = cp_parser_parameter_declaration_clause (parser);
25814 if (!parens.require_close (parser))
25815 return error_mark_node;
25817 return parms;
25820 /* Parse the body of a requirement.
25822 requirement-body:
25823 '{' requirement-list '}' */
25824 static tree
25825 cp_parser_requirement_body (cp_parser *parser)
25827 matching_braces braces;
25828 if (!braces.require_open (parser))
25829 return error_mark_node;
25831 tree reqs = cp_parser_requirement_list (parser);
25833 if (!braces.require_close (parser))
25834 return error_mark_node;
25836 return reqs;
25839 /* Parse a list of requirements.
25841 requirement-list:
25842 requirement
25843 requirement-list ';' requirement[opt] */
25844 static tree
25845 cp_parser_requirement_list (cp_parser *parser)
25847 tree result = NULL_TREE;
25848 while (true)
25850 tree req = cp_parser_requirement (parser);
25851 if (req == error_mark_node)
25852 return error_mark_node;
25854 result = tree_cons (NULL_TREE, req, result);
25856 /* If we see a semi-colon, consume it. */
25857 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25858 cp_lexer_consume_token (parser->lexer);
25860 /* Stop processing at the end of the list. */
25861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25862 break;
25865 /* Reverse the order of requirements so they are analyzed in
25866 declaration order. */
25867 return nreverse (result);
25870 /* Parse a syntactic requirement or type requirement.
25872 requirement:
25873 simple-requirement
25874 compound-requirement
25875 type-requirement
25876 nested-requirement */
25877 static tree
25878 cp_parser_requirement (cp_parser *parser)
25880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25881 return cp_parser_compound_requirement (parser);
25882 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25883 return cp_parser_type_requirement (parser);
25884 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25885 return cp_parser_nested_requirement (parser);
25886 else
25887 return cp_parser_simple_requirement (parser);
25890 /* Parse a simple requirement.
25892 simple-requirement:
25893 expression ';' */
25894 static tree
25895 cp_parser_simple_requirement (cp_parser *parser)
25897 tree expr = cp_parser_expression (parser, NULL, false, false);
25898 if (!expr || expr == error_mark_node)
25899 return error_mark_node;
25901 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25902 return error_mark_node;
25904 return finish_simple_requirement (expr);
25907 /* Parse a type requirement
25909 type-requirement
25910 nested-name-specifier [opt] required-type-name ';'
25912 required-type-name:
25913 type-name
25914 'template' [opt] simple-template-id */
25915 static tree
25916 cp_parser_type_requirement (cp_parser *parser)
25918 cp_lexer_consume_token (parser->lexer);
25920 // Save the scope before parsing name specifiers.
25921 tree saved_scope = parser->scope;
25922 tree saved_object_scope = parser->object_scope;
25923 tree saved_qualifying_scope = parser->qualifying_scope;
25924 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25925 cp_parser_nested_name_specifier_opt (parser,
25926 /*typename_keyword_p=*/true,
25927 /*check_dependency_p=*/false,
25928 /*type_p=*/true,
25929 /*is_declaration=*/false);
25931 tree type;
25932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25934 cp_lexer_consume_token (parser->lexer);
25935 type = cp_parser_template_id (parser,
25936 /*template_keyword_p=*/true,
25937 /*check_dependency=*/false,
25938 /*tag_type=*/none_type,
25939 /*is_declaration=*/false);
25940 type = make_typename_type (parser->scope, type, typename_type,
25941 /*complain=*/tf_error);
25943 else
25944 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25946 if (TREE_CODE (type) == TYPE_DECL)
25947 type = TREE_TYPE (type);
25949 parser->scope = saved_scope;
25950 parser->object_scope = saved_object_scope;
25951 parser->qualifying_scope = saved_qualifying_scope;
25953 if (type == error_mark_node)
25954 cp_parser_skip_to_end_of_statement (parser);
25956 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25957 return error_mark_node;
25958 if (type == error_mark_node)
25959 return error_mark_node;
25961 return finish_type_requirement (type);
25964 /* Parse a compound requirement
25966 compound-requirement:
25967 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25968 static tree
25969 cp_parser_compound_requirement (cp_parser *parser)
25971 /* Parse an expression enclosed in '{ }'s. */
25972 matching_braces braces;
25973 if (!braces.require_open (parser))
25974 return error_mark_node;
25976 tree expr = cp_parser_expression (parser, NULL, false, false);
25977 if (!expr || expr == error_mark_node)
25978 return error_mark_node;
25980 if (!braces.require_close (parser))
25981 return error_mark_node;
25983 /* Parse the optional noexcept. */
25984 bool noexcept_p = false;
25985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25987 cp_lexer_consume_token (parser->lexer);
25988 noexcept_p = true;
25991 /* Parse the optional trailing return type. */
25992 tree type = NULL_TREE;
25993 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25995 cp_lexer_consume_token (parser->lexer);
25996 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25997 parser->in_result_type_constraint_p = true;
25998 type = cp_parser_trailing_type_id (parser);
25999 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26000 if (type == error_mark_node)
26001 return error_mark_node;
26004 return finish_compound_requirement (expr, type, noexcept_p);
26007 /* Parse a nested requirement. This is the same as a requires clause.
26009 nested-requirement:
26010 requires-clause */
26011 static tree
26012 cp_parser_nested_requirement (cp_parser *parser)
26014 cp_lexer_consume_token (parser->lexer);
26015 tree req = cp_parser_requires_clause (parser);
26016 if (req == error_mark_node)
26017 return error_mark_node;
26018 return finish_nested_requirement (req);
26021 /* Support Functions */
26023 /* Return the appropriate prefer_type argument for lookup_name_real based on
26024 tag_type and template_mem_access. */
26026 static inline int
26027 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26029 /* DR 141: When looking in the current enclosing context for a template-name
26030 after -> or ., only consider class templates. */
26031 if (template_mem_access)
26032 return 2;
26033 switch (tag_type)
26035 case none_type: return 0; // No preference.
26036 case scope_type: return 1; // Type or namespace.
26037 default: return 2; // Type only.
26041 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26042 NAME should have one of the representations used for an
26043 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26044 is returned. If PARSER->SCOPE is a dependent type, then a
26045 SCOPE_REF is returned.
26047 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26048 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26049 was formed. Abstractly, such entities should not be passed to this
26050 function, because they do not need to be looked up, but it is
26051 simpler to check for this special case here, rather than at the
26052 call-sites.
26054 In cases not explicitly covered above, this function returns a
26055 DECL, OVERLOAD, or baselink representing the result of the lookup.
26056 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26057 is returned.
26059 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26060 (e.g., "struct") that was used. In that case bindings that do not
26061 refer to types are ignored.
26063 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26064 ignored.
26066 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26067 are ignored.
26069 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26070 types.
26072 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26073 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26074 NULL_TREE otherwise. */
26076 static cp_expr
26077 cp_parser_lookup_name (cp_parser *parser, tree name,
26078 enum tag_types tag_type,
26079 bool is_template,
26080 bool is_namespace,
26081 bool check_dependency,
26082 tree *ambiguous_decls,
26083 location_t name_location)
26085 tree decl;
26086 tree object_type = parser->context->object_type;
26088 /* Assume that the lookup will be unambiguous. */
26089 if (ambiguous_decls)
26090 *ambiguous_decls = NULL_TREE;
26092 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26093 no longer valid. Note that if we are parsing tentatively, and
26094 the parse fails, OBJECT_TYPE will be automatically restored. */
26095 parser->context->object_type = NULL_TREE;
26097 if (name == error_mark_node)
26098 return error_mark_node;
26100 /* A template-id has already been resolved; there is no lookup to
26101 do. */
26102 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26103 return name;
26104 if (BASELINK_P (name))
26106 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26107 == TEMPLATE_ID_EXPR);
26108 return name;
26111 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26112 it should already have been checked to make sure that the name
26113 used matches the type being destroyed. */
26114 if (TREE_CODE (name) == BIT_NOT_EXPR)
26116 tree type;
26118 /* Figure out to which type this destructor applies. */
26119 if (parser->scope)
26120 type = parser->scope;
26121 else if (object_type)
26122 type = object_type;
26123 else
26124 type = current_class_type;
26125 /* If that's not a class type, there is no destructor. */
26126 if (!type || !CLASS_TYPE_P (type))
26127 return error_mark_node;
26129 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26130 lazily_declare_fn (sfk_destructor, type);
26132 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26133 return dtor;
26135 return error_mark_node;
26138 /* By this point, the NAME should be an ordinary identifier. If
26139 the id-expression was a qualified name, the qualifying scope is
26140 stored in PARSER->SCOPE at this point. */
26141 gcc_assert (identifier_p (name));
26143 /* Perform the lookup. */
26144 if (parser->scope)
26146 bool dependent_p;
26148 if (parser->scope == error_mark_node)
26149 return error_mark_node;
26151 /* If the SCOPE is dependent, the lookup must be deferred until
26152 the template is instantiated -- unless we are explicitly
26153 looking up names in uninstantiated templates. Even then, we
26154 cannot look up the name if the scope is not a class type; it
26155 might, for example, be a template type parameter. */
26156 dependent_p = (TYPE_P (parser->scope)
26157 && dependent_scope_p (parser->scope));
26158 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26159 && dependent_p)
26160 /* Defer lookup. */
26161 decl = error_mark_node;
26162 else
26164 tree pushed_scope = NULL_TREE;
26166 /* If PARSER->SCOPE is a dependent type, then it must be a
26167 class type, and we must not be checking dependencies;
26168 otherwise, we would have processed this lookup above. So
26169 that PARSER->SCOPE is not considered a dependent base by
26170 lookup_member, we must enter the scope here. */
26171 if (dependent_p)
26172 pushed_scope = push_scope (parser->scope);
26174 /* If the PARSER->SCOPE is a template specialization, it
26175 may be instantiated during name lookup. In that case,
26176 errors may be issued. Even if we rollback the current
26177 tentative parse, those errors are valid. */
26178 decl = lookup_qualified_name (parser->scope, name,
26179 prefer_type_arg (tag_type),
26180 /*complain=*/true);
26182 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26183 lookup result and the nested-name-specifier nominates a class C:
26184 * if the name specified after the nested-name-specifier, when
26185 looked up in C, is the injected-class-name of C (Clause 9), or
26186 * if the name specified after the nested-name-specifier is the
26187 same as the identifier or the simple-template-id's template-
26188 name in the last component of the nested-name-specifier,
26189 the name is instead considered to name the constructor of
26190 class C. [ Note: for example, the constructor is not an
26191 acceptable lookup result in an elaborated-type-specifier so
26192 the constructor would not be used in place of the
26193 injected-class-name. --end note ] Such a constructor name
26194 shall be used only in the declarator-id of a declaration that
26195 names a constructor or in a using-declaration. */
26196 if (tag_type == none_type
26197 && DECL_SELF_REFERENCE_P (decl)
26198 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26199 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26200 prefer_type_arg (tag_type),
26201 /*complain=*/true);
26203 /* If we have a single function from a using decl, pull it out. */
26204 if (TREE_CODE (decl) == OVERLOAD
26205 && !really_overloaded_fn (decl))
26206 decl = OVL_FUNCTION (decl);
26208 if (pushed_scope)
26209 pop_scope (pushed_scope);
26212 /* If the scope is a dependent type and either we deferred lookup or
26213 we did lookup but didn't find the name, rememeber the name. */
26214 if (decl == error_mark_node && TYPE_P (parser->scope)
26215 && dependent_type_p (parser->scope))
26217 if (tag_type)
26219 tree type;
26221 /* The resolution to Core Issue 180 says that `struct
26222 A::B' should be considered a type-name, even if `A'
26223 is dependent. */
26224 type = make_typename_type (parser->scope, name, tag_type,
26225 /*complain=*/tf_error);
26226 if (type != error_mark_node)
26227 decl = TYPE_NAME (type);
26229 else if (is_template
26230 && (cp_parser_next_token_ends_template_argument_p (parser)
26231 || cp_lexer_next_token_is (parser->lexer,
26232 CPP_CLOSE_PAREN)))
26233 decl = make_unbound_class_template (parser->scope,
26234 name, NULL_TREE,
26235 /*complain=*/tf_error);
26236 else
26237 decl = build_qualified_name (/*type=*/NULL_TREE,
26238 parser->scope, name,
26239 is_template);
26241 parser->qualifying_scope = parser->scope;
26242 parser->object_scope = NULL_TREE;
26244 else if (object_type)
26246 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26247 OBJECT_TYPE is not a class. */
26248 if (CLASS_TYPE_P (object_type))
26249 /* If the OBJECT_TYPE is a template specialization, it may
26250 be instantiated during name lookup. In that case, errors
26251 may be issued. Even if we rollback the current tentative
26252 parse, those errors are valid. */
26253 decl = lookup_member (object_type,
26254 name,
26255 /*protect=*/0,
26256 prefer_type_arg (tag_type),
26257 tf_warning_or_error);
26258 else
26259 decl = NULL_TREE;
26261 if (!decl)
26262 /* Look it up in the enclosing context. DR 141: When looking for a
26263 template-name after -> or ., only consider class templates. */
26264 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26265 /*nonclass=*/0,
26266 /*block_p=*/true, is_namespace, 0);
26267 if (object_type == unknown_type_node)
26268 /* The object is type-dependent, so we can't look anything up; we used
26269 this to get the DR 141 behavior. */
26270 object_type = NULL_TREE;
26271 parser->object_scope = object_type;
26272 parser->qualifying_scope = NULL_TREE;
26274 else
26276 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26277 /*nonclass=*/0,
26278 /*block_p=*/true, is_namespace, 0);
26279 parser->qualifying_scope = NULL_TREE;
26280 parser->object_scope = NULL_TREE;
26283 /* If the lookup failed, let our caller know. */
26284 if (!decl || decl == error_mark_node)
26285 return error_mark_node;
26287 /* Pull out the template from an injected-class-name (or multiple). */
26288 if (is_template)
26289 decl = maybe_get_template_decl_from_type_decl (decl);
26291 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26292 if (TREE_CODE (decl) == TREE_LIST)
26294 if (ambiguous_decls)
26295 *ambiguous_decls = decl;
26296 /* The error message we have to print is too complicated for
26297 cp_parser_error, so we incorporate its actions directly. */
26298 if (!cp_parser_simulate_error (parser))
26300 error_at (name_location, "reference to %qD is ambiguous",
26301 name);
26302 print_candidates (decl);
26304 return error_mark_node;
26307 gcc_assert (DECL_P (decl)
26308 || TREE_CODE (decl) == OVERLOAD
26309 || TREE_CODE (decl) == SCOPE_REF
26310 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26311 || BASELINK_P (decl));
26313 /* If we have resolved the name of a member declaration, check to
26314 see if the declaration is accessible. When the name resolves to
26315 set of overloaded functions, accessibility is checked when
26316 overload resolution is done.
26318 During an explicit instantiation, access is not checked at all,
26319 as per [temp.explicit]. */
26320 if (DECL_P (decl))
26321 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26323 maybe_record_typedef_use (decl);
26325 return cp_expr (decl, name_location);
26328 /* Like cp_parser_lookup_name, but for use in the typical case where
26329 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26330 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26332 static tree
26333 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26335 return cp_parser_lookup_name (parser, name,
26336 none_type,
26337 /*is_template=*/false,
26338 /*is_namespace=*/false,
26339 /*check_dependency=*/true,
26340 /*ambiguous_decls=*/NULL,
26341 location);
26344 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26345 the current context, return the TYPE_DECL. If TAG_NAME_P is
26346 true, the DECL indicates the class being defined in a class-head,
26347 or declared in an elaborated-type-specifier.
26349 Otherwise, return DECL. */
26351 static tree
26352 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26354 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26355 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26357 struct A {
26358 template <typename T> struct B;
26361 template <typename T> struct A::B {};
26363 Similarly, in an elaborated-type-specifier:
26365 namespace N { struct X{}; }
26367 struct A {
26368 template <typename T> friend struct N::X;
26371 However, if the DECL refers to a class type, and we are in
26372 the scope of the class, then the name lookup automatically
26373 finds the TYPE_DECL created by build_self_reference rather
26374 than a TEMPLATE_DECL. For example, in:
26376 template <class T> struct S {
26377 S s;
26380 there is no need to handle such case. */
26382 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26383 return DECL_TEMPLATE_RESULT (decl);
26385 return decl;
26388 /* If too many, or too few, template-parameter lists apply to the
26389 declarator, issue an error message. Returns TRUE if all went well,
26390 and FALSE otherwise. */
26392 static bool
26393 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26394 cp_declarator *declarator,
26395 location_t declarator_location)
26397 switch (declarator->kind)
26399 case cdk_id:
26401 unsigned num_templates = 0;
26402 tree scope = declarator->u.id.qualifying_scope;
26403 bool template_id_p = false;
26405 if (scope)
26406 num_templates = num_template_headers_for_class (scope);
26407 else if (TREE_CODE (declarator->u.id.unqualified_name)
26408 == TEMPLATE_ID_EXPR)
26410 /* If the DECLARATOR has the form `X<y>' then it uses one
26411 additional level of template parameters. */
26412 ++num_templates;
26413 template_id_p = true;
26416 return cp_parser_check_template_parameters
26417 (parser, num_templates, template_id_p, declarator_location,
26418 declarator);
26421 case cdk_function:
26422 case cdk_array:
26423 case cdk_pointer:
26424 case cdk_reference:
26425 case cdk_ptrmem:
26426 return (cp_parser_check_declarator_template_parameters
26427 (parser, declarator->declarator, declarator_location));
26429 case cdk_decomp:
26430 case cdk_error:
26431 return true;
26433 default:
26434 gcc_unreachable ();
26436 return false;
26439 /* NUM_TEMPLATES were used in the current declaration. If that is
26440 invalid, return FALSE and issue an error messages. Otherwise,
26441 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26442 declarator and we can print more accurate diagnostics. */
26444 static bool
26445 cp_parser_check_template_parameters (cp_parser* parser,
26446 unsigned num_templates,
26447 bool template_id_p,
26448 location_t location,
26449 cp_declarator *declarator)
26451 /* If there are the same number of template classes and parameter
26452 lists, that's OK. */
26453 if (parser->num_template_parameter_lists == num_templates)
26454 return true;
26455 /* If there are more, but only one more, and the name ends in an identifier,
26456 then we are declaring a primary template. That's OK too. */
26457 if (!template_id_p
26458 && parser->num_template_parameter_lists == num_templates + 1)
26459 return true;
26460 /* If there are more template classes than parameter lists, we have
26461 something like:
26463 template <class T> void S<T>::R<T>::f (); */
26464 if (parser->num_template_parameter_lists < num_templates)
26466 if (declarator && !current_function_decl)
26467 error_at (location, "specializing member %<%T::%E%> "
26468 "requires %<template<>%> syntax",
26469 declarator->u.id.qualifying_scope,
26470 declarator->u.id.unqualified_name);
26471 else if (declarator)
26472 error_at (location, "invalid declaration of %<%T::%E%>",
26473 declarator->u.id.qualifying_scope,
26474 declarator->u.id.unqualified_name);
26475 else
26476 error_at (location, "too few template-parameter-lists");
26477 return false;
26479 /* Otherwise, there are too many template parameter lists. We have
26480 something like:
26482 template <class T> template <class U> void S::f(); */
26483 error_at (location, "too many template-parameter-lists");
26484 return false;
26487 /* Parse an optional `::' token indicating that the following name is
26488 from the global namespace. If so, PARSER->SCOPE is set to the
26489 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26490 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26491 Returns the new value of PARSER->SCOPE, if the `::' token is
26492 present, and NULL_TREE otherwise. */
26494 static tree
26495 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26497 cp_token *token;
26499 /* Peek at the next token. */
26500 token = cp_lexer_peek_token (parser->lexer);
26501 /* If we're looking at a `::' token then we're starting from the
26502 global namespace, not our current location. */
26503 if (token->type == CPP_SCOPE)
26505 /* Consume the `::' token. */
26506 cp_lexer_consume_token (parser->lexer);
26507 /* Set the SCOPE so that we know where to start the lookup. */
26508 parser->scope = global_namespace;
26509 parser->qualifying_scope = global_namespace;
26510 parser->object_scope = NULL_TREE;
26512 return parser->scope;
26514 else if (!current_scope_valid_p)
26516 parser->scope = NULL_TREE;
26517 parser->qualifying_scope = NULL_TREE;
26518 parser->object_scope = NULL_TREE;
26521 return NULL_TREE;
26524 /* Returns TRUE if the upcoming token sequence is the start of a
26525 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26526 declarator is preceded by the `friend' specifier. */
26528 static bool
26529 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26531 bool constructor_p;
26532 bool outside_class_specifier_p;
26533 tree nested_name_specifier;
26534 cp_token *next_token;
26536 /* The common case is that this is not a constructor declarator, so
26537 try to avoid doing lots of work if at all possible. It's not
26538 valid declare a constructor at function scope. */
26539 if (parser->in_function_body)
26540 return false;
26541 /* And only certain tokens can begin a constructor declarator. */
26542 next_token = cp_lexer_peek_token (parser->lexer);
26543 if (next_token->type != CPP_NAME
26544 && next_token->type != CPP_SCOPE
26545 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26546 && next_token->type != CPP_TEMPLATE_ID)
26547 return false;
26549 /* Parse tentatively; we are going to roll back all of the tokens
26550 consumed here. */
26551 cp_parser_parse_tentatively (parser);
26552 /* Assume that we are looking at a constructor declarator. */
26553 constructor_p = true;
26555 /* Look for the optional `::' operator. */
26556 cp_parser_global_scope_opt (parser,
26557 /*current_scope_valid_p=*/false);
26558 /* Look for the nested-name-specifier. */
26559 nested_name_specifier
26560 = (cp_parser_nested_name_specifier_opt (parser,
26561 /*typename_keyword_p=*/false,
26562 /*check_dependency_p=*/false,
26563 /*type_p=*/false,
26564 /*is_declaration=*/false));
26566 outside_class_specifier_p = (!at_class_scope_p ()
26567 || !TYPE_BEING_DEFINED (current_class_type)
26568 || friend_p);
26570 /* Outside of a class-specifier, there must be a
26571 nested-name-specifier. Except in C++17 mode, where we
26572 might be declaring a guiding declaration. */
26573 if (!nested_name_specifier && outside_class_specifier_p
26574 && cxx_dialect < cxx17)
26575 constructor_p = false;
26576 else if (nested_name_specifier == error_mark_node)
26577 constructor_p = false;
26579 /* If we have a class scope, this is easy; DR 147 says that S::S always
26580 names the constructor, and no other qualified name could. */
26581 if (constructor_p && nested_name_specifier
26582 && CLASS_TYPE_P (nested_name_specifier))
26584 tree id = cp_parser_unqualified_id (parser,
26585 /*template_keyword_p=*/false,
26586 /*check_dependency_p=*/false,
26587 /*declarator_p=*/true,
26588 /*optional_p=*/false);
26589 if (is_overloaded_fn (id))
26590 id = DECL_NAME (get_first_fn (id));
26591 if (!constructor_name_p (id, nested_name_specifier))
26592 constructor_p = false;
26594 /* If we still think that this might be a constructor-declarator,
26595 look for a class-name. */
26596 else if (constructor_p)
26598 /* If we have:
26600 template <typename T> struct S {
26601 S();
26604 we must recognize that the nested `S' names a class. */
26605 if (cxx_dialect >= cxx17)
26606 cp_parser_parse_tentatively (parser);
26608 tree type_decl;
26609 type_decl = cp_parser_class_name (parser,
26610 /*typename_keyword_p=*/false,
26611 /*template_keyword_p=*/false,
26612 none_type,
26613 /*check_dependency_p=*/false,
26614 /*class_head_p=*/false,
26615 /*is_declaration=*/false);
26617 if (cxx_dialect >= cxx17
26618 && !cp_parser_parse_definitely (parser))
26620 type_decl = NULL_TREE;
26621 tree tmpl = cp_parser_template_name (parser,
26622 /*template_keyword*/false,
26623 /*check_dependency_p*/false,
26624 /*is_declaration*/false,
26625 none_type,
26626 /*is_identifier*/NULL);
26627 if (DECL_CLASS_TEMPLATE_P (tmpl)
26628 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26629 /* It's a deduction guide, return true. */;
26630 else
26631 cp_parser_simulate_error (parser);
26634 /* If there was no class-name, then this is not a constructor.
26635 Otherwise, if we are in a class-specifier and we aren't
26636 handling a friend declaration, check that its type matches
26637 current_class_type (c++/38313). Note: error_mark_node
26638 is left alone for error recovery purposes. */
26639 constructor_p = (!cp_parser_error_occurred (parser)
26640 && (outside_class_specifier_p
26641 || type_decl == NULL_TREE
26642 || type_decl == error_mark_node
26643 || same_type_p (current_class_type,
26644 TREE_TYPE (type_decl))));
26646 /* If we're still considering a constructor, we have to see a `(',
26647 to begin the parameter-declaration-clause, followed by either a
26648 `)', an `...', or a decl-specifier. We need to check for a
26649 type-specifier to avoid being fooled into thinking that:
26651 S (f) (int);
26653 is a constructor. (It is actually a function named `f' that
26654 takes one parameter (of type `int') and returns a value of type
26655 `S'. */
26656 if (constructor_p
26657 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26658 constructor_p = false;
26660 if (constructor_p
26661 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26662 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26663 /* A parameter declaration begins with a decl-specifier,
26664 which is either the "attribute" keyword, a storage class
26665 specifier, or (usually) a type-specifier. */
26666 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26668 tree type;
26669 tree pushed_scope = NULL_TREE;
26670 unsigned saved_num_template_parameter_lists;
26672 /* Names appearing in the type-specifier should be looked up
26673 in the scope of the class. */
26674 if (current_class_type)
26675 type = NULL_TREE;
26676 else if (type_decl)
26678 type = TREE_TYPE (type_decl);
26679 if (TREE_CODE (type) == TYPENAME_TYPE)
26681 type = resolve_typename_type (type,
26682 /*only_current_p=*/false);
26683 if (TREE_CODE (type) == TYPENAME_TYPE)
26685 cp_parser_abort_tentative_parse (parser);
26686 return false;
26689 pushed_scope = push_scope (type);
26692 /* Inside the constructor parameter list, surrounding
26693 template-parameter-lists do not apply. */
26694 saved_num_template_parameter_lists
26695 = parser->num_template_parameter_lists;
26696 parser->num_template_parameter_lists = 0;
26698 /* Look for the type-specifier. */
26699 cp_parser_type_specifier (parser,
26700 CP_PARSER_FLAGS_NONE,
26701 /*decl_specs=*/NULL,
26702 /*is_declarator=*/true,
26703 /*declares_class_or_enum=*/NULL,
26704 /*is_cv_qualifier=*/NULL);
26706 parser->num_template_parameter_lists
26707 = saved_num_template_parameter_lists;
26709 /* Leave the scope of the class. */
26710 if (pushed_scope)
26711 pop_scope (pushed_scope);
26713 constructor_p = !cp_parser_error_occurred (parser);
26717 /* We did not really want to consume any tokens. */
26718 cp_parser_abort_tentative_parse (parser);
26720 return constructor_p;
26723 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26724 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26725 they must be performed once we are in the scope of the function.
26727 Returns the function defined. */
26729 static tree
26730 cp_parser_function_definition_from_specifiers_and_declarator
26731 (cp_parser* parser,
26732 cp_decl_specifier_seq *decl_specifiers,
26733 tree attributes,
26734 const cp_declarator *declarator)
26736 tree fn;
26737 bool success_p;
26739 /* Begin the function-definition. */
26740 success_p = start_function (decl_specifiers, declarator, attributes);
26742 /* The things we're about to see are not directly qualified by any
26743 template headers we've seen thus far. */
26744 reset_specialization ();
26746 /* If there were names looked up in the decl-specifier-seq that we
26747 did not check, check them now. We must wait until we are in the
26748 scope of the function to perform the checks, since the function
26749 might be a friend. */
26750 perform_deferred_access_checks (tf_warning_or_error);
26752 if (success_p)
26754 cp_finalize_omp_declare_simd (parser, current_function_decl);
26755 parser->omp_declare_simd = NULL;
26756 cp_finalize_oacc_routine (parser, current_function_decl, true);
26757 parser->oacc_routine = NULL;
26760 if (!success_p)
26762 /* Skip the entire function. */
26763 cp_parser_skip_to_end_of_block_or_statement (parser);
26764 fn = error_mark_node;
26766 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26768 /* Seen already, skip it. An error message has already been output. */
26769 cp_parser_skip_to_end_of_block_or_statement (parser);
26770 fn = current_function_decl;
26771 current_function_decl = NULL_TREE;
26772 /* If this is a function from a class, pop the nested class. */
26773 if (current_class_name)
26774 pop_nested_class ();
26776 else
26778 timevar_id_t tv;
26779 if (DECL_DECLARED_INLINE_P (current_function_decl))
26780 tv = TV_PARSE_INLINE;
26781 else
26782 tv = TV_PARSE_FUNC;
26783 timevar_push (tv);
26784 fn = cp_parser_function_definition_after_declarator (parser,
26785 /*inline_p=*/false);
26786 timevar_pop (tv);
26789 return fn;
26792 /* Parse the part of a function-definition that follows the
26793 declarator. INLINE_P is TRUE iff this function is an inline
26794 function defined within a class-specifier.
26796 Returns the function defined. */
26798 static tree
26799 cp_parser_function_definition_after_declarator (cp_parser* parser,
26800 bool inline_p)
26802 tree fn;
26803 bool saved_in_unbraced_linkage_specification_p;
26804 bool saved_in_function_body;
26805 unsigned saved_num_template_parameter_lists;
26806 cp_token *token;
26807 bool fully_implicit_function_template_p
26808 = parser->fully_implicit_function_template_p;
26809 parser->fully_implicit_function_template_p = false;
26810 tree implicit_template_parms
26811 = parser->implicit_template_parms;
26812 parser->implicit_template_parms = 0;
26813 cp_binding_level* implicit_template_scope
26814 = parser->implicit_template_scope;
26815 parser->implicit_template_scope = 0;
26817 saved_in_function_body = parser->in_function_body;
26818 parser->in_function_body = true;
26819 /* If the next token is `return', then the code may be trying to
26820 make use of the "named return value" extension that G++ used to
26821 support. */
26822 token = cp_lexer_peek_token (parser->lexer);
26823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26825 /* Consume the `return' keyword. */
26826 cp_lexer_consume_token (parser->lexer);
26827 /* Look for the identifier that indicates what value is to be
26828 returned. */
26829 cp_parser_identifier (parser);
26830 /* Issue an error message. */
26831 error_at (token->location,
26832 "named return values are no longer supported");
26833 /* Skip tokens until we reach the start of the function body. */
26834 while (true)
26836 cp_token *token = cp_lexer_peek_token (parser->lexer);
26837 if (token->type == CPP_OPEN_BRACE
26838 || token->type == CPP_EOF
26839 || token->type == CPP_PRAGMA_EOL)
26840 break;
26841 cp_lexer_consume_token (parser->lexer);
26844 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26845 anything declared inside `f'. */
26846 saved_in_unbraced_linkage_specification_p
26847 = parser->in_unbraced_linkage_specification_p;
26848 parser->in_unbraced_linkage_specification_p = false;
26849 /* Inside the function, surrounding template-parameter-lists do not
26850 apply. */
26851 saved_num_template_parameter_lists
26852 = parser->num_template_parameter_lists;
26853 parser->num_template_parameter_lists = 0;
26855 /* If the next token is `try', `__transaction_atomic', or
26856 `__transaction_relaxed`, then we are looking at either function-try-block
26857 or function-transaction-block. Note that all of these include the
26858 function-body. */
26859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26860 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26861 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26862 RID_TRANSACTION_RELAXED))
26863 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26864 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26865 cp_parser_function_try_block (parser);
26866 else
26867 cp_parser_ctor_initializer_opt_and_function_body
26868 (parser, /*in_function_try_block=*/false);
26870 /* Finish the function. */
26871 fn = finish_function (inline_p);
26872 /* Generate code for it, if necessary. */
26873 expand_or_defer_fn (fn);
26874 /* Restore the saved values. */
26875 parser->in_unbraced_linkage_specification_p
26876 = saved_in_unbraced_linkage_specification_p;
26877 parser->num_template_parameter_lists
26878 = saved_num_template_parameter_lists;
26879 parser->in_function_body = saved_in_function_body;
26881 parser->fully_implicit_function_template_p
26882 = fully_implicit_function_template_p;
26883 parser->implicit_template_parms
26884 = implicit_template_parms;
26885 parser->implicit_template_scope
26886 = implicit_template_scope;
26888 if (parser->fully_implicit_function_template_p)
26889 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26891 return fn;
26894 /* Parse a template-declaration body (following argument list). */
26896 static void
26897 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26898 tree parameter_list,
26899 bool member_p)
26901 tree decl = NULL_TREE;
26902 bool friend_p = false;
26904 /* We just processed one more parameter list. */
26905 ++parser->num_template_parameter_lists;
26907 /* Get the deferred access checks from the parameter list. These
26908 will be checked once we know what is being declared, as for a
26909 member template the checks must be performed in the scope of the
26910 class containing the member. */
26911 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26913 /* Tentatively parse for a new template parameter list, which can either be
26914 the template keyword or a template introduction. */
26915 if (cp_parser_template_declaration_after_export (parser, member_p))
26916 /* OK */;
26917 else if (cxx_dialect >= cxx11
26918 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26919 decl = cp_parser_alias_declaration (parser);
26920 else
26922 /* There are no access checks when parsing a template, as we do not
26923 know if a specialization will be a friend. */
26924 push_deferring_access_checks (dk_no_check);
26925 cp_token *token = cp_lexer_peek_token (parser->lexer);
26926 decl = cp_parser_single_declaration (parser,
26927 checks,
26928 member_p,
26929 /*explicit_specialization_p=*/false,
26930 &friend_p);
26931 pop_deferring_access_checks ();
26933 /* If this is a member template declaration, let the front
26934 end know. */
26935 if (member_p && !friend_p && decl)
26937 if (TREE_CODE (decl) == TYPE_DECL)
26938 cp_parser_check_access_in_redeclaration (decl, token->location);
26940 decl = finish_member_template_decl (decl);
26942 else if (friend_p && decl
26943 && DECL_DECLARES_TYPE_P (decl))
26944 make_friend_class (current_class_type, TREE_TYPE (decl),
26945 /*complain=*/true);
26947 /* We are done with the current parameter list. */
26948 --parser->num_template_parameter_lists;
26950 pop_deferring_access_checks ();
26952 /* Finish up. */
26953 finish_template_decl (parameter_list);
26955 /* Check the template arguments for a literal operator template. */
26956 if (decl
26957 && DECL_DECLARES_FUNCTION_P (decl)
26958 && UDLIT_OPER_P (DECL_NAME (decl)))
26960 bool ok = true;
26961 if (parameter_list == NULL_TREE)
26962 ok = false;
26963 else
26965 int num_parms = TREE_VEC_LENGTH (parameter_list);
26966 if (num_parms == 1)
26968 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26969 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26970 if (TREE_TYPE (parm) != char_type_node
26971 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26972 ok = false;
26974 else if (num_parms == 2 && cxx_dialect >= cxx14)
26976 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26977 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26978 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26979 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26980 if (parm == error_mark_node
26981 || TREE_TYPE (parm) != TREE_TYPE (type)
26982 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26983 ok = false;
26985 else
26986 ok = false;
26988 if (!ok)
26990 if (cxx_dialect >= cxx14)
26991 error ("literal operator template %qD has invalid parameter list."
26992 " Expected non-type template argument pack <char...>"
26993 " or <typename CharT, CharT...>",
26994 decl);
26995 else
26996 error ("literal operator template %qD has invalid parameter list."
26997 " Expected non-type template argument pack <char...>",
26998 decl);
27002 /* Register member declarations. */
27003 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27004 finish_member_declaration (decl);
27005 /* If DECL is a function template, we must return to parse it later.
27006 (Even though there is no definition, there might be default
27007 arguments that need handling.) */
27008 if (member_p && decl
27009 && DECL_DECLARES_FUNCTION_P (decl))
27010 vec_safe_push (unparsed_funs_with_definitions, decl);
27013 /* Parse a template introduction header for a template-declaration. Returns
27014 false if tentative parse fails. */
27016 static bool
27017 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27019 cp_parser_parse_tentatively (parser);
27021 tree saved_scope = parser->scope;
27022 tree saved_object_scope = parser->object_scope;
27023 tree saved_qualifying_scope = parser->qualifying_scope;
27025 /* Look for the optional `::' operator. */
27026 cp_parser_global_scope_opt (parser,
27027 /*current_scope_valid_p=*/false);
27028 /* Look for the nested-name-specifier. */
27029 cp_parser_nested_name_specifier_opt (parser,
27030 /*typename_keyword_p=*/false,
27031 /*check_dependency_p=*/true,
27032 /*type_p=*/false,
27033 /*is_declaration=*/false);
27035 cp_token *token = cp_lexer_peek_token (parser->lexer);
27036 tree concept_name = cp_parser_identifier (parser);
27038 /* Look up the concept for which we will be matching
27039 template parameters. */
27040 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27041 token->location);
27042 parser->scope = saved_scope;
27043 parser->object_scope = saved_object_scope;
27044 parser->qualifying_scope = saved_qualifying_scope;
27046 if (concept_name == error_mark_node)
27047 cp_parser_simulate_error (parser);
27049 /* Look for opening brace for introduction. */
27050 matching_braces braces;
27051 braces.require_open (parser);
27053 if (!cp_parser_parse_definitely (parser))
27054 return false;
27056 push_deferring_access_checks (dk_deferred);
27058 /* Build vector of placeholder parameters and grab
27059 matching identifiers. */
27060 tree introduction_list = cp_parser_introduction_list (parser);
27062 /* The introduction-list shall not be empty. */
27063 int nargs = TREE_VEC_LENGTH (introduction_list);
27064 if (nargs == 0)
27066 error ("empty introduction-list");
27067 return true;
27070 /* Look for closing brace for introduction. */
27071 if (!braces.require_close (parser))
27072 return true;
27074 if (tmpl_decl == error_mark_node)
27076 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27077 token->location);
27078 return true;
27081 /* Build and associate the constraint. */
27082 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27083 if (parms && parms != error_mark_node)
27085 cp_parser_template_declaration_after_parameters (parser, parms,
27086 member_p);
27087 return true;
27090 error_at (token->location, "no matching concept for template-introduction");
27091 return true;
27094 /* Parse a normal template-declaration following the template keyword. */
27096 static void
27097 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27099 tree parameter_list;
27100 bool need_lang_pop;
27101 location_t location = input_location;
27103 /* Look for the `<' token. */
27104 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27105 return;
27106 if (at_class_scope_p () && current_function_decl)
27108 /* 14.5.2.2 [temp.mem]
27110 A local class shall not have member templates. */
27111 error_at (location,
27112 "invalid declaration of member template in local class");
27113 cp_parser_skip_to_end_of_block_or_statement (parser);
27114 return;
27116 /* [temp]
27118 A template ... shall not have C linkage. */
27119 if (current_lang_name == lang_name_c)
27121 error_at (location, "template with C linkage");
27122 maybe_show_extern_c_location ();
27123 /* Give it C++ linkage to avoid confusing other parts of the
27124 front end. */
27125 push_lang_context (lang_name_cplusplus);
27126 need_lang_pop = true;
27128 else
27129 need_lang_pop = false;
27131 /* We cannot perform access checks on the template parameter
27132 declarations until we know what is being declared, just as we
27133 cannot check the decl-specifier list. */
27134 push_deferring_access_checks (dk_deferred);
27136 /* If the next token is `>', then we have an invalid
27137 specialization. Rather than complain about an invalid template
27138 parameter, issue an error message here. */
27139 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27141 cp_parser_error (parser, "invalid explicit specialization");
27142 begin_specialization ();
27143 parameter_list = NULL_TREE;
27145 else
27147 /* Parse the template parameters. */
27148 parameter_list = cp_parser_template_parameter_list (parser);
27151 /* Look for the `>'. */
27152 cp_parser_skip_to_end_of_template_parameter_list (parser);
27154 /* Manage template requirements */
27155 if (flag_concepts)
27157 tree reqs = get_shorthand_constraints (current_template_parms);
27158 if (tree r = cp_parser_requires_clause_opt (parser))
27159 reqs = conjoin_constraints (reqs, normalize_expression (r));
27160 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27163 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27164 member_p);
27166 /* For the erroneous case of a template with C linkage, we pushed an
27167 implicit C++ linkage scope; exit that scope now. */
27168 if (need_lang_pop)
27169 pop_lang_context ();
27172 /* Parse a template-declaration, assuming that the `export' (and
27173 `extern') keywords, if present, has already been scanned. MEMBER_P
27174 is as for cp_parser_template_declaration. */
27176 static bool
27177 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27181 cp_lexer_consume_token (parser->lexer);
27182 cp_parser_explicit_template_declaration (parser, member_p);
27183 return true;
27185 else if (flag_concepts)
27186 return cp_parser_template_introduction (parser, member_p);
27188 return false;
27191 /* Perform the deferred access checks from a template-parameter-list.
27192 CHECKS is a TREE_LIST of access checks, as returned by
27193 get_deferred_access_checks. */
27195 static void
27196 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27198 ++processing_template_parmlist;
27199 perform_access_checks (checks, tf_warning_or_error);
27200 --processing_template_parmlist;
27203 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27204 `function-definition' sequence that follows a template header.
27205 If MEMBER_P is true, this declaration appears in a class scope.
27207 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27208 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27210 static tree
27211 cp_parser_single_declaration (cp_parser* parser,
27212 vec<deferred_access_check, va_gc> *checks,
27213 bool member_p,
27214 bool explicit_specialization_p,
27215 bool* friend_p)
27217 int declares_class_or_enum;
27218 tree decl = NULL_TREE;
27219 cp_decl_specifier_seq decl_specifiers;
27220 bool function_definition_p = false;
27221 cp_token *decl_spec_token_start;
27223 /* This function is only used when processing a template
27224 declaration. */
27225 gcc_assert (innermost_scope_kind () == sk_template_parms
27226 || innermost_scope_kind () == sk_template_spec);
27228 /* Defer access checks until we know what is being declared. */
27229 push_deferring_access_checks (dk_deferred);
27231 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27232 alternative. */
27233 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27234 cp_parser_decl_specifier_seq (parser,
27235 CP_PARSER_FLAGS_OPTIONAL,
27236 &decl_specifiers,
27237 &declares_class_or_enum);
27238 if (friend_p)
27239 *friend_p = cp_parser_friend_p (&decl_specifiers);
27241 /* There are no template typedefs. */
27242 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27244 error_at (decl_spec_token_start->location,
27245 "template declaration of %<typedef%>");
27246 decl = error_mark_node;
27249 /* Gather up the access checks that occurred the
27250 decl-specifier-seq. */
27251 stop_deferring_access_checks ();
27253 /* Check for the declaration of a template class. */
27254 if (declares_class_or_enum)
27256 if (cp_parser_declares_only_class_p (parser)
27257 || (declares_class_or_enum & 2))
27259 // If this is a declaration, but not a definition, associate
27260 // any constraints with the type declaration. Constraints
27261 // are associated with definitions in cp_parser_class_specifier.
27262 if (declares_class_or_enum == 1)
27263 associate_classtype_constraints (decl_specifiers.type);
27265 decl = shadow_tag (&decl_specifiers);
27267 /* In this case:
27269 struct C {
27270 friend template <typename T> struct A<T>::B;
27273 A<T>::B will be represented by a TYPENAME_TYPE, and
27274 therefore not recognized by shadow_tag. */
27275 if (friend_p && *friend_p
27276 && !decl
27277 && decl_specifiers.type
27278 && TYPE_P (decl_specifiers.type))
27279 decl = decl_specifiers.type;
27281 if (decl && decl != error_mark_node)
27282 decl = TYPE_NAME (decl);
27283 else
27284 decl = error_mark_node;
27286 /* Perform access checks for template parameters. */
27287 cp_parser_perform_template_parameter_access_checks (checks);
27289 /* Give a helpful diagnostic for
27290 template <class T> struct A { } a;
27291 if we aren't already recovering from an error. */
27292 if (!cp_parser_declares_only_class_p (parser)
27293 && !seen_error ())
27295 error_at (cp_lexer_peek_token (parser->lexer)->location,
27296 "a class template declaration must not declare "
27297 "anything else");
27298 cp_parser_skip_to_end_of_block_or_statement (parser);
27299 goto out;
27304 /* Complain about missing 'typename' or other invalid type names. */
27305 if (!decl_specifiers.any_type_specifiers_p
27306 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27308 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27309 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27310 the rest of this declaration. */
27311 decl = error_mark_node;
27312 goto out;
27315 /* If it's not a template class, try for a template function. If
27316 the next token is a `;', then this declaration does not declare
27317 anything. But, if there were errors in the decl-specifiers, then
27318 the error might well have come from an attempted class-specifier.
27319 In that case, there's no need to warn about a missing declarator. */
27320 if (!decl
27321 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27322 || decl_specifiers.type != error_mark_node))
27324 decl = cp_parser_init_declarator (parser,
27325 &decl_specifiers,
27326 checks,
27327 /*function_definition_allowed_p=*/true,
27328 member_p,
27329 declares_class_or_enum,
27330 &function_definition_p,
27331 NULL, NULL, NULL);
27333 /* 7.1.1-1 [dcl.stc]
27335 A storage-class-specifier shall not be specified in an explicit
27336 specialization... */
27337 if (decl
27338 && explicit_specialization_p
27339 && decl_specifiers.storage_class != sc_none)
27341 error_at (decl_spec_token_start->location,
27342 "explicit template specialization cannot have a storage class");
27343 decl = error_mark_node;
27346 if (decl && VAR_P (decl))
27347 check_template_variable (decl);
27350 /* Look for a trailing `;' after the declaration. */
27351 if (!function_definition_p
27352 && (decl == error_mark_node
27353 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27354 cp_parser_skip_to_end_of_block_or_statement (parser);
27356 out:
27357 pop_deferring_access_checks ();
27359 /* Clear any current qualification; whatever comes next is the start
27360 of something new. */
27361 parser->scope = NULL_TREE;
27362 parser->qualifying_scope = NULL_TREE;
27363 parser->object_scope = NULL_TREE;
27365 return decl;
27368 /* Parse a cast-expression that is not the operand of a unary "&". */
27370 static cp_expr
27371 cp_parser_simple_cast_expression (cp_parser *parser)
27373 return cp_parser_cast_expression (parser, /*address_p=*/false,
27374 /*cast_p=*/false, /*decltype*/false, NULL);
27377 /* Parse a functional cast to TYPE. Returns an expression
27378 representing the cast. */
27380 static cp_expr
27381 cp_parser_functional_cast (cp_parser* parser, tree type)
27383 vec<tree, va_gc> *vec;
27384 tree expression_list;
27385 cp_expr cast;
27386 bool nonconst_p;
27388 location_t start_loc = input_location;
27390 if (!type)
27391 type = error_mark_node;
27393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27395 cp_lexer_set_source_position (parser->lexer);
27396 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27397 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27398 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27399 if (TREE_CODE (type) == TYPE_DECL)
27400 type = TREE_TYPE (type);
27402 cast = finish_compound_literal (type, expression_list,
27403 tf_warning_or_error, fcl_functional);
27404 /* Create a location of the form:
27405 type_name{i, f}
27406 ^~~~~~~~~~~~~~~
27407 with caret == start at the start of the type name,
27408 finishing at the closing brace. */
27409 location_t finish_loc
27410 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27411 location_t combined_loc = make_location (start_loc, start_loc,
27412 finish_loc);
27413 cast.set_location (combined_loc);
27414 return cast;
27418 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27419 /*cast_p=*/true,
27420 /*allow_expansion_p=*/true,
27421 /*non_constant_p=*/NULL);
27422 if (vec == NULL)
27423 expression_list = error_mark_node;
27424 else
27426 expression_list = build_tree_list_vec (vec);
27427 release_tree_vector (vec);
27430 cast = build_functional_cast (type, expression_list,
27431 tf_warning_or_error);
27432 /* [expr.const]/1: In an integral constant expression "only type
27433 conversions to integral or enumeration type can be used". */
27434 if (TREE_CODE (type) == TYPE_DECL)
27435 type = TREE_TYPE (type);
27436 if (cast != error_mark_node
27437 && !cast_valid_in_integral_constant_expression_p (type)
27438 && cp_parser_non_integral_constant_expression (parser,
27439 NIC_CONSTRUCTOR))
27440 return error_mark_node;
27442 /* Create a location of the form:
27443 float(i)
27444 ^~~~~~~~
27445 with caret == start at the start of the type name,
27446 finishing at the closing paren. */
27447 location_t finish_loc
27448 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27449 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27450 cast.set_location (combined_loc);
27451 return cast;
27454 /* Save the tokens that make up the body of a member function defined
27455 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27456 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27457 specifiers applied to the declaration. Returns the FUNCTION_DECL
27458 for the member function. */
27460 static tree
27461 cp_parser_save_member_function_body (cp_parser* parser,
27462 cp_decl_specifier_seq *decl_specifiers,
27463 cp_declarator *declarator,
27464 tree attributes)
27466 cp_token *first;
27467 cp_token *last;
27468 tree fn;
27469 bool function_try_block = false;
27471 /* Create the FUNCTION_DECL. */
27472 fn = grokmethod (decl_specifiers, declarator, attributes);
27473 cp_finalize_omp_declare_simd (parser, fn);
27474 cp_finalize_oacc_routine (parser, fn, true);
27475 /* If something went badly wrong, bail out now. */
27476 if (fn == error_mark_node)
27478 /* If there's a function-body, skip it. */
27479 if (cp_parser_token_starts_function_definition_p
27480 (cp_lexer_peek_token (parser->lexer)))
27481 cp_parser_skip_to_end_of_block_or_statement (parser);
27482 return error_mark_node;
27485 /* Remember it, if there default args to post process. */
27486 cp_parser_save_default_args (parser, fn);
27488 /* Save away the tokens that make up the body of the
27489 function. */
27490 first = parser->lexer->next_token;
27492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27493 cp_lexer_consume_token (parser->lexer);
27494 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27495 RID_TRANSACTION_ATOMIC))
27497 cp_lexer_consume_token (parser->lexer);
27498 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27500 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27501 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27502 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27503 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27504 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27506 cp_lexer_consume_token (parser->lexer);
27507 cp_lexer_consume_token (parser->lexer);
27508 cp_lexer_consume_token (parser->lexer);
27509 cp_lexer_consume_token (parser->lexer);
27510 cp_lexer_consume_token (parser->lexer);
27512 else
27513 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27514 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27516 cp_lexer_consume_token (parser->lexer);
27517 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27518 break;
27522 /* Handle function try blocks. */
27523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27525 cp_lexer_consume_token (parser->lexer);
27526 function_try_block = true;
27528 /* We can have braced-init-list mem-initializers before the fn body. */
27529 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27531 cp_lexer_consume_token (parser->lexer);
27532 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27534 /* cache_group will stop after an un-nested { } pair, too. */
27535 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27536 break;
27538 /* variadic mem-inits have ... after the ')'. */
27539 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27540 cp_lexer_consume_token (parser->lexer);
27543 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27544 /* Handle function try blocks. */
27545 if (function_try_block)
27546 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27547 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27548 last = parser->lexer->next_token;
27550 /* Save away the inline definition; we will process it when the
27551 class is complete. */
27552 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27553 DECL_PENDING_INLINE_P (fn) = 1;
27555 /* We need to know that this was defined in the class, so that
27556 friend templates are handled correctly. */
27557 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27559 /* Add FN to the queue of functions to be parsed later. */
27560 vec_safe_push (unparsed_funs_with_definitions, fn);
27562 return fn;
27565 /* Save the tokens that make up the in-class initializer for a non-static
27566 data member. Returns a DEFAULT_ARG. */
27568 static tree
27569 cp_parser_save_nsdmi (cp_parser* parser)
27571 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27574 /* Parse a template-argument-list, as well as the trailing ">" (but
27575 not the opening "<"). See cp_parser_template_argument_list for the
27576 return value. */
27578 static tree
27579 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27581 tree arguments;
27582 tree saved_scope;
27583 tree saved_qualifying_scope;
27584 tree saved_object_scope;
27585 bool saved_greater_than_is_operator_p;
27586 int saved_unevaluated_operand;
27587 int saved_inhibit_evaluation_warnings;
27589 /* [temp.names]
27591 When parsing a template-id, the first non-nested `>' is taken as
27592 the end of the template-argument-list rather than a greater-than
27593 operator. */
27594 saved_greater_than_is_operator_p
27595 = parser->greater_than_is_operator_p;
27596 parser->greater_than_is_operator_p = false;
27597 /* Parsing the argument list may modify SCOPE, so we save it
27598 here. */
27599 saved_scope = parser->scope;
27600 saved_qualifying_scope = parser->qualifying_scope;
27601 saved_object_scope = parser->object_scope;
27602 /* We need to evaluate the template arguments, even though this
27603 template-id may be nested within a "sizeof". */
27604 saved_unevaluated_operand = cp_unevaluated_operand;
27605 cp_unevaluated_operand = 0;
27606 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27607 c_inhibit_evaluation_warnings = 0;
27608 /* Parse the template-argument-list itself. */
27609 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27610 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27611 arguments = NULL_TREE;
27612 else
27613 arguments = cp_parser_template_argument_list (parser);
27614 /* Look for the `>' that ends the template-argument-list. If we find
27615 a '>>' instead, it's probably just a typo. */
27616 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27618 if (cxx_dialect != cxx98)
27620 /* In C++0x, a `>>' in a template argument list or cast
27621 expression is considered to be two separate `>'
27622 tokens. So, change the current token to a `>', but don't
27623 consume it: it will be consumed later when the outer
27624 template argument list (or cast expression) is parsed.
27625 Note that this replacement of `>' for `>>' is necessary
27626 even if we are parsing tentatively: in the tentative
27627 case, after calling
27628 cp_parser_enclosed_template_argument_list we will always
27629 throw away all of the template arguments and the first
27630 closing `>', either because the template argument list
27631 was erroneous or because we are replacing those tokens
27632 with a CPP_TEMPLATE_ID token. The second `>' (which will
27633 not have been thrown away) is needed either to close an
27634 outer template argument list or to complete a new-style
27635 cast. */
27636 cp_token *token = cp_lexer_peek_token (parser->lexer);
27637 token->type = CPP_GREATER;
27639 else if (!saved_greater_than_is_operator_p)
27641 /* If we're in a nested template argument list, the '>>' has
27642 to be a typo for '> >'. We emit the error message, but we
27643 continue parsing and we push a '>' as next token, so that
27644 the argument list will be parsed correctly. Note that the
27645 global source location is still on the token before the
27646 '>>', so we need to say explicitly where we want it. */
27647 cp_token *token = cp_lexer_peek_token (parser->lexer);
27648 gcc_rich_location richloc (token->location);
27649 richloc.add_fixit_replace ("> >");
27650 error_at (&richloc, "%<>>%> should be %<> >%> "
27651 "within a nested template argument list");
27653 token->type = CPP_GREATER;
27655 else
27657 /* If this is not a nested template argument list, the '>>'
27658 is a typo for '>'. Emit an error message and continue.
27659 Same deal about the token location, but here we can get it
27660 right by consuming the '>>' before issuing the diagnostic. */
27661 cp_token *token = cp_lexer_consume_token (parser->lexer);
27662 error_at (token->location,
27663 "spurious %<>>%>, use %<>%> to terminate "
27664 "a template argument list");
27667 else
27668 cp_parser_skip_to_end_of_template_parameter_list (parser);
27669 /* The `>' token might be a greater-than operator again now. */
27670 parser->greater_than_is_operator_p
27671 = saved_greater_than_is_operator_p;
27672 /* Restore the SAVED_SCOPE. */
27673 parser->scope = saved_scope;
27674 parser->qualifying_scope = saved_qualifying_scope;
27675 parser->object_scope = saved_object_scope;
27676 cp_unevaluated_operand = saved_unevaluated_operand;
27677 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27679 return arguments;
27682 /* MEMBER_FUNCTION is a member function, or a friend. If default
27683 arguments, or the body of the function have not yet been parsed,
27684 parse them now. */
27686 static void
27687 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27689 timevar_push (TV_PARSE_INMETH);
27690 /* If this member is a template, get the underlying
27691 FUNCTION_DECL. */
27692 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27693 member_function = DECL_TEMPLATE_RESULT (member_function);
27695 /* There should not be any class definitions in progress at this
27696 point; the bodies of members are only parsed outside of all class
27697 definitions. */
27698 gcc_assert (parser->num_classes_being_defined == 0);
27699 /* While we're parsing the member functions we might encounter more
27700 classes. We want to handle them right away, but we don't want
27701 them getting mixed up with functions that are currently in the
27702 queue. */
27703 push_unparsed_function_queues (parser);
27705 /* Make sure that any template parameters are in scope. */
27706 maybe_begin_member_template_processing (member_function);
27708 /* If the body of the function has not yet been parsed, parse it
27709 now. */
27710 if (DECL_PENDING_INLINE_P (member_function))
27712 tree function_scope;
27713 cp_token_cache *tokens;
27715 /* The function is no longer pending; we are processing it. */
27716 tokens = DECL_PENDING_INLINE_INFO (member_function);
27717 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27718 DECL_PENDING_INLINE_P (member_function) = 0;
27720 /* If this is a local class, enter the scope of the containing
27721 function. */
27722 function_scope = current_function_decl;
27723 if (function_scope)
27724 push_function_context ();
27726 /* Push the body of the function onto the lexer stack. */
27727 cp_parser_push_lexer_for_tokens (parser, tokens);
27729 /* Let the front end know that we going to be defining this
27730 function. */
27731 start_preparsed_function (member_function, NULL_TREE,
27732 SF_PRE_PARSED | SF_INCLASS_INLINE);
27734 /* Don't do access checking if it is a templated function. */
27735 if (processing_template_decl)
27736 push_deferring_access_checks (dk_no_check);
27738 /* #pragma omp declare reduction needs special parsing. */
27739 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27741 parser->lexer->in_pragma = true;
27742 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27743 finish_function (/*inline_p=*/true);
27744 cp_check_omp_declare_reduction (member_function);
27746 else
27747 /* Now, parse the body of the function. */
27748 cp_parser_function_definition_after_declarator (parser,
27749 /*inline_p=*/true);
27751 if (processing_template_decl)
27752 pop_deferring_access_checks ();
27754 /* Leave the scope of the containing function. */
27755 if (function_scope)
27756 pop_function_context ();
27757 cp_parser_pop_lexer (parser);
27760 /* Remove any template parameters from the symbol table. */
27761 maybe_end_member_template_processing ();
27763 /* Restore the queue. */
27764 pop_unparsed_function_queues (parser);
27765 timevar_pop (TV_PARSE_INMETH);
27768 /* If DECL contains any default args, remember it on the unparsed
27769 functions queue. */
27771 static void
27772 cp_parser_save_default_args (cp_parser* parser, tree decl)
27774 tree probe;
27776 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27777 probe;
27778 probe = TREE_CHAIN (probe))
27779 if (TREE_PURPOSE (probe))
27781 cp_default_arg_entry entry = {current_class_type, decl};
27782 vec_safe_push (unparsed_funs_with_default_args, entry);
27783 break;
27787 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27788 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27789 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27790 from the parameter-type-list. */
27792 static tree
27793 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27794 tree default_arg, tree parmtype)
27796 cp_token_cache *tokens;
27797 tree parsed_arg;
27798 bool dummy;
27800 if (default_arg == error_mark_node)
27801 return error_mark_node;
27803 /* Push the saved tokens for the default argument onto the parser's
27804 lexer stack. */
27805 tokens = DEFARG_TOKENS (default_arg);
27806 cp_parser_push_lexer_for_tokens (parser, tokens);
27808 start_lambda_scope (decl);
27810 /* Parse the default argument. */
27811 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27812 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27813 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27815 finish_lambda_scope ();
27817 if (parsed_arg == error_mark_node)
27818 cp_parser_skip_to_end_of_statement (parser);
27820 if (!processing_template_decl)
27822 /* In a non-template class, check conversions now. In a template,
27823 we'll wait and instantiate these as needed. */
27824 if (TREE_CODE (decl) == PARM_DECL)
27825 parsed_arg = check_default_argument (parmtype, parsed_arg,
27826 tf_warning_or_error);
27827 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27828 parsed_arg = error_mark_node;
27829 else
27830 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27833 /* If the token stream has not been completely used up, then
27834 there was extra junk after the end of the default
27835 argument. */
27836 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27838 if (TREE_CODE (decl) == PARM_DECL)
27839 cp_parser_error (parser, "expected %<,%>");
27840 else
27841 cp_parser_error (parser, "expected %<;%>");
27844 /* Revert to the main lexer. */
27845 cp_parser_pop_lexer (parser);
27847 return parsed_arg;
27850 /* FIELD is a non-static data member with an initializer which we saved for
27851 later; parse it now. */
27853 static void
27854 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27856 tree def;
27858 maybe_begin_member_template_processing (field);
27860 push_unparsed_function_queues (parser);
27861 def = cp_parser_late_parse_one_default_arg (parser, field,
27862 DECL_INITIAL (field),
27863 NULL_TREE);
27864 pop_unparsed_function_queues (parser);
27866 maybe_end_member_template_processing ();
27868 DECL_INITIAL (field) = def;
27871 /* FN is a FUNCTION_DECL which may contains a parameter with an
27872 unparsed DEFAULT_ARG. Parse the default args now. This function
27873 assumes that the current scope is the scope in which the default
27874 argument should be processed. */
27876 static void
27877 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27879 bool saved_local_variables_forbidden_p;
27880 tree parm, parmdecl;
27882 /* While we're parsing the default args, we might (due to the
27883 statement expression extension) encounter more classes. We want
27884 to handle them right away, but we don't want them getting mixed
27885 up with default args that are currently in the queue. */
27886 push_unparsed_function_queues (parser);
27888 /* Local variable names (and the `this' keyword) may not appear
27889 in a default argument. */
27890 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27891 parser->local_variables_forbidden_p = true;
27893 push_defarg_context (fn);
27895 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27896 parmdecl = DECL_ARGUMENTS (fn);
27897 parm && parm != void_list_node;
27898 parm = TREE_CHAIN (parm),
27899 parmdecl = DECL_CHAIN (parmdecl))
27901 tree default_arg = TREE_PURPOSE (parm);
27902 tree parsed_arg;
27903 vec<tree, va_gc> *insts;
27904 tree copy;
27905 unsigned ix;
27907 if (!default_arg)
27908 continue;
27910 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27911 /* This can happen for a friend declaration for a function
27912 already declared with default arguments. */
27913 continue;
27915 parsed_arg
27916 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27917 default_arg,
27918 TREE_VALUE (parm));
27919 TREE_PURPOSE (parm) = parsed_arg;
27921 /* Update any instantiations we've already created. */
27922 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27923 vec_safe_iterate (insts, ix, &copy); ix++)
27924 TREE_PURPOSE (copy) = parsed_arg;
27927 pop_defarg_context ();
27929 /* Make sure no default arg is missing. */
27930 check_default_args (fn);
27932 /* Restore the state of local_variables_forbidden_p. */
27933 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27935 /* Restore the queue. */
27936 pop_unparsed_function_queues (parser);
27939 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27941 sizeof ... ( identifier )
27943 where the 'sizeof' token has already been consumed. */
27945 static tree
27946 cp_parser_sizeof_pack (cp_parser *parser)
27948 /* Consume the `...'. */
27949 cp_lexer_consume_token (parser->lexer);
27950 maybe_warn_variadic_templates ();
27952 matching_parens parens;
27953 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27954 if (paren)
27955 parens.consume_open (parser);
27956 else
27957 permerror (cp_lexer_peek_token (parser->lexer)->location,
27958 "%<sizeof...%> argument must be surrounded by parentheses");
27960 cp_token *token = cp_lexer_peek_token (parser->lexer);
27961 tree name = cp_parser_identifier (parser);
27962 if (name == error_mark_node)
27963 return error_mark_node;
27964 /* The name is not qualified. */
27965 parser->scope = NULL_TREE;
27966 parser->qualifying_scope = NULL_TREE;
27967 parser->object_scope = NULL_TREE;
27968 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27969 if (expr == error_mark_node)
27970 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27971 token->location);
27972 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27973 expr = TREE_TYPE (expr);
27974 else if (TREE_CODE (expr) == CONST_DECL)
27975 expr = DECL_INITIAL (expr);
27976 expr = make_pack_expansion (expr);
27977 PACK_EXPANSION_SIZEOF_P (expr) = true;
27979 if (paren)
27980 parens.require_close (parser);
27982 return expr;
27985 /* Parse the operand of `sizeof' (or a similar operator). Returns
27986 either a TYPE or an expression, depending on the form of the
27987 input. The KEYWORD indicates which kind of expression we have
27988 encountered. */
27990 static tree
27991 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27993 tree expr = NULL_TREE;
27994 const char *saved_message;
27995 char *tmp;
27996 bool saved_integral_constant_expression_p;
27997 bool saved_non_integral_constant_expression_p;
27999 /* If it's a `...', then we are computing the length of a parameter
28000 pack. */
28001 if (keyword == RID_SIZEOF
28002 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28003 return cp_parser_sizeof_pack (parser);
28005 /* Types cannot be defined in a `sizeof' expression. Save away the
28006 old message. */
28007 saved_message = parser->type_definition_forbidden_message;
28008 /* And create the new one. */
28009 tmp = concat ("types may not be defined in %<",
28010 IDENTIFIER_POINTER (ridpointers[keyword]),
28011 "%> expressions", NULL);
28012 parser->type_definition_forbidden_message = tmp;
28014 /* The restrictions on constant-expressions do not apply inside
28015 sizeof expressions. */
28016 saved_integral_constant_expression_p
28017 = parser->integral_constant_expression_p;
28018 saved_non_integral_constant_expression_p
28019 = parser->non_integral_constant_expression_p;
28020 parser->integral_constant_expression_p = false;
28022 /* Do not actually evaluate the expression. */
28023 ++cp_unevaluated_operand;
28024 ++c_inhibit_evaluation_warnings;
28025 /* If it's a `(', then we might be looking at the type-id
28026 construction. */
28027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28029 tree type = NULL_TREE;
28031 /* We can't be sure yet whether we're looking at a type-id or an
28032 expression. */
28033 cp_parser_parse_tentatively (parser);
28035 matching_parens parens;
28036 parens.consume_open (parser);
28038 /* Note: as a GNU Extension, compound literals are considered
28039 postfix-expressions as they are in C99, so they are valid
28040 arguments to sizeof. See comment in cp_parser_cast_expression
28041 for details. */
28042 if (cp_parser_compound_literal_p (parser))
28043 cp_parser_simulate_error (parser);
28044 else
28046 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28047 parser->in_type_id_in_expr_p = true;
28048 /* Look for the type-id. */
28049 type = cp_parser_type_id (parser);
28050 /* Look for the closing `)'. */
28051 parens.require_close (parser);
28052 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28055 /* If all went well, then we're done. */
28056 if (cp_parser_parse_definitely (parser))
28058 cp_decl_specifier_seq decl_specs;
28060 /* Build a trivial decl-specifier-seq. */
28061 clear_decl_specs (&decl_specs);
28062 decl_specs.type = type;
28064 /* Call grokdeclarator to figure out what type this is. */
28065 expr = grokdeclarator (NULL,
28066 &decl_specs,
28067 TYPENAME,
28068 /*initialized=*/0,
28069 /*attrlist=*/NULL);
28073 /* If the type-id production did not work out, then we must be
28074 looking at the unary-expression production. */
28075 if (!expr)
28076 expr = cp_parser_unary_expression (parser);
28078 /* Go back to evaluating expressions. */
28079 --cp_unevaluated_operand;
28080 --c_inhibit_evaluation_warnings;
28082 /* Free the message we created. */
28083 free (tmp);
28084 /* And restore the old one. */
28085 parser->type_definition_forbidden_message = saved_message;
28086 parser->integral_constant_expression_p
28087 = saved_integral_constant_expression_p;
28088 parser->non_integral_constant_expression_p
28089 = saved_non_integral_constant_expression_p;
28091 return expr;
28094 /* If the current declaration has no declarator, return true. */
28096 static bool
28097 cp_parser_declares_only_class_p (cp_parser *parser)
28099 /* If the next token is a `;' or a `,' then there is no
28100 declarator. */
28101 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28102 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28105 /* Update the DECL_SPECS to reflect the storage class indicated by
28106 KEYWORD. */
28108 static void
28109 cp_parser_set_storage_class (cp_parser *parser,
28110 cp_decl_specifier_seq *decl_specs,
28111 enum rid keyword,
28112 cp_token *token)
28114 cp_storage_class storage_class;
28116 if (parser->in_unbraced_linkage_specification_p)
28118 error_at (token->location, "invalid use of %qD in linkage specification",
28119 ridpointers[keyword]);
28120 return;
28122 else if (decl_specs->storage_class != sc_none)
28124 decl_specs->conflicting_specifiers_p = true;
28125 return;
28128 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28129 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28130 && decl_specs->gnu_thread_keyword_p)
28132 pedwarn (decl_specs->locations[ds_thread], 0,
28133 "%<__thread%> before %qD", ridpointers[keyword]);
28136 switch (keyword)
28138 case RID_AUTO:
28139 storage_class = sc_auto;
28140 break;
28141 case RID_REGISTER:
28142 storage_class = sc_register;
28143 break;
28144 case RID_STATIC:
28145 storage_class = sc_static;
28146 break;
28147 case RID_EXTERN:
28148 storage_class = sc_extern;
28149 break;
28150 case RID_MUTABLE:
28151 storage_class = sc_mutable;
28152 break;
28153 default:
28154 gcc_unreachable ();
28156 decl_specs->storage_class = storage_class;
28157 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28159 /* A storage class specifier cannot be applied alongside a typedef
28160 specifier. If there is a typedef specifier present then set
28161 conflicting_specifiers_p which will trigger an error later
28162 on in grokdeclarator. */
28163 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28164 decl_specs->conflicting_specifiers_p = true;
28167 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28168 is true, the type is a class or enum definition. */
28170 static void
28171 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28172 tree type_spec,
28173 cp_token *token,
28174 bool type_definition_p)
28176 decl_specs->any_specifiers_p = true;
28178 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28179 (with, for example, in "typedef int wchar_t;") we remember that
28180 this is what happened. In system headers, we ignore these
28181 declarations so that G++ can work with system headers that are not
28182 C++-safe. */
28183 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28184 && !type_definition_p
28185 && (type_spec == boolean_type_node
28186 || type_spec == char16_type_node
28187 || type_spec == char32_type_node
28188 || type_spec == wchar_type_node)
28189 && (decl_specs->type
28190 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28191 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28192 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28193 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28195 decl_specs->redefined_builtin_type = type_spec;
28196 set_and_check_decl_spec_loc (decl_specs,
28197 ds_redefined_builtin_type_spec,
28198 token);
28199 if (!decl_specs->type)
28201 decl_specs->type = type_spec;
28202 decl_specs->type_definition_p = false;
28203 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28206 else if (decl_specs->type)
28207 decl_specs->multiple_types_p = true;
28208 else
28210 decl_specs->type = type_spec;
28211 decl_specs->type_definition_p = type_definition_p;
28212 decl_specs->redefined_builtin_type = NULL_TREE;
28213 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28217 /* True iff TOKEN is the GNU keyword __thread. */
28219 static bool
28220 token_is__thread (cp_token *token)
28222 gcc_assert (token->keyword == RID_THREAD);
28223 return id_equal (token->u.value, "__thread");
28226 /* Set the location for a declarator specifier and check if it is
28227 duplicated.
28229 DECL_SPECS is the sequence of declarator specifiers onto which to
28230 set the location.
28232 DS is the single declarator specifier to set which location is to
28233 be set onto the existing sequence of declarators.
28235 LOCATION is the location for the declarator specifier to
28236 consider. */
28238 static void
28239 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28240 cp_decl_spec ds, cp_token *token)
28242 gcc_assert (ds < ds_last);
28244 if (decl_specs == NULL)
28245 return;
28247 source_location location = token->location;
28249 if (decl_specs->locations[ds] == 0)
28251 decl_specs->locations[ds] = location;
28252 if (ds == ds_thread)
28253 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28255 else
28257 if (ds == ds_long)
28259 if (decl_specs->locations[ds_long_long] != 0)
28260 error_at (location,
28261 "%<long long long%> is too long for GCC");
28262 else
28264 decl_specs->locations[ds_long_long] = location;
28265 pedwarn_cxx98 (location,
28266 OPT_Wlong_long,
28267 "ISO C++ 1998 does not support %<long long%>");
28270 else if (ds == ds_thread)
28272 bool gnu = token_is__thread (token);
28273 if (gnu != decl_specs->gnu_thread_keyword_p)
28274 error_at (location,
28275 "both %<__thread%> and %<thread_local%> specified");
28276 else
28278 gcc_rich_location richloc (location);
28279 richloc.add_fixit_remove ();
28280 error_at (&richloc, "duplicate %qD", token->u.value);
28283 else
28285 static const char *const decl_spec_names[] = {
28286 "signed",
28287 "unsigned",
28288 "short",
28289 "long",
28290 "const",
28291 "volatile",
28292 "restrict",
28293 "inline",
28294 "virtual",
28295 "explicit",
28296 "friend",
28297 "typedef",
28298 "using",
28299 "constexpr",
28300 "__complex"
28302 gcc_rich_location richloc (location);
28303 richloc.add_fixit_remove ();
28304 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28309 /* Return true iff the declarator specifier DS is present in the
28310 sequence of declarator specifiers DECL_SPECS. */
28312 bool
28313 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28314 cp_decl_spec ds)
28316 gcc_assert (ds < ds_last);
28318 if (decl_specs == NULL)
28319 return false;
28321 return decl_specs->locations[ds] != 0;
28324 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28325 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28327 static bool
28328 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28330 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28333 /* Issue an error message indicating that TOKEN_DESC was expected.
28334 If KEYWORD is true, it indicated this function is called by
28335 cp_parser_require_keword and the required token can only be
28336 a indicated keyword.
28338 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28339 within any error as the location of an "opening" token matching
28340 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28341 RT_CLOSE_PAREN). */
28343 static void
28344 cp_parser_required_error (cp_parser *parser,
28345 required_token token_desc,
28346 bool keyword,
28347 location_t matching_location)
28349 if (cp_parser_simulate_error (parser))
28350 return;
28352 const char *gmsgid = NULL;
28353 switch (token_desc)
28355 case RT_NEW:
28356 gmsgid = G_("expected %<new%>");
28357 break;
28358 case RT_DELETE:
28359 gmsgid = G_("expected %<delete%>");
28360 break;
28361 case RT_RETURN:
28362 gmsgid = G_("expected %<return%>");
28363 break;
28364 case RT_WHILE:
28365 gmsgid = G_("expected %<while%>");
28366 break;
28367 case RT_EXTERN:
28368 gmsgid = G_("expected %<extern%>");
28369 break;
28370 case RT_STATIC_ASSERT:
28371 gmsgid = G_("expected %<static_assert%>");
28372 break;
28373 case RT_DECLTYPE:
28374 gmsgid = G_("expected %<decltype%>");
28375 break;
28376 case RT_OPERATOR:
28377 gmsgid = G_("expected %<operator%>");
28378 break;
28379 case RT_CLASS:
28380 gmsgid = G_("expected %<class%>");
28381 break;
28382 case RT_TEMPLATE:
28383 gmsgid = G_("expected %<template%>");
28384 break;
28385 case RT_NAMESPACE:
28386 gmsgid = G_("expected %<namespace%>");
28387 break;
28388 case RT_USING:
28389 gmsgid = G_("expected %<using%>");
28390 break;
28391 case RT_ASM:
28392 gmsgid = G_("expected %<asm%>");
28393 break;
28394 case RT_TRY:
28395 gmsgid = G_("expected %<try%>");
28396 break;
28397 case RT_CATCH:
28398 gmsgid = G_("expected %<catch%>");
28399 break;
28400 case RT_THROW:
28401 gmsgid = G_("expected %<throw%>");
28402 break;
28403 case RT_LABEL:
28404 gmsgid = G_("expected %<__label__%>");
28405 break;
28406 case RT_AT_TRY:
28407 gmsgid = G_("expected %<@try%>");
28408 break;
28409 case RT_AT_SYNCHRONIZED:
28410 gmsgid = G_("expected %<@synchronized%>");
28411 break;
28412 case RT_AT_THROW:
28413 gmsgid = G_("expected %<@throw%>");
28414 break;
28415 case RT_TRANSACTION_ATOMIC:
28416 gmsgid = G_("expected %<__transaction_atomic%>");
28417 break;
28418 case RT_TRANSACTION_RELAXED:
28419 gmsgid = G_("expected %<__transaction_relaxed%>");
28420 break;
28421 default:
28422 break;
28425 if (!gmsgid && !keyword)
28427 switch (token_desc)
28429 case RT_SEMICOLON:
28430 gmsgid = G_("expected %<;%>");
28431 break;
28432 case RT_OPEN_PAREN:
28433 gmsgid = G_("expected %<(%>");
28434 break;
28435 case RT_CLOSE_BRACE:
28436 gmsgid = G_("expected %<}%>");
28437 break;
28438 case RT_OPEN_BRACE:
28439 gmsgid = G_("expected %<{%>");
28440 break;
28441 case RT_CLOSE_SQUARE:
28442 gmsgid = G_("expected %<]%>");
28443 break;
28444 case RT_OPEN_SQUARE:
28445 gmsgid = G_("expected %<[%>");
28446 break;
28447 case RT_COMMA:
28448 gmsgid = G_("expected %<,%>");
28449 break;
28450 case RT_SCOPE:
28451 gmsgid = G_("expected %<::%>");
28452 break;
28453 case RT_LESS:
28454 gmsgid = G_("expected %<<%>");
28455 break;
28456 case RT_GREATER:
28457 gmsgid = G_("expected %<>%>");
28458 break;
28459 case RT_EQ:
28460 gmsgid = G_("expected %<=%>");
28461 break;
28462 case RT_ELLIPSIS:
28463 gmsgid = G_("expected %<...%>");
28464 break;
28465 case RT_MULT:
28466 gmsgid = G_("expected %<*%>");
28467 break;
28468 case RT_COMPL:
28469 gmsgid = G_("expected %<~%>");
28470 break;
28471 case RT_COLON:
28472 gmsgid = G_("expected %<:%>");
28473 break;
28474 case RT_COLON_SCOPE:
28475 gmsgid = G_("expected %<:%> or %<::%>");
28476 break;
28477 case RT_CLOSE_PAREN:
28478 gmsgid = G_("expected %<)%>");
28479 break;
28480 case RT_COMMA_CLOSE_PAREN:
28481 gmsgid = G_("expected %<,%> or %<)%>");
28482 break;
28483 case RT_PRAGMA_EOL:
28484 gmsgid = G_("expected end of line");
28485 break;
28486 case RT_NAME:
28487 gmsgid = G_("expected identifier");
28488 break;
28489 case RT_SELECT:
28490 gmsgid = G_("expected selection-statement");
28491 break;
28492 case RT_ITERATION:
28493 gmsgid = G_("expected iteration-statement");
28494 break;
28495 case RT_JUMP:
28496 gmsgid = G_("expected jump-statement");
28497 break;
28498 case RT_CLASS_KEY:
28499 gmsgid = G_("expected class-key");
28500 break;
28501 case RT_CLASS_TYPENAME_TEMPLATE:
28502 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28503 break;
28504 default:
28505 gcc_unreachable ();
28509 if (gmsgid)
28510 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28514 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28515 issue an error message indicating that TOKEN_DESC was expected.
28517 Returns the token consumed, if the token had the appropriate type.
28518 Otherwise, returns NULL.
28520 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28521 within any error as the location of an "opening" token matching
28522 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28523 RT_CLOSE_PAREN). */
28525 static cp_token *
28526 cp_parser_require (cp_parser* parser,
28527 enum cpp_ttype type,
28528 required_token token_desc,
28529 location_t matching_location)
28531 if (cp_lexer_next_token_is (parser->lexer, type))
28532 return cp_lexer_consume_token (parser->lexer);
28533 else
28535 /* Output the MESSAGE -- unless we're parsing tentatively. */
28536 if (!cp_parser_simulate_error (parser))
28537 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28538 matching_location);
28539 return NULL;
28543 /* An error message is produced if the next token is not '>'.
28544 All further tokens are skipped until the desired token is
28545 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28547 static void
28548 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28550 /* Current level of '< ... >'. */
28551 unsigned level = 0;
28552 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28553 unsigned nesting_depth = 0;
28555 /* Are we ready, yet? If not, issue error message. */
28556 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28557 return;
28559 /* Skip tokens until the desired token is found. */
28560 while (true)
28562 /* Peek at the next token. */
28563 switch (cp_lexer_peek_token (parser->lexer)->type)
28565 case CPP_LESS:
28566 if (!nesting_depth)
28567 ++level;
28568 break;
28570 case CPP_RSHIFT:
28571 if (cxx_dialect == cxx98)
28572 /* C++0x views the `>>' operator as two `>' tokens, but
28573 C++98 does not. */
28574 break;
28575 else if (!nesting_depth && level-- == 0)
28577 /* We've hit a `>>' where the first `>' closes the
28578 template argument list, and the second `>' is
28579 spurious. Just consume the `>>' and stop; we've
28580 already produced at least one error. */
28581 cp_lexer_consume_token (parser->lexer);
28582 return;
28584 /* Fall through for C++0x, so we handle the second `>' in
28585 the `>>'. */
28586 gcc_fallthrough ();
28588 case CPP_GREATER:
28589 if (!nesting_depth && level-- == 0)
28591 /* We've reached the token we want, consume it and stop. */
28592 cp_lexer_consume_token (parser->lexer);
28593 return;
28595 break;
28597 case CPP_OPEN_PAREN:
28598 case CPP_OPEN_SQUARE:
28599 ++nesting_depth;
28600 break;
28602 case CPP_CLOSE_PAREN:
28603 case CPP_CLOSE_SQUARE:
28604 if (nesting_depth-- == 0)
28605 return;
28606 break;
28608 case CPP_EOF:
28609 case CPP_PRAGMA_EOL:
28610 case CPP_SEMICOLON:
28611 case CPP_OPEN_BRACE:
28612 case CPP_CLOSE_BRACE:
28613 /* The '>' was probably forgotten, don't look further. */
28614 return;
28616 default:
28617 break;
28620 /* Consume this token. */
28621 cp_lexer_consume_token (parser->lexer);
28625 /* If the next token is the indicated keyword, consume it. Otherwise,
28626 issue an error message indicating that TOKEN_DESC was expected.
28628 Returns the token consumed, if the token had the appropriate type.
28629 Otherwise, returns NULL. */
28631 static cp_token *
28632 cp_parser_require_keyword (cp_parser* parser,
28633 enum rid keyword,
28634 required_token token_desc)
28636 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28638 if (token && token->keyword != keyword)
28640 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28641 UNKNOWN_LOCATION);
28642 return NULL;
28645 return token;
28648 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28649 function-definition. */
28651 static bool
28652 cp_parser_token_starts_function_definition_p (cp_token* token)
28654 return (/* An ordinary function-body begins with an `{'. */
28655 token->type == CPP_OPEN_BRACE
28656 /* A ctor-initializer begins with a `:'. */
28657 || token->type == CPP_COLON
28658 /* A function-try-block begins with `try'. */
28659 || token->keyword == RID_TRY
28660 /* A function-transaction-block begins with `__transaction_atomic'
28661 or `__transaction_relaxed'. */
28662 || token->keyword == RID_TRANSACTION_ATOMIC
28663 || token->keyword == RID_TRANSACTION_RELAXED
28664 /* The named return value extension begins with `return'. */
28665 || token->keyword == RID_RETURN);
28668 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28669 definition. */
28671 static bool
28672 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28674 cp_token *token;
28676 token = cp_lexer_peek_token (parser->lexer);
28677 return (token->type == CPP_OPEN_BRACE
28678 || (token->type == CPP_COLON
28679 && !parser->colon_doesnt_start_class_def_p));
28682 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28683 C++0x) ending a template-argument. */
28685 static bool
28686 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28688 cp_token *token;
28690 token = cp_lexer_peek_token (parser->lexer);
28691 return (token->type == CPP_COMMA
28692 || token->type == CPP_GREATER
28693 || token->type == CPP_ELLIPSIS
28694 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28697 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28698 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28700 static bool
28701 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28702 size_t n)
28704 cp_token *token;
28706 token = cp_lexer_peek_nth_token (parser->lexer, n);
28707 if (token->type == CPP_LESS)
28708 return true;
28709 /* Check for the sequence `<::' in the original code. It would be lexed as
28710 `[:', where `[' is a digraph, and there is no whitespace before
28711 `:'. */
28712 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28714 cp_token *token2;
28715 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28716 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28717 return true;
28719 return false;
28722 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28723 or none_type otherwise. */
28725 static enum tag_types
28726 cp_parser_token_is_class_key (cp_token* token)
28728 switch (token->keyword)
28730 case RID_CLASS:
28731 return class_type;
28732 case RID_STRUCT:
28733 return record_type;
28734 case RID_UNION:
28735 return union_type;
28737 default:
28738 return none_type;
28742 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28743 or none_type otherwise or if the token is null. */
28745 static enum tag_types
28746 cp_parser_token_is_type_parameter_key (cp_token* token)
28748 if (!token)
28749 return none_type;
28751 switch (token->keyword)
28753 case RID_CLASS:
28754 return class_type;
28755 case RID_TYPENAME:
28756 return typename_type;
28758 default:
28759 return none_type;
28763 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28765 static void
28766 cp_parser_check_class_key (enum tag_types class_key, tree type)
28768 if (type == error_mark_node)
28769 return;
28770 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28772 if (permerror (input_location, "%qs tag used in naming %q#T",
28773 class_key == union_type ? "union"
28774 : class_key == record_type ? "struct" : "class",
28775 type))
28776 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28777 "%q#T was previously declared here", type);
28781 /* Issue an error message if DECL is redeclared with different
28782 access than its original declaration [class.access.spec/3].
28783 This applies to nested classes, nested class templates and
28784 enumerations [class.mem/1]. */
28786 static void
28787 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28789 if (!decl
28790 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28791 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28792 return;
28794 if ((TREE_PRIVATE (decl)
28795 != (current_access_specifier == access_private_node))
28796 || (TREE_PROTECTED (decl)
28797 != (current_access_specifier == access_protected_node)))
28798 error_at (location, "%qD redeclared with different access", decl);
28801 /* Look for the `template' keyword, as a syntactic disambiguator.
28802 Return TRUE iff it is present, in which case it will be
28803 consumed. */
28805 static bool
28806 cp_parser_optional_template_keyword (cp_parser *parser)
28808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28810 /* In C++98 the `template' keyword can only be used within templates;
28811 outside templates the parser can always figure out what is a
28812 template and what is not. In C++11, per the resolution of DR 468,
28813 `template' is allowed in cases where it is not strictly necessary. */
28814 if (!processing_template_decl
28815 && pedantic && cxx_dialect == cxx98)
28817 cp_token *token = cp_lexer_peek_token (parser->lexer);
28818 pedwarn (token->location, OPT_Wpedantic,
28819 "in C++98 %<template%> (as a disambiguator) is only "
28820 "allowed within templates");
28821 /* If this part of the token stream is rescanned, the same
28822 error message would be generated. So, we purge the token
28823 from the stream. */
28824 cp_lexer_purge_token (parser->lexer);
28825 return false;
28827 else
28829 /* Consume the `template' keyword. */
28830 cp_lexer_consume_token (parser->lexer);
28831 return true;
28834 return false;
28837 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28838 set PARSER->SCOPE, and perform other related actions. */
28840 static void
28841 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28843 struct tree_check *check_value;
28845 /* Get the stored value. */
28846 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28847 /* Set the scope from the stored value. */
28848 parser->scope = saved_checks_value (check_value);
28849 parser->qualifying_scope = check_value->qualifying_scope;
28850 parser->object_scope = NULL_TREE;
28853 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28854 encounter the end of a block before what we were looking for. */
28856 static bool
28857 cp_parser_cache_group (cp_parser *parser,
28858 enum cpp_ttype end,
28859 unsigned depth)
28861 while (true)
28863 cp_token *token = cp_lexer_peek_token (parser->lexer);
28865 /* Abort a parenthesized expression if we encounter a semicolon. */
28866 if ((end == CPP_CLOSE_PAREN || depth == 0)
28867 && token->type == CPP_SEMICOLON)
28868 return true;
28869 /* If we've reached the end of the file, stop. */
28870 if (token->type == CPP_EOF
28871 || (end != CPP_PRAGMA_EOL
28872 && token->type == CPP_PRAGMA_EOL))
28873 return true;
28874 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28875 /* We've hit the end of an enclosing block, so there's been some
28876 kind of syntax error. */
28877 return true;
28879 /* Consume the token. */
28880 cp_lexer_consume_token (parser->lexer);
28881 /* See if it starts a new group. */
28882 if (token->type == CPP_OPEN_BRACE)
28884 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28885 /* In theory this should probably check end == '}', but
28886 cp_parser_save_member_function_body needs it to exit
28887 after either '}' or ')' when called with ')'. */
28888 if (depth == 0)
28889 return false;
28891 else if (token->type == CPP_OPEN_PAREN)
28893 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28894 if (depth == 0 && end == CPP_CLOSE_PAREN)
28895 return false;
28897 else if (token->type == CPP_PRAGMA)
28898 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28899 else if (token->type == end)
28900 return false;
28904 /* Like above, for caching a default argument or NSDMI. Both of these are
28905 terminated by a non-nested comma, but it can be unclear whether or not a
28906 comma is nested in a template argument list unless we do more parsing.
28907 In order to handle this ambiguity, when we encounter a ',' after a '<'
28908 we try to parse what follows as a parameter-declaration-list (in the
28909 case of a default argument) or a member-declarator (in the case of an
28910 NSDMI). If that succeeds, then we stop caching. */
28912 static tree
28913 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28915 unsigned depth = 0;
28916 int maybe_template_id = 0;
28917 cp_token *first_token;
28918 cp_token *token;
28919 tree default_argument;
28921 /* Add tokens until we have processed the entire default
28922 argument. We add the range [first_token, token). */
28923 first_token = cp_lexer_peek_token (parser->lexer);
28924 if (first_token->type == CPP_OPEN_BRACE)
28926 /* For list-initialization, this is straightforward. */
28927 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28928 token = cp_lexer_peek_token (parser->lexer);
28930 else while (true)
28932 bool done = false;
28934 /* Peek at the next token. */
28935 token = cp_lexer_peek_token (parser->lexer);
28936 /* What we do depends on what token we have. */
28937 switch (token->type)
28939 /* In valid code, a default argument must be
28940 immediately followed by a `,' `)', or `...'. */
28941 case CPP_COMMA:
28942 if (depth == 0 && maybe_template_id)
28944 /* If we've seen a '<', we might be in a
28945 template-argument-list. Until Core issue 325 is
28946 resolved, we don't know how this situation ought
28947 to be handled, so try to DTRT. We check whether
28948 what comes after the comma is a valid parameter
28949 declaration list. If it is, then the comma ends
28950 the default argument; otherwise the default
28951 argument continues. */
28952 bool error = false;
28953 cp_token *peek;
28955 /* Set ITALP so cp_parser_parameter_declaration_list
28956 doesn't decide to commit to this parse. */
28957 bool saved_italp = parser->in_template_argument_list_p;
28958 parser->in_template_argument_list_p = true;
28960 cp_parser_parse_tentatively (parser);
28962 if (nsdmi)
28964 /* Parse declarators until we reach a non-comma or
28965 somthing that cannot be an initializer.
28966 Just checking whether we're looking at a single
28967 declarator is insufficient. Consider:
28968 int var = tuple<T,U>::x;
28969 The template parameter 'U' looks exactly like a
28970 declarator. */
28973 int ctor_dtor_or_conv_p;
28974 cp_lexer_consume_token (parser->lexer);
28975 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28976 &ctor_dtor_or_conv_p,
28977 /*parenthesized_p=*/NULL,
28978 /*member_p=*/true,
28979 /*friend_p=*/false);
28980 peek = cp_lexer_peek_token (parser->lexer);
28981 if (cp_parser_error_occurred (parser))
28982 break;
28984 while (peek->type == CPP_COMMA);
28985 /* If we met an '=' or ';' then the original comma
28986 was the end of the NSDMI. Otherwise assume
28987 we're still in the NSDMI. */
28988 error = (peek->type != CPP_EQ
28989 && peek->type != CPP_SEMICOLON);
28991 else
28993 cp_lexer_consume_token (parser->lexer);
28994 begin_scope (sk_function_parms, NULL_TREE);
28995 cp_parser_parameter_declaration_list (parser, &error);
28996 pop_bindings_and_leave_scope ();
28998 if (!cp_parser_error_occurred (parser) && !error)
28999 done = true;
29000 cp_parser_abort_tentative_parse (parser);
29002 parser->in_template_argument_list_p = saved_italp;
29003 break;
29005 /* FALLTHRU */
29006 case CPP_CLOSE_PAREN:
29007 case CPP_ELLIPSIS:
29008 /* If we run into a non-nested `;', `}', or `]',
29009 then the code is invalid -- but the default
29010 argument is certainly over. */
29011 case CPP_SEMICOLON:
29012 case CPP_CLOSE_BRACE:
29013 case CPP_CLOSE_SQUARE:
29014 if (depth == 0
29015 /* Handle correctly int n = sizeof ... ( p ); */
29016 && token->type != CPP_ELLIPSIS)
29017 done = true;
29018 /* Update DEPTH, if necessary. */
29019 else if (token->type == CPP_CLOSE_PAREN
29020 || token->type == CPP_CLOSE_BRACE
29021 || token->type == CPP_CLOSE_SQUARE)
29022 --depth;
29023 break;
29025 case CPP_OPEN_PAREN:
29026 case CPP_OPEN_SQUARE:
29027 case CPP_OPEN_BRACE:
29028 ++depth;
29029 break;
29031 case CPP_LESS:
29032 if (depth == 0)
29033 /* This might be the comparison operator, or it might
29034 start a template argument list. */
29035 ++maybe_template_id;
29036 break;
29038 case CPP_RSHIFT:
29039 if (cxx_dialect == cxx98)
29040 break;
29041 /* Fall through for C++0x, which treats the `>>'
29042 operator like two `>' tokens in certain
29043 cases. */
29044 gcc_fallthrough ();
29046 case CPP_GREATER:
29047 if (depth == 0)
29049 /* This might be an operator, or it might close a
29050 template argument list. But if a previous '<'
29051 started a template argument list, this will have
29052 closed it, so we can't be in one anymore. */
29053 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29054 if (maybe_template_id < 0)
29055 maybe_template_id = 0;
29057 break;
29059 /* If we run out of tokens, issue an error message. */
29060 case CPP_EOF:
29061 case CPP_PRAGMA_EOL:
29062 error_at (token->location, "file ends in default argument");
29063 return error_mark_node;
29065 case CPP_NAME:
29066 case CPP_SCOPE:
29067 /* In these cases, we should look for template-ids.
29068 For example, if the default argument is
29069 `X<int, double>()', we need to do name lookup to
29070 figure out whether or not `X' is a template; if
29071 so, the `,' does not end the default argument.
29073 That is not yet done. */
29074 break;
29076 default:
29077 break;
29080 /* If we've reached the end, stop. */
29081 if (done)
29082 break;
29084 /* Add the token to the token block. */
29085 token = cp_lexer_consume_token (parser->lexer);
29088 /* Create a DEFAULT_ARG to represent the unparsed default
29089 argument. */
29090 default_argument = make_node (DEFAULT_ARG);
29091 DEFARG_TOKENS (default_argument)
29092 = cp_token_cache_new (first_token, token);
29093 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29095 return default_argument;
29098 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29100 location_t
29101 defarg_location (tree default_argument)
29103 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29104 location_t start = tokens->first->location;
29105 location_t end = tokens->last->location;
29106 return make_location (start, start, end);
29109 /* Begin parsing tentatively. We always save tokens while parsing
29110 tentatively so that if the tentative parsing fails we can restore the
29111 tokens. */
29113 static void
29114 cp_parser_parse_tentatively (cp_parser* parser)
29116 /* Enter a new parsing context. */
29117 parser->context = cp_parser_context_new (parser->context);
29118 /* Begin saving tokens. */
29119 cp_lexer_save_tokens (parser->lexer);
29120 /* In order to avoid repetitive access control error messages,
29121 access checks are queued up until we are no longer parsing
29122 tentatively. */
29123 push_deferring_access_checks (dk_deferred);
29126 /* Commit to the currently active tentative parse. */
29128 static void
29129 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29131 cp_parser_context *context;
29132 cp_lexer *lexer;
29134 /* Mark all of the levels as committed. */
29135 lexer = parser->lexer;
29136 for (context = parser->context; context->next; context = context->next)
29138 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29139 break;
29140 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29141 while (!cp_lexer_saving_tokens (lexer))
29142 lexer = lexer->next;
29143 cp_lexer_commit_tokens (lexer);
29147 /* Commit to the topmost currently active tentative parse.
29149 Note that this function shouldn't be called when there are
29150 irreversible side-effects while in a tentative state. For
29151 example, we shouldn't create a permanent entry in the symbol
29152 table, or issue an error message that might not apply if the
29153 tentative parse is aborted. */
29155 static void
29156 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29158 cp_parser_context *context = parser->context;
29159 cp_lexer *lexer = parser->lexer;
29161 if (context)
29163 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29164 return;
29165 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29167 while (!cp_lexer_saving_tokens (lexer))
29168 lexer = lexer->next;
29169 cp_lexer_commit_tokens (lexer);
29173 /* Abort the currently active tentative parse. All consumed tokens
29174 will be rolled back, and no diagnostics will be issued. */
29176 static void
29177 cp_parser_abort_tentative_parse (cp_parser* parser)
29179 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29180 || errorcount > 0);
29181 cp_parser_simulate_error (parser);
29182 /* Now, pretend that we want to see if the construct was
29183 successfully parsed. */
29184 cp_parser_parse_definitely (parser);
29187 /* Stop parsing tentatively. If a parse error has occurred, restore the
29188 token stream. Otherwise, commit to the tokens we have consumed.
29189 Returns true if no error occurred; false otherwise. */
29191 static bool
29192 cp_parser_parse_definitely (cp_parser* parser)
29194 bool error_occurred;
29195 cp_parser_context *context;
29197 /* Remember whether or not an error occurred, since we are about to
29198 destroy that information. */
29199 error_occurred = cp_parser_error_occurred (parser);
29200 /* Remove the topmost context from the stack. */
29201 context = parser->context;
29202 parser->context = context->next;
29203 /* If no parse errors occurred, commit to the tentative parse. */
29204 if (!error_occurred)
29206 /* Commit to the tokens read tentatively, unless that was
29207 already done. */
29208 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29209 cp_lexer_commit_tokens (parser->lexer);
29211 pop_to_parent_deferring_access_checks ();
29213 /* Otherwise, if errors occurred, roll back our state so that things
29214 are just as they were before we began the tentative parse. */
29215 else
29217 cp_lexer_rollback_tokens (parser->lexer);
29218 pop_deferring_access_checks ();
29220 /* Add the context to the front of the free list. */
29221 context->next = cp_parser_context_free_list;
29222 cp_parser_context_free_list = context;
29224 return !error_occurred;
29227 /* Returns true if we are parsing tentatively and are not committed to
29228 this tentative parse. */
29230 static bool
29231 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29233 return (cp_parser_parsing_tentatively (parser)
29234 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29237 /* Returns nonzero iff an error has occurred during the most recent
29238 tentative parse. */
29240 static bool
29241 cp_parser_error_occurred (cp_parser* parser)
29243 return (cp_parser_parsing_tentatively (parser)
29244 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29247 /* Returns nonzero if GNU extensions are allowed. */
29249 static bool
29250 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29252 return parser->allow_gnu_extensions_p;
29255 /* Objective-C++ Productions */
29258 /* Parse an Objective-C expression, which feeds into a primary-expression
29259 above.
29261 objc-expression:
29262 objc-message-expression
29263 objc-string-literal
29264 objc-encode-expression
29265 objc-protocol-expression
29266 objc-selector-expression
29268 Returns a tree representation of the expression. */
29270 static cp_expr
29271 cp_parser_objc_expression (cp_parser* parser)
29273 /* Try to figure out what kind of declaration is present. */
29274 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29276 switch (kwd->type)
29278 case CPP_OPEN_SQUARE:
29279 return cp_parser_objc_message_expression (parser);
29281 case CPP_OBJC_STRING:
29282 kwd = cp_lexer_consume_token (parser->lexer);
29283 return objc_build_string_object (kwd->u.value);
29285 case CPP_KEYWORD:
29286 switch (kwd->keyword)
29288 case RID_AT_ENCODE:
29289 return cp_parser_objc_encode_expression (parser);
29291 case RID_AT_PROTOCOL:
29292 return cp_parser_objc_protocol_expression (parser);
29294 case RID_AT_SELECTOR:
29295 return cp_parser_objc_selector_expression (parser);
29297 default:
29298 break;
29300 /* FALLTHRU */
29301 default:
29302 error_at (kwd->location,
29303 "misplaced %<@%D%> Objective-C++ construct",
29304 kwd->u.value);
29305 cp_parser_skip_to_end_of_block_or_statement (parser);
29308 return error_mark_node;
29311 /* Parse an Objective-C message expression.
29313 objc-message-expression:
29314 [ objc-message-receiver objc-message-args ]
29316 Returns a representation of an Objective-C message. */
29318 static tree
29319 cp_parser_objc_message_expression (cp_parser* parser)
29321 tree receiver, messageargs;
29323 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29324 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29325 receiver = cp_parser_objc_message_receiver (parser);
29326 messageargs = cp_parser_objc_message_args (parser);
29327 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29328 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29330 tree result = objc_build_message_expr (receiver, messageargs);
29332 /* Construct a location e.g.
29333 [self func1:5]
29334 ^~~~~~~~~~~~~~
29335 ranging from the '[' to the ']', with the caret at the start. */
29336 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29337 protected_set_expr_location (result, combined_loc);
29339 return result;
29342 /* Parse an objc-message-receiver.
29344 objc-message-receiver:
29345 expression
29346 simple-type-specifier
29348 Returns a representation of the type or expression. */
29350 static tree
29351 cp_parser_objc_message_receiver (cp_parser* parser)
29353 tree rcv;
29355 /* An Objective-C message receiver may be either (1) a type
29356 or (2) an expression. */
29357 cp_parser_parse_tentatively (parser);
29358 rcv = cp_parser_expression (parser);
29360 /* If that worked out, fine. */
29361 if (cp_parser_parse_definitely (parser))
29362 return rcv;
29364 cp_parser_parse_tentatively (parser);
29365 rcv = cp_parser_simple_type_specifier (parser,
29366 /*decl_specs=*/NULL,
29367 CP_PARSER_FLAGS_NONE);
29369 if (cp_parser_parse_definitely (parser))
29370 return objc_get_class_reference (rcv);
29372 cp_parser_error (parser, "objective-c++ message receiver expected");
29373 return error_mark_node;
29376 /* Parse the arguments and selectors comprising an Objective-C message.
29378 objc-message-args:
29379 objc-selector
29380 objc-selector-args
29381 objc-selector-args , objc-comma-args
29383 objc-selector-args:
29384 objc-selector [opt] : assignment-expression
29385 objc-selector-args objc-selector [opt] : assignment-expression
29387 objc-comma-args:
29388 assignment-expression
29389 objc-comma-args , assignment-expression
29391 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29392 selector arguments and TREE_VALUE containing a list of comma
29393 arguments. */
29395 static tree
29396 cp_parser_objc_message_args (cp_parser* parser)
29398 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29399 bool maybe_unary_selector_p = true;
29400 cp_token *token = cp_lexer_peek_token (parser->lexer);
29402 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29404 tree selector = NULL_TREE, arg;
29406 if (token->type != CPP_COLON)
29407 selector = cp_parser_objc_selector (parser);
29409 /* Detect if we have a unary selector. */
29410 if (maybe_unary_selector_p
29411 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29412 return build_tree_list (selector, NULL_TREE);
29414 maybe_unary_selector_p = false;
29415 cp_parser_require (parser, CPP_COLON, RT_COLON);
29416 arg = cp_parser_assignment_expression (parser);
29418 sel_args
29419 = chainon (sel_args,
29420 build_tree_list (selector, arg));
29422 token = cp_lexer_peek_token (parser->lexer);
29425 /* Handle non-selector arguments, if any. */
29426 while (token->type == CPP_COMMA)
29428 tree arg;
29430 cp_lexer_consume_token (parser->lexer);
29431 arg = cp_parser_assignment_expression (parser);
29433 addl_args
29434 = chainon (addl_args,
29435 build_tree_list (NULL_TREE, arg));
29437 token = cp_lexer_peek_token (parser->lexer);
29440 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29442 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29443 return build_tree_list (error_mark_node, error_mark_node);
29446 return build_tree_list (sel_args, addl_args);
29449 /* Parse an Objective-C encode expression.
29451 objc-encode-expression:
29452 @encode objc-typename
29454 Returns an encoded representation of the type argument. */
29456 static cp_expr
29457 cp_parser_objc_encode_expression (cp_parser* parser)
29459 tree type;
29460 cp_token *token;
29461 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29463 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29464 matching_parens parens;
29465 parens.require_open (parser);
29466 token = cp_lexer_peek_token (parser->lexer);
29467 type = complete_type (cp_parser_type_id (parser));
29468 parens.require_close (parser);
29470 if (!type)
29472 error_at (token->location,
29473 "%<@encode%> must specify a type as an argument");
29474 return error_mark_node;
29477 /* This happens if we find @encode(T) (where T is a template
29478 typename or something dependent on a template typename) when
29479 parsing a template. In that case, we can't compile it
29480 immediately, but we rather create an AT_ENCODE_EXPR which will
29481 need to be instantiated when the template is used.
29483 if (dependent_type_p (type))
29485 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29486 TREE_READONLY (value) = 1;
29487 return value;
29491 /* Build a location of the form:
29492 @encode(int)
29493 ^~~~~~~~~~~~
29494 with caret==start at the @ token, finishing at the close paren. */
29495 location_t combined_loc
29496 = make_location (start_loc, start_loc,
29497 cp_lexer_previous_token (parser->lexer)->location);
29499 return cp_expr (objc_build_encode_expr (type), combined_loc);
29502 /* Parse an Objective-C @defs expression. */
29504 static tree
29505 cp_parser_objc_defs_expression (cp_parser *parser)
29507 tree name;
29509 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29510 matching_parens parens;
29511 parens.require_open (parser);
29512 name = cp_parser_identifier (parser);
29513 parens.require_close (parser);
29515 return objc_get_class_ivars (name);
29518 /* Parse an Objective-C protocol expression.
29520 objc-protocol-expression:
29521 @protocol ( identifier )
29523 Returns a representation of the protocol expression. */
29525 static tree
29526 cp_parser_objc_protocol_expression (cp_parser* parser)
29528 tree proto;
29529 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29531 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29532 matching_parens parens;
29533 parens.require_open (parser);
29534 proto = cp_parser_identifier (parser);
29535 parens.require_close (parser);
29537 /* Build a location of the form:
29538 @protocol(prot)
29539 ^~~~~~~~~~~~~~~
29540 with caret==start at the @ token, finishing at the close paren. */
29541 location_t combined_loc
29542 = make_location (start_loc, start_loc,
29543 cp_lexer_previous_token (parser->lexer)->location);
29544 tree result = objc_build_protocol_expr (proto);
29545 protected_set_expr_location (result, combined_loc);
29546 return result;
29549 /* Parse an Objective-C selector expression.
29551 objc-selector-expression:
29552 @selector ( objc-method-signature )
29554 objc-method-signature:
29555 objc-selector
29556 objc-selector-seq
29558 objc-selector-seq:
29559 objc-selector :
29560 objc-selector-seq objc-selector :
29562 Returns a representation of the method selector. */
29564 static tree
29565 cp_parser_objc_selector_expression (cp_parser* parser)
29567 tree sel_seq = NULL_TREE;
29568 bool maybe_unary_selector_p = true;
29569 cp_token *token;
29570 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29572 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29573 matching_parens parens;
29574 parens.require_open (parser);
29575 token = cp_lexer_peek_token (parser->lexer);
29577 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29578 || token->type == CPP_SCOPE)
29580 tree selector = NULL_TREE;
29582 if (token->type != CPP_COLON
29583 || token->type == CPP_SCOPE)
29584 selector = cp_parser_objc_selector (parser);
29586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29587 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29589 /* Detect if we have a unary selector. */
29590 if (maybe_unary_selector_p)
29592 sel_seq = selector;
29593 goto finish_selector;
29595 else
29597 cp_parser_error (parser, "expected %<:%>");
29600 maybe_unary_selector_p = false;
29601 token = cp_lexer_consume_token (parser->lexer);
29603 if (token->type == CPP_SCOPE)
29605 sel_seq
29606 = chainon (sel_seq,
29607 build_tree_list (selector, NULL_TREE));
29608 sel_seq
29609 = chainon (sel_seq,
29610 build_tree_list (NULL_TREE, NULL_TREE));
29612 else
29613 sel_seq
29614 = chainon (sel_seq,
29615 build_tree_list (selector, NULL_TREE));
29617 token = cp_lexer_peek_token (parser->lexer);
29620 finish_selector:
29621 parens.require_close (parser);
29624 /* Build a location of the form:
29625 @selector(func)
29626 ^~~~~~~~~~~~~~~
29627 with caret==start at the @ token, finishing at the close paren. */
29628 location_t combined_loc
29629 = make_location (loc, loc,
29630 cp_lexer_previous_token (parser->lexer)->location);
29631 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29632 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29633 protected_set_expr_location (result, combined_loc);
29634 return result;
29637 /* Parse a list of identifiers.
29639 objc-identifier-list:
29640 identifier
29641 objc-identifier-list , identifier
29643 Returns a TREE_LIST of identifier nodes. */
29645 static tree
29646 cp_parser_objc_identifier_list (cp_parser* parser)
29648 tree identifier;
29649 tree list;
29650 cp_token *sep;
29652 identifier = cp_parser_identifier (parser);
29653 if (identifier == error_mark_node)
29654 return error_mark_node;
29656 list = build_tree_list (NULL_TREE, identifier);
29657 sep = cp_lexer_peek_token (parser->lexer);
29659 while (sep->type == CPP_COMMA)
29661 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29662 identifier = cp_parser_identifier (parser);
29663 if (identifier == error_mark_node)
29664 return list;
29666 list = chainon (list, build_tree_list (NULL_TREE,
29667 identifier));
29668 sep = cp_lexer_peek_token (parser->lexer);
29671 return list;
29674 /* Parse an Objective-C alias declaration.
29676 objc-alias-declaration:
29677 @compatibility_alias identifier identifier ;
29679 This function registers the alias mapping with the Objective-C front end.
29680 It returns nothing. */
29682 static void
29683 cp_parser_objc_alias_declaration (cp_parser* parser)
29685 tree alias, orig;
29687 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29688 alias = cp_parser_identifier (parser);
29689 orig = cp_parser_identifier (parser);
29690 objc_declare_alias (alias, orig);
29691 cp_parser_consume_semicolon_at_end_of_statement (parser);
29694 /* Parse an Objective-C class forward-declaration.
29696 objc-class-declaration:
29697 @class objc-identifier-list ;
29699 The function registers the forward declarations with the Objective-C
29700 front end. It returns nothing. */
29702 static void
29703 cp_parser_objc_class_declaration (cp_parser* parser)
29705 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29706 while (true)
29708 tree id;
29710 id = cp_parser_identifier (parser);
29711 if (id == error_mark_node)
29712 break;
29714 objc_declare_class (id);
29716 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29717 cp_lexer_consume_token (parser->lexer);
29718 else
29719 break;
29721 cp_parser_consume_semicolon_at_end_of_statement (parser);
29724 /* Parse a list of Objective-C protocol references.
29726 objc-protocol-refs-opt:
29727 objc-protocol-refs [opt]
29729 objc-protocol-refs:
29730 < objc-identifier-list >
29732 Returns a TREE_LIST of identifiers, if any. */
29734 static tree
29735 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29737 tree protorefs = NULL_TREE;
29739 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29741 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29742 protorefs = cp_parser_objc_identifier_list (parser);
29743 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29746 return protorefs;
29749 /* Parse a Objective-C visibility specification. */
29751 static void
29752 cp_parser_objc_visibility_spec (cp_parser* parser)
29754 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29756 switch (vis->keyword)
29758 case RID_AT_PRIVATE:
29759 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29760 break;
29761 case RID_AT_PROTECTED:
29762 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29763 break;
29764 case RID_AT_PUBLIC:
29765 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29766 break;
29767 case RID_AT_PACKAGE:
29768 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29769 break;
29770 default:
29771 return;
29774 /* Eat '@private'/'@protected'/'@public'. */
29775 cp_lexer_consume_token (parser->lexer);
29778 /* Parse an Objective-C method type. Return 'true' if it is a class
29779 (+) method, and 'false' if it is an instance (-) method. */
29781 static inline bool
29782 cp_parser_objc_method_type (cp_parser* parser)
29784 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29785 return true;
29786 else
29787 return false;
29790 /* Parse an Objective-C protocol qualifier. */
29792 static tree
29793 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29795 tree quals = NULL_TREE, node;
29796 cp_token *token = cp_lexer_peek_token (parser->lexer);
29798 node = token->u.value;
29800 while (node && identifier_p (node)
29801 && (node == ridpointers [(int) RID_IN]
29802 || node == ridpointers [(int) RID_OUT]
29803 || node == ridpointers [(int) RID_INOUT]
29804 || node == ridpointers [(int) RID_BYCOPY]
29805 || node == ridpointers [(int) RID_BYREF]
29806 || node == ridpointers [(int) RID_ONEWAY]))
29808 quals = tree_cons (NULL_TREE, node, quals);
29809 cp_lexer_consume_token (parser->lexer);
29810 token = cp_lexer_peek_token (parser->lexer);
29811 node = token->u.value;
29814 return quals;
29817 /* Parse an Objective-C typename. */
29819 static tree
29820 cp_parser_objc_typename (cp_parser* parser)
29822 tree type_name = NULL_TREE;
29824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29826 tree proto_quals, cp_type = NULL_TREE;
29828 matching_parens parens;
29829 parens.consume_open (parser); /* Eat '('. */
29830 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29832 /* An ObjC type name may consist of just protocol qualifiers, in which
29833 case the type shall default to 'id'. */
29834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29836 cp_type = cp_parser_type_id (parser);
29838 /* If the type could not be parsed, an error has already
29839 been produced. For error recovery, behave as if it had
29840 not been specified, which will use the default type
29841 'id'. */
29842 if (cp_type == error_mark_node)
29844 cp_type = NULL_TREE;
29845 /* We need to skip to the closing parenthesis as
29846 cp_parser_type_id() does not seem to do it for
29847 us. */
29848 cp_parser_skip_to_closing_parenthesis (parser,
29849 /*recovering=*/true,
29850 /*or_comma=*/false,
29851 /*consume_paren=*/false);
29855 parens.require_close (parser);
29856 type_name = build_tree_list (proto_quals, cp_type);
29859 return type_name;
29862 /* Check to see if TYPE refers to an Objective-C selector name. */
29864 static bool
29865 cp_parser_objc_selector_p (enum cpp_ttype type)
29867 return (type == CPP_NAME || type == CPP_KEYWORD
29868 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29869 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29870 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29871 || type == CPP_XOR || type == CPP_XOR_EQ);
29874 /* Parse an Objective-C selector. */
29876 static tree
29877 cp_parser_objc_selector (cp_parser* parser)
29879 cp_token *token = cp_lexer_consume_token (parser->lexer);
29881 if (!cp_parser_objc_selector_p (token->type))
29883 error_at (token->location, "invalid Objective-C++ selector name");
29884 return error_mark_node;
29887 /* C++ operator names are allowed to appear in ObjC selectors. */
29888 switch (token->type)
29890 case CPP_AND_AND: return get_identifier ("and");
29891 case CPP_AND_EQ: return get_identifier ("and_eq");
29892 case CPP_AND: return get_identifier ("bitand");
29893 case CPP_OR: return get_identifier ("bitor");
29894 case CPP_COMPL: return get_identifier ("compl");
29895 case CPP_NOT: return get_identifier ("not");
29896 case CPP_NOT_EQ: return get_identifier ("not_eq");
29897 case CPP_OR_OR: return get_identifier ("or");
29898 case CPP_OR_EQ: return get_identifier ("or_eq");
29899 case CPP_XOR: return get_identifier ("xor");
29900 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29901 default: return token->u.value;
29905 /* Parse an Objective-C params list. */
29907 static tree
29908 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29910 tree params = NULL_TREE;
29911 bool maybe_unary_selector_p = true;
29912 cp_token *token = cp_lexer_peek_token (parser->lexer);
29914 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29916 tree selector = NULL_TREE, type_name, identifier;
29917 tree parm_attr = NULL_TREE;
29919 if (token->keyword == RID_ATTRIBUTE)
29920 break;
29922 if (token->type != CPP_COLON)
29923 selector = cp_parser_objc_selector (parser);
29925 /* Detect if we have a unary selector. */
29926 if (maybe_unary_selector_p
29927 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29929 params = selector; /* Might be followed by attributes. */
29930 break;
29933 maybe_unary_selector_p = false;
29934 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29936 /* Something went quite wrong. There should be a colon
29937 here, but there is not. Stop parsing parameters. */
29938 break;
29940 type_name = cp_parser_objc_typename (parser);
29941 /* New ObjC allows attributes on parameters too. */
29942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29943 parm_attr = cp_parser_attributes_opt (parser);
29944 identifier = cp_parser_identifier (parser);
29946 params
29947 = chainon (params,
29948 objc_build_keyword_decl (selector,
29949 type_name,
29950 identifier,
29951 parm_attr));
29953 token = cp_lexer_peek_token (parser->lexer);
29956 if (params == NULL_TREE)
29958 cp_parser_error (parser, "objective-c++ method declaration is expected");
29959 return error_mark_node;
29962 /* We allow tail attributes for the method. */
29963 if (token->keyword == RID_ATTRIBUTE)
29965 *attributes = cp_parser_attributes_opt (parser);
29966 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29967 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29968 return params;
29969 cp_parser_error (parser,
29970 "method attributes must be specified at the end");
29971 return error_mark_node;
29974 if (params == NULL_TREE)
29976 cp_parser_error (parser, "objective-c++ method declaration is expected");
29977 return error_mark_node;
29979 return params;
29982 /* Parse the non-keyword Objective-C params. */
29984 static tree
29985 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29986 tree* attributes)
29988 tree params = make_node (TREE_LIST);
29989 cp_token *token = cp_lexer_peek_token (parser->lexer);
29990 *ellipsisp = false; /* Initially, assume no ellipsis. */
29992 while (token->type == CPP_COMMA)
29994 cp_parameter_declarator *parmdecl;
29995 tree parm;
29997 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29998 token = cp_lexer_peek_token (parser->lexer);
30000 if (token->type == CPP_ELLIPSIS)
30002 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30003 *ellipsisp = true;
30004 token = cp_lexer_peek_token (parser->lexer);
30005 break;
30008 /* TODO: parse attributes for tail parameters. */
30009 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30010 parm = grokdeclarator (parmdecl->declarator,
30011 &parmdecl->decl_specifiers,
30012 PARM, /*initialized=*/0,
30013 /*attrlist=*/NULL);
30015 chainon (params, build_tree_list (NULL_TREE, parm));
30016 token = cp_lexer_peek_token (parser->lexer);
30019 /* We allow tail attributes for the method. */
30020 if (token->keyword == RID_ATTRIBUTE)
30022 if (*attributes == NULL_TREE)
30024 *attributes = cp_parser_attributes_opt (parser);
30025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30026 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30027 return params;
30029 else
30030 /* We have an error, but parse the attributes, so that we can
30031 carry on. */
30032 *attributes = cp_parser_attributes_opt (parser);
30034 cp_parser_error (parser,
30035 "method attributes must be specified at the end");
30036 return error_mark_node;
30039 return params;
30042 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30044 static void
30045 cp_parser_objc_interstitial_code (cp_parser* parser)
30047 cp_token *token = cp_lexer_peek_token (parser->lexer);
30049 /* If the next token is `extern' and the following token is a string
30050 literal, then we have a linkage specification. */
30051 if (token->keyword == RID_EXTERN
30052 && cp_parser_is_pure_string_literal
30053 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30054 cp_parser_linkage_specification (parser);
30055 /* Handle #pragma, if any. */
30056 else if (token->type == CPP_PRAGMA)
30057 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30058 /* Allow stray semicolons. */
30059 else if (token->type == CPP_SEMICOLON)
30060 cp_lexer_consume_token (parser->lexer);
30061 /* Mark methods as optional or required, when building protocols. */
30062 else if (token->keyword == RID_AT_OPTIONAL)
30064 cp_lexer_consume_token (parser->lexer);
30065 objc_set_method_opt (true);
30067 else if (token->keyword == RID_AT_REQUIRED)
30069 cp_lexer_consume_token (parser->lexer);
30070 objc_set_method_opt (false);
30072 else if (token->keyword == RID_NAMESPACE)
30073 cp_parser_namespace_definition (parser);
30074 /* Other stray characters must generate errors. */
30075 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30077 cp_lexer_consume_token (parser->lexer);
30078 error ("stray %qs between Objective-C++ methods",
30079 token->type == CPP_OPEN_BRACE ? "{" : "}");
30081 /* Finally, try to parse a block-declaration, or a function-definition. */
30082 else
30083 cp_parser_block_declaration (parser, /*statement_p=*/false);
30086 /* Parse a method signature. */
30088 static tree
30089 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30091 tree rettype, kwdparms, optparms;
30092 bool ellipsis = false;
30093 bool is_class_method;
30095 is_class_method = cp_parser_objc_method_type (parser);
30096 rettype = cp_parser_objc_typename (parser);
30097 *attributes = NULL_TREE;
30098 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30099 if (kwdparms == error_mark_node)
30100 return error_mark_node;
30101 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30102 if (optparms == error_mark_node)
30103 return error_mark_node;
30105 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30108 static bool
30109 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30111 tree tattr;
30112 cp_lexer_save_tokens (parser->lexer);
30113 tattr = cp_parser_attributes_opt (parser);
30114 gcc_assert (tattr) ;
30116 /* If the attributes are followed by a method introducer, this is not allowed.
30117 Dump the attributes and flag the situation. */
30118 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30119 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30120 return true;
30122 /* Otherwise, the attributes introduce some interstitial code, possibly so
30123 rewind to allow that check. */
30124 cp_lexer_rollback_tokens (parser->lexer);
30125 return false;
30128 /* Parse an Objective-C method prototype list. */
30130 static void
30131 cp_parser_objc_method_prototype_list (cp_parser* parser)
30133 cp_token *token = cp_lexer_peek_token (parser->lexer);
30135 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30137 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30139 tree attributes, sig;
30140 bool is_class_method;
30141 if (token->type == CPP_PLUS)
30142 is_class_method = true;
30143 else
30144 is_class_method = false;
30145 sig = cp_parser_objc_method_signature (parser, &attributes);
30146 if (sig == error_mark_node)
30148 cp_parser_skip_to_end_of_block_or_statement (parser);
30149 token = cp_lexer_peek_token (parser->lexer);
30150 continue;
30152 objc_add_method_declaration (is_class_method, sig, attributes);
30153 cp_parser_consume_semicolon_at_end_of_statement (parser);
30155 else if (token->keyword == RID_AT_PROPERTY)
30156 cp_parser_objc_at_property_declaration (parser);
30157 else if (token->keyword == RID_ATTRIBUTE
30158 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30159 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30160 OPT_Wattributes,
30161 "prefix attributes are ignored for methods");
30162 else
30163 /* Allow for interspersed non-ObjC++ code. */
30164 cp_parser_objc_interstitial_code (parser);
30166 token = cp_lexer_peek_token (parser->lexer);
30169 if (token->type != CPP_EOF)
30170 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30171 else
30172 cp_parser_error (parser, "expected %<@end%>");
30174 objc_finish_interface ();
30177 /* Parse an Objective-C method definition list. */
30179 static void
30180 cp_parser_objc_method_definition_list (cp_parser* parser)
30182 cp_token *token = cp_lexer_peek_token (parser->lexer);
30184 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30186 tree meth;
30188 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30190 cp_token *ptk;
30191 tree sig, attribute;
30192 bool is_class_method;
30193 if (token->type == CPP_PLUS)
30194 is_class_method = true;
30195 else
30196 is_class_method = false;
30197 push_deferring_access_checks (dk_deferred);
30198 sig = cp_parser_objc_method_signature (parser, &attribute);
30199 if (sig == error_mark_node)
30201 cp_parser_skip_to_end_of_block_or_statement (parser);
30202 token = cp_lexer_peek_token (parser->lexer);
30203 continue;
30205 objc_start_method_definition (is_class_method, sig, attribute,
30206 NULL_TREE);
30208 /* For historical reasons, we accept an optional semicolon. */
30209 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30210 cp_lexer_consume_token (parser->lexer);
30212 ptk = cp_lexer_peek_token (parser->lexer);
30213 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30214 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30216 perform_deferred_access_checks (tf_warning_or_error);
30217 stop_deferring_access_checks ();
30218 meth = cp_parser_function_definition_after_declarator (parser,
30219 false);
30220 pop_deferring_access_checks ();
30221 objc_finish_method_definition (meth);
30224 /* The following case will be removed once @synthesize is
30225 completely implemented. */
30226 else if (token->keyword == RID_AT_PROPERTY)
30227 cp_parser_objc_at_property_declaration (parser);
30228 else if (token->keyword == RID_AT_SYNTHESIZE)
30229 cp_parser_objc_at_synthesize_declaration (parser);
30230 else if (token->keyword == RID_AT_DYNAMIC)
30231 cp_parser_objc_at_dynamic_declaration (parser);
30232 else if (token->keyword == RID_ATTRIBUTE
30233 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30234 warning_at (token->location, OPT_Wattributes,
30235 "prefix attributes are ignored for methods");
30236 else
30237 /* Allow for interspersed non-ObjC++ code. */
30238 cp_parser_objc_interstitial_code (parser);
30240 token = cp_lexer_peek_token (parser->lexer);
30243 if (token->type != CPP_EOF)
30244 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30245 else
30246 cp_parser_error (parser, "expected %<@end%>");
30248 objc_finish_implementation ();
30251 /* Parse Objective-C ivars. */
30253 static void
30254 cp_parser_objc_class_ivars (cp_parser* parser)
30256 cp_token *token = cp_lexer_peek_token (parser->lexer);
30258 if (token->type != CPP_OPEN_BRACE)
30259 return; /* No ivars specified. */
30261 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30262 token = cp_lexer_peek_token (parser->lexer);
30264 while (token->type != CPP_CLOSE_BRACE
30265 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30267 cp_decl_specifier_seq declspecs;
30268 int decl_class_or_enum_p;
30269 tree prefix_attributes;
30271 cp_parser_objc_visibility_spec (parser);
30273 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30274 break;
30276 cp_parser_decl_specifier_seq (parser,
30277 CP_PARSER_FLAGS_OPTIONAL,
30278 &declspecs,
30279 &decl_class_or_enum_p);
30281 /* auto, register, static, extern, mutable. */
30282 if (declspecs.storage_class != sc_none)
30284 cp_parser_error (parser, "invalid type for instance variable");
30285 declspecs.storage_class = sc_none;
30288 /* thread_local. */
30289 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30291 cp_parser_error (parser, "invalid type for instance variable");
30292 declspecs.locations[ds_thread] = 0;
30295 /* typedef. */
30296 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30298 cp_parser_error (parser, "invalid type for instance variable");
30299 declspecs.locations[ds_typedef] = 0;
30302 prefix_attributes = declspecs.attributes;
30303 declspecs.attributes = NULL_TREE;
30305 /* Keep going until we hit the `;' at the end of the
30306 declaration. */
30307 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30309 tree width = NULL_TREE, attributes, first_attribute, decl;
30310 cp_declarator *declarator = NULL;
30311 int ctor_dtor_or_conv_p;
30313 /* Check for a (possibly unnamed) bitfield declaration. */
30314 token = cp_lexer_peek_token (parser->lexer);
30315 if (token->type == CPP_COLON)
30316 goto eat_colon;
30318 if (token->type == CPP_NAME
30319 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30320 == CPP_COLON))
30322 /* Get the name of the bitfield. */
30323 declarator = make_id_declarator (NULL_TREE,
30324 cp_parser_identifier (parser),
30325 sfk_none);
30327 eat_colon:
30328 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30329 /* Get the width of the bitfield. */
30330 width
30331 = cp_parser_constant_expression (parser);
30333 else
30335 /* Parse the declarator. */
30336 declarator
30337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30338 &ctor_dtor_or_conv_p,
30339 /*parenthesized_p=*/NULL,
30340 /*member_p=*/false,
30341 /*friend_p=*/false);
30344 /* Look for attributes that apply to the ivar. */
30345 attributes = cp_parser_attributes_opt (parser);
30346 /* Remember which attributes are prefix attributes and
30347 which are not. */
30348 first_attribute = attributes;
30349 /* Combine the attributes. */
30350 attributes = attr_chainon (prefix_attributes, attributes);
30352 if (width)
30353 /* Create the bitfield declaration. */
30354 decl = grokbitfield (declarator, &declspecs,
30355 width, NULL_TREE, attributes);
30356 else
30357 decl = grokfield (declarator, &declspecs,
30358 NULL_TREE, /*init_const_expr_p=*/false,
30359 NULL_TREE, attributes);
30361 /* Add the instance variable. */
30362 if (decl != error_mark_node && decl != NULL_TREE)
30363 objc_add_instance_variable (decl);
30365 /* Reset PREFIX_ATTRIBUTES. */
30366 if (attributes != error_mark_node)
30368 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30369 attributes = TREE_CHAIN (attributes);
30370 if (attributes)
30371 TREE_CHAIN (attributes) = NULL_TREE;
30374 token = cp_lexer_peek_token (parser->lexer);
30376 if (token->type == CPP_COMMA)
30378 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30379 continue;
30381 break;
30384 cp_parser_consume_semicolon_at_end_of_statement (parser);
30385 token = cp_lexer_peek_token (parser->lexer);
30388 if (token->keyword == RID_AT_END)
30389 cp_parser_error (parser, "expected %<}%>");
30391 /* Do not consume the RID_AT_END, so it will be read again as terminating
30392 the @interface of @implementation. */
30393 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30394 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30396 /* For historical reasons, we accept an optional semicolon. */
30397 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30398 cp_lexer_consume_token (parser->lexer);
30401 /* Parse an Objective-C protocol declaration. */
30403 static void
30404 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30406 tree proto, protorefs;
30407 cp_token *tok;
30409 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30412 tok = cp_lexer_peek_token (parser->lexer);
30413 error_at (tok->location, "identifier expected after %<@protocol%>");
30414 cp_parser_consume_semicolon_at_end_of_statement (parser);
30415 return;
30418 /* See if we have a forward declaration or a definition. */
30419 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30421 /* Try a forward declaration first. */
30422 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30424 while (true)
30426 tree id;
30428 id = cp_parser_identifier (parser);
30429 if (id == error_mark_node)
30430 break;
30432 objc_declare_protocol (id, attributes);
30434 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30435 cp_lexer_consume_token (parser->lexer);
30436 else
30437 break;
30439 cp_parser_consume_semicolon_at_end_of_statement (parser);
30442 /* Ok, we got a full-fledged definition (or at least should). */
30443 else
30445 proto = cp_parser_identifier (parser);
30446 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30447 objc_start_protocol (proto, protorefs, attributes);
30448 cp_parser_objc_method_prototype_list (parser);
30452 /* Parse an Objective-C superclass or category. */
30454 static void
30455 cp_parser_objc_superclass_or_category (cp_parser *parser,
30456 bool iface_p,
30457 tree *super,
30458 tree *categ, bool *is_class_extension)
30460 cp_token *next = cp_lexer_peek_token (parser->lexer);
30462 *super = *categ = NULL_TREE;
30463 *is_class_extension = false;
30464 if (next->type == CPP_COLON)
30466 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30467 *super = cp_parser_identifier (parser);
30469 else if (next->type == CPP_OPEN_PAREN)
30471 matching_parens parens;
30472 parens.consume_open (parser); /* Eat '('. */
30474 /* If there is no category name, and this is an @interface, we
30475 have a class extension. */
30476 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30478 *categ = NULL_TREE;
30479 *is_class_extension = true;
30481 else
30482 *categ = cp_parser_identifier (parser);
30484 parens.require_close (parser);
30488 /* Parse an Objective-C class interface. */
30490 static void
30491 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30493 tree name, super, categ, protos;
30494 bool is_class_extension;
30496 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30497 name = cp_parser_identifier (parser);
30498 if (name == error_mark_node)
30500 /* It's hard to recover because even if valid @interface stuff
30501 is to follow, we can't compile it (or validate it) if we
30502 don't even know which class it refers to. Let's assume this
30503 was a stray '@interface' token in the stream and skip it.
30505 return;
30507 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30508 &is_class_extension);
30509 protos = cp_parser_objc_protocol_refs_opt (parser);
30511 /* We have either a class or a category on our hands. */
30512 if (categ || is_class_extension)
30513 objc_start_category_interface (name, categ, protos, attributes);
30514 else
30516 objc_start_class_interface (name, super, protos, attributes);
30517 /* Handle instance variable declarations, if any. */
30518 cp_parser_objc_class_ivars (parser);
30519 objc_continue_interface ();
30522 cp_parser_objc_method_prototype_list (parser);
30525 /* Parse an Objective-C class implementation. */
30527 static void
30528 cp_parser_objc_class_implementation (cp_parser* parser)
30530 tree name, super, categ;
30531 bool is_class_extension;
30533 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30534 name = cp_parser_identifier (parser);
30535 if (name == error_mark_node)
30537 /* It's hard to recover because even if valid @implementation
30538 stuff is to follow, we can't compile it (or validate it) if
30539 we don't even know which class it refers to. Let's assume
30540 this was a stray '@implementation' token in the stream and
30541 skip it.
30543 return;
30545 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30546 &is_class_extension);
30548 /* We have either a class or a category on our hands. */
30549 if (categ)
30550 objc_start_category_implementation (name, categ);
30551 else
30553 objc_start_class_implementation (name, super);
30554 /* Handle instance variable declarations, if any. */
30555 cp_parser_objc_class_ivars (parser);
30556 objc_continue_implementation ();
30559 cp_parser_objc_method_definition_list (parser);
30562 /* Consume the @end token and finish off the implementation. */
30564 static void
30565 cp_parser_objc_end_implementation (cp_parser* parser)
30567 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30568 objc_finish_implementation ();
30571 /* Parse an Objective-C declaration. */
30573 static void
30574 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30576 /* Try to figure out what kind of declaration is present. */
30577 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30579 if (attributes)
30580 switch (kwd->keyword)
30582 case RID_AT_ALIAS:
30583 case RID_AT_CLASS:
30584 case RID_AT_END:
30585 error_at (kwd->location, "attributes may not be specified before"
30586 " the %<@%D%> Objective-C++ keyword",
30587 kwd->u.value);
30588 attributes = NULL;
30589 break;
30590 case RID_AT_IMPLEMENTATION:
30591 warning_at (kwd->location, OPT_Wattributes,
30592 "prefix attributes are ignored before %<@%D%>",
30593 kwd->u.value);
30594 attributes = NULL;
30595 default:
30596 break;
30599 switch (kwd->keyword)
30601 case RID_AT_ALIAS:
30602 cp_parser_objc_alias_declaration (parser);
30603 break;
30604 case RID_AT_CLASS:
30605 cp_parser_objc_class_declaration (parser);
30606 break;
30607 case RID_AT_PROTOCOL:
30608 cp_parser_objc_protocol_declaration (parser, attributes);
30609 break;
30610 case RID_AT_INTERFACE:
30611 cp_parser_objc_class_interface (parser, attributes);
30612 break;
30613 case RID_AT_IMPLEMENTATION:
30614 cp_parser_objc_class_implementation (parser);
30615 break;
30616 case RID_AT_END:
30617 cp_parser_objc_end_implementation (parser);
30618 break;
30619 default:
30620 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30621 kwd->u.value);
30622 cp_parser_skip_to_end_of_block_or_statement (parser);
30626 /* Parse an Objective-C try-catch-finally statement.
30628 objc-try-catch-finally-stmt:
30629 @try compound-statement objc-catch-clause-seq [opt]
30630 objc-finally-clause [opt]
30632 objc-catch-clause-seq:
30633 objc-catch-clause objc-catch-clause-seq [opt]
30635 objc-catch-clause:
30636 @catch ( objc-exception-declaration ) compound-statement
30638 objc-finally-clause:
30639 @finally compound-statement
30641 objc-exception-declaration:
30642 parameter-declaration
30643 '...'
30645 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30647 Returns NULL_TREE.
30649 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30650 for C. Keep them in sync. */
30652 static tree
30653 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30655 location_t location;
30656 tree stmt;
30658 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30659 location = cp_lexer_peek_token (parser->lexer)->location;
30660 objc_maybe_warn_exceptions (location);
30661 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30662 node, lest it get absorbed into the surrounding block. */
30663 stmt = push_stmt_list ();
30664 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30665 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30667 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30669 cp_parameter_declarator *parm;
30670 tree parameter_declaration = error_mark_node;
30671 bool seen_open_paren = false;
30672 matching_parens parens;
30674 cp_lexer_consume_token (parser->lexer);
30675 if (parens.require_open (parser))
30676 seen_open_paren = true;
30677 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30679 /* We have "@catch (...)" (where the '...' are literally
30680 what is in the code). Skip the '...'.
30681 parameter_declaration is set to NULL_TREE, and
30682 objc_being_catch_clauses() knows that that means
30683 '...'. */
30684 cp_lexer_consume_token (parser->lexer);
30685 parameter_declaration = NULL_TREE;
30687 else
30689 /* We have "@catch (NSException *exception)" or something
30690 like that. Parse the parameter declaration. */
30691 parm = cp_parser_parameter_declaration (parser, false, NULL);
30692 if (parm == NULL)
30693 parameter_declaration = error_mark_node;
30694 else
30695 parameter_declaration = grokdeclarator (parm->declarator,
30696 &parm->decl_specifiers,
30697 PARM, /*initialized=*/0,
30698 /*attrlist=*/NULL);
30700 if (seen_open_paren)
30701 parens.require_close (parser);
30702 else
30704 /* If there was no open parenthesis, we are recovering from
30705 an error, and we are trying to figure out what mistake
30706 the user has made. */
30708 /* If there is an immediate closing parenthesis, the user
30709 probably forgot the opening one (ie, they typed "@catch
30710 NSException *e)". Parse the closing parenthesis and keep
30711 going. */
30712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30713 cp_lexer_consume_token (parser->lexer);
30715 /* If these is no immediate closing parenthesis, the user
30716 probably doesn't know that parenthesis are required at
30717 all (ie, they typed "@catch NSException *e"). So, just
30718 forget about the closing parenthesis and keep going. */
30720 objc_begin_catch_clause (parameter_declaration);
30721 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30722 objc_finish_catch_clause ();
30724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30726 cp_lexer_consume_token (parser->lexer);
30727 location = cp_lexer_peek_token (parser->lexer)->location;
30728 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30729 node, lest it get absorbed into the surrounding block. */
30730 stmt = push_stmt_list ();
30731 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30732 objc_build_finally_clause (location, pop_stmt_list (stmt));
30735 return objc_finish_try_stmt ();
30738 /* Parse an Objective-C synchronized statement.
30740 objc-synchronized-stmt:
30741 @synchronized ( expression ) compound-statement
30743 Returns NULL_TREE. */
30745 static tree
30746 cp_parser_objc_synchronized_statement (cp_parser *parser)
30748 location_t location;
30749 tree lock, stmt;
30751 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30753 location = cp_lexer_peek_token (parser->lexer)->location;
30754 objc_maybe_warn_exceptions (location);
30755 matching_parens parens;
30756 parens.require_open (parser);
30757 lock = cp_parser_expression (parser);
30758 parens.require_close (parser);
30760 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30761 node, lest it get absorbed into the surrounding block. */
30762 stmt = push_stmt_list ();
30763 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30765 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30768 /* Parse an Objective-C throw statement.
30770 objc-throw-stmt:
30771 @throw assignment-expression [opt] ;
30773 Returns a constructed '@throw' statement. */
30775 static tree
30776 cp_parser_objc_throw_statement (cp_parser *parser)
30778 tree expr = NULL_TREE;
30779 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30781 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30784 expr = cp_parser_expression (parser);
30786 cp_parser_consume_semicolon_at_end_of_statement (parser);
30788 return objc_build_throw_stmt (loc, expr);
30791 /* Parse an Objective-C statement. */
30793 static tree
30794 cp_parser_objc_statement (cp_parser * parser)
30796 /* Try to figure out what kind of declaration is present. */
30797 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30799 switch (kwd->keyword)
30801 case RID_AT_TRY:
30802 return cp_parser_objc_try_catch_finally_statement (parser);
30803 case RID_AT_SYNCHRONIZED:
30804 return cp_parser_objc_synchronized_statement (parser);
30805 case RID_AT_THROW:
30806 return cp_parser_objc_throw_statement (parser);
30807 default:
30808 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30809 kwd->u.value);
30810 cp_parser_skip_to_end_of_block_or_statement (parser);
30813 return error_mark_node;
30816 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30817 look ahead to see if an objc keyword follows the attributes. This
30818 is to detect the use of prefix attributes on ObjC @interface and
30819 @protocol. */
30821 static bool
30822 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30824 cp_lexer_save_tokens (parser->lexer);
30825 *attrib = cp_parser_attributes_opt (parser);
30826 gcc_assert (*attrib);
30827 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30829 cp_lexer_commit_tokens (parser->lexer);
30830 return true;
30832 cp_lexer_rollback_tokens (parser->lexer);
30833 return false;
30836 /* This routine is a minimal replacement for
30837 c_parser_struct_declaration () used when parsing the list of
30838 types/names or ObjC++ properties. For example, when parsing the
30839 code
30841 @property (readonly) int a, b, c;
30843 this function is responsible for parsing "int a, int b, int c" and
30844 returning the declarations as CHAIN of DECLs.
30846 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30847 similar parsing. */
30848 static tree
30849 cp_parser_objc_struct_declaration (cp_parser *parser)
30851 tree decls = NULL_TREE;
30852 cp_decl_specifier_seq declspecs;
30853 int decl_class_or_enum_p;
30854 tree prefix_attributes;
30856 cp_parser_decl_specifier_seq (parser,
30857 CP_PARSER_FLAGS_NONE,
30858 &declspecs,
30859 &decl_class_or_enum_p);
30861 if (declspecs.type == error_mark_node)
30862 return error_mark_node;
30864 /* auto, register, static, extern, mutable. */
30865 if (declspecs.storage_class != sc_none)
30867 cp_parser_error (parser, "invalid type for property");
30868 declspecs.storage_class = sc_none;
30871 /* thread_local. */
30872 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30874 cp_parser_error (parser, "invalid type for property");
30875 declspecs.locations[ds_thread] = 0;
30878 /* typedef. */
30879 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30881 cp_parser_error (parser, "invalid type for property");
30882 declspecs.locations[ds_typedef] = 0;
30885 prefix_attributes = declspecs.attributes;
30886 declspecs.attributes = NULL_TREE;
30888 /* Keep going until we hit the `;' at the end of the declaration. */
30889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30891 tree attributes, first_attribute, decl;
30892 cp_declarator *declarator;
30893 cp_token *token;
30895 /* Parse the declarator. */
30896 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30897 NULL, NULL, false, false);
30899 /* Look for attributes that apply to the ivar. */
30900 attributes = cp_parser_attributes_opt (parser);
30901 /* Remember which attributes are prefix attributes and
30902 which are not. */
30903 first_attribute = attributes;
30904 /* Combine the attributes. */
30905 attributes = attr_chainon (prefix_attributes, attributes);
30907 decl = grokfield (declarator, &declspecs,
30908 NULL_TREE, /*init_const_expr_p=*/false,
30909 NULL_TREE, attributes);
30911 if (decl == error_mark_node || decl == NULL_TREE)
30912 return error_mark_node;
30914 /* Reset PREFIX_ATTRIBUTES. */
30915 if (attributes != error_mark_node)
30917 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30918 attributes = TREE_CHAIN (attributes);
30919 if (attributes)
30920 TREE_CHAIN (attributes) = NULL_TREE;
30923 DECL_CHAIN (decl) = decls;
30924 decls = decl;
30926 token = cp_lexer_peek_token (parser->lexer);
30927 if (token->type == CPP_COMMA)
30929 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30930 continue;
30932 else
30933 break;
30935 return decls;
30938 /* Parse an Objective-C @property declaration. The syntax is:
30940 objc-property-declaration:
30941 '@property' objc-property-attributes[opt] struct-declaration ;
30943 objc-property-attributes:
30944 '(' objc-property-attribute-list ')'
30946 objc-property-attribute-list:
30947 objc-property-attribute
30948 objc-property-attribute-list, objc-property-attribute
30950 objc-property-attribute
30951 'getter' = identifier
30952 'setter' = identifier
30953 'readonly'
30954 'readwrite'
30955 'assign'
30956 'retain'
30957 'copy'
30958 'nonatomic'
30960 For example:
30961 @property NSString *name;
30962 @property (readonly) id object;
30963 @property (retain, nonatomic, getter=getTheName) id name;
30964 @property int a, b, c;
30966 PS: This function is identical to
30967 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30968 static void
30969 cp_parser_objc_at_property_declaration (cp_parser *parser)
30971 /* The following variables hold the attributes of the properties as
30972 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30973 seen. When we see an attribute, we set them to 'true' (if they
30974 are boolean properties) or to the identifier (if they have an
30975 argument, ie, for getter and setter). Note that here we only
30976 parse the list of attributes, check the syntax and accumulate the
30977 attributes that we find. objc_add_property_declaration() will
30978 then process the information. */
30979 bool property_assign = false;
30980 bool property_copy = false;
30981 tree property_getter_ident = NULL_TREE;
30982 bool property_nonatomic = false;
30983 bool property_readonly = false;
30984 bool property_readwrite = false;
30985 bool property_retain = false;
30986 tree property_setter_ident = NULL_TREE;
30988 /* 'properties' is the list of properties that we read. Usually a
30989 single one, but maybe more (eg, in "@property int a, b, c;" there
30990 are three). */
30991 tree properties;
30992 location_t loc;
30994 loc = cp_lexer_peek_token (parser->lexer)->location;
30996 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30998 /* Parse the optional attribute list... */
30999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31001 /* Eat the '('. */
31002 matching_parens parens;
31003 parens.consume_open (parser);
31005 while (true)
31007 bool syntax_error = false;
31008 cp_token *token = cp_lexer_peek_token (parser->lexer);
31009 enum rid keyword;
31011 if (token->type != CPP_NAME)
31013 cp_parser_error (parser, "expected identifier");
31014 break;
31016 keyword = C_RID_CODE (token->u.value);
31017 cp_lexer_consume_token (parser->lexer);
31018 switch (keyword)
31020 case RID_ASSIGN: property_assign = true; break;
31021 case RID_COPY: property_copy = true; break;
31022 case RID_NONATOMIC: property_nonatomic = true; break;
31023 case RID_READONLY: property_readonly = true; break;
31024 case RID_READWRITE: property_readwrite = true; break;
31025 case RID_RETAIN: property_retain = true; break;
31027 case RID_GETTER:
31028 case RID_SETTER:
31029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31031 if (keyword == RID_GETTER)
31032 cp_parser_error (parser,
31033 "missing %<=%> (after %<getter%> attribute)");
31034 else
31035 cp_parser_error (parser,
31036 "missing %<=%> (after %<setter%> attribute)");
31037 syntax_error = true;
31038 break;
31040 cp_lexer_consume_token (parser->lexer); /* eat the = */
31041 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31043 cp_parser_error (parser, "expected identifier");
31044 syntax_error = true;
31045 break;
31047 if (keyword == RID_SETTER)
31049 if (property_setter_ident != NULL_TREE)
31051 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31052 cp_lexer_consume_token (parser->lexer);
31054 else
31055 property_setter_ident = cp_parser_objc_selector (parser);
31056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31057 cp_parser_error (parser, "setter name must terminate with %<:%>");
31058 else
31059 cp_lexer_consume_token (parser->lexer);
31061 else
31063 if (property_getter_ident != NULL_TREE)
31065 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31066 cp_lexer_consume_token (parser->lexer);
31068 else
31069 property_getter_ident = cp_parser_objc_selector (parser);
31071 break;
31072 default:
31073 cp_parser_error (parser, "unknown property attribute");
31074 syntax_error = true;
31075 break;
31078 if (syntax_error)
31079 break;
31081 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31082 cp_lexer_consume_token (parser->lexer);
31083 else
31084 break;
31087 /* FIXME: "@property (setter, assign);" will generate a spurious
31088 "error: expected ‘)’ before ‘,’ token". This is because
31089 cp_parser_require, unlike the C counterpart, will produce an
31090 error even if we are in error recovery. */
31091 if (!parens.require_close (parser))
31093 cp_parser_skip_to_closing_parenthesis (parser,
31094 /*recovering=*/true,
31095 /*or_comma=*/false,
31096 /*consume_paren=*/true);
31100 /* ... and the property declaration(s). */
31101 properties = cp_parser_objc_struct_declaration (parser);
31103 if (properties == error_mark_node)
31105 cp_parser_skip_to_end_of_statement (parser);
31106 /* If the next token is now a `;', consume it. */
31107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31108 cp_lexer_consume_token (parser->lexer);
31109 return;
31112 if (properties == NULL_TREE)
31113 cp_parser_error (parser, "expected identifier");
31114 else
31116 /* Comma-separated properties are chained together in
31117 reverse order; add them one by one. */
31118 properties = nreverse (properties);
31120 for (; properties; properties = TREE_CHAIN (properties))
31121 objc_add_property_declaration (loc, copy_node (properties),
31122 property_readonly, property_readwrite,
31123 property_assign, property_retain,
31124 property_copy, property_nonatomic,
31125 property_getter_ident, property_setter_ident);
31128 cp_parser_consume_semicolon_at_end_of_statement (parser);
31131 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31133 objc-synthesize-declaration:
31134 @synthesize objc-synthesize-identifier-list ;
31136 objc-synthesize-identifier-list:
31137 objc-synthesize-identifier
31138 objc-synthesize-identifier-list, objc-synthesize-identifier
31140 objc-synthesize-identifier
31141 identifier
31142 identifier = identifier
31144 For example:
31145 @synthesize MyProperty;
31146 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31148 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31149 for C. Keep them in sync.
31151 static void
31152 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31154 tree list = NULL_TREE;
31155 location_t loc;
31156 loc = cp_lexer_peek_token (parser->lexer)->location;
31158 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31159 while (true)
31161 tree property, ivar;
31162 property = cp_parser_identifier (parser);
31163 if (property == error_mark_node)
31165 cp_parser_consume_semicolon_at_end_of_statement (parser);
31166 return;
31168 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31170 cp_lexer_consume_token (parser->lexer);
31171 ivar = cp_parser_identifier (parser);
31172 if (ivar == error_mark_node)
31174 cp_parser_consume_semicolon_at_end_of_statement (parser);
31175 return;
31178 else
31179 ivar = NULL_TREE;
31180 list = chainon (list, build_tree_list (ivar, property));
31181 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31182 cp_lexer_consume_token (parser->lexer);
31183 else
31184 break;
31186 cp_parser_consume_semicolon_at_end_of_statement (parser);
31187 objc_add_synthesize_declaration (loc, list);
31190 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31192 objc-dynamic-declaration:
31193 @dynamic identifier-list ;
31195 For example:
31196 @dynamic MyProperty;
31197 @dynamic MyProperty, AnotherProperty;
31199 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31200 for C. Keep them in sync.
31202 static void
31203 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31205 tree list = NULL_TREE;
31206 location_t loc;
31207 loc = cp_lexer_peek_token (parser->lexer)->location;
31209 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31210 while (true)
31212 tree property;
31213 property = cp_parser_identifier (parser);
31214 if (property == error_mark_node)
31216 cp_parser_consume_semicolon_at_end_of_statement (parser);
31217 return;
31219 list = chainon (list, build_tree_list (NULL, property));
31220 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31221 cp_lexer_consume_token (parser->lexer);
31222 else
31223 break;
31225 cp_parser_consume_semicolon_at_end_of_statement (parser);
31226 objc_add_dynamic_declaration (loc, list);
31230 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31232 /* Returns name of the next clause.
31233 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31234 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31235 returned and the token is consumed. */
31237 static pragma_omp_clause
31238 cp_parser_omp_clause_name (cp_parser *parser)
31240 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31243 result = PRAGMA_OACC_CLAUSE_AUTO;
31244 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31245 result = PRAGMA_OMP_CLAUSE_IF;
31246 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31247 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31248 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31249 result = PRAGMA_OACC_CLAUSE_DELETE;
31250 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31251 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31252 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31253 result = PRAGMA_OMP_CLAUSE_FOR;
31254 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31256 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31257 const char *p = IDENTIFIER_POINTER (id);
31259 switch (p[0])
31261 case 'a':
31262 if (!strcmp ("aligned", p))
31263 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31264 else if (!strcmp ("async", p))
31265 result = PRAGMA_OACC_CLAUSE_ASYNC;
31266 break;
31267 case 'c':
31268 if (!strcmp ("collapse", p))
31269 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31270 else if (!strcmp ("copy", p))
31271 result = PRAGMA_OACC_CLAUSE_COPY;
31272 else if (!strcmp ("copyin", p))
31273 result = PRAGMA_OMP_CLAUSE_COPYIN;
31274 else if (!strcmp ("copyout", p))
31275 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31276 else if (!strcmp ("copyprivate", p))
31277 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31278 else if (!strcmp ("create", p))
31279 result = PRAGMA_OACC_CLAUSE_CREATE;
31280 break;
31281 case 'd':
31282 if (!strcmp ("defaultmap", p))
31283 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31284 else if (!strcmp ("depend", p))
31285 result = PRAGMA_OMP_CLAUSE_DEPEND;
31286 else if (!strcmp ("device", p))
31287 result = PRAGMA_OMP_CLAUSE_DEVICE;
31288 else if (!strcmp ("deviceptr", p))
31289 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31290 else if (!strcmp ("device_resident", p))
31291 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31292 else if (!strcmp ("dist_schedule", p))
31293 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31294 break;
31295 case 'f':
31296 if (!strcmp ("final", p))
31297 result = PRAGMA_OMP_CLAUSE_FINAL;
31298 else if (!strcmp ("firstprivate", p))
31299 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31300 else if (!strcmp ("from", p))
31301 result = PRAGMA_OMP_CLAUSE_FROM;
31302 break;
31303 case 'g':
31304 if (!strcmp ("gang", p))
31305 result = PRAGMA_OACC_CLAUSE_GANG;
31306 else if (!strcmp ("grainsize", p))
31307 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31308 break;
31309 case 'h':
31310 if (!strcmp ("hint", p))
31311 result = PRAGMA_OMP_CLAUSE_HINT;
31312 else if (!strcmp ("host", p))
31313 result = PRAGMA_OACC_CLAUSE_HOST;
31314 break;
31315 case 'i':
31316 if (!strcmp ("inbranch", p))
31317 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31318 else if (!strcmp ("independent", p))
31319 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31320 else if (!strcmp ("is_device_ptr", p))
31321 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31322 break;
31323 case 'l':
31324 if (!strcmp ("lastprivate", p))
31325 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31326 else if (!strcmp ("linear", p))
31327 result = PRAGMA_OMP_CLAUSE_LINEAR;
31328 else if (!strcmp ("link", p))
31329 result = PRAGMA_OMP_CLAUSE_LINK;
31330 break;
31331 case 'm':
31332 if (!strcmp ("map", p))
31333 result = PRAGMA_OMP_CLAUSE_MAP;
31334 else if (!strcmp ("mergeable", p))
31335 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31336 break;
31337 case 'n':
31338 if (!strcmp ("nogroup", p))
31339 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31340 else if (!strcmp ("notinbranch", p))
31341 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31342 else if (!strcmp ("nowait", p))
31343 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31344 else if (!strcmp ("num_gangs", p))
31345 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31346 else if (!strcmp ("num_tasks", p))
31347 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31348 else if (!strcmp ("num_teams", p))
31349 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31350 else if (!strcmp ("num_threads", p))
31351 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31352 else if (!strcmp ("num_workers", p))
31353 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31354 break;
31355 case 'o':
31356 if (!strcmp ("ordered", p))
31357 result = PRAGMA_OMP_CLAUSE_ORDERED;
31358 break;
31359 case 'p':
31360 if (!strcmp ("parallel", p))
31361 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31362 else if (!strcmp ("present", p))
31363 result = PRAGMA_OACC_CLAUSE_PRESENT;
31364 else if (!strcmp ("present_or_copy", p)
31365 || !strcmp ("pcopy", p))
31366 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31367 else if (!strcmp ("present_or_copyin", p)
31368 || !strcmp ("pcopyin", p))
31369 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31370 else if (!strcmp ("present_or_copyout", p)
31371 || !strcmp ("pcopyout", p))
31372 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31373 else if (!strcmp ("present_or_create", p)
31374 || !strcmp ("pcreate", p))
31375 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31376 else if (!strcmp ("priority", p))
31377 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31378 else if (!strcmp ("proc_bind", p))
31379 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31380 break;
31381 case 'r':
31382 if (!strcmp ("reduction", p))
31383 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31384 break;
31385 case 's':
31386 if (!strcmp ("safelen", p))
31387 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31388 else if (!strcmp ("schedule", p))
31389 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31390 else if (!strcmp ("sections", p))
31391 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31392 else if (!strcmp ("self", p))
31393 result = PRAGMA_OACC_CLAUSE_SELF;
31394 else if (!strcmp ("seq", p))
31395 result = PRAGMA_OACC_CLAUSE_SEQ;
31396 else if (!strcmp ("shared", p))
31397 result = PRAGMA_OMP_CLAUSE_SHARED;
31398 else if (!strcmp ("simd", p))
31399 result = PRAGMA_OMP_CLAUSE_SIMD;
31400 else if (!strcmp ("simdlen", p))
31401 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31402 break;
31403 case 't':
31404 if (!strcmp ("taskgroup", p))
31405 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31406 else if (!strcmp ("thread_limit", p))
31407 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31408 else if (!strcmp ("threads", p))
31409 result = PRAGMA_OMP_CLAUSE_THREADS;
31410 else if (!strcmp ("tile", p))
31411 result = PRAGMA_OACC_CLAUSE_TILE;
31412 else if (!strcmp ("to", p))
31413 result = PRAGMA_OMP_CLAUSE_TO;
31414 break;
31415 case 'u':
31416 if (!strcmp ("uniform", p))
31417 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31418 else if (!strcmp ("untied", p))
31419 result = PRAGMA_OMP_CLAUSE_UNTIED;
31420 else if (!strcmp ("use_device", p))
31421 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31422 else if (!strcmp ("use_device_ptr", p))
31423 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31424 break;
31425 case 'v':
31426 if (!strcmp ("vector", p))
31427 result = PRAGMA_OACC_CLAUSE_VECTOR;
31428 else if (!strcmp ("vector_length", p))
31429 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31430 break;
31431 case 'w':
31432 if (!strcmp ("wait", p))
31433 result = PRAGMA_OACC_CLAUSE_WAIT;
31434 else if (!strcmp ("worker", p))
31435 result = PRAGMA_OACC_CLAUSE_WORKER;
31436 break;
31440 if (result != PRAGMA_OMP_CLAUSE_NONE)
31441 cp_lexer_consume_token (parser->lexer);
31443 return result;
31446 /* Validate that a clause of the given type does not already exist. */
31448 static void
31449 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31450 const char *name, location_t location)
31452 tree c;
31454 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31455 if (OMP_CLAUSE_CODE (c) == code)
31457 error_at (location, "too many %qs clauses", name);
31458 break;
31462 /* OpenMP 2.5:
31463 variable-list:
31464 identifier
31465 variable-list , identifier
31467 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31468 colon). An opening parenthesis will have been consumed by the caller.
31470 If KIND is nonzero, create the appropriate node and install the decl
31471 in OMP_CLAUSE_DECL and add the node to the head of the list.
31473 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31474 return the list created.
31476 COLON can be NULL if only closing parenthesis should end the list,
31477 or pointer to bool which will receive false if the list is terminated
31478 by closing parenthesis or true if the list is terminated by colon. */
31480 static tree
31481 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31482 tree list, bool *colon)
31484 cp_token *token;
31485 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31486 if (colon)
31488 parser->colon_corrects_to_scope_p = false;
31489 *colon = false;
31491 while (1)
31493 tree name, decl;
31495 token = cp_lexer_peek_token (parser->lexer);
31496 if (kind != 0
31497 && current_class_ptr
31498 && cp_parser_is_keyword (token, RID_THIS))
31500 decl = finish_this_expr ();
31501 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31502 || CONVERT_EXPR_P (decl))
31503 decl = TREE_OPERAND (decl, 0);
31504 cp_lexer_consume_token (parser->lexer);
31506 else
31508 name = cp_parser_id_expression (parser, /*template_p=*/false,
31509 /*check_dependency_p=*/true,
31510 /*template_p=*/NULL,
31511 /*declarator_p=*/false,
31512 /*optional_p=*/false);
31513 if (name == error_mark_node)
31514 goto skip_comma;
31516 if (identifier_p (name))
31517 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31518 else
31519 decl = name;
31520 if (decl == error_mark_node)
31521 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31522 token->location);
31524 if (decl == error_mark_node)
31526 else if (kind != 0)
31528 switch (kind)
31530 case OMP_CLAUSE__CACHE_:
31531 /* The OpenACC cache directive explicitly only allows "array
31532 elements or subarrays". */
31533 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31535 error_at (token->location, "expected %<[%>");
31536 decl = error_mark_node;
31537 break;
31539 /* FALLTHROUGH. */
31540 case OMP_CLAUSE_MAP:
31541 case OMP_CLAUSE_FROM:
31542 case OMP_CLAUSE_TO:
31543 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31545 location_t loc
31546 = cp_lexer_peek_token (parser->lexer)->location;
31547 cp_id_kind idk = CP_ID_KIND_NONE;
31548 cp_lexer_consume_token (parser->lexer);
31549 decl = convert_from_reference (decl);
31550 decl
31551 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31552 decl, false,
31553 &idk, loc);
31555 /* FALLTHROUGH. */
31556 case OMP_CLAUSE_DEPEND:
31557 case OMP_CLAUSE_REDUCTION:
31558 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31560 tree low_bound = NULL_TREE, length = NULL_TREE;
31562 parser->colon_corrects_to_scope_p = false;
31563 cp_lexer_consume_token (parser->lexer);
31564 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31565 low_bound = cp_parser_expression (parser);
31566 if (!colon)
31567 parser->colon_corrects_to_scope_p
31568 = saved_colon_corrects_to_scope_p;
31569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31570 length = integer_one_node;
31571 else
31573 /* Look for `:'. */
31574 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31575 goto skip_comma;
31576 if (!cp_lexer_next_token_is (parser->lexer,
31577 CPP_CLOSE_SQUARE))
31578 length = cp_parser_expression (parser);
31580 /* Look for the closing `]'. */
31581 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31582 RT_CLOSE_SQUARE))
31583 goto skip_comma;
31585 decl = tree_cons (low_bound, length, decl);
31587 break;
31588 default:
31589 break;
31592 tree u = build_omp_clause (token->location, kind);
31593 OMP_CLAUSE_DECL (u) = decl;
31594 OMP_CLAUSE_CHAIN (u) = list;
31595 list = u;
31597 else
31598 list = tree_cons (decl, NULL_TREE, list);
31600 get_comma:
31601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31602 break;
31603 cp_lexer_consume_token (parser->lexer);
31606 if (colon)
31607 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31609 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31611 *colon = true;
31612 cp_parser_require (parser, CPP_COLON, RT_COLON);
31613 return list;
31616 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31618 int ending;
31620 /* Try to resync to an unnested comma. Copied from
31621 cp_parser_parenthesized_expression_list. */
31622 skip_comma:
31623 if (colon)
31624 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31625 ending = cp_parser_skip_to_closing_parenthesis (parser,
31626 /*recovering=*/true,
31627 /*or_comma=*/true,
31628 /*consume_paren=*/true);
31629 if (ending < 0)
31630 goto get_comma;
31633 return list;
31636 /* Similarly, but expect leading and trailing parenthesis. This is a very
31637 common case for omp clauses. */
31639 static tree
31640 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31642 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31643 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31644 return list;
31647 /* OpenACC 2.0:
31648 copy ( variable-list )
31649 copyin ( variable-list )
31650 copyout ( variable-list )
31651 create ( variable-list )
31652 delete ( variable-list )
31653 present ( variable-list )
31654 present_or_copy ( variable-list )
31655 pcopy ( variable-list )
31656 present_or_copyin ( variable-list )
31657 pcopyin ( variable-list )
31658 present_or_copyout ( variable-list )
31659 pcopyout ( variable-list )
31660 present_or_create ( variable-list )
31661 pcreate ( variable-list ) */
31663 static tree
31664 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31665 tree list)
31667 enum gomp_map_kind kind;
31668 switch (c_kind)
31670 case PRAGMA_OACC_CLAUSE_COPY:
31671 kind = GOMP_MAP_FORCE_TOFROM;
31672 break;
31673 case PRAGMA_OACC_CLAUSE_COPYIN:
31674 kind = GOMP_MAP_FORCE_TO;
31675 break;
31676 case PRAGMA_OACC_CLAUSE_COPYOUT:
31677 kind = GOMP_MAP_FORCE_FROM;
31678 break;
31679 case PRAGMA_OACC_CLAUSE_CREATE:
31680 kind = GOMP_MAP_FORCE_ALLOC;
31681 break;
31682 case PRAGMA_OACC_CLAUSE_DELETE:
31683 kind = GOMP_MAP_DELETE;
31684 break;
31685 case PRAGMA_OACC_CLAUSE_DEVICE:
31686 kind = GOMP_MAP_FORCE_TO;
31687 break;
31688 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31689 kind = GOMP_MAP_DEVICE_RESIDENT;
31690 break;
31691 case PRAGMA_OACC_CLAUSE_HOST:
31692 case PRAGMA_OACC_CLAUSE_SELF:
31693 kind = GOMP_MAP_FORCE_FROM;
31694 break;
31695 case PRAGMA_OACC_CLAUSE_LINK:
31696 kind = GOMP_MAP_LINK;
31697 break;
31698 case PRAGMA_OACC_CLAUSE_PRESENT:
31699 kind = GOMP_MAP_FORCE_PRESENT;
31700 break;
31701 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31702 kind = GOMP_MAP_TOFROM;
31703 break;
31704 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31705 kind = GOMP_MAP_TO;
31706 break;
31707 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31708 kind = GOMP_MAP_FROM;
31709 break;
31710 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31711 kind = GOMP_MAP_ALLOC;
31712 break;
31713 default:
31714 gcc_unreachable ();
31716 tree nl, c;
31717 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31719 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31720 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31722 return nl;
31725 /* OpenACC 2.0:
31726 deviceptr ( variable-list ) */
31728 static tree
31729 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31731 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31732 tree vars, t;
31734 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31735 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31736 variable-list must only allow for pointer variables. */
31737 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31738 for (t = vars; t; t = TREE_CHAIN (t))
31740 tree v = TREE_PURPOSE (t);
31741 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31742 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31743 OMP_CLAUSE_DECL (u) = v;
31744 OMP_CLAUSE_CHAIN (u) = list;
31745 list = u;
31748 return list;
31751 /* OpenACC 2.0:
31752 auto
31753 independent
31754 nohost
31755 seq */
31757 static tree
31758 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31759 enum omp_clause_code code,
31760 tree list, location_t location)
31762 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31763 tree c = build_omp_clause (location, code);
31764 OMP_CLAUSE_CHAIN (c) = list;
31765 return c;
31768 /* OpenACC:
31769 num_gangs ( expression )
31770 num_workers ( expression )
31771 vector_length ( expression ) */
31773 static tree
31774 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31775 const char *str, tree list)
31777 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31779 matching_parens parens;
31780 if (!parens.require_open (parser))
31781 return list;
31783 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31785 if (t == error_mark_node
31786 || !parens.require_close (parser))
31788 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31789 /*or_comma=*/false,
31790 /*consume_paren=*/true);
31791 return list;
31794 check_no_duplicate_clause (list, code, str, loc);
31796 tree c = build_omp_clause (loc, code);
31797 OMP_CLAUSE_OPERAND (c, 0) = t;
31798 OMP_CLAUSE_CHAIN (c) = list;
31799 return c;
31802 /* OpenACC:
31804 gang [( gang-arg-list )]
31805 worker [( [num:] int-expr )]
31806 vector [( [length:] int-expr )]
31808 where gang-arg is one of:
31810 [num:] int-expr
31811 static: size-expr
31813 and size-expr may be:
31816 int-expr
31819 static tree
31820 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31821 const char *str, tree list)
31823 const char *id = "num";
31824 cp_lexer *lexer = parser->lexer;
31825 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31826 location_t loc = cp_lexer_peek_token (lexer)->location;
31828 if (kind == OMP_CLAUSE_VECTOR)
31829 id = "length";
31831 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31833 matching_parens parens;
31834 parens.consume_open (parser);
31838 cp_token *next = cp_lexer_peek_token (lexer);
31839 int idx = 0;
31841 /* Gang static argument. */
31842 if (kind == OMP_CLAUSE_GANG
31843 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31845 cp_lexer_consume_token (lexer);
31847 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31848 goto cleanup_error;
31850 idx = 1;
31851 if (ops[idx] != NULL)
31853 cp_parser_error (parser, "too many %<static%> arguments");
31854 goto cleanup_error;
31857 /* Check for the '*' argument. */
31858 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31859 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31860 || cp_lexer_nth_token_is (parser->lexer, 2,
31861 CPP_CLOSE_PAREN)))
31863 cp_lexer_consume_token (lexer);
31864 ops[idx] = integer_minus_one_node;
31866 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31868 cp_lexer_consume_token (lexer);
31869 continue;
31871 else break;
31874 /* Worker num: argument and vector length: arguments. */
31875 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31876 && id_equal (next->u.value, id)
31877 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31879 cp_lexer_consume_token (lexer); /* id */
31880 cp_lexer_consume_token (lexer); /* ':' */
31883 /* Now collect the actual argument. */
31884 if (ops[idx] != NULL_TREE)
31886 cp_parser_error (parser, "unexpected argument");
31887 goto cleanup_error;
31890 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31891 false);
31892 if (expr == error_mark_node)
31893 goto cleanup_error;
31895 mark_exp_read (expr);
31896 ops[idx] = expr;
31898 if (kind == OMP_CLAUSE_GANG
31899 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31901 cp_lexer_consume_token (lexer);
31902 continue;
31904 break;
31906 while (1);
31908 if (!parens.require_close (parser))
31909 goto cleanup_error;
31912 check_no_duplicate_clause (list, kind, str, loc);
31914 c = build_omp_clause (loc, kind);
31916 if (ops[1])
31917 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31919 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31920 OMP_CLAUSE_CHAIN (c) = list;
31922 return c;
31924 cleanup_error:
31925 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31926 return list;
31929 /* OpenACC 2.0:
31930 tile ( size-expr-list ) */
31932 static tree
31933 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31935 tree c, expr = error_mark_node;
31936 tree tile = NULL_TREE;
31938 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31939 so, but the spec authors never considered such a case and have
31940 differing opinions on what it might mean, including 'not
31941 allowed'.) */
31942 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31943 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31944 clause_loc);
31946 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31947 return list;
31951 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31952 return list;
31954 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31955 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31956 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31958 cp_lexer_consume_token (parser->lexer);
31959 expr = integer_zero_node;
31961 else
31962 expr = cp_parser_constant_expression (parser);
31964 tile = tree_cons (NULL_TREE, expr, tile);
31966 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31968 /* Consume the trailing ')'. */
31969 cp_lexer_consume_token (parser->lexer);
31971 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31972 tile = nreverse (tile);
31973 OMP_CLAUSE_TILE_LIST (c) = tile;
31974 OMP_CLAUSE_CHAIN (c) = list;
31975 return c;
31978 /* OpenACC 2.0
31979 Parse wait clause or directive parameters. */
31981 static tree
31982 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31984 vec<tree, va_gc> *args;
31985 tree t, args_tree;
31987 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31988 /*cast_p=*/false,
31989 /*allow_expansion_p=*/true,
31990 /*non_constant_p=*/NULL);
31992 if (args == NULL || args->length () == 0)
31994 cp_parser_error (parser, "expected integer expression before ')'");
31995 if (args != NULL)
31996 release_tree_vector (args);
31997 return list;
32000 args_tree = build_tree_list_vec (args);
32002 release_tree_vector (args);
32004 for (t = args_tree; t; t = TREE_CHAIN (t))
32006 tree targ = TREE_VALUE (t);
32008 if (targ != error_mark_node)
32010 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32011 error ("%<wait%> expression must be integral");
32012 else
32014 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32016 targ = mark_rvalue_use (targ);
32017 OMP_CLAUSE_DECL (c) = targ;
32018 OMP_CLAUSE_CHAIN (c) = list;
32019 list = c;
32024 return list;
32027 /* OpenACC:
32028 wait ( int-expr-list ) */
32030 static tree
32031 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32033 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32035 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32036 return list;
32038 list = cp_parser_oacc_wait_list (parser, location, list);
32040 return list;
32043 /* OpenMP 3.0:
32044 collapse ( constant-expression ) */
32046 static tree
32047 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32049 tree c, num;
32050 location_t loc;
32051 HOST_WIDE_INT n;
32053 loc = cp_lexer_peek_token (parser->lexer)->location;
32054 matching_parens parens;
32055 if (!parens.require_open (parser))
32056 return list;
32058 num = cp_parser_constant_expression (parser);
32060 if (!parens.require_close (parser))
32061 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32062 /*or_comma=*/false,
32063 /*consume_paren=*/true);
32065 if (num == error_mark_node)
32066 return list;
32067 num = fold_non_dependent_expr (num);
32068 if (!tree_fits_shwi_p (num)
32069 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32070 || (n = tree_to_shwi (num)) <= 0
32071 || (int) n != n)
32073 error_at (loc, "collapse argument needs positive constant integer expression");
32074 return list;
32077 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32078 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32079 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32080 OMP_CLAUSE_CHAIN (c) = list;
32081 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32083 return c;
32086 /* OpenMP 2.5:
32087 default ( none | shared )
32089 OpenACC:
32090 default ( none | present ) */
32092 static tree
32093 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32094 location_t location, bool is_oacc)
32096 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32097 tree c;
32099 matching_parens parens;
32100 if (!parens.require_open (parser))
32101 return list;
32102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32105 const char *p = IDENTIFIER_POINTER (id);
32107 switch (p[0])
32109 case 'n':
32110 if (strcmp ("none", p) != 0)
32111 goto invalid_kind;
32112 kind = OMP_CLAUSE_DEFAULT_NONE;
32113 break;
32115 case 'p':
32116 if (strcmp ("present", p) != 0 || !is_oacc)
32117 goto invalid_kind;
32118 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32119 break;
32121 case 's':
32122 if (strcmp ("shared", p) != 0 || is_oacc)
32123 goto invalid_kind;
32124 kind = OMP_CLAUSE_DEFAULT_SHARED;
32125 break;
32127 default:
32128 goto invalid_kind;
32131 cp_lexer_consume_token (parser->lexer);
32133 else
32135 invalid_kind:
32136 if (is_oacc)
32137 cp_parser_error (parser, "expected %<none%> or %<present%>");
32138 else
32139 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32142 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32143 || !parens.require_close (parser))
32144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32145 /*or_comma=*/false,
32146 /*consume_paren=*/true);
32148 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32149 return list;
32151 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32152 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32153 OMP_CLAUSE_CHAIN (c) = list;
32154 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32156 return c;
32159 /* OpenMP 3.1:
32160 final ( expression ) */
32162 static tree
32163 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32165 tree t, c;
32167 matching_parens parens;
32168 if (!parens.require_open (parser))
32169 return list;
32171 t = cp_parser_condition (parser);
32173 if (t == error_mark_node
32174 || !parens.require_close (parser))
32175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32176 /*or_comma=*/false,
32177 /*consume_paren=*/true);
32179 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32181 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32182 OMP_CLAUSE_FINAL_EXPR (c) = t;
32183 OMP_CLAUSE_CHAIN (c) = list;
32185 return c;
32188 /* OpenMP 2.5:
32189 if ( expression )
32191 OpenMP 4.5:
32192 if ( directive-name-modifier : expression )
32194 directive-name-modifier:
32195 parallel | task | taskloop | target data | target | target update
32196 | target enter data | target exit data */
32198 static tree
32199 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32200 bool is_omp)
32202 tree t, c;
32203 enum tree_code if_modifier = ERROR_MARK;
32205 matching_parens parens;
32206 if (!parens.require_open (parser))
32207 return list;
32209 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32211 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32212 const char *p = IDENTIFIER_POINTER (id);
32213 int n = 2;
32215 if (strcmp ("parallel", p) == 0)
32216 if_modifier = OMP_PARALLEL;
32217 else if (strcmp ("task", p) == 0)
32218 if_modifier = OMP_TASK;
32219 else if (strcmp ("taskloop", p) == 0)
32220 if_modifier = OMP_TASKLOOP;
32221 else if (strcmp ("target", p) == 0)
32223 if_modifier = OMP_TARGET;
32224 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32226 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32227 p = IDENTIFIER_POINTER (id);
32228 if (strcmp ("data", p) == 0)
32229 if_modifier = OMP_TARGET_DATA;
32230 else if (strcmp ("update", p) == 0)
32231 if_modifier = OMP_TARGET_UPDATE;
32232 else if (strcmp ("enter", p) == 0)
32233 if_modifier = OMP_TARGET_ENTER_DATA;
32234 else if (strcmp ("exit", p) == 0)
32235 if_modifier = OMP_TARGET_EXIT_DATA;
32236 if (if_modifier != OMP_TARGET)
32237 n = 3;
32238 else
32240 location_t loc
32241 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32242 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32243 "or %<exit%>");
32244 if_modifier = ERROR_MARK;
32246 if (if_modifier == OMP_TARGET_ENTER_DATA
32247 || if_modifier == OMP_TARGET_EXIT_DATA)
32249 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32251 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32252 p = IDENTIFIER_POINTER (id);
32253 if (strcmp ("data", p) == 0)
32254 n = 4;
32256 if (n != 4)
32258 location_t loc
32259 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32260 error_at (loc, "expected %<data%>");
32261 if_modifier = ERROR_MARK;
32266 if (if_modifier != ERROR_MARK)
32268 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32270 while (n-- > 0)
32271 cp_lexer_consume_token (parser->lexer);
32273 else
32275 if (n > 2)
32277 location_t loc
32278 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32279 error_at (loc, "expected %<:%>");
32281 if_modifier = ERROR_MARK;
32286 t = cp_parser_condition (parser);
32288 if (t == error_mark_node
32289 || !parens.require_close (parser))
32290 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32291 /*or_comma=*/false,
32292 /*consume_paren=*/true);
32294 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32295 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32297 if (if_modifier != ERROR_MARK
32298 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32300 const char *p = NULL;
32301 switch (if_modifier)
32303 case OMP_PARALLEL: p = "parallel"; break;
32304 case OMP_TASK: p = "task"; break;
32305 case OMP_TASKLOOP: p = "taskloop"; break;
32306 case OMP_TARGET_DATA: p = "target data"; break;
32307 case OMP_TARGET: p = "target"; break;
32308 case OMP_TARGET_UPDATE: p = "target update"; break;
32309 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32310 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32311 default: gcc_unreachable ();
32313 error_at (location, "too many %<if%> clauses with %qs modifier",
32315 return list;
32317 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32319 if (!is_omp)
32320 error_at (location, "too many %<if%> clauses");
32321 else
32322 error_at (location, "too many %<if%> clauses without modifier");
32323 return list;
32325 else if (if_modifier == ERROR_MARK
32326 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32328 error_at (location, "if any %<if%> clause has modifier, then all "
32329 "%<if%> clauses have to use modifier");
32330 return list;
32334 c = build_omp_clause (location, OMP_CLAUSE_IF);
32335 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32336 OMP_CLAUSE_IF_EXPR (c) = t;
32337 OMP_CLAUSE_CHAIN (c) = list;
32339 return c;
32342 /* OpenMP 3.1:
32343 mergeable */
32345 static tree
32346 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32347 tree list, location_t location)
32349 tree c;
32351 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32352 location);
32354 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32355 OMP_CLAUSE_CHAIN (c) = list;
32356 return c;
32359 /* OpenMP 2.5:
32360 nowait */
32362 static tree
32363 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32364 tree list, location_t location)
32366 tree c;
32368 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32370 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32371 OMP_CLAUSE_CHAIN (c) = list;
32372 return c;
32375 /* OpenMP 2.5:
32376 num_threads ( expression ) */
32378 static tree
32379 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32380 location_t location)
32382 tree t, c;
32384 matching_parens parens;
32385 if (!parens.require_open (parser))
32386 return list;
32388 t = cp_parser_expression (parser);
32390 if (t == error_mark_node
32391 || !parens.require_close (parser))
32392 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32393 /*or_comma=*/false,
32394 /*consume_paren=*/true);
32396 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32397 "num_threads", location);
32399 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32400 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32401 OMP_CLAUSE_CHAIN (c) = list;
32403 return c;
32406 /* OpenMP 4.5:
32407 num_tasks ( expression ) */
32409 static tree
32410 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32411 location_t location)
32413 tree t, c;
32415 matching_parens parens;
32416 if (!parens.require_open (parser))
32417 return list;
32419 t = cp_parser_expression (parser);
32421 if (t == error_mark_node
32422 || !parens.require_close (parser))
32423 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32424 /*or_comma=*/false,
32425 /*consume_paren=*/true);
32427 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32428 "num_tasks", location);
32430 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32431 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32432 OMP_CLAUSE_CHAIN (c) = list;
32434 return c;
32437 /* OpenMP 4.5:
32438 grainsize ( expression ) */
32440 static tree
32441 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32442 location_t location)
32444 tree t, c;
32446 matching_parens parens;
32447 if (!parens.require_open (parser))
32448 return list;
32450 t = cp_parser_expression (parser);
32452 if (t == error_mark_node
32453 || !parens.require_close (parser))
32454 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32455 /*or_comma=*/false,
32456 /*consume_paren=*/true);
32458 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32459 "grainsize", location);
32461 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32462 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32463 OMP_CLAUSE_CHAIN (c) = list;
32465 return c;
32468 /* OpenMP 4.5:
32469 priority ( expression ) */
32471 static tree
32472 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32473 location_t location)
32475 tree t, c;
32477 matching_parens parens;
32478 if (!parens.require_open (parser))
32479 return list;
32481 t = cp_parser_expression (parser);
32483 if (t == error_mark_node
32484 || !parens.require_close (parser))
32485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32486 /*or_comma=*/false,
32487 /*consume_paren=*/true);
32489 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32490 "priority", location);
32492 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32493 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32494 OMP_CLAUSE_CHAIN (c) = list;
32496 return c;
32499 /* OpenMP 4.5:
32500 hint ( expression ) */
32502 static tree
32503 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32504 location_t location)
32506 tree t, c;
32508 matching_parens parens;
32509 if (!parens.require_open (parser))
32510 return list;
32512 t = cp_parser_expression (parser);
32514 if (t == error_mark_node
32515 || !parens.require_close (parser))
32516 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32517 /*or_comma=*/false,
32518 /*consume_paren=*/true);
32520 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32522 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32523 OMP_CLAUSE_HINT_EXPR (c) = t;
32524 OMP_CLAUSE_CHAIN (c) = list;
32526 return c;
32529 /* OpenMP 4.5:
32530 defaultmap ( tofrom : scalar ) */
32532 static tree
32533 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32534 location_t location)
32536 tree c, id;
32537 const char *p;
32539 matching_parens parens;
32540 if (!parens.require_open (parser))
32541 return list;
32543 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32545 cp_parser_error (parser, "expected %<tofrom%>");
32546 goto out_err;
32548 id = cp_lexer_peek_token (parser->lexer)->u.value;
32549 p = IDENTIFIER_POINTER (id);
32550 if (strcmp (p, "tofrom") != 0)
32552 cp_parser_error (parser, "expected %<tofrom%>");
32553 goto out_err;
32555 cp_lexer_consume_token (parser->lexer);
32556 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32557 goto out_err;
32559 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32561 cp_parser_error (parser, "expected %<scalar%>");
32562 goto out_err;
32564 id = cp_lexer_peek_token (parser->lexer)->u.value;
32565 p = IDENTIFIER_POINTER (id);
32566 if (strcmp (p, "scalar") != 0)
32568 cp_parser_error (parser, "expected %<scalar%>");
32569 goto out_err;
32571 cp_lexer_consume_token (parser->lexer);
32572 if (!parens.require_close (parser))
32573 goto out_err;
32575 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32576 location);
32578 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32579 OMP_CLAUSE_CHAIN (c) = list;
32580 return c;
32582 out_err:
32583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32584 /*or_comma=*/false,
32585 /*consume_paren=*/true);
32586 return list;
32589 /* OpenMP 2.5:
32590 ordered
32592 OpenMP 4.5:
32593 ordered ( constant-expression ) */
32595 static tree
32596 cp_parser_omp_clause_ordered (cp_parser *parser,
32597 tree list, location_t location)
32599 tree c, num = NULL_TREE;
32600 HOST_WIDE_INT n;
32602 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32603 "ordered", location);
32605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32607 matching_parens parens;
32608 parens.consume_open (parser);
32610 num = cp_parser_constant_expression (parser);
32612 if (!parens.require_close (parser))
32613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32614 /*or_comma=*/false,
32615 /*consume_paren=*/true);
32617 if (num == error_mark_node)
32618 return list;
32619 num = fold_non_dependent_expr (num);
32620 if (!tree_fits_shwi_p (num)
32621 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32622 || (n = tree_to_shwi (num)) <= 0
32623 || (int) n != n)
32625 error_at (location,
32626 "ordered argument needs positive constant integer "
32627 "expression");
32628 return list;
32632 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32633 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32634 OMP_CLAUSE_CHAIN (c) = list;
32635 return c;
32638 /* OpenMP 2.5:
32639 reduction ( reduction-operator : variable-list )
32641 reduction-operator:
32642 One of: + * - & ^ | && ||
32644 OpenMP 3.1:
32646 reduction-operator:
32647 One of: + * - & ^ | && || min max
32649 OpenMP 4.0:
32651 reduction-operator:
32652 One of: + * - & ^ | && ||
32653 id-expression */
32655 static tree
32656 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32658 enum tree_code code = ERROR_MARK;
32659 tree nlist, c, id = NULL_TREE;
32661 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32662 return list;
32664 switch (cp_lexer_peek_token (parser->lexer)->type)
32666 case CPP_PLUS: code = PLUS_EXPR; break;
32667 case CPP_MULT: code = MULT_EXPR; break;
32668 case CPP_MINUS: code = MINUS_EXPR; break;
32669 case CPP_AND: code = BIT_AND_EXPR; break;
32670 case CPP_XOR: code = BIT_XOR_EXPR; break;
32671 case CPP_OR: code = BIT_IOR_EXPR; break;
32672 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32673 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32674 default: break;
32677 if (code != ERROR_MARK)
32678 cp_lexer_consume_token (parser->lexer);
32679 else
32681 bool saved_colon_corrects_to_scope_p;
32682 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32683 parser->colon_corrects_to_scope_p = false;
32684 id = cp_parser_id_expression (parser, /*template_p=*/false,
32685 /*check_dependency_p=*/true,
32686 /*template_p=*/NULL,
32687 /*declarator_p=*/false,
32688 /*optional_p=*/false);
32689 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32690 if (identifier_p (id))
32692 const char *p = IDENTIFIER_POINTER (id);
32694 if (strcmp (p, "min") == 0)
32695 code = MIN_EXPR;
32696 else if (strcmp (p, "max") == 0)
32697 code = MAX_EXPR;
32698 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32699 code = PLUS_EXPR;
32700 else if (id == ovl_op_identifier (false, MULT_EXPR))
32701 code = MULT_EXPR;
32702 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32703 code = MINUS_EXPR;
32704 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32705 code = BIT_AND_EXPR;
32706 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32707 code = BIT_IOR_EXPR;
32708 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32709 code = BIT_XOR_EXPR;
32710 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32711 code = TRUTH_ANDIF_EXPR;
32712 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32713 code = TRUTH_ORIF_EXPR;
32714 id = omp_reduction_id (code, id, NULL_TREE);
32715 tree scope = parser->scope;
32716 if (scope)
32717 id = build_qualified_name (NULL_TREE, scope, id, false);
32718 parser->scope = NULL_TREE;
32719 parser->qualifying_scope = NULL_TREE;
32720 parser->object_scope = NULL_TREE;
32722 else
32724 error ("invalid reduction-identifier");
32725 resync_fail:
32726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32727 /*or_comma=*/false,
32728 /*consume_paren=*/true);
32729 return list;
32733 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32734 goto resync_fail;
32736 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32737 NULL);
32738 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32740 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32741 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32744 return nlist;
32747 /* OpenMP 2.5:
32748 schedule ( schedule-kind )
32749 schedule ( schedule-kind , expression )
32751 schedule-kind:
32752 static | dynamic | guided | runtime | auto
32754 OpenMP 4.5:
32755 schedule ( schedule-modifier : schedule-kind )
32756 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32758 schedule-modifier:
32759 simd
32760 monotonic
32761 nonmonotonic */
32763 static tree
32764 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32766 tree c, t;
32767 int modifiers = 0, nmodifiers = 0;
32769 matching_parens parens;
32770 if (!parens.require_open (parser))
32771 return list;
32773 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32775 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32777 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32778 const char *p = IDENTIFIER_POINTER (id);
32779 if (strcmp ("simd", p) == 0)
32780 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32781 else if (strcmp ("monotonic", p) == 0)
32782 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32783 else if (strcmp ("nonmonotonic", p) == 0)
32784 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32785 else
32786 break;
32787 cp_lexer_consume_token (parser->lexer);
32788 if (nmodifiers++ == 0
32789 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32790 cp_lexer_consume_token (parser->lexer);
32791 else
32793 cp_parser_require (parser, CPP_COLON, RT_COLON);
32794 break;
32798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32801 const char *p = IDENTIFIER_POINTER (id);
32803 switch (p[0])
32805 case 'd':
32806 if (strcmp ("dynamic", p) != 0)
32807 goto invalid_kind;
32808 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32809 break;
32811 case 'g':
32812 if (strcmp ("guided", p) != 0)
32813 goto invalid_kind;
32814 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32815 break;
32817 case 'r':
32818 if (strcmp ("runtime", p) != 0)
32819 goto invalid_kind;
32820 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32821 break;
32823 default:
32824 goto invalid_kind;
32827 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32828 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32829 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32830 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32831 else
32832 goto invalid_kind;
32833 cp_lexer_consume_token (parser->lexer);
32835 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32836 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32837 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32838 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32840 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32841 "specified");
32842 modifiers = 0;
32845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32847 cp_token *token;
32848 cp_lexer_consume_token (parser->lexer);
32850 token = cp_lexer_peek_token (parser->lexer);
32851 t = cp_parser_assignment_expression (parser);
32853 if (t == error_mark_node)
32854 goto resync_fail;
32855 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32856 error_at (token->location, "schedule %<runtime%> does not take "
32857 "a %<chunk_size%> parameter");
32858 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32859 error_at (token->location, "schedule %<auto%> does not take "
32860 "a %<chunk_size%> parameter");
32861 else
32862 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32864 if (!parens.require_close (parser))
32865 goto resync_fail;
32867 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32868 goto resync_fail;
32870 OMP_CLAUSE_SCHEDULE_KIND (c)
32871 = (enum omp_clause_schedule_kind)
32872 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32874 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32875 OMP_CLAUSE_CHAIN (c) = list;
32876 return c;
32878 invalid_kind:
32879 cp_parser_error (parser, "invalid schedule kind");
32880 resync_fail:
32881 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32882 /*or_comma=*/false,
32883 /*consume_paren=*/true);
32884 return list;
32887 /* OpenMP 3.0:
32888 untied */
32890 static tree
32891 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32892 tree list, location_t location)
32894 tree c;
32896 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32898 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32899 OMP_CLAUSE_CHAIN (c) = list;
32900 return c;
32903 /* OpenMP 4.0:
32904 inbranch
32905 notinbranch */
32907 static tree
32908 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32909 tree list, location_t location)
32911 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32912 tree c = build_omp_clause (location, code);
32913 OMP_CLAUSE_CHAIN (c) = list;
32914 return c;
32917 /* OpenMP 4.0:
32918 parallel
32920 sections
32921 taskgroup */
32923 static tree
32924 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32925 enum omp_clause_code code,
32926 tree list, location_t location)
32928 tree c = build_omp_clause (location, code);
32929 OMP_CLAUSE_CHAIN (c) = list;
32930 return c;
32933 /* OpenMP 4.5:
32934 nogroup */
32936 static tree
32937 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32938 tree list, location_t location)
32940 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32941 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32942 OMP_CLAUSE_CHAIN (c) = list;
32943 return c;
32946 /* OpenMP 4.5:
32947 simd
32948 threads */
32950 static tree
32951 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32952 enum omp_clause_code code,
32953 tree list, location_t location)
32955 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32956 tree c = build_omp_clause (location, code);
32957 OMP_CLAUSE_CHAIN (c) = list;
32958 return c;
32961 /* OpenMP 4.0:
32962 num_teams ( expression ) */
32964 static tree
32965 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32966 location_t location)
32968 tree t, c;
32970 matching_parens parens;
32971 if (!parens.require_open (parser))
32972 return list;
32974 t = cp_parser_expression (parser);
32976 if (t == error_mark_node
32977 || !parens.require_close (parser))
32978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32979 /*or_comma=*/false,
32980 /*consume_paren=*/true);
32982 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32983 "num_teams", location);
32985 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32986 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32987 OMP_CLAUSE_CHAIN (c) = list;
32989 return c;
32992 /* OpenMP 4.0:
32993 thread_limit ( expression ) */
32995 static tree
32996 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32997 location_t location)
32999 tree t, c;
33001 matching_parens parens;
33002 if (!parens.require_open (parser))
33003 return list;
33005 t = cp_parser_expression (parser);
33007 if (t == error_mark_node
33008 || !parens.require_close (parser))
33009 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33010 /*or_comma=*/false,
33011 /*consume_paren=*/true);
33013 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33014 "thread_limit", location);
33016 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33017 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33018 OMP_CLAUSE_CHAIN (c) = list;
33020 return c;
33023 /* OpenMP 4.0:
33024 aligned ( variable-list )
33025 aligned ( variable-list : constant-expression ) */
33027 static tree
33028 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33030 tree nlist, c, alignment = NULL_TREE;
33031 bool colon;
33033 matching_parens parens;
33034 if (!parens.require_open (parser))
33035 return list;
33037 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33038 &colon);
33040 if (colon)
33042 alignment = cp_parser_constant_expression (parser);
33044 if (!parens.require_close (parser))
33045 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33046 /*or_comma=*/false,
33047 /*consume_paren=*/true);
33049 if (alignment == error_mark_node)
33050 alignment = NULL_TREE;
33053 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33054 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33056 return nlist;
33059 /* OpenMP 4.0:
33060 linear ( variable-list )
33061 linear ( variable-list : expression )
33063 OpenMP 4.5:
33064 linear ( modifier ( variable-list ) )
33065 linear ( modifier ( variable-list ) : expression ) */
33067 static tree
33068 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33069 bool declare_simd)
33071 tree nlist, c, step = integer_one_node;
33072 bool colon;
33073 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33075 matching_parens parens;
33076 if (!parens.require_open (parser))
33077 return list;
33079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33081 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33082 const char *p = IDENTIFIER_POINTER (id);
33084 if (strcmp ("ref", p) == 0)
33085 kind = OMP_CLAUSE_LINEAR_REF;
33086 else if (strcmp ("val", p) == 0)
33087 kind = OMP_CLAUSE_LINEAR_VAL;
33088 else if (strcmp ("uval", p) == 0)
33089 kind = OMP_CLAUSE_LINEAR_UVAL;
33090 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33091 cp_lexer_consume_token (parser->lexer);
33092 else
33093 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33096 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33097 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33098 &colon);
33099 else
33101 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33102 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33103 if (colon)
33104 cp_parser_require (parser, CPP_COLON, RT_COLON);
33105 else if (!parens.require_close (parser))
33106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33107 /*or_comma=*/false,
33108 /*consume_paren=*/true);
33111 if (colon)
33113 step = NULL_TREE;
33114 if (declare_simd
33115 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33116 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33118 cp_token *token = cp_lexer_peek_token (parser->lexer);
33119 cp_parser_parse_tentatively (parser);
33120 step = cp_parser_id_expression (parser, /*template_p=*/false,
33121 /*check_dependency_p=*/true,
33122 /*template_p=*/NULL,
33123 /*declarator_p=*/false,
33124 /*optional_p=*/false);
33125 if (step != error_mark_node)
33126 step = cp_parser_lookup_name_simple (parser, step, token->location);
33127 if (step == error_mark_node)
33129 step = NULL_TREE;
33130 cp_parser_abort_tentative_parse (parser);
33132 else if (!cp_parser_parse_definitely (parser))
33133 step = NULL_TREE;
33135 if (!step)
33136 step = cp_parser_expression (parser);
33138 if (!parens.require_close (parser))
33139 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33140 /*or_comma=*/false,
33141 /*consume_paren=*/true);
33143 if (step == error_mark_node)
33144 return list;
33147 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33149 OMP_CLAUSE_LINEAR_STEP (c) = step;
33150 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33153 return nlist;
33156 /* OpenMP 4.0:
33157 safelen ( constant-expression ) */
33159 static tree
33160 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33161 location_t location)
33163 tree t, c;
33165 matching_parens parens;
33166 if (!parens.require_open (parser))
33167 return list;
33169 t = cp_parser_constant_expression (parser);
33171 if (t == error_mark_node
33172 || !parens.require_close (parser))
33173 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33174 /*or_comma=*/false,
33175 /*consume_paren=*/true);
33177 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33179 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33180 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33181 OMP_CLAUSE_CHAIN (c) = list;
33183 return c;
33186 /* OpenMP 4.0:
33187 simdlen ( constant-expression ) */
33189 static tree
33190 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33191 location_t location)
33193 tree t, c;
33195 matching_parens parens;
33196 if (!parens.require_open (parser))
33197 return list;
33199 t = cp_parser_constant_expression (parser);
33201 if (t == error_mark_node
33202 || !parens.require_close (parser))
33203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33204 /*or_comma=*/false,
33205 /*consume_paren=*/true);
33207 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33209 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33210 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33211 OMP_CLAUSE_CHAIN (c) = list;
33213 return c;
33216 /* OpenMP 4.5:
33217 vec:
33218 identifier [+/- integer]
33219 vec , identifier [+/- integer]
33222 static tree
33223 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33224 tree list)
33226 tree vec = NULL;
33228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33230 cp_parser_error (parser, "expected identifier");
33231 return list;
33234 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33236 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33237 tree t, identifier = cp_parser_identifier (parser);
33238 tree addend = NULL;
33240 if (identifier == error_mark_node)
33241 t = error_mark_node;
33242 else
33244 t = cp_parser_lookup_name_simple
33245 (parser, identifier,
33246 cp_lexer_peek_token (parser->lexer)->location);
33247 if (t == error_mark_node)
33248 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33249 id_loc);
33252 bool neg = false;
33253 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33254 neg = true;
33255 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33257 addend = integer_zero_node;
33258 goto add_to_vector;
33260 cp_lexer_consume_token (parser->lexer);
33262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33264 cp_parser_error (parser, "expected integer");
33265 return list;
33268 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33269 if (TREE_CODE (addend) != INTEGER_CST)
33271 cp_parser_error (parser, "expected integer");
33272 return list;
33274 cp_lexer_consume_token (parser->lexer);
33276 add_to_vector:
33277 if (t != error_mark_node)
33279 vec = tree_cons (addend, t, vec);
33280 if (neg)
33281 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33285 break;
33287 cp_lexer_consume_token (parser->lexer);
33290 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33292 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33293 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33294 OMP_CLAUSE_DECL (u) = nreverse (vec);
33295 OMP_CLAUSE_CHAIN (u) = list;
33296 return u;
33298 return list;
33301 /* OpenMP 4.0:
33302 depend ( depend-kind : variable-list )
33304 depend-kind:
33305 in | out | inout
33307 OpenMP 4.5:
33308 depend ( source )
33310 depend ( sink : vec ) */
33312 static tree
33313 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33315 tree nlist, c;
33316 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33318 matching_parens parens;
33319 if (!parens.require_open (parser))
33320 return list;
33322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33325 const char *p = IDENTIFIER_POINTER (id);
33327 if (strcmp ("in", p) == 0)
33328 kind = OMP_CLAUSE_DEPEND_IN;
33329 else if (strcmp ("inout", p) == 0)
33330 kind = OMP_CLAUSE_DEPEND_INOUT;
33331 else if (strcmp ("out", p) == 0)
33332 kind = OMP_CLAUSE_DEPEND_OUT;
33333 else if (strcmp ("source", p) == 0)
33334 kind = OMP_CLAUSE_DEPEND_SOURCE;
33335 else if (strcmp ("sink", p) == 0)
33336 kind = OMP_CLAUSE_DEPEND_SINK;
33337 else
33338 goto invalid_kind;
33340 else
33341 goto invalid_kind;
33343 cp_lexer_consume_token (parser->lexer);
33345 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33347 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33348 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33349 OMP_CLAUSE_DECL (c) = NULL_TREE;
33350 OMP_CLAUSE_CHAIN (c) = list;
33351 if (!parens.require_close (parser))
33352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33353 /*or_comma=*/false,
33354 /*consume_paren=*/true);
33355 return c;
33358 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33359 goto resync_fail;
33361 if (kind == OMP_CLAUSE_DEPEND_SINK)
33362 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33363 else
33365 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33366 list, NULL);
33368 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33369 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33371 return nlist;
33373 invalid_kind:
33374 cp_parser_error (parser, "invalid depend kind");
33375 resync_fail:
33376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33377 /*or_comma=*/false,
33378 /*consume_paren=*/true);
33379 return list;
33382 /* OpenMP 4.0:
33383 map ( map-kind : variable-list )
33384 map ( variable-list )
33386 map-kind:
33387 alloc | to | from | tofrom
33389 OpenMP 4.5:
33390 map-kind:
33391 alloc | to | from | tofrom | release | delete
33393 map ( always [,] map-kind: variable-list ) */
33395 static tree
33396 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33398 tree nlist, c;
33399 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33400 bool always = false;
33402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33403 return list;
33405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33407 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33408 const char *p = IDENTIFIER_POINTER (id);
33410 if (strcmp ("always", p) == 0)
33412 int nth = 2;
33413 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33414 nth++;
33415 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33416 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33417 == RID_DELETE))
33418 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33419 == CPP_COLON))
33421 always = true;
33422 cp_lexer_consume_token (parser->lexer);
33423 if (nth == 3)
33424 cp_lexer_consume_token (parser->lexer);
33429 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33430 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33432 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33433 const char *p = IDENTIFIER_POINTER (id);
33435 if (strcmp ("alloc", p) == 0)
33436 kind = GOMP_MAP_ALLOC;
33437 else if (strcmp ("to", p) == 0)
33438 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33439 else if (strcmp ("from", p) == 0)
33440 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33441 else if (strcmp ("tofrom", p) == 0)
33442 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33443 else if (strcmp ("release", p) == 0)
33444 kind = GOMP_MAP_RELEASE;
33445 else
33447 cp_parser_error (parser, "invalid map kind");
33448 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33449 /*or_comma=*/false,
33450 /*consume_paren=*/true);
33451 return list;
33453 cp_lexer_consume_token (parser->lexer);
33454 cp_lexer_consume_token (parser->lexer);
33456 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33457 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33459 kind = GOMP_MAP_DELETE;
33460 cp_lexer_consume_token (parser->lexer);
33461 cp_lexer_consume_token (parser->lexer);
33464 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33465 NULL);
33467 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33468 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33470 return nlist;
33473 /* OpenMP 4.0:
33474 device ( expression ) */
33476 static tree
33477 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33478 location_t location)
33480 tree t, c;
33482 matching_parens parens;
33483 if (!parens.require_open (parser))
33484 return list;
33486 t = cp_parser_expression (parser);
33488 if (t == error_mark_node
33489 || !parens.require_close (parser))
33490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33491 /*or_comma=*/false,
33492 /*consume_paren=*/true);
33494 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33495 "device", location);
33497 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33498 OMP_CLAUSE_DEVICE_ID (c) = t;
33499 OMP_CLAUSE_CHAIN (c) = list;
33501 return c;
33504 /* OpenMP 4.0:
33505 dist_schedule ( static )
33506 dist_schedule ( static , expression ) */
33508 static tree
33509 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33510 location_t location)
33512 tree c, t;
33514 matching_parens parens;
33515 if (!parens.require_open (parser))
33516 return list;
33518 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33520 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33521 goto invalid_kind;
33522 cp_lexer_consume_token (parser->lexer);
33524 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33526 cp_lexer_consume_token (parser->lexer);
33528 t = cp_parser_assignment_expression (parser);
33530 if (t == error_mark_node)
33531 goto resync_fail;
33532 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33534 if (!parens.require_close (parser))
33535 goto resync_fail;
33537 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33538 goto resync_fail;
33540 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33541 location);
33542 OMP_CLAUSE_CHAIN (c) = list;
33543 return c;
33545 invalid_kind:
33546 cp_parser_error (parser, "invalid dist_schedule kind");
33547 resync_fail:
33548 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33549 /*or_comma=*/false,
33550 /*consume_paren=*/true);
33551 return list;
33554 /* OpenMP 4.0:
33555 proc_bind ( proc-bind-kind )
33557 proc-bind-kind:
33558 master | close | spread */
33560 static tree
33561 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33562 location_t location)
33564 tree c;
33565 enum omp_clause_proc_bind_kind kind;
33567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33568 return list;
33570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33573 const char *p = IDENTIFIER_POINTER (id);
33575 if (strcmp ("master", p) == 0)
33576 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33577 else if (strcmp ("close", p) == 0)
33578 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33579 else if (strcmp ("spread", p) == 0)
33580 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33581 else
33582 goto invalid_kind;
33584 else
33585 goto invalid_kind;
33587 cp_lexer_consume_token (parser->lexer);
33588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33589 goto resync_fail;
33591 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33592 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33593 location);
33594 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33595 OMP_CLAUSE_CHAIN (c) = list;
33596 return c;
33598 invalid_kind:
33599 cp_parser_error (parser, "invalid depend kind");
33600 resync_fail:
33601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33602 /*or_comma=*/false,
33603 /*consume_paren=*/true);
33604 return list;
33607 /* OpenACC:
33608 async [( int-expr )] */
33610 static tree
33611 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33613 tree c, t;
33614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33616 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33618 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33620 matching_parens parens;
33621 parens.consume_open (parser);
33623 t = cp_parser_expression (parser);
33624 if (t == error_mark_node
33625 || !parens.require_close (parser))
33626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33627 /*or_comma=*/false,
33628 /*consume_paren=*/true);
33631 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33633 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33634 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33635 OMP_CLAUSE_CHAIN (c) = list;
33636 list = c;
33638 return list;
33641 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33642 is a bitmask in MASK. Return the list of clauses found. */
33644 static tree
33645 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33646 const char *where, cp_token *pragma_tok,
33647 bool finish_p = true)
33649 tree clauses = NULL;
33650 bool first = true;
33652 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33654 location_t here;
33655 pragma_omp_clause c_kind;
33656 omp_clause_code code;
33657 const char *c_name;
33658 tree prev = clauses;
33660 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33661 cp_lexer_consume_token (parser->lexer);
33663 here = cp_lexer_peek_token (parser->lexer)->location;
33664 c_kind = cp_parser_omp_clause_name (parser);
33666 switch (c_kind)
33668 case PRAGMA_OACC_CLAUSE_ASYNC:
33669 clauses = cp_parser_oacc_clause_async (parser, clauses);
33670 c_name = "async";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_AUTO:
33673 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33674 clauses, here);
33675 c_name = "auto";
33676 break;
33677 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33678 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33679 c_name = "collapse";
33680 break;
33681 case PRAGMA_OACC_CLAUSE_COPY:
33682 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33683 c_name = "copy";
33684 break;
33685 case PRAGMA_OACC_CLAUSE_COPYIN:
33686 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33687 c_name = "copyin";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_COPYOUT:
33690 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33691 c_name = "copyout";
33692 break;
33693 case PRAGMA_OACC_CLAUSE_CREATE:
33694 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33695 c_name = "create";
33696 break;
33697 case PRAGMA_OACC_CLAUSE_DELETE:
33698 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33699 c_name = "delete";
33700 break;
33701 case PRAGMA_OMP_CLAUSE_DEFAULT:
33702 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33703 c_name = "default";
33704 break;
33705 case PRAGMA_OACC_CLAUSE_DEVICE:
33706 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33707 c_name = "device";
33708 break;
33709 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33710 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33711 c_name = "deviceptr";
33712 break;
33713 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33714 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33715 c_name = "device_resident";
33716 break;
33717 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33718 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33719 clauses);
33720 c_name = "firstprivate";
33721 break;
33722 case PRAGMA_OACC_CLAUSE_GANG:
33723 c_name = "gang";
33724 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33725 c_name, clauses);
33726 break;
33727 case PRAGMA_OACC_CLAUSE_HOST:
33728 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33729 c_name = "host";
33730 break;
33731 case PRAGMA_OACC_CLAUSE_IF:
33732 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33733 c_name = "if";
33734 break;
33735 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33736 clauses = cp_parser_oacc_simple_clause (parser,
33737 OMP_CLAUSE_INDEPENDENT,
33738 clauses, here);
33739 c_name = "independent";
33740 break;
33741 case PRAGMA_OACC_CLAUSE_LINK:
33742 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33743 c_name = "link";
33744 break;
33745 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33746 code = OMP_CLAUSE_NUM_GANGS;
33747 c_name = "num_gangs";
33748 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33749 clauses);
33750 break;
33751 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33752 c_name = "num_workers";
33753 code = OMP_CLAUSE_NUM_WORKERS;
33754 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33755 clauses);
33756 break;
33757 case PRAGMA_OACC_CLAUSE_PRESENT:
33758 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33759 c_name = "present";
33760 break;
33761 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33762 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33763 c_name = "present_or_copy";
33764 break;
33765 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33766 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33767 c_name = "present_or_copyin";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33770 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33771 c_name = "present_or_copyout";
33772 break;
33773 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33774 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33775 c_name = "present_or_create";
33776 break;
33777 case PRAGMA_OACC_CLAUSE_PRIVATE:
33778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33779 clauses);
33780 c_name = "private";
33781 break;
33782 case PRAGMA_OACC_CLAUSE_REDUCTION:
33783 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33784 c_name = "reduction";
33785 break;
33786 case PRAGMA_OACC_CLAUSE_SELF:
33787 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788 c_name = "self";
33789 break;
33790 case PRAGMA_OACC_CLAUSE_SEQ:
33791 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33792 clauses, here);
33793 c_name = "seq";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_TILE:
33796 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33797 c_name = "tile";
33798 break;
33799 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33800 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33801 clauses);
33802 c_name = "use_device";
33803 break;
33804 case PRAGMA_OACC_CLAUSE_VECTOR:
33805 c_name = "vector";
33806 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33807 c_name, clauses);
33808 break;
33809 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33810 c_name = "vector_length";
33811 code = OMP_CLAUSE_VECTOR_LENGTH;
33812 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33813 clauses);
33814 break;
33815 case PRAGMA_OACC_CLAUSE_WAIT:
33816 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33817 c_name = "wait";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_WORKER:
33820 c_name = "worker";
33821 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33822 c_name, clauses);
33823 break;
33824 default:
33825 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33826 goto saw_error;
33829 first = false;
33831 if (((mask >> c_kind) & 1) == 0)
33833 /* Remove the invalid clause(s) from the list to avoid
33834 confusing the rest of the compiler. */
33835 clauses = prev;
33836 error_at (here, "%qs is not valid for %qs", c_name, where);
33840 saw_error:
33841 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33843 if (finish_p)
33844 return finish_omp_clauses (clauses, C_ORT_ACC);
33846 return clauses;
33849 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33850 is a bitmask in MASK. Return the list of clauses found; the result
33851 of clause default goes in *pdefault. */
33853 static tree
33854 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33855 const char *where, cp_token *pragma_tok,
33856 bool finish_p = true)
33858 tree clauses = NULL;
33859 bool first = true;
33860 cp_token *token = NULL;
33862 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33864 pragma_omp_clause c_kind;
33865 const char *c_name;
33866 tree prev = clauses;
33868 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33869 cp_lexer_consume_token (parser->lexer);
33871 token = cp_lexer_peek_token (parser->lexer);
33872 c_kind = cp_parser_omp_clause_name (parser);
33874 switch (c_kind)
33876 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33877 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33878 token->location);
33879 c_name = "collapse";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_COPYIN:
33882 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33883 c_name = "copyin";
33884 break;
33885 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33886 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33887 clauses);
33888 c_name = "copyprivate";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_DEFAULT:
33891 clauses = cp_parser_omp_clause_default (parser, clauses,
33892 token->location, false);
33893 c_name = "default";
33894 break;
33895 case PRAGMA_OMP_CLAUSE_FINAL:
33896 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33897 c_name = "final";
33898 break;
33899 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33900 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33901 clauses);
33902 c_name = "firstprivate";
33903 break;
33904 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33905 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33906 token->location);
33907 c_name = "grainsize";
33908 break;
33909 case PRAGMA_OMP_CLAUSE_HINT:
33910 clauses = cp_parser_omp_clause_hint (parser, clauses,
33911 token->location);
33912 c_name = "hint";
33913 break;
33914 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33915 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33916 token->location);
33917 c_name = "defaultmap";
33918 break;
33919 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33920 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33921 clauses);
33922 c_name = "use_device_ptr";
33923 break;
33924 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33925 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33926 clauses);
33927 c_name = "is_device_ptr";
33928 break;
33929 case PRAGMA_OMP_CLAUSE_IF:
33930 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33931 true);
33932 c_name = "if";
33933 break;
33934 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33935 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33936 clauses);
33937 c_name = "lastprivate";
33938 break;
33939 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33940 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33941 token->location);
33942 c_name = "mergeable";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_NOWAIT:
33945 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33946 c_name = "nowait";
33947 break;
33948 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33949 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33950 token->location);
33951 c_name = "num_tasks";
33952 break;
33953 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33954 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33955 token->location);
33956 c_name = "num_threads";
33957 break;
33958 case PRAGMA_OMP_CLAUSE_ORDERED:
33959 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33960 token->location);
33961 c_name = "ordered";
33962 break;
33963 case PRAGMA_OMP_CLAUSE_PRIORITY:
33964 clauses = cp_parser_omp_clause_priority (parser, clauses,
33965 token->location);
33966 c_name = "priority";
33967 break;
33968 case PRAGMA_OMP_CLAUSE_PRIVATE:
33969 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33970 clauses);
33971 c_name = "private";
33972 break;
33973 case PRAGMA_OMP_CLAUSE_REDUCTION:
33974 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33975 c_name = "reduction";
33976 break;
33977 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33978 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33979 token->location);
33980 c_name = "schedule";
33981 break;
33982 case PRAGMA_OMP_CLAUSE_SHARED:
33983 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33984 clauses);
33985 c_name = "shared";
33986 break;
33987 case PRAGMA_OMP_CLAUSE_UNTIED:
33988 clauses = cp_parser_omp_clause_untied (parser, clauses,
33989 token->location);
33990 c_name = "untied";
33991 break;
33992 case PRAGMA_OMP_CLAUSE_INBRANCH:
33993 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33994 clauses, token->location);
33995 c_name = "inbranch";
33996 break;
33997 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33998 clauses = cp_parser_omp_clause_branch (parser,
33999 OMP_CLAUSE_NOTINBRANCH,
34000 clauses, token->location);
34001 c_name = "notinbranch";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_PARALLEL:
34004 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34005 clauses, token->location);
34006 c_name = "parallel";
34007 if (!first)
34009 clause_not_first:
34010 error_at (token->location, "%qs must be the first clause of %qs",
34011 c_name, where);
34012 clauses = prev;
34014 break;
34015 case PRAGMA_OMP_CLAUSE_FOR:
34016 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34017 clauses, token->location);
34018 c_name = "for";
34019 if (!first)
34020 goto clause_not_first;
34021 break;
34022 case PRAGMA_OMP_CLAUSE_SECTIONS:
34023 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34024 clauses, token->location);
34025 c_name = "sections";
34026 if (!first)
34027 goto clause_not_first;
34028 break;
34029 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34030 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34031 clauses, token->location);
34032 c_name = "taskgroup";
34033 if (!first)
34034 goto clause_not_first;
34035 break;
34036 case PRAGMA_OMP_CLAUSE_LINK:
34037 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34038 c_name = "to";
34039 break;
34040 case PRAGMA_OMP_CLAUSE_TO:
34041 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34042 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34043 clauses);
34044 else
34045 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34046 c_name = "to";
34047 break;
34048 case PRAGMA_OMP_CLAUSE_FROM:
34049 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34050 c_name = "from";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_UNIFORM:
34053 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34054 clauses);
34055 c_name = "uniform";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34058 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34059 token->location);
34060 c_name = "num_teams";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34063 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34064 token->location);
34065 c_name = "thread_limit";
34066 break;
34067 case PRAGMA_OMP_CLAUSE_ALIGNED:
34068 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34069 c_name = "aligned";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_LINEAR:
34073 bool declare_simd = false;
34074 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34075 declare_simd = true;
34076 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34078 c_name = "linear";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_DEPEND:
34081 clauses = cp_parser_omp_clause_depend (parser, clauses,
34082 token->location);
34083 c_name = "depend";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_MAP:
34086 clauses = cp_parser_omp_clause_map (parser, clauses);
34087 c_name = "map";
34088 break;
34089 case PRAGMA_OMP_CLAUSE_DEVICE:
34090 clauses = cp_parser_omp_clause_device (parser, clauses,
34091 token->location);
34092 c_name = "device";
34093 break;
34094 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34095 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34096 token->location);
34097 c_name = "dist_schedule";
34098 break;
34099 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34100 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34101 token->location);
34102 c_name = "proc_bind";
34103 break;
34104 case PRAGMA_OMP_CLAUSE_SAFELEN:
34105 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34106 token->location);
34107 c_name = "safelen";
34108 break;
34109 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34110 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34111 token->location);
34112 c_name = "simdlen";
34113 break;
34114 case PRAGMA_OMP_CLAUSE_NOGROUP:
34115 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34116 token->location);
34117 c_name = "nogroup";
34118 break;
34119 case PRAGMA_OMP_CLAUSE_THREADS:
34120 clauses
34121 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34122 clauses, token->location);
34123 c_name = "threads";
34124 break;
34125 case PRAGMA_OMP_CLAUSE_SIMD:
34126 clauses
34127 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34128 clauses, token->location);
34129 c_name = "simd";
34130 break;
34131 default:
34132 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34133 goto saw_error;
34136 first = false;
34138 if (((mask >> c_kind) & 1) == 0)
34140 /* Remove the invalid clause(s) from the list to avoid
34141 confusing the rest of the compiler. */
34142 clauses = prev;
34143 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34146 saw_error:
34147 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34148 if (finish_p)
34150 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34151 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34152 else
34153 return finish_omp_clauses (clauses, C_ORT_OMP);
34155 return clauses;
34158 /* OpenMP 2.5:
34159 structured-block:
34160 statement
34162 In practice, we're also interested in adding the statement to an
34163 outer node. So it is convenient if we work around the fact that
34164 cp_parser_statement calls add_stmt. */
34166 static unsigned
34167 cp_parser_begin_omp_structured_block (cp_parser *parser)
34169 unsigned save = parser->in_statement;
34171 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34172 This preserves the "not within loop or switch" style error messages
34173 for nonsense cases like
34174 void foo() {
34175 #pragma omp single
34176 break;
34179 if (parser->in_statement)
34180 parser->in_statement = IN_OMP_BLOCK;
34182 return save;
34185 static void
34186 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34188 parser->in_statement = save;
34191 static tree
34192 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34194 tree stmt = begin_omp_structured_block ();
34195 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34197 cp_parser_statement (parser, NULL_TREE, false, if_p);
34199 cp_parser_end_omp_structured_block (parser, save);
34200 return finish_omp_structured_block (stmt);
34203 /* OpenMP 2.5:
34204 # pragma omp atomic new-line
34205 expression-stmt
34207 expression-stmt:
34208 x binop= expr | x++ | ++x | x-- | --x
34209 binop:
34210 +, *, -, /, &, ^, |, <<, >>
34212 where x is an lvalue expression with scalar type.
34214 OpenMP 3.1:
34215 # pragma omp atomic new-line
34216 update-stmt
34218 # pragma omp atomic read new-line
34219 read-stmt
34221 # pragma omp atomic write new-line
34222 write-stmt
34224 # pragma omp atomic update new-line
34225 update-stmt
34227 # pragma omp atomic capture new-line
34228 capture-stmt
34230 # pragma omp atomic capture new-line
34231 capture-block
34233 read-stmt:
34234 v = x
34235 write-stmt:
34236 x = expr
34237 update-stmt:
34238 expression-stmt | x = x binop expr
34239 capture-stmt:
34240 v = expression-stmt
34241 capture-block:
34242 { v = x; update-stmt; } | { update-stmt; v = x; }
34244 OpenMP 4.0:
34245 update-stmt:
34246 expression-stmt | x = x binop expr | x = expr binop x
34247 capture-stmt:
34248 v = update-stmt
34249 capture-block:
34250 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34252 where x and v are lvalue expressions with scalar type. */
34254 static void
34255 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34257 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34258 tree rhs1 = NULL_TREE, orig_lhs;
34259 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34260 bool structured_block = false;
34261 bool seq_cst = false;
34263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34266 const char *p = IDENTIFIER_POINTER (id);
34268 if (!strcmp (p, "seq_cst"))
34270 seq_cst = true;
34271 cp_lexer_consume_token (parser->lexer);
34272 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34273 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34274 cp_lexer_consume_token (parser->lexer);
34277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34280 const char *p = IDENTIFIER_POINTER (id);
34282 if (!strcmp (p, "read"))
34283 code = OMP_ATOMIC_READ;
34284 else if (!strcmp (p, "write"))
34285 code = NOP_EXPR;
34286 else if (!strcmp (p, "update"))
34287 code = OMP_ATOMIC;
34288 else if (!strcmp (p, "capture"))
34289 code = OMP_ATOMIC_CAPTURE_NEW;
34290 else
34291 p = NULL;
34292 if (p)
34293 cp_lexer_consume_token (parser->lexer);
34295 if (!seq_cst)
34297 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34298 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34299 cp_lexer_consume_token (parser->lexer);
34301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34304 const char *p = IDENTIFIER_POINTER (id);
34306 if (!strcmp (p, "seq_cst"))
34308 seq_cst = true;
34309 cp_lexer_consume_token (parser->lexer);
34313 cp_parser_require_pragma_eol (parser, pragma_tok);
34315 switch (code)
34317 case OMP_ATOMIC_READ:
34318 case NOP_EXPR: /* atomic write */
34319 v = cp_parser_unary_expression (parser);
34320 if (v == error_mark_node)
34321 goto saw_error;
34322 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34323 goto saw_error;
34324 if (code == NOP_EXPR)
34325 lhs = cp_parser_expression (parser);
34326 else
34327 lhs = cp_parser_unary_expression (parser);
34328 if (lhs == error_mark_node)
34329 goto saw_error;
34330 if (code == NOP_EXPR)
34332 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34333 opcode. */
34334 code = OMP_ATOMIC;
34335 rhs = lhs;
34336 lhs = v;
34337 v = NULL_TREE;
34339 goto done;
34340 case OMP_ATOMIC_CAPTURE_NEW:
34341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34343 cp_lexer_consume_token (parser->lexer);
34344 structured_block = true;
34346 else
34348 v = cp_parser_unary_expression (parser);
34349 if (v == error_mark_node)
34350 goto saw_error;
34351 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34352 goto saw_error;
34354 default:
34355 break;
34358 restart:
34359 lhs = cp_parser_unary_expression (parser);
34360 orig_lhs = lhs;
34361 switch (TREE_CODE (lhs))
34363 case ERROR_MARK:
34364 goto saw_error;
34366 case POSTINCREMENT_EXPR:
34367 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34368 code = OMP_ATOMIC_CAPTURE_OLD;
34369 /* FALLTHROUGH */
34370 case PREINCREMENT_EXPR:
34371 lhs = TREE_OPERAND (lhs, 0);
34372 opcode = PLUS_EXPR;
34373 rhs = integer_one_node;
34374 break;
34376 case POSTDECREMENT_EXPR:
34377 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34378 code = OMP_ATOMIC_CAPTURE_OLD;
34379 /* FALLTHROUGH */
34380 case PREDECREMENT_EXPR:
34381 lhs = TREE_OPERAND (lhs, 0);
34382 opcode = MINUS_EXPR;
34383 rhs = integer_one_node;
34384 break;
34386 case COMPOUND_EXPR:
34387 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34388 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34389 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34390 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34391 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34392 (TREE_OPERAND (lhs, 1), 0), 0)))
34393 == BOOLEAN_TYPE)
34394 /* Undo effects of boolean_increment for post {in,de}crement. */
34395 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34396 /* FALLTHRU */
34397 case MODIFY_EXPR:
34398 if (TREE_CODE (lhs) == MODIFY_EXPR
34399 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34401 /* Undo effects of boolean_increment. */
34402 if (integer_onep (TREE_OPERAND (lhs, 1)))
34404 /* This is pre or post increment. */
34405 rhs = TREE_OPERAND (lhs, 1);
34406 lhs = TREE_OPERAND (lhs, 0);
34407 opcode = NOP_EXPR;
34408 if (code == OMP_ATOMIC_CAPTURE_NEW
34409 && !structured_block
34410 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34411 code = OMP_ATOMIC_CAPTURE_OLD;
34412 break;
34415 /* FALLTHRU */
34416 default:
34417 switch (cp_lexer_peek_token (parser->lexer)->type)
34419 case CPP_MULT_EQ:
34420 opcode = MULT_EXPR;
34421 break;
34422 case CPP_DIV_EQ:
34423 opcode = TRUNC_DIV_EXPR;
34424 break;
34425 case CPP_PLUS_EQ:
34426 opcode = PLUS_EXPR;
34427 break;
34428 case CPP_MINUS_EQ:
34429 opcode = MINUS_EXPR;
34430 break;
34431 case CPP_LSHIFT_EQ:
34432 opcode = LSHIFT_EXPR;
34433 break;
34434 case CPP_RSHIFT_EQ:
34435 opcode = RSHIFT_EXPR;
34436 break;
34437 case CPP_AND_EQ:
34438 opcode = BIT_AND_EXPR;
34439 break;
34440 case CPP_OR_EQ:
34441 opcode = BIT_IOR_EXPR;
34442 break;
34443 case CPP_XOR_EQ:
34444 opcode = BIT_XOR_EXPR;
34445 break;
34446 case CPP_EQ:
34447 enum cp_parser_prec oprec;
34448 cp_token *token;
34449 cp_lexer_consume_token (parser->lexer);
34450 cp_parser_parse_tentatively (parser);
34451 rhs1 = cp_parser_simple_cast_expression (parser);
34452 if (rhs1 == error_mark_node)
34454 cp_parser_abort_tentative_parse (parser);
34455 cp_parser_simple_cast_expression (parser);
34456 goto saw_error;
34458 token = cp_lexer_peek_token (parser->lexer);
34459 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34461 cp_parser_abort_tentative_parse (parser);
34462 cp_parser_parse_tentatively (parser);
34463 rhs = cp_parser_binary_expression (parser, false, true,
34464 PREC_NOT_OPERATOR, NULL);
34465 if (rhs == error_mark_node)
34467 cp_parser_abort_tentative_parse (parser);
34468 cp_parser_binary_expression (parser, false, true,
34469 PREC_NOT_OPERATOR, NULL);
34470 goto saw_error;
34472 switch (TREE_CODE (rhs))
34474 case MULT_EXPR:
34475 case TRUNC_DIV_EXPR:
34476 case RDIV_EXPR:
34477 case PLUS_EXPR:
34478 case MINUS_EXPR:
34479 case LSHIFT_EXPR:
34480 case RSHIFT_EXPR:
34481 case BIT_AND_EXPR:
34482 case BIT_IOR_EXPR:
34483 case BIT_XOR_EXPR:
34484 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34486 if (cp_parser_parse_definitely (parser))
34488 opcode = TREE_CODE (rhs);
34489 rhs1 = TREE_OPERAND (rhs, 0);
34490 rhs = TREE_OPERAND (rhs, 1);
34491 goto stmt_done;
34493 else
34494 goto saw_error;
34496 break;
34497 default:
34498 break;
34500 cp_parser_abort_tentative_parse (parser);
34501 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34503 rhs = cp_parser_expression (parser);
34504 if (rhs == error_mark_node)
34505 goto saw_error;
34506 opcode = NOP_EXPR;
34507 rhs1 = NULL_TREE;
34508 goto stmt_done;
34510 cp_parser_error (parser,
34511 "invalid form of %<#pragma omp atomic%>");
34512 goto saw_error;
34514 if (!cp_parser_parse_definitely (parser))
34515 goto saw_error;
34516 switch (token->type)
34518 case CPP_SEMICOLON:
34519 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34521 code = OMP_ATOMIC_CAPTURE_OLD;
34522 v = lhs;
34523 lhs = NULL_TREE;
34524 lhs1 = rhs1;
34525 rhs1 = NULL_TREE;
34526 cp_lexer_consume_token (parser->lexer);
34527 goto restart;
34529 else if (structured_block)
34531 opcode = NOP_EXPR;
34532 rhs = rhs1;
34533 rhs1 = NULL_TREE;
34534 goto stmt_done;
34536 cp_parser_error (parser,
34537 "invalid form of %<#pragma omp atomic%>");
34538 goto saw_error;
34539 case CPP_MULT:
34540 opcode = MULT_EXPR;
34541 break;
34542 case CPP_DIV:
34543 opcode = TRUNC_DIV_EXPR;
34544 break;
34545 case CPP_PLUS:
34546 opcode = PLUS_EXPR;
34547 break;
34548 case CPP_MINUS:
34549 opcode = MINUS_EXPR;
34550 break;
34551 case CPP_LSHIFT:
34552 opcode = LSHIFT_EXPR;
34553 break;
34554 case CPP_RSHIFT:
34555 opcode = RSHIFT_EXPR;
34556 break;
34557 case CPP_AND:
34558 opcode = BIT_AND_EXPR;
34559 break;
34560 case CPP_OR:
34561 opcode = BIT_IOR_EXPR;
34562 break;
34563 case CPP_XOR:
34564 opcode = BIT_XOR_EXPR;
34565 break;
34566 default:
34567 cp_parser_error (parser,
34568 "invalid operator for %<#pragma omp atomic%>");
34569 goto saw_error;
34571 oprec = TOKEN_PRECEDENCE (token);
34572 gcc_assert (oprec != PREC_NOT_OPERATOR);
34573 if (commutative_tree_code (opcode))
34574 oprec = (enum cp_parser_prec) (oprec - 1);
34575 cp_lexer_consume_token (parser->lexer);
34576 rhs = cp_parser_binary_expression (parser, false, false,
34577 oprec, NULL);
34578 if (rhs == error_mark_node)
34579 goto saw_error;
34580 goto stmt_done;
34581 /* FALLTHROUGH */
34582 default:
34583 cp_parser_error (parser,
34584 "invalid operator for %<#pragma omp atomic%>");
34585 goto saw_error;
34587 cp_lexer_consume_token (parser->lexer);
34589 rhs = cp_parser_expression (parser);
34590 if (rhs == error_mark_node)
34591 goto saw_error;
34592 break;
34594 stmt_done:
34595 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34597 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34598 goto saw_error;
34599 v = cp_parser_unary_expression (parser);
34600 if (v == error_mark_node)
34601 goto saw_error;
34602 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34603 goto saw_error;
34604 lhs1 = cp_parser_unary_expression (parser);
34605 if (lhs1 == error_mark_node)
34606 goto saw_error;
34608 if (structured_block)
34610 cp_parser_consume_semicolon_at_end_of_statement (parser);
34611 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34613 done:
34614 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34615 if (!structured_block)
34616 cp_parser_consume_semicolon_at_end_of_statement (parser);
34617 return;
34619 saw_error:
34620 cp_parser_skip_to_end_of_block_or_statement (parser);
34621 if (structured_block)
34623 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34624 cp_lexer_consume_token (parser->lexer);
34625 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34627 cp_parser_skip_to_end_of_block_or_statement (parser);
34628 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34629 cp_lexer_consume_token (parser->lexer);
34635 /* OpenMP 2.5:
34636 # pragma omp barrier new-line */
34638 static void
34639 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34641 cp_parser_require_pragma_eol (parser, pragma_tok);
34642 finish_omp_barrier ();
34645 /* OpenMP 2.5:
34646 # pragma omp critical [(name)] new-line
34647 structured-block
34649 OpenMP 4.5:
34650 # pragma omp critical [(name) [hint(expression)]] new-line
34651 structured-block */
34653 #define OMP_CRITICAL_CLAUSE_MASK \
34654 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34656 static tree
34657 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34659 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34663 matching_parens parens;
34664 parens.consume_open (parser);
34666 name = cp_parser_identifier (parser);
34668 if (name == error_mark_node
34669 || !parens.require_close (parser))
34670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34671 /*or_comma=*/false,
34672 /*consume_paren=*/true);
34673 if (name == error_mark_node)
34674 name = NULL;
34676 clauses = cp_parser_omp_all_clauses (parser,
34677 OMP_CRITICAL_CLAUSE_MASK,
34678 "#pragma omp critical", pragma_tok);
34680 else
34681 cp_parser_require_pragma_eol (parser, pragma_tok);
34683 stmt = cp_parser_omp_structured_block (parser, if_p);
34684 return c_finish_omp_critical (input_location, stmt, name, clauses);
34687 /* OpenMP 2.5:
34688 # pragma omp flush flush-vars[opt] new-line
34690 flush-vars:
34691 ( variable-list ) */
34693 static void
34694 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34697 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34698 cp_parser_require_pragma_eol (parser, pragma_tok);
34700 finish_omp_flush ();
34703 /* Helper function, to parse omp for increment expression. */
34705 static tree
34706 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34708 tree cond = cp_parser_binary_expression (parser, false, true,
34709 PREC_NOT_OPERATOR, NULL);
34710 if (cond == error_mark_node
34711 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34713 cp_parser_skip_to_end_of_statement (parser);
34714 return error_mark_node;
34717 switch (TREE_CODE (cond))
34719 case GT_EXPR:
34720 case GE_EXPR:
34721 case LT_EXPR:
34722 case LE_EXPR:
34723 break;
34724 case NE_EXPR:
34725 /* Fall through: OpenMP disallows NE_EXPR. */
34726 gcc_fallthrough ();
34727 default:
34728 return error_mark_node;
34731 /* If decl is an iterator, preserve LHS and RHS of the relational
34732 expr until finish_omp_for. */
34733 if (decl
34734 && (type_dependent_expression_p (decl)
34735 || CLASS_TYPE_P (TREE_TYPE (decl))))
34736 return cond;
34738 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34739 TREE_CODE (cond),
34740 TREE_OPERAND (cond, 0), ERROR_MARK,
34741 TREE_OPERAND (cond, 1), ERROR_MARK,
34742 /*overload=*/NULL, tf_warning_or_error);
34745 /* Helper function, to parse omp for increment expression. */
34747 static tree
34748 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34750 cp_token *token = cp_lexer_peek_token (parser->lexer);
34751 enum tree_code op;
34752 tree lhs, rhs;
34753 cp_id_kind idk;
34754 bool decl_first;
34756 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34758 op = (token->type == CPP_PLUS_PLUS
34759 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34760 cp_lexer_consume_token (parser->lexer);
34761 lhs = cp_parser_simple_cast_expression (parser);
34762 if (lhs != decl
34763 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34764 return error_mark_node;
34765 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34768 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34769 if (lhs != decl
34770 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34771 return error_mark_node;
34773 token = cp_lexer_peek_token (parser->lexer);
34774 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34776 op = (token->type == CPP_PLUS_PLUS
34777 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34778 cp_lexer_consume_token (parser->lexer);
34779 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34782 op = cp_parser_assignment_operator_opt (parser);
34783 if (op == ERROR_MARK)
34784 return error_mark_node;
34786 if (op != NOP_EXPR)
34788 rhs = cp_parser_assignment_expression (parser);
34789 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34790 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34793 lhs = cp_parser_binary_expression (parser, false, false,
34794 PREC_ADDITIVE_EXPRESSION, NULL);
34795 token = cp_lexer_peek_token (parser->lexer);
34796 decl_first = (lhs == decl
34797 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34798 if (decl_first)
34799 lhs = NULL_TREE;
34800 if (token->type != CPP_PLUS
34801 && token->type != CPP_MINUS)
34802 return error_mark_node;
34806 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34807 cp_lexer_consume_token (parser->lexer);
34808 rhs = cp_parser_binary_expression (parser, false, false,
34809 PREC_ADDITIVE_EXPRESSION, NULL);
34810 token = cp_lexer_peek_token (parser->lexer);
34811 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34813 if (lhs == NULL_TREE)
34815 if (op == PLUS_EXPR)
34816 lhs = rhs;
34817 else
34818 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34819 tf_warning_or_error);
34821 else
34822 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34823 ERROR_MARK, NULL, tf_warning_or_error);
34826 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34828 if (!decl_first)
34830 if ((rhs != decl
34831 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34832 || op == MINUS_EXPR)
34833 return error_mark_node;
34834 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34836 else
34837 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34839 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34842 /* Parse the initialization statement of an OpenMP for loop.
34844 Return true if the resulting construct should have an
34845 OMP_CLAUSE_PRIVATE added to it. */
34847 static tree
34848 cp_parser_omp_for_loop_init (cp_parser *parser,
34849 tree &this_pre_body,
34850 vec<tree, va_gc> *for_block,
34851 tree &init,
34852 tree &orig_init,
34853 tree &decl,
34854 tree &real_decl)
34856 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34857 return NULL_TREE;
34859 tree add_private_clause = NULL_TREE;
34861 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34863 init-expr:
34864 var = lb
34865 integer-type var = lb
34866 random-access-iterator-type var = lb
34867 pointer-type var = lb
34869 cp_decl_specifier_seq type_specifiers;
34871 /* First, try to parse as an initialized declaration. See
34872 cp_parser_condition, from whence the bulk of this is copied. */
34874 cp_parser_parse_tentatively (parser);
34875 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34876 /*is_trailing_return=*/false,
34877 &type_specifiers);
34878 if (cp_parser_parse_definitely (parser))
34880 /* If parsing a type specifier seq succeeded, then this
34881 MUST be a initialized declaration. */
34882 tree asm_specification, attributes;
34883 cp_declarator *declarator;
34885 declarator = cp_parser_declarator (parser,
34886 CP_PARSER_DECLARATOR_NAMED,
34887 /*ctor_dtor_or_conv_p=*/NULL,
34888 /*parenthesized_p=*/NULL,
34889 /*member_p=*/false,
34890 /*friend_p=*/false);
34891 attributes = cp_parser_attributes_opt (parser);
34892 asm_specification = cp_parser_asm_specification_opt (parser);
34894 if (declarator == cp_error_declarator)
34895 cp_parser_skip_to_end_of_statement (parser);
34897 else
34899 tree pushed_scope, auto_node;
34901 decl = start_decl (declarator, &type_specifiers,
34902 SD_INITIALIZED, attributes,
34903 /*prefix_attributes=*/NULL_TREE,
34904 &pushed_scope);
34906 auto_node = type_uses_auto (TREE_TYPE (decl));
34907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34909 if (cp_lexer_next_token_is (parser->lexer,
34910 CPP_OPEN_PAREN))
34911 error ("parenthesized initialization is not allowed in "
34912 "OpenMP %<for%> loop");
34913 else
34914 /* Trigger an error. */
34915 cp_parser_require (parser, CPP_EQ, RT_EQ);
34917 init = error_mark_node;
34918 cp_parser_skip_to_end_of_statement (parser);
34920 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34921 || type_dependent_expression_p (decl)
34922 || auto_node)
34924 bool is_direct_init, is_non_constant_init;
34926 init = cp_parser_initializer (parser,
34927 &is_direct_init,
34928 &is_non_constant_init);
34930 if (auto_node)
34932 TREE_TYPE (decl)
34933 = do_auto_deduction (TREE_TYPE (decl), init,
34934 auto_node);
34936 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34937 && !type_dependent_expression_p (decl))
34938 goto non_class;
34941 cp_finish_decl (decl, init, !is_non_constant_init,
34942 asm_specification,
34943 LOOKUP_ONLYCONVERTING);
34944 orig_init = init;
34945 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34947 vec_safe_push (for_block, this_pre_body);
34948 init = NULL_TREE;
34950 else
34952 init = pop_stmt_list (this_pre_body);
34953 if (init && TREE_CODE (init) == STATEMENT_LIST)
34955 tree_stmt_iterator i = tsi_start (init);
34956 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34957 while (!tsi_end_p (i))
34959 tree t = tsi_stmt (i);
34960 if (TREE_CODE (t) == DECL_EXPR
34961 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34963 tsi_delink (&i);
34964 vec_safe_push (for_block, t);
34965 continue;
34967 break;
34969 if (tsi_one_before_end_p (i))
34971 tree t = tsi_stmt (i);
34972 tsi_delink (&i);
34973 free_stmt_list (init);
34974 init = t;
34978 this_pre_body = NULL_TREE;
34980 else
34982 /* Consume '='. */
34983 cp_lexer_consume_token (parser->lexer);
34984 init = cp_parser_assignment_expression (parser);
34986 non_class:
34987 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34988 init = error_mark_node;
34989 else
34990 cp_finish_decl (decl, NULL_TREE,
34991 /*init_const_expr_p=*/false,
34992 asm_specification,
34993 LOOKUP_ONLYCONVERTING);
34996 if (pushed_scope)
34997 pop_scope (pushed_scope);
35000 else
35002 cp_id_kind idk;
35003 /* If parsing a type specifier sequence failed, then
35004 this MUST be a simple expression. */
35005 cp_parser_parse_tentatively (parser);
35006 decl = cp_parser_primary_expression (parser, false, false,
35007 false, &idk);
35008 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35009 if (!cp_parser_error_occurred (parser)
35010 && decl
35011 && (TREE_CODE (decl) == COMPONENT_REF
35012 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35014 cp_parser_abort_tentative_parse (parser);
35015 cp_parser_parse_tentatively (parser);
35016 cp_token *token = cp_lexer_peek_token (parser->lexer);
35017 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35018 /*check_dependency_p=*/true,
35019 /*template_p=*/NULL,
35020 /*declarator_p=*/false,
35021 /*optional_p=*/false);
35022 if (name != error_mark_node
35023 && last_tok == cp_lexer_peek_token (parser->lexer))
35025 decl = cp_parser_lookup_name_simple (parser, name,
35026 token->location);
35027 if (TREE_CODE (decl) == FIELD_DECL)
35028 add_private_clause = omp_privatize_field (decl, false);
35030 cp_parser_abort_tentative_parse (parser);
35031 cp_parser_parse_tentatively (parser);
35032 decl = cp_parser_primary_expression (parser, false, false,
35033 false, &idk);
35035 if (!cp_parser_error_occurred (parser)
35036 && decl
35037 && DECL_P (decl)
35038 && CLASS_TYPE_P (TREE_TYPE (decl)))
35040 tree rhs;
35042 cp_parser_parse_definitely (parser);
35043 cp_parser_require (parser, CPP_EQ, RT_EQ);
35044 rhs = cp_parser_assignment_expression (parser);
35045 orig_init = rhs;
35046 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35047 decl, NOP_EXPR,
35048 rhs,
35049 tf_warning_or_error));
35050 if (!add_private_clause)
35051 add_private_clause = decl;
35053 else
35055 decl = NULL;
35056 cp_parser_abort_tentative_parse (parser);
35057 init = cp_parser_expression (parser);
35058 if (init)
35060 if (TREE_CODE (init) == MODIFY_EXPR
35061 || TREE_CODE (init) == MODOP_EXPR)
35062 real_decl = TREE_OPERAND (init, 0);
35066 return add_private_clause;
35069 /* Parse the restricted form of the for statement allowed by OpenMP. */
35071 static tree
35072 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35073 tree *cclauses, bool *if_p)
35075 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35076 tree real_decl, initv, condv, incrv, declv;
35077 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35078 location_t loc_first;
35079 bool collapse_err = false;
35080 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35081 vec<tree, va_gc> *for_block = make_tree_vector ();
35082 auto_vec<tree, 4> orig_inits;
35083 bool tiling = false;
35085 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35086 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35087 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35088 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35090 tiling = true;
35091 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35093 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35094 && OMP_CLAUSE_ORDERED_EXPR (cl))
35096 ordered_cl = cl;
35097 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35100 if (ordered && ordered < collapse)
35102 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35103 "%<ordered%> clause parameter is less than %<collapse%>");
35104 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35105 = build_int_cst (NULL_TREE, collapse);
35106 ordered = collapse;
35108 if (ordered)
35110 for (tree *pc = &clauses; *pc; )
35111 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35113 error_at (OMP_CLAUSE_LOCATION (*pc),
35114 "%<linear%> clause may not be specified together "
35115 "with %<ordered%> clause with a parameter");
35116 *pc = OMP_CLAUSE_CHAIN (*pc);
35118 else
35119 pc = &OMP_CLAUSE_CHAIN (*pc);
35122 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35123 count = ordered ? ordered : collapse;
35125 declv = make_tree_vec (count);
35126 initv = make_tree_vec (count);
35127 condv = make_tree_vec (count);
35128 incrv = make_tree_vec (count);
35130 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35132 for (i = 0; i < count; i++)
35134 int bracecount = 0;
35135 tree add_private_clause = NULL_TREE;
35136 location_t loc;
35138 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35140 if (!collapse_err)
35141 cp_parser_error (parser, "for statement expected");
35142 return NULL;
35144 loc = cp_lexer_consume_token (parser->lexer)->location;
35146 matching_parens parens;
35147 if (!parens.require_open (parser))
35148 return NULL;
35150 init = orig_init = decl = real_decl = NULL;
35151 this_pre_body = push_stmt_list ();
35153 add_private_clause
35154 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35155 init, orig_init, decl, real_decl);
35157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35158 if (this_pre_body)
35160 this_pre_body = pop_stmt_list (this_pre_body);
35161 if (pre_body)
35163 tree t = pre_body;
35164 pre_body = push_stmt_list ();
35165 add_stmt (t);
35166 add_stmt (this_pre_body);
35167 pre_body = pop_stmt_list (pre_body);
35169 else
35170 pre_body = this_pre_body;
35173 if (decl)
35174 real_decl = decl;
35175 if (cclauses != NULL
35176 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35177 && real_decl != NULL_TREE)
35179 tree *c;
35180 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35181 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35182 && OMP_CLAUSE_DECL (*c) == real_decl)
35184 error_at (loc, "iteration variable %qD"
35185 " should not be firstprivate", real_decl);
35186 *c = OMP_CLAUSE_CHAIN (*c);
35188 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35189 && OMP_CLAUSE_DECL (*c) == real_decl)
35191 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35192 tree l = *c;
35193 *c = OMP_CLAUSE_CHAIN (*c);
35194 if (code == OMP_SIMD)
35196 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35197 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35199 else
35201 OMP_CLAUSE_CHAIN (l) = clauses;
35202 clauses = l;
35204 add_private_clause = NULL_TREE;
35206 else
35208 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35209 && OMP_CLAUSE_DECL (*c) == real_decl)
35210 add_private_clause = NULL_TREE;
35211 c = &OMP_CLAUSE_CHAIN (*c);
35215 if (add_private_clause)
35217 tree c;
35218 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35220 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35221 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35222 && OMP_CLAUSE_DECL (c) == decl)
35223 break;
35224 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35225 && OMP_CLAUSE_DECL (c) == decl)
35226 error_at (loc, "iteration variable %qD "
35227 "should not be firstprivate",
35228 decl);
35229 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35230 && OMP_CLAUSE_DECL (c) == decl)
35231 error_at (loc, "iteration variable %qD should not be reduction",
35232 decl);
35234 if (c == NULL)
35236 if (code != OMP_SIMD)
35237 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35238 else if (collapse == 1)
35239 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35240 else
35241 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35242 OMP_CLAUSE_DECL (c) = add_private_clause;
35243 c = finish_omp_clauses (c, C_ORT_OMP);
35244 if (c)
35246 OMP_CLAUSE_CHAIN (c) = clauses;
35247 clauses = c;
35248 /* For linear, signal that we need to fill up
35249 the so far unknown linear step. */
35250 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35251 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35256 cond = NULL;
35257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35258 cond = cp_parser_omp_for_cond (parser, decl);
35259 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35261 incr = NULL;
35262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35264 /* If decl is an iterator, preserve the operator on decl
35265 until finish_omp_for. */
35266 if (real_decl
35267 && ((processing_template_decl
35268 && (TREE_TYPE (real_decl) == NULL_TREE
35269 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35270 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35271 incr = cp_parser_omp_for_incr (parser, real_decl);
35272 else
35273 incr = cp_parser_expression (parser);
35274 if (!EXPR_HAS_LOCATION (incr))
35275 protected_set_expr_location (incr, input_location);
35278 if (!parens.require_close (parser))
35279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35280 /*or_comma=*/false,
35281 /*consume_paren=*/true);
35283 TREE_VEC_ELT (declv, i) = decl;
35284 TREE_VEC_ELT (initv, i) = init;
35285 TREE_VEC_ELT (condv, i) = cond;
35286 TREE_VEC_ELT (incrv, i) = incr;
35287 if (orig_init)
35289 orig_inits.safe_grow_cleared (i + 1);
35290 orig_inits[i] = orig_init;
35293 if (i == count - 1)
35294 break;
35296 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35297 in between the collapsed for loops to be still considered perfectly
35298 nested. Hopefully the final version clarifies this.
35299 For now handle (multiple) {'s and empty statements. */
35300 cp_parser_parse_tentatively (parser);
35301 for (;;)
35303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35304 break;
35305 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35307 cp_lexer_consume_token (parser->lexer);
35308 bracecount++;
35310 else if (bracecount
35311 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35312 cp_lexer_consume_token (parser->lexer);
35313 else
35315 loc = cp_lexer_peek_token (parser->lexer)->location;
35316 error_at (loc, "not enough for loops to collapse");
35317 collapse_err = true;
35318 cp_parser_abort_tentative_parse (parser);
35319 declv = NULL_TREE;
35320 break;
35324 if (declv)
35326 cp_parser_parse_definitely (parser);
35327 nbraces += bracecount;
35331 if (nbraces)
35332 if_p = NULL;
35334 /* Note that we saved the original contents of this flag when we entered
35335 the structured block, and so we don't need to re-save it here. */
35336 parser->in_statement = IN_OMP_FOR;
35338 /* Note that the grammar doesn't call for a structured block here,
35339 though the loop as a whole is a structured block. */
35340 body = push_stmt_list ();
35341 cp_parser_statement (parser, NULL_TREE, false, if_p);
35342 body = pop_stmt_list (body);
35344 if (declv == NULL_TREE)
35345 ret = NULL_TREE;
35346 else
35347 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35348 body, pre_body, &orig_inits, clauses);
35350 while (nbraces)
35352 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35354 cp_lexer_consume_token (parser->lexer);
35355 nbraces--;
35357 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35358 cp_lexer_consume_token (parser->lexer);
35359 else
35361 if (!collapse_err)
35363 error_at (cp_lexer_peek_token (parser->lexer)->location,
35364 "collapsed loops not perfectly nested");
35366 collapse_err = true;
35367 cp_parser_statement_seq_opt (parser, NULL);
35368 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35369 break;
35373 while (!for_block->is_empty ())
35375 tree t = for_block->pop ();
35376 if (TREE_CODE (t) == STATEMENT_LIST)
35377 add_stmt (pop_stmt_list (t));
35378 else
35379 add_stmt (t);
35381 release_tree_vector (for_block);
35383 return ret;
35386 /* Helper function for OpenMP parsing, split clauses and call
35387 finish_omp_clauses on each of the set of clauses afterwards. */
35389 static void
35390 cp_omp_split_clauses (location_t loc, enum tree_code code,
35391 omp_clause_mask mask, tree clauses, tree *cclauses)
35393 int i;
35394 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35395 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35396 if (cclauses[i])
35397 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35400 /* OpenMP 4.0:
35401 #pragma omp simd simd-clause[optseq] new-line
35402 for-loop */
35404 #define OMP_SIMD_CLAUSE_MASK \
35405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35414 static tree
35415 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35416 char *p_name, omp_clause_mask mask, tree *cclauses,
35417 bool *if_p)
35419 tree clauses, sb, ret;
35420 unsigned int save;
35421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35423 strcat (p_name, " simd");
35424 mask |= OMP_SIMD_CLAUSE_MASK;
35426 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35427 cclauses == NULL);
35428 if (cclauses)
35430 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35431 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35432 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35433 OMP_CLAUSE_ORDERED);
35434 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35436 error_at (OMP_CLAUSE_LOCATION (c),
35437 "%<ordered%> clause with parameter may not be specified "
35438 "on %qs construct", p_name);
35439 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35443 sb = begin_omp_structured_block ();
35444 save = cp_parser_begin_omp_structured_block (parser);
35446 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35448 cp_parser_end_omp_structured_block (parser, save);
35449 add_stmt (finish_omp_structured_block (sb));
35451 return ret;
35454 /* OpenMP 2.5:
35455 #pragma omp for for-clause[optseq] new-line
35456 for-loop
35458 OpenMP 4.0:
35459 #pragma omp for simd for-simd-clause[optseq] new-line
35460 for-loop */
35462 #define OMP_FOR_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35473 static tree
35474 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35475 char *p_name, omp_clause_mask mask, tree *cclauses,
35476 bool *if_p)
35478 tree clauses, sb, ret;
35479 unsigned int save;
35480 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35482 strcat (p_name, " for");
35483 mask |= OMP_FOR_CLAUSE_MASK;
35484 /* parallel for{, simd} disallows nowait clause, but for
35485 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35486 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35487 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35488 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35489 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35490 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35494 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35495 const char *p = IDENTIFIER_POINTER (id);
35497 if (strcmp (p, "simd") == 0)
35499 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35500 if (cclauses == NULL)
35501 cclauses = cclauses_buf;
35503 cp_lexer_consume_token (parser->lexer);
35504 if (!flag_openmp) /* flag_openmp_simd */
35505 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35506 cclauses, if_p);
35507 sb = begin_omp_structured_block ();
35508 save = cp_parser_begin_omp_structured_block (parser);
35509 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35510 cclauses, if_p);
35511 cp_parser_end_omp_structured_block (parser, save);
35512 tree body = finish_omp_structured_block (sb);
35513 if (ret == NULL)
35514 return ret;
35515 ret = make_node (OMP_FOR);
35516 TREE_TYPE (ret) = void_type_node;
35517 OMP_FOR_BODY (ret) = body;
35518 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35519 SET_EXPR_LOCATION (ret, loc);
35520 add_stmt (ret);
35521 return ret;
35524 if (!flag_openmp) /* flag_openmp_simd */
35526 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35527 return NULL_TREE;
35530 /* Composite distribute parallel for disallows linear clause. */
35531 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35532 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35534 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35535 cclauses == NULL);
35536 if (cclauses)
35538 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35539 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35542 sb = begin_omp_structured_block ();
35543 save = cp_parser_begin_omp_structured_block (parser);
35545 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35547 cp_parser_end_omp_structured_block (parser, save);
35548 add_stmt (finish_omp_structured_block (sb));
35550 return ret;
35553 /* OpenMP 2.5:
35554 # pragma omp master new-line
35555 structured-block */
35557 static tree
35558 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35560 cp_parser_require_pragma_eol (parser, pragma_tok);
35561 return c_finish_omp_master (input_location,
35562 cp_parser_omp_structured_block (parser, if_p));
35565 /* OpenMP 2.5:
35566 # pragma omp ordered new-line
35567 structured-block
35569 OpenMP 4.5:
35570 # pragma omp ordered ordered-clauses new-line
35571 structured-block */
35573 #define OMP_ORDERED_CLAUSE_MASK \
35574 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35577 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35578 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35580 static bool
35581 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35582 enum pragma_context context, bool *if_p)
35584 location_t loc = pragma_tok->location;
35586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35589 const char *p = IDENTIFIER_POINTER (id);
35591 if (strcmp (p, "depend") == 0)
35593 if (!flag_openmp) /* flag_openmp_simd */
35595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35596 return false;
35598 if (context == pragma_stmt)
35600 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35601 "%<depend%> clause may only be used in compound "
35602 "statements");
35603 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35604 return false;
35606 tree clauses
35607 = cp_parser_omp_all_clauses (parser,
35608 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35609 "#pragma omp ordered", pragma_tok);
35610 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35611 return false;
35615 tree clauses
35616 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35617 "#pragma omp ordered", pragma_tok);
35619 if (!flag_openmp /* flag_openmp_simd */
35620 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35621 return false;
35623 c_finish_omp_ordered (loc, clauses,
35624 cp_parser_omp_structured_block (parser, if_p));
35625 return true;
35628 /* OpenMP 2.5:
35630 section-scope:
35631 { section-sequence }
35633 section-sequence:
35634 section-directive[opt] structured-block
35635 section-sequence section-directive structured-block */
35637 static tree
35638 cp_parser_omp_sections_scope (cp_parser *parser)
35640 tree stmt, substmt;
35641 bool error_suppress = false;
35642 cp_token *tok;
35644 matching_braces braces;
35645 if (!braces.require_open (parser))
35646 return NULL_TREE;
35648 stmt = push_stmt_list ();
35650 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35651 != PRAGMA_OMP_SECTION)
35653 substmt = cp_parser_omp_structured_block (parser, NULL);
35654 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35655 add_stmt (substmt);
35658 while (1)
35660 tok = cp_lexer_peek_token (parser->lexer);
35661 if (tok->type == CPP_CLOSE_BRACE)
35662 break;
35663 if (tok->type == CPP_EOF)
35664 break;
35666 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35668 cp_lexer_consume_token (parser->lexer);
35669 cp_parser_require_pragma_eol (parser, tok);
35670 error_suppress = false;
35672 else if (!error_suppress)
35674 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35675 error_suppress = true;
35678 substmt = cp_parser_omp_structured_block (parser, NULL);
35679 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35680 add_stmt (substmt);
35682 braces.require_close (parser);
35684 substmt = pop_stmt_list (stmt);
35686 stmt = make_node (OMP_SECTIONS);
35687 TREE_TYPE (stmt) = void_type_node;
35688 OMP_SECTIONS_BODY (stmt) = substmt;
35690 add_stmt (stmt);
35691 return stmt;
35694 /* OpenMP 2.5:
35695 # pragma omp sections sections-clause[optseq] newline
35696 sections-scope */
35698 #define OMP_SECTIONS_CLAUSE_MASK \
35699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35705 static tree
35706 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35707 char *p_name, omp_clause_mask mask, tree *cclauses)
35709 tree clauses, ret;
35710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35712 strcat (p_name, " sections");
35713 mask |= OMP_SECTIONS_CLAUSE_MASK;
35714 if (cclauses)
35715 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35717 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35718 cclauses == NULL);
35719 if (cclauses)
35721 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35722 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35725 ret = cp_parser_omp_sections_scope (parser);
35726 if (ret)
35727 OMP_SECTIONS_CLAUSES (ret) = clauses;
35729 return ret;
35732 /* OpenMP 2.5:
35733 # pragma omp parallel parallel-clause[optseq] new-line
35734 structured-block
35735 # pragma omp parallel for parallel-for-clause[optseq] new-line
35736 structured-block
35737 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35738 structured-block
35740 OpenMP 4.0:
35741 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35742 structured-block */
35744 #define OMP_PARALLEL_CLAUSE_MASK \
35745 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35755 static tree
35756 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35757 char *p_name, omp_clause_mask mask, tree *cclauses,
35758 bool *if_p)
35760 tree stmt, clauses, block;
35761 unsigned int save;
35762 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35764 strcat (p_name, " parallel");
35765 mask |= OMP_PARALLEL_CLAUSE_MASK;
35766 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35767 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35768 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35769 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35773 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35774 if (cclauses == NULL)
35775 cclauses = cclauses_buf;
35777 cp_lexer_consume_token (parser->lexer);
35778 if (!flag_openmp) /* flag_openmp_simd */
35779 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35780 if_p);
35781 block = begin_omp_parallel ();
35782 save = cp_parser_begin_omp_structured_block (parser);
35783 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35784 if_p);
35785 cp_parser_end_omp_structured_block (parser, save);
35786 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35787 block);
35788 if (ret == NULL_TREE)
35789 return ret;
35790 OMP_PARALLEL_COMBINED (stmt) = 1;
35791 return stmt;
35793 /* When combined with distribute, parallel has to be followed by for.
35794 #pragma omp target parallel is allowed though. */
35795 else if (cclauses
35796 && (mask & (OMP_CLAUSE_MASK_1
35797 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35799 error_at (loc, "expected %<for%> after %qs", p_name);
35800 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35801 return NULL_TREE;
35803 else if (!flag_openmp) /* flag_openmp_simd */
35805 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35806 return NULL_TREE;
35808 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35810 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35811 const char *p = IDENTIFIER_POINTER (id);
35812 if (strcmp (p, "sections") == 0)
35814 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35815 cclauses = cclauses_buf;
35817 cp_lexer_consume_token (parser->lexer);
35818 block = begin_omp_parallel ();
35819 save = cp_parser_begin_omp_structured_block (parser);
35820 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35821 cp_parser_end_omp_structured_block (parser, save);
35822 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35823 block);
35824 OMP_PARALLEL_COMBINED (stmt) = 1;
35825 return stmt;
35829 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35830 cclauses == NULL);
35831 if (cclauses)
35833 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35834 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35837 block = begin_omp_parallel ();
35838 save = cp_parser_begin_omp_structured_block (parser);
35839 cp_parser_statement (parser, NULL_TREE, false, if_p);
35840 cp_parser_end_omp_structured_block (parser, save);
35841 stmt = finish_omp_parallel (clauses, block);
35842 return stmt;
35845 /* OpenMP 2.5:
35846 # pragma omp single single-clause[optseq] new-line
35847 structured-block */
35849 #define OMP_SINGLE_CLAUSE_MASK \
35850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35855 static tree
35856 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35858 tree stmt = make_node (OMP_SINGLE);
35859 TREE_TYPE (stmt) = void_type_node;
35861 OMP_SINGLE_CLAUSES (stmt)
35862 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35863 "#pragma omp single", pragma_tok);
35864 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35866 return add_stmt (stmt);
35869 /* OpenMP 3.0:
35870 # pragma omp task task-clause[optseq] new-line
35871 structured-block */
35873 #define OMP_TASK_CLAUSE_MASK \
35874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35885 static tree
35886 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35888 tree clauses, block;
35889 unsigned int save;
35891 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35892 "#pragma omp task", pragma_tok);
35893 block = begin_omp_task ();
35894 save = cp_parser_begin_omp_structured_block (parser);
35895 cp_parser_statement (parser, NULL_TREE, false, if_p);
35896 cp_parser_end_omp_structured_block (parser, save);
35897 return finish_omp_task (clauses, block);
35900 /* OpenMP 3.0:
35901 # pragma omp taskwait new-line */
35903 static void
35904 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35906 cp_parser_require_pragma_eol (parser, pragma_tok);
35907 finish_omp_taskwait ();
35910 /* OpenMP 3.1:
35911 # pragma omp taskyield new-line */
35913 static void
35914 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35916 cp_parser_require_pragma_eol (parser, pragma_tok);
35917 finish_omp_taskyield ();
35920 /* OpenMP 4.0:
35921 # pragma omp taskgroup new-line
35922 structured-block */
35924 static tree
35925 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35927 cp_parser_require_pragma_eol (parser, pragma_tok);
35928 return c_finish_omp_taskgroup (input_location,
35929 cp_parser_omp_structured_block (parser,
35930 if_p));
35934 /* OpenMP 2.5:
35935 # pragma omp threadprivate (variable-list) */
35937 static void
35938 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35940 tree vars;
35942 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35943 cp_parser_require_pragma_eol (parser, pragma_tok);
35945 finish_omp_threadprivate (vars);
35948 /* OpenMP 4.0:
35949 # pragma omp cancel cancel-clause[optseq] new-line */
35951 #define OMP_CANCEL_CLAUSE_MASK \
35952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35958 static void
35959 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35961 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35962 "#pragma omp cancel", pragma_tok);
35963 finish_omp_cancel (clauses);
35966 /* OpenMP 4.0:
35967 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35969 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35970 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35975 static void
35976 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35977 enum pragma_context context)
35979 tree clauses;
35980 bool point_seen = false;
35982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35985 const char *p = IDENTIFIER_POINTER (id);
35987 if (strcmp (p, "point") == 0)
35989 cp_lexer_consume_token (parser->lexer);
35990 point_seen = true;
35993 if (!point_seen)
35995 cp_parser_error (parser, "expected %<point%>");
35996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35997 return;
36000 if (context != pragma_compound)
36002 if (context == pragma_stmt)
36003 error_at (pragma_tok->location,
36004 "%<#pragma %s%> may only be used in compound statements",
36005 "omp cancellation point");
36006 else
36007 cp_parser_error (parser, "expected declaration specifiers");
36008 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36009 return;
36012 clauses = cp_parser_omp_all_clauses (parser,
36013 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36014 "#pragma omp cancellation point",
36015 pragma_tok);
36016 finish_omp_cancellation_point (clauses);
36019 /* OpenMP 4.0:
36020 #pragma omp distribute distribute-clause[optseq] new-line
36021 for-loop */
36023 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36030 static tree
36031 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36032 char *p_name, omp_clause_mask mask, tree *cclauses,
36033 bool *if_p)
36035 tree clauses, sb, ret;
36036 unsigned int save;
36037 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36039 strcat (p_name, " distribute");
36040 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36044 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36045 const char *p = IDENTIFIER_POINTER (id);
36046 bool simd = false;
36047 bool parallel = false;
36049 if (strcmp (p, "simd") == 0)
36050 simd = true;
36051 else
36052 parallel = strcmp (p, "parallel") == 0;
36053 if (parallel || simd)
36055 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36056 if (cclauses == NULL)
36057 cclauses = cclauses_buf;
36058 cp_lexer_consume_token (parser->lexer);
36059 if (!flag_openmp) /* flag_openmp_simd */
36061 if (simd)
36062 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36063 cclauses, if_p);
36064 else
36065 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36066 cclauses, if_p);
36068 sb = begin_omp_structured_block ();
36069 save = cp_parser_begin_omp_structured_block (parser);
36070 if (simd)
36071 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36072 cclauses, if_p);
36073 else
36074 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36075 cclauses, if_p);
36076 cp_parser_end_omp_structured_block (parser, save);
36077 tree body = finish_omp_structured_block (sb);
36078 if (ret == NULL)
36079 return ret;
36080 ret = make_node (OMP_DISTRIBUTE);
36081 TREE_TYPE (ret) = void_type_node;
36082 OMP_FOR_BODY (ret) = body;
36083 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36084 SET_EXPR_LOCATION (ret, loc);
36085 add_stmt (ret);
36086 return ret;
36089 if (!flag_openmp) /* flag_openmp_simd */
36091 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36092 return NULL_TREE;
36095 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36096 cclauses == NULL);
36097 if (cclauses)
36099 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36100 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36103 sb = begin_omp_structured_block ();
36104 save = cp_parser_begin_omp_structured_block (parser);
36106 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36108 cp_parser_end_omp_structured_block (parser, save);
36109 add_stmt (finish_omp_structured_block (sb));
36111 return ret;
36114 /* OpenMP 4.0:
36115 # pragma omp teams teams-clause[optseq] new-line
36116 structured-block */
36118 #define OMP_TEAMS_CLAUSE_MASK \
36119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36127 static tree
36128 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36129 char *p_name, omp_clause_mask mask, tree *cclauses,
36130 bool *if_p)
36132 tree clauses, sb, ret;
36133 unsigned int save;
36134 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36136 strcat (p_name, " teams");
36137 mask |= OMP_TEAMS_CLAUSE_MASK;
36139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36141 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36142 const char *p = IDENTIFIER_POINTER (id);
36143 if (strcmp (p, "distribute") == 0)
36145 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36146 if (cclauses == NULL)
36147 cclauses = cclauses_buf;
36149 cp_lexer_consume_token (parser->lexer);
36150 if (!flag_openmp) /* flag_openmp_simd */
36151 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36152 cclauses, if_p);
36153 sb = begin_omp_structured_block ();
36154 save = cp_parser_begin_omp_structured_block (parser);
36155 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36156 cclauses, if_p);
36157 cp_parser_end_omp_structured_block (parser, save);
36158 tree body = finish_omp_structured_block (sb);
36159 if (ret == NULL)
36160 return ret;
36161 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36162 ret = make_node (OMP_TEAMS);
36163 TREE_TYPE (ret) = void_type_node;
36164 OMP_TEAMS_CLAUSES (ret) = clauses;
36165 OMP_TEAMS_BODY (ret) = body;
36166 OMP_TEAMS_COMBINED (ret) = 1;
36167 SET_EXPR_LOCATION (ret, loc);
36168 return add_stmt (ret);
36171 if (!flag_openmp) /* flag_openmp_simd */
36173 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36174 return NULL_TREE;
36177 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36178 cclauses == NULL);
36179 if (cclauses)
36181 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36182 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36185 tree stmt = make_node (OMP_TEAMS);
36186 TREE_TYPE (stmt) = void_type_node;
36187 OMP_TEAMS_CLAUSES (stmt) = clauses;
36188 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36189 SET_EXPR_LOCATION (stmt, loc);
36191 return add_stmt (stmt);
36194 /* OpenMP 4.0:
36195 # pragma omp target data target-data-clause[optseq] new-line
36196 structured-block */
36198 #define OMP_TARGET_DATA_CLAUSE_MASK \
36199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36204 static tree
36205 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36207 tree clauses
36208 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36209 "#pragma omp target data", pragma_tok);
36210 int map_seen = 0;
36211 for (tree *pc = &clauses; *pc;)
36213 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36214 switch (OMP_CLAUSE_MAP_KIND (*pc))
36216 case GOMP_MAP_TO:
36217 case GOMP_MAP_ALWAYS_TO:
36218 case GOMP_MAP_FROM:
36219 case GOMP_MAP_ALWAYS_FROM:
36220 case GOMP_MAP_TOFROM:
36221 case GOMP_MAP_ALWAYS_TOFROM:
36222 case GOMP_MAP_ALLOC:
36223 map_seen = 3;
36224 break;
36225 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36226 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36227 case GOMP_MAP_ALWAYS_POINTER:
36228 break;
36229 default:
36230 map_seen |= 1;
36231 error_at (OMP_CLAUSE_LOCATION (*pc),
36232 "%<#pragma omp target data%> with map-type other "
36233 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36234 "on %<map%> clause");
36235 *pc = OMP_CLAUSE_CHAIN (*pc);
36236 continue;
36238 pc = &OMP_CLAUSE_CHAIN (*pc);
36241 if (map_seen != 3)
36243 if (map_seen == 0)
36244 error_at (pragma_tok->location,
36245 "%<#pragma omp target data%> must contain at least "
36246 "one %<map%> clause");
36247 return NULL_TREE;
36250 tree stmt = make_node (OMP_TARGET_DATA);
36251 TREE_TYPE (stmt) = void_type_node;
36252 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36254 keep_next_level (true);
36255 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36257 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36258 return add_stmt (stmt);
36261 /* OpenMP 4.5:
36262 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36263 structured-block */
36265 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36272 static tree
36273 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36274 enum pragma_context context)
36276 bool data_seen = false;
36277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36280 const char *p = IDENTIFIER_POINTER (id);
36282 if (strcmp (p, "data") == 0)
36284 cp_lexer_consume_token (parser->lexer);
36285 data_seen = true;
36288 if (!data_seen)
36290 cp_parser_error (parser, "expected %<data%>");
36291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36292 return NULL_TREE;
36295 if (context == pragma_stmt)
36297 error_at (pragma_tok->location,
36298 "%<#pragma %s%> may only be used in compound statements",
36299 "omp target enter data");
36300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36301 return NULL_TREE;
36304 tree clauses
36305 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36306 "#pragma omp target enter data", pragma_tok);
36307 int map_seen = 0;
36308 for (tree *pc = &clauses; *pc;)
36310 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36311 switch (OMP_CLAUSE_MAP_KIND (*pc))
36313 case GOMP_MAP_TO:
36314 case GOMP_MAP_ALWAYS_TO:
36315 case GOMP_MAP_ALLOC:
36316 map_seen = 3;
36317 break;
36318 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36319 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36320 case GOMP_MAP_ALWAYS_POINTER:
36321 break;
36322 default:
36323 map_seen |= 1;
36324 error_at (OMP_CLAUSE_LOCATION (*pc),
36325 "%<#pragma omp target enter data%> with map-type other "
36326 "than %<to%> or %<alloc%> on %<map%> clause");
36327 *pc = OMP_CLAUSE_CHAIN (*pc);
36328 continue;
36330 pc = &OMP_CLAUSE_CHAIN (*pc);
36333 if (map_seen != 3)
36335 if (map_seen == 0)
36336 error_at (pragma_tok->location,
36337 "%<#pragma omp target enter data%> must contain at least "
36338 "one %<map%> clause");
36339 return NULL_TREE;
36342 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36343 TREE_TYPE (stmt) = void_type_node;
36344 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36345 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36346 return add_stmt (stmt);
36349 /* OpenMP 4.5:
36350 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36351 structured-block */
36353 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36360 static tree
36361 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36362 enum pragma_context context)
36364 bool data_seen = false;
36365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36368 const char *p = IDENTIFIER_POINTER (id);
36370 if (strcmp (p, "data") == 0)
36372 cp_lexer_consume_token (parser->lexer);
36373 data_seen = true;
36376 if (!data_seen)
36378 cp_parser_error (parser, "expected %<data%>");
36379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36380 return NULL_TREE;
36383 if (context == pragma_stmt)
36385 error_at (pragma_tok->location,
36386 "%<#pragma %s%> may only be used in compound statements",
36387 "omp target exit data");
36388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36389 return NULL_TREE;
36392 tree clauses
36393 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36394 "#pragma omp target exit data", pragma_tok);
36395 int map_seen = 0;
36396 for (tree *pc = &clauses; *pc;)
36398 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36399 switch (OMP_CLAUSE_MAP_KIND (*pc))
36401 case GOMP_MAP_FROM:
36402 case GOMP_MAP_ALWAYS_FROM:
36403 case GOMP_MAP_RELEASE:
36404 case GOMP_MAP_DELETE:
36405 map_seen = 3;
36406 break;
36407 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36408 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36409 case GOMP_MAP_ALWAYS_POINTER:
36410 break;
36411 default:
36412 map_seen |= 1;
36413 error_at (OMP_CLAUSE_LOCATION (*pc),
36414 "%<#pragma omp target exit data%> with map-type other "
36415 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36416 " clause");
36417 *pc = OMP_CLAUSE_CHAIN (*pc);
36418 continue;
36420 pc = &OMP_CLAUSE_CHAIN (*pc);
36423 if (map_seen != 3)
36425 if (map_seen == 0)
36426 error_at (pragma_tok->location,
36427 "%<#pragma omp target exit data%> must contain at least "
36428 "one %<map%> clause");
36429 return NULL_TREE;
36432 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36433 TREE_TYPE (stmt) = void_type_node;
36434 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36435 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36436 return add_stmt (stmt);
36439 /* OpenMP 4.0:
36440 # pragma omp target update target-update-clause[optseq] new-line */
36442 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36450 static bool
36451 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36452 enum pragma_context context)
36454 if (context == pragma_stmt)
36456 error_at (pragma_tok->location,
36457 "%<#pragma %s%> may only be used in compound statements",
36458 "omp target update");
36459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36460 return false;
36463 tree clauses
36464 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36465 "#pragma omp target update", pragma_tok);
36466 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36467 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36469 error_at (pragma_tok->location,
36470 "%<#pragma omp target update%> must contain at least one "
36471 "%<from%> or %<to%> clauses");
36472 return false;
36475 tree stmt = make_node (OMP_TARGET_UPDATE);
36476 TREE_TYPE (stmt) = void_type_node;
36477 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36478 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36479 add_stmt (stmt);
36480 return false;
36483 /* OpenMP 4.0:
36484 # pragma omp target target-clause[optseq] new-line
36485 structured-block */
36487 #define OMP_TARGET_CLAUSE_MASK \
36488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36498 static bool
36499 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36500 enum pragma_context context, bool *if_p)
36502 tree *pc = NULL, stmt;
36504 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36507 const char *p = IDENTIFIER_POINTER (id);
36508 enum tree_code ccode = ERROR_MARK;
36510 if (strcmp (p, "teams") == 0)
36511 ccode = OMP_TEAMS;
36512 else if (strcmp (p, "parallel") == 0)
36513 ccode = OMP_PARALLEL;
36514 else if (strcmp (p, "simd") == 0)
36515 ccode = OMP_SIMD;
36516 if (ccode != ERROR_MARK)
36518 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36519 char p_name[sizeof ("#pragma omp target teams distribute "
36520 "parallel for simd")];
36522 cp_lexer_consume_token (parser->lexer);
36523 strcpy (p_name, "#pragma omp target");
36524 if (!flag_openmp) /* flag_openmp_simd */
36526 tree stmt;
36527 switch (ccode)
36529 case OMP_TEAMS:
36530 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36531 OMP_TARGET_CLAUSE_MASK,
36532 cclauses, if_p);
36533 break;
36534 case OMP_PARALLEL:
36535 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36536 OMP_TARGET_CLAUSE_MASK,
36537 cclauses, if_p);
36538 break;
36539 case OMP_SIMD:
36540 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36541 OMP_TARGET_CLAUSE_MASK,
36542 cclauses, if_p);
36543 break;
36544 default:
36545 gcc_unreachable ();
36547 return stmt != NULL_TREE;
36549 keep_next_level (true);
36550 tree sb = begin_omp_structured_block (), ret;
36551 unsigned save = cp_parser_begin_omp_structured_block (parser);
36552 switch (ccode)
36554 case OMP_TEAMS:
36555 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36556 OMP_TARGET_CLAUSE_MASK, cclauses,
36557 if_p);
36558 break;
36559 case OMP_PARALLEL:
36560 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36561 OMP_TARGET_CLAUSE_MASK, cclauses,
36562 if_p);
36563 break;
36564 case OMP_SIMD:
36565 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36566 OMP_TARGET_CLAUSE_MASK, cclauses,
36567 if_p);
36568 break;
36569 default:
36570 gcc_unreachable ();
36572 cp_parser_end_omp_structured_block (parser, save);
36573 tree body = finish_omp_structured_block (sb);
36574 if (ret == NULL_TREE)
36575 return false;
36576 if (ccode == OMP_TEAMS && !processing_template_decl)
36578 /* For combined target teams, ensure the num_teams and
36579 thread_limit clause expressions are evaluated on the host,
36580 before entering the target construct. */
36581 tree c;
36582 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36583 c; c = OMP_CLAUSE_CHAIN (c))
36584 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36585 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36586 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36588 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36589 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36590 if (expr == error_mark_node)
36591 continue;
36592 tree tmp = TARGET_EXPR_SLOT (expr);
36593 add_stmt (expr);
36594 OMP_CLAUSE_OPERAND (c, 0) = expr;
36595 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36596 OMP_CLAUSE_FIRSTPRIVATE);
36597 OMP_CLAUSE_DECL (tc) = tmp;
36598 OMP_CLAUSE_CHAIN (tc)
36599 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36600 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36603 tree stmt = make_node (OMP_TARGET);
36604 TREE_TYPE (stmt) = void_type_node;
36605 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36606 OMP_TARGET_BODY (stmt) = body;
36607 OMP_TARGET_COMBINED (stmt) = 1;
36608 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36609 add_stmt (stmt);
36610 pc = &OMP_TARGET_CLAUSES (stmt);
36611 goto check_clauses;
36613 else if (!flag_openmp) /* flag_openmp_simd */
36615 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36616 return false;
36618 else if (strcmp (p, "data") == 0)
36620 cp_lexer_consume_token (parser->lexer);
36621 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36622 return true;
36624 else if (strcmp (p, "enter") == 0)
36626 cp_lexer_consume_token (parser->lexer);
36627 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36628 return false;
36630 else if (strcmp (p, "exit") == 0)
36632 cp_lexer_consume_token (parser->lexer);
36633 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36634 return false;
36636 else if (strcmp (p, "update") == 0)
36638 cp_lexer_consume_token (parser->lexer);
36639 return cp_parser_omp_target_update (parser, pragma_tok, context);
36642 if (!flag_openmp) /* flag_openmp_simd */
36644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36645 return false;
36648 stmt = make_node (OMP_TARGET);
36649 TREE_TYPE (stmt) = void_type_node;
36651 OMP_TARGET_CLAUSES (stmt)
36652 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36653 "#pragma omp target", pragma_tok);
36654 pc = &OMP_TARGET_CLAUSES (stmt);
36655 keep_next_level (true);
36656 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36658 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36659 add_stmt (stmt);
36661 check_clauses:
36662 while (*pc)
36664 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36665 switch (OMP_CLAUSE_MAP_KIND (*pc))
36667 case GOMP_MAP_TO:
36668 case GOMP_MAP_ALWAYS_TO:
36669 case GOMP_MAP_FROM:
36670 case GOMP_MAP_ALWAYS_FROM:
36671 case GOMP_MAP_TOFROM:
36672 case GOMP_MAP_ALWAYS_TOFROM:
36673 case GOMP_MAP_ALLOC:
36674 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36675 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36676 case GOMP_MAP_ALWAYS_POINTER:
36677 break;
36678 default:
36679 error_at (OMP_CLAUSE_LOCATION (*pc),
36680 "%<#pragma omp target%> with map-type other "
36681 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36682 "on %<map%> clause");
36683 *pc = OMP_CLAUSE_CHAIN (*pc);
36684 continue;
36686 pc = &OMP_CLAUSE_CHAIN (*pc);
36688 return true;
36691 /* OpenACC 2.0:
36692 # pragma acc cache (variable-list) new-line
36695 static tree
36696 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36698 tree stmt, clauses;
36700 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36701 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36703 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36705 stmt = make_node (OACC_CACHE);
36706 TREE_TYPE (stmt) = void_type_node;
36707 OACC_CACHE_CLAUSES (stmt) = clauses;
36708 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36709 add_stmt (stmt);
36711 return stmt;
36714 /* OpenACC 2.0:
36715 # pragma acc data oacc-data-clause[optseq] new-line
36716 structured-block */
36718 #define OACC_DATA_CLAUSE_MASK \
36719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36731 static tree
36732 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36734 tree stmt, clauses, block;
36735 unsigned int save;
36737 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36738 "#pragma acc data", pragma_tok);
36740 block = begin_omp_parallel ();
36741 save = cp_parser_begin_omp_structured_block (parser);
36742 cp_parser_statement (parser, NULL_TREE, false, if_p);
36743 cp_parser_end_omp_structured_block (parser, save);
36744 stmt = finish_oacc_data (clauses, block);
36745 return stmt;
36748 /* OpenACC 2.0:
36749 # pragma acc host_data <clauses> new-line
36750 structured-block */
36752 #define OACC_HOST_DATA_CLAUSE_MASK \
36753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36755 static tree
36756 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36758 tree stmt, clauses, block;
36759 unsigned int save;
36761 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36762 "#pragma acc host_data", pragma_tok);
36764 block = begin_omp_parallel ();
36765 save = cp_parser_begin_omp_structured_block (parser);
36766 cp_parser_statement (parser, NULL_TREE, false, if_p);
36767 cp_parser_end_omp_structured_block (parser, save);
36768 stmt = finish_oacc_host_data (clauses, block);
36769 return stmt;
36772 /* OpenACC 2.0:
36773 # pragma acc declare oacc-data-clause[optseq] new-line
36776 #define OACC_DECLARE_CLAUSE_MASK \
36777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36790 static tree
36791 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36793 tree clauses, stmt;
36794 bool error = false;
36796 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36797 "#pragma acc declare", pragma_tok, true);
36800 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36802 error_at (pragma_tok->location,
36803 "no valid clauses specified in %<#pragma acc declare%>");
36804 return NULL_TREE;
36807 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36809 location_t loc = OMP_CLAUSE_LOCATION (t);
36810 tree decl = OMP_CLAUSE_DECL (t);
36811 if (!DECL_P (decl))
36813 error_at (loc, "array section in %<#pragma acc declare%>");
36814 error = true;
36815 continue;
36817 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36818 switch (OMP_CLAUSE_MAP_KIND (t))
36820 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36821 case GOMP_MAP_FORCE_ALLOC:
36822 case GOMP_MAP_FORCE_TO:
36823 case GOMP_MAP_FORCE_DEVICEPTR:
36824 case GOMP_MAP_DEVICE_RESIDENT:
36825 break;
36827 case GOMP_MAP_LINK:
36828 if (!global_bindings_p ()
36829 && (TREE_STATIC (decl)
36830 || !DECL_EXTERNAL (decl)))
36832 error_at (loc,
36833 "%qD must be a global variable in "
36834 "%<#pragma acc declare link%>",
36835 decl);
36836 error = true;
36837 continue;
36839 break;
36841 default:
36842 if (global_bindings_p ())
36844 error_at (loc, "invalid OpenACC clause at file scope");
36845 error = true;
36846 continue;
36848 if (DECL_EXTERNAL (decl))
36850 error_at (loc,
36851 "invalid use of %<extern%> variable %qD "
36852 "in %<#pragma acc declare%>", decl);
36853 error = true;
36854 continue;
36856 else if (TREE_PUBLIC (decl))
36858 error_at (loc,
36859 "invalid use of %<global%> variable %qD "
36860 "in %<#pragma acc declare%>", decl);
36861 error = true;
36862 continue;
36864 break;
36867 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36868 || lookup_attribute ("omp declare target link",
36869 DECL_ATTRIBUTES (decl)))
36871 error_at (loc, "variable %qD used more than once with "
36872 "%<#pragma acc declare%>", decl);
36873 error = true;
36874 continue;
36877 if (!error)
36879 tree id;
36881 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36882 id = get_identifier ("omp declare target link");
36883 else
36884 id = get_identifier ("omp declare target");
36886 DECL_ATTRIBUTES (decl)
36887 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36888 if (global_bindings_p ())
36890 symtab_node *node = symtab_node::get (decl);
36891 if (node != NULL)
36893 node->offloadable = 1;
36894 if (ENABLE_OFFLOADING)
36896 g->have_offload = true;
36897 if (is_a <varpool_node *> (node))
36898 vec_safe_push (offload_vars, decl);
36905 if (error || global_bindings_p ())
36906 return NULL_TREE;
36908 stmt = make_node (OACC_DECLARE);
36909 TREE_TYPE (stmt) = void_type_node;
36910 OACC_DECLARE_CLAUSES (stmt) = clauses;
36911 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36913 add_stmt (stmt);
36915 return NULL_TREE;
36918 /* OpenACC 2.0:
36919 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36923 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36925 LOC is the location of the #pragma token.
36928 #define OACC_ENTER_DATA_CLAUSE_MASK \
36929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36937 #define OACC_EXIT_DATA_CLAUSE_MASK \
36938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36944 static tree
36945 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36946 bool enter)
36948 location_t loc = pragma_tok->location;
36949 tree stmt, clauses;
36950 const char *p = "";
36952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36953 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36955 if (strcmp (p, "data") != 0)
36957 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36958 enter ? "enter" : "exit");
36959 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36960 return NULL_TREE;
36963 cp_lexer_consume_token (parser->lexer);
36965 if (enter)
36966 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36967 "#pragma acc enter data", pragma_tok);
36968 else
36969 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36970 "#pragma acc exit data", pragma_tok);
36972 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36974 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36975 enter ? "enter" : "exit");
36976 return NULL_TREE;
36979 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36980 TREE_TYPE (stmt) = void_type_node;
36981 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36982 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36983 add_stmt (stmt);
36984 return stmt;
36987 /* OpenACC 2.0:
36988 # pragma acc loop oacc-loop-clause[optseq] new-line
36989 structured-block */
36991 #define OACC_LOOP_CLAUSE_MASK \
36992 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37003 static tree
37004 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37005 omp_clause_mask mask, tree *cclauses, bool *if_p)
37007 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37009 strcat (p_name, " loop");
37010 mask |= OACC_LOOP_CLAUSE_MASK;
37012 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37013 cclauses == NULL);
37014 if (cclauses)
37016 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37017 if (*cclauses)
37018 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37019 if (clauses)
37020 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37023 tree block = begin_omp_structured_block ();
37024 int save = cp_parser_begin_omp_structured_block (parser);
37025 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37026 cp_parser_end_omp_structured_block (parser, save);
37027 add_stmt (finish_omp_structured_block (block));
37029 return stmt;
37032 /* OpenACC 2.0:
37033 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37034 structured-block
37038 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37039 structured-block
37042 #define OACC_KERNELS_CLAUSE_MASK \
37043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37061 #define OACC_PARALLEL_CLAUSE_MASK \
37062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37083 static tree
37084 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37085 char *p_name, bool *if_p)
37087 omp_clause_mask mask;
37088 enum tree_code code;
37089 switch (cp_parser_pragma_kind (pragma_tok))
37091 case PRAGMA_OACC_KERNELS:
37092 strcat (p_name, " kernels");
37093 mask = OACC_KERNELS_CLAUSE_MASK;
37094 code = OACC_KERNELS;
37095 break;
37096 case PRAGMA_OACC_PARALLEL:
37097 strcat (p_name, " parallel");
37098 mask = OACC_PARALLEL_CLAUSE_MASK;
37099 code = OACC_PARALLEL;
37100 break;
37101 default:
37102 gcc_unreachable ();
37105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37107 const char *p
37108 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37109 if (strcmp (p, "loop") == 0)
37111 cp_lexer_consume_token (parser->lexer);
37112 tree block = begin_omp_parallel ();
37113 tree clauses;
37114 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37115 if_p);
37116 return finish_omp_construct (code, block, clauses);
37120 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37122 tree block = begin_omp_parallel ();
37123 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37124 cp_parser_statement (parser, NULL_TREE, false, if_p);
37125 cp_parser_end_omp_structured_block (parser, save);
37126 return finish_omp_construct (code, block, clauses);
37129 /* OpenACC 2.0:
37130 # pragma acc update oacc-update-clause[optseq] new-line
37133 #define OACC_UPDATE_CLAUSE_MASK \
37134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37141 static tree
37142 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37144 tree stmt, clauses;
37146 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37147 "#pragma acc update", pragma_tok);
37149 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37151 error_at (pragma_tok->location,
37152 "%<#pragma acc update%> must contain at least one "
37153 "%<device%> or %<host%> or %<self%> clause");
37154 return NULL_TREE;
37157 stmt = make_node (OACC_UPDATE);
37158 TREE_TYPE (stmt) = void_type_node;
37159 OACC_UPDATE_CLAUSES (stmt) = clauses;
37160 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37161 add_stmt (stmt);
37162 return stmt;
37165 /* OpenACC 2.0:
37166 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37168 LOC is the location of the #pragma token.
37171 #define OACC_WAIT_CLAUSE_MASK \
37172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37174 static tree
37175 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37177 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37178 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37180 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37181 list = cp_parser_oacc_wait_list (parser, loc, list);
37183 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37184 "#pragma acc wait", pragma_tok);
37186 stmt = c_finish_oacc_wait (loc, list, clauses);
37187 stmt = finish_expr_stmt (stmt);
37189 return stmt;
37192 /* OpenMP 4.0:
37193 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37195 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37196 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37203 static void
37204 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37205 enum pragma_context context)
37207 bool first_p = parser->omp_declare_simd == NULL;
37208 cp_omp_declare_simd_data data;
37209 if (first_p)
37211 data.error_seen = false;
37212 data.fndecl_seen = false;
37213 data.tokens = vNULL;
37214 data.clauses = NULL_TREE;
37215 /* It is safe to take the address of a local variable; it will only be
37216 used while this scope is live. */
37217 parser->omp_declare_simd = &data;
37220 /* Store away all pragma tokens. */
37221 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37222 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37223 cp_lexer_consume_token (parser->lexer);
37224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37225 parser->omp_declare_simd->error_seen = true;
37226 cp_parser_require_pragma_eol (parser, pragma_tok);
37227 struct cp_token_cache *cp
37228 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37229 parser->omp_declare_simd->tokens.safe_push (cp);
37231 if (first_p)
37233 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37234 cp_parser_pragma (parser, context, NULL);
37235 switch (context)
37237 case pragma_external:
37238 cp_parser_declaration (parser);
37239 break;
37240 case pragma_member:
37241 cp_parser_member_declaration (parser);
37242 break;
37243 case pragma_objc_icode:
37244 cp_parser_block_declaration (parser, /*statement_p=*/false);
37245 break;
37246 default:
37247 cp_parser_declaration_statement (parser);
37248 break;
37250 if (parser->omp_declare_simd
37251 && !parser->omp_declare_simd->error_seen
37252 && !parser->omp_declare_simd->fndecl_seen)
37253 error_at (pragma_tok->location,
37254 "%<#pragma omp declare simd%> not immediately followed by "
37255 "function declaration or definition");
37256 data.tokens.release ();
37257 parser->omp_declare_simd = NULL;
37261 /* Finalize #pragma omp declare simd clauses after direct declarator has
37262 been parsed, and put that into "omp declare simd" attribute. */
37264 static tree
37265 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37267 struct cp_token_cache *ce;
37268 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37269 int i;
37271 if (!data->error_seen && data->fndecl_seen)
37273 error ("%<#pragma omp declare simd%> not immediately followed by "
37274 "a single function declaration or definition");
37275 data->error_seen = true;
37277 if (data->error_seen)
37278 return attrs;
37280 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37282 tree c, cl;
37284 cp_parser_push_lexer_for_tokens (parser, ce);
37285 parser->lexer->in_pragma = true;
37286 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37287 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37288 cp_lexer_consume_token (parser->lexer);
37289 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37290 "#pragma omp declare simd", pragma_tok);
37291 cp_parser_pop_lexer (parser);
37292 if (cl)
37293 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37294 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37295 TREE_CHAIN (c) = attrs;
37296 if (processing_template_decl)
37297 ATTR_IS_DEPENDENT (c) = 1;
37298 attrs = c;
37301 data->fndecl_seen = true;
37302 return attrs;
37306 /* OpenMP 4.0:
37307 # pragma omp declare target new-line
37308 declarations and definitions
37309 # pragma omp end declare target new-line
37311 OpenMP 4.5:
37312 # pragma omp declare target ( extended-list ) new-line
37314 # pragma omp declare target declare-target-clauses[seq] new-line */
37316 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37320 static void
37321 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37323 tree clauses = NULL_TREE;
37324 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37325 clauses
37326 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37327 "#pragma omp declare target", pragma_tok);
37328 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37331 clauses);
37332 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37333 cp_parser_require_pragma_eol (parser, pragma_tok);
37335 else
37337 cp_parser_require_pragma_eol (parser, pragma_tok);
37338 scope_chain->omp_declare_target_attribute++;
37339 return;
37341 if (scope_chain->omp_declare_target_attribute)
37342 error_at (pragma_tok->location,
37343 "%<#pragma omp declare target%> with clauses in between "
37344 "%<#pragma omp declare target%> without clauses and "
37345 "%<#pragma omp end declare target%>");
37346 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37348 tree t = OMP_CLAUSE_DECL (c), id;
37349 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37350 tree at2 = lookup_attribute ("omp declare target link",
37351 DECL_ATTRIBUTES (t));
37352 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37354 id = get_identifier ("omp declare target link");
37355 std::swap (at1, at2);
37357 else
37358 id = get_identifier ("omp declare target");
37359 if (at2)
37361 error_at (OMP_CLAUSE_LOCATION (c),
37362 "%qD specified both in declare target %<link%> and %<to%>"
37363 " clauses", t);
37364 continue;
37366 if (!at1)
37368 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37369 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37370 continue;
37372 symtab_node *node = symtab_node::get (t);
37373 if (node != NULL)
37375 node->offloadable = 1;
37376 if (ENABLE_OFFLOADING)
37378 g->have_offload = true;
37379 if (is_a <varpool_node *> (node))
37380 vec_safe_push (offload_vars, t);
37387 static void
37388 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37390 const char *p = "";
37391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37393 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37394 p = IDENTIFIER_POINTER (id);
37396 if (strcmp (p, "declare") == 0)
37398 cp_lexer_consume_token (parser->lexer);
37399 p = "";
37400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37402 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37403 p = IDENTIFIER_POINTER (id);
37405 if (strcmp (p, "target") == 0)
37406 cp_lexer_consume_token (parser->lexer);
37407 else
37409 cp_parser_error (parser, "expected %<target%>");
37410 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37411 return;
37414 else
37416 cp_parser_error (parser, "expected %<declare%>");
37417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37418 return;
37420 cp_parser_require_pragma_eol (parser, pragma_tok);
37421 if (!scope_chain->omp_declare_target_attribute)
37422 error_at (pragma_tok->location,
37423 "%<#pragma omp end declare target%> without corresponding "
37424 "%<#pragma omp declare target%>");
37425 else
37426 scope_chain->omp_declare_target_attribute--;
37429 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37430 expression and optional initializer clause of
37431 #pragma omp declare reduction. We store the expression(s) as
37432 either 3, 6 or 7 special statements inside of the artificial function's
37433 body. The first two statements are DECL_EXPRs for the artificial
37434 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37435 expression that uses those variables.
37436 If there was any INITIALIZER clause, this is followed by further statements,
37437 the fourth and fifth statements are DECL_EXPRs for the artificial
37438 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37439 constructor variant (first token after open paren is not omp_priv),
37440 then the sixth statement is a statement with the function call expression
37441 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37442 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37443 to initialize the OMP_PRIV artificial variable and there is seventh
37444 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37446 static bool
37447 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37449 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37450 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37451 type = TREE_TYPE (type);
37452 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37453 DECL_ARTIFICIAL (omp_out) = 1;
37454 pushdecl (omp_out);
37455 add_decl_expr (omp_out);
37456 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37457 DECL_ARTIFICIAL (omp_in) = 1;
37458 pushdecl (omp_in);
37459 add_decl_expr (omp_in);
37460 tree combiner;
37461 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37463 keep_next_level (true);
37464 tree block = begin_omp_structured_block ();
37465 combiner = cp_parser_expression (parser);
37466 finish_expr_stmt (combiner);
37467 block = finish_omp_structured_block (block);
37468 add_stmt (block);
37470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37471 return false;
37473 const char *p = "";
37474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37476 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37477 p = IDENTIFIER_POINTER (id);
37480 if (strcmp (p, "initializer") == 0)
37482 cp_lexer_consume_token (parser->lexer);
37483 matching_parens parens;
37484 if (!parens.require_open (parser))
37485 return false;
37487 p = "";
37488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37490 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37491 p = IDENTIFIER_POINTER (id);
37494 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37495 DECL_ARTIFICIAL (omp_priv) = 1;
37496 pushdecl (omp_priv);
37497 add_decl_expr (omp_priv);
37498 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37499 DECL_ARTIFICIAL (omp_orig) = 1;
37500 pushdecl (omp_orig);
37501 add_decl_expr (omp_orig);
37503 keep_next_level (true);
37504 block = begin_omp_structured_block ();
37506 bool ctor = false;
37507 if (strcmp (p, "omp_priv") == 0)
37509 bool is_direct_init, is_non_constant_init;
37510 ctor = true;
37511 cp_lexer_consume_token (parser->lexer);
37512 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37513 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37514 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37515 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37516 == CPP_CLOSE_PAREN
37517 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37518 == CPP_CLOSE_PAREN))
37520 finish_omp_structured_block (block);
37521 error ("invalid initializer clause");
37522 return false;
37524 initializer = cp_parser_initializer (parser, &is_direct_init,
37525 &is_non_constant_init);
37526 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37527 NULL_TREE, LOOKUP_ONLYCONVERTING);
37529 else
37531 cp_parser_parse_tentatively (parser);
37532 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37533 /*check_dependency_p=*/true,
37534 /*template_p=*/NULL,
37535 /*declarator_p=*/false,
37536 /*optional_p=*/false);
37537 vec<tree, va_gc> *args;
37538 if (fn_name == error_mark_node
37539 || cp_parser_error_occurred (parser)
37540 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37541 || ((args = cp_parser_parenthesized_expression_list
37542 (parser, non_attr, /*cast_p=*/false,
37543 /*allow_expansion_p=*/true,
37544 /*non_constant_p=*/NULL)),
37545 cp_parser_error_occurred (parser)))
37547 finish_omp_structured_block (block);
37548 cp_parser_abort_tentative_parse (parser);
37549 cp_parser_error (parser, "expected id-expression (arguments)");
37550 return false;
37552 unsigned int i;
37553 tree arg;
37554 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37555 if (arg == omp_priv
37556 || (TREE_CODE (arg) == ADDR_EXPR
37557 && TREE_OPERAND (arg, 0) == omp_priv))
37558 break;
37559 cp_parser_abort_tentative_parse (parser);
37560 if (arg == NULL_TREE)
37561 error ("one of the initializer call arguments should be %<omp_priv%>"
37562 " or %<&omp_priv%>");
37563 initializer = cp_parser_postfix_expression (parser, false, false, false,
37564 false, NULL);
37565 finish_expr_stmt (initializer);
37568 block = finish_omp_structured_block (block);
37569 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37570 add_stmt (block);
37572 if (ctor)
37573 add_decl_expr (omp_orig);
37575 if (!parens.require_close (parser))
37576 return false;
37579 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37580 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37581 UNKNOWN_LOCATION);
37583 return true;
37586 /* OpenMP 4.0
37587 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37588 initializer-clause[opt] new-line
37590 initializer-clause:
37591 initializer (omp_priv initializer)
37592 initializer (function-name (argument-list)) */
37594 static void
37595 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37596 enum pragma_context)
37598 auto_vec<tree> types;
37599 enum tree_code reduc_code = ERROR_MARK;
37600 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37601 unsigned int i;
37602 cp_token *first_token;
37603 cp_token_cache *cp;
37604 int errs;
37605 void *p;
37607 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37608 p = obstack_alloc (&declarator_obstack, 0);
37610 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37611 goto fail;
37613 switch (cp_lexer_peek_token (parser->lexer)->type)
37615 case CPP_PLUS:
37616 reduc_code = PLUS_EXPR;
37617 break;
37618 case CPP_MULT:
37619 reduc_code = MULT_EXPR;
37620 break;
37621 case CPP_MINUS:
37622 reduc_code = MINUS_EXPR;
37623 break;
37624 case CPP_AND:
37625 reduc_code = BIT_AND_EXPR;
37626 break;
37627 case CPP_XOR:
37628 reduc_code = BIT_XOR_EXPR;
37629 break;
37630 case CPP_OR:
37631 reduc_code = BIT_IOR_EXPR;
37632 break;
37633 case CPP_AND_AND:
37634 reduc_code = TRUTH_ANDIF_EXPR;
37635 break;
37636 case CPP_OR_OR:
37637 reduc_code = TRUTH_ORIF_EXPR;
37638 break;
37639 case CPP_NAME:
37640 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37641 break;
37642 default:
37643 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37644 "%<|%>, %<&&%>, %<||%> or identifier");
37645 goto fail;
37648 if (reduc_code != ERROR_MARK)
37649 cp_lexer_consume_token (parser->lexer);
37651 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37652 if (reduc_id == error_mark_node)
37653 goto fail;
37655 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37656 goto fail;
37658 /* Types may not be defined in declare reduction type list. */
37659 const char *saved_message;
37660 saved_message = parser->type_definition_forbidden_message;
37661 parser->type_definition_forbidden_message
37662 = G_("types may not be defined in declare reduction type list");
37663 bool saved_colon_corrects_to_scope_p;
37664 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37665 parser->colon_corrects_to_scope_p = false;
37666 bool saved_colon_doesnt_start_class_def_p;
37667 saved_colon_doesnt_start_class_def_p
37668 = parser->colon_doesnt_start_class_def_p;
37669 parser->colon_doesnt_start_class_def_p = true;
37671 while (true)
37673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37674 type = cp_parser_type_id (parser);
37675 if (type == error_mark_node)
37677 else if (ARITHMETIC_TYPE_P (type)
37678 && (orig_reduc_id == NULL_TREE
37679 || (TREE_CODE (type) != COMPLEX_TYPE
37680 && (id_equal (orig_reduc_id, "min")
37681 || id_equal (orig_reduc_id, "max")))))
37682 error_at (loc, "predeclared arithmetic type %qT in "
37683 "%<#pragma omp declare reduction%>", type);
37684 else if (TREE_CODE (type) == FUNCTION_TYPE
37685 || TREE_CODE (type) == METHOD_TYPE
37686 || TREE_CODE (type) == ARRAY_TYPE)
37687 error_at (loc, "function or array type %qT in "
37688 "%<#pragma omp declare reduction%>", type);
37689 else if (TREE_CODE (type) == REFERENCE_TYPE)
37690 error_at (loc, "reference type %qT in "
37691 "%<#pragma omp declare reduction%>", type);
37692 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37693 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37694 "%<#pragma omp declare reduction%>", type);
37695 else
37696 types.safe_push (type);
37698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37699 cp_lexer_consume_token (parser->lexer);
37700 else
37701 break;
37704 /* Restore the saved message. */
37705 parser->type_definition_forbidden_message = saved_message;
37706 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37707 parser->colon_doesnt_start_class_def_p
37708 = saved_colon_doesnt_start_class_def_p;
37710 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37711 || types.is_empty ())
37713 fail:
37714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37715 goto done;
37718 first_token = cp_lexer_peek_token (parser->lexer);
37719 cp = NULL;
37720 errs = errorcount;
37721 FOR_EACH_VEC_ELT (types, i, type)
37723 tree fntype
37724 = build_function_type_list (void_type_node,
37725 cp_build_reference_type (type, false),
37726 NULL_TREE);
37727 tree this_reduc_id = reduc_id;
37728 if (!dependent_type_p (type))
37729 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37730 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37731 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37732 DECL_ARTIFICIAL (fndecl) = 1;
37733 DECL_EXTERNAL (fndecl) = 1;
37734 DECL_DECLARED_INLINE_P (fndecl) = 1;
37735 DECL_IGNORED_P (fndecl) = 1;
37736 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37737 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37738 DECL_ATTRIBUTES (fndecl)
37739 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37740 DECL_ATTRIBUTES (fndecl));
37741 if (processing_template_decl)
37742 fndecl = push_template_decl (fndecl);
37743 bool block_scope = false;
37744 tree block = NULL_TREE;
37745 if (current_function_decl)
37747 block_scope = true;
37748 DECL_CONTEXT (fndecl) = global_namespace;
37749 if (!processing_template_decl)
37750 pushdecl (fndecl);
37752 else if (current_class_type)
37754 if (cp == NULL)
37756 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37757 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37758 cp_lexer_consume_token (parser->lexer);
37759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37760 goto fail;
37761 cp = cp_token_cache_new (first_token,
37762 cp_lexer_peek_nth_token (parser->lexer,
37763 2));
37765 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37766 finish_member_declaration (fndecl);
37767 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37768 DECL_PENDING_INLINE_P (fndecl) = 1;
37769 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37770 continue;
37772 else
37774 DECL_CONTEXT (fndecl) = current_namespace;
37775 pushdecl (fndecl);
37777 if (!block_scope)
37778 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37779 else
37780 block = begin_omp_structured_block ();
37781 if (cp)
37783 cp_parser_push_lexer_for_tokens (parser, cp);
37784 parser->lexer->in_pragma = true;
37786 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37788 if (!block_scope)
37789 finish_function (/*inline_p=*/false);
37790 else
37791 DECL_CONTEXT (fndecl) = current_function_decl;
37792 if (cp)
37793 cp_parser_pop_lexer (parser);
37794 goto fail;
37796 if (cp)
37797 cp_parser_pop_lexer (parser);
37798 if (!block_scope)
37799 finish_function (/*inline_p=*/false);
37800 else
37802 DECL_CONTEXT (fndecl) = current_function_decl;
37803 block = finish_omp_structured_block (block);
37804 if (TREE_CODE (block) == BIND_EXPR)
37805 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37806 else if (TREE_CODE (block) == STATEMENT_LIST)
37807 DECL_SAVED_TREE (fndecl) = block;
37808 if (processing_template_decl)
37809 add_decl_expr (fndecl);
37811 cp_check_omp_declare_reduction (fndecl);
37812 if (cp == NULL && types.length () > 1)
37813 cp = cp_token_cache_new (first_token,
37814 cp_lexer_peek_nth_token (parser->lexer, 2));
37815 if (errs != errorcount)
37816 break;
37819 cp_parser_require_pragma_eol (parser, pragma_tok);
37821 done:
37822 /* Free any declarators allocated. */
37823 obstack_free (&declarator_obstack, p);
37826 /* OpenMP 4.0
37827 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37828 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37829 initializer-clause[opt] new-line
37830 #pragma omp declare target new-line */
37832 static bool
37833 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37834 enum pragma_context context)
37836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37839 const char *p = IDENTIFIER_POINTER (id);
37841 if (strcmp (p, "simd") == 0)
37843 cp_lexer_consume_token (parser->lexer);
37844 cp_parser_omp_declare_simd (parser, pragma_tok,
37845 context);
37846 return true;
37848 cp_ensure_no_omp_declare_simd (parser);
37849 if (strcmp (p, "reduction") == 0)
37851 cp_lexer_consume_token (parser->lexer);
37852 cp_parser_omp_declare_reduction (parser, pragma_tok,
37853 context);
37854 return false;
37856 if (!flag_openmp) /* flag_openmp_simd */
37858 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37859 return false;
37861 if (strcmp (p, "target") == 0)
37863 cp_lexer_consume_token (parser->lexer);
37864 cp_parser_omp_declare_target (parser, pragma_tok);
37865 return false;
37868 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37869 "or %<target%>");
37870 cp_parser_require_pragma_eol (parser, pragma_tok);
37871 return false;
37874 /* OpenMP 4.5:
37875 #pragma omp taskloop taskloop-clause[optseq] new-line
37876 for-loop
37878 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37879 for-loop */
37881 #define OMP_TASKLOOP_CLAUSE_MASK \
37882 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37897 static tree
37898 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37899 char *p_name, omp_clause_mask mask, tree *cclauses,
37900 bool *if_p)
37902 tree clauses, sb, ret;
37903 unsigned int save;
37904 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37906 strcat (p_name, " taskloop");
37907 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37911 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37912 const char *p = IDENTIFIER_POINTER (id);
37914 if (strcmp (p, "simd") == 0)
37916 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37917 if (cclauses == NULL)
37918 cclauses = cclauses_buf;
37920 cp_lexer_consume_token (parser->lexer);
37921 if (!flag_openmp) /* flag_openmp_simd */
37922 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37923 cclauses, if_p);
37924 sb = begin_omp_structured_block ();
37925 save = cp_parser_begin_omp_structured_block (parser);
37926 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37927 cclauses, if_p);
37928 cp_parser_end_omp_structured_block (parser, save);
37929 tree body = finish_omp_structured_block (sb);
37930 if (ret == NULL)
37931 return ret;
37932 ret = make_node (OMP_TASKLOOP);
37933 TREE_TYPE (ret) = void_type_node;
37934 OMP_FOR_BODY (ret) = body;
37935 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37936 SET_EXPR_LOCATION (ret, loc);
37937 add_stmt (ret);
37938 return ret;
37941 if (!flag_openmp) /* flag_openmp_simd */
37943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37944 return NULL_TREE;
37947 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37948 cclauses == NULL);
37949 if (cclauses)
37951 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37952 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37955 sb = begin_omp_structured_block ();
37956 save = cp_parser_begin_omp_structured_block (parser);
37958 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37959 if_p);
37961 cp_parser_end_omp_structured_block (parser, save);
37962 add_stmt (finish_omp_structured_block (sb));
37964 return ret;
37968 /* OpenACC 2.0:
37969 # pragma acc routine oacc-routine-clause[optseq] new-line
37970 function-definition
37972 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37975 #define OACC_ROUTINE_CLAUSE_MASK \
37976 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37982 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37983 component, which must resolve to a declared namespace-scope
37984 function. The clauses are either processed directly (for a named
37985 function), or defered until the immediatley following declaration
37986 is parsed. */
37988 static void
37989 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37990 enum pragma_context context)
37992 gcc_checking_assert (context == pragma_external);
37993 /* The checking for "another pragma following this one" in the "no optional
37994 '( name )'" case makes sure that we dont re-enter. */
37995 gcc_checking_assert (parser->oacc_routine == NULL);
37997 cp_oacc_routine_data data;
37998 data.error_seen = false;
37999 data.fndecl_seen = false;
38000 data.tokens = vNULL;
38001 data.clauses = NULL_TREE;
38002 data.loc = pragma_tok->location;
38003 /* It is safe to take the address of a local variable; it will only be
38004 used while this scope is live. */
38005 parser->oacc_routine = &data;
38007 /* Look for optional '( name )'. */
38008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38010 matching_parens parens;
38011 parens.consume_open (parser); /* '(' */
38013 /* We parse the name as an id-expression. If it resolves to
38014 anything other than a non-overloaded function at namespace
38015 scope, it's an error. */
38016 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38017 tree name = cp_parser_id_expression (parser,
38018 /*template_keyword_p=*/false,
38019 /*check_dependency_p=*/false,
38020 /*template_p=*/NULL,
38021 /*declarator_p=*/false,
38022 /*optional_p=*/false);
38023 tree decl = (identifier_p (name)
38024 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38025 : name);
38026 if (name != error_mark_node && decl == error_mark_node)
38027 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38029 if (decl == error_mark_node
38030 || !parens.require_close (parser))
38032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38033 parser->oacc_routine = NULL;
38034 return;
38037 data.clauses
38038 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38039 "#pragma acc routine",
38040 cp_lexer_peek_token (parser->lexer));
38042 if (decl && is_overloaded_fn (decl)
38043 && (TREE_CODE (decl) != FUNCTION_DECL
38044 || DECL_FUNCTION_TEMPLATE_P (decl)))
38046 error_at (name_loc,
38047 "%<#pragma acc routine%> names a set of overloads");
38048 parser->oacc_routine = NULL;
38049 return;
38052 /* Perhaps we should use the same rule as declarations in different
38053 namespaces? */
38054 if (!DECL_NAMESPACE_SCOPE_P (decl))
38056 error_at (name_loc,
38057 "%qD does not refer to a namespace scope function", decl);
38058 parser->oacc_routine = NULL;
38059 return;
38062 if (TREE_CODE (decl) != FUNCTION_DECL)
38064 error_at (name_loc, "%qD does not refer to a function", decl);
38065 parser->oacc_routine = NULL;
38066 return;
38069 cp_finalize_oacc_routine (parser, decl, false);
38070 parser->oacc_routine = NULL;
38072 else /* No optional '( name )'. */
38074 /* Store away all pragma tokens. */
38075 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38076 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38077 cp_lexer_consume_token (parser->lexer);
38078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38079 parser->oacc_routine->error_seen = true;
38080 cp_parser_require_pragma_eol (parser, pragma_tok);
38081 struct cp_token_cache *cp
38082 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38083 parser->oacc_routine->tokens.safe_push (cp);
38085 /* Emit a helpful diagnostic if there's another pragma following this
38086 one. */
38087 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38089 cp_ensure_no_oacc_routine (parser);
38090 data.tokens.release ();
38091 /* ..., and then just keep going. */
38092 return;
38095 /* We only have to consider the pragma_external case here. */
38096 cp_parser_declaration (parser);
38097 if (parser->oacc_routine
38098 && !parser->oacc_routine->fndecl_seen)
38099 cp_ensure_no_oacc_routine (parser);
38100 else
38101 parser->oacc_routine = NULL;
38102 data.tokens.release ();
38106 /* Finalize #pragma acc routine clauses after direct declarator has
38107 been parsed. */
38109 static tree
38110 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38112 struct cp_token_cache *ce;
38113 cp_oacc_routine_data *data = parser->oacc_routine;
38115 if (!data->error_seen && data->fndecl_seen)
38117 error_at (data->loc,
38118 "%<#pragma acc routine%> not immediately followed by "
38119 "a single function declaration or definition");
38120 data->error_seen = true;
38122 if (data->error_seen)
38123 return attrs;
38125 gcc_checking_assert (data->tokens.length () == 1);
38126 ce = data->tokens[0];
38128 cp_parser_push_lexer_for_tokens (parser, ce);
38129 parser->lexer->in_pragma = true;
38130 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38132 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38133 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38134 parser->oacc_routine->clauses
38135 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38136 "#pragma acc routine", pragma_tok);
38137 cp_parser_pop_lexer (parser);
38138 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38139 fndecl_seen. */
38141 return attrs;
38144 /* Apply any saved OpenACC routine clauses to a just-parsed
38145 declaration. */
38147 static void
38148 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38150 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38152 /* Keep going if we're in error reporting mode. */
38153 if (parser->oacc_routine->error_seen
38154 || fndecl == error_mark_node)
38155 return;
38157 if (parser->oacc_routine->fndecl_seen)
38159 error_at (parser->oacc_routine->loc,
38160 "%<#pragma acc routine%> not immediately followed by"
38161 " a single function declaration or definition");
38162 parser->oacc_routine = NULL;
38163 return;
38165 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38167 cp_ensure_no_oacc_routine (parser);
38168 return;
38171 if (oacc_get_fn_attrib (fndecl))
38173 error_at (parser->oacc_routine->loc,
38174 "%<#pragma acc routine%> already applied to %qD", fndecl);
38175 parser->oacc_routine = NULL;
38176 return;
38179 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38181 error_at (parser->oacc_routine->loc,
38182 TREE_USED (fndecl)
38183 ? G_("%<#pragma acc routine%> must be applied before use")
38184 : G_("%<#pragma acc routine%> must be applied before "
38185 "definition"));
38186 parser->oacc_routine = NULL;
38187 return;
38190 /* Process the routine's dimension clauses. */
38191 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38192 oacc_replace_fn_attrib (fndecl, dims);
38194 /* Add an "omp declare target" attribute. */
38195 DECL_ATTRIBUTES (fndecl)
38196 = tree_cons (get_identifier ("omp declare target"),
38197 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38199 /* Don't unset parser->oacc_routine here: we may still need it to
38200 diagnose wrong usage. But, remember that we've used this "#pragma acc
38201 routine". */
38202 parser->oacc_routine->fndecl_seen = true;
38206 /* Main entry point to OpenMP statement pragmas. */
38208 static void
38209 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38211 tree stmt;
38212 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38213 omp_clause_mask mask (0);
38215 switch (cp_parser_pragma_kind (pragma_tok))
38217 case PRAGMA_OACC_ATOMIC:
38218 cp_parser_omp_atomic (parser, pragma_tok);
38219 return;
38220 case PRAGMA_OACC_CACHE:
38221 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38222 break;
38223 case PRAGMA_OACC_DATA:
38224 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38225 break;
38226 case PRAGMA_OACC_ENTER_DATA:
38227 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38228 break;
38229 case PRAGMA_OACC_EXIT_DATA:
38230 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38231 break;
38232 case PRAGMA_OACC_HOST_DATA:
38233 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38234 break;
38235 case PRAGMA_OACC_KERNELS:
38236 case PRAGMA_OACC_PARALLEL:
38237 strcpy (p_name, "#pragma acc");
38238 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38239 if_p);
38240 break;
38241 case PRAGMA_OACC_LOOP:
38242 strcpy (p_name, "#pragma acc");
38243 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38244 if_p);
38245 break;
38246 case PRAGMA_OACC_UPDATE:
38247 stmt = cp_parser_oacc_update (parser, pragma_tok);
38248 break;
38249 case PRAGMA_OACC_WAIT:
38250 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38251 break;
38252 case PRAGMA_OMP_ATOMIC:
38253 cp_parser_omp_atomic (parser, pragma_tok);
38254 return;
38255 case PRAGMA_OMP_CRITICAL:
38256 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38257 break;
38258 case PRAGMA_OMP_DISTRIBUTE:
38259 strcpy (p_name, "#pragma omp");
38260 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38261 if_p);
38262 break;
38263 case PRAGMA_OMP_FOR:
38264 strcpy (p_name, "#pragma omp");
38265 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38266 if_p);
38267 break;
38268 case PRAGMA_OMP_MASTER:
38269 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38270 break;
38271 case PRAGMA_OMP_PARALLEL:
38272 strcpy (p_name, "#pragma omp");
38273 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38274 if_p);
38275 break;
38276 case PRAGMA_OMP_SECTIONS:
38277 strcpy (p_name, "#pragma omp");
38278 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38279 break;
38280 case PRAGMA_OMP_SIMD:
38281 strcpy (p_name, "#pragma omp");
38282 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38283 if_p);
38284 break;
38285 case PRAGMA_OMP_SINGLE:
38286 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38287 break;
38288 case PRAGMA_OMP_TASK:
38289 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38290 break;
38291 case PRAGMA_OMP_TASKGROUP:
38292 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38293 break;
38294 case PRAGMA_OMP_TASKLOOP:
38295 strcpy (p_name, "#pragma omp");
38296 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38297 if_p);
38298 break;
38299 case PRAGMA_OMP_TEAMS:
38300 strcpy (p_name, "#pragma omp");
38301 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38302 if_p);
38303 break;
38304 default:
38305 gcc_unreachable ();
38308 protected_set_expr_location (stmt, pragma_tok->location);
38311 /* Transactional Memory parsing routines. */
38313 /* Parse a transaction attribute.
38315 txn-attribute:
38316 attribute
38317 [ [ identifier ] ]
38319 We use this instead of cp_parser_attributes_opt for transactions to avoid
38320 the pedwarn in C++98 mode. */
38322 static tree
38323 cp_parser_txn_attribute_opt (cp_parser *parser)
38325 cp_token *token;
38326 tree attr_name, attr = NULL;
38328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38329 return cp_parser_attributes_opt (parser);
38331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38332 return NULL_TREE;
38333 cp_lexer_consume_token (parser->lexer);
38334 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38335 goto error1;
38337 token = cp_lexer_peek_token (parser->lexer);
38338 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38340 token = cp_lexer_consume_token (parser->lexer);
38342 attr_name = (token->type == CPP_KEYWORD
38343 /* For keywords, use the canonical spelling,
38344 not the parsed identifier. */
38345 ? ridpointers[(int) token->keyword]
38346 : token->u.value);
38347 attr = build_tree_list (attr_name, NULL_TREE);
38349 else
38350 cp_parser_error (parser, "expected identifier");
38352 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38353 error1:
38354 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38355 return attr;
38358 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38360 transaction-statement:
38361 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38362 compound-statement
38363 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38366 static tree
38367 cp_parser_transaction (cp_parser *parser, cp_token *token)
38369 unsigned char old_in = parser->in_transaction;
38370 unsigned char this_in = 1, new_in;
38371 enum rid keyword = token->keyword;
38372 tree stmt, attrs, noex;
38374 cp_lexer_consume_token (parser->lexer);
38376 if (keyword == RID_TRANSACTION_RELAXED
38377 || keyword == RID_SYNCHRONIZED)
38378 this_in |= TM_STMT_ATTR_RELAXED;
38379 else
38381 attrs = cp_parser_txn_attribute_opt (parser);
38382 if (attrs)
38383 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38386 /* Parse a noexcept specification. */
38387 if (keyword == RID_ATOMIC_NOEXCEPT)
38388 noex = boolean_true_node;
38389 else if (keyword == RID_ATOMIC_CANCEL)
38391 /* cancel-and-throw is unimplemented. */
38392 sorry ("atomic_cancel");
38393 noex = NULL_TREE;
38395 else
38396 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38398 /* Keep track if we're in the lexical scope of an outer transaction. */
38399 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38401 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38403 parser->in_transaction = new_in;
38404 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38405 parser->in_transaction = old_in;
38407 finish_transaction_stmt (stmt, NULL, this_in, noex);
38409 return stmt;
38412 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38414 transaction-expression:
38415 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38416 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38419 static tree
38420 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38422 unsigned char old_in = parser->in_transaction;
38423 unsigned char this_in = 1;
38424 cp_token *token;
38425 tree expr, noex;
38426 bool noex_expr;
38427 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38429 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38430 || keyword == RID_TRANSACTION_RELAXED);
38432 if (!flag_tm)
38433 error_at (loc,
38434 keyword == RID_TRANSACTION_RELAXED
38435 ? G_("%<__transaction_relaxed%> without transactional memory "
38436 "support enabled")
38437 : G_("%<__transaction_atomic%> without transactional memory "
38438 "support enabled"));
38440 token = cp_parser_require_keyword (parser, keyword,
38441 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38442 : RT_TRANSACTION_RELAXED));
38443 gcc_assert (token != NULL);
38445 if (keyword == RID_TRANSACTION_RELAXED)
38446 this_in |= TM_STMT_ATTR_RELAXED;
38448 /* Set this early. This might mean that we allow transaction_cancel in
38449 an expression that we find out later actually has to be a constexpr.
38450 However, we expect that cxx_constant_value will be able to deal with
38451 this; also, if the noexcept has no constexpr, then what we parse next
38452 really is a transaction's body. */
38453 parser->in_transaction = this_in;
38455 /* Parse a noexcept specification. */
38456 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38457 true);
38459 if (!noex || !noex_expr
38460 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38462 matching_parens parens;
38463 parens.require_open (parser);
38465 expr = cp_parser_expression (parser);
38466 expr = finish_parenthesized_expr (expr);
38468 parens.require_close (parser);
38470 else
38472 /* The only expression that is available got parsed for the noexcept
38473 already. noexcept is true then. */
38474 expr = noex;
38475 noex = boolean_true_node;
38478 expr = build_transaction_expr (token->location, expr, this_in, noex);
38479 parser->in_transaction = old_in;
38481 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38482 return error_mark_node;
38484 return (flag_tm ? expr : error_mark_node);
38487 /* Parse a function-transaction-block.
38489 function-transaction-block:
38490 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38491 function-body
38492 __transaction_atomic txn-attribute[opt] function-try-block
38493 __transaction_relaxed ctor-initializer[opt] function-body
38494 __transaction_relaxed function-try-block
38497 static void
38498 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38500 unsigned char old_in = parser->in_transaction;
38501 unsigned char new_in = 1;
38502 tree compound_stmt, stmt, attrs;
38503 cp_token *token;
38505 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38506 || keyword == RID_TRANSACTION_RELAXED);
38507 token = cp_parser_require_keyword (parser, keyword,
38508 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38509 : RT_TRANSACTION_RELAXED));
38510 gcc_assert (token != NULL);
38512 if (keyword == RID_TRANSACTION_RELAXED)
38513 new_in |= TM_STMT_ATTR_RELAXED;
38514 else
38516 attrs = cp_parser_txn_attribute_opt (parser);
38517 if (attrs)
38518 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38521 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38523 parser->in_transaction = new_in;
38525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38526 cp_parser_function_try_block (parser);
38527 else
38528 cp_parser_ctor_initializer_opt_and_function_body
38529 (parser, /*in_function_try_block=*/false);
38531 parser->in_transaction = old_in;
38533 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38536 /* Parse a __transaction_cancel statement.
38538 cancel-statement:
38539 __transaction_cancel txn-attribute[opt] ;
38540 __transaction_cancel txn-attribute[opt] throw-expression ;
38542 ??? Cancel and throw is not yet implemented. */
38544 static tree
38545 cp_parser_transaction_cancel (cp_parser *parser)
38547 cp_token *token;
38548 bool is_outer = false;
38549 tree stmt, attrs;
38551 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38552 RT_TRANSACTION_CANCEL);
38553 gcc_assert (token != NULL);
38555 attrs = cp_parser_txn_attribute_opt (parser);
38556 if (attrs)
38557 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38559 /* ??? Parse cancel-and-throw here. */
38561 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38563 if (!flag_tm)
38565 error_at (token->location, "%<__transaction_cancel%> without "
38566 "transactional memory support enabled");
38567 return error_mark_node;
38569 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38571 error_at (token->location, "%<__transaction_cancel%> within a "
38572 "%<__transaction_relaxed%>");
38573 return error_mark_node;
38575 else if (is_outer)
38577 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38578 && !is_tm_may_cancel_outer (current_function_decl))
38580 error_at (token->location, "outer %<__transaction_cancel%> not "
38581 "within outer %<__transaction_atomic%>");
38582 error_at (token->location,
38583 " or a %<transaction_may_cancel_outer%> function");
38584 return error_mark_node;
38587 else if (parser->in_transaction == 0)
38589 error_at (token->location, "%<__transaction_cancel%> not within "
38590 "%<__transaction_atomic%>");
38591 return error_mark_node;
38594 stmt = build_tm_abort_call (token->location, is_outer);
38595 add_stmt (stmt);
38597 return stmt;
38600 /* The parser. */
38602 static GTY (()) cp_parser *the_parser;
38605 /* Special handling for the first token or line in the file. The first
38606 thing in the file might be #pragma GCC pch_preprocess, which loads a
38607 PCH file, which is a GC collection point. So we need to handle this
38608 first pragma without benefit of an existing lexer structure.
38610 Always returns one token to the caller in *FIRST_TOKEN. This is
38611 either the true first token of the file, or the first token after
38612 the initial pragma. */
38614 static void
38615 cp_parser_initial_pragma (cp_token *first_token)
38617 tree name = NULL;
38619 cp_lexer_get_preprocessor_token (NULL, first_token);
38620 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38621 return;
38623 cp_lexer_get_preprocessor_token (NULL, first_token);
38624 if (first_token->type == CPP_STRING)
38626 name = first_token->u.value;
38628 cp_lexer_get_preprocessor_token (NULL, first_token);
38629 if (first_token->type != CPP_PRAGMA_EOL)
38630 error_at (first_token->location,
38631 "junk at end of %<#pragma GCC pch_preprocess%>");
38633 else
38634 error_at (first_token->location, "expected string literal");
38636 /* Skip to the end of the pragma. */
38637 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38638 cp_lexer_get_preprocessor_token (NULL, first_token);
38640 /* Now actually load the PCH file. */
38641 if (name)
38642 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38644 /* Read one more token to return to our caller. We have to do this
38645 after reading the PCH file in, since its pointers have to be
38646 live. */
38647 cp_lexer_get_preprocessor_token (NULL, first_token);
38650 /* Parse a pragma GCC ivdep. */
38652 static bool
38653 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38655 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38656 return true;
38659 /* Parse a pragma GCC unroll. */
38661 static unsigned short
38662 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38664 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38665 tree expr = cp_parser_constant_expression (parser);
38666 unsigned short unroll;
38667 expr = maybe_constant_value (expr);
38668 HOST_WIDE_INT lunroll = 0;
38669 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38670 || TREE_CODE (expr) != INTEGER_CST
38671 || (lunroll = tree_to_shwi (expr)) < 0
38672 || lunroll >= USHRT_MAX)
38674 error_at (location, "%<#pragma GCC unroll%> requires an"
38675 " assignment-expression that evaluates to a non-negative"
38676 " integral constant less than %u", USHRT_MAX);
38677 unroll = 0;
38679 else
38681 unroll = (unsigned short)lunroll;
38682 if (unroll == 0)
38683 unroll = 1;
38685 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38686 return unroll;
38689 /* Normal parsing of a pragma token. Here we can (and must) use the
38690 regular lexer. */
38692 static bool
38693 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38695 cp_token *pragma_tok;
38696 unsigned int id;
38697 tree stmt;
38698 bool ret;
38700 pragma_tok = cp_lexer_consume_token (parser->lexer);
38701 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38702 parser->lexer->in_pragma = true;
38704 id = cp_parser_pragma_kind (pragma_tok);
38705 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38706 cp_ensure_no_omp_declare_simd (parser);
38707 switch (id)
38709 case PRAGMA_GCC_PCH_PREPROCESS:
38710 error_at (pragma_tok->location,
38711 "%<#pragma GCC pch_preprocess%> must be first");
38712 break;
38714 case PRAGMA_OMP_BARRIER:
38715 switch (context)
38717 case pragma_compound:
38718 cp_parser_omp_barrier (parser, pragma_tok);
38719 return false;
38720 case pragma_stmt:
38721 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38722 "used in compound statements", "omp barrier");
38723 break;
38724 default:
38725 goto bad_stmt;
38727 break;
38729 case PRAGMA_OMP_FLUSH:
38730 switch (context)
38732 case pragma_compound:
38733 cp_parser_omp_flush (parser, pragma_tok);
38734 return false;
38735 case pragma_stmt:
38736 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38737 "used in compound statements", "omp flush");
38738 break;
38739 default:
38740 goto bad_stmt;
38742 break;
38744 case PRAGMA_OMP_TASKWAIT:
38745 switch (context)
38747 case pragma_compound:
38748 cp_parser_omp_taskwait (parser, pragma_tok);
38749 return false;
38750 case pragma_stmt:
38751 error_at (pragma_tok->location,
38752 "%<#pragma %s%> may only be used in compound statements",
38753 "omp taskwait");
38754 break;
38755 default:
38756 goto bad_stmt;
38758 break;
38760 case PRAGMA_OMP_TASKYIELD:
38761 switch (context)
38763 case pragma_compound:
38764 cp_parser_omp_taskyield (parser, pragma_tok);
38765 return false;
38766 case pragma_stmt:
38767 error_at (pragma_tok->location,
38768 "%<#pragma %s%> may only be used in compound statements",
38769 "omp taskyield");
38770 break;
38771 default:
38772 goto bad_stmt;
38774 break;
38776 case PRAGMA_OMP_CANCEL:
38777 switch (context)
38779 case pragma_compound:
38780 cp_parser_omp_cancel (parser, pragma_tok);
38781 return false;
38782 case pragma_stmt:
38783 error_at (pragma_tok->location,
38784 "%<#pragma %s%> may only be used in compound statements",
38785 "omp cancel");
38786 break;
38787 default:
38788 goto bad_stmt;
38790 break;
38792 case PRAGMA_OMP_CANCELLATION_POINT:
38793 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38794 return false;
38796 case PRAGMA_OMP_THREADPRIVATE:
38797 cp_parser_omp_threadprivate (parser, pragma_tok);
38798 return false;
38800 case PRAGMA_OMP_DECLARE:
38801 return cp_parser_omp_declare (parser, pragma_tok, context);
38803 case PRAGMA_OACC_DECLARE:
38804 cp_parser_oacc_declare (parser, pragma_tok);
38805 return false;
38807 case PRAGMA_OACC_ENTER_DATA:
38808 if (context == pragma_stmt)
38810 error_at (pragma_tok->location,
38811 "%<#pragma %s%> may only be used in compound statements",
38812 "acc enter data");
38813 break;
38815 else if (context != pragma_compound)
38816 goto bad_stmt;
38817 cp_parser_omp_construct (parser, pragma_tok, if_p);
38818 return true;
38820 case PRAGMA_OACC_EXIT_DATA:
38821 if (context == pragma_stmt)
38823 error_at (pragma_tok->location,
38824 "%<#pragma %s%> may only be used in compound statements",
38825 "acc exit data");
38826 break;
38828 else if (context != pragma_compound)
38829 goto bad_stmt;
38830 cp_parser_omp_construct (parser, pragma_tok, if_p);
38831 return true;
38833 case PRAGMA_OACC_ROUTINE:
38834 if (context != pragma_external)
38836 error_at (pragma_tok->location,
38837 "%<#pragma acc routine%> must be at file scope");
38838 break;
38840 cp_parser_oacc_routine (parser, pragma_tok, context);
38841 return false;
38843 case PRAGMA_OACC_UPDATE:
38844 if (context == pragma_stmt)
38846 error_at (pragma_tok->location,
38847 "%<#pragma %s%> may only be used in compound statements",
38848 "acc update");
38849 break;
38851 else if (context != pragma_compound)
38852 goto bad_stmt;
38853 cp_parser_omp_construct (parser, pragma_tok, if_p);
38854 return true;
38856 case PRAGMA_OACC_WAIT:
38857 if (context == pragma_stmt)
38859 error_at (pragma_tok->location,
38860 "%<#pragma %s%> may only be used in compound statements",
38861 "acc wait");
38862 break;
38864 else if (context != pragma_compound)
38865 goto bad_stmt;
38866 cp_parser_omp_construct (parser, pragma_tok, if_p);
38867 return true;
38869 case PRAGMA_OACC_ATOMIC:
38870 case PRAGMA_OACC_CACHE:
38871 case PRAGMA_OACC_DATA:
38872 case PRAGMA_OACC_HOST_DATA:
38873 case PRAGMA_OACC_KERNELS:
38874 case PRAGMA_OACC_PARALLEL:
38875 case PRAGMA_OACC_LOOP:
38876 case PRAGMA_OMP_ATOMIC:
38877 case PRAGMA_OMP_CRITICAL:
38878 case PRAGMA_OMP_DISTRIBUTE:
38879 case PRAGMA_OMP_FOR:
38880 case PRAGMA_OMP_MASTER:
38881 case PRAGMA_OMP_PARALLEL:
38882 case PRAGMA_OMP_SECTIONS:
38883 case PRAGMA_OMP_SIMD:
38884 case PRAGMA_OMP_SINGLE:
38885 case PRAGMA_OMP_TASK:
38886 case PRAGMA_OMP_TASKGROUP:
38887 case PRAGMA_OMP_TASKLOOP:
38888 case PRAGMA_OMP_TEAMS:
38889 if (context != pragma_stmt && context != pragma_compound)
38890 goto bad_stmt;
38891 stmt = push_omp_privatization_clauses (false);
38892 cp_parser_omp_construct (parser, pragma_tok, if_p);
38893 pop_omp_privatization_clauses (stmt);
38894 return true;
38896 case PRAGMA_OMP_ORDERED:
38897 if (context != pragma_stmt && context != pragma_compound)
38898 goto bad_stmt;
38899 stmt = push_omp_privatization_clauses (false);
38900 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38901 pop_omp_privatization_clauses (stmt);
38902 return ret;
38904 case PRAGMA_OMP_TARGET:
38905 if (context != pragma_stmt && context != pragma_compound)
38906 goto bad_stmt;
38907 stmt = push_omp_privatization_clauses (false);
38908 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38909 pop_omp_privatization_clauses (stmt);
38910 return ret;
38912 case PRAGMA_OMP_END_DECLARE_TARGET:
38913 cp_parser_omp_end_declare_target (parser, pragma_tok);
38914 return false;
38916 case PRAGMA_OMP_SECTION:
38917 error_at (pragma_tok->location,
38918 "%<#pragma omp section%> may only be used in "
38919 "%<#pragma omp sections%> construct");
38920 break;
38922 case PRAGMA_IVDEP:
38924 if (context == pragma_external)
38926 error_at (pragma_tok->location,
38927 "%<#pragma GCC ivdep%> must be inside a function");
38928 break;
38930 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38931 unsigned short unroll;
38932 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38933 if (tok->type == CPP_PRAGMA
38934 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38936 tok = cp_lexer_consume_token (parser->lexer);
38937 unroll = cp_parser_pragma_unroll (parser, tok);
38938 tok = cp_lexer_peek_token (the_parser->lexer);
38940 else
38941 unroll = 0;
38942 if (tok->type != CPP_KEYWORD
38943 || (tok->keyword != RID_FOR
38944 && tok->keyword != RID_WHILE
38945 && tok->keyword != RID_DO))
38947 cp_parser_error (parser, "for, while or do statement expected");
38948 return false;
38950 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38951 return true;
38954 case PRAGMA_UNROLL:
38956 if (context == pragma_external)
38958 error_at (pragma_tok->location,
38959 "%<#pragma GCC unroll%> must be inside a function");
38960 break;
38962 const unsigned short unroll
38963 = cp_parser_pragma_unroll (parser, pragma_tok);
38964 bool ivdep;
38965 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38966 if (tok->type == CPP_PRAGMA
38967 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38969 tok = cp_lexer_consume_token (parser->lexer);
38970 ivdep = cp_parser_pragma_ivdep (parser, tok);
38971 tok = cp_lexer_peek_token (the_parser->lexer);
38973 else
38974 ivdep = false;
38975 if (tok->type != CPP_KEYWORD
38976 || (tok->keyword != RID_FOR
38977 && tok->keyword != RID_WHILE
38978 && tok->keyword != RID_DO))
38980 cp_parser_error (parser, "for, while or do statement expected");
38981 return false;
38983 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38984 return true;
38987 default:
38988 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38989 c_invoke_pragma_handler (id);
38990 break;
38992 bad_stmt:
38993 cp_parser_error (parser, "expected declaration specifiers");
38994 break;
38997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38998 return false;
39001 /* The interface the pragma parsers have to the lexer. */
39003 enum cpp_ttype
39004 pragma_lex (tree *value, location_t *loc)
39006 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39007 enum cpp_ttype ret = tok->type;
39009 *value = tok->u.value;
39010 if (loc)
39011 *loc = tok->location;
39013 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39014 ret = CPP_EOF;
39015 else if (ret == CPP_STRING)
39016 *value = cp_parser_string_literal (the_parser, false, false);
39017 else
39019 if (ret == CPP_KEYWORD)
39020 ret = CPP_NAME;
39021 cp_lexer_consume_token (the_parser->lexer);
39024 return ret;
39028 /* External interface. */
39030 /* Parse one entire translation unit. */
39032 void
39033 c_parse_file (void)
39035 static bool already_called = false;
39037 if (already_called)
39038 fatal_error (input_location,
39039 "inter-module optimizations not implemented for C++");
39040 already_called = true;
39042 the_parser = cp_parser_new ();
39043 push_deferring_access_checks (flag_access_control
39044 ? dk_no_deferred : dk_no_check);
39045 cp_parser_translation_unit (the_parser);
39046 the_parser = NULL;
39049 /* Create an identifier for a generic parameter type (a synthesized
39050 template parameter implied by `auto' or a concept identifier). */
39052 static GTY(()) int generic_parm_count;
39053 static tree
39054 make_generic_type_name ()
39056 char buf[32];
39057 sprintf (buf, "auto:%d", ++generic_parm_count);
39058 return get_identifier (buf);
39061 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39062 (creating a new template parameter list if necessary). Returns the newly
39063 created template type parm. */
39065 static tree
39066 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39068 gcc_assert (current_binding_level->kind == sk_function_parms);
39070 /* Before committing to modifying any scope, if we're in an
39071 implicit template scope, and we're trying to synthesize a
39072 constrained parameter, try to find a previous parameter with
39073 the same name. This is the same-type rule for abbreviated
39074 function templates.
39076 NOTE: We can generate implicit parameters when tentatively
39077 parsing a nested name specifier, only to reject that parse
39078 later. However, matching the same template-id as part of a
39079 direct-declarator should generate an identical template
39080 parameter, so this rule will merge them. */
39081 if (parser->implicit_template_scope && constr)
39083 tree t = parser->implicit_template_parms;
39084 while (t)
39086 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39088 tree d = TREE_VALUE (t);
39089 if (TREE_CODE (d) == PARM_DECL)
39090 /* Return the TEMPLATE_PARM_INDEX. */
39091 d = DECL_INITIAL (d);
39092 return d;
39094 t = TREE_CHAIN (t);
39098 /* We are either continuing a function template that already contains implicit
39099 template parameters, creating a new fully-implicit function template, or
39100 extending an existing explicit function template with implicit template
39101 parameters. */
39103 cp_binding_level *const entry_scope = current_binding_level;
39105 bool become_template = false;
39106 cp_binding_level *parent_scope = 0;
39108 if (parser->implicit_template_scope)
39110 gcc_assert (parser->implicit_template_parms);
39112 current_binding_level = parser->implicit_template_scope;
39114 else
39116 /* Roll back to the existing template parameter scope (in the case of
39117 extending an explicit function template) or introduce a new template
39118 parameter scope ahead of the function parameter scope (or class scope
39119 in the case of out-of-line member definitions). The function scope is
39120 added back after template parameter synthesis below. */
39122 cp_binding_level *scope = entry_scope;
39124 while (scope->kind == sk_function_parms)
39126 parent_scope = scope;
39127 scope = scope->level_chain;
39129 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39131 /* If not defining a class, then any class scope is a scope level in
39132 an out-of-line member definition. In this case simply wind back
39133 beyond the first such scope to inject the template parameter list.
39134 Otherwise wind back to the class being defined. The latter can
39135 occur in class member friend declarations such as:
39137 class A {
39138 void foo (auto);
39140 class B {
39141 friend void A::foo (auto);
39144 The template parameter list synthesized for the friend declaration
39145 must be injected in the scope of 'B'. This can also occur in
39146 erroneous cases such as:
39148 struct A {
39149 struct B {
39150 void foo (auto);
39152 void B::foo (auto) {}
39155 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39156 but, nevertheless, the template parameter list synthesized for the
39157 declarator should be injected into the scope of 'A' as if the
39158 ill-formed template was specified explicitly. */
39160 while (scope->kind == sk_class && !scope->defining_class_p)
39162 parent_scope = scope;
39163 scope = scope->level_chain;
39167 current_binding_level = scope;
39169 if (scope->kind != sk_template_parms
39170 || !function_being_declared_is_template_p (parser))
39172 /* Introduce a new template parameter list for implicit template
39173 parameters. */
39175 become_template = true;
39177 parser->implicit_template_scope
39178 = begin_scope (sk_template_parms, NULL);
39180 ++processing_template_decl;
39182 parser->fully_implicit_function_template_p = true;
39183 ++parser->num_template_parameter_lists;
39185 else
39187 /* Synthesize implicit template parameters at the end of the explicit
39188 template parameter list. */
39190 gcc_assert (current_template_parms);
39192 parser->implicit_template_scope = scope;
39194 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39195 parser->implicit_template_parms
39196 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39200 /* Synthesize a new template parameter and track the current template
39201 parameter chain with implicit_template_parms. */
39203 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39204 tree synth_id = make_generic_type_name ();
39205 tree synth_tmpl_parm;
39206 bool non_type = false;
39208 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39209 synth_tmpl_parm
39210 = finish_template_type_parm (class_type_node, synth_id);
39211 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39212 synth_tmpl_parm
39213 = finish_constrained_template_template_parm (proto, synth_id);
39214 else
39216 synth_tmpl_parm = copy_decl (proto);
39217 DECL_NAME (synth_tmpl_parm) = synth_id;
39218 non_type = true;
39221 // Attach the constraint to the parm before processing.
39222 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39223 TREE_TYPE (node) = constr;
39224 tree new_parm
39225 = process_template_parm (parser->implicit_template_parms,
39226 input_location,
39227 node,
39228 /*non_type=*/non_type,
39229 /*param_pack=*/false);
39231 // Chain the new parameter to the list of implicit parameters.
39232 if (parser->implicit_template_parms)
39233 parser->implicit_template_parms
39234 = TREE_CHAIN (parser->implicit_template_parms);
39235 else
39236 parser->implicit_template_parms = new_parm;
39238 tree new_decl = get_local_decls ();
39239 if (non_type)
39240 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39241 new_decl = DECL_INITIAL (new_decl);
39243 /* If creating a fully implicit function template, start the new implicit
39244 template parameter list with this synthesized type, otherwise grow the
39245 current template parameter list. */
39247 if (become_template)
39249 parent_scope->level_chain = current_binding_level;
39251 tree new_parms = make_tree_vec (1);
39252 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39253 current_template_parms = tree_cons (size_int (processing_template_decl),
39254 new_parms, current_template_parms);
39256 else
39258 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39259 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39260 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39261 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39264 // If the new parameter was constrained, we need to add that to the
39265 // constraints in the template parameter list.
39266 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39268 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39269 reqs = conjoin_constraints (reqs, req);
39270 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39273 current_binding_level = entry_scope;
39275 return new_decl;
39278 /* Finish the declaration of a fully implicit function template. Such a
39279 template has no explicit template parameter list so has not been through the
39280 normal template head and tail processing. synthesize_implicit_template_parm
39281 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39282 provided if the declaration is a class member such that its template
39283 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39284 form is returned. Otherwise NULL_TREE is returned. */
39286 static tree
39287 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39289 gcc_assert (parser->fully_implicit_function_template_p);
39291 if (member_decl_opt && member_decl_opt != error_mark_node
39292 && DECL_VIRTUAL_P (member_decl_opt))
39294 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39295 "implicit templates may not be %<virtual%>");
39296 DECL_VIRTUAL_P (member_decl_opt) = false;
39299 if (member_decl_opt)
39300 member_decl_opt = finish_member_template_decl (member_decl_opt);
39301 end_template_decl ();
39303 parser->fully_implicit_function_template_p = false;
39304 parser->implicit_template_parms = 0;
39305 parser->implicit_template_scope = 0;
39306 --parser->num_template_parameter_lists;
39308 return member_decl_opt;
39311 /* Like finish_fully_implicit_template, but to be used in error
39312 recovery, rearranging scopes so that we restore the state we had
39313 before synthesize_implicit_template_parm inserted the implement
39314 template parms scope. */
39316 static void
39317 abort_fully_implicit_template (cp_parser *parser)
39319 cp_binding_level *return_to_scope = current_binding_level;
39321 if (parser->implicit_template_scope
39322 && return_to_scope != parser->implicit_template_scope)
39324 cp_binding_level *child = return_to_scope;
39325 for (cp_binding_level *scope = child->level_chain;
39326 scope != parser->implicit_template_scope;
39327 scope = child->level_chain)
39328 child = scope;
39329 child->level_chain = parser->implicit_template_scope->level_chain;
39330 parser->implicit_template_scope->level_chain = return_to_scope;
39331 current_binding_level = parser->implicit_template_scope;
39333 else
39334 return_to_scope = return_to_scope->level_chain;
39336 finish_fully_implicit_template (parser, NULL);
39338 gcc_assert (current_binding_level == return_to_scope);
39341 /* Helper function for diagnostics that have complained about things
39342 being used with 'extern "C"' linkage.
39344 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39346 void
39347 maybe_show_extern_c_location (void)
39349 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39350 inform (the_parser->innermost_linkage_specification_location,
39351 "%<extern \"C\"%> linkage started here");
39354 #include "gt-cp-parser.h"