PR c++/85710 - ICE with -Wmemset-elt-size.
[official-gcc.git] / gcc / cp / parser.c
blob03aea2f11507fb74254f79211c5168f1a0eb6d0c
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 *);
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 *, bool = false);
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;
3496 unsigned condop_depth = 0;
3498 if (recovering && or_ttype == CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3500 return 0;
3502 while (true)
3504 cp_token * token = cp_lexer_peek_token (parser->lexer);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token->type == or_ttype && or_ttype != CPP_EOF
3508 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3509 return -1;
3511 switch (token->type)
3513 case CPP_EOF:
3514 case CPP_PRAGMA_EOL:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3516 return 0;
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE:
3520 ++square_depth;
3521 break;
3522 case CPP_CLOSE_SQUARE:
3523 if (!square_depth--)
3524 return 0;
3525 break;
3527 case CPP_SEMICOLON:
3528 /* This matches the processing in skip_to_end_of_statement. */
3529 if (!brace_depth)
3530 return 0;
3531 break;
3533 case CPP_OPEN_BRACE:
3534 ++brace_depth;
3535 break;
3536 case CPP_CLOSE_BRACE:
3537 if (!brace_depth--)
3538 return 0;
3539 break;
3541 case CPP_OPEN_PAREN:
3542 if (!brace_depth)
3543 ++paren_depth;
3544 break;
3546 case CPP_CLOSE_PAREN:
3547 if (!brace_depth && !paren_depth--)
3549 if (consume_paren)
3550 cp_lexer_consume_token (parser->lexer);
3551 return 1;
3553 break;
3555 case CPP_QUERY:
3556 if (!brace_depth && !paren_depth && !square_depth)
3557 ++condop_depth;
3558 break;
3560 case CPP_COLON:
3561 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3562 condop_depth--;
3563 break;
3565 default:
3566 break;
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser->lexer);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3579 static int
3580 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3581 bool recovering,
3582 bool or_comma,
3583 bool consume_paren)
3585 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3587 ttype, consume_paren);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3594 static void
3595 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3597 unsigned nesting_depth = 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser->fully_implicit_function_template_p)
3601 abort_fully_implicit_template (parser);
3603 while (true)
3605 cp_token *token = cp_lexer_peek_token (parser->lexer);
3607 switch (token->type)
3609 case CPP_EOF:
3610 case CPP_PRAGMA_EOL:
3611 /* If we've run out of tokens, stop. */
3612 return;
3614 case CPP_SEMICOLON:
3615 /* If the next token is a `;', we have reached the end of the
3616 statement. */
3617 if (!nesting_depth)
3618 return;
3619 break;
3621 case CPP_CLOSE_BRACE:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3625 { 3 + }
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth == 0)
3630 return;
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3634 That way given:
3636 void f g () { ... }
3637 typedef int I;
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3641 declaration. */
3642 if (--nesting_depth == 0)
3644 cp_lexer_consume_token (parser->lexer);
3645 return;
3647 break;
3649 case CPP_OPEN_BRACE:
3650 ++nesting_depth;
3651 break;
3653 default:
3654 break;
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser->lexer);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3666 static void
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3672 /* If there is additional (erroneous) input, skip to the end of
3673 the statement. */
3674 cp_parser_skip_to_end_of_statement (parser);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3677 cp_lexer_consume_token (parser->lexer);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3684 static void
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3687 int nesting_depth = 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser->fully_implicit_function_template_p)
3691 abort_fully_implicit_template (parser);
3693 while (nesting_depth >= 0)
3695 cp_token *token = cp_lexer_peek_token (parser->lexer);
3697 switch (token->type)
3699 case CPP_EOF:
3700 case CPP_PRAGMA_EOL:
3701 /* If we've run out of tokens, stop. */
3702 return;
3704 case CPP_SEMICOLON:
3705 /* Stop if this is an unnested ';'. */
3706 if (!nesting_depth)
3707 nesting_depth = -1;
3708 break;
3710 case CPP_CLOSE_BRACE:
3711 /* Stop if this is an unnested '}', or closes the outermost
3712 nesting level. */
3713 nesting_depth--;
3714 if (nesting_depth < 0)
3715 return;
3716 if (!nesting_depth)
3717 nesting_depth = -1;
3718 break;
3720 case CPP_OPEN_BRACE:
3721 /* Nest. */
3722 nesting_depth++;
3723 break;
3725 default:
3726 break;
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser->lexer);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3736 false otherwise. */
3738 static bool
3739 cp_parser_skip_to_closing_brace (cp_parser *parser)
3741 unsigned nesting_depth = 0;
3743 while (true)
3745 cp_token *token = cp_lexer_peek_token (parser->lexer);
3747 switch (token->type)
3749 case CPP_EOF:
3750 case CPP_PRAGMA_EOL:
3751 /* If we've run out of tokens, stop. */
3752 return false;
3754 case CPP_CLOSE_BRACE:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth-- == 0)
3758 return true;
3759 break;
3761 case CPP_OPEN_BRACE:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3764 ++nesting_depth;
3765 break;
3767 default:
3768 break;
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser->lexer);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3778 sequence. */
3780 static void
3781 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3783 cp_token *token;
3785 parser->lexer->in_pragma = false;
3788 token = cp_lexer_consume_token (parser->lexer);
3789 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3798 static void
3799 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3801 parser->lexer->in_pragma = false;
3802 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3803 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3810 static tree
3811 cp_parser_make_typename_type (cp_parser *parser, tree id,
3812 location_t id_location)
3814 tree result;
3815 if (identifier_p (id))
3817 result = make_typename_type (parser->scope, id, typename_type,
3818 /*complain=*/tf_none);
3819 if (result == error_mark_node)
3820 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3821 return result;
3823 return make_typename_type (parser->scope, id, typename_type, tf_error);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator *
3834 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3835 cp_cv_quals cv_qualifiers,
3836 cp_declarator *target,
3837 tree attributes)
3839 if (code == ERROR_MARK || target == cp_error_declarator)
3840 return cp_error_declarator;
3842 if (code == INDIRECT_REF)
3843 if (class_type == NULL_TREE)
3844 return make_pointer_declarator (cv_qualifiers, target, attributes);
3845 else
3846 return make_ptrmem_declarator (cv_qualifiers, class_type,
3847 target, attributes);
3848 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3849 return make_reference_declarator (cv_qualifiers, target,
3850 false, attributes);
3851 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3852 return make_reference_declarator (cv_qualifiers, target,
3853 true, attributes);
3854 gcc_unreachable ();
3857 /* Create a new C++ parser. */
3859 static cp_parser *
3860 cp_parser_new (void)
3862 cp_parser *parser;
3863 cp_lexer *lexer;
3864 unsigned i;
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer = cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3873 binops_by_token[binops[i].token_type] = binops[i];
3875 parser = ggc_cleared_alloc<cp_parser> ();
3876 parser->lexer = lexer;
3877 parser->context = cp_parser_context_new (NULL);
3879 /* For now, we always accept GNU extensions. */
3880 parser->allow_gnu_extensions_p = 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3883 template-id. */
3884 parser->greater_than_is_operator_p = true;
3886 parser->default_arg_ok_p = true;
3888 /* We are not parsing a constant-expression. */
3889 parser->integral_constant_expression_p = false;
3890 parser->allow_non_integral_constant_expression_p = false;
3891 parser->non_integral_constant_expression_p = false;
3893 /* Local variable names are not forbidden. */
3894 parser->local_variables_forbidden_p = false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser->in_unbraced_linkage_specification_p = false;
3899 /* We are not processing a declarator. */
3900 parser->in_declarator_p = false;
3902 /* We are not processing a template-argument-list. */
3903 parser->in_template_argument_list_p = false;
3905 /* We are not in an iteration statement. */
3906 parser->in_statement = 0;
3908 /* We are not in a switch statement. */
3909 parser->in_switch_statement_p = false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser->in_type_id_in_expr_p = false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser->implicit_extern_c = false;
3917 /* String literals should be translated to the execution character set. */
3918 parser->translate_strings_p = true;
3920 /* We are not parsing a function body. */
3921 parser->in_function_body = false;
3923 /* We can correct until told otherwise. */
3924 parser->colon_corrects_to_scope_p = true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser);
3929 /* There are no classes being defined. */
3930 parser->num_classes_being_defined = 0;
3932 /* No template parameters apply. */
3933 parser->num_template_parameter_lists = 0;
3935 /* Special parsing data structures. */
3936 parser->omp_declare_simd = NULL;
3937 parser->oacc_routine = NULL;
3939 /* Not declaring an implicit function template. */
3940 parser->auto_is_implicit_function_template_parm_p = false;
3941 parser->fully_implicit_function_template_p = false;
3942 parser->implicit_template_parms = 0;
3943 parser->implicit_template_scope = 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser->prevent_constrained_type_specifiers = 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3951 return parser;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3958 static void
3959 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3961 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3962 lexer->next = parser->lexer;
3963 parser->lexer = lexer;
3965 /* Move the current source position to that of the first token in the
3966 new lexer. */
3967 cp_lexer_set_source_position_from_token (lexer->next_token);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3972 static void
3973 cp_parser_pop_lexer (cp_parser *parser)
3975 cp_lexer *lexer = parser->lexer;
3976 parser->lexer = lexer->next;
3977 cp_lexer_destroy (lexer);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3987 identifier. */
3989 static cp_expr
3990 cp_parser_identifier (cp_parser* parser)
3992 cp_token *token;
3994 /* Look for the identifier. */
3995 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3996 /* Return the value. */
3997 if (token)
3998 return cp_expr (token->u.value, token->location);
3999 else
4000 return error_mark_node;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4007 invalid here.
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4017 static cp_expr
4018 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4019 bool lookup_udlit = true)
4021 tree value;
4022 size_t count;
4023 struct obstack str_ob;
4024 cpp_string str, istr, *strs;
4025 cp_token *tok;
4026 enum cpp_ttype type, curr_type;
4027 int have_suffix_p = 0;
4028 tree string_tree;
4029 tree suffix_id = NULL_TREE;
4030 bool curr_tok_is_userdef_p = false;
4032 tok = cp_lexer_peek_token (parser->lexer);
4033 if (!cp_parser_is_string_literal (tok))
4035 cp_parser_error (parser, "expected string-literal");
4036 return error_mark_node;
4039 location_t loc = tok->location;
4041 if (cpp_userdef_string_p (tok->type))
4043 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4044 curr_type = cpp_userdef_string_remove_type (tok->type);
4045 curr_tok_is_userdef_p = true;
4047 else
4049 string_tree = tok->u.value;
4050 curr_type = tok->type;
4052 type = curr_type;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4059 cp_lexer_consume_token (parser->lexer);
4061 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4062 str.len = TREE_STRING_LENGTH (string_tree);
4063 count = 1;
4065 if (curr_tok_is_userdef_p)
4067 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4068 have_suffix_p = 1;
4069 curr_type = cpp_userdef_string_remove_type (tok->type);
4071 else
4072 curr_type = tok->type;
4074 strs = &str;
4076 else
4078 location_t last_tok_loc = tok->location;
4079 gcc_obstack_init (&str_ob);
4080 count = 0;
4084 cp_lexer_consume_token (parser->lexer);
4085 count++;
4086 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4087 str.len = TREE_STRING_LENGTH (string_tree);
4089 if (curr_tok_is_userdef_p)
4091 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4092 if (have_suffix_p == 0)
4094 suffix_id = curr_suffix_id;
4095 have_suffix_p = 1;
4097 else if (have_suffix_p == 1
4098 && curr_suffix_id != suffix_id)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id, curr_suffix_id);
4103 have_suffix_p = -1;
4105 curr_type = cpp_userdef_string_remove_type (tok->type);
4107 else
4108 curr_type = tok->type;
4110 if (type != curr_type)
4112 if (type == CPP_STRING)
4113 type = curr_type;
4114 else if (curr_type != CPP_STRING)
4116 rich_location rich_loc (line_table, tok->location);
4117 rich_loc.add_range (last_tok_loc, false);
4118 error_at (&rich_loc,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4126 last_tok_loc = tok->location;
4128 tok = cp_lexer_peek_token (parser->lexer);
4129 if (cpp_userdef_string_p (tok->type))
4131 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 curr_tok_is_userdef_p = true;
4135 else
4137 string_tree = tok->u.value;
4138 curr_type = tok->type;
4139 curr_tok_is_userdef_p = false;
4142 while (cp_parser_is_string_literal (tok));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc = make_location (loc, loc, get_finish (last_tok_loc));
4149 strs = (cpp_string *) obstack_finish (&str_ob);
4152 if (type != CPP_STRING && !wide_ok)
4154 cp_parser_error (parser, "a wide string is invalid in this context");
4155 type = CPP_STRING;
4158 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4159 (parse_in, strs, count, &istr, type))
4161 value = build_string (istr.len, (const char *)istr.text);
4162 free (CONST_CAST (unsigned char *, istr.text));
4164 switch (type)
4166 default:
4167 case CPP_STRING:
4168 case CPP_UTF8STRING:
4169 TREE_TYPE (value) = char_array_type_node;
4170 break;
4171 case CPP_STRING16:
4172 TREE_TYPE (value) = char16_array_type_node;
4173 break;
4174 case CPP_STRING32:
4175 TREE_TYPE (value) = char32_array_type_node;
4176 break;
4177 case CPP_WSTRING:
4178 TREE_TYPE (value) = wchar_array_type_node;
4179 break;
4182 value = fix_string_type (value);
4184 if (have_suffix_p)
4186 tree literal = build_userdef_literal (suffix_id, value,
4187 OT_NONE, NULL_TREE);
4188 if (lookup_udlit)
4189 value = cp_parser_userdef_string_literal (literal);
4190 else
4191 value = literal;
4194 else
4195 /* cpp_interpret_string has issued an error. */
4196 value = error_mark_node;
4198 if (count > 1)
4199 obstack_free (&str_ob, 0);
4201 return cp_expr (value, loc);
4204 /* Look up a literal operator with the name and the exact arguments. */
4206 static tree
4207 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4209 tree decl;
4210 decl = lookup_name (name);
4211 if (!decl || !is_overloaded_fn (decl))
4212 return error_mark_node;
4214 for (lkp_iterator iter (decl); iter; ++iter)
4216 unsigned int ix;
4217 bool found = true;
4218 tree fn = *iter;
4219 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4220 if (parmtypes != NULL_TREE)
4222 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4223 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4225 tree tparm = TREE_VALUE (parmtypes);
4226 tree targ = TREE_TYPE ((*args)[ix]);
4227 bool ptr = TYPE_PTR_P (tparm);
4228 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4229 if ((ptr || arr || !same_type_p (tparm, targ))
4230 && (!ptr || !arr
4231 || !same_type_p (TREE_TYPE (tparm),
4232 TREE_TYPE (targ))))
4233 found = false;
4235 if (found
4236 && ix == vec_safe_length (args)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes == void_list_node)
4242 return decl;
4246 return error_mark_node;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4252 static cp_expr
4253 cp_parser_userdef_char_literal (cp_parser *parser)
4255 cp_token *token = cp_lexer_consume_token (parser->lexer);
4256 tree literal = token->u.value;
4257 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4258 tree value = USERDEF_LITERAL_VALUE (literal);
4259 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4260 tree decl, result;
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec<tree, va_gc> *args = make_tree_vector ();
4265 vec_safe_push (args, value);
4266 decl = lookup_literal_operator (name, args);
4267 if (!decl || decl == error_mark_node)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name, TREE_TYPE (value));
4271 release_tree_vector (args);
4272 return error_mark_node;
4274 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4275 release_tree_vector (args);
4276 return result;
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4282 static tree
4283 make_char_string_pack (tree value)
4285 tree charvec;
4286 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4287 const char *str = TREE_STRING_POINTER (value);
4288 int i, len = TREE_STRING_LENGTH (value) - 1;
4289 tree argvec = make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec = make_tree_vec (len);
4293 for (i = 0; i < len; ++i)
4294 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4299 TREE_VEC_ELT (argvec, 0) = argpack;
4301 return argvec;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4307 static tree
4308 make_string_pack (tree value)
4310 tree charvec;
4311 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value);
4314 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4315 int len = TREE_STRING_LENGTH (value) / sz - 1;
4316 tree argvec = make_tree_vec (2);
4318 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4319 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec = make_tree_vec (len);
4326 for (int i = 0; i < len; ++i)
4327 TREE_VEC_ELT (charvec, i)
4328 = double_int_to_tree (str_char_type_node,
4329 double_int::from_buffer (str + i * sz, sz));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4334 TREE_VEC_ELT (argvec, 1) = argpack;
4336 return argvec;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4342 static cp_expr
4343 cp_parser_userdef_numeric_literal (cp_parser *parser)
4345 cp_token *token = cp_lexer_consume_token (parser->lexer);
4346 tree literal = token->u.value;
4347 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4348 tree value = USERDEF_LITERAL_VALUE (literal);
4349 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4350 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4351 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4352 tree decl, result;
4353 vec<tree, va_gc> *args;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args = make_tree_vector ();
4358 vec_safe_push (args, value);
4359 decl = lookup_literal_operator (name, args);
4360 if (decl && decl != error_mark_node)
4362 result = finish_call_expr (decl, &args, false, true,
4363 tf_warning_or_error);
4365 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node);
4371 else
4373 if (overflow > 0)
4374 warning_at (token->location, OPT_Woverflow,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node);
4377 else if (overflow < 0)
4378 warning_at (token->location, OPT_Woverflow,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args);
4383 return result;
4385 release_tree_vector (args);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args = make_tree_vector ();
4391 vec_safe_push (args, num_string);
4392 decl = lookup_literal_operator (name, args);
4393 if (decl && decl != error_mark_node)
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4400 release_tree_vector (args);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args = make_tree_vector ();
4406 decl = lookup_literal_operator (name, args);
4407 if (decl && decl != error_mark_node)
4409 tree tmpl_args = make_char_string_pack (num_string);
4410 decl = lookup_template_function (decl, tmpl_args);
4411 result = finish_call_expr (decl, &args, false, true,
4412 tf_warning_or_error);
4413 release_tree_vector (args);
4414 return result;
4417 release_tree_vector (args);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4422 bool i14 = (cxx_dialect > cxx11
4423 && (id_equal (suffix_id, "i")
4424 || id_equal (suffix_id, "if")
4425 || id_equal (suffix_id, "il")));
4426 diagnostic_t kind = DK_ERROR;
4427 int opt = 0;
4429 if (i14 && ext)
4431 tree cxlit = lookup_qualified_name (std_node,
4432 get_identifier ("complex_literals"),
4433 0, false, false);
4434 if (cxlit == error_mark_node)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4437 kind = DK_PEDWARN;
4438 opt = OPT_Wpedantic;
4442 bool complained
4443 = emit_diagnostic (kind, input_location, opt,
4444 "unable to find numeric literal operator %qD", name);
4446 if (!complained)
4447 /* Don't inform either. */;
4448 else if (i14)
4450 inform (token->location, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4452 "suffixes");
4453 if (ext)
4454 inform (token->location, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4457 else if (!ext)
4458 inform (token->location, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind == DK_ERROR)
4462 value = error_mark_node;
4463 else
4465 /* Use the built-in semantics. */
4466 tree type;
4467 if (id_equal (suffix_id, "i"))
4469 if (TREE_CODE (value) == INTEGER_CST)
4470 type = integer_type_node;
4471 else
4472 type = double_type_node;
4474 else if (id_equal (suffix_id, "if"))
4475 type = float_type_node;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type = long_double_type_node;
4479 value = build_complex (build_complex_type (type),
4480 fold_convert (type, integer_zero_node),
4481 fold_convert (type, value));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4485 /* Avoid repeated diagnostics. */
4486 token->u.value = value;
4487 return value;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4492 as arguments. */
4494 static tree
4495 cp_parser_userdef_string_literal (tree literal)
4497 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4498 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4499 tree value = USERDEF_LITERAL_VALUE (literal);
4500 int len = TREE_STRING_LENGTH (value)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4502 tree decl, result;
4503 vec<tree, va_gc> *args;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args = make_tree_vector ();
4508 vec_safe_push (args, value);
4509 vec_safe_push (args, build_int_cst (size_type_node, len));
4510 decl = lookup_literal_operator (name, args);
4512 if (decl && decl != error_mark_node)
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4519 release_tree_vector (args);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args = make_tree_vector ();
4525 decl = lookup_literal_operator (name, args);
4526 if (decl && decl != error_mark_node)
4528 tree tmpl_args = make_string_pack (value);
4529 decl = lookup_template_function (decl, tmpl_args);
4530 result = finish_call_expr (decl, &args, false, true,
4531 tf_warning_or_error);
4532 release_tree_vector (args);
4533 return result;
4535 release_tree_vector (args);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name, TREE_TYPE (value), size_type_node);
4539 return error_mark_node;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4547 translation-unit:
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4552 static bool
4553 cp_parser_translation_unit (cp_parser* parser)
4555 /* The address of the first non-permanent object on the declarator
4556 obstack. */
4557 static void *declarator_obstack_base;
4559 bool success;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator)
4564 gcc_obstack_init (&declarator_obstack);
4565 /* Create the error declarator. */
4566 cp_error_declarator = make_declarator (cdk_error);
4567 /* Create the empty parameter list. */
4568 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4569 UNKNOWN_LOCATION);
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4574 cp_parser_declaration_seq_opt (parser);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser->lexer);
4581 parser->lexer = NULL;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser->implicit_extern_c)
4587 pop_lang_context ();
4588 parser->implicit_extern_c = false;
4591 /* Finish up. */
4592 finish_translation_unit ();
4594 success = true;
4596 else
4598 cp_parser_error (parser, "expected declaration");
4599 success = false;
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack)
4604 == declarator_obstack_base);
4606 /* All went well. */
4607 return success;
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p)
4616 tsubst_flags_t complain = tf_warning_or_error;
4617 if (decltype_p)
4618 complain |= tf_decltype;
4619 return complain;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser *parser)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4630 return 0;
4632 cp_parser_parse_tentatively (parser);
4633 cp_parser_commit_to_topmost_tentative_parse (parser);
4634 return cp_lexer_token_position (parser->lexer, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4640 static void
4641 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4642 tree expr)
4644 if (!start)
4645 return;
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4651 token->type = CPP_PREPARSED_EXPR;
4652 token->u.value = expr;
4653 token->keyword = RID_MAX;
4654 cp_lexer_purge_tokens_after (parser->lexer, start);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4664 cp_parser *parser;
4665 bool set;
4667 tentative_firewall (cp_parser *p): parser(p)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4673 cp_parser_parse_tentatively (parser);
4674 cp_parser_commit_to_topmost_tentative_parse (parser);
4675 cp_parser_parse_tentatively (parser);
4679 ~tentative_firewall()
4681 if (set)
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err = cp_parser_error_occurred (parser);
4686 cp_parser_parse_definitely (parser);
4687 cp_parser_parse_definitely (parser);
4688 if (err)
4689 cp_parser_simulate_error (parser);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4699 problem. */
4701 template <typename traits_t>
4702 class token_pair
4704 public:
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4709 return true.
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser *parser)
4715 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4716 return cp_parser_require (parser, traits_t::open_token_type,
4717 traits_t::required_token_open);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token * consume_open (cp_parser *parser)
4725 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4726 gcc_assert (tok->type == traits_t::open_token_type);
4727 m_open_loc = tok->location;
4728 return tok;
4731 /* If the next token is the closing symbol for this pair, consume it
4732 and return it.
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token *require_close (cp_parser *parser) const
4738 return cp_parser_require (parser, traits_t::close_token_type,
4739 traits_t::required_token_close,
4740 m_open_loc);
4743 private:
4744 location_t m_open_loc;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4752 static const enum required_token required_token_open = RT_OPEN_PAREN;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4754 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair<matching_paren_traits> matching_parens;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4767 static const enum required_token required_token_open = RT_OPEN_BRACE;
4768 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4769 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4773 pairs of braces. */
4775 typedef token_pair<matching_brace_traits> matching_braces;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4781 static cp_expr
4782 cp_parser_statement_expr (cp_parser *parser)
4784 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4786 /* Consume the '('. */
4787 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4788 matching_parens parens;
4789 parens.consume_open (parser);
4790 /* Start the statement-expression. */
4791 tree expr = begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4794 /* Finish up. */
4795 expr = finish_stmt_expr (expr, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4798 if (!parens.require_close (parser))
4799 cp_parser_skip_to_end_of_statement (parser);
4801 cp_parser_end_tentative_firewall (parser, start, expr);
4802 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4803 return cp_expr (expr, combined_loc);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4810 fold-operator:
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4820 static int
4821 cp_parser_fold_operator (cp_token *token)
4823 switch (token->type)
4825 case CPP_PLUS: return PLUS_EXPR;
4826 case CPP_MINUS: return MINUS_EXPR;
4827 case CPP_MULT: return MULT_EXPR;
4828 case CPP_DIV: return TRUNC_DIV_EXPR;
4829 case CPP_MOD: return TRUNC_MOD_EXPR;
4830 case CPP_XOR: return BIT_XOR_EXPR;
4831 case CPP_AND: return BIT_AND_EXPR;
4832 case CPP_OR: return BIT_IOR_EXPR;
4833 case CPP_LSHIFT: return LSHIFT_EXPR;
4834 case CPP_RSHIFT: return RSHIFT_EXPR;
4836 case CPP_EQ: return -NOP_EXPR;
4837 case CPP_PLUS_EQ: return -PLUS_EXPR;
4838 case CPP_MINUS_EQ: return -MINUS_EXPR;
4839 case CPP_MULT_EQ: return -MULT_EXPR;
4840 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4841 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4842 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4843 case CPP_AND_EQ: return -BIT_AND_EXPR;
4844 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4845 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4846 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4848 case CPP_EQ_EQ: return EQ_EXPR;
4849 case CPP_NOT_EQ: return NE_EXPR;
4850 case CPP_LESS: return LT_EXPR;
4851 case CPP_GREATER: return GT_EXPR;
4852 case CPP_LESS_EQ: return LE_EXPR;
4853 case CPP_GREATER_EQ: return GE_EXPR;
4855 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4856 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4858 case CPP_COMMA: return COMPOUND_EXPR;
4860 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4861 case CPP_DEREF_STAR: return MEMBER_REF;
4863 default: return ERROR_MARK;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4871 static bool
4872 is_binary_op (tree_code code)
4874 switch (code)
4876 case PLUS_EXPR:
4877 case POINTER_PLUS_EXPR:
4878 case MINUS_EXPR:
4879 case MULT_EXPR:
4880 case TRUNC_DIV_EXPR:
4881 case TRUNC_MOD_EXPR:
4882 case BIT_XOR_EXPR:
4883 case BIT_AND_EXPR:
4884 case BIT_IOR_EXPR:
4885 case LSHIFT_EXPR:
4886 case RSHIFT_EXPR:
4888 case MODOP_EXPR:
4890 case EQ_EXPR:
4891 case NE_EXPR:
4892 case LE_EXPR:
4893 case GE_EXPR:
4894 case LT_EXPR:
4895 case GT_EXPR:
4897 case TRUTH_ANDIF_EXPR:
4898 case TRUTH_ORIF_EXPR:
4900 case COMPOUND_EXPR:
4902 case DOTSTAR_EXPR:
4903 case MEMBER_REF:
4904 return true;
4906 default:
4907 return false;
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4914 static int
4915 cp_parser_fold_operator (cp_parser *parser)
4917 cp_token* token = cp_lexer_peek_token (parser->lexer);
4918 int code = cp_parser_fold_operator (token);
4919 if (code != ERROR_MARK)
4920 cp_lexer_consume_token (parser->lexer);
4921 return code;
4924 /* Parse a fold-expression.
4926 fold-expression:
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4933 static cp_expr
4934 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4936 cp_id_kind pidk;
4938 // Left fold.
4939 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4941 cp_lexer_consume_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4949 tree expr = cp_parser_cast_expression (parser, false, false,
4950 false, &pidk);
4951 if (expr == error_mark_node)
4952 return error_mark_node;
4953 return finish_left_unary_fold_expr (expr, op);
4956 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4957 int op = cp_parser_fold_operator (parser);
4958 if (op == ERROR_MARK)
4960 cp_parser_error (parser, "expected binary operator");
4961 return error_mark_node;
4964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4966 cp_parser_error (parser, "expected ...");
4967 return error_mark_node;
4969 cp_lexer_consume_token (parser->lexer);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1)))
4977 error_at (location_of (expr1),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1)
4981 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4982 error_at (location_of (expr1),
4983 "conditional expression in operand of fold-expression");
4985 // Right fold.
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4987 return finish_right_unary_fold_expr (expr1, op);
4989 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4991 cp_parser_error (parser, "mismatched operator in fold-expression");
4992 return error_mark_node;
4994 cp_lexer_consume_token (parser->lexer);
4996 // Binary left or right fold.
4997 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4998 if (expr2 == error_mark_node)
4999 return error_mark_node;
5000 return finish_binary_fold_expr (expr1, expr2, op);
5003 /* Parse a primary-expression.
5005 primary-expression:
5006 literal
5007 this
5008 ( expression )
5009 id-expression
5010 lambda-expression (C++11)
5012 GNU Extensions:
5014 primary-expression:
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5019 C++ Extensions:
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5043 primary-expression:
5044 objc-expression
5046 literal:
5047 __null
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5057 static cp_expr
5058 cp_parser_primary_expression (cp_parser *parser,
5059 bool address_p,
5060 bool cast_p,
5061 bool template_arg_p,
5062 bool decltype_p,
5063 cp_id_kind *idk)
5065 cp_token *token = NULL;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk = CP_ID_KIND_NONE;
5070 /* Peek at the next token. */
5071 token = cp_lexer_peek_token (parser->lexer);
5072 switch ((int) token->type)
5074 /* literal:
5075 integer-literal
5076 character-literal
5077 floating-literal
5078 string-literal
5079 boolean-literal
5080 pointer-literal
5081 user-defined-literal */
5082 case CPP_CHAR:
5083 case CPP_CHAR16:
5084 case CPP_CHAR32:
5085 case CPP_WCHAR:
5086 case CPP_UTF8CHAR:
5087 case CPP_NUMBER:
5088 case CPP_PREPARSED_EXPR:
5089 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5090 return cp_parser_userdef_numeric_literal (parser);
5091 token = cp_lexer_consume_token (parser->lexer);
5092 if (TREE_CODE (token->u.value) == FIXED_CST)
5094 error_at (token->location,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token->u.value) == REAL_CST
5102 && parser->integral_constant_expression_p
5103 && pedantic)
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5108 if (cast_p)
5110 cp_token *next_token;
5112 next_token = cp_lexer_peek_token (parser->lexer);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token->type != CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token->type != CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token->type != CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token->type != CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token->type != CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token->type != CPP_GREATER
5126 || parser->greater_than_is_operator_p)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token->type != CPP_RSHIFT
5130 || (cxx_dialect == cxx98)
5131 || parser->greater_than_is_operator_p))
5132 cast_p = false;
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5139 if (!cast_p)
5140 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5142 return cp_expr (token->u.value, token->location);
5144 case CPP_CHAR_USERDEF:
5145 case CPP_CHAR16_USERDEF:
5146 case CPP_CHAR32_USERDEF:
5147 case CPP_WCHAR_USERDEF:
5148 case CPP_UTF8CHAR_USERDEF:
5149 return cp_parser_userdef_char_literal (parser);
5151 case CPP_STRING:
5152 case CPP_STRING16:
5153 case CPP_STRING32:
5154 case CPP_WSTRING:
5155 case CPP_UTF8STRING:
5156 case CPP_STRING_USERDEF:
5157 case CPP_STRING16_USERDEF:
5158 case CPP_STRING32_USERDEF:
5159 case CPP_WSTRING_USERDEF:
5160 case CPP_UTF8STRING_USERDEF:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser,
5165 parser->translate_strings_p,
5166 true);
5168 case CPP_OPEN_PAREN:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser)
5172 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token->location, OPT_Wpedantic,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser->in_function_body
5185 || parser->in_template_argument_list_p)
5187 error_at (token->location,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser);
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5192 cp_lexer_consume_token (parser->lexer);
5193 return error_mark_node;
5195 else
5196 return cp_parser_statement_expr (parser);
5198 /* Otherwise it's a normal parenthesized expression. */
5200 cp_expr expr;
5201 bool saved_greater_than_is_operator_p;
5203 location_t open_paren_loc = token->location;
5205 /* Consume the `('. */
5206 matching_parens parens;
5207 parens.consume_open (parser);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser->greater_than_is_operator_p;
5212 parser->greater_than_is_operator_p = true;
5214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5215 /* Left fold expression. */
5216 expr = NULL_TREE;
5217 else
5218 /* Parse the parenthesized expression. */
5219 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5221 token = cp_lexer_peek_token (parser->lexer);
5222 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5224 expr = cp_parser_fold_expression (parser, expr);
5225 if (expr != error_mark_node
5226 && cxx_dialect < cxx17
5227 && !in_system_header_at (input_location))
5228 pedwarn (input_location, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5231 else
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5236 not. */
5237 expr = finish_parenthesized_expr (expr);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk != CP_ID_KIND_QUALIFIED)
5244 *idk = CP_ID_KIND_NONE;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p;
5251 /* Consume the `)'. */
5252 token = cp_lexer_peek_token (parser->lexer);
5253 location_t close_paren_loc = token->location;
5254 expr.set_range (open_paren_loc, close_paren_loc);
5255 if (!parens.require_close (parser)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5257 cp_parser_skip_to_end_of_statement (parser);
5259 return expr;
5262 case CPP_OPEN_SQUARE:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser);
5268 tree msg = cp_parser_objc_message_expression (parser);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser))
5271 return msg;
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam = cp_parser_lambda_expression (parser);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser))
5277 return error_mark_node;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5279 return lam;
5282 case CPP_OBJC_STRING:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser);
5286 cp_parser_error (parser, "expected primary-expression");
5287 return error_mark_node;
5289 case CPP_KEYWORD:
5290 switch (token->keyword)
5292 /* These two are the boolean literals. */
5293 case RID_TRUE:
5294 cp_lexer_consume_token (parser->lexer);
5295 return cp_expr (boolean_true_node, token->location);
5296 case RID_FALSE:
5297 cp_lexer_consume_token (parser->lexer);
5298 return cp_expr (boolean_false_node, token->location);
5300 /* The `__null' literal. */
5301 case RID_NULL:
5302 cp_lexer_consume_token (parser->lexer);
5303 return cp_expr (null_node, token->location);
5305 /* The `nullptr' literal. */
5306 case RID_NULLPTR:
5307 cp_lexer_consume_token (parser->lexer);
5308 return cp_expr (nullptr_node, token->location);
5310 /* Recognize the `this' keyword. */
5311 case RID_THIS:
5312 cp_lexer_consume_token (parser->lexer);
5313 if (parser->local_variables_forbidden_p)
5315 error_at (token->location,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5321 return error_mark_node;
5322 return cp_expr (finish_this_expr (), token->location);
5324 /* The `operator' keyword can be the beginning of an
5325 id-expression. */
5326 case RID_OPERATOR:
5327 goto id_expression;
5329 case RID_FUNCTION_NAME:
5330 case RID_PRETTY_FUNCTION_NAME:
5331 case RID_C99_FUNCTION_NAME:
5333 non_integral_constant name;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token = cp_lexer_consume_token (parser->lexer);
5344 switch (token->keyword)
5346 case RID_FUNCTION_NAME:
5347 name = NIC_FUNC_NAME;
5348 break;
5349 case RID_PRETTY_FUNCTION_NAME:
5350 name = NIC_PRETTY_FUNC;
5351 break;
5352 case RID_C99_FUNCTION_NAME:
5353 name = NIC_C99_FUNC;
5354 break;
5355 default:
5356 gcc_unreachable ();
5359 if (cp_parser_non_integral_constant_expression (parser, name))
5360 return error_mark_node;
5362 /* Look up the name. */
5363 return finish_fname (token->u.value);
5366 case RID_VA_ARG:
5368 tree expression;
5369 tree type;
5370 source_location type_location;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser->lexer)->location;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser->lexer);
5376 /* Look for the opening `('. */
5377 matching_parens parens;
5378 parens.require_open (parser);
5379 /* Now, parse the assignment-expression. */
5380 expression = cp_parser_assignment_expression (parser);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5383 type_location = cp_lexer_peek_token (parser->lexer)->location;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel s (parser);
5387 type = cp_parser_type_id (parser);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser->lexer)->location;
5392 parens.require_close (parser);
5393 /* Using `va_arg' in a constant-expression is not
5394 allowed. */
5395 if (cp_parser_non_integral_constant_expression (parser,
5396 NIC_VA_ARG))
5397 return error_mark_node;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location, start_loc, finish_loc);
5405 return build_x_va_arg (combined_loc, expression, type);
5408 case RID_OFFSETOF:
5409 return cp_parser_builtin_offsetof (parser);
5411 case RID_HAS_NOTHROW_ASSIGN:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR:
5413 case RID_HAS_NOTHROW_COPY:
5414 case RID_HAS_TRIVIAL_ASSIGN:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5416 case RID_HAS_TRIVIAL_COPY:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR:
5420 case RID_IS_ABSTRACT:
5421 case RID_IS_AGGREGATE:
5422 case RID_IS_BASE_OF:
5423 case RID_IS_CLASS:
5424 case RID_IS_EMPTY:
5425 case RID_IS_ENUM:
5426 case RID_IS_FINAL:
5427 case RID_IS_LITERAL_TYPE:
5428 case RID_IS_POD:
5429 case RID_IS_POLYMORPHIC:
5430 case RID_IS_SAME_AS:
5431 case RID_IS_STD_LAYOUT:
5432 case RID_IS_TRIVIAL:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5435 case RID_IS_TRIVIALLY_COPYABLE:
5436 case RID_IS_UNION:
5437 case RID_IS_ASSIGNABLE:
5438 case RID_IS_CONSTRUCTIBLE:
5439 return cp_parser_trait_expr (parser, token->keyword);
5441 // C++ concepts
5442 case RID_REQUIRES:
5443 return cp_parser_requires_expression (parser);
5445 /* Objective-C++ expressions. */
5446 case RID_AT_ENCODE:
5447 case RID_AT_PROTOCOL:
5448 case RID_AT_SELECTOR:
5449 return cp_parser_objc_expression (parser);
5451 case RID_TEMPLATE:
5452 if (parser->in_function_body
5453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5454 == CPP_LESS))
5456 error_at (token->location,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser);
5459 return error_mark_node;
5461 /* FALLTHRU */
5462 default:
5463 cp_parser_error (parser, "expected primary-expression");
5464 return error_mark_node;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5469 keyword. */
5470 case CPP_NAME:
5471 case CPP_SCOPE:
5472 case CPP_TEMPLATE_ID:
5473 case CPP_NESTED_NAME_SPECIFIER:
5475 id_expression:
5476 cp_expr id_expression;
5477 cp_expr decl;
5478 const char *error_msg;
5479 bool template_p;
5480 bool done;
5481 cp_token *id_expr_token;
5483 /* Parse the id-expression. */
5484 id_expression
5485 = cp_parser_id_expression (parser,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5488 &template_p,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression == error_mark_node)
5492 return error_mark_node;
5493 id_expr_token = token;
5494 token = cp_lexer_peek_token (parser->lexer);
5495 done = (token->type != CPP_OPEN_SQUARE
5496 && token->type != CPP_OPEN_PAREN
5497 && token->type != CPP_DOT
5498 && token->type != CPP_DEREF
5499 && token->type != CPP_PLUS_PLUS
5500 && token->type != CPP_MINUS_MINUS);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression) == TYPE_DECL)
5506 decl = id_expression;
5507 /* Look up the name. */
5508 else
5510 tree ambiguous_decls;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5514 to check again. */
5515 if (id_expr_token->type == CPP_NAME
5516 && id_expr_token->error_reported)
5518 cp_parser_simulate_error (parser);
5519 return error_mark_node;
5522 decl = cp_parser_lookup_name (parser, id_expression,
5523 none_type,
5524 template_p,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5527 &ambiguous_decls,
5528 id_expr_token->location);
5529 /* If the lookup was ambiguous, an error will already have
5530 been issued. */
5531 if (ambiguous_decls)
5532 return error_mark_node;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5538 && TREE_CODE (decl) == TYPE_DECL
5539 && objc_is_class_name (decl))
5541 tree component;
5542 cp_lexer_consume_token (parser->lexer);
5543 component = cp_parser_identifier (parser);
5544 if (component == error_mark_node)
5545 return error_mark_node;
5547 tree result = objc_build_class_component_ref (id_expression,
5548 component);
5549 /* Build a location of the form:
5550 expr.component
5551 ~~~~~^~~~~~~~~
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location, id_expression.get_start (),
5557 get_finish (input_location));
5558 protected_set_expr_location (result, combined_loc);
5559 return result;
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5574 id_expression);
5575 if (decl_tree != decl.get_value ())
5576 decl = cp_expr (decl_tree);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl) == SCOPE_REF)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5587 int a[B<N>::i];
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5592 return decl;
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser->local_variables_forbidden_p
5597 && local_variable_p (decl))
5599 error_at (id_expr_token->location,
5600 "local variable %qD may not appear in this context",
5601 decl.get_value ());
5602 return error_mark_node;
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5640 /* Parse an id-expression.
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5735 return unqualified_id;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5741 cp_token *token;
5742 tree id;
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5770 switch (token->type)
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5792 /* Parse an unqualified-id.
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5821 cp_token *token;
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5826 switch ((int) token->type)
5828 case CPP_NAME:
5830 tree id;
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5853 case CPP_COMPL:
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5884 struct S {
5885 int S;
5886 ~S();
5889 S::~S() {}
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5920 gcc_assert (!scope || TYPE_P (scope));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6005 /* Look in the surrounding context. */
6006 if (!done)
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6059 /* [class.dtor]
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6077 cp_expr id;
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6098 return id;
6100 /* Fall through. */
6102 default:
6103 if (optional_p)
6104 return NULL_TREE;
6105 cp_parser_error (parser, "expected unqualified-id");
6106 return error_mark_node;
6110 /* Parse an (optional) nested-name-specifier.
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6116 nested-name-specifier: [C++0x]
6117 type-name ::
6118 namespace-name ::
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6125 in name lookups.
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6135 static tree
6136 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6137 bool typename_keyword_p,
6138 bool check_dependency_p,
6139 bool type_p,
6140 bool is_declaration,
6141 bool template_keyword_p /* = false */)
6143 bool success = false;
6144 cp_token_position start = 0;
6145 cp_token *token;
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6150 start = cp_lexer_token_position (parser->lexer, false);
6151 push_deferring_access_checks (dk_deferred);
6154 while (true)
6156 tree new_scope;
6157 tree old_scope;
6158 tree saved_qualifying_scope;
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token = cp_lexer_peek_token (parser->lexer);
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6173 if (is_declaration
6174 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6176 new_scope = resolve_typename_type (parser->scope,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6179 parser->scope = new_scope;
6181 success = true;
6182 continue;
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success && token->keyword == RID_TEMPLATE)
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token->type == CPP_TEMPLATE_ID)
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token))
6196 else
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token->type != CPP_NAME)
6201 break;
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6207 if (token->type == CPP_COLON
6208 && parser->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6211 gcc_rich_location richloc (token->location);
6212 richloc.add_fixit_replace ("::");
6213 error_at (&richloc,
6214 "found %<:%> in nested-name-specifier, "
6215 "expected %<::%>");
6216 token->type = CPP_SCOPE;
6219 if (token->type != CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6221 (parser, 2))
6222 break;
6225 /* The nested-name-specifier is optional, so we parse
6226 tentatively. */
6227 cp_parser_parse_tentatively (parser);
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6231 if (success)
6232 template_keyword_p = cp_parser_optional_template_keyword (parser);
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope = parser->scope;
6237 saved_qualifying_scope = parser->qualifying_scope;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6242 if (is_declaration
6243 && !typename_keyword_p
6244 && parser->scope
6245 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6246 parser->scope = resolve_typename_type (parser->scope,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6249 new_scope
6250 = cp_parser_qualifying_entity (parser,
6251 typename_keyword_p,
6252 template_keyword_p,
6253 check_dependency_p,
6254 type_p,
6255 is_declaration);
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6259 /* If we found what we wanted, we keep going; otherwise, we're
6260 done. */
6261 if (!cp_parser_parse_definitely (parser))
6263 bool error_p = false;
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser->scope = old_scope;
6269 parser->qualifying_scope = saved_qualifying_scope;
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6278 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6279 == CPP_SCOPE))
6281 token = cp_lexer_consume_token (parser->lexer);
6282 error_at (token->location, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token->u.tree_check_value->value);
6285 parser->scope = error_mark_node;
6286 error_p = true;
6287 /* As below. */
6288 success = true;
6289 cp_lexer_consume_token (parser->lexer);
6292 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6293 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token = cp_lexer_peek_token (parser->lexer);
6298 tree tid = token->u.tree_check_value->value;
6299 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6302 tree tmpl = NULL_TREE;
6303 if (is_overloaded_fn (tid))
6305 tree fns = get_fns (tid);
6306 if (OVL_SINGLE_P (fns))
6307 tmpl = OVL_FIRST (fns);
6308 error_at (token->location, "function template-id %qD "
6309 "in nested-name-specifier", tid);
6311 else
6313 /* Variable template. */
6314 tmpl = TREE_OPERAND (tid, 0);
6315 gcc_assert (variable_template_p (tmpl));
6316 error_at (token->location, "variable template-id %qD "
6317 "in nested-name-specifier", tid);
6319 if (tmpl)
6320 inform (DECL_SOURCE_LOCATION (tmpl),
6321 "%qD declared here", tmpl);
6323 parser->scope = error_mark_node;
6324 error_p = true;
6325 /* As below. */
6326 success = true;
6327 cp_lexer_consume_token (parser->lexer);
6328 cp_lexer_consume_token (parser->lexer);
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6333 break;
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6339 == CPP_SCOPE)
6340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6341 != CPP_COMPL))
6343 token = cp_lexer_consume_token (parser->lexer);
6344 if (!error_p)
6346 if (!token->error_reported)
6348 tree decl;
6349 tree ambiguous_decls;
6351 decl = cp_parser_lookup_name (parser, token->u.value,
6352 none_type,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6356 &ambiguous_decls,
6357 token->location);
6358 if (TREE_CODE (decl) == TEMPLATE_DECL)
6359 error_at (token->location,
6360 "%qD used without template arguments",
6361 decl);
6362 else if (ambiguous_decls)
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6367 (parser))
6369 error_at (token->location,
6370 "reference to %qD is ambiguous",
6371 token->u.value);
6372 print_candidates (ambiguous_decls);
6374 decl = error_mark_node;
6376 else
6378 if (cxx_dialect != cxx98)
6379 cp_parser_name_lookup_error
6380 (parser, token->u.value, decl, NLE_NOT_CXX98,
6381 token->location);
6382 else
6383 cp_parser_name_lookup_error
6384 (parser, token->u.value, decl, NLE_CXX98,
6385 token->location);
6388 parser->scope = error_mark_node;
6389 error_p = true;
6390 /* Treat this as a successful nested-name-specifier
6391 due to:
6393 [basic.lookup.qual]
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6398 success = true;
6400 cp_lexer_consume_token (parser->lexer);
6402 break;
6404 /* We've found one valid nested-name-specifier. */
6405 success = true;
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope) == TYPE_DECL)
6408 new_scope = TREE_TYPE (new_scope);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6415 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6417 == TEMPLATE_ID_EXPR)))
6418 permerror (input_location, TYPE_P (new_scope)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6421 new_scope);
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope))
6431 new_scope = complete_type (new_scope);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6434 it yet. */
6435 if (!COMPLETE_TYPE_P (new_scope)
6436 && currently_open_class (new_scope))
6437 new_scope = TYPE_MAIN_VARIANT (new_scope);
6439 /* Make sure we look in the right scope the next time through
6440 the loop. */
6441 parser->scope = new_scope;
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success && start)
6451 cp_token *token;
6453 token = cp_lexer_token_at (parser->lexer, start);
6454 /* Reset the contents of the START token. */
6455 token->type = CPP_NESTED_NAME_SPECIFIER;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6459 token->u.tree_check_value->value = parser->scope;
6460 token->u.tree_check_value->checks = get_deferred_access_checks ();
6461 token->u.tree_check_value->qualifying_scope =
6462 parser->qualifying_scope;
6463 token->keyword = RID_MAX;
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser->lexer, start);
6469 if (start)
6470 pop_to_parent_deferring_access_checks ();
6472 return success ? parser->scope : NULL_TREE;
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6480 static tree
6481 cp_parser_nested_name_specifier (cp_parser *parser,
6482 bool typename_keyword_p,
6483 bool check_dependency_p,
6484 bool type_p,
6485 bool is_declaration)
6487 tree scope;
6489 /* Look for the nested-name-specifier. */
6490 scope = cp_parser_nested_name_specifier_opt (parser,
6491 typename_keyword_p,
6492 check_dependency_p,
6493 type_p,
6494 is_declaration);
6495 /* If it was not present, issue an error message. */
6496 if (!scope)
6498 cp_parser_error (parser, "expected nested-name-specifier");
6499 parser->scope = NULL_TREE;
6502 return scope;
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6516 scope.
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6522 static tree
6523 cp_parser_qualifying_entity (cp_parser *parser,
6524 bool typename_keyword_p,
6525 bool template_keyword_p,
6526 bool check_dependency_p,
6527 bool type_p,
6528 bool is_declaration)
6530 tree saved_scope;
6531 tree saved_qualifying_scope;
6532 tree saved_object_scope;
6533 tree scope;
6534 bool only_class_p;
6535 bool successful_parse_p;
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser->lexer))
6540 scope = cp_parser_decltype (parser);
6541 if (TREE_CODE (scope) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope))
6544 cp_parser_simulate_error (parser);
6545 return error_mark_node;
6547 if (TYPE_NAME (scope))
6548 scope = TYPE_NAME (scope);
6549 return scope;
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6554 it. */
6555 saved_scope = parser->scope;
6556 saved_qualifying_scope = parser->qualifying_scope;
6557 saved_object_scope = parser->object_scope;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p = template_keyword_p
6561 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6562 if (!only_class_p)
6563 cp_parser_parse_tentatively (parser);
6564 scope = cp_parser_class_name (parser,
6565 typename_keyword_p,
6566 template_keyword_p,
6567 type_p ? class_type : none_type,
6568 check_dependency_p,
6569 /*class_head_p=*/false,
6570 is_declaration,
6571 /*enum_ok=*/cxx_dialect > cxx98);
6572 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p && !successful_parse_p)
6576 /* Restore the saved scope. */
6577 parser->scope = saved_scope;
6578 parser->qualifying_scope = saved_qualifying_scope;
6579 parser->object_scope = saved_object_scope;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6585 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6586 return error_mark_node;
6587 scope = cp_parser_namespace_name (parser);
6590 return scope;
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6595 static bool
6596 cp_parser_compound_literal_p (cp_parser *parser)
6598 cp_lexer_save_tokens (parser->lexer);
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser->lexer);
6611 return compound_literal_p;
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6617 bool
6618 literal_integer_zerop (const_tree expr)
6620 return (location_wrapper_p (expr)
6621 && integer_zerop (TREE_OPERAND (expr, 0)));
6624 /* Parse a postfix-expression.
6626 postfix-expression:
6627 primary-expression
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6646 typeid ( type-id )
6648 GNU Extension:
6650 postfix-expression:
6651 ( type-id ) { initializer-list , [opt] }
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6659 cast.
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6664 Returns a representation of the expression. */
6666 static cp_expr
6667 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6668 bool member_access_only_p, bool decltype_p,
6669 cp_id_kind * pidk_return)
6671 cp_token *token;
6672 location_t loc;
6673 enum rid keyword;
6674 cp_id_kind idk = CP_ID_KIND_NONE;
6675 cp_expr postfix_expression = NULL_TREE;
6676 bool is_member_access = false;
6678 /* Peek at the next token. */
6679 token = cp_lexer_peek_token (parser->lexer);
6680 loc = token->location;
6681 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6683 /* Some of the productions are determined by keywords. */
6684 keyword = token->keyword;
6685 switch (keyword)
6687 case RID_DYNCAST:
6688 case RID_STATCAST:
6689 case RID_REINTCAST:
6690 case RID_CONSTCAST:
6692 tree type;
6693 cp_expr expression;
6694 const char *saved_message;
6695 bool saved_in_type_id_in_expr_p;
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser->lexer);
6702 /* New types cannot be defined in the cast. */
6703 saved_message = parser->type_definition_forbidden_message;
6704 parser->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser, CPP_LESS, RT_LESS);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6711 parser->in_type_id_in_expr_p = true;
6712 type = cp_parser_type_id (parser);
6713 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6716 /* Restore the old message. */
6717 parser->type_definition_forbidden_message = saved_message;
6719 bool saved_greater_than_is_operator_p
6720 = parser->greater_than_is_operator_p;
6721 parser->greater_than_is_operator_p = true;
6723 /* And the expression which is being cast. */
6724 matching_parens parens;
6725 parens.require_open (parser);
6726 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6727 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6728 RT_CLOSE_PAREN);
6729 location_t end_loc = close_paren ?
6730 close_paren->location : UNKNOWN_LOCATION;
6732 parser->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p;
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type)
6738 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6740 postfix_expression = error_mark_node;
6741 break;
6744 switch (keyword)
6746 case RID_DYNCAST:
6747 postfix_expression
6748 = build_dynamic_cast (type, expression, tf_warning_or_error);
6749 break;
6750 case RID_STATCAST:
6751 postfix_expression
6752 = build_static_cast (type, expression, tf_warning_or_error);
6753 break;
6754 case RID_REINTCAST:
6755 postfix_expression
6756 = build_reinterpret_cast (type, expression,
6757 tf_warning_or_error);
6758 break;
6759 case RID_CONSTCAST:
6760 postfix_expression
6761 = build_const_cast (type, expression, tf_warning_or_error);
6762 break;
6763 default:
6764 gcc_unreachable ();
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6773 postfix_expression.set_location (cp_cast_loc);
6775 break;
6777 case RID_TYPEID:
6779 tree type;
6780 const char *saved_message;
6781 bool saved_in_type_id_in_expr_p;
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser->lexer);
6785 /* Look for the `(' token. */
6786 matching_parens parens;
6787 parens.require_open (parser);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message = parser->type_definition_forbidden_message;
6790 parser->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6793 expression. */
6794 cp_parser_parse_tentatively (parser);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6797 parser->in_type_id_in_expr_p = true;
6798 type = cp_parser_type_id (parser);
6799 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token *close_paren = parens.require_close (parser);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser))
6806 postfix_expression = get_typeid (type, tf_warning_or_error);
6807 /* Otherwise, fall back to the expression variant. */
6808 else
6810 tree expression;
6812 /* Look for an expression. */
6813 expression = cp_parser_expression (parser, & idk);
6814 /* Compute its typeid. */
6815 postfix_expression = build_typeid (expression, tf_warning_or_error);
6816 /* Look for the `)' token. */
6817 close_paren = parens.require_close (parser);
6819 /* Restore the saved message. */
6820 parser->type_definition_forbidden_message = saved_message;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6823 postfix_expression = error_mark_node;
6825 /* Construct a location e.g. :
6826 typeid (expr)
6827 ^~~~~~~~~~~~~
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6830 if (close_paren)
6832 location_t typeid_loc
6833 = make_location (start_loc, start_loc, close_paren->location);
6834 postfix_expression.set_location (typeid_loc);
6835 postfix_expression.maybe_add_location_wrapper ();
6838 break;
6840 case RID_TYPENAME:
6842 tree type;
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser->prevent_constrained_type_specifiers;
6846 type = cp_parser_elaborated_type_specifier (parser,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser->prevent_constrained_type_specifiers;
6850 postfix_expression = cp_parser_functional_cast (parser, type);
6852 break;
6854 case RID_ADDRESSOF:
6855 case RID_BUILTIN_SHUFFLE:
6856 case RID_BUILTIN_LAUNDER:
6858 vec<tree, va_gc> *vec;
6859 unsigned int i;
6860 tree p;
6862 cp_lexer_consume_token (parser->lexer);
6863 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL);
6866 if (vec == NULL)
6868 postfix_expression = error_mark_node;
6869 break;
6872 FOR_EACH_VEC_ELT (*vec, i, p)
6873 mark_exp_read (p);
6875 switch (keyword)
6877 case RID_ADDRESSOF:
6878 if (vec->length () == 1)
6879 postfix_expression
6880 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6881 else
6883 error_at (loc, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression = error_mark_node;
6887 break;
6889 case RID_BUILTIN_LAUNDER:
6890 if (vec->length () == 1)
6891 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6892 tf_warning_or_error);
6893 else
6895 error_at (loc, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression = error_mark_node;
6899 break;
6901 case RID_BUILTIN_SHUFFLE:
6902 if (vec->length () == 2)
6903 postfix_expression
6904 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6905 (*vec)[1], tf_warning_or_error);
6906 else if (vec->length () == 3)
6907 postfix_expression
6908 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6909 (*vec)[2], tf_warning_or_error);
6910 else
6912 error_at (loc, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression = error_mark_node;
6916 break;
6918 default:
6919 gcc_unreachable ();
6921 break;
6924 default:
6926 tree type;
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser);
6933 /* Look for the simple-type-specifier. */
6934 ++parser->prevent_constrained_type_specifiers;
6935 type = cp_parser_simple_type_specifier (parser,
6936 /*decl_specs=*/NULL,
6937 CP_PARSER_FLAGS_NONE);
6938 --parser->prevent_constrained_type_specifiers;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser))
6941 postfix_expression
6942 = cp_parser_functional_cast (parser, type);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser))
6945 break;
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser)
6950 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6952 cp_expr initializer = NULL_TREE;
6954 cp_parser_parse_tentatively (parser);
6956 matching_parens parens;
6957 parens.consume_open (parser);
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser))
6962 cp_parser_simulate_error (parser);
6963 else
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6967 parser->in_type_id_in_expr_p = true;
6968 type = cp_parser_type_id (parser);
6969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6970 parens.require_close (parser);
6973 /* If things aren't going well, there's no need to
6974 keep going. */
6975 if (!cp_parser_error_occurred (parser))
6977 bool non_constant_p;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer = cp_parser_braced_list (parser,
6980 &non_constant_p);
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser))
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location, OPT_Wpedantic,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser,
7000 NIC_NCC))
7002 postfix_expression = error_mark_node;
7003 break;
7005 /* Form the representation of the compound-literal. */
7006 postfix_expression
7007 = finish_compound_literal (type, initializer,
7008 tf_warning_or_error, fcl_c99);
7009 postfix_expression.set_location (initializer.get_location ());
7010 break;
7014 /* It must be a primary-expression. */
7015 postfix_expression
7016 = cp_parser_primary_expression (parser, address_p, cast_p,
7017 /*template_arg_p=*/false,
7018 decltype_p,
7019 &idk);
7021 break;
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7027 all these cases. */
7029 /* Keep looping until the postfix-expression is complete. */
7030 while (true)
7032 if (idk == CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression)
7034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7035 /* It is not a Koenig lookup function call. */
7036 postfix_expression
7037 = unqualified_name_lookup_error (postfix_expression);
7039 /* Peek at the next token. */
7040 token = cp_lexer_peek_token (parser->lexer);
7042 switch (token->type)
7044 case CPP_OPEN_SQUARE:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser))
7047 cp_parser_error (parser,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node;
7052 postfix_expression
7053 = cp_parser_postfix_open_square_expression (parser,
7054 postfix_expression,
7055 false,
7056 decltype_p);
7057 postfix_expression.set_range (start_loc,
7058 postfix_expression.get_location ());
7060 idk = CP_ID_KIND_NONE;
7061 is_member_access = false;
7062 break;
7064 case CPP_OPEN_PAREN:
7065 /* postfix-expression ( expression-list [opt] ) */
7067 bool koenig_p;
7068 bool is_builtin_constant_p;
7069 bool saved_integral_constant_expression_p = false;
7070 bool saved_non_integral_constant_expression_p = false;
7071 tsubst_flags_t complain = complain_flags (decltype_p);
7072 vec<tree, va_gc> *args;
7073 location_t close_paren_loc = UNKNOWN_LOCATION;
7075 is_member_access = false;
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7079 if (is_builtin_constant_p)
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser->integral_constant_expression_p;
7085 saved_non_integral_constant_expression_p
7086 = parser->non_integral_constant_expression_p;
7087 parser->integral_constant_expression_p = false;
7089 args = (cp_parser_parenthesized_expression_list
7090 (parser, non_attr,
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL,
7093 /*close_paren_loc=*/&close_paren_loc,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p)
7097 parser->integral_constant_expression_p
7098 = saved_integral_constant_expression_p;
7099 parser->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p;
7103 if (args == NULL)
7105 postfix_expression = error_mark_node;
7106 break;
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7112 && cp_parser_non_integral_constant_expression (parser,
7113 NIC_FUNC_CALL))
7115 postfix_expression = error_mark_node;
7116 release_tree_vector (args);
7117 break;
7120 koenig_p = false;
7121 if (idk == CP_ID_KIND_UNQUALIFIED
7122 || idk == CP_ID_KIND_TEMPLATE_ID)
7124 if (identifier_p (postfix_expression))
7126 if (!args->is_empty ())
7128 koenig_p = true;
7129 if (!any_type_dependent_arguments_p (args))
7130 postfix_expression
7131 = perform_koenig_lookup (postfix_expression, args,
7132 complain);
7134 else
7135 postfix_expression
7136 = unqualified_fn_lookup_error (postfix_expression);
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args->is_empty ()
7142 && is_overloaded_fn (postfix_expression))
7144 tree fn = get_first_fn (postfix_expression);
7145 fn = STRIP_TEMPLATE (fn);
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn)
7151 && !DECL_LOCAL_FUNCTION_P (fn))
7153 koenig_p = true;
7154 if (!any_type_dependent_arguments_p (args))
7155 postfix_expression
7156 = perform_koenig_lookup (postfix_expression, args,
7157 complain);
7162 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7164 tree instance = TREE_OPERAND (postfix_expression, 0);
7165 tree fn = TREE_OPERAND (postfix_expression, 1);
7167 if (processing_template_decl
7168 && (type_dependent_object_expression_p (instance)
7169 || (!BASELINK_P (fn)
7170 && TREE_CODE (fn) != FIELD_DECL)
7171 || type_dependent_expression_p (fn)
7172 || any_type_dependent_arguments_p (args)))
7174 maybe_generic_this_capture (instance, fn);
7175 postfix_expression
7176 = build_min_nt_call_vec (postfix_expression, args);
7177 release_tree_vector (args);
7178 break;
7181 if (BASELINK_P (fn))
7183 postfix_expression
7184 = (build_new_method_call
7185 (instance, fn, &args, NULL_TREE,
7186 (idk == CP_ID_KIND_QUALIFIED
7187 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7188 : LOOKUP_NORMAL),
7189 /*fn_p=*/NULL,
7190 complain));
7192 else
7193 postfix_expression
7194 = finish_call_expr (postfix_expression, &args,
7195 /*disallow_virtual=*/false,
7196 /*koenig_p=*/false,
7197 complain);
7199 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7200 || TREE_CODE (postfix_expression) == MEMBER_REF
7201 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7202 postfix_expression = (build_offset_ref_call_from_tree
7203 (postfix_expression, &args,
7204 complain));
7205 else if (idk == CP_ID_KIND_QUALIFIED)
7206 /* A call to a static class member, or a namespace-scope
7207 function. */
7208 postfix_expression
7209 = finish_call_expr (postfix_expression, &args,
7210 /*disallow_virtual=*/true,
7211 koenig_p,
7212 complain);
7213 else
7214 /* All other function calls. */
7215 postfix_expression
7216 = finish_call_expr (postfix_expression, &args,
7217 /*disallow_virtual=*/false,
7218 koenig_p,
7219 complain);
7221 if (close_paren_loc != UNKNOWN_LOCATION)
7223 location_t combined_loc = make_location (token->location,
7224 start_loc,
7225 close_paren_loc);
7226 postfix_expression.set_location (combined_loc);
7229 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7230 idk = CP_ID_KIND_NONE;
7232 release_tree_vector (args);
7234 break;
7236 case CPP_DOT:
7237 case CPP_DEREF:
7238 /* postfix-expression . template [opt] id-expression
7239 postfix-expression . pseudo-destructor-name
7240 postfix-expression -> template [opt] id-expression
7241 postfix-expression -> pseudo-destructor-name */
7243 /* Consume the `.' or `->' operator. */
7244 cp_lexer_consume_token (parser->lexer);
7246 postfix_expression
7247 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7248 postfix_expression,
7249 false, &idk, loc);
7251 is_member_access = true;
7252 break;
7254 case CPP_PLUS_PLUS:
7255 /* postfix-expression ++ */
7256 /* Consume the `++' token. */
7257 cp_lexer_consume_token (parser->lexer);
7258 /* Generate a representation for the complete expression. */
7259 postfix_expression
7260 = finish_increment_expr (postfix_expression,
7261 POSTINCREMENT_EXPR);
7262 /* Increments may not appear in constant-expressions. */
7263 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7264 postfix_expression = error_mark_node;
7265 idk = CP_ID_KIND_NONE;
7266 is_member_access = false;
7267 break;
7269 case CPP_MINUS_MINUS:
7270 /* postfix-expression -- */
7271 /* Consume the `--' token. */
7272 cp_lexer_consume_token (parser->lexer);
7273 /* Generate a representation for the complete expression. */
7274 postfix_expression
7275 = finish_increment_expr (postfix_expression,
7276 POSTDECREMENT_EXPR);
7277 /* Decrements may not appear in constant-expressions. */
7278 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7279 postfix_expression = error_mark_node;
7280 idk = CP_ID_KIND_NONE;
7281 is_member_access = false;
7282 break;
7284 default:
7285 if (pidk_return != NULL)
7286 * pidk_return = idk;
7287 if (member_access_only_p)
7288 return is_member_access
7289 ? postfix_expression
7290 : cp_expr (error_mark_node);
7291 else
7292 return postfix_expression;
7296 /* We should never get here. */
7297 gcc_unreachable ();
7298 return error_mark_node;
7301 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7302 by cp_parser_builtin_offsetof. We're looking for
7304 postfix-expression [ expression ]
7305 postfix-expression [ braced-init-list ] (C++11)
7307 FOR_OFFSETOF is set if we're being called in that context, which
7308 changes how we deal with integer constant expressions. */
7310 static tree
7311 cp_parser_postfix_open_square_expression (cp_parser *parser,
7312 tree postfix_expression,
7313 bool for_offsetof,
7314 bool decltype_p)
7316 tree index = NULL_TREE;
7317 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7318 bool saved_greater_than_is_operator_p;
7320 /* Consume the `[' token. */
7321 cp_lexer_consume_token (parser->lexer);
7323 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7324 parser->greater_than_is_operator_p = true;
7326 /* Parse the index expression. */
7327 /* ??? For offsetof, there is a question of what to allow here. If
7328 offsetof is not being used in an integral constant expression context,
7329 then we *could* get the right answer by computing the value at runtime.
7330 If we are in an integral constant expression context, then we might
7331 could accept any constant expression; hard to say without analysis.
7332 Rather than open the barn door too wide right away, allow only integer
7333 constant expressions here. */
7334 if (for_offsetof)
7335 index = cp_parser_constant_expression (parser);
7336 else
7338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7340 bool expr_nonconst_p;
7341 cp_lexer_set_source_position (parser->lexer);
7342 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7343 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7345 else
7346 index = cp_parser_expression (parser);
7349 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7351 /* Look for the closing `]'. */
7352 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7354 /* Build the ARRAY_REF. */
7355 postfix_expression = grok_array_decl (loc, postfix_expression,
7356 index, decltype_p);
7358 /* When not doing offsetof, array references are not permitted in
7359 constant-expressions. */
7360 if (!for_offsetof
7361 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7362 postfix_expression = error_mark_node;
7364 return postfix_expression;
7367 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7368 dereference of incomplete type, returns true if error_mark_node should
7369 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7370 and *DEPENDENT_P. */
7372 bool
7373 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7374 bool *dependent_p)
7376 /* In a template, be permissive by treating an object expression
7377 of incomplete type as dependent (after a pedwarn). */
7378 diagnostic_t kind = (processing_template_decl
7379 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7381 switch (TREE_CODE (*postfix_expression))
7383 case CAST_EXPR:
7384 case REINTERPRET_CAST_EXPR:
7385 case CONST_CAST_EXPR:
7386 case STATIC_CAST_EXPR:
7387 case DYNAMIC_CAST_EXPR:
7388 case IMPLICIT_CONV_EXPR:
7389 case VIEW_CONVERT_EXPR:
7390 case NON_LVALUE_EXPR:
7391 kind = DK_ERROR;
7392 break;
7393 case OVERLOAD:
7394 /* Don't emit any diagnostic for OVERLOADs. */
7395 kind = DK_IGNORED;
7396 break;
7397 default:
7398 /* Avoid clobbering e.g. DECLs. */
7399 if (!EXPR_P (*postfix_expression))
7400 kind = DK_ERROR;
7401 break;
7404 if (kind == DK_IGNORED)
7405 return false;
7407 location_t exploc = location_of (*postfix_expression);
7408 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7409 if (!MAYBE_CLASS_TYPE_P (*scope))
7410 return true;
7411 if (kind == DK_ERROR)
7412 *scope = *postfix_expression = error_mark_node;
7413 else if (processing_template_decl)
7415 *dependent_p = true;
7416 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7418 return false;
7421 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7422 by cp_parser_builtin_offsetof. We're looking for
7424 postfix-expression . template [opt] id-expression
7425 postfix-expression . pseudo-destructor-name
7426 postfix-expression -> template [opt] id-expression
7427 postfix-expression -> pseudo-destructor-name
7429 FOR_OFFSETOF is set if we're being called in that context. That sorta
7430 limits what of the above we'll actually accept, but nevermind.
7431 TOKEN_TYPE is the "." or "->" token, which will already have been
7432 removed from the stream. */
7434 static tree
7435 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7436 enum cpp_ttype token_type,
7437 cp_expr postfix_expression,
7438 bool for_offsetof, cp_id_kind *idk,
7439 location_t location)
7441 tree name;
7442 bool dependent_p;
7443 bool pseudo_destructor_p;
7444 tree scope = NULL_TREE;
7445 location_t start_loc = postfix_expression.get_start ();
7447 /* If this is a `->' operator, dereference the pointer. */
7448 if (token_type == CPP_DEREF)
7449 postfix_expression = build_x_arrow (location, postfix_expression,
7450 tf_warning_or_error);
7451 /* Check to see whether or not the expression is type-dependent and
7452 not the current instantiation. */
7453 dependent_p = type_dependent_object_expression_p (postfix_expression);
7454 /* The identifier following the `->' or `.' is not qualified. */
7455 parser->scope = NULL_TREE;
7456 parser->qualifying_scope = NULL_TREE;
7457 parser->object_scope = NULL_TREE;
7458 *idk = CP_ID_KIND_NONE;
7460 /* Enter the scope corresponding to the type of the object
7461 given by the POSTFIX_EXPRESSION. */
7462 if (!dependent_p)
7464 scope = TREE_TYPE (postfix_expression);
7465 /* According to the standard, no expression should ever have
7466 reference type. Unfortunately, we do not currently match
7467 the standard in this respect in that our internal representation
7468 of an expression may have reference type even when the standard
7469 says it does not. Therefore, we have to manually obtain the
7470 underlying type here. */
7471 scope = non_reference (scope);
7472 /* The type of the POSTFIX_EXPRESSION must be complete. */
7473 /* Unlike the object expression in other contexts, *this is not
7474 required to be of complete type for purposes of class member
7475 access (5.2.5) outside the member function body. */
7476 if (postfix_expression != current_class_ref
7477 && scope != error_mark_node
7478 && !currently_open_class (scope))
7480 scope = complete_type (scope);
7481 if (!COMPLETE_TYPE_P (scope)
7482 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7483 &dependent_p))
7484 return error_mark_node;
7487 if (!dependent_p)
7489 /* Let the name lookup machinery know that we are processing a
7490 class member access expression. */
7491 parser->context->object_type = scope;
7492 /* If something went wrong, we want to be able to discern that case,
7493 as opposed to the case where there was no SCOPE due to the type
7494 of expression being dependent. */
7495 if (!scope)
7496 scope = error_mark_node;
7497 /* If the SCOPE was erroneous, make the various semantic analysis
7498 functions exit quickly -- and without issuing additional error
7499 messages. */
7500 if (scope == error_mark_node)
7501 postfix_expression = error_mark_node;
7505 if (dependent_p)
7506 /* Tell cp_parser_lookup_name that there was an object, even though it's
7507 type-dependent. */
7508 parser->context->object_type = unknown_type_node;
7510 /* Assume this expression is not a pseudo-destructor access. */
7511 pseudo_destructor_p = false;
7513 /* If the SCOPE is a scalar type, then, if this is a valid program,
7514 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7515 is type dependent, it can be pseudo-destructor-name or something else.
7516 Try to parse it as pseudo-destructor-name first. */
7517 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7519 tree s;
7520 tree type;
7522 cp_parser_parse_tentatively (parser);
7523 /* Parse the pseudo-destructor-name. */
7524 s = NULL_TREE;
7525 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7526 &s, &type);
7527 if (dependent_p
7528 && (cp_parser_error_occurred (parser)
7529 || !SCALAR_TYPE_P (type)))
7530 cp_parser_abort_tentative_parse (parser);
7531 else if (cp_parser_parse_definitely (parser))
7533 pseudo_destructor_p = true;
7534 postfix_expression
7535 = finish_pseudo_destructor_expr (postfix_expression,
7536 s, type, location);
7540 if (!pseudo_destructor_p)
7542 /* If the SCOPE is not a scalar type, we are looking at an
7543 ordinary class member access expression, rather than a
7544 pseudo-destructor-name. */
7545 bool template_p;
7546 cp_token *token = cp_lexer_peek_token (parser->lexer);
7547 /* Parse the id-expression. */
7548 name = (cp_parser_id_expression
7549 (parser,
7550 cp_parser_optional_template_keyword (parser),
7551 /*check_dependency_p=*/true,
7552 &template_p,
7553 /*declarator_p=*/false,
7554 /*optional_p=*/false));
7555 /* In general, build a SCOPE_REF if the member name is qualified.
7556 However, if the name was not dependent and has already been
7557 resolved; there is no need to build the SCOPE_REF. For example;
7559 struct X { void f(); };
7560 template <typename T> void f(T* t) { t->X::f(); }
7562 Even though "t" is dependent, "X::f" is not and has been resolved
7563 to a BASELINK; there is no need to include scope information. */
7565 /* But we do need to remember that there was an explicit scope for
7566 virtual function calls. */
7567 if (parser->scope)
7568 *idk = CP_ID_KIND_QUALIFIED;
7570 /* If the name is a template-id that names a type, we will get a
7571 TYPE_DECL here. That is invalid code. */
7572 if (TREE_CODE (name) == TYPE_DECL)
7574 error_at (token->location, "invalid use of %qD", name);
7575 postfix_expression = error_mark_node;
7577 else
7579 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7581 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7583 error_at (token->location, "%<%D::%D%> is not a class member",
7584 parser->scope, name);
7585 postfix_expression = error_mark_node;
7587 else
7588 name = build_qualified_name (/*type=*/NULL_TREE,
7589 parser->scope,
7590 name,
7591 template_p);
7592 parser->scope = NULL_TREE;
7593 parser->qualifying_scope = NULL_TREE;
7594 parser->object_scope = NULL_TREE;
7596 if (parser->scope && name && BASELINK_P (name))
7597 adjust_result_of_qualified_name_lookup
7598 (name, parser->scope, scope);
7599 postfix_expression
7600 = finish_class_member_access_expr (postfix_expression, name,
7601 template_p,
7602 tf_warning_or_error);
7603 /* Build a location e.g.:
7604 ptr->access_expr
7605 ~~~^~~~~~~~~~~~~
7606 where the caret is at the deref token, ranging from
7607 the start of postfix_expression to the end of the access expr. */
7608 location_t end_loc
7609 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7610 location_t combined_loc
7611 = make_location (input_location, start_loc, end_loc);
7612 protected_set_expr_location (postfix_expression, combined_loc);
7616 /* We no longer need to look up names in the scope of the object on
7617 the left-hand side of the `.' or `->' operator. */
7618 parser->context->object_type = NULL_TREE;
7620 /* Outside of offsetof, these operators may not appear in
7621 constant-expressions. */
7622 if (!for_offsetof
7623 && (cp_parser_non_integral_constant_expression
7624 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7625 postfix_expression = error_mark_node;
7627 return postfix_expression;
7630 /* Parse a parenthesized expression-list.
7632 expression-list:
7633 assignment-expression
7634 expression-list, assignment-expression
7636 attribute-list:
7637 expression-list
7638 identifier
7639 identifier, expression-list
7641 CAST_P is true if this expression is the target of a cast.
7643 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7644 argument pack.
7646 WRAP_LOCATIONS_P is true if expressions within this list for which
7647 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7648 their source locations.
7650 Returns a vector of trees. Each element is a representation of an
7651 assignment-expression. NULL is returned if the ( and or ) are
7652 missing. An empty, but allocated, vector is returned on no
7653 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7654 if we are parsing an attribute list for an attribute that wants a
7655 plain identifier argument, normal_attr for an attribute that wants
7656 an expression, or non_attr if we aren't parsing an attribute list. If
7657 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7658 not all of the expressions in the list were constant.
7659 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7660 will be written to with the location of the closing parenthesis. If
7661 an error occurs, it may or may not be written to. */
7663 static vec<tree, va_gc> *
7664 cp_parser_parenthesized_expression_list (cp_parser* parser,
7665 int is_attribute_list,
7666 bool cast_p,
7667 bool allow_expansion_p,
7668 bool *non_constant_p,
7669 location_t *close_paren_loc,
7670 bool wrap_locations_p)
7672 vec<tree, va_gc> *expression_list;
7673 bool fold_expr_p = is_attribute_list != non_attr;
7674 tree identifier = NULL_TREE;
7675 bool saved_greater_than_is_operator_p;
7677 /* Assume all the expressions will be constant. */
7678 if (non_constant_p)
7679 *non_constant_p = false;
7681 matching_parens parens;
7682 if (!parens.require_open (parser))
7683 return NULL;
7685 expression_list = make_tree_vector ();
7687 /* Within a parenthesized expression, a `>' token is always
7688 the greater-than operator. */
7689 saved_greater_than_is_operator_p
7690 = parser->greater_than_is_operator_p;
7691 parser->greater_than_is_operator_p = true;
7693 cp_expr expr (NULL_TREE);
7695 /* Consume expressions until there are no more. */
7696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7697 while (true)
7699 /* At the beginning of attribute lists, check to see if the
7700 next token is an identifier. */
7701 if (is_attribute_list == id_attr
7702 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7704 cp_token *token;
7706 /* Consume the identifier. */
7707 token = cp_lexer_consume_token (parser->lexer);
7708 /* Save the identifier. */
7709 identifier = token->u.value;
7711 else
7713 bool expr_non_constant_p;
7715 /* Parse the next assignment-expression. */
7716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718 /* A braced-init-list. */
7719 cp_lexer_set_source_position (parser->lexer);
7720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7721 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7722 if (non_constant_p && expr_non_constant_p)
7723 *non_constant_p = true;
7725 else if (non_constant_p)
7727 expr = (cp_parser_constant_expression
7728 (parser, /*allow_non_constant_p=*/true,
7729 &expr_non_constant_p));
7730 if (expr_non_constant_p)
7731 *non_constant_p = true;
7733 else
7734 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7735 cast_p);
7737 if (fold_expr_p)
7738 expr = instantiate_non_dependent_expr (expr);
7740 /* If we have an ellipsis, then this is an expression
7741 expansion. */
7742 if (allow_expansion_p
7743 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7745 /* Consume the `...'. */
7746 cp_lexer_consume_token (parser->lexer);
7748 /* Build the argument pack. */
7749 expr = make_pack_expansion (expr);
7752 if (wrap_locations_p)
7753 expr.maybe_add_location_wrapper ();
7755 /* Add it to the list. We add error_mark_node
7756 expressions to the list, so that we can still tell if
7757 the correct form for a parenthesized expression-list
7758 is found. That gives better errors. */
7759 vec_safe_push (expression_list, expr.get_value ());
7761 if (expr == error_mark_node)
7762 goto skip_comma;
7765 /* After the first item, attribute lists look the same as
7766 expression lists. */
7767 is_attribute_list = non_attr;
7769 get_comma:;
7770 /* If the next token isn't a `,', then we are done. */
7771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7772 break;
7774 /* Otherwise, consume the `,' and keep going. */
7775 cp_lexer_consume_token (parser->lexer);
7778 if (close_paren_loc)
7779 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7781 if (!parens.require_close (parser))
7783 int ending;
7785 skip_comma:;
7786 /* We try and resync to an unnested comma, as that will give the
7787 user better diagnostics. */
7788 ending = cp_parser_skip_to_closing_parenthesis (parser,
7789 /*recovering=*/true,
7790 /*or_comma=*/true,
7791 /*consume_paren=*/true);
7792 if (ending < 0)
7793 goto get_comma;
7794 if (!ending)
7796 parser->greater_than_is_operator_p
7797 = saved_greater_than_is_operator_p;
7798 return NULL;
7802 parser->greater_than_is_operator_p
7803 = saved_greater_than_is_operator_p;
7805 if (identifier)
7806 vec_safe_insert (expression_list, 0, identifier);
7808 return expression_list;
7811 /* Parse a pseudo-destructor-name.
7813 pseudo-destructor-name:
7814 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7815 :: [opt] nested-name-specifier template template-id :: ~ type-name
7816 :: [opt] nested-name-specifier [opt] ~ type-name
7818 If either of the first two productions is used, sets *SCOPE to the
7819 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7820 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7821 or ERROR_MARK_NODE if the parse fails. */
7823 static void
7824 cp_parser_pseudo_destructor_name (cp_parser* parser,
7825 tree object,
7826 tree* scope,
7827 tree* type)
7829 bool nested_name_specifier_p;
7831 /* Handle ~auto. */
7832 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7833 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7834 && !type_dependent_expression_p (object))
7836 if (cxx_dialect < cxx14)
7837 pedwarn (input_location, 0,
7838 "%<~auto%> only available with "
7839 "-std=c++14 or -std=gnu++14");
7840 cp_lexer_consume_token (parser->lexer);
7841 cp_lexer_consume_token (parser->lexer);
7842 *scope = NULL_TREE;
7843 *type = TREE_TYPE (object);
7844 return;
7847 /* Assume that things will not work out. */
7848 *type = error_mark_node;
7850 /* Look for the optional `::' operator. */
7851 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7852 /* Look for the optional nested-name-specifier. */
7853 nested_name_specifier_p
7854 = (cp_parser_nested_name_specifier_opt (parser,
7855 /*typename_keyword_p=*/false,
7856 /*check_dependency_p=*/true,
7857 /*type_p=*/false,
7858 /*is_declaration=*/false)
7859 != NULL_TREE);
7860 /* Now, if we saw a nested-name-specifier, we might be doing the
7861 second production. */
7862 if (nested_name_specifier_p
7863 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7865 /* Consume the `template' keyword. */
7866 cp_lexer_consume_token (parser->lexer);
7867 /* Parse the template-id. */
7868 cp_parser_template_id (parser,
7869 /*template_keyword_p=*/true,
7870 /*check_dependency_p=*/false,
7871 class_type,
7872 /*is_declaration=*/true);
7873 /* Look for the `::' token. */
7874 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7876 /* If the next token is not a `~', then there might be some
7877 additional qualification. */
7878 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7880 /* At this point, we're looking for "type-name :: ~". The type-name
7881 must not be a class-name, since this is a pseudo-destructor. So,
7882 it must be either an enum-name, or a typedef-name -- both of which
7883 are just identifiers. So, we peek ahead to check that the "::"
7884 and "~" tokens are present; if they are not, then we can avoid
7885 calling type_name. */
7886 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7887 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7888 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7890 cp_parser_error (parser, "non-scalar type");
7891 return;
7894 /* Look for the type-name. */
7895 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7896 if (*scope == error_mark_node)
7897 return;
7899 /* Look for the `::' token. */
7900 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7902 else
7903 *scope = NULL_TREE;
7905 /* Look for the `~'. */
7906 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7908 /* Once we see the ~, this has to be a pseudo-destructor. */
7909 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7910 cp_parser_commit_to_topmost_tentative_parse (parser);
7912 /* Look for the type-name again. We are not responsible for
7913 checking that it matches the first type-name. */
7914 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7917 /* Parse a unary-expression.
7919 unary-expression:
7920 postfix-expression
7921 ++ cast-expression
7922 -- cast-expression
7923 unary-operator cast-expression
7924 sizeof unary-expression
7925 sizeof ( type-id )
7926 alignof ( type-id ) [C++0x]
7927 new-expression
7928 delete-expression
7930 GNU Extensions:
7932 unary-expression:
7933 __extension__ cast-expression
7934 __alignof__ unary-expression
7935 __alignof__ ( type-id )
7936 alignof unary-expression [C++0x]
7937 __real__ cast-expression
7938 __imag__ cast-expression
7939 && identifier
7940 sizeof ( type-id ) { initializer-list , [opt] }
7941 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7942 __alignof__ ( type-id ) { initializer-list , [opt] }
7944 ADDRESS_P is true iff the unary-expression is appearing as the
7945 operand of the `&' operator. CAST_P is true if this expression is
7946 the target of a cast.
7948 Returns a representation of the expression. */
7950 static cp_expr
7951 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7952 bool address_p, bool cast_p, bool decltype_p)
7954 cp_token *token;
7955 enum tree_code unary_operator;
7957 /* Peek at the next token. */
7958 token = cp_lexer_peek_token (parser->lexer);
7959 /* Some keywords give away the kind of expression. */
7960 if (token->type == CPP_KEYWORD)
7962 enum rid keyword = token->keyword;
7964 switch (keyword)
7966 case RID_ALIGNOF:
7967 case RID_SIZEOF:
7969 tree operand, ret;
7970 enum tree_code op;
7971 location_t start_loc = token->location;
7973 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7974 bool std_alignof = id_equal (token->u.value, "alignof");
7976 /* Consume the token. */
7977 cp_lexer_consume_token (parser->lexer);
7978 /* Parse the operand. */
7979 operand = cp_parser_sizeof_operand (parser, keyword);
7981 if (TYPE_P (operand))
7982 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
7983 true);
7984 else
7986 /* ISO C++ defines alignof only with types, not with
7987 expressions. So pedwarn if alignof is used with a non-
7988 type expression. However, __alignof__ is ok. */
7989 if (std_alignof)
7990 pedwarn (token->location, OPT_Wpedantic,
7991 "ISO C++ does not allow %<alignof%> "
7992 "with a non-type");
7994 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7996 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7997 SIZEOF_EXPR with the original operand. */
7998 if (op == SIZEOF_EXPR && ret != error_mark_node)
8000 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8002 if (!processing_template_decl && TYPE_P (operand))
8004 ret = build_min (SIZEOF_EXPR, size_type_node,
8005 build1 (NOP_EXPR, operand,
8006 error_mark_node));
8007 SIZEOF_EXPR_TYPE_P (ret) = 1;
8009 else
8010 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8011 TREE_SIDE_EFFECTS (ret) = 0;
8012 TREE_READONLY (ret) = 1;
8016 /* Construct a location e.g. :
8017 alignof (expr)
8018 ^~~~~~~~~~~~~~
8019 with start == caret at the start of the "alignof"/"sizeof"
8020 token, with the endpoint at the final closing paren. */
8021 location_t finish_loc
8022 = cp_lexer_previous_token (parser->lexer)->location;
8023 location_t compound_loc
8024 = make_location (start_loc, start_loc, finish_loc);
8026 cp_expr ret_expr (ret);
8027 ret_expr.set_location (compound_loc);
8028 ret_expr = ret_expr.maybe_add_location_wrapper ();
8029 return ret_expr;
8032 case RID_NEW:
8033 return cp_parser_new_expression (parser);
8035 case RID_DELETE:
8036 return cp_parser_delete_expression (parser);
8038 case RID_EXTENSION:
8040 /* The saved value of the PEDANTIC flag. */
8041 int saved_pedantic;
8042 tree expr;
8044 /* Save away the PEDANTIC flag. */
8045 cp_parser_extension_opt (parser, &saved_pedantic);
8046 /* Parse the cast-expression. */
8047 expr = cp_parser_simple_cast_expression (parser);
8048 /* Restore the PEDANTIC flag. */
8049 pedantic = saved_pedantic;
8051 return expr;
8054 case RID_REALPART:
8055 case RID_IMAGPART:
8057 tree expression;
8059 /* Consume the `__real__' or `__imag__' token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 /* Parse the cast-expression. */
8062 expression = cp_parser_simple_cast_expression (parser);
8063 /* Create the complete representation. */
8064 return build_x_unary_op (token->location,
8065 (keyword == RID_REALPART
8066 ? REALPART_EXPR : IMAGPART_EXPR),
8067 expression,
8068 tf_warning_or_error);
8070 break;
8072 case RID_TRANSACTION_ATOMIC:
8073 case RID_TRANSACTION_RELAXED:
8074 return cp_parser_transaction_expression (parser, keyword);
8076 case RID_NOEXCEPT:
8078 tree expr;
8079 const char *saved_message;
8080 bool saved_integral_constant_expression_p;
8081 bool saved_non_integral_constant_expression_p;
8082 bool saved_greater_than_is_operator_p;
8084 location_t start_loc = token->location;
8086 cp_lexer_consume_token (parser->lexer);
8087 matching_parens parens;
8088 parens.require_open (parser);
8090 saved_message = parser->type_definition_forbidden_message;
8091 parser->type_definition_forbidden_message
8092 = G_("types may not be defined in %<noexcept%> expressions");
8094 saved_integral_constant_expression_p
8095 = parser->integral_constant_expression_p;
8096 saved_non_integral_constant_expression_p
8097 = parser->non_integral_constant_expression_p;
8098 parser->integral_constant_expression_p = false;
8100 saved_greater_than_is_operator_p
8101 = parser->greater_than_is_operator_p;
8102 parser->greater_than_is_operator_p = true;
8104 ++cp_unevaluated_operand;
8105 ++c_inhibit_evaluation_warnings;
8106 ++cp_noexcept_operand;
8107 expr = cp_parser_expression (parser);
8108 --cp_noexcept_operand;
8109 --c_inhibit_evaluation_warnings;
8110 --cp_unevaluated_operand;
8112 parser->greater_than_is_operator_p
8113 = saved_greater_than_is_operator_p;
8115 parser->integral_constant_expression_p
8116 = saved_integral_constant_expression_p;
8117 parser->non_integral_constant_expression_p
8118 = saved_non_integral_constant_expression_p;
8120 parser->type_definition_forbidden_message = saved_message;
8122 location_t finish_loc
8123 = cp_lexer_peek_token (parser->lexer)->location;
8124 parens.require_close (parser);
8126 /* Construct a location of the form:
8127 noexcept (expr)
8128 ^~~~~~~~~~~~~~~
8129 with start == caret, finishing at the close-paren. */
8130 location_t noexcept_loc
8131 = make_location (start_loc, start_loc, finish_loc);
8133 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8134 noexcept_loc);
8137 default:
8138 break;
8142 /* Look for the `:: new' and `:: delete', which also signal the
8143 beginning of a new-expression, or delete-expression,
8144 respectively. If the next token is `::', then it might be one of
8145 these. */
8146 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8148 enum rid keyword;
8150 /* See if the token after the `::' is one of the keywords in
8151 which we're interested. */
8152 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8153 /* If it's `new', we have a new-expression. */
8154 if (keyword == RID_NEW)
8155 return cp_parser_new_expression (parser);
8156 /* Similarly, for `delete'. */
8157 else if (keyword == RID_DELETE)
8158 return cp_parser_delete_expression (parser);
8161 /* Look for a unary operator. */
8162 unary_operator = cp_parser_unary_operator (token);
8163 /* The `++' and `--' operators can be handled similarly, even though
8164 they are not technically unary-operators in the grammar. */
8165 if (unary_operator == ERROR_MARK)
8167 if (token->type == CPP_PLUS_PLUS)
8168 unary_operator = PREINCREMENT_EXPR;
8169 else if (token->type == CPP_MINUS_MINUS)
8170 unary_operator = PREDECREMENT_EXPR;
8171 /* Handle the GNU address-of-label extension. */
8172 else if (cp_parser_allow_gnu_extensions_p (parser)
8173 && token->type == CPP_AND_AND)
8175 tree identifier;
8176 tree expression;
8177 location_t start_loc = token->location;
8179 /* Consume the '&&' token. */
8180 cp_lexer_consume_token (parser->lexer);
8181 /* Look for the identifier. */
8182 location_t finish_loc
8183 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8184 identifier = cp_parser_identifier (parser);
8185 /* Construct a location of the form:
8186 &&label
8187 ^~~~~~~
8188 with caret==start at the "&&", finish at the end of the label. */
8189 location_t combined_loc
8190 = make_location (start_loc, start_loc, finish_loc);
8191 /* Create an expression representing the address. */
8192 expression = finish_label_address_expr (identifier, combined_loc);
8193 if (cp_parser_non_integral_constant_expression (parser,
8194 NIC_ADDR_LABEL))
8195 expression = error_mark_node;
8196 return expression;
8199 if (unary_operator != ERROR_MARK)
8201 cp_expr cast_expression;
8202 cp_expr expression = error_mark_node;
8203 non_integral_constant non_constant_p = NIC_NONE;
8204 location_t loc = token->location;
8205 tsubst_flags_t complain = complain_flags (decltype_p);
8207 /* Consume the operator token. */
8208 token = cp_lexer_consume_token (parser->lexer);
8209 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8211 /* Parse the cast-expression. */
8212 cast_expression
8213 = cp_parser_cast_expression (parser,
8214 unary_operator == ADDR_EXPR,
8215 /*cast_p=*/false,
8216 /*decltype*/false,
8217 pidk);
8219 /* Make a location:
8220 OP_TOKEN CAST_EXPRESSION
8221 ^~~~~~~~~~~~~~~~~~~~~~~~~
8222 with start==caret at the operator token, and
8223 extending to the end of the cast_expression. */
8224 loc = make_location (loc, loc, cast_expression.get_finish ());
8226 /* Now, build an appropriate representation. */
8227 switch (unary_operator)
8229 case INDIRECT_REF:
8230 non_constant_p = NIC_STAR;
8231 expression = build_x_indirect_ref (loc, cast_expression,
8232 RO_UNARY_STAR,
8233 complain);
8234 /* TODO: build_x_indirect_ref does not always honor the
8235 location, so ensure it is set. */
8236 expression.set_location (loc);
8237 break;
8239 case ADDR_EXPR:
8240 non_constant_p = NIC_ADDR;
8241 /* Fall through. */
8242 case BIT_NOT_EXPR:
8243 expression = build_x_unary_op (loc, unary_operator,
8244 cast_expression,
8245 complain);
8246 /* TODO: build_x_unary_op does not always honor the location,
8247 so ensure it is set. */
8248 expression.set_location (loc);
8249 break;
8251 case PREINCREMENT_EXPR:
8252 case PREDECREMENT_EXPR:
8253 non_constant_p = unary_operator == PREINCREMENT_EXPR
8254 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8255 /* Fall through. */
8256 case NEGATE_EXPR:
8257 /* Immediately fold negation of a constant, unless the constant is 0
8258 (since -0 == 0) or it would overflow. */
8259 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8260 && CONSTANT_CLASS_P (cast_expression)
8261 && !integer_zerop (cast_expression)
8262 && !TREE_OVERFLOW (cast_expression))
8264 tree folded = fold_build1 (unary_operator,
8265 TREE_TYPE (cast_expression),
8266 cast_expression);
8267 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8269 expression = cp_expr (folded, loc);
8270 break;
8273 /* Fall through. */
8274 case UNARY_PLUS_EXPR:
8275 case TRUTH_NOT_EXPR:
8276 expression = finish_unary_op_expr (loc, unary_operator,
8277 cast_expression, complain);
8278 break;
8280 default:
8281 gcc_unreachable ();
8284 if (non_constant_p != NIC_NONE
8285 && cp_parser_non_integral_constant_expression (parser,
8286 non_constant_p))
8287 expression = error_mark_node;
8289 return expression;
8292 return cp_parser_postfix_expression (parser, address_p, cast_p,
8293 /*member_access_only_p=*/false,
8294 decltype_p,
8295 pidk);
8298 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8299 unary-operator, the corresponding tree code is returned. */
8301 static enum tree_code
8302 cp_parser_unary_operator (cp_token* token)
8304 switch (token->type)
8306 case CPP_MULT:
8307 return INDIRECT_REF;
8309 case CPP_AND:
8310 return ADDR_EXPR;
8312 case CPP_PLUS:
8313 return UNARY_PLUS_EXPR;
8315 case CPP_MINUS:
8316 return NEGATE_EXPR;
8318 case CPP_NOT:
8319 return TRUTH_NOT_EXPR;
8321 case CPP_COMPL:
8322 return BIT_NOT_EXPR;
8324 default:
8325 return ERROR_MARK;
8329 /* Parse a new-expression.
8331 new-expression:
8332 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8333 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8335 Returns a representation of the expression. */
8337 static tree
8338 cp_parser_new_expression (cp_parser* parser)
8340 bool global_scope_p;
8341 vec<tree, va_gc> *placement;
8342 tree type;
8343 vec<tree, va_gc> *initializer;
8344 tree nelts = NULL_TREE;
8345 tree ret;
8347 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8349 /* Look for the optional `::' operator. */
8350 global_scope_p
8351 = (cp_parser_global_scope_opt (parser,
8352 /*current_scope_valid_p=*/false)
8353 != NULL_TREE);
8354 /* Look for the `new' operator. */
8355 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8356 /* There's no easy way to tell a new-placement from the
8357 `( type-id )' construct. */
8358 cp_parser_parse_tentatively (parser);
8359 /* Look for a new-placement. */
8360 placement = cp_parser_new_placement (parser);
8361 /* If that didn't work out, there's no new-placement. */
8362 if (!cp_parser_parse_definitely (parser))
8364 if (placement != NULL)
8365 release_tree_vector (placement);
8366 placement = NULL;
8369 /* If the next token is a `(', then we have a parenthesized
8370 type-id. */
8371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8373 cp_token *token;
8374 const char *saved_message = parser->type_definition_forbidden_message;
8376 /* Consume the `('. */
8377 matching_parens parens;
8378 parens.consume_open (parser);
8380 /* Parse the type-id. */
8381 parser->type_definition_forbidden_message
8382 = G_("types may not be defined in a new-expression");
8384 type_id_in_expr_sentinel s (parser);
8385 type = cp_parser_type_id (parser);
8387 parser->type_definition_forbidden_message = saved_message;
8389 /* Look for the closing `)'. */
8390 parens.require_close (parser);
8391 token = cp_lexer_peek_token (parser->lexer);
8392 /* There should not be a direct-new-declarator in this production,
8393 but GCC used to allowed this, so we check and emit a sensible error
8394 message for this case. */
8395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8397 error_at (token->location,
8398 "array bound forbidden after parenthesized type-id");
8399 inform (token->location,
8400 "try removing the parentheses around the type-id");
8401 cp_parser_direct_new_declarator (parser);
8404 /* Otherwise, there must be a new-type-id. */
8405 else
8406 type = cp_parser_new_type_id (parser, &nelts);
8408 /* If the next token is a `(' or '{', then we have a new-initializer. */
8409 cp_token *token = cp_lexer_peek_token (parser->lexer);
8410 if (token->type == CPP_OPEN_PAREN
8411 || token->type == CPP_OPEN_BRACE)
8412 initializer = cp_parser_new_initializer (parser);
8413 else
8414 initializer = NULL;
8416 /* A new-expression may not appear in an integral constant
8417 expression. */
8418 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8419 ret = error_mark_node;
8420 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8421 of a new-type-id or type-id of a new-expression, the new-expression shall
8422 contain a new-initializer of the form ( assignment-expression )".
8423 Additionally, consistently with the spirit of DR 1467, we want to accept
8424 'new auto { 2 }' too. */
8425 else if ((ret = type_uses_auto (type))
8426 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8427 && (vec_safe_length (initializer) != 1
8428 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8429 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8431 error_at (token->location,
8432 "initialization of new-expression for type %<auto%> "
8433 "requires exactly one element");
8434 ret = error_mark_node;
8436 else
8438 /* Construct a location e.g.:
8439 ptr = new int[100]
8440 ^~~~~~~~~~~~
8441 with caret == start at the start of the "new" token, and the end
8442 at the end of the final token we consumed. */
8443 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8444 location_t end_loc = get_finish (end_tok->location);
8445 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8447 /* Create a representation of the new-expression. */
8448 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8449 tf_warning_or_error);
8450 protected_set_expr_location (ret, combined_loc);
8453 if (placement != NULL)
8454 release_tree_vector (placement);
8455 if (initializer != NULL)
8456 release_tree_vector (initializer);
8458 return ret;
8461 /* Parse a new-placement.
8463 new-placement:
8464 ( expression-list )
8466 Returns the same representation as for an expression-list. */
8468 static vec<tree, va_gc> *
8469 cp_parser_new_placement (cp_parser* parser)
8471 vec<tree, va_gc> *expression_list;
8473 /* Parse the expression-list. */
8474 expression_list = (cp_parser_parenthesized_expression_list
8475 (parser, non_attr, /*cast_p=*/false,
8476 /*allow_expansion_p=*/true,
8477 /*non_constant_p=*/NULL));
8479 if (expression_list && expression_list->is_empty ())
8480 error ("expected expression-list or type-id");
8482 return expression_list;
8485 /* Parse a new-type-id.
8487 new-type-id:
8488 type-specifier-seq new-declarator [opt]
8490 Returns the TYPE allocated. If the new-type-id indicates an array
8491 type, *NELTS is set to the number of elements in the last array
8492 bound; the TYPE will not include the last array bound. */
8494 static tree
8495 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8497 cp_decl_specifier_seq type_specifier_seq;
8498 cp_declarator *new_declarator;
8499 cp_declarator *declarator;
8500 cp_declarator *outer_declarator;
8501 const char *saved_message;
8503 /* The type-specifier sequence must not contain type definitions.
8504 (It cannot contain declarations of new types either, but if they
8505 are not definitions we will catch that because they are not
8506 complete.) */
8507 saved_message = parser->type_definition_forbidden_message;
8508 parser->type_definition_forbidden_message
8509 = G_("types may not be defined in a new-type-id");
8510 /* Parse the type-specifier-seq. */
8511 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8512 /*is_trailing_return=*/false,
8513 &type_specifier_seq);
8514 /* Restore the old message. */
8515 parser->type_definition_forbidden_message = saved_message;
8517 if (type_specifier_seq.type == error_mark_node)
8518 return error_mark_node;
8520 /* Parse the new-declarator. */
8521 new_declarator = cp_parser_new_declarator_opt (parser);
8523 /* Determine the number of elements in the last array dimension, if
8524 any. */
8525 *nelts = NULL_TREE;
8526 /* Skip down to the last array dimension. */
8527 declarator = new_declarator;
8528 outer_declarator = NULL;
8529 while (declarator && (declarator->kind == cdk_pointer
8530 || declarator->kind == cdk_ptrmem))
8532 outer_declarator = declarator;
8533 declarator = declarator->declarator;
8535 while (declarator
8536 && declarator->kind == cdk_array
8537 && declarator->declarator
8538 && declarator->declarator->kind == cdk_array)
8540 outer_declarator = declarator;
8541 declarator = declarator->declarator;
8544 if (declarator && declarator->kind == cdk_array)
8546 *nelts = declarator->u.array.bounds;
8547 if (*nelts == error_mark_node)
8548 *nelts = integer_one_node;
8550 if (outer_declarator)
8551 outer_declarator->declarator = declarator->declarator;
8552 else
8553 new_declarator = NULL;
8556 return groktypename (&type_specifier_seq, new_declarator, false);
8559 /* Parse an (optional) new-declarator.
8561 new-declarator:
8562 ptr-operator new-declarator [opt]
8563 direct-new-declarator
8565 Returns the declarator. */
8567 static cp_declarator *
8568 cp_parser_new_declarator_opt (cp_parser* parser)
8570 enum tree_code code;
8571 tree type, std_attributes = NULL_TREE;
8572 cp_cv_quals cv_quals;
8574 /* We don't know if there's a ptr-operator next, or not. */
8575 cp_parser_parse_tentatively (parser);
8576 /* Look for a ptr-operator. */
8577 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8578 /* If that worked, look for more new-declarators. */
8579 if (cp_parser_parse_definitely (parser))
8581 cp_declarator *declarator;
8583 /* Parse another optional declarator. */
8584 declarator = cp_parser_new_declarator_opt (parser);
8586 declarator = cp_parser_make_indirect_declarator
8587 (code, type, cv_quals, declarator, std_attributes);
8589 return declarator;
8592 /* If the next token is a `[', there is a direct-new-declarator. */
8593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8594 return cp_parser_direct_new_declarator (parser);
8596 return NULL;
8599 /* Parse a direct-new-declarator.
8601 direct-new-declarator:
8602 [ expression ]
8603 direct-new-declarator [constant-expression]
8607 static cp_declarator *
8608 cp_parser_direct_new_declarator (cp_parser* parser)
8610 cp_declarator *declarator = NULL;
8612 while (true)
8614 tree expression;
8615 cp_token *token;
8617 /* Look for the opening `['. */
8618 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8620 token = cp_lexer_peek_token (parser->lexer);
8621 expression = cp_parser_expression (parser);
8622 /* The standard requires that the expression have integral
8623 type. DR 74 adds enumeration types. We believe that the
8624 real intent is that these expressions be handled like the
8625 expression in a `switch' condition, which also allows
8626 classes with a single conversion to integral or
8627 enumeration type. */
8628 if (!processing_template_decl)
8630 expression
8631 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8632 expression,
8633 /*complain=*/true);
8634 if (!expression)
8636 error_at (token->location,
8637 "expression in new-declarator must have integral "
8638 "or enumeration type");
8639 expression = error_mark_node;
8643 /* Look for the closing `]'. */
8644 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8646 /* Add this bound to the declarator. */
8647 declarator = make_array_declarator (declarator, expression);
8649 /* If the next token is not a `[', then there are no more
8650 bounds. */
8651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8652 break;
8655 return declarator;
8658 /* Parse a new-initializer.
8660 new-initializer:
8661 ( expression-list [opt] )
8662 braced-init-list
8664 Returns a representation of the expression-list. */
8666 static vec<tree, va_gc> *
8667 cp_parser_new_initializer (cp_parser* parser)
8669 vec<tree, va_gc> *expression_list;
8671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8673 tree t;
8674 bool expr_non_constant_p;
8675 cp_lexer_set_source_position (parser->lexer);
8676 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8677 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8678 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8679 expression_list = make_tree_vector_single (t);
8681 else
8682 expression_list = (cp_parser_parenthesized_expression_list
8683 (parser, non_attr, /*cast_p=*/false,
8684 /*allow_expansion_p=*/true,
8685 /*non_constant_p=*/NULL));
8687 return expression_list;
8690 /* Parse a delete-expression.
8692 delete-expression:
8693 :: [opt] delete cast-expression
8694 :: [opt] delete [ ] cast-expression
8696 Returns a representation of the expression. */
8698 static tree
8699 cp_parser_delete_expression (cp_parser* parser)
8701 bool global_scope_p;
8702 bool array_p;
8703 tree expression;
8705 /* Look for the optional `::' operator. */
8706 global_scope_p
8707 = (cp_parser_global_scope_opt (parser,
8708 /*current_scope_valid_p=*/false)
8709 != NULL_TREE);
8710 /* Look for the `delete' keyword. */
8711 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8712 /* See if the array syntax is in use. */
8713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8715 /* Consume the `[' token. */
8716 cp_lexer_consume_token (parser->lexer);
8717 /* Look for the `]' token. */
8718 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8719 /* Remember that this is the `[]' construct. */
8720 array_p = true;
8722 else
8723 array_p = false;
8725 /* Parse the cast-expression. */
8726 expression = cp_parser_simple_cast_expression (parser);
8728 /* A delete-expression may not appear in an integral constant
8729 expression. */
8730 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8731 return error_mark_node;
8733 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8734 tf_warning_or_error);
8737 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8738 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8739 0 otherwise. */
8741 static int
8742 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8744 cp_token *token = cp_lexer_peek_token (parser->lexer);
8745 switch (token->type)
8747 case CPP_COMMA:
8748 case CPP_SEMICOLON:
8749 case CPP_QUERY:
8750 case CPP_COLON:
8751 case CPP_CLOSE_SQUARE:
8752 case CPP_CLOSE_PAREN:
8753 case CPP_CLOSE_BRACE:
8754 case CPP_OPEN_BRACE:
8755 case CPP_DOT:
8756 case CPP_DOT_STAR:
8757 case CPP_DEREF:
8758 case CPP_DEREF_STAR:
8759 case CPP_DIV:
8760 case CPP_MOD:
8761 case CPP_LSHIFT:
8762 case CPP_RSHIFT:
8763 case CPP_LESS:
8764 case CPP_GREATER:
8765 case CPP_LESS_EQ:
8766 case CPP_GREATER_EQ:
8767 case CPP_EQ_EQ:
8768 case CPP_NOT_EQ:
8769 case CPP_EQ:
8770 case CPP_MULT_EQ:
8771 case CPP_DIV_EQ:
8772 case CPP_MOD_EQ:
8773 case CPP_PLUS_EQ:
8774 case CPP_MINUS_EQ:
8775 case CPP_RSHIFT_EQ:
8776 case CPP_LSHIFT_EQ:
8777 case CPP_AND_EQ:
8778 case CPP_XOR_EQ:
8779 case CPP_OR_EQ:
8780 case CPP_XOR:
8781 case CPP_OR:
8782 case CPP_OR_OR:
8783 case CPP_EOF:
8784 case CPP_ELLIPSIS:
8785 return 0;
8787 case CPP_OPEN_PAREN:
8788 /* In ((type ()) () the last () isn't a valid cast-expression,
8789 so the whole must be parsed as postfix-expression. */
8790 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8791 != CPP_CLOSE_PAREN;
8793 case CPP_OPEN_SQUARE:
8794 /* '[' may start a primary-expression in obj-c++ and in C++11,
8795 as a lambda-expression, eg, '(void)[]{}'. */
8796 if (cxx_dialect >= cxx11)
8797 return -1;
8798 return c_dialect_objc ();
8800 case CPP_PLUS_PLUS:
8801 case CPP_MINUS_MINUS:
8802 /* '++' and '--' may or may not start a cast-expression:
8804 struct T { void operator++(int); };
8805 void f() { (T())++; }
8809 int a;
8810 (int)++a; */
8811 return -1;
8813 default:
8814 return 1;
8818 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8819 in the order: const_cast, static_cast, reinterpret_cast.
8821 Don't suggest dynamic_cast.
8823 Return the first legal cast kind found, or NULL otherwise. */
8825 static const char *
8826 get_cast_suggestion (tree dst_type, tree orig_expr)
8828 tree trial;
8830 /* Reuse the parser logic by attempting to build the various kinds of
8831 cast, with "complain" disabled.
8832 Identify the first such cast that is valid. */
8834 /* Don't attempt to run such logic within template processing. */
8835 if (processing_template_decl)
8836 return NULL;
8838 /* First try const_cast. */
8839 trial = build_const_cast (dst_type, orig_expr, tf_none);
8840 if (trial != error_mark_node)
8841 return "const_cast";
8843 /* If that fails, try static_cast. */
8844 trial = build_static_cast (dst_type, orig_expr, tf_none);
8845 if (trial != error_mark_node)
8846 return "static_cast";
8848 /* Finally, try reinterpret_cast. */
8849 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8850 if (trial != error_mark_node)
8851 return "reinterpret_cast";
8853 /* No such cast possible. */
8854 return NULL;
8857 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8858 suggesting how to convert a C-style cast of the form:
8860 (DST_TYPE)ORIG_EXPR
8862 to a C++-style cast.
8864 The primary range of RICHLOC is asssumed to be that of the original
8865 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8866 of the parens in the C-style cast. */
8868 static void
8869 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8870 location_t close_paren_loc, tree orig_expr,
8871 tree dst_type)
8873 /* This function is non-trivial, so bail out now if the warning isn't
8874 going to be emitted. */
8875 if (!warn_old_style_cast)
8876 return;
8878 /* Try to find a legal C++ cast, trying them in order:
8879 const_cast, static_cast, reinterpret_cast. */
8880 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8881 if (!cast_suggestion)
8882 return;
8884 /* Replace the open paren with "CAST_SUGGESTION<". */
8885 pretty_printer pp;
8886 pp_printf (&pp, "%s<", cast_suggestion);
8887 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8889 /* Replace the close paren with "> (". */
8890 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8892 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8893 rich_loc->add_fixit_insert_after (")");
8897 /* Parse a cast-expression.
8899 cast-expression:
8900 unary-expression
8901 ( type-id ) cast-expression
8903 ADDRESS_P is true iff the unary-expression is appearing as the
8904 operand of the `&' operator. CAST_P is true if this expression is
8905 the target of a cast.
8907 Returns a representation of the expression. */
8909 static cp_expr
8910 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8911 bool decltype_p, cp_id_kind * pidk)
8913 /* If it's a `(', then we might be looking at a cast. */
8914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8916 tree type = NULL_TREE;
8917 cp_expr expr (NULL_TREE);
8918 int cast_expression = 0;
8919 const char *saved_message;
8921 /* There's no way to know yet whether or not this is a cast.
8922 For example, `(int (3))' is a unary-expression, while `(int)
8923 3' is a cast. So, we resort to parsing tentatively. */
8924 cp_parser_parse_tentatively (parser);
8925 /* Types may not be defined in a cast. */
8926 saved_message = parser->type_definition_forbidden_message;
8927 parser->type_definition_forbidden_message
8928 = G_("types may not be defined in casts");
8929 /* Consume the `('. */
8930 matching_parens parens;
8931 cp_token *open_paren = parens.consume_open (parser);
8932 location_t open_paren_loc = open_paren->location;
8933 location_t close_paren_loc = UNKNOWN_LOCATION;
8935 /* A very tricky bit is that `(struct S) { 3 }' is a
8936 compound-literal (which we permit in C++ as an extension).
8937 But, that construct is not a cast-expression -- it is a
8938 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8939 is legal; if the compound-literal were a cast-expression,
8940 you'd need an extra set of parentheses.) But, if we parse
8941 the type-id, and it happens to be a class-specifier, then we
8942 will commit to the parse at that point, because we cannot
8943 undo the action that is done when creating a new class. So,
8944 then we cannot back up and do a postfix-expression.
8946 Another tricky case is the following (c++/29234):
8948 struct S { void operator () (); };
8950 void foo ()
8952 ( S()() );
8955 As a type-id we parse the parenthesized S()() as a function
8956 returning a function, groktypename complains and we cannot
8957 back up in this case either.
8959 Therefore, we scan ahead to the closing `)', and check to see
8960 if the tokens after the `)' can start a cast-expression. Otherwise
8961 we are dealing with an unary-expression, a postfix-expression
8962 or something else.
8964 Yet another tricky case, in C++11, is the following (c++/54891):
8966 (void)[]{};
8968 The issue is that usually, besides the case of lambda-expressions,
8969 the parenthesized type-id cannot be followed by '[', and, eg, we
8970 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8971 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8972 we don't commit, we try a cast-expression, then an unary-expression.
8974 Save tokens so that we can put them back. */
8975 cp_lexer_save_tokens (parser->lexer);
8977 /* We may be looking at a cast-expression. */
8978 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8979 /*consume_paren=*/true))
8980 cast_expression
8981 = cp_parser_tokens_start_cast_expression (parser);
8983 /* Roll back the tokens we skipped. */
8984 cp_lexer_rollback_tokens (parser->lexer);
8985 /* If we aren't looking at a cast-expression, simulate an error so
8986 that the call to cp_parser_error_occurred below returns true. */
8987 if (!cast_expression)
8988 cp_parser_simulate_error (parser);
8989 else
8991 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8992 parser->in_type_id_in_expr_p = true;
8993 /* Look for the type-id. */
8994 type = cp_parser_type_id (parser);
8995 /* Look for the closing `)'. */
8996 cp_token *close_paren = parens.require_close (parser);
8997 if (close_paren)
8998 close_paren_loc = close_paren->location;
8999 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9002 /* Restore the saved message. */
9003 parser->type_definition_forbidden_message = saved_message;
9005 /* At this point this can only be either a cast or a
9006 parenthesized ctor such as `(T ())' that looks like a cast to
9007 function returning T. */
9008 if (!cp_parser_error_occurred (parser))
9010 /* Only commit if the cast-expression doesn't start with
9011 '++', '--', or '[' in C++11. */
9012 if (cast_expression > 0)
9013 cp_parser_commit_to_topmost_tentative_parse (parser);
9015 expr = cp_parser_cast_expression (parser,
9016 /*address_p=*/false,
9017 /*cast_p=*/true,
9018 /*decltype_p=*/false,
9019 pidk);
9021 if (cp_parser_parse_definitely (parser))
9023 /* Warn about old-style casts, if so requested. */
9024 if (warn_old_style_cast
9025 && !in_system_header_at (input_location)
9026 && !VOID_TYPE_P (type)
9027 && current_lang_name != lang_name_c)
9029 gcc_rich_location rich_loc (input_location);
9030 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9031 expr, type);
9032 warning_at (&rich_loc, OPT_Wold_style_cast,
9033 "use of old-style cast to %q#T", type);
9036 /* Only type conversions to integral or enumeration types
9037 can be used in constant-expressions. */
9038 if (!cast_valid_in_integral_constant_expression_p (type)
9039 && cp_parser_non_integral_constant_expression (parser,
9040 NIC_CAST))
9041 return error_mark_node;
9043 /* Perform the cast. */
9044 /* Make a location:
9045 (TYPE) EXPR
9046 ^~~~~~~~~~~
9047 with start==caret at the open paren, extending to the
9048 end of "expr". */
9049 location_t cast_loc = make_location (open_paren_loc,
9050 open_paren_loc,
9051 expr.get_finish ());
9052 expr = build_c_cast (cast_loc, type, expr);
9053 return expr;
9056 else
9057 cp_parser_abort_tentative_parse (parser);
9060 /* If we get here, then it's not a cast, so it must be a
9061 unary-expression. */
9062 return cp_parser_unary_expression (parser, pidk, address_p,
9063 cast_p, decltype_p);
9066 /* Parse a binary expression of the general form:
9068 pm-expression:
9069 cast-expression
9070 pm-expression .* cast-expression
9071 pm-expression ->* cast-expression
9073 multiplicative-expression:
9074 pm-expression
9075 multiplicative-expression * pm-expression
9076 multiplicative-expression / pm-expression
9077 multiplicative-expression % pm-expression
9079 additive-expression:
9080 multiplicative-expression
9081 additive-expression + multiplicative-expression
9082 additive-expression - multiplicative-expression
9084 shift-expression:
9085 additive-expression
9086 shift-expression << additive-expression
9087 shift-expression >> additive-expression
9089 relational-expression:
9090 shift-expression
9091 relational-expression < shift-expression
9092 relational-expression > shift-expression
9093 relational-expression <= shift-expression
9094 relational-expression >= shift-expression
9096 GNU Extension:
9098 relational-expression:
9099 relational-expression <? shift-expression
9100 relational-expression >? shift-expression
9102 equality-expression:
9103 relational-expression
9104 equality-expression == relational-expression
9105 equality-expression != relational-expression
9107 and-expression:
9108 equality-expression
9109 and-expression & equality-expression
9111 exclusive-or-expression:
9112 and-expression
9113 exclusive-or-expression ^ and-expression
9115 inclusive-or-expression:
9116 exclusive-or-expression
9117 inclusive-or-expression | exclusive-or-expression
9119 logical-and-expression:
9120 inclusive-or-expression
9121 logical-and-expression && inclusive-or-expression
9123 logical-or-expression:
9124 logical-and-expression
9125 logical-or-expression || logical-and-expression
9127 All these are implemented with a single function like:
9129 binary-expression:
9130 simple-cast-expression
9131 binary-expression <token> binary-expression
9133 CAST_P is true if this expression is the target of a cast.
9135 The binops_by_token map is used to get the tree codes for each <token> type.
9136 binary-expressions are associated according to a precedence table. */
9138 #define TOKEN_PRECEDENCE(token) \
9139 (((token->type == CPP_GREATER \
9140 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9141 && !parser->greater_than_is_operator_p) \
9142 ? PREC_NOT_OPERATOR \
9143 : binops_by_token[token->type].prec)
9145 static cp_expr
9146 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9147 bool no_toplevel_fold_p,
9148 bool decltype_p,
9149 enum cp_parser_prec prec,
9150 cp_id_kind * pidk)
9152 cp_parser_expression_stack stack;
9153 cp_parser_expression_stack_entry *sp = &stack[0];
9154 cp_parser_expression_stack_entry current;
9155 cp_expr rhs;
9156 cp_token *token;
9157 enum tree_code rhs_type;
9158 enum cp_parser_prec new_prec, lookahead_prec;
9159 tree overload;
9161 /* Parse the first expression. */
9162 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9163 ? TRUTH_NOT_EXPR : ERROR_MARK);
9164 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9165 cast_p, decltype_p, pidk);
9166 current.prec = prec;
9168 if (cp_parser_error_occurred (parser))
9169 return error_mark_node;
9171 for (;;)
9173 /* Get an operator token. */
9174 token = cp_lexer_peek_token (parser->lexer);
9176 if (warn_cxx11_compat
9177 && token->type == CPP_RSHIFT
9178 && !parser->greater_than_is_operator_p)
9180 if (warning_at (token->location, OPT_Wc__11_compat,
9181 "%<>>%> operator is treated"
9182 " as two right angle brackets in C++11"))
9183 inform (token->location,
9184 "suggest parentheses around %<>>%> expression");
9187 new_prec = TOKEN_PRECEDENCE (token);
9188 if (new_prec != PREC_NOT_OPERATOR
9189 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9190 /* This is a fold-expression; handle it later. */
9191 new_prec = PREC_NOT_OPERATOR;
9193 /* Popping an entry off the stack means we completed a subexpression:
9194 - either we found a token which is not an operator (`>' where it is not
9195 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9196 will happen repeatedly;
9197 - or, we found an operator which has lower priority. This is the case
9198 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9199 parsing `3 * 4'. */
9200 if (new_prec <= current.prec)
9202 if (sp == stack)
9203 break;
9204 else
9205 goto pop;
9208 get_rhs:
9209 current.tree_type = binops_by_token[token->type].tree_type;
9210 current.loc = token->location;
9212 /* We used the operator token. */
9213 cp_lexer_consume_token (parser->lexer);
9215 /* For "false && x" or "true || x", x will never be executed;
9216 disable warnings while evaluating it. */
9217 if (current.tree_type == TRUTH_ANDIF_EXPR)
9218 c_inhibit_evaluation_warnings +=
9219 cp_fully_fold (current.lhs) == truthvalue_false_node;
9220 else if (current.tree_type == TRUTH_ORIF_EXPR)
9221 c_inhibit_evaluation_warnings +=
9222 cp_fully_fold (current.lhs) == truthvalue_true_node;
9224 /* Extract another operand. It may be the RHS of this expression
9225 or the LHS of a new, higher priority expression. */
9226 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9227 ? TRUTH_NOT_EXPR : ERROR_MARK);
9228 rhs = cp_parser_simple_cast_expression (parser);
9230 /* Get another operator token. Look up its precedence to avoid
9231 building a useless (immediately popped) stack entry for common
9232 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9233 token = cp_lexer_peek_token (parser->lexer);
9234 lookahead_prec = TOKEN_PRECEDENCE (token);
9235 if (lookahead_prec != PREC_NOT_OPERATOR
9236 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9237 lookahead_prec = PREC_NOT_OPERATOR;
9238 if (lookahead_prec > new_prec)
9240 /* ... and prepare to parse the RHS of the new, higher priority
9241 expression. Since precedence levels on the stack are
9242 monotonically increasing, we do not have to care about
9243 stack overflows. */
9244 *sp = current;
9245 ++sp;
9246 current.lhs = rhs;
9247 current.lhs_type = rhs_type;
9248 current.prec = new_prec;
9249 new_prec = lookahead_prec;
9250 goto get_rhs;
9252 pop:
9253 lookahead_prec = new_prec;
9254 /* If the stack is not empty, we have parsed into LHS the right side
9255 (`4' in the example above) of an expression we had suspended.
9256 We can use the information on the stack to recover the LHS (`3')
9257 from the stack together with the tree code (`MULT_EXPR'), and
9258 the precedence of the higher level subexpression
9259 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9260 which will be used to actually build the additive expression. */
9261 rhs = current.lhs;
9262 rhs_type = current.lhs_type;
9263 --sp;
9264 current = *sp;
9267 /* Undo the disabling of warnings done above. */
9268 if (current.tree_type == TRUTH_ANDIF_EXPR)
9269 c_inhibit_evaluation_warnings -=
9270 cp_fully_fold (current.lhs) == truthvalue_false_node;
9271 else if (current.tree_type == TRUTH_ORIF_EXPR)
9272 c_inhibit_evaluation_warnings -=
9273 cp_fully_fold (current.lhs) == truthvalue_true_node;
9275 if (warn_logical_not_paren
9276 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9277 && current.lhs_type == TRUTH_NOT_EXPR
9278 /* Avoid warning for !!x == y. */
9279 && (TREE_CODE (current.lhs) != NE_EXPR
9280 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9281 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9282 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9283 /* Avoid warning for !b == y where b is boolean. */
9284 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9285 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9286 != BOOLEAN_TYPE))))
9287 /* Avoid warning for !!b == y where b is boolean. */
9288 && (!DECL_P (current.lhs)
9289 || TREE_TYPE (current.lhs) == NULL_TREE
9290 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9291 warn_logical_not_parentheses (current.loc, current.tree_type,
9292 current.lhs, maybe_constant_value (rhs));
9294 overload = NULL;
9296 location_t combined_loc = make_location (current.loc,
9297 current.lhs.get_start (),
9298 rhs.get_finish ());
9300 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9301 ERROR_MARK for everything that is not a binary expression.
9302 This makes warn_about_parentheses miss some warnings that
9303 involve unary operators. For unary expressions we should
9304 pass the correct tree_code unless the unary expression was
9305 surrounded by parentheses.
9307 if (no_toplevel_fold_p
9308 && lookahead_prec <= current.prec
9309 && sp == stack)
9311 if (current.lhs == error_mark_node || rhs == error_mark_node)
9312 current.lhs = error_mark_node;
9313 else
9315 current.lhs
9316 = build_min (current.tree_type,
9317 TREE_CODE_CLASS (current.tree_type)
9318 == tcc_comparison
9319 ? boolean_type_node : TREE_TYPE (current.lhs),
9320 current.lhs.get_value (), rhs.get_value ());
9321 SET_EXPR_LOCATION (current.lhs, combined_loc);
9324 else
9326 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9327 current.lhs, current.lhs_type,
9328 rhs, rhs_type, &overload,
9329 complain_flags (decltype_p));
9330 /* TODO: build_x_binary_op doesn't always honor the location. */
9331 current.lhs.set_location (combined_loc);
9333 current.lhs_type = current.tree_type;
9335 /* If the binary operator required the use of an overloaded operator,
9336 then this expression cannot be an integral constant-expression.
9337 An overloaded operator can be used even if both operands are
9338 otherwise permissible in an integral constant-expression if at
9339 least one of the operands is of enumeration type. */
9341 if (overload
9342 && cp_parser_non_integral_constant_expression (parser,
9343 NIC_OVERLOADED))
9344 return error_mark_node;
9347 return current.lhs;
9350 static cp_expr
9351 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9352 bool no_toplevel_fold_p,
9353 enum cp_parser_prec prec,
9354 cp_id_kind * pidk)
9356 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9357 /*decltype*/false, prec, pidk);
9360 /* Parse the `? expression : assignment-expression' part of a
9361 conditional-expression. The LOGICAL_OR_EXPR is the
9362 logical-or-expression that started the conditional-expression.
9363 Returns a representation of the entire conditional-expression.
9365 This routine is used by cp_parser_assignment_expression.
9367 ? expression : assignment-expression
9369 GNU Extensions:
9371 ? : assignment-expression */
9373 static tree
9374 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9376 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9377 cp_expr assignment_expr;
9378 struct cp_token *token;
9379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9381 /* Consume the `?' token. */
9382 cp_lexer_consume_token (parser->lexer);
9383 token = cp_lexer_peek_token (parser->lexer);
9384 if (cp_parser_allow_gnu_extensions_p (parser)
9385 && token->type == CPP_COLON)
9387 pedwarn (token->location, OPT_Wpedantic,
9388 "ISO C++ does not allow ?: with omitted middle operand");
9389 /* Implicit true clause. */
9390 expr = NULL_TREE;
9391 c_inhibit_evaluation_warnings +=
9392 folded_logical_or_expr == truthvalue_true_node;
9393 warn_for_omitted_condop (token->location, logical_or_expr);
9395 else
9397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9398 parser->colon_corrects_to_scope_p = false;
9399 /* Parse the expression. */
9400 c_inhibit_evaluation_warnings +=
9401 folded_logical_or_expr == truthvalue_false_node;
9402 expr = cp_parser_expression (parser);
9403 c_inhibit_evaluation_warnings +=
9404 ((folded_logical_or_expr == truthvalue_true_node)
9405 - (folded_logical_or_expr == truthvalue_false_node));
9406 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9409 /* The next token should be a `:'. */
9410 cp_parser_require (parser, CPP_COLON, RT_COLON);
9411 /* Parse the assignment-expression. */
9412 assignment_expr = cp_parser_assignment_expression (parser);
9413 c_inhibit_evaluation_warnings -=
9414 folded_logical_or_expr == truthvalue_true_node;
9416 /* Make a location:
9417 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9418 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9419 with the caret at the "?", ranging from the start of
9420 the logical_or_expr to the end of the assignment_expr. */
9421 loc = make_location (loc,
9422 logical_or_expr.get_start (),
9423 assignment_expr.get_finish ());
9425 /* Build the conditional-expression. */
9426 return build_x_conditional_expr (loc, logical_or_expr,
9427 expr,
9428 assignment_expr,
9429 tf_warning_or_error);
9432 /* Parse an assignment-expression.
9434 assignment-expression:
9435 conditional-expression
9436 logical-or-expression assignment-operator assignment_expression
9437 throw-expression
9439 CAST_P is true if this expression is the target of a cast.
9440 DECLTYPE_P is true if this expression is the operand of decltype.
9442 Returns a representation for the expression. */
9444 static cp_expr
9445 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9446 bool cast_p, bool decltype_p)
9448 cp_expr expr;
9450 /* If the next token is the `throw' keyword, then we're looking at
9451 a throw-expression. */
9452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9453 expr = cp_parser_throw_expression (parser);
9454 /* Otherwise, it must be that we are looking at a
9455 logical-or-expression. */
9456 else
9458 /* Parse the binary expressions (logical-or-expression). */
9459 expr = cp_parser_binary_expression (parser, cast_p, false,
9460 decltype_p,
9461 PREC_NOT_OPERATOR, pidk);
9462 /* If the next token is a `?' then we're actually looking at a
9463 conditional-expression. */
9464 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9465 return cp_parser_question_colon_clause (parser, expr);
9466 else
9468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9470 /* If it's an assignment-operator, we're using the second
9471 production. */
9472 enum tree_code assignment_operator
9473 = cp_parser_assignment_operator_opt (parser);
9474 if (assignment_operator != ERROR_MARK)
9476 bool non_constant_p;
9478 /* Parse the right-hand side of the assignment. */
9479 cp_expr rhs = cp_parser_initializer_clause (parser,
9480 &non_constant_p);
9482 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9485 /* An assignment may not appear in a
9486 constant-expression. */
9487 if (cp_parser_non_integral_constant_expression (parser,
9488 NIC_ASSIGNMENT))
9489 return error_mark_node;
9490 /* Build the assignment expression. Its default
9491 location:
9492 LHS = RHS
9493 ~~~~^~~~~
9494 is the location of the '=' token as the
9495 caret, ranging from the start of the lhs to the
9496 end of the rhs. */
9497 loc = make_location (loc,
9498 expr.get_start (),
9499 rhs.get_finish ());
9500 expr = build_x_modify_expr (loc, expr,
9501 assignment_operator,
9502 rhs,
9503 complain_flags (decltype_p));
9504 /* TODO: build_x_modify_expr doesn't honor the location,
9505 so we must set it here. */
9506 expr.set_location (loc);
9511 return expr;
9514 /* Parse an (optional) assignment-operator.
9516 assignment-operator: one of
9517 = *= /= %= += -= >>= <<= &= ^= |=
9519 GNU Extension:
9521 assignment-operator: one of
9522 <?= >?=
9524 If the next token is an assignment operator, the corresponding tree
9525 code is returned, and the token is consumed. For example, for
9526 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9527 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9528 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9529 operator, ERROR_MARK is returned. */
9531 static enum tree_code
9532 cp_parser_assignment_operator_opt (cp_parser* parser)
9534 enum tree_code op;
9535 cp_token *token;
9537 /* Peek at the next token. */
9538 token = cp_lexer_peek_token (parser->lexer);
9540 switch (token->type)
9542 case CPP_EQ:
9543 op = NOP_EXPR;
9544 break;
9546 case CPP_MULT_EQ:
9547 op = MULT_EXPR;
9548 break;
9550 case CPP_DIV_EQ:
9551 op = TRUNC_DIV_EXPR;
9552 break;
9554 case CPP_MOD_EQ:
9555 op = TRUNC_MOD_EXPR;
9556 break;
9558 case CPP_PLUS_EQ:
9559 op = PLUS_EXPR;
9560 break;
9562 case CPP_MINUS_EQ:
9563 op = MINUS_EXPR;
9564 break;
9566 case CPP_RSHIFT_EQ:
9567 op = RSHIFT_EXPR;
9568 break;
9570 case CPP_LSHIFT_EQ:
9571 op = LSHIFT_EXPR;
9572 break;
9574 case CPP_AND_EQ:
9575 op = BIT_AND_EXPR;
9576 break;
9578 case CPP_XOR_EQ:
9579 op = BIT_XOR_EXPR;
9580 break;
9582 case CPP_OR_EQ:
9583 op = BIT_IOR_EXPR;
9584 break;
9586 default:
9587 /* Nothing else is an assignment operator. */
9588 op = ERROR_MARK;
9591 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9592 if (op != ERROR_MARK
9593 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9594 op = ERROR_MARK;
9596 /* If it was an assignment operator, consume it. */
9597 if (op != ERROR_MARK)
9598 cp_lexer_consume_token (parser->lexer);
9600 return op;
9603 /* Parse an expression.
9605 expression:
9606 assignment-expression
9607 expression , assignment-expression
9609 CAST_P is true if this expression is the target of a cast.
9610 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9611 except possibly parenthesized or on the RHS of a comma (N3276).
9613 Returns a representation of the expression. */
9615 static cp_expr
9616 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9617 bool cast_p, bool decltype_p)
9619 cp_expr expression = NULL_TREE;
9620 location_t loc = UNKNOWN_LOCATION;
9622 while (true)
9624 cp_expr assignment_expression;
9626 /* Parse the next assignment-expression. */
9627 assignment_expression
9628 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9630 /* We don't create a temporary for a call that is the immediate operand
9631 of decltype or on the RHS of a comma. But when we see a comma, we
9632 need to create a temporary for a call on the LHS. */
9633 if (decltype_p && !processing_template_decl
9634 && TREE_CODE (assignment_expression) == CALL_EXPR
9635 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9636 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9637 assignment_expression
9638 = build_cplus_new (TREE_TYPE (assignment_expression),
9639 assignment_expression, tf_warning_or_error);
9641 /* If this is the first assignment-expression, we can just
9642 save it away. */
9643 if (!expression)
9644 expression = assignment_expression;
9645 else
9647 /* Create a location with caret at the comma, ranging
9648 from the start of the LHS to the end of the RHS. */
9649 loc = make_location (loc,
9650 expression.get_start (),
9651 assignment_expression.get_finish ());
9652 expression = build_x_compound_expr (loc, expression,
9653 assignment_expression,
9654 complain_flags (decltype_p));
9655 expression.set_location (loc);
9657 /* If the next token is not a comma, or we're in a fold-expression, then
9658 we are done with the expression. */
9659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9660 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9661 break;
9662 /* Consume the `,'. */
9663 loc = cp_lexer_peek_token (parser->lexer)->location;
9664 cp_lexer_consume_token (parser->lexer);
9665 /* A comma operator cannot appear in a constant-expression. */
9666 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9667 expression = error_mark_node;
9670 return expression;
9673 /* Parse a constant-expression.
9675 constant-expression:
9676 conditional-expression
9678 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9679 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9680 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9681 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9682 only parse a conditional-expression, otherwise parse an
9683 assignment-expression. See below for rationale. */
9685 static cp_expr
9686 cp_parser_constant_expression (cp_parser* parser,
9687 bool allow_non_constant_p,
9688 bool *non_constant_p,
9689 bool strict_p)
9691 bool saved_integral_constant_expression_p;
9692 bool saved_allow_non_integral_constant_expression_p;
9693 bool saved_non_integral_constant_expression_p;
9694 cp_expr expression;
9696 /* It might seem that we could simply parse the
9697 conditional-expression, and then check to see if it were
9698 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9699 one that the compiler can figure out is constant, possibly after
9700 doing some simplifications or optimizations. The standard has a
9701 precise definition of constant-expression, and we must honor
9702 that, even though it is somewhat more restrictive.
9704 For example:
9706 int i[(2, 3)];
9708 is not a legal declaration, because `(2, 3)' is not a
9709 constant-expression. The `,' operator is forbidden in a
9710 constant-expression. However, GCC's constant-folding machinery
9711 will fold this operation to an INTEGER_CST for `3'. */
9713 /* Save the old settings. */
9714 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9715 saved_allow_non_integral_constant_expression_p
9716 = parser->allow_non_integral_constant_expression_p;
9717 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9718 /* We are now parsing a constant-expression. */
9719 parser->integral_constant_expression_p = true;
9720 parser->allow_non_integral_constant_expression_p
9721 = (allow_non_constant_p || cxx_dialect >= cxx11);
9722 parser->non_integral_constant_expression_p = false;
9723 /* Although the grammar says "conditional-expression", when not STRICT_P,
9724 we parse an "assignment-expression", which also permits
9725 "throw-expression" and the use of assignment operators. In the case
9726 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9727 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9728 actually essential that we look for an assignment-expression.
9729 For example, cp_parser_initializer_clauses uses this function to
9730 determine whether a particular assignment-expression is in fact
9731 constant. */
9732 if (strict_p)
9734 /* Parse the binary expressions (logical-or-expression). */
9735 expression = cp_parser_binary_expression (parser, false, false, false,
9736 PREC_NOT_OPERATOR, NULL);
9737 /* If the next token is a `?' then we're actually looking at
9738 a conditional-expression; otherwise we're done. */
9739 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9740 expression = cp_parser_question_colon_clause (parser, expression);
9742 else
9743 expression = cp_parser_assignment_expression (parser);
9744 /* Restore the old settings. */
9745 parser->integral_constant_expression_p
9746 = saved_integral_constant_expression_p;
9747 parser->allow_non_integral_constant_expression_p
9748 = saved_allow_non_integral_constant_expression_p;
9749 if (cxx_dialect >= cxx11)
9751 /* Require an rvalue constant expression here; that's what our
9752 callers expect. Reference constant expressions are handled
9753 separately in e.g. cp_parser_template_argument. */
9754 tree decay = expression;
9755 if (TREE_TYPE (expression)
9756 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9757 decay = build_address (expression);
9758 bool is_const = potential_rvalue_constant_expression (decay);
9759 parser->non_integral_constant_expression_p = !is_const;
9760 if (!is_const && !allow_non_constant_p)
9761 require_potential_rvalue_constant_expression (decay);
9763 if (allow_non_constant_p)
9764 *non_constant_p = parser->non_integral_constant_expression_p;
9765 parser->non_integral_constant_expression_p
9766 = saved_non_integral_constant_expression_p;
9768 return expression;
9771 /* Parse __builtin_offsetof.
9773 offsetof-expression:
9774 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9776 offsetof-member-designator:
9777 id-expression
9778 | offsetof-member-designator "." id-expression
9779 | offsetof-member-designator "[" expression "]"
9780 | offsetof-member-designator "->" id-expression */
9782 static cp_expr
9783 cp_parser_builtin_offsetof (cp_parser *parser)
9785 int save_ice_p, save_non_ice_p;
9786 tree type;
9787 cp_expr expr;
9788 cp_id_kind dummy;
9789 cp_token *token;
9790 location_t finish_loc;
9792 /* We're about to accept non-integral-constant things, but will
9793 definitely yield an integral constant expression. Save and
9794 restore these values around our local parsing. */
9795 save_ice_p = parser->integral_constant_expression_p;
9796 save_non_ice_p = parser->non_integral_constant_expression_p;
9798 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9800 /* Consume the "__builtin_offsetof" token. */
9801 cp_lexer_consume_token (parser->lexer);
9802 /* Consume the opening `('. */
9803 matching_parens parens;
9804 parens.require_open (parser);
9805 /* Parse the type-id. */
9806 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9808 const char *saved_message = parser->type_definition_forbidden_message;
9809 parser->type_definition_forbidden_message
9810 = G_("types may not be defined within __builtin_offsetof");
9811 type = cp_parser_type_id (parser);
9812 parser->type_definition_forbidden_message = saved_message;
9814 /* Look for the `,'. */
9815 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9816 token = cp_lexer_peek_token (parser->lexer);
9818 /* Build the (type *)null that begins the traditional offsetof macro. */
9819 tree object_ptr
9820 = build_static_cast (build_pointer_type (type), null_pointer_node,
9821 tf_warning_or_error);
9823 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9824 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9825 true, &dummy, token->location);
9826 while (true)
9828 token = cp_lexer_peek_token (parser->lexer);
9829 switch (token->type)
9831 case CPP_OPEN_SQUARE:
9832 /* offsetof-member-designator "[" expression "]" */
9833 expr = cp_parser_postfix_open_square_expression (parser, expr,
9834 true, false);
9835 break;
9837 case CPP_DEREF:
9838 /* offsetof-member-designator "->" identifier */
9839 expr = grok_array_decl (token->location, expr,
9840 integer_zero_node, false);
9841 /* FALLTHRU */
9843 case CPP_DOT:
9844 /* offsetof-member-designator "." identifier */
9845 cp_lexer_consume_token (parser->lexer);
9846 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9847 expr, true, &dummy,
9848 token->location);
9849 break;
9851 case CPP_CLOSE_PAREN:
9852 /* Consume the ")" token. */
9853 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9854 cp_lexer_consume_token (parser->lexer);
9855 goto success;
9857 default:
9858 /* Error. We know the following require will fail, but
9859 that gives the proper error message. */
9860 parens.require_close (parser);
9861 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9862 expr = error_mark_node;
9863 goto failure;
9867 success:
9868 /* Make a location of the form:
9869 __builtin_offsetof (struct s, f)
9870 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9871 with caret at the type-id, ranging from the start of the
9872 "_builtin_offsetof" token to the close paren. */
9873 loc = make_location (loc, start_loc, finish_loc);
9874 /* The result will be an INTEGER_CST, so we need to explicitly
9875 preserve the location. */
9876 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9878 failure:
9879 parser->integral_constant_expression_p = save_ice_p;
9880 parser->non_integral_constant_expression_p = save_non_ice_p;
9882 expr = expr.maybe_add_location_wrapper ();
9883 return expr;
9886 /* Parse a trait expression.
9888 Returns a representation of the expression, the underlying type
9889 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9891 static cp_expr
9892 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9894 cp_trait_kind kind;
9895 tree type1, type2 = NULL_TREE;
9896 bool binary = false;
9897 bool variadic = false;
9899 switch (keyword)
9901 case RID_HAS_NOTHROW_ASSIGN:
9902 kind = CPTK_HAS_NOTHROW_ASSIGN;
9903 break;
9904 case RID_HAS_NOTHROW_CONSTRUCTOR:
9905 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9906 break;
9907 case RID_HAS_NOTHROW_COPY:
9908 kind = CPTK_HAS_NOTHROW_COPY;
9909 break;
9910 case RID_HAS_TRIVIAL_ASSIGN:
9911 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9912 break;
9913 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9914 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9915 break;
9916 case RID_HAS_TRIVIAL_COPY:
9917 kind = CPTK_HAS_TRIVIAL_COPY;
9918 break;
9919 case RID_HAS_TRIVIAL_DESTRUCTOR:
9920 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9921 break;
9922 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9923 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9924 break;
9925 case RID_HAS_VIRTUAL_DESTRUCTOR:
9926 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9927 break;
9928 case RID_IS_ABSTRACT:
9929 kind = CPTK_IS_ABSTRACT;
9930 break;
9931 case RID_IS_AGGREGATE:
9932 kind = CPTK_IS_AGGREGATE;
9933 break;
9934 case RID_IS_BASE_OF:
9935 kind = CPTK_IS_BASE_OF;
9936 binary = true;
9937 break;
9938 case RID_IS_CLASS:
9939 kind = CPTK_IS_CLASS;
9940 break;
9941 case RID_IS_EMPTY:
9942 kind = CPTK_IS_EMPTY;
9943 break;
9944 case RID_IS_ENUM:
9945 kind = CPTK_IS_ENUM;
9946 break;
9947 case RID_IS_FINAL:
9948 kind = CPTK_IS_FINAL;
9949 break;
9950 case RID_IS_LITERAL_TYPE:
9951 kind = CPTK_IS_LITERAL_TYPE;
9952 break;
9953 case RID_IS_POD:
9954 kind = CPTK_IS_POD;
9955 break;
9956 case RID_IS_POLYMORPHIC:
9957 kind = CPTK_IS_POLYMORPHIC;
9958 break;
9959 case RID_IS_SAME_AS:
9960 kind = CPTK_IS_SAME_AS;
9961 binary = true;
9962 break;
9963 case RID_IS_STD_LAYOUT:
9964 kind = CPTK_IS_STD_LAYOUT;
9965 break;
9966 case RID_IS_TRIVIAL:
9967 kind = CPTK_IS_TRIVIAL;
9968 break;
9969 case RID_IS_TRIVIALLY_ASSIGNABLE:
9970 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9971 binary = true;
9972 break;
9973 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9974 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9975 variadic = true;
9976 break;
9977 case RID_IS_TRIVIALLY_COPYABLE:
9978 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9979 break;
9980 case RID_IS_UNION:
9981 kind = CPTK_IS_UNION;
9982 break;
9983 case RID_UNDERLYING_TYPE:
9984 kind = CPTK_UNDERLYING_TYPE;
9985 break;
9986 case RID_BASES:
9987 kind = CPTK_BASES;
9988 break;
9989 case RID_DIRECT_BASES:
9990 kind = CPTK_DIRECT_BASES;
9991 break;
9992 case RID_IS_ASSIGNABLE:
9993 kind = CPTK_IS_ASSIGNABLE;
9994 binary = true;
9995 break;
9996 case RID_IS_CONSTRUCTIBLE:
9997 kind = CPTK_IS_CONSTRUCTIBLE;
9998 variadic = true;
9999 break;
10000 default:
10001 gcc_unreachable ();
10004 /* Get location of initial token. */
10005 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10007 /* Consume the token. */
10008 cp_lexer_consume_token (parser->lexer);
10010 matching_parens parens;
10011 parens.require_open (parser);
10014 type_id_in_expr_sentinel s (parser);
10015 type1 = cp_parser_type_id (parser);
10018 if (type1 == error_mark_node)
10019 return error_mark_node;
10021 if (binary)
10023 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10026 type_id_in_expr_sentinel s (parser);
10027 type2 = cp_parser_type_id (parser);
10030 if (type2 == error_mark_node)
10031 return error_mark_node;
10033 else if (variadic)
10035 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10037 cp_lexer_consume_token (parser->lexer);
10038 tree elt = cp_parser_type_id (parser);
10039 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10041 cp_lexer_consume_token (parser->lexer);
10042 elt = make_pack_expansion (elt);
10044 if (elt == error_mark_node)
10045 return error_mark_node;
10046 type2 = tree_cons (NULL_TREE, elt, type2);
10050 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10051 parens.require_close (parser);
10053 /* Construct a location of the form:
10054 __is_trivially_copyable(_Tp)
10055 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10056 with start == caret, finishing at the close-paren. */
10057 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10059 /* Complete the trait expression, which may mean either processing
10060 the trait expr now or saving it for template instantiation. */
10061 switch (kind)
10063 case CPTK_UNDERLYING_TYPE:
10064 return cp_expr (finish_underlying_type (type1), trait_loc);
10065 case CPTK_BASES:
10066 return cp_expr (finish_bases (type1, false), trait_loc);
10067 case CPTK_DIRECT_BASES:
10068 return cp_expr (finish_bases (type1, true), trait_loc);
10069 default:
10070 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10074 /* Parse a lambda expression.
10076 lambda-expression:
10077 lambda-introducer lambda-declarator [opt] compound-statement
10079 Returns a representation of the expression. */
10081 static cp_expr
10082 cp_parser_lambda_expression (cp_parser* parser)
10084 tree lambda_expr = build_lambda_expr ();
10085 tree type;
10086 bool ok = true;
10087 cp_token *token = cp_lexer_peek_token (parser->lexer);
10088 cp_token_position start = 0;
10090 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10092 if (cp_unevaluated_operand)
10094 if (!token->error_reported)
10096 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10097 "lambda-expression in unevaluated context");
10098 token->error_reported = true;
10100 ok = false;
10102 else if (parser->in_template_argument_list_p)
10104 if (!token->error_reported)
10106 error_at (token->location, "lambda-expression in template-argument");
10107 token->error_reported = true;
10109 ok = false;
10112 /* We may be in the middle of deferred access check. Disable
10113 it now. */
10114 push_deferring_access_checks (dk_no_deferred);
10116 cp_parser_lambda_introducer (parser, lambda_expr);
10118 type = begin_lambda_type (lambda_expr);
10119 if (type == error_mark_node)
10120 return error_mark_node;
10122 record_lambda_scope (lambda_expr);
10124 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10125 determine_visibility (TYPE_NAME (type));
10127 /* Now that we've started the type, add the capture fields for any
10128 explicit captures. */
10129 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10132 /* Inside the class, surrounding template-parameter-lists do not apply. */
10133 unsigned int saved_num_template_parameter_lists
10134 = parser->num_template_parameter_lists;
10135 unsigned char in_statement = parser->in_statement;
10136 bool in_switch_statement_p = parser->in_switch_statement_p;
10137 bool fully_implicit_function_template_p
10138 = parser->fully_implicit_function_template_p;
10139 tree implicit_template_parms = parser->implicit_template_parms;
10140 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10141 bool auto_is_implicit_function_template_parm_p
10142 = parser->auto_is_implicit_function_template_parm_p;
10144 parser->num_template_parameter_lists = 0;
10145 parser->in_statement = 0;
10146 parser->in_switch_statement_p = false;
10147 parser->fully_implicit_function_template_p = false;
10148 parser->implicit_template_parms = 0;
10149 parser->implicit_template_scope = 0;
10150 parser->auto_is_implicit_function_template_parm_p = false;
10152 /* By virtue of defining a local class, a lambda expression has access to
10153 the private variables of enclosing classes. */
10155 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10157 if (ok && cp_parser_error_occurred (parser))
10158 ok = false;
10160 if (ok)
10162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10163 && cp_parser_start_tentative_firewall (parser))
10164 start = token;
10165 cp_parser_lambda_body (parser, lambda_expr);
10167 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10169 if (cp_parser_skip_to_closing_brace (parser))
10170 cp_lexer_consume_token (parser->lexer);
10173 /* The capture list was built up in reverse order; fix that now. */
10174 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10175 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10177 if (ok)
10178 maybe_add_lambda_conv_op (type);
10180 type = finish_struct (type, /*attributes=*/NULL_TREE);
10182 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10183 parser->in_statement = in_statement;
10184 parser->in_switch_statement_p = in_switch_statement_p;
10185 parser->fully_implicit_function_template_p
10186 = fully_implicit_function_template_p;
10187 parser->implicit_template_parms = implicit_template_parms;
10188 parser->implicit_template_scope = implicit_template_scope;
10189 parser->auto_is_implicit_function_template_parm_p
10190 = auto_is_implicit_function_template_parm_p;
10193 /* This field is only used during parsing of the lambda. */
10194 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10196 /* This lambda shouldn't have any proxies left at this point. */
10197 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10198 /* And now that we're done, push proxies for an enclosing lambda. */
10199 insert_pending_capture_proxies ();
10201 if (ok)
10202 lambda_expr = build_lambda_object (lambda_expr);
10203 else
10204 lambda_expr = error_mark_node;
10206 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10208 pop_deferring_access_checks ();
10210 return lambda_expr;
10213 /* Parse the beginning of a lambda expression.
10215 lambda-introducer:
10216 [ lambda-capture [opt] ]
10218 LAMBDA_EXPR is the current representation of the lambda expression. */
10220 static void
10221 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10223 /* Need commas after the first capture. */
10224 bool first = true;
10226 /* Eat the leading `['. */
10227 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10229 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10230 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10231 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10232 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10233 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10234 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10236 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10238 cp_lexer_consume_token (parser->lexer);
10239 first = false;
10242 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10244 cp_token* capture_token;
10245 tree capture_id;
10246 tree capture_init_expr;
10247 cp_id_kind idk = CP_ID_KIND_NONE;
10248 bool explicit_init_p = false;
10250 enum capture_kind_type
10252 BY_COPY,
10253 BY_REFERENCE
10255 enum capture_kind_type capture_kind = BY_COPY;
10257 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10259 error ("expected end of capture-list");
10260 return;
10263 if (first)
10264 first = false;
10265 else
10266 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10268 /* Possibly capture `this'. */
10269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10271 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10272 if (cxx_dialect < cxx2a
10273 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10274 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10275 "with by-copy capture default");
10276 cp_lexer_consume_token (parser->lexer);
10277 add_capture (lambda_expr,
10278 /*id=*/this_identifier,
10279 /*initializer=*/finish_this_expr (),
10280 /*by_reference_p=*/true,
10281 explicit_init_p);
10282 continue;
10285 /* Possibly capture `*this'. */
10286 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10287 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10290 if (cxx_dialect < cxx17)
10291 pedwarn (loc, 0, "%<*this%> capture only available with "
10292 "-std=c++17 or -std=gnu++17");
10293 cp_lexer_consume_token (parser->lexer);
10294 cp_lexer_consume_token (parser->lexer);
10295 add_capture (lambda_expr,
10296 /*id=*/this_identifier,
10297 /*initializer=*/finish_this_expr (),
10298 /*by_reference_p=*/false,
10299 explicit_init_p);
10300 continue;
10303 /* Remember whether we want to capture as a reference or not. */
10304 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10306 capture_kind = BY_REFERENCE;
10307 cp_lexer_consume_token (parser->lexer);
10310 /* Get the identifier. */
10311 capture_token = cp_lexer_peek_token (parser->lexer);
10312 capture_id = cp_parser_identifier (parser);
10314 if (capture_id == error_mark_node)
10315 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10316 delimiters, but I modified this to stop on unnested ']' as well. It
10317 was already changed to stop on unnested '}', so the
10318 "closing_parenthesis" name is no more misleading with my change. */
10320 cp_parser_skip_to_closing_parenthesis (parser,
10321 /*recovering=*/true,
10322 /*or_comma=*/true,
10323 /*consume_paren=*/true);
10324 break;
10327 /* Find the initializer for this capture. */
10328 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10329 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10330 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10332 bool direct, non_constant;
10333 /* An explicit initializer exists. */
10334 if (cxx_dialect < cxx14)
10335 pedwarn (input_location, 0,
10336 "lambda capture initializers "
10337 "only available with -std=c++14 or -std=gnu++14");
10338 capture_init_expr = cp_parser_initializer (parser, &direct,
10339 &non_constant, true);
10340 explicit_init_p = true;
10341 if (capture_init_expr == NULL_TREE)
10343 error ("empty initializer for lambda init-capture");
10344 capture_init_expr = error_mark_node;
10347 else
10349 const char* error_msg;
10351 /* Turn the identifier into an id-expression. */
10352 capture_init_expr
10353 = cp_parser_lookup_name_simple (parser, capture_id,
10354 capture_token->location);
10356 if (capture_init_expr == error_mark_node)
10358 unqualified_name_lookup_error (capture_id);
10359 continue;
10361 else if (!VAR_P (capture_init_expr)
10362 && TREE_CODE (capture_init_expr) != PARM_DECL)
10364 error_at (capture_token->location,
10365 "capture of non-variable %qE",
10366 capture_init_expr);
10367 if (DECL_P (capture_init_expr))
10368 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10369 "%q#D declared here", capture_init_expr);
10370 continue;
10372 if (VAR_P (capture_init_expr)
10373 && decl_storage_duration (capture_init_expr) != dk_auto)
10375 if (pedwarn (capture_token->location, 0, "capture of variable "
10376 "%qD with non-automatic storage duration",
10377 capture_init_expr))
10378 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10379 "%q#D declared here", capture_init_expr);
10380 continue;
10383 capture_init_expr
10384 = finish_id_expression
10385 (capture_id,
10386 capture_init_expr,
10387 parser->scope,
10388 &idk,
10389 /*integral_constant_expression_p=*/false,
10390 /*allow_non_integral_constant_expression_p=*/false,
10391 /*non_integral_constant_expression_p=*/NULL,
10392 /*template_p=*/false,
10393 /*done=*/true,
10394 /*address_p=*/false,
10395 /*template_arg_p=*/false,
10396 &error_msg,
10397 capture_token->location);
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10401 cp_lexer_consume_token (parser->lexer);
10402 capture_init_expr = make_pack_expansion (capture_init_expr);
10406 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10407 && !explicit_init_p)
10409 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10410 && capture_kind == BY_COPY)
10411 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10412 "of %qD redundant with by-copy capture default",
10413 capture_id);
10414 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10415 && capture_kind == BY_REFERENCE)
10416 pedwarn (capture_token->location, 0, "explicit by-reference "
10417 "capture of %qD redundant with by-reference capture "
10418 "default", capture_id);
10421 add_capture (lambda_expr,
10422 capture_id,
10423 capture_init_expr,
10424 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10425 explicit_init_p);
10427 /* If there is any qualification still in effect, clear it
10428 now; we will be starting fresh with the next capture. */
10429 parser->scope = NULL_TREE;
10430 parser->qualifying_scope = NULL_TREE;
10431 parser->object_scope = NULL_TREE;
10434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10437 /* Parse the (optional) middle of a lambda expression.
10439 lambda-declarator:
10440 < template-parameter-list [opt] >
10441 ( parameter-declaration-clause [opt] )
10442 attribute-specifier [opt]
10443 decl-specifier-seq [opt]
10444 exception-specification [opt]
10445 lambda-return-type-clause [opt]
10447 LAMBDA_EXPR is the current representation of the lambda expression. */
10449 static bool
10450 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10452 /* 5.1.1.4 of the standard says:
10453 If a lambda-expression does not include a lambda-declarator, it is as if
10454 the lambda-declarator were ().
10455 This means an empty parameter list, no attributes, and no exception
10456 specification. */
10457 tree param_list = void_list_node;
10458 tree attributes = NULL_TREE;
10459 tree exception_spec = NULL_TREE;
10460 tree template_param_list = NULL_TREE;
10461 tree tx_qual = NULL_TREE;
10462 tree return_type = NULL_TREE;
10463 cp_decl_specifier_seq lambda_specs;
10464 clear_decl_specs (&lambda_specs);
10466 /* The template-parameter-list is optional, but must begin with
10467 an opening angle if present. */
10468 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10470 if (cxx_dialect < cxx14)
10471 pedwarn (parser->lexer->next_token->location, 0,
10472 "lambda templates are only available with "
10473 "-std=c++14 or -std=gnu++14");
10474 else if (cxx_dialect < cxx2a)
10475 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10476 "lambda templates are only available with "
10477 "-std=c++2a or -std=gnu++2a");
10479 cp_lexer_consume_token (parser->lexer);
10481 template_param_list = cp_parser_template_parameter_list (parser);
10483 cp_parser_skip_to_end_of_template_parameter_list (parser);
10485 /* We just processed one more parameter list. */
10486 ++parser->num_template_parameter_lists;
10489 /* The parameter-declaration-clause is optional (unless
10490 template-parameter-list was given), but must begin with an
10491 opening parenthesis if present. */
10492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10494 matching_parens parens;
10495 parens.consume_open (parser);
10497 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10499 /* Parse parameters. */
10500 param_list = cp_parser_parameter_declaration_clause (parser);
10502 /* Default arguments shall not be specified in the
10503 parameter-declaration-clause of a lambda-declarator. */
10504 if (cxx_dialect < cxx14)
10505 for (tree t = param_list; t; t = TREE_CHAIN (t))
10506 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10507 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10508 "default argument specified for lambda parameter");
10510 parens.require_close (parser);
10512 attributes = cp_parser_attributes_opt (parser);
10514 /* In the decl-specifier-seq of the lambda-declarator, each
10515 decl-specifier shall either be mutable or constexpr. */
10516 int declares_class_or_enum;
10517 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10518 cp_parser_decl_specifier_seq (parser,
10519 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10520 &lambda_specs, &declares_class_or_enum);
10521 if (lambda_specs.storage_class == sc_mutable)
10523 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10524 if (lambda_specs.conflicting_specifiers_p)
10525 error_at (lambda_specs.locations[ds_storage_class],
10526 "duplicate %<mutable%>");
10529 tx_qual = cp_parser_tx_qualifier_opt (parser);
10531 /* Parse optional exception specification. */
10532 exception_spec = cp_parser_exception_specification_opt (parser);
10534 /* Parse optional trailing return type. */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10537 cp_lexer_consume_token (parser->lexer);
10538 return_type = cp_parser_trailing_type_id (parser);
10541 /* The function parameters must be in scope all the way until after the
10542 trailing-return-type in case of decltype. */
10543 pop_bindings_and_leave_scope ();
10545 else if (template_param_list != NULL_TREE) // generate diagnostic
10546 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10548 /* Create the function call operator.
10550 Messing with declarators like this is no uglier than building up the
10551 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10552 other code. */
10554 cp_decl_specifier_seq return_type_specs;
10555 cp_declarator* declarator;
10556 tree fco;
10557 int quals;
10558 void *p;
10560 clear_decl_specs (&return_type_specs);
10561 return_type_specs.type = make_auto ();
10563 if (lambda_specs.locations[ds_constexpr])
10565 if (cxx_dialect >= cxx17)
10566 return_type_specs.locations[ds_constexpr]
10567 = lambda_specs.locations[ds_constexpr];
10568 else
10569 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10570 "lambda only available with -std=c++17 or -std=gnu++17");
10573 p = obstack_alloc (&declarator_obstack, 0);
10575 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10577 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10578 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10579 declarator = make_call_declarator (declarator, param_list, quals,
10580 VIRT_SPEC_UNSPECIFIED,
10581 REF_QUAL_NONE,
10582 tx_qual,
10583 exception_spec,
10584 /*late_return_type=*/NULL_TREE,
10585 /*requires_clause*/NULL_TREE);
10586 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10587 if (return_type)
10588 declarator->u.function.late_return_type = return_type;
10590 fco = grokmethod (&return_type_specs,
10591 declarator,
10592 attributes);
10593 if (fco != error_mark_node)
10595 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10596 DECL_ARTIFICIAL (fco) = 1;
10597 /* Give the object parameter a different name. */
10598 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10600 if (template_param_list)
10602 fco = finish_member_template_decl (fco);
10603 finish_template_decl (template_param_list);
10604 --parser->num_template_parameter_lists;
10606 else if (parser->fully_implicit_function_template_p)
10607 fco = finish_fully_implicit_template (parser, fco);
10609 finish_member_declaration (fco);
10611 obstack_free (&declarator_obstack, p);
10613 return (fco != error_mark_node);
10617 /* Parse the body of a lambda expression, which is simply
10619 compound-statement
10621 but which requires special handling.
10622 LAMBDA_EXPR is the current representation of the lambda expression. */
10624 static void
10625 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10627 bool nested = (current_function_decl != NULL_TREE);
10628 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10629 bool in_function_body = parser->in_function_body;
10631 if (nested)
10632 push_function_context ();
10633 else
10634 /* Still increment function_depth so that we don't GC in the
10635 middle of an expression. */
10636 ++function_depth;
10638 vec<tree> omp_privatization_save;
10639 save_omp_privatization_clauses (omp_privatization_save);
10640 /* Clear this in case we're in the middle of a default argument. */
10641 parser->local_variables_forbidden_p = false;
10642 parser->in_function_body = true;
10645 local_specialization_stack s (lss_copy);
10646 tree fco = lambda_function (lambda_expr);
10647 tree body = start_lambda_function (fco, lambda_expr);
10648 matching_braces braces;
10650 if (braces.require_open (parser))
10652 tree compound_stmt = begin_compound_stmt (0);
10654 /* Originally C++11 required us to peek for 'return expr'; and
10655 process it specially here to deduce the return type. N3638
10656 removed the need for that. */
10658 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10659 cp_parser_label_declaration (parser);
10660 cp_parser_statement_seq_opt (parser, NULL_TREE);
10661 braces.require_close (parser);
10663 finish_compound_stmt (compound_stmt);
10666 finish_lambda_function (body);
10669 restore_omp_privatization_clauses (omp_privatization_save);
10670 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10671 parser->in_function_body = in_function_body;
10672 if (nested)
10673 pop_function_context();
10674 else
10675 --function_depth;
10678 /* Statements [gram.stmt.stmt] */
10680 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10682 static void
10683 add_debug_begin_stmt (location_t loc)
10685 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10686 return;
10687 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10688 /* A concept is never expanded normally. */
10689 return;
10691 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10692 SET_EXPR_LOCATION (stmt, loc);
10693 add_stmt (stmt);
10696 /* Parse a statement.
10698 statement:
10699 labeled-statement
10700 expression-statement
10701 compound-statement
10702 selection-statement
10703 iteration-statement
10704 jump-statement
10705 declaration-statement
10706 try-block
10708 C++11:
10710 statement:
10711 labeled-statement
10712 attribute-specifier-seq (opt) expression-statement
10713 attribute-specifier-seq (opt) compound-statement
10714 attribute-specifier-seq (opt) selection-statement
10715 attribute-specifier-seq (opt) iteration-statement
10716 attribute-specifier-seq (opt) jump-statement
10717 declaration-statement
10718 attribute-specifier-seq (opt) try-block
10720 init-statement:
10721 expression-statement
10722 simple-declaration
10724 TM Extension:
10726 statement:
10727 atomic-statement
10729 IN_COMPOUND is true when the statement is nested inside a
10730 cp_parser_compound_statement; this matters for certain pragmas.
10732 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10733 is a (possibly labeled) if statement which is not enclosed in braces
10734 and has an else clause. This is used to implement -Wparentheses.
10736 CHAIN is a vector of if-else-if conditions. */
10738 static void
10739 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10740 bool in_compound, bool *if_p, vec<tree> *chain,
10741 location_t *loc_after_labels)
10743 tree statement, std_attrs = NULL_TREE;
10744 cp_token *token;
10745 location_t statement_location, attrs_location;
10747 restart:
10748 if (if_p != NULL)
10749 *if_p = false;
10750 /* There is no statement yet. */
10751 statement = NULL_TREE;
10753 saved_token_sentinel saved_tokens (parser->lexer);
10754 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10755 if (c_dialect_objc ())
10756 /* In obj-c++, seeing '[[' might be the either the beginning of
10757 c++11 attributes, or a nested objc-message-expression. So
10758 let's parse the c++11 attributes tentatively. */
10759 cp_parser_parse_tentatively (parser);
10760 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10761 if (c_dialect_objc ())
10763 if (!cp_parser_parse_definitely (parser))
10764 std_attrs = NULL_TREE;
10767 /* Peek at the next token. */
10768 token = cp_lexer_peek_token (parser->lexer);
10769 /* Remember the location of the first token in the statement. */
10770 statement_location = token->location;
10771 add_debug_begin_stmt (statement_location);
10772 /* If this is a keyword, then that will often determine what kind of
10773 statement we have. */
10774 if (token->type == CPP_KEYWORD)
10776 enum rid keyword = token->keyword;
10778 switch (keyword)
10780 case RID_CASE:
10781 case RID_DEFAULT:
10782 /* Looks like a labeled-statement with a case label.
10783 Parse the label, and then use tail recursion to parse
10784 the statement. */
10785 cp_parser_label_for_labeled_statement (parser, std_attrs);
10786 in_compound = false;
10787 goto restart;
10789 case RID_IF:
10790 case RID_SWITCH:
10791 statement = cp_parser_selection_statement (parser, if_p, chain);
10792 break;
10794 case RID_WHILE:
10795 case RID_DO:
10796 case RID_FOR:
10797 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10798 break;
10800 case RID_BREAK:
10801 case RID_CONTINUE:
10802 case RID_RETURN:
10803 case RID_GOTO:
10804 statement = cp_parser_jump_statement (parser);
10805 break;
10807 /* Objective-C++ exception-handling constructs. */
10808 case RID_AT_TRY:
10809 case RID_AT_CATCH:
10810 case RID_AT_FINALLY:
10811 case RID_AT_SYNCHRONIZED:
10812 case RID_AT_THROW:
10813 statement = cp_parser_objc_statement (parser);
10814 break;
10816 case RID_TRY:
10817 statement = cp_parser_try_block (parser);
10818 break;
10820 case RID_NAMESPACE:
10821 /* This must be a namespace alias definition. */
10822 cp_parser_declaration_statement (parser);
10823 return;
10825 case RID_TRANSACTION_ATOMIC:
10826 case RID_TRANSACTION_RELAXED:
10827 case RID_SYNCHRONIZED:
10828 case RID_ATOMIC_NOEXCEPT:
10829 case RID_ATOMIC_CANCEL:
10830 statement = cp_parser_transaction (parser, token);
10831 break;
10832 case RID_TRANSACTION_CANCEL:
10833 statement = cp_parser_transaction_cancel (parser);
10834 break;
10836 default:
10837 /* It might be a keyword like `int' that can start a
10838 declaration-statement. */
10839 break;
10842 else if (token->type == CPP_NAME)
10844 /* If the next token is a `:', then we are looking at a
10845 labeled-statement. */
10846 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10847 if (token->type == CPP_COLON)
10849 /* Looks like a labeled-statement with an ordinary label.
10850 Parse the label, and then use tail recursion to parse
10851 the statement. */
10853 cp_parser_label_for_labeled_statement (parser, std_attrs);
10854 in_compound = false;
10855 goto restart;
10858 /* Anything that starts with a `{' must be a compound-statement. */
10859 else if (token->type == CPP_OPEN_BRACE)
10860 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10861 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10862 a statement all its own. */
10863 else if (token->type == CPP_PRAGMA)
10865 /* Only certain OpenMP pragmas are attached to statements, and thus
10866 are considered statements themselves. All others are not. In
10867 the context of a compound, accept the pragma as a "statement" and
10868 return so that we can check for a close brace. Otherwise we
10869 require a real statement and must go back and read one. */
10870 if (in_compound)
10871 cp_parser_pragma (parser, pragma_compound, if_p);
10872 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10873 goto restart;
10874 return;
10876 else if (token->type == CPP_EOF)
10878 cp_parser_error (parser, "expected statement");
10879 return;
10882 /* Everything else must be a declaration-statement or an
10883 expression-statement. Try for the declaration-statement
10884 first, unless we are looking at a `;', in which case we know that
10885 we have an expression-statement. */
10886 if (!statement)
10888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10890 if (std_attrs != NULL_TREE)
10892 /* Attributes should be parsed as part of the the
10893 declaration, so let's un-parse them. */
10894 saved_tokens.rollback();
10895 std_attrs = NULL_TREE;
10898 cp_parser_parse_tentatively (parser);
10899 /* Try to parse the declaration-statement. */
10900 cp_parser_declaration_statement (parser);
10901 /* If that worked, we're done. */
10902 if (cp_parser_parse_definitely (parser))
10903 return;
10905 /* All preceding labels have been parsed at this point. */
10906 if (loc_after_labels != NULL)
10907 *loc_after_labels = statement_location;
10909 /* Look for an expression-statement instead. */
10910 statement = cp_parser_expression_statement (parser, in_statement_expr);
10912 /* Handle [[fallthrough]];. */
10913 if (attribute_fallthrough_p (std_attrs))
10915 /* The next token after the fallthrough attribute is ';'. */
10916 if (statement == NULL_TREE)
10918 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10919 statement = build_call_expr_internal_loc (statement_location,
10920 IFN_FALLTHROUGH,
10921 void_type_node, 0);
10922 finish_expr_stmt (statement);
10924 else
10925 warning_at (statement_location, OPT_Wattributes,
10926 "%<fallthrough%> attribute not followed by %<;%>");
10927 std_attrs = NULL_TREE;
10931 /* Set the line number for the statement. */
10932 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10933 SET_EXPR_LOCATION (statement, statement_location);
10935 /* Allow "[[fallthrough]];", but warn otherwise. */
10936 if (std_attrs != NULL_TREE)
10937 warning_at (attrs_location,
10938 OPT_Wattributes,
10939 "attributes at the beginning of statement are ignored");
10942 /* Append ATTR to attribute list ATTRS. */
10944 static tree
10945 attr_chainon (tree attrs, tree attr)
10947 if (attrs == error_mark_node)
10948 return error_mark_node;
10949 if (attr == error_mark_node)
10950 return error_mark_node;
10951 return chainon (attrs, attr);
10954 /* Parse the label for a labeled-statement, i.e.
10956 identifier :
10957 case constant-expression :
10958 default :
10960 GNU Extension:
10961 case constant-expression ... constant-expression : statement
10963 When a label is parsed without errors, the label is added to the
10964 parse tree by the finish_* functions, so this function doesn't
10965 have to return the label. */
10967 static void
10968 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10970 cp_token *token;
10971 tree label = NULL_TREE;
10972 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10974 /* The next token should be an identifier. */
10975 token = cp_lexer_peek_token (parser->lexer);
10976 if (token->type != CPP_NAME
10977 && token->type != CPP_KEYWORD)
10979 cp_parser_error (parser, "expected labeled-statement");
10980 return;
10983 /* Remember whether this case or a user-defined label is allowed to fall
10984 through to. */
10985 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10987 parser->colon_corrects_to_scope_p = false;
10988 switch (token->keyword)
10990 case RID_CASE:
10992 tree expr, expr_hi;
10993 cp_token *ellipsis;
10995 /* Consume the `case' token. */
10996 cp_lexer_consume_token (parser->lexer);
10997 /* Parse the constant-expression. */
10998 expr = cp_parser_constant_expression (parser);
10999 if (check_for_bare_parameter_packs (expr))
11000 expr = error_mark_node;
11002 ellipsis = cp_lexer_peek_token (parser->lexer);
11003 if (ellipsis->type == CPP_ELLIPSIS)
11005 /* Consume the `...' token. */
11006 cp_lexer_consume_token (parser->lexer);
11007 expr_hi = cp_parser_constant_expression (parser);
11008 if (check_for_bare_parameter_packs (expr_hi))
11009 expr_hi = error_mark_node;
11011 /* We don't need to emit warnings here, as the common code
11012 will do this for us. */
11014 else
11015 expr_hi = NULL_TREE;
11017 if (parser->in_switch_statement_p)
11019 tree l = finish_case_label (token->location, expr, expr_hi);
11020 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11021 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11023 else
11024 error_at (token->location,
11025 "case label %qE not within a switch statement",
11026 expr);
11028 break;
11030 case RID_DEFAULT:
11031 /* Consume the `default' token. */
11032 cp_lexer_consume_token (parser->lexer);
11034 if (parser->in_switch_statement_p)
11036 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11037 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11038 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11040 else
11041 error_at (token->location, "case label not within a switch statement");
11042 break;
11044 default:
11045 /* Anything else must be an ordinary label. */
11046 label = finish_label_stmt (cp_parser_identifier (parser));
11047 if (label && TREE_CODE (label) == LABEL_DECL)
11048 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11049 break;
11052 /* Require the `:' token. */
11053 cp_parser_require (parser, CPP_COLON, RT_COLON);
11055 /* An ordinary label may optionally be followed by attributes.
11056 However, this is only permitted if the attributes are then
11057 followed by a semicolon. This is because, for backward
11058 compatibility, when parsing
11059 lab: __attribute__ ((unused)) int i;
11060 we want the attribute to attach to "i", not "lab". */
11061 if (label != NULL_TREE
11062 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11064 tree attrs;
11065 cp_parser_parse_tentatively (parser);
11066 attrs = cp_parser_gnu_attributes_opt (parser);
11067 if (attrs == NULL_TREE
11068 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11069 cp_parser_abort_tentative_parse (parser);
11070 else if (!cp_parser_parse_definitely (parser))
11072 else
11073 attributes = attr_chainon (attributes, attrs);
11076 if (attributes != NULL_TREE)
11077 cplus_decl_attributes (&label, attributes, 0);
11079 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11082 /* Parse an expression-statement.
11084 expression-statement:
11085 expression [opt] ;
11087 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11088 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11089 indicates whether this expression-statement is part of an
11090 expression statement. */
11092 static tree
11093 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11095 tree statement = NULL_TREE;
11096 cp_token *token = cp_lexer_peek_token (parser->lexer);
11097 location_t loc = token->location;
11099 /* There might be attribute fallthrough. */
11100 tree attr = cp_parser_gnu_attributes_opt (parser);
11102 /* If the next token is a ';', then there is no expression
11103 statement. */
11104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11106 statement = cp_parser_expression (parser);
11107 if (statement == error_mark_node
11108 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11110 cp_parser_skip_to_end_of_block_or_statement (parser);
11111 return error_mark_node;
11115 /* Handle [[fallthrough]];. */
11116 if (attribute_fallthrough_p (attr))
11118 /* The next token after the fallthrough attribute is ';'. */
11119 if (statement == NULL_TREE)
11120 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11121 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11122 void_type_node, 0);
11123 else
11124 warning_at (loc, OPT_Wattributes,
11125 "%<fallthrough%> attribute not followed by %<;%>");
11126 attr = NULL_TREE;
11129 /* Allow "[[fallthrough]];", but warn otherwise. */
11130 if (attr != NULL_TREE)
11131 warning_at (loc, OPT_Wattributes,
11132 "attributes at the beginning of statement are ignored");
11134 /* Give a helpful message for "A<T>::type t;" and the like. */
11135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11136 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11138 if (TREE_CODE (statement) == SCOPE_REF)
11139 error_at (token->location, "need %<typename%> before %qE because "
11140 "%qT is a dependent scope",
11141 statement, TREE_OPERAND (statement, 0));
11142 else if (is_overloaded_fn (statement)
11143 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11145 /* A::A a; */
11146 tree fn = get_first_fn (statement);
11147 error_at (token->location,
11148 "%<%T::%D%> names the constructor, not the type",
11149 DECL_CONTEXT (fn), DECL_NAME (fn));
11153 /* Consume the final `;'. */
11154 cp_parser_consume_semicolon_at_end_of_statement (parser);
11156 if (in_statement_expr
11157 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11158 /* This is the final expression statement of a statement
11159 expression. */
11160 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11161 else if (statement)
11162 statement = finish_expr_stmt (statement);
11164 return statement;
11167 /* Parse a compound-statement.
11169 compound-statement:
11170 { statement-seq [opt] }
11172 GNU extension:
11174 compound-statement:
11175 { label-declaration-seq [opt] statement-seq [opt] }
11177 label-declaration-seq:
11178 label-declaration
11179 label-declaration-seq label-declaration
11181 Returns a tree representing the statement. */
11183 static tree
11184 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11185 int bcs_flags, bool function_body)
11187 tree compound_stmt;
11188 matching_braces braces;
11190 /* Consume the `{'. */
11191 if (!braces.require_open (parser))
11192 return error_mark_node;
11193 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11194 && !function_body && cxx_dialect < cxx14)
11195 pedwarn (input_location, OPT_Wpedantic,
11196 "compound-statement in %<constexpr%> function");
11197 /* Begin the compound-statement. */
11198 compound_stmt = begin_compound_stmt (bcs_flags);
11199 /* If the next keyword is `__label__' we have a label declaration. */
11200 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11201 cp_parser_label_declaration (parser);
11202 /* Parse an (optional) statement-seq. */
11203 cp_parser_statement_seq_opt (parser, in_statement_expr);
11204 /* Finish the compound-statement. */
11205 finish_compound_stmt (compound_stmt);
11206 /* Consume the `}'. */
11207 braces.require_close (parser);
11209 return compound_stmt;
11212 /* Parse an (optional) statement-seq.
11214 statement-seq:
11215 statement
11216 statement-seq [opt] statement */
11218 static void
11219 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11221 /* Scan statements until there aren't any more. */
11222 while (true)
11224 cp_token *token = cp_lexer_peek_token (parser->lexer);
11226 /* If we are looking at a `}', then we have run out of
11227 statements; the same is true if we have reached the end
11228 of file, or have stumbled upon a stray '@end'. */
11229 if (token->type == CPP_CLOSE_BRACE
11230 || token->type == CPP_EOF
11231 || token->type == CPP_PRAGMA_EOL
11232 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11233 break;
11235 /* If we are in a compound statement and find 'else' then
11236 something went wrong. */
11237 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11239 if (parser->in_statement & IN_IF_STMT)
11240 break;
11241 else
11243 token = cp_lexer_consume_token (parser->lexer);
11244 error_at (token->location, "%<else%> without a previous %<if%>");
11248 /* Parse the statement. */
11249 cp_parser_statement (parser, in_statement_expr, true, NULL);
11253 /* Return true if this is the C++20 version of range-based-for with
11254 init-statement. */
11256 static bool
11257 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11259 bool r = false;
11261 /* Save tokens so that we can put them back. */
11262 cp_lexer_save_tokens (parser->lexer);
11264 /* There has to be an unnested ; followed by an unnested :. */
11265 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11266 /*recovering=*/false,
11267 CPP_SEMICOLON,
11268 /*consume_paren=*/false) != -1)
11269 goto out;
11271 /* We found the semicolon, eat it now. */
11272 cp_lexer_consume_token (parser->lexer);
11274 /* Now look for ':' that is not nested in () or {}. */
11275 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11276 /*recovering=*/false,
11277 CPP_COLON,
11278 /*consume_paren=*/false) == -1);
11280 out:
11281 /* Roll back the tokens we skipped. */
11282 cp_lexer_rollback_tokens (parser->lexer);
11284 return r;
11287 /* Return true if we're looking at (init; cond), false otherwise. */
11289 static bool
11290 cp_parser_init_statement_p (cp_parser *parser)
11292 /* Save tokens so that we can put them back. */
11293 cp_lexer_save_tokens (parser->lexer);
11295 /* Look for ';' that is not nested in () or {}. */
11296 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11297 /*recovering=*/false,
11298 CPP_SEMICOLON,
11299 /*consume_paren=*/false);
11301 /* Roll back the tokens we skipped. */
11302 cp_lexer_rollback_tokens (parser->lexer);
11304 return ret == -1;
11307 /* Parse a selection-statement.
11309 selection-statement:
11310 if ( init-statement [opt] condition ) statement
11311 if ( init-statement [opt] condition ) statement else statement
11312 switch ( init-statement [opt] condition ) statement
11314 Returns the new IF_STMT or SWITCH_STMT.
11316 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11317 is a (possibly labeled) if statement which is not enclosed in
11318 braces and has an else clause. This is used to implement
11319 -Wparentheses.
11321 CHAIN is a vector of if-else-if conditions. This is used to implement
11322 -Wduplicated-cond. */
11324 static tree
11325 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11326 vec<tree> *chain)
11328 cp_token *token;
11329 enum rid keyword;
11330 token_indent_info guard_tinfo;
11332 if (if_p != NULL)
11333 *if_p = false;
11335 /* Peek at the next token. */
11336 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11337 guard_tinfo = get_token_indent_info (token);
11339 /* See what kind of keyword it is. */
11340 keyword = token->keyword;
11341 switch (keyword)
11343 case RID_IF:
11344 case RID_SWITCH:
11346 tree statement;
11347 tree condition;
11349 bool cx = false;
11350 if (keyword == RID_IF
11351 && cp_lexer_next_token_is_keyword (parser->lexer,
11352 RID_CONSTEXPR))
11354 cx = true;
11355 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11356 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11357 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11358 "with -std=c++17 or -std=gnu++17");
11361 /* Look for the `('. */
11362 matching_parens parens;
11363 if (!parens.require_open (parser))
11365 cp_parser_skip_to_end_of_statement (parser);
11366 return error_mark_node;
11369 /* Begin the selection-statement. */
11370 if (keyword == RID_IF)
11372 statement = begin_if_stmt ();
11373 IF_STMT_CONSTEXPR_P (statement) = cx;
11375 else
11376 statement = begin_switch_stmt ();
11378 /* Parse the optional init-statement. */
11379 if (cp_parser_init_statement_p (parser))
11381 tree decl;
11382 if (cxx_dialect < cxx17)
11383 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11384 "init-statement in selection statements only available "
11385 "with -std=c++17 or -std=gnu++17");
11386 cp_parser_init_statement (parser, &decl);
11389 /* Parse the condition. */
11390 condition = cp_parser_condition (parser);
11391 /* Look for the `)'. */
11392 if (!parens.require_close (parser))
11393 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11394 /*consume_paren=*/true);
11396 if (keyword == RID_IF)
11398 bool nested_if;
11399 unsigned char in_statement;
11401 /* Add the condition. */
11402 condition = finish_if_stmt_cond (condition, statement);
11404 if (warn_duplicated_cond)
11405 warn_duplicated_cond_add_or_warn (token->location, condition,
11406 &chain);
11408 /* Parse the then-clause. */
11409 in_statement = parser->in_statement;
11410 parser->in_statement |= IN_IF_STMT;
11412 /* Outside a template, the non-selected branch of a constexpr
11413 if is a 'discarded statement', i.e. unevaluated. */
11414 bool was_discarded = in_discarded_stmt;
11415 bool discard_then = (cx && !processing_template_decl
11416 && integer_zerop (condition));
11417 if (discard_then)
11419 in_discarded_stmt = true;
11420 ++c_inhibit_evaluation_warnings;
11423 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11424 guard_tinfo);
11426 parser->in_statement = in_statement;
11428 finish_then_clause (statement);
11430 if (discard_then)
11432 THEN_CLAUSE (statement) = NULL_TREE;
11433 in_discarded_stmt = was_discarded;
11434 --c_inhibit_evaluation_warnings;
11437 /* If the next token is `else', parse the else-clause. */
11438 if (cp_lexer_next_token_is_keyword (parser->lexer,
11439 RID_ELSE))
11441 bool discard_else = (cx && !processing_template_decl
11442 && integer_nonzerop (condition));
11443 if (discard_else)
11445 in_discarded_stmt = true;
11446 ++c_inhibit_evaluation_warnings;
11449 guard_tinfo
11450 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11451 /* Consume the `else' keyword. */
11452 cp_lexer_consume_token (parser->lexer);
11453 if (warn_duplicated_cond)
11455 if (cp_lexer_next_token_is_keyword (parser->lexer,
11456 RID_IF)
11457 && chain == NULL)
11459 /* We've got "if (COND) else if (COND2)". Start
11460 the condition chain and add COND as the first
11461 element. */
11462 chain = new vec<tree> ();
11463 if (!CONSTANT_CLASS_P (condition)
11464 && !TREE_SIDE_EFFECTS (condition))
11466 /* Wrap it in a NOP_EXPR so that we can set the
11467 location of the condition. */
11468 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11469 condition);
11470 SET_EXPR_LOCATION (e, token->location);
11471 chain->safe_push (e);
11474 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11475 RID_IF))
11477 /* This is if-else without subsequent if. Zap the
11478 condition chain; we would have already warned at
11479 this point. */
11480 delete chain;
11481 chain = NULL;
11484 begin_else_clause (statement);
11485 /* Parse the else-clause. */
11486 cp_parser_implicitly_scoped_statement (parser, NULL,
11487 guard_tinfo, chain);
11489 finish_else_clause (statement);
11491 /* If we are currently parsing a then-clause, then
11492 IF_P will not be NULL. We set it to true to
11493 indicate that this if statement has an else clause.
11494 This may trigger the Wparentheses warning below
11495 when we get back up to the parent if statement. */
11496 if (if_p != NULL)
11497 *if_p = true;
11499 if (discard_else)
11501 ELSE_CLAUSE (statement) = NULL_TREE;
11502 in_discarded_stmt = was_discarded;
11503 --c_inhibit_evaluation_warnings;
11506 else
11508 /* This if statement does not have an else clause. If
11509 NESTED_IF is true, then the then-clause has an if
11510 statement which does have an else clause. We warn
11511 about the potential ambiguity. */
11512 if (nested_if)
11513 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11514 "suggest explicit braces to avoid ambiguous"
11515 " %<else%>");
11516 if (warn_duplicated_cond)
11518 /* We don't need the condition chain anymore. */
11519 delete chain;
11520 chain = NULL;
11524 /* Now we're all done with the if-statement. */
11525 finish_if_stmt (statement);
11527 else
11529 bool in_switch_statement_p;
11530 unsigned char in_statement;
11532 /* Add the condition. */
11533 finish_switch_cond (condition, statement);
11535 /* Parse the body of the switch-statement. */
11536 in_switch_statement_p = parser->in_switch_statement_p;
11537 in_statement = parser->in_statement;
11538 parser->in_switch_statement_p = true;
11539 parser->in_statement |= IN_SWITCH_STMT;
11540 cp_parser_implicitly_scoped_statement (parser, if_p,
11541 guard_tinfo);
11542 parser->in_switch_statement_p = in_switch_statement_p;
11543 parser->in_statement = in_statement;
11545 /* Now we're all done with the switch-statement. */
11546 finish_switch_stmt (statement);
11549 return statement;
11551 break;
11553 default:
11554 cp_parser_error (parser, "expected selection-statement");
11555 return error_mark_node;
11559 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11560 If we have seen at least one decl-specifier, and the next token
11561 is not a parenthesis, then we must be looking at a declaration.
11562 (After "int (" we might be looking at a functional cast.) */
11564 static void
11565 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11566 bool any_specifiers_p)
11568 if (any_specifiers_p
11569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11571 && !cp_parser_error_occurred (parser))
11572 cp_parser_commit_to_tentative_parse (parser);
11575 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11576 The declarator shall not specify a function or an array. Returns
11577 TRUE if the declarator is valid, FALSE otherwise. */
11579 static bool
11580 cp_parser_check_condition_declarator (cp_parser* parser,
11581 cp_declarator *declarator,
11582 location_t loc)
11584 if (declarator == cp_error_declarator
11585 || function_declarator_p (declarator)
11586 || declarator->kind == cdk_array)
11588 if (declarator == cp_error_declarator)
11589 /* Already complained. */;
11590 else if (declarator->kind == cdk_array)
11591 error_at (loc, "condition declares an array");
11592 else
11593 error_at (loc, "condition declares a function");
11594 if (parser->fully_implicit_function_template_p)
11595 abort_fully_implicit_template (parser);
11596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11597 /*or_comma=*/false,
11598 /*consume_paren=*/false);
11599 return false;
11601 else
11602 return true;
11605 /* Parse a condition.
11607 condition:
11608 expression
11609 type-specifier-seq declarator = initializer-clause
11610 type-specifier-seq declarator braced-init-list
11612 GNU Extension:
11614 condition:
11615 type-specifier-seq declarator asm-specification [opt]
11616 attributes [opt] = assignment-expression
11618 Returns the expression that should be tested. */
11620 static tree
11621 cp_parser_condition (cp_parser* parser)
11623 cp_decl_specifier_seq type_specifiers;
11624 const char *saved_message;
11625 int declares_class_or_enum;
11627 /* Try the declaration first. */
11628 cp_parser_parse_tentatively (parser);
11629 /* New types are not allowed in the type-specifier-seq for a
11630 condition. */
11631 saved_message = parser->type_definition_forbidden_message;
11632 parser->type_definition_forbidden_message
11633 = G_("types may not be defined in conditions");
11634 /* Parse the type-specifier-seq. */
11635 cp_parser_decl_specifier_seq (parser,
11636 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11637 &type_specifiers,
11638 &declares_class_or_enum);
11639 /* Restore the saved message. */
11640 parser->type_definition_forbidden_message = saved_message;
11642 cp_parser_maybe_commit_to_declaration (parser,
11643 type_specifiers.any_specifiers_p);
11645 /* If all is well, we might be looking at a declaration. */
11646 if (!cp_parser_error_occurred (parser))
11648 tree decl;
11649 tree asm_specification;
11650 tree attributes;
11651 cp_declarator *declarator;
11652 tree initializer = NULL_TREE;
11653 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11655 /* Parse the declarator. */
11656 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11657 /*ctor_dtor_or_conv_p=*/NULL,
11658 /*parenthesized_p=*/NULL,
11659 /*member_p=*/false,
11660 /*friend_p=*/false);
11661 /* Parse the attributes. */
11662 attributes = cp_parser_attributes_opt (parser);
11663 /* Parse the asm-specification. */
11664 asm_specification = cp_parser_asm_specification_opt (parser);
11665 /* If the next token is not an `=' or '{', then we might still be
11666 looking at an expression. For example:
11668 if (A(a).x)
11670 looks like a decl-specifier-seq and a declarator -- but then
11671 there is no `=', so this is an expression. */
11672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11673 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11674 cp_parser_simulate_error (parser);
11676 /* If we did see an `=' or '{', then we are looking at a declaration
11677 for sure. */
11678 if (cp_parser_parse_definitely (parser))
11680 tree pushed_scope;
11681 bool non_constant_p;
11682 int flags = LOOKUP_ONLYCONVERTING;
11684 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11685 return error_mark_node;
11687 /* Create the declaration. */
11688 decl = start_decl (declarator, &type_specifiers,
11689 /*initialized_p=*/true,
11690 attributes, /*prefix_attributes=*/NULL_TREE,
11691 &pushed_scope);
11693 /* Parse the initializer. */
11694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11696 initializer = cp_parser_braced_list (parser, &non_constant_p);
11697 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11698 flags = 0;
11700 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11702 /* Consume the `='. */
11703 cp_lexer_consume_token (parser->lexer);
11704 initializer = cp_parser_initializer_clause (parser,
11705 &non_constant_p);
11707 else
11709 cp_parser_error (parser, "expected initializer");
11710 initializer = error_mark_node;
11712 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11715 /* Process the initializer. */
11716 cp_finish_decl (decl,
11717 initializer, !non_constant_p,
11718 asm_specification,
11719 flags);
11721 if (pushed_scope)
11722 pop_scope (pushed_scope);
11724 return convert_from_reference (decl);
11727 /* If we didn't even get past the declarator successfully, we are
11728 definitely not looking at a declaration. */
11729 else
11730 cp_parser_abort_tentative_parse (parser);
11732 /* Otherwise, we are looking at an expression. */
11733 return cp_parser_expression (parser);
11736 /* Parses a for-statement or range-for-statement until the closing ')',
11737 not included. */
11739 static tree
11740 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11742 tree init, scope, decl;
11743 bool is_range_for;
11745 /* Begin the for-statement. */
11746 scope = begin_for_scope (&init);
11748 /* Parse the initialization. */
11749 is_range_for = cp_parser_init_statement (parser, &decl);
11751 if (is_range_for)
11752 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11753 else
11754 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11757 static tree
11758 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11759 unsigned short unroll)
11761 /* Normal for loop */
11762 tree condition = NULL_TREE;
11763 tree expression = NULL_TREE;
11764 tree stmt;
11766 stmt = begin_for_stmt (scope, init);
11767 /* The init-statement has already been parsed in
11768 cp_parser_init_statement, so no work is needed here. */
11769 finish_init_stmt (stmt);
11771 /* If there's a condition, process it. */
11772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11773 condition = cp_parser_condition (parser);
11774 else if (ivdep)
11776 cp_parser_error (parser, "missing loop condition in loop with "
11777 "%<GCC ivdep%> pragma");
11778 condition = error_mark_node;
11780 else if (unroll)
11782 cp_parser_error (parser, "missing loop condition in loop with "
11783 "%<GCC unroll%> pragma");
11784 condition = error_mark_node;
11786 finish_for_cond (condition, stmt, ivdep, unroll);
11787 /* Look for the `;'. */
11788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11790 /* If there's an expression, process it. */
11791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11792 expression = cp_parser_expression (parser);
11793 finish_for_expr (expression, stmt);
11795 return stmt;
11798 /* Tries to parse a range-based for-statement:
11800 range-based-for:
11801 decl-specifier-seq declarator : expression
11803 The decl-specifier-seq declarator and the `:' are already parsed by
11804 cp_parser_init_statement. If processing_template_decl it returns a
11805 newly created RANGE_FOR_STMT; if not, it is converted to a
11806 regular FOR_STMT. */
11808 static tree
11809 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11810 bool ivdep, unsigned short unroll)
11812 tree stmt, range_expr;
11813 auto_vec <cxx_binding *, 16> bindings;
11814 auto_vec <tree, 16> names;
11815 tree decomp_first_name = NULL_TREE;
11816 unsigned int decomp_cnt = 0;
11818 /* Get the range declaration momentarily out of the way so that
11819 the range expression doesn't clash with it. */
11820 if (range_decl != error_mark_node)
11822 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11824 tree v = DECL_VALUE_EXPR (range_decl);
11825 /* For decomposition declaration get all of the corresponding
11826 declarations out of the way. */
11827 if (TREE_CODE (v) == ARRAY_REF
11828 && VAR_P (TREE_OPERAND (v, 0))
11829 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11831 tree d = range_decl;
11832 range_decl = TREE_OPERAND (v, 0);
11833 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11834 decomp_first_name = d;
11835 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11837 tree name = DECL_NAME (d);
11838 names.safe_push (name);
11839 bindings.safe_push (IDENTIFIER_BINDING (name));
11840 IDENTIFIER_BINDING (name)
11841 = IDENTIFIER_BINDING (name)->previous;
11845 if (names.is_empty ())
11847 tree name = DECL_NAME (range_decl);
11848 names.safe_push (name);
11849 bindings.safe_push (IDENTIFIER_BINDING (name));
11850 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11856 bool expr_non_constant_p;
11857 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11859 else
11860 range_expr = cp_parser_expression (parser);
11862 /* Put the range declaration(s) back into scope. */
11863 for (unsigned int i = 0; i < names.length (); i++)
11865 cxx_binding *binding = bindings[i];
11866 binding->previous = IDENTIFIER_BINDING (names[i]);
11867 IDENTIFIER_BINDING (names[i]) = binding;
11870 /* If in template, STMT is converted to a normal for-statement
11871 at instantiation. If not, it is done just ahead. */
11872 if (processing_template_decl)
11874 if (check_for_bare_parameter_packs (range_expr))
11875 range_expr = error_mark_node;
11876 stmt = begin_range_for_stmt (scope, init);
11877 if (ivdep)
11878 RANGE_FOR_IVDEP (stmt) = 1;
11879 if (unroll)
11880 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11881 finish_range_for_decl (stmt, range_decl, range_expr);
11882 if (!type_dependent_expression_p (range_expr)
11883 /* do_auto_deduction doesn't mess with template init-lists. */
11884 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11885 do_range_for_auto_deduction (range_decl, range_expr);
11887 else
11889 stmt = begin_for_stmt (scope, init);
11890 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11891 decomp_first_name, decomp_cnt, ivdep,
11892 unroll);
11894 return stmt;
11897 /* Subroutine of cp_convert_range_for: given the initializer expression,
11898 builds up the range temporary. */
11900 static tree
11901 build_range_temp (tree range_expr)
11903 tree range_type, range_temp;
11905 /* Find out the type deduced by the declaration
11906 `auto &&__range = range_expr'. */
11907 range_type = cp_build_reference_type (make_auto (), true);
11908 range_type = do_auto_deduction (range_type, range_expr,
11909 type_uses_auto (range_type));
11911 /* Create the __range variable. */
11912 range_temp = build_decl (input_location, VAR_DECL,
11913 get_identifier ("__for_range"), range_type);
11914 TREE_USED (range_temp) = 1;
11915 DECL_ARTIFICIAL (range_temp) = 1;
11917 return range_temp;
11920 /* Used by cp_parser_range_for in template context: we aren't going to
11921 do a full conversion yet, but we still need to resolve auto in the
11922 type of the for-range-declaration if present. This is basically
11923 a shortcut version of cp_convert_range_for. */
11925 static void
11926 do_range_for_auto_deduction (tree decl, tree range_expr)
11928 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11929 if (auto_node)
11931 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11932 range_temp = convert_from_reference (build_range_temp (range_expr));
11933 iter_type = (cp_parser_perform_range_for_lookup
11934 (range_temp, &begin_dummy, &end_dummy));
11935 if (iter_type)
11937 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11938 iter_type);
11939 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11940 RO_UNARY_STAR,
11941 tf_warning_or_error);
11942 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11943 iter_decl, auto_node);
11948 /* Converts a range-based for-statement into a normal
11949 for-statement, as per the definition.
11951 for (RANGE_DECL : RANGE_EXPR)
11952 BLOCK
11954 should be equivalent to:
11957 auto &&__range = RANGE_EXPR;
11958 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11959 __begin != __end;
11960 ++__begin)
11962 RANGE_DECL = *__begin;
11963 BLOCK
11967 If RANGE_EXPR is an array:
11968 BEGIN_EXPR = __range
11969 END_EXPR = __range + ARRAY_SIZE(__range)
11970 Else if RANGE_EXPR has a member 'begin' or 'end':
11971 BEGIN_EXPR = __range.begin()
11972 END_EXPR = __range.end()
11973 Else:
11974 BEGIN_EXPR = begin(__range)
11975 END_EXPR = end(__range);
11977 If __range has a member 'begin' but not 'end', or vice versa, we must
11978 still use the second alternative (it will surely fail, however).
11979 When calling begin()/end() in the third alternative we must use
11980 argument dependent lookup, but always considering 'std' as an associated
11981 namespace. */
11983 tree
11984 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11985 tree decomp_first_name, unsigned int decomp_cnt,
11986 bool ivdep, unsigned short unroll)
11988 tree begin, end;
11989 tree iter_type, begin_expr, end_expr;
11990 tree condition, expression;
11992 range_expr = mark_lvalue_use (range_expr);
11994 if (range_decl == error_mark_node || range_expr == error_mark_node)
11995 /* If an error happened previously do nothing or else a lot of
11996 unhelpful errors would be issued. */
11997 begin_expr = end_expr = iter_type = error_mark_node;
11998 else
12000 tree range_temp;
12002 if (VAR_P (range_expr)
12003 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12004 /* Can't bind a reference to an array of runtime bound. */
12005 range_temp = range_expr;
12006 else
12008 range_temp = build_range_temp (range_expr);
12009 pushdecl (range_temp);
12010 cp_finish_decl (range_temp, range_expr,
12011 /*is_constant_init*/false, NULL_TREE,
12012 LOOKUP_ONLYCONVERTING);
12013 range_temp = convert_from_reference (range_temp);
12015 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12016 &begin_expr, &end_expr);
12019 /* The new for initialization statement. */
12020 begin = build_decl (input_location, VAR_DECL,
12021 get_identifier ("__for_begin"), iter_type);
12022 TREE_USED (begin) = 1;
12023 DECL_ARTIFICIAL (begin) = 1;
12024 pushdecl (begin);
12025 cp_finish_decl (begin, begin_expr,
12026 /*is_constant_init*/false, NULL_TREE,
12027 LOOKUP_ONLYCONVERTING);
12029 if (cxx_dialect >= cxx17)
12030 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12031 end = build_decl (input_location, VAR_DECL,
12032 get_identifier ("__for_end"), iter_type);
12033 TREE_USED (end) = 1;
12034 DECL_ARTIFICIAL (end) = 1;
12035 pushdecl (end);
12036 cp_finish_decl (end, end_expr,
12037 /*is_constant_init*/false, NULL_TREE,
12038 LOOKUP_ONLYCONVERTING);
12040 finish_init_stmt (statement);
12042 /* The new for condition. */
12043 condition = build_x_binary_op (input_location, NE_EXPR,
12044 begin, ERROR_MARK,
12045 end, ERROR_MARK,
12046 NULL, tf_warning_or_error);
12047 finish_for_cond (condition, statement, ivdep, unroll);
12049 /* The new increment expression. */
12050 expression = finish_unary_op_expr (input_location,
12051 PREINCREMENT_EXPR, begin,
12052 tf_warning_or_error);
12053 finish_for_expr (expression, statement);
12055 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12056 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12058 /* The declaration is initialized with *__begin inside the loop body. */
12059 cp_finish_decl (range_decl,
12060 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12061 tf_warning_or_error),
12062 /*is_constant_init*/false, NULL_TREE,
12063 LOOKUP_ONLYCONVERTING);
12064 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12065 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12067 return statement;
12070 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12071 We need to solve both at the same time because the method used
12072 depends on the existence of members begin or end.
12073 Returns the type deduced for the iterator expression. */
12075 static tree
12076 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12078 if (error_operand_p (range))
12080 *begin = *end = error_mark_node;
12081 return error_mark_node;
12084 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12086 error ("range-based %<for%> expression of type %qT "
12087 "has incomplete type", TREE_TYPE (range));
12088 *begin = *end = error_mark_node;
12089 return error_mark_node;
12091 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12093 /* If RANGE is an array, we will use pointer arithmetic. */
12094 *begin = decay_conversion (range, tf_warning_or_error);
12095 *end = build_binary_op (input_location, PLUS_EXPR,
12096 range,
12097 array_type_nelts_top (TREE_TYPE (range)),
12098 false);
12099 return TREE_TYPE (*begin);
12101 else
12103 /* If it is not an array, we must do a bit of magic. */
12104 tree id_begin, id_end;
12105 tree member_begin, member_end;
12107 *begin = *end = error_mark_node;
12109 id_begin = get_identifier ("begin");
12110 id_end = get_identifier ("end");
12111 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12112 /*protect=*/2, /*want_type=*/false,
12113 tf_warning_or_error);
12114 member_end = lookup_member (TREE_TYPE (range), id_end,
12115 /*protect=*/2, /*want_type=*/false,
12116 tf_warning_or_error);
12118 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12120 /* Use the member functions. */
12121 *begin = cp_parser_range_for_member_function (range, id_begin);
12122 *end = cp_parser_range_for_member_function (range, id_end);
12124 else
12126 /* Use global functions with ADL. */
12127 vec<tree, va_gc> *vec;
12128 vec = make_tree_vector ();
12130 vec_safe_push (vec, range);
12132 member_begin = perform_koenig_lookup (id_begin, vec,
12133 tf_warning_or_error);
12134 *begin = finish_call_expr (member_begin, &vec, false, true,
12135 tf_warning_or_error);
12136 member_end = perform_koenig_lookup (id_end, vec,
12137 tf_warning_or_error);
12138 *end = finish_call_expr (member_end, &vec, false, true,
12139 tf_warning_or_error);
12141 release_tree_vector (vec);
12144 /* Last common checks. */
12145 if (*begin == error_mark_node || *end == error_mark_node)
12147 /* If one of the expressions is an error do no more checks. */
12148 *begin = *end = error_mark_node;
12149 return error_mark_node;
12151 else if (type_dependent_expression_p (*begin)
12152 || type_dependent_expression_p (*end))
12153 /* Can happen, when, eg, in a template context, Koenig lookup
12154 can't resolve begin/end (c++/58503). */
12155 return NULL_TREE;
12156 else
12158 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12159 /* The unqualified type of the __begin and __end temporaries should
12160 be the same, as required by the multiple auto declaration. */
12161 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12163 if (cxx_dialect >= cxx17
12164 && (build_x_binary_op (input_location, NE_EXPR,
12165 *begin, ERROR_MARK,
12166 *end, ERROR_MARK,
12167 NULL, tf_none)
12168 != error_mark_node))
12169 /* P0184R0 allows __begin and __end to have different types,
12170 but make sure they are comparable so we can give a better
12171 diagnostic. */;
12172 else
12173 error ("inconsistent begin/end types in range-based %<for%> "
12174 "statement: %qT and %qT",
12175 TREE_TYPE (*begin), TREE_TYPE (*end));
12177 return iter_type;
12182 /* Helper function for cp_parser_perform_range_for_lookup.
12183 Builds a tree for RANGE.IDENTIFIER(). */
12185 static tree
12186 cp_parser_range_for_member_function (tree range, tree identifier)
12188 tree member, res;
12189 vec<tree, va_gc> *vec;
12191 member = finish_class_member_access_expr (range, identifier,
12192 false, tf_warning_or_error);
12193 if (member == error_mark_node)
12194 return error_mark_node;
12196 vec = make_tree_vector ();
12197 res = finish_call_expr (member, &vec,
12198 /*disallow_virtual=*/false,
12199 /*koenig_p=*/false,
12200 tf_warning_or_error);
12201 release_tree_vector (vec);
12202 return res;
12205 /* Parse an iteration-statement.
12207 iteration-statement:
12208 while ( condition ) statement
12209 do statement while ( expression ) ;
12210 for ( init-statement condition [opt] ; expression [opt] )
12211 statement
12213 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12215 static tree
12216 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12217 unsigned short unroll)
12219 cp_token *token;
12220 enum rid keyword;
12221 tree statement;
12222 unsigned char in_statement;
12223 token_indent_info guard_tinfo;
12225 /* Peek at the next token. */
12226 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12227 if (!token)
12228 return error_mark_node;
12230 guard_tinfo = get_token_indent_info (token);
12232 /* Remember whether or not we are already within an iteration
12233 statement. */
12234 in_statement = parser->in_statement;
12236 /* See what kind of keyword it is. */
12237 keyword = token->keyword;
12238 switch (keyword)
12240 case RID_WHILE:
12242 tree condition;
12244 /* Begin the while-statement. */
12245 statement = begin_while_stmt ();
12246 /* Look for the `('. */
12247 matching_parens parens;
12248 parens.require_open (parser);
12249 /* Parse the condition. */
12250 condition = cp_parser_condition (parser);
12251 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12252 /* Look for the `)'. */
12253 parens.require_close (parser);
12254 /* Parse the dependent statement. */
12255 parser->in_statement = IN_ITERATION_STMT;
12256 bool prev = note_iteration_stmt_body_start ();
12257 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12258 note_iteration_stmt_body_end (prev);
12259 parser->in_statement = in_statement;
12260 /* We're done with the while-statement. */
12261 finish_while_stmt (statement);
12263 break;
12265 case RID_DO:
12267 tree expression;
12269 /* Begin the do-statement. */
12270 statement = begin_do_stmt ();
12271 /* Parse the body of the do-statement. */
12272 parser->in_statement = IN_ITERATION_STMT;
12273 bool prev = note_iteration_stmt_body_start ();
12274 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12275 note_iteration_stmt_body_end (prev);
12276 parser->in_statement = in_statement;
12277 finish_do_body (statement);
12278 /* Look for the `while' keyword. */
12279 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12280 /* Look for the `('. */
12281 matching_parens parens;
12282 parens.require_open (parser);
12283 /* Parse the expression. */
12284 expression = cp_parser_expression (parser);
12285 /* We're done with the do-statement. */
12286 finish_do_stmt (expression, statement, ivdep, unroll);
12287 /* Look for the `)'. */
12288 parens.require_close (parser);
12289 /* Look for the `;'. */
12290 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12292 break;
12294 case RID_FOR:
12296 /* Look for the `('. */
12297 matching_parens parens;
12298 parens.require_open (parser);
12300 statement = cp_parser_for (parser, ivdep, unroll);
12302 /* Look for the `)'. */
12303 parens.require_close (parser);
12305 /* Parse the body of the for-statement. */
12306 parser->in_statement = IN_ITERATION_STMT;
12307 bool prev = note_iteration_stmt_body_start ();
12308 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12309 note_iteration_stmt_body_end (prev);
12310 parser->in_statement = in_statement;
12312 /* We're done with the for-statement. */
12313 finish_for_stmt (statement);
12315 break;
12317 default:
12318 cp_parser_error (parser, "expected iteration-statement");
12319 statement = error_mark_node;
12320 break;
12323 return statement;
12326 /* Parse a init-statement or the declarator of a range-based-for.
12327 Returns true if a range-based-for declaration is seen.
12329 init-statement:
12330 expression-statement
12331 simple-declaration */
12333 static bool
12334 cp_parser_init_statement (cp_parser *parser, tree *decl)
12336 /* If the next token is a `;', then we have an empty
12337 expression-statement. Grammatically, this is also a
12338 simple-declaration, but an invalid one, because it does not
12339 declare anything. Therefore, if we did not handle this case
12340 specially, we would issue an error message about an invalid
12341 declaration. */
12342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12344 bool is_range_for = false;
12345 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12347 /* Try to parse the init-statement. */
12348 if (cp_parser_range_based_for_with_init_p (parser))
12350 tree dummy;
12351 cp_parser_parse_tentatively (parser);
12352 /* Parse the declaration. */
12353 cp_parser_simple_declaration (parser,
12354 /*function_definition_allowed_p=*/false,
12355 &dummy);
12356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12357 if (!cp_parser_parse_definitely (parser))
12358 /* That didn't work, try to parse it as an expression-statement. */
12359 cp_parser_expression_statement (parser, NULL_TREE);
12361 if (cxx_dialect < cxx2a)
12363 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12364 "range-based %<for%> loops with initializer only "
12365 "available with -std=c++2a or -std=gnu++2a");
12366 *decl = error_mark_node;
12370 /* A colon is used in range-based for. */
12371 parser->colon_corrects_to_scope_p = false;
12373 /* We're going to speculatively look for a declaration, falling back
12374 to an expression, if necessary. */
12375 cp_parser_parse_tentatively (parser);
12376 /* Parse the declaration. */
12377 cp_parser_simple_declaration (parser,
12378 /*function_definition_allowed_p=*/false,
12379 decl);
12380 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12381 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12383 /* It is a range-for, consume the ':'. */
12384 cp_lexer_consume_token (parser->lexer);
12385 is_range_for = true;
12386 if (cxx_dialect < cxx11)
12388 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12389 "range-based %<for%> loops only available with "
12390 "-std=c++11 or -std=gnu++11");
12391 *decl = error_mark_node;
12394 else
12395 /* The ';' is not consumed yet because we told
12396 cp_parser_simple_declaration not to. */
12397 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12399 if (cp_parser_parse_definitely (parser))
12400 return is_range_for;
12401 /* If the tentative parse failed, then we shall need to look for an
12402 expression-statement. */
12404 /* If we are here, it is an expression-statement. */
12405 cp_parser_expression_statement (parser, NULL_TREE);
12406 return false;
12409 /* Parse a jump-statement.
12411 jump-statement:
12412 break ;
12413 continue ;
12414 return expression [opt] ;
12415 return braced-init-list ;
12416 goto identifier ;
12418 GNU extension:
12420 jump-statement:
12421 goto * expression ;
12423 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12425 static tree
12426 cp_parser_jump_statement (cp_parser* parser)
12428 tree statement = error_mark_node;
12429 cp_token *token;
12430 enum rid keyword;
12431 unsigned char in_statement;
12433 /* Peek at the next token. */
12434 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12435 if (!token)
12436 return error_mark_node;
12438 /* See what kind of keyword it is. */
12439 keyword = token->keyword;
12440 switch (keyword)
12442 case RID_BREAK:
12443 in_statement = parser->in_statement & ~IN_IF_STMT;
12444 switch (in_statement)
12446 case 0:
12447 error_at (token->location, "break statement not within loop or switch");
12448 break;
12449 default:
12450 gcc_assert ((in_statement & IN_SWITCH_STMT)
12451 || in_statement == IN_ITERATION_STMT);
12452 statement = finish_break_stmt ();
12453 if (in_statement == IN_ITERATION_STMT)
12454 break_maybe_infinite_loop ();
12455 break;
12456 case IN_OMP_BLOCK:
12457 error_at (token->location, "invalid exit from OpenMP structured block");
12458 break;
12459 case IN_OMP_FOR:
12460 error_at (token->location, "break statement used with OpenMP for loop");
12461 break;
12463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12464 break;
12466 case RID_CONTINUE:
12467 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12469 case 0:
12470 error_at (token->location, "continue statement not within a loop");
12471 break;
12472 /* Fall through. */
12473 case IN_ITERATION_STMT:
12474 case IN_OMP_FOR:
12475 statement = finish_continue_stmt ();
12476 break;
12477 case IN_OMP_BLOCK:
12478 error_at (token->location, "invalid exit from OpenMP structured block");
12479 break;
12480 default:
12481 gcc_unreachable ();
12483 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12484 break;
12486 case RID_RETURN:
12488 tree expr;
12489 bool expr_non_constant_p;
12491 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12493 cp_lexer_set_source_position (parser->lexer);
12494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12495 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12497 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12498 expr = cp_parser_expression (parser);
12499 else
12500 /* If the next token is a `;', then there is no
12501 expression. */
12502 expr = NULL_TREE;
12503 /* Build the return-statement. */
12504 if (current_function_auto_return_pattern && in_discarded_stmt)
12505 /* Don't deduce from a discarded return statement. */;
12506 else
12507 statement = finish_return_stmt (expr);
12508 /* Look for the final `;'. */
12509 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12511 break;
12513 case RID_GOTO:
12514 if (parser->in_function_body
12515 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12517 error ("%<goto%> in %<constexpr%> function");
12518 cp_function_chain->invalid_constexpr = true;
12521 /* Create the goto-statement. */
12522 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12524 /* Issue a warning about this use of a GNU extension. */
12525 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12526 /* Consume the '*' token. */
12527 cp_lexer_consume_token (parser->lexer);
12528 /* Parse the dependent expression. */
12529 finish_goto_stmt (cp_parser_expression (parser));
12531 else
12532 finish_goto_stmt (cp_parser_identifier (parser));
12533 /* Look for the final `;'. */
12534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12535 break;
12537 default:
12538 cp_parser_error (parser, "expected jump-statement");
12539 break;
12542 return statement;
12545 /* Parse a declaration-statement.
12547 declaration-statement:
12548 block-declaration */
12550 static void
12551 cp_parser_declaration_statement (cp_parser* parser)
12553 void *p;
12555 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12556 p = obstack_alloc (&declarator_obstack, 0);
12558 /* Parse the block-declaration. */
12559 cp_parser_block_declaration (parser, /*statement_p=*/true);
12561 /* Free any declarators allocated. */
12562 obstack_free (&declarator_obstack, p);
12565 /* Some dependent statements (like `if (cond) statement'), are
12566 implicitly in their own scope. In other words, if the statement is
12567 a single statement (as opposed to a compound-statement), it is
12568 none-the-less treated as if it were enclosed in braces. Any
12569 declarations appearing in the dependent statement are out of scope
12570 after control passes that point. This function parses a statement,
12571 but ensures that is in its own scope, even if it is not a
12572 compound-statement.
12574 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12575 is a (possibly labeled) if statement which is not enclosed in
12576 braces and has an else clause. This is used to implement
12577 -Wparentheses.
12579 CHAIN is a vector of if-else-if conditions. This is used to implement
12580 -Wduplicated-cond.
12582 Returns the new statement. */
12584 static tree
12585 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12586 const token_indent_info &guard_tinfo,
12587 vec<tree> *chain)
12589 tree statement;
12590 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12591 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12592 token_indent_info body_tinfo
12593 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12595 if (if_p != NULL)
12596 *if_p = false;
12598 /* Mark if () ; with a special NOP_EXPR. */
12599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12601 cp_lexer_consume_token (parser->lexer);
12602 statement = add_stmt (build_empty_stmt (body_loc));
12604 if (guard_tinfo.keyword == RID_IF
12605 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12606 warning_at (body_loc, OPT_Wempty_body,
12607 "suggest braces around empty body in an %<if%> statement");
12608 else if (guard_tinfo.keyword == RID_ELSE)
12609 warning_at (body_loc, OPT_Wempty_body,
12610 "suggest braces around empty body in an %<else%> statement");
12612 /* if a compound is opened, we simply parse the statement directly. */
12613 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12614 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12615 /* If the token is not a `{', then we must take special action. */
12616 else
12618 /* Create a compound-statement. */
12619 statement = begin_compound_stmt (0);
12620 /* Parse the dependent-statement. */
12621 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12622 &body_loc_after_labels);
12623 /* Finish the dummy compound-statement. */
12624 finish_compound_stmt (statement);
12627 token_indent_info next_tinfo
12628 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12629 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12631 if (body_loc_after_labels != UNKNOWN_LOCATION
12632 && next_tinfo.type != CPP_SEMICOLON)
12633 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12634 guard_tinfo.location, guard_tinfo.keyword);
12636 /* Return the statement. */
12637 return statement;
12640 /* For some dependent statements (like `while (cond) statement'), we
12641 have already created a scope. Therefore, even if the dependent
12642 statement is a compound-statement, we do not want to create another
12643 scope. */
12645 static void
12646 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12647 const token_indent_info &guard_tinfo)
12649 /* If the token is a `{', then we must take special action. */
12650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12652 token_indent_info body_tinfo
12653 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12654 location_t loc_after_labels = UNKNOWN_LOCATION;
12656 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12657 &loc_after_labels);
12658 token_indent_info next_tinfo
12659 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12660 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12662 if (loc_after_labels != UNKNOWN_LOCATION
12663 && next_tinfo.type != CPP_SEMICOLON)
12664 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12665 guard_tinfo.location,
12666 guard_tinfo.keyword);
12668 else
12670 /* Avoid calling cp_parser_compound_statement, so that we
12671 don't create a new scope. Do everything else by hand. */
12672 matching_braces braces;
12673 braces.require_open (parser);
12674 /* If the next keyword is `__label__' we have a label declaration. */
12675 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12676 cp_parser_label_declaration (parser);
12677 /* Parse an (optional) statement-seq. */
12678 cp_parser_statement_seq_opt (parser, NULL_TREE);
12679 braces.require_close (parser);
12683 /* Declarations [gram.dcl.dcl] */
12685 /* Parse an optional declaration-sequence.
12687 declaration-seq:
12688 declaration
12689 declaration-seq declaration */
12691 static void
12692 cp_parser_declaration_seq_opt (cp_parser* parser)
12694 while (true)
12696 cp_token *token;
12698 token = cp_lexer_peek_token (parser->lexer);
12700 if (token->type == CPP_CLOSE_BRACE
12701 || token->type == CPP_EOF
12702 || token->type == CPP_PRAGMA_EOL)
12703 break;
12705 if (token->type == CPP_SEMICOLON)
12707 /* A declaration consisting of a single semicolon is
12708 invalid. Allow it unless we're being pedantic. */
12709 cp_lexer_consume_token (parser->lexer);
12710 if (!in_system_header_at (input_location))
12711 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12712 continue;
12715 /* If we're entering or exiting a region that's implicitly
12716 extern "C", modify the lang context appropriately. */
12717 if (!parser->implicit_extern_c && token->implicit_extern_c)
12719 push_lang_context (lang_name_c);
12720 parser->implicit_extern_c = true;
12722 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12724 pop_lang_context ();
12725 parser->implicit_extern_c = false;
12728 if (token->type == CPP_PRAGMA)
12730 /* A top-level declaration can consist solely of a #pragma.
12731 A nested declaration cannot, so this is done here and not
12732 in cp_parser_declaration. (A #pragma at block scope is
12733 handled in cp_parser_statement.) */
12734 cp_parser_pragma (parser, pragma_external, NULL);
12735 continue;
12738 /* Parse the declaration itself. */
12739 cp_parser_declaration (parser);
12743 /* Parse a declaration.
12745 declaration:
12746 block-declaration
12747 function-definition
12748 template-declaration
12749 explicit-instantiation
12750 explicit-specialization
12751 linkage-specification
12752 namespace-definition
12754 C++17:
12755 deduction-guide
12757 GNU extension:
12759 declaration:
12760 __extension__ declaration */
12762 static void
12763 cp_parser_declaration (cp_parser* parser)
12765 cp_token token1;
12766 cp_token token2;
12767 int saved_pedantic;
12768 void *p;
12769 tree attributes = NULL_TREE;
12771 /* Check for the `__extension__' keyword. */
12772 if (cp_parser_extension_opt (parser, &saved_pedantic))
12774 /* Parse the qualified declaration. */
12775 cp_parser_declaration (parser);
12776 /* Restore the PEDANTIC flag. */
12777 pedantic = saved_pedantic;
12779 return;
12782 /* Try to figure out what kind of declaration is present. */
12783 token1 = *cp_lexer_peek_token (parser->lexer);
12785 if (token1.type != CPP_EOF)
12786 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12787 else
12789 token2.type = CPP_EOF;
12790 token2.keyword = RID_MAX;
12793 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12794 p = obstack_alloc (&declarator_obstack, 0);
12796 /* If the next token is `extern' and the following token is a string
12797 literal, then we have a linkage specification. */
12798 if (token1.keyword == RID_EXTERN
12799 && cp_parser_is_pure_string_literal (&token2))
12800 cp_parser_linkage_specification (parser);
12801 /* If the next token is `template', then we have either a template
12802 declaration, an explicit instantiation, or an explicit
12803 specialization. */
12804 else if (token1.keyword == RID_TEMPLATE)
12806 /* `template <>' indicates a template specialization. */
12807 if (token2.type == CPP_LESS
12808 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12809 cp_parser_explicit_specialization (parser);
12810 /* `template <' indicates a template declaration. */
12811 else if (token2.type == CPP_LESS)
12812 cp_parser_template_declaration (parser, /*member_p=*/false);
12813 /* Anything else must be an explicit instantiation. */
12814 else
12815 cp_parser_explicit_instantiation (parser);
12817 /* If the next token is `export', then we have a template
12818 declaration. */
12819 else if (token1.keyword == RID_EXPORT)
12820 cp_parser_template_declaration (parser, /*member_p=*/false);
12821 /* If the next token is `extern', 'static' or 'inline' and the one
12822 after that is `template', we have a GNU extended explicit
12823 instantiation directive. */
12824 else if (cp_parser_allow_gnu_extensions_p (parser)
12825 && (token1.keyword == RID_EXTERN
12826 || token1.keyword == RID_STATIC
12827 || token1.keyword == RID_INLINE)
12828 && token2.keyword == RID_TEMPLATE)
12829 cp_parser_explicit_instantiation (parser);
12830 /* If the next token is `namespace', check for a named or unnamed
12831 namespace definition. */
12832 else if (token1.keyword == RID_NAMESPACE
12833 && (/* A named namespace definition. */
12834 (token2.type == CPP_NAME
12835 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12836 != CPP_EQ))
12837 || (token2.type == CPP_OPEN_SQUARE
12838 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12839 == CPP_OPEN_SQUARE)
12840 /* An unnamed namespace definition. */
12841 || token2.type == CPP_OPEN_BRACE
12842 || token2.keyword == RID_ATTRIBUTE))
12843 cp_parser_namespace_definition (parser);
12844 /* An inline (associated) namespace definition. */
12845 else if (token1.keyword == RID_INLINE
12846 && token2.keyword == RID_NAMESPACE)
12847 cp_parser_namespace_definition (parser);
12848 /* Objective-C++ declaration/definition. */
12849 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12850 cp_parser_objc_declaration (parser, NULL_TREE);
12851 else if (c_dialect_objc ()
12852 && token1.keyword == RID_ATTRIBUTE
12853 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12854 cp_parser_objc_declaration (parser, attributes);
12855 /* At this point we may have a template declared by a concept
12856 introduction. */
12857 else if (flag_concepts
12858 && cp_parser_template_declaration_after_export (parser,
12859 /*member_p=*/false))
12860 /* We did. */;
12861 else
12862 /* Try to parse a block-declaration, or a function-definition. */
12863 cp_parser_block_declaration (parser, /*statement_p=*/false);
12865 /* Free any declarators allocated. */
12866 obstack_free (&declarator_obstack, p);
12869 /* Parse a block-declaration.
12871 block-declaration:
12872 simple-declaration
12873 asm-definition
12874 namespace-alias-definition
12875 using-declaration
12876 using-directive
12878 GNU Extension:
12880 block-declaration:
12881 __extension__ block-declaration
12883 C++0x Extension:
12885 block-declaration:
12886 static_assert-declaration
12888 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12889 part of a declaration-statement. */
12891 static void
12892 cp_parser_block_declaration (cp_parser *parser,
12893 bool statement_p)
12895 cp_token *token1;
12896 int saved_pedantic;
12898 /* Check for the `__extension__' keyword. */
12899 if (cp_parser_extension_opt (parser, &saved_pedantic))
12901 /* Parse the qualified declaration. */
12902 cp_parser_block_declaration (parser, statement_p);
12903 /* Restore the PEDANTIC flag. */
12904 pedantic = saved_pedantic;
12906 return;
12909 /* Peek at the next token to figure out which kind of declaration is
12910 present. */
12911 token1 = cp_lexer_peek_token (parser->lexer);
12913 /* If the next keyword is `asm', we have an asm-definition. */
12914 if (token1->keyword == RID_ASM)
12916 if (statement_p)
12917 cp_parser_commit_to_tentative_parse (parser);
12918 cp_parser_asm_definition (parser);
12920 /* If the next keyword is `namespace', we have a
12921 namespace-alias-definition. */
12922 else if (token1->keyword == RID_NAMESPACE)
12923 cp_parser_namespace_alias_definition (parser);
12924 /* If the next keyword is `using', we have a
12925 using-declaration, a using-directive, or an alias-declaration. */
12926 else if (token1->keyword == RID_USING)
12928 cp_token *token2;
12930 if (statement_p)
12931 cp_parser_commit_to_tentative_parse (parser);
12932 /* If the token after `using' is `namespace', then we have a
12933 using-directive. */
12934 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12935 if (token2->keyword == RID_NAMESPACE)
12936 cp_parser_using_directive (parser);
12937 /* If the second token after 'using' is '=', then we have an
12938 alias-declaration. */
12939 else if (cxx_dialect >= cxx11
12940 && token2->type == CPP_NAME
12941 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12942 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12943 cp_parser_alias_declaration (parser);
12944 /* Otherwise, it's a using-declaration. */
12945 else
12946 cp_parser_using_declaration (parser,
12947 /*access_declaration_p=*/false);
12949 /* If the next keyword is `__label__' we have a misplaced label
12950 declaration. */
12951 else if (token1->keyword == RID_LABEL)
12953 cp_lexer_consume_token (parser->lexer);
12954 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12955 cp_parser_skip_to_end_of_statement (parser);
12956 /* If the next token is now a `;', consume it. */
12957 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12958 cp_lexer_consume_token (parser->lexer);
12960 /* If the next token is `static_assert' we have a static assertion. */
12961 else if (token1->keyword == RID_STATIC_ASSERT)
12962 cp_parser_static_assert (parser, /*member_p=*/false);
12963 /* Anything else must be a simple-declaration. */
12964 else
12965 cp_parser_simple_declaration (parser, !statement_p,
12966 /*maybe_range_for_decl*/NULL);
12969 /* Parse a simple-declaration.
12971 simple-declaration:
12972 decl-specifier-seq [opt] init-declarator-list [opt] ;
12973 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12974 brace-or-equal-initializer ;
12976 init-declarator-list:
12977 init-declarator
12978 init-declarator-list , init-declarator
12980 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12981 function-definition as a simple-declaration.
12983 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12984 parsed declaration if it is an uninitialized single declarator not followed
12985 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12986 if present, will not be consumed. */
12988 static void
12989 cp_parser_simple_declaration (cp_parser* parser,
12990 bool function_definition_allowed_p,
12991 tree *maybe_range_for_decl)
12993 cp_decl_specifier_seq decl_specifiers;
12994 int declares_class_or_enum;
12995 bool saw_declarator;
12996 location_t comma_loc = UNKNOWN_LOCATION;
12997 location_t init_loc = UNKNOWN_LOCATION;
12999 if (maybe_range_for_decl)
13000 *maybe_range_for_decl = NULL_TREE;
13002 /* Defer access checks until we know what is being declared; the
13003 checks for names appearing in the decl-specifier-seq should be
13004 done as if we were in the scope of the thing being declared. */
13005 push_deferring_access_checks (dk_deferred);
13007 /* Parse the decl-specifier-seq. We have to keep track of whether
13008 or not the decl-specifier-seq declares a named class or
13009 enumeration type, since that is the only case in which the
13010 init-declarator-list is allowed to be empty.
13012 [dcl.dcl]
13014 In a simple-declaration, the optional init-declarator-list can be
13015 omitted only when declaring a class or enumeration, that is when
13016 the decl-specifier-seq contains either a class-specifier, an
13017 elaborated-type-specifier, or an enum-specifier. */
13018 cp_parser_decl_specifier_seq (parser,
13019 CP_PARSER_FLAGS_OPTIONAL,
13020 &decl_specifiers,
13021 &declares_class_or_enum);
13022 /* We no longer need to defer access checks. */
13023 stop_deferring_access_checks ();
13025 /* In a block scope, a valid declaration must always have a
13026 decl-specifier-seq. By not trying to parse declarators, we can
13027 resolve the declaration/expression ambiguity more quickly. */
13028 if (!function_definition_allowed_p
13029 && !decl_specifiers.any_specifiers_p)
13031 cp_parser_error (parser, "expected declaration");
13032 goto done;
13035 /* If the next two tokens are both identifiers, the code is
13036 erroneous. The usual cause of this situation is code like:
13038 T t;
13040 where "T" should name a type -- but does not. */
13041 if (!decl_specifiers.any_type_specifiers_p
13042 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13044 /* If parsing tentatively, we should commit; we really are
13045 looking at a declaration. */
13046 cp_parser_commit_to_tentative_parse (parser);
13047 /* Give up. */
13048 goto done;
13051 cp_parser_maybe_commit_to_declaration (parser,
13052 decl_specifiers.any_specifiers_p);
13054 /* Look for C++17 decomposition declaration. */
13055 for (size_t n = 1; ; n++)
13056 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13057 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13058 continue;
13059 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13060 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13061 && decl_specifiers.any_specifiers_p)
13063 tree decl
13064 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13065 maybe_range_for_decl,
13066 &init_loc);
13068 /* The next token should be either a `,' or a `;'. */
13069 cp_token *token = cp_lexer_peek_token (parser->lexer);
13070 /* If it's a `;', we are done. */
13071 if (token->type == CPP_SEMICOLON)
13072 goto finish;
13073 else if (maybe_range_for_decl)
13075 if (*maybe_range_for_decl == NULL_TREE)
13076 *maybe_range_for_decl = error_mark_node;
13077 goto finish;
13079 /* Anything else is an error. */
13080 else
13082 /* If we have already issued an error message we don't need
13083 to issue another one. */
13084 if ((decl != error_mark_node
13085 && DECL_INITIAL (decl) != error_mark_node)
13086 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13087 cp_parser_error (parser, "expected %<,%> or %<;%>");
13088 /* Skip tokens until we reach the end of the statement. */
13089 cp_parser_skip_to_end_of_statement (parser);
13090 /* If the next token is now a `;', consume it. */
13091 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13092 cp_lexer_consume_token (parser->lexer);
13093 goto done;
13096 else
13097 break;
13099 tree last_type;
13100 bool auto_specifier_p;
13101 /* NULL_TREE if both variable and function declaration are allowed,
13102 error_mark_node if function declaration are not allowed and
13103 a FUNCTION_DECL that should be diagnosed if it is followed by
13104 variable declarations. */
13105 tree auto_function_declaration;
13107 last_type = NULL_TREE;
13108 auto_specifier_p
13109 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13110 auto_function_declaration = NULL_TREE;
13112 /* Keep going until we hit the `;' at the end of the simple
13113 declaration. */
13114 saw_declarator = false;
13115 while (cp_lexer_next_token_is_not (parser->lexer,
13116 CPP_SEMICOLON))
13118 cp_token *token;
13119 bool function_definition_p;
13120 tree decl;
13121 tree auto_result = NULL_TREE;
13123 if (saw_declarator)
13125 /* If we are processing next declarator, comma is expected */
13126 token = cp_lexer_peek_token (parser->lexer);
13127 gcc_assert (token->type == CPP_COMMA);
13128 cp_lexer_consume_token (parser->lexer);
13129 if (maybe_range_for_decl)
13131 *maybe_range_for_decl = error_mark_node;
13132 if (comma_loc == UNKNOWN_LOCATION)
13133 comma_loc = token->location;
13136 else
13137 saw_declarator = true;
13139 /* Parse the init-declarator. */
13140 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13141 /*checks=*/NULL,
13142 function_definition_allowed_p,
13143 /*member_p=*/false,
13144 declares_class_or_enum,
13145 &function_definition_p,
13146 maybe_range_for_decl,
13147 &init_loc,
13148 &auto_result);
13149 /* If an error occurred while parsing tentatively, exit quickly.
13150 (That usually happens when in the body of a function; each
13151 statement is treated as a declaration-statement until proven
13152 otherwise.) */
13153 if (cp_parser_error_occurred (parser))
13154 goto done;
13156 if (auto_specifier_p && cxx_dialect >= cxx14)
13158 /* If the init-declarator-list contains more than one
13159 init-declarator, they shall all form declarations of
13160 variables. */
13161 if (auto_function_declaration == NULL_TREE)
13162 auto_function_declaration
13163 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13164 else if (TREE_CODE (decl) == FUNCTION_DECL
13165 || auto_function_declaration != error_mark_node)
13167 error_at (decl_specifiers.locations[ds_type_spec],
13168 "non-variable %qD in declaration with more than one "
13169 "declarator with placeholder type",
13170 TREE_CODE (decl) == FUNCTION_DECL
13171 ? decl : auto_function_declaration);
13172 auto_function_declaration = error_mark_node;
13176 if (auto_result
13177 && (!processing_template_decl || !type_uses_auto (auto_result)))
13179 if (last_type
13180 && last_type != error_mark_node
13181 && !same_type_p (auto_result, last_type))
13183 /* If the list of declarators contains more than one declarator,
13184 the type of each declared variable is determined as described
13185 above. If the type deduced for the template parameter U is not
13186 the same in each deduction, the program is ill-formed. */
13187 error_at (decl_specifiers.locations[ds_type_spec],
13188 "inconsistent deduction for %qT: %qT and then %qT",
13189 decl_specifiers.type, last_type, auto_result);
13190 last_type = error_mark_node;
13192 else
13193 last_type = auto_result;
13196 /* Handle function definitions specially. */
13197 if (function_definition_p)
13199 /* If the next token is a `,', then we are probably
13200 processing something like:
13202 void f() {}, *p;
13204 which is erroneous. */
13205 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13207 cp_token *token = cp_lexer_peek_token (parser->lexer);
13208 error_at (token->location,
13209 "mixing"
13210 " declarations and function-definitions is forbidden");
13212 /* Otherwise, we're done with the list of declarators. */
13213 else
13215 pop_deferring_access_checks ();
13216 return;
13219 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13220 *maybe_range_for_decl = decl;
13221 /* The next token should be either a `,' or a `;'. */
13222 token = cp_lexer_peek_token (parser->lexer);
13223 /* If it's a `,', there are more declarators to come. */
13224 if (token->type == CPP_COMMA)
13225 /* will be consumed next time around */;
13226 /* If it's a `;', we are done. */
13227 else if (token->type == CPP_SEMICOLON)
13228 break;
13229 else if (maybe_range_for_decl)
13231 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13232 permerror (decl_specifiers.locations[ds_type_spec],
13233 "types may not be defined in a for-range-declaration");
13234 break;
13236 /* Anything else is an error. */
13237 else
13239 /* If we have already issued an error message we don't need
13240 to issue another one. */
13241 if ((decl != error_mark_node
13242 && DECL_INITIAL (decl) != error_mark_node)
13243 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13244 cp_parser_error (parser, "expected %<,%> or %<;%>");
13245 /* Skip tokens until we reach the end of the statement. */
13246 cp_parser_skip_to_end_of_statement (parser);
13247 /* If the next token is now a `;', consume it. */
13248 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13249 cp_lexer_consume_token (parser->lexer);
13250 goto done;
13252 /* After the first time around, a function-definition is not
13253 allowed -- even if it was OK at first. For example:
13255 int i, f() {}
13257 is not valid. */
13258 function_definition_allowed_p = false;
13261 /* Issue an error message if no declarators are present, and the
13262 decl-specifier-seq does not itself declare a class or
13263 enumeration: [dcl.dcl]/3. */
13264 if (!saw_declarator)
13266 if (cp_parser_declares_only_class_p (parser))
13268 if (!declares_class_or_enum
13269 && decl_specifiers.type
13270 && OVERLOAD_TYPE_P (decl_specifiers.type))
13271 /* Ensure an error is issued anyway when finish_decltype_type,
13272 called via cp_parser_decl_specifier_seq, returns a class or
13273 an enumeration (c++/51786). */
13274 decl_specifiers.type = NULL_TREE;
13275 shadow_tag (&decl_specifiers);
13277 /* Perform any deferred access checks. */
13278 perform_deferred_access_checks (tf_warning_or_error);
13281 /* Consume the `;'. */
13282 finish:
13283 if (!maybe_range_for_decl)
13284 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13285 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13287 if (init_loc != UNKNOWN_LOCATION)
13288 error_at (init_loc, "initializer in range-based %<for%> loop");
13289 if (comma_loc != UNKNOWN_LOCATION)
13290 error_at (comma_loc,
13291 "multiple declarations in range-based %<for%> loop");
13294 done:
13295 pop_deferring_access_checks ();
13298 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13299 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13300 initializer ; */
13302 static tree
13303 cp_parser_decomposition_declaration (cp_parser *parser,
13304 cp_decl_specifier_seq *decl_specifiers,
13305 tree *maybe_range_for_decl,
13306 location_t *init_loc)
13308 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13309 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13310 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13312 /* Parse the identifier-list. */
13313 auto_vec<cp_expr, 10> v;
13314 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13315 while (true)
13317 cp_expr e = cp_parser_identifier (parser);
13318 if (e.get_value () == error_mark_node)
13319 break;
13320 v.safe_push (e);
13321 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13322 break;
13323 cp_lexer_consume_token (parser->lexer);
13326 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13327 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13329 end_loc = UNKNOWN_LOCATION;
13330 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13331 false);
13332 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13333 cp_lexer_consume_token (parser->lexer);
13334 else
13336 cp_parser_skip_to_end_of_statement (parser);
13337 return error_mark_node;
13341 if (cxx_dialect < cxx17)
13342 pedwarn (loc, 0, "structured bindings only available with "
13343 "-std=c++17 or -std=gnu++17");
13345 tree pushed_scope;
13346 cp_declarator *declarator = make_declarator (cdk_decomp);
13347 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13348 declarator->id_loc = loc;
13349 if (ref_qual != REF_QUAL_NONE)
13350 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13351 ref_qual == REF_QUAL_RVALUE,
13352 NULL_TREE);
13353 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13354 NULL_TREE, decl_specifiers->attributes,
13355 &pushed_scope);
13356 tree orig_decl = decl;
13358 unsigned int i;
13359 cp_expr e;
13360 cp_decl_specifier_seq decl_specs;
13361 clear_decl_specs (&decl_specs);
13362 decl_specs.type = make_auto ();
13363 tree prev = decl;
13364 FOR_EACH_VEC_ELT (v, i, e)
13366 if (i == 0)
13367 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13368 else
13369 declarator->u.id.unqualified_name = e.get_value ();
13370 declarator->id_loc = e.get_location ();
13371 tree elt_pushed_scope;
13372 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13373 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13374 if (decl2 == error_mark_node)
13375 decl = error_mark_node;
13376 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13378 /* Ensure we've diagnosed redeclaration if we aren't creating
13379 a new VAR_DECL. */
13380 gcc_assert (errorcount);
13381 decl = error_mark_node;
13383 else
13384 prev = decl2;
13385 if (elt_pushed_scope)
13386 pop_scope (elt_pushed_scope);
13389 if (v.is_empty ())
13391 error_at (loc, "empty structured binding declaration");
13392 decl = error_mark_node;
13395 if (maybe_range_for_decl == NULL
13396 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13398 bool non_constant_p = false, is_direct_init = false;
13399 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13400 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13401 &non_constant_p);
13402 if (initializer == NULL_TREE
13403 || (TREE_CODE (initializer) == TREE_LIST
13404 && TREE_CHAIN (initializer))
13405 || (is_direct_init
13406 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13407 && CONSTRUCTOR_NELTS (initializer) != 1))
13409 error_at (loc, "invalid initializer for structured binding "
13410 "declaration");
13411 initializer = error_mark_node;
13414 if (decl != error_mark_node)
13416 cp_maybe_mangle_decomp (decl, prev, v.length ());
13417 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13418 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13419 cp_finish_decomp (decl, prev, v.length ());
13422 else if (decl != error_mark_node)
13424 *maybe_range_for_decl = prev;
13425 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13426 the underlying DECL. */
13427 cp_finish_decomp (decl, prev, v.length ());
13430 if (pushed_scope)
13431 pop_scope (pushed_scope);
13433 if (decl == error_mark_node && DECL_P (orig_decl))
13435 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13436 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13439 return decl;
13442 /* Parse a decl-specifier-seq.
13444 decl-specifier-seq:
13445 decl-specifier-seq [opt] decl-specifier
13446 decl-specifier attribute-specifier-seq [opt] (C++11)
13448 decl-specifier:
13449 storage-class-specifier
13450 type-specifier
13451 function-specifier
13452 friend
13453 typedef
13455 GNU Extension:
13457 decl-specifier:
13458 attributes
13460 Concepts Extension:
13462 decl-specifier:
13463 concept
13465 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13467 The parser flags FLAGS is used to control type-specifier parsing.
13469 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13470 flags:
13472 1: one of the decl-specifiers is an elaborated-type-specifier
13473 (i.e., a type declaration)
13474 2: one of the decl-specifiers is an enum-specifier or a
13475 class-specifier (i.e., a type definition)
13479 static void
13480 cp_parser_decl_specifier_seq (cp_parser* parser,
13481 cp_parser_flags flags,
13482 cp_decl_specifier_seq *decl_specs,
13483 int* declares_class_or_enum)
13485 bool constructor_possible_p = !parser->in_declarator_p;
13486 bool found_decl_spec = false;
13487 cp_token *start_token = NULL;
13488 cp_decl_spec ds;
13490 /* Clear DECL_SPECS. */
13491 clear_decl_specs (decl_specs);
13493 /* Assume no class or enumeration type is declared. */
13494 *declares_class_or_enum = 0;
13496 /* Keep reading specifiers until there are no more to read. */
13497 while (true)
13499 bool constructor_p;
13500 cp_token *token;
13501 ds = ds_last;
13503 /* Peek at the next token. */
13504 token = cp_lexer_peek_token (parser->lexer);
13506 /* Save the first token of the decl spec list for error
13507 reporting. */
13508 if (!start_token)
13509 start_token = token;
13510 /* Handle attributes. */
13511 if (cp_next_tokens_can_be_attribute_p (parser))
13513 /* Parse the attributes. */
13514 tree attrs = cp_parser_attributes_opt (parser);
13516 /* In a sequence of declaration specifiers, c++11 attributes
13517 appertain to the type that precede them. In that case
13518 [dcl.spec]/1 says:
13520 The attribute-specifier-seq affects the type only for
13521 the declaration it appears in, not other declarations
13522 involving the same type.
13524 But for now let's force the user to position the
13525 attribute either at the beginning of the declaration or
13526 after the declarator-id, which would clearly mean that it
13527 applies to the declarator. */
13528 if (cxx11_attribute_p (attrs))
13530 if (!found_decl_spec)
13531 /* The c++11 attribute is at the beginning of the
13532 declaration. It appertains to the entity being
13533 declared. */;
13534 else
13536 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13538 /* This is an attribute following a
13539 class-specifier. */
13540 if (decl_specs->type_definition_p)
13541 warn_misplaced_attr_for_class_type (token->location,
13542 decl_specs->type);
13543 attrs = NULL_TREE;
13545 else
13547 decl_specs->std_attributes
13548 = attr_chainon (decl_specs->std_attributes, attrs);
13549 if (decl_specs->locations[ds_std_attribute] == 0)
13550 decl_specs->locations[ds_std_attribute] = token->location;
13552 continue;
13556 decl_specs->attributes
13557 = attr_chainon (decl_specs->attributes, attrs);
13558 if (decl_specs->locations[ds_attribute] == 0)
13559 decl_specs->locations[ds_attribute] = token->location;
13560 continue;
13562 /* Assume we will find a decl-specifier keyword. */
13563 found_decl_spec = true;
13564 /* If the next token is an appropriate keyword, we can simply
13565 add it to the list. */
13566 switch (token->keyword)
13568 /* decl-specifier:
13569 friend
13570 constexpr */
13571 case RID_FRIEND:
13572 if (!at_class_scope_p ())
13574 gcc_rich_location richloc (token->location);
13575 richloc.add_fixit_remove ();
13576 error_at (&richloc, "%<friend%> used outside of class");
13577 cp_lexer_purge_token (parser->lexer);
13579 else
13581 ds = ds_friend;
13582 /* Consume the token. */
13583 cp_lexer_consume_token (parser->lexer);
13585 break;
13587 case RID_CONSTEXPR:
13588 ds = ds_constexpr;
13589 cp_lexer_consume_token (parser->lexer);
13590 break;
13592 case RID_CONCEPT:
13593 ds = ds_concept;
13594 cp_lexer_consume_token (parser->lexer);
13595 break;
13597 /* function-specifier:
13598 inline
13599 virtual
13600 explicit */
13601 case RID_INLINE:
13602 case RID_VIRTUAL:
13603 case RID_EXPLICIT:
13604 cp_parser_function_specifier_opt (parser, decl_specs);
13605 break;
13607 /* decl-specifier:
13608 typedef */
13609 case RID_TYPEDEF:
13610 ds = ds_typedef;
13611 /* Consume the token. */
13612 cp_lexer_consume_token (parser->lexer);
13613 /* A constructor declarator cannot appear in a typedef. */
13614 constructor_possible_p = false;
13615 /* The "typedef" keyword can only occur in a declaration; we
13616 may as well commit at this point. */
13617 cp_parser_commit_to_tentative_parse (parser);
13619 if (decl_specs->storage_class != sc_none)
13620 decl_specs->conflicting_specifiers_p = true;
13621 break;
13623 /* storage-class-specifier:
13624 auto
13625 register
13626 static
13627 extern
13628 mutable
13630 GNU Extension:
13631 thread */
13632 case RID_AUTO:
13633 if (cxx_dialect == cxx98)
13635 /* Consume the token. */
13636 cp_lexer_consume_token (parser->lexer);
13638 /* Complain about `auto' as a storage specifier, if
13639 we're complaining about C++0x compatibility. */
13640 gcc_rich_location richloc (token->location);
13641 richloc.add_fixit_remove ();
13642 warning_at (&richloc, OPT_Wc__11_compat,
13643 "%<auto%> changes meaning in C++11; "
13644 "please remove it");
13646 /* Set the storage class anyway. */
13647 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13648 token);
13650 else
13651 /* C++0x auto type-specifier. */
13652 found_decl_spec = false;
13653 break;
13655 case RID_REGISTER:
13656 case RID_STATIC:
13657 case RID_EXTERN:
13658 case RID_MUTABLE:
13659 /* Consume the token. */
13660 cp_lexer_consume_token (parser->lexer);
13661 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13662 token);
13663 break;
13664 case RID_THREAD:
13665 /* Consume the token. */
13666 ds = ds_thread;
13667 cp_lexer_consume_token (parser->lexer);
13668 break;
13670 default:
13671 /* We did not yet find a decl-specifier yet. */
13672 found_decl_spec = false;
13673 break;
13676 if (found_decl_spec
13677 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13678 && token->keyword != RID_CONSTEXPR)
13679 error ("decl-specifier invalid in condition");
13681 if (found_decl_spec
13682 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13683 && token->keyword != RID_MUTABLE
13684 && token->keyword != RID_CONSTEXPR)
13685 error_at (token->location, "%qD invalid in lambda",
13686 ridpointers[token->keyword]);
13688 if (ds != ds_last)
13689 set_and_check_decl_spec_loc (decl_specs, ds, token);
13691 /* Constructors are a special case. The `S' in `S()' is not a
13692 decl-specifier; it is the beginning of the declarator. */
13693 constructor_p
13694 = (!found_decl_spec
13695 && constructor_possible_p
13696 && (cp_parser_constructor_declarator_p
13697 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13699 /* If we don't have a DECL_SPEC yet, then we must be looking at
13700 a type-specifier. */
13701 if (!found_decl_spec && !constructor_p)
13703 int decl_spec_declares_class_or_enum;
13704 bool is_cv_qualifier;
13705 tree type_spec;
13707 type_spec
13708 = cp_parser_type_specifier (parser, flags,
13709 decl_specs,
13710 /*is_declaration=*/true,
13711 &decl_spec_declares_class_or_enum,
13712 &is_cv_qualifier);
13713 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13715 /* If this type-specifier referenced a user-defined type
13716 (a typedef, class-name, etc.), then we can't allow any
13717 more such type-specifiers henceforth.
13719 [dcl.spec]
13721 The longest sequence of decl-specifiers that could
13722 possibly be a type name is taken as the
13723 decl-specifier-seq of a declaration. The sequence shall
13724 be self-consistent as described below.
13726 [dcl.type]
13728 As a general rule, at most one type-specifier is allowed
13729 in the complete decl-specifier-seq of a declaration. The
13730 only exceptions are the following:
13732 -- const or volatile can be combined with any other
13733 type-specifier.
13735 -- signed or unsigned can be combined with char, long,
13736 short, or int.
13738 -- ..
13740 Example:
13742 typedef char* Pc;
13743 void g (const int Pc);
13745 Here, Pc is *not* part of the decl-specifier seq; it's
13746 the declarator. Therefore, once we see a type-specifier
13747 (other than a cv-qualifier), we forbid any additional
13748 user-defined types. We *do* still allow things like `int
13749 int' to be considered a decl-specifier-seq, and issue the
13750 error message later. */
13751 if (type_spec && !is_cv_qualifier)
13752 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13753 /* A constructor declarator cannot follow a type-specifier. */
13754 if (type_spec)
13756 constructor_possible_p = false;
13757 found_decl_spec = true;
13758 if (!is_cv_qualifier)
13759 decl_specs->any_type_specifiers_p = true;
13763 /* If we still do not have a DECL_SPEC, then there are no more
13764 decl-specifiers. */
13765 if (!found_decl_spec)
13766 break;
13768 decl_specs->any_specifiers_p = true;
13769 /* After we see one decl-specifier, further decl-specifiers are
13770 always optional. */
13771 flags |= CP_PARSER_FLAGS_OPTIONAL;
13774 /* Don't allow a friend specifier with a class definition. */
13775 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13776 && (*declares_class_or_enum & 2))
13777 error_at (decl_specs->locations[ds_friend],
13778 "class definition may not be declared a friend");
13781 /* Parse an (optional) storage-class-specifier.
13783 storage-class-specifier:
13784 auto
13785 register
13786 static
13787 extern
13788 mutable
13790 GNU Extension:
13792 storage-class-specifier:
13793 thread
13795 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13797 static tree
13798 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13800 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13802 case RID_AUTO:
13803 if (cxx_dialect != cxx98)
13804 return NULL_TREE;
13805 /* Fall through for C++98. */
13806 gcc_fallthrough ();
13808 case RID_REGISTER:
13809 case RID_STATIC:
13810 case RID_EXTERN:
13811 case RID_MUTABLE:
13812 case RID_THREAD:
13813 /* Consume the token. */
13814 return cp_lexer_consume_token (parser->lexer)->u.value;
13816 default:
13817 return NULL_TREE;
13821 /* Parse an (optional) function-specifier.
13823 function-specifier:
13824 inline
13825 virtual
13826 explicit
13828 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13829 Updates DECL_SPECS, if it is non-NULL. */
13831 static tree
13832 cp_parser_function_specifier_opt (cp_parser* parser,
13833 cp_decl_specifier_seq *decl_specs)
13835 cp_token *token = cp_lexer_peek_token (parser->lexer);
13836 switch (token->keyword)
13838 case RID_INLINE:
13839 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13840 break;
13842 case RID_VIRTUAL:
13843 /* 14.5.2.3 [temp.mem]
13845 A member function template shall not be virtual. */
13846 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13847 && current_class_type)
13848 error_at (token->location, "templates may not be %<virtual%>");
13849 else
13850 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13851 break;
13853 case RID_EXPLICIT:
13854 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13855 break;
13857 default:
13858 return NULL_TREE;
13861 /* Consume the token. */
13862 return cp_lexer_consume_token (parser->lexer)->u.value;
13865 /* Parse a linkage-specification.
13867 linkage-specification:
13868 extern string-literal { declaration-seq [opt] }
13869 extern string-literal declaration */
13871 static void
13872 cp_parser_linkage_specification (cp_parser* parser)
13874 tree linkage;
13876 /* Look for the `extern' keyword. */
13877 cp_token *extern_token
13878 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13880 /* Look for the string-literal. */
13881 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13882 linkage = cp_parser_string_literal (parser, false, false);
13884 /* Transform the literal into an identifier. If the literal is a
13885 wide-character string, or contains embedded NULs, then we can't
13886 handle it as the user wants. */
13887 if (strlen (TREE_STRING_POINTER (linkage))
13888 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13890 cp_parser_error (parser, "invalid linkage-specification");
13891 /* Assume C++ linkage. */
13892 linkage = lang_name_cplusplus;
13894 else
13895 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13897 /* We're now using the new linkage. */
13898 push_lang_context (linkage);
13900 /* Preserve the location of the the innermost linkage specification,
13901 tracking the locations of nested specifications via a local. */
13902 location_t saved_location
13903 = parser->innermost_linkage_specification_location;
13904 /* Construct a location ranging from the start of the "extern" to
13905 the end of the string-literal, with the caret at the start, e.g.:
13906 extern "C" {
13907 ^~~~~~~~~~
13909 parser->innermost_linkage_specification_location
13910 = make_location (extern_token->location,
13911 extern_token->location,
13912 get_finish (string_token->location));
13914 /* If the next token is a `{', then we're using the first
13915 production. */
13916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13918 cp_ensure_no_omp_declare_simd (parser);
13919 cp_ensure_no_oacc_routine (parser);
13921 /* Consume the `{' token. */
13922 matching_braces braces;
13923 braces.consume_open (parser)->location;
13924 /* Parse the declarations. */
13925 cp_parser_declaration_seq_opt (parser);
13926 /* Look for the closing `}'. */
13927 braces.require_close (parser);
13929 /* Otherwise, there's just one declaration. */
13930 else
13932 bool saved_in_unbraced_linkage_specification_p;
13934 saved_in_unbraced_linkage_specification_p
13935 = parser->in_unbraced_linkage_specification_p;
13936 parser->in_unbraced_linkage_specification_p = true;
13937 cp_parser_declaration (parser);
13938 parser->in_unbraced_linkage_specification_p
13939 = saved_in_unbraced_linkage_specification_p;
13942 /* We're done with the linkage-specification. */
13943 pop_lang_context ();
13945 /* Restore location of parent linkage specification, if any. */
13946 parser->innermost_linkage_specification_location = saved_location;
13949 /* Parse a static_assert-declaration.
13951 static_assert-declaration:
13952 static_assert ( constant-expression , string-literal ) ;
13953 static_assert ( constant-expression ) ; (C++17)
13955 If MEMBER_P, this static_assert is a class member. */
13957 static void
13958 cp_parser_static_assert(cp_parser *parser, bool member_p)
13960 cp_expr condition;
13961 location_t token_loc;
13962 tree message;
13963 bool dummy;
13965 /* Peek at the `static_assert' token so we can keep track of exactly
13966 where the static assertion started. */
13967 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13969 /* Look for the `static_assert' keyword. */
13970 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13971 RT_STATIC_ASSERT))
13972 return;
13974 /* We know we are in a static assertion; commit to any tentative
13975 parse. */
13976 if (cp_parser_parsing_tentatively (parser))
13977 cp_parser_commit_to_tentative_parse (parser);
13979 /* Parse the `(' starting the static assertion condition. */
13980 matching_parens parens;
13981 parens.require_open (parser);
13983 /* Parse the constant-expression. Allow a non-constant expression
13984 here in order to give better diagnostics in finish_static_assert. */
13985 condition =
13986 cp_parser_constant_expression (parser,
13987 /*allow_non_constant_p=*/true,
13988 /*non_constant_p=*/&dummy);
13990 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13992 if (cxx_dialect < cxx17)
13993 pedwarn (input_location, OPT_Wpedantic,
13994 "static_assert without a message "
13995 "only available with -std=c++17 or -std=gnu++17");
13996 /* Eat the ')' */
13997 cp_lexer_consume_token (parser->lexer);
13998 message = build_string (1, "");
13999 TREE_TYPE (message) = char_array_type_node;
14000 fix_string_type (message);
14002 else
14004 /* Parse the separating `,'. */
14005 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14007 /* Parse the string-literal message. */
14008 message = cp_parser_string_literal (parser,
14009 /*translate=*/false,
14010 /*wide_ok=*/true);
14012 /* A `)' completes the static assertion. */
14013 if (!parens.require_close (parser))
14014 cp_parser_skip_to_closing_parenthesis (parser,
14015 /*recovering=*/true,
14016 /*or_comma=*/false,
14017 /*consume_paren=*/true);
14020 /* A semicolon terminates the declaration. */
14021 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14023 /* Get the location for the static assertion. Use that of the
14024 condition if available, otherwise, use that of the "static_assert"
14025 token. */
14026 location_t assert_loc = condition.get_location ();
14027 if (assert_loc == UNKNOWN_LOCATION)
14028 assert_loc = token_loc;
14030 /* Complete the static assertion, which may mean either processing
14031 the static assert now or saving it for template instantiation. */
14032 finish_static_assert (condition, message, assert_loc, member_p);
14035 /* Parse the expression in decltype ( expression ). */
14037 static tree
14038 cp_parser_decltype_expr (cp_parser *parser,
14039 bool &id_expression_or_member_access_p)
14041 cp_token *id_expr_start_token;
14042 tree expr;
14044 /* Since we're going to preserve any side-effects from this parse, set up a
14045 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14046 in the expression. */
14047 tentative_firewall firewall (parser);
14049 /* First, try parsing an id-expression. */
14050 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14051 cp_parser_parse_tentatively (parser);
14052 expr = cp_parser_id_expression (parser,
14053 /*template_keyword_p=*/false,
14054 /*check_dependency_p=*/true,
14055 /*template_p=*/NULL,
14056 /*declarator_p=*/false,
14057 /*optional_p=*/false);
14059 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14061 bool non_integral_constant_expression_p = false;
14062 tree id_expression = expr;
14063 cp_id_kind idk;
14064 const char *error_msg;
14066 if (identifier_p (expr))
14067 /* Lookup the name we got back from the id-expression. */
14068 expr = cp_parser_lookup_name_simple (parser, expr,
14069 id_expr_start_token->location);
14071 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14072 /* A template without args is not a complete id-expression. */
14073 expr = error_mark_node;
14075 if (expr
14076 && expr != error_mark_node
14077 && TREE_CODE (expr) != TYPE_DECL
14078 && (TREE_CODE (expr) != BIT_NOT_EXPR
14079 || !TYPE_P (TREE_OPERAND (expr, 0)))
14080 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14082 /* Complete lookup of the id-expression. */
14083 expr = (finish_id_expression
14084 (id_expression, expr, parser->scope, &idk,
14085 /*integral_constant_expression_p=*/false,
14086 /*allow_non_integral_constant_expression_p=*/true,
14087 &non_integral_constant_expression_p,
14088 /*template_p=*/false,
14089 /*done=*/true,
14090 /*address_p=*/false,
14091 /*template_arg_p=*/false,
14092 &error_msg,
14093 id_expr_start_token->location));
14095 if (expr == error_mark_node)
14096 /* We found an id-expression, but it was something that we
14097 should not have found. This is an error, not something
14098 we can recover from, so note that we found an
14099 id-expression and we'll recover as gracefully as
14100 possible. */
14101 id_expression_or_member_access_p = true;
14104 if (expr
14105 && expr != error_mark_node
14106 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14107 /* We have an id-expression. */
14108 id_expression_or_member_access_p = true;
14111 if (!id_expression_or_member_access_p)
14113 /* Abort the id-expression parse. */
14114 cp_parser_abort_tentative_parse (parser);
14116 /* Parsing tentatively, again. */
14117 cp_parser_parse_tentatively (parser);
14119 /* Parse a class member access. */
14120 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14121 /*cast_p=*/false, /*decltype*/true,
14122 /*member_access_only_p=*/true, NULL);
14124 if (expr
14125 && expr != error_mark_node
14126 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14127 /* We have an id-expression. */
14128 id_expression_or_member_access_p = true;
14131 if (id_expression_or_member_access_p)
14132 /* We have parsed the complete id-expression or member access. */
14133 cp_parser_parse_definitely (parser);
14134 else
14136 /* Abort our attempt to parse an id-expression or member access
14137 expression. */
14138 cp_parser_abort_tentative_parse (parser);
14140 /* Commit to the tentative_firewall so we get syntax errors. */
14141 cp_parser_commit_to_tentative_parse (parser);
14143 /* Parse a full expression. */
14144 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14145 /*decltype_p=*/true);
14148 return expr;
14151 /* Parse a `decltype' type. Returns the type.
14153 simple-type-specifier:
14154 decltype ( expression )
14155 C++14 proposal:
14156 decltype ( auto ) */
14158 static tree
14159 cp_parser_decltype (cp_parser *parser)
14161 bool id_expression_or_member_access_p = false;
14162 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14164 if (start_token->type == CPP_DECLTYPE)
14166 /* Already parsed. */
14167 cp_lexer_consume_token (parser->lexer);
14168 return saved_checks_value (start_token->u.tree_check_value);
14171 /* Look for the `decltype' token. */
14172 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14173 return error_mark_node;
14175 /* Parse the opening `('. */
14176 matching_parens parens;
14177 if (!parens.require_open (parser))
14178 return error_mark_node;
14180 push_deferring_access_checks (dk_deferred);
14182 tree expr = NULL_TREE;
14184 if (cxx_dialect >= cxx14
14185 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14186 /* decltype (auto) */
14187 cp_lexer_consume_token (parser->lexer);
14188 else
14190 /* decltype (expression) */
14192 /* Types cannot be defined in a `decltype' expression. Save away the
14193 old message and set the new one. */
14194 const char *saved_message = parser->type_definition_forbidden_message;
14195 parser->type_definition_forbidden_message
14196 = G_("types may not be defined in %<decltype%> expressions");
14198 /* The restrictions on constant-expressions do not apply inside
14199 decltype expressions. */
14200 bool saved_integral_constant_expression_p
14201 = parser->integral_constant_expression_p;
14202 bool saved_non_integral_constant_expression_p
14203 = parser->non_integral_constant_expression_p;
14204 parser->integral_constant_expression_p = false;
14206 /* Within a parenthesized expression, a `>' token is always
14207 the greater-than operator. */
14208 bool saved_greater_than_is_operator_p
14209 = parser->greater_than_is_operator_p;
14210 parser->greater_than_is_operator_p = true;
14212 /* Do not actually evaluate the expression. */
14213 ++cp_unevaluated_operand;
14215 /* Do not warn about problems with the expression. */
14216 ++c_inhibit_evaluation_warnings;
14218 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14220 /* Go back to evaluating expressions. */
14221 --cp_unevaluated_operand;
14222 --c_inhibit_evaluation_warnings;
14224 /* The `>' token might be the end of a template-id or
14225 template-parameter-list now. */
14226 parser->greater_than_is_operator_p
14227 = saved_greater_than_is_operator_p;
14229 /* Restore the old message and the integral constant expression
14230 flags. */
14231 parser->type_definition_forbidden_message = saved_message;
14232 parser->integral_constant_expression_p
14233 = saved_integral_constant_expression_p;
14234 parser->non_integral_constant_expression_p
14235 = saved_non_integral_constant_expression_p;
14238 /* Parse to the closing `)'. */
14239 if (!parens.require_close (parser))
14241 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14242 /*consume_paren=*/true);
14243 pop_deferring_access_checks ();
14244 return error_mark_node;
14247 if (!expr)
14249 /* Build auto. */
14250 expr = make_decltype_auto ();
14251 AUTO_IS_DECLTYPE (expr) = true;
14253 else
14254 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14255 tf_warning_or_error);
14257 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14258 it again. */
14259 start_token->type = CPP_DECLTYPE;
14260 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14261 start_token->u.tree_check_value->value = expr;
14262 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14263 start_token->keyword = RID_MAX;
14264 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14266 pop_to_parent_deferring_access_checks ();
14268 return expr;
14271 /* Special member functions [gram.special] */
14273 /* Parse a conversion-function-id.
14275 conversion-function-id:
14276 operator conversion-type-id
14278 Returns an IDENTIFIER_NODE representing the operator. */
14280 static tree
14281 cp_parser_conversion_function_id (cp_parser* parser)
14283 tree type;
14284 tree saved_scope;
14285 tree saved_qualifying_scope;
14286 tree saved_object_scope;
14287 tree pushed_scope = NULL_TREE;
14289 /* Look for the `operator' token. */
14290 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14291 return error_mark_node;
14292 /* When we parse the conversion-type-id, the current scope will be
14293 reset. However, we need that information in able to look up the
14294 conversion function later, so we save it here. */
14295 saved_scope = parser->scope;
14296 saved_qualifying_scope = parser->qualifying_scope;
14297 saved_object_scope = parser->object_scope;
14298 /* We must enter the scope of the class so that the names of
14299 entities declared within the class are available in the
14300 conversion-type-id. For example, consider:
14302 struct S {
14303 typedef int I;
14304 operator I();
14307 S::operator I() { ... }
14309 In order to see that `I' is a type-name in the definition, we
14310 must be in the scope of `S'. */
14311 if (saved_scope)
14312 pushed_scope = push_scope (saved_scope);
14313 /* Parse the conversion-type-id. */
14314 type = cp_parser_conversion_type_id (parser);
14315 /* Leave the scope of the class, if any. */
14316 if (pushed_scope)
14317 pop_scope (pushed_scope);
14318 /* Restore the saved scope. */
14319 parser->scope = saved_scope;
14320 parser->qualifying_scope = saved_qualifying_scope;
14321 parser->object_scope = saved_object_scope;
14322 /* If the TYPE is invalid, indicate failure. */
14323 if (type == error_mark_node)
14324 return error_mark_node;
14325 return make_conv_op_name (type);
14328 /* Parse a conversion-type-id:
14330 conversion-type-id:
14331 type-specifier-seq conversion-declarator [opt]
14333 Returns the TYPE specified. */
14335 static tree
14336 cp_parser_conversion_type_id (cp_parser* parser)
14338 tree attributes;
14339 cp_decl_specifier_seq type_specifiers;
14340 cp_declarator *declarator;
14341 tree type_specified;
14342 const char *saved_message;
14344 /* Parse the attributes. */
14345 attributes = cp_parser_attributes_opt (parser);
14347 saved_message = parser->type_definition_forbidden_message;
14348 parser->type_definition_forbidden_message
14349 = G_("types may not be defined in a conversion-type-id");
14351 /* Parse the type-specifiers. */
14352 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14353 /*is_trailing_return=*/false,
14354 &type_specifiers);
14356 parser->type_definition_forbidden_message = saved_message;
14358 /* If that didn't work, stop. */
14359 if (type_specifiers.type == error_mark_node)
14360 return error_mark_node;
14361 /* Parse the conversion-declarator. */
14362 declarator = cp_parser_conversion_declarator_opt (parser);
14364 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14365 /*initialized=*/0, &attributes);
14366 if (attributes)
14367 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14369 /* Don't give this error when parsing tentatively. This happens to
14370 work because we always parse this definitively once. */
14371 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14372 && type_uses_auto (type_specified))
14374 if (cxx_dialect < cxx14)
14376 error ("invalid use of %<auto%> in conversion operator");
14377 return error_mark_node;
14379 else if (template_parm_scope_p ())
14380 warning (0, "use of %<auto%> in member template "
14381 "conversion operator can never be deduced");
14384 return type_specified;
14387 /* Parse an (optional) conversion-declarator.
14389 conversion-declarator:
14390 ptr-operator conversion-declarator [opt]
14394 static cp_declarator *
14395 cp_parser_conversion_declarator_opt (cp_parser* parser)
14397 enum tree_code code;
14398 tree class_type, std_attributes = NULL_TREE;
14399 cp_cv_quals cv_quals;
14401 /* We don't know if there's a ptr-operator next, or not. */
14402 cp_parser_parse_tentatively (parser);
14403 /* Try the ptr-operator. */
14404 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14405 &std_attributes);
14406 /* If it worked, look for more conversion-declarators. */
14407 if (cp_parser_parse_definitely (parser))
14409 cp_declarator *declarator;
14411 /* Parse another optional declarator. */
14412 declarator = cp_parser_conversion_declarator_opt (parser);
14414 declarator = cp_parser_make_indirect_declarator
14415 (code, class_type, cv_quals, declarator, std_attributes);
14417 return declarator;
14420 return NULL;
14423 /* Parse an (optional) ctor-initializer.
14425 ctor-initializer:
14426 : mem-initializer-list */
14428 static void
14429 cp_parser_ctor_initializer_opt (cp_parser* parser)
14431 /* If the next token is not a `:', then there is no
14432 ctor-initializer. */
14433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14435 /* Do default initialization of any bases and members. */
14436 if (DECL_CONSTRUCTOR_P (current_function_decl))
14437 finish_mem_initializers (NULL_TREE);
14438 return;
14441 /* Consume the `:' token. */
14442 cp_lexer_consume_token (parser->lexer);
14443 /* And the mem-initializer-list. */
14444 cp_parser_mem_initializer_list (parser);
14447 /* Parse a mem-initializer-list.
14449 mem-initializer-list:
14450 mem-initializer ... [opt]
14451 mem-initializer ... [opt] , mem-initializer-list */
14453 static void
14454 cp_parser_mem_initializer_list (cp_parser* parser)
14456 tree mem_initializer_list = NULL_TREE;
14457 tree target_ctor = error_mark_node;
14458 cp_token *token = cp_lexer_peek_token (parser->lexer);
14460 /* Let the semantic analysis code know that we are starting the
14461 mem-initializer-list. */
14462 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14463 error_at (token->location,
14464 "only constructors take member initializers");
14466 /* Loop through the list. */
14467 while (true)
14469 tree mem_initializer;
14471 token = cp_lexer_peek_token (parser->lexer);
14472 /* Parse the mem-initializer. */
14473 mem_initializer = cp_parser_mem_initializer (parser);
14474 /* If the next token is a `...', we're expanding member initializers. */
14475 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14476 if (ellipsis
14477 || (mem_initializer != error_mark_node
14478 && check_for_bare_parameter_packs (TREE_PURPOSE
14479 (mem_initializer))))
14481 /* Consume the `...'. */
14482 if (ellipsis)
14483 cp_lexer_consume_token (parser->lexer);
14485 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14486 can be expanded but members cannot. */
14487 if (mem_initializer != error_mark_node
14488 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14490 error_at (token->location,
14491 "cannot expand initializer for member %qD",
14492 TREE_PURPOSE (mem_initializer));
14493 mem_initializer = error_mark_node;
14496 /* Construct the pack expansion type. */
14497 if (mem_initializer != error_mark_node)
14498 mem_initializer = make_pack_expansion (mem_initializer);
14500 if (target_ctor != error_mark_node
14501 && mem_initializer != error_mark_node)
14503 error ("mem-initializer for %qD follows constructor delegation",
14504 TREE_PURPOSE (mem_initializer));
14505 mem_initializer = error_mark_node;
14507 /* Look for a target constructor. */
14508 if (mem_initializer != error_mark_node
14509 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14510 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14512 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14513 if (mem_initializer_list)
14515 error ("constructor delegation follows mem-initializer for %qD",
14516 TREE_PURPOSE (mem_initializer_list));
14517 mem_initializer = error_mark_node;
14519 target_ctor = mem_initializer;
14521 /* Add it to the list, unless it was erroneous. */
14522 if (mem_initializer != error_mark_node)
14524 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14525 mem_initializer_list = mem_initializer;
14527 /* If the next token is not a `,', we're done. */
14528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14529 break;
14530 /* Consume the `,' token. */
14531 cp_lexer_consume_token (parser->lexer);
14534 /* Perform semantic analysis. */
14535 if (DECL_CONSTRUCTOR_P (current_function_decl))
14536 finish_mem_initializers (mem_initializer_list);
14539 /* Parse a mem-initializer.
14541 mem-initializer:
14542 mem-initializer-id ( expression-list [opt] )
14543 mem-initializer-id braced-init-list
14545 GNU extension:
14547 mem-initializer:
14548 ( expression-list [opt] )
14550 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14551 class) or FIELD_DECL (for a non-static data member) to initialize;
14552 the TREE_VALUE is the expression-list. An empty initialization
14553 list is represented by void_list_node. */
14555 static tree
14556 cp_parser_mem_initializer (cp_parser* parser)
14558 tree mem_initializer_id;
14559 tree expression_list;
14560 tree member;
14561 cp_token *token = cp_lexer_peek_token (parser->lexer);
14563 /* Find out what is being initialized. */
14564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14566 permerror (token->location,
14567 "anachronistic old-style base class initializer");
14568 mem_initializer_id = NULL_TREE;
14570 else
14572 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14573 if (mem_initializer_id == error_mark_node)
14574 return mem_initializer_id;
14576 member = expand_member_init (mem_initializer_id);
14577 if (member && !DECL_P (member))
14578 in_base_initializer = 1;
14580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14582 bool expr_non_constant_p;
14583 cp_lexer_set_source_position (parser->lexer);
14584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14585 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14586 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14587 expression_list = build_tree_list (NULL_TREE, expression_list);
14589 else
14591 vec<tree, va_gc> *vec;
14592 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14593 /*cast_p=*/false,
14594 /*allow_expansion_p=*/true,
14595 /*non_constant_p=*/NULL);
14596 if (vec == NULL)
14597 return error_mark_node;
14598 expression_list = build_tree_list_vec (vec);
14599 release_tree_vector (vec);
14602 if (expression_list == error_mark_node)
14603 return error_mark_node;
14604 if (!expression_list)
14605 expression_list = void_type_node;
14607 in_base_initializer = 0;
14609 return member ? build_tree_list (member, expression_list) : error_mark_node;
14612 /* Parse a mem-initializer-id.
14614 mem-initializer-id:
14615 :: [opt] nested-name-specifier [opt] class-name
14616 decltype-specifier (C++11)
14617 identifier
14619 Returns a TYPE indicating the class to be initialized for the first
14620 production (and the second in C++11). Returns an IDENTIFIER_NODE
14621 indicating the data member to be initialized for the last production. */
14623 static tree
14624 cp_parser_mem_initializer_id (cp_parser* parser)
14626 bool global_scope_p;
14627 bool nested_name_specifier_p;
14628 bool template_p = false;
14629 tree id;
14631 cp_token *token = cp_lexer_peek_token (parser->lexer);
14633 /* `typename' is not allowed in this context ([temp.res]). */
14634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14636 error_at (token->location,
14637 "keyword %<typename%> not allowed in this context (a qualified "
14638 "member initializer is implicitly a type)");
14639 cp_lexer_consume_token (parser->lexer);
14641 /* Look for the optional `::' operator. */
14642 global_scope_p
14643 = (cp_parser_global_scope_opt (parser,
14644 /*current_scope_valid_p=*/false)
14645 != NULL_TREE);
14646 /* Look for the optional nested-name-specifier. The simplest way to
14647 implement:
14649 [temp.res]
14651 The keyword `typename' is not permitted in a base-specifier or
14652 mem-initializer; in these contexts a qualified name that
14653 depends on a template-parameter is implicitly assumed to be a
14654 type name.
14656 is to assume that we have seen the `typename' keyword at this
14657 point. */
14658 nested_name_specifier_p
14659 = (cp_parser_nested_name_specifier_opt (parser,
14660 /*typename_keyword_p=*/true,
14661 /*check_dependency_p=*/true,
14662 /*type_p=*/true,
14663 /*is_declaration=*/true)
14664 != NULL_TREE);
14665 if (nested_name_specifier_p)
14666 template_p = cp_parser_optional_template_keyword (parser);
14667 /* If there is a `::' operator or a nested-name-specifier, then we
14668 are definitely looking for a class-name. */
14669 if (global_scope_p || nested_name_specifier_p)
14670 return cp_parser_class_name (parser,
14671 /*typename_keyword_p=*/true,
14672 /*template_keyword_p=*/template_p,
14673 typename_type,
14674 /*check_dependency_p=*/true,
14675 /*class_head_p=*/false,
14676 /*is_declaration=*/true);
14677 /* Otherwise, we could also be looking for an ordinary identifier. */
14678 cp_parser_parse_tentatively (parser);
14679 if (cp_lexer_next_token_is_decltype (parser->lexer))
14680 /* Try a decltype-specifier. */
14681 id = cp_parser_decltype (parser);
14682 else
14683 /* Otherwise, try a class-name. */
14684 id = cp_parser_class_name (parser,
14685 /*typename_keyword_p=*/true,
14686 /*template_keyword_p=*/false,
14687 none_type,
14688 /*check_dependency_p=*/true,
14689 /*class_head_p=*/false,
14690 /*is_declaration=*/true);
14691 /* If we found one, we're done. */
14692 if (cp_parser_parse_definitely (parser))
14693 return id;
14694 /* Otherwise, look for an ordinary identifier. */
14695 return cp_parser_identifier (parser);
14698 /* Overloading [gram.over] */
14700 /* Parse an operator-function-id.
14702 operator-function-id:
14703 operator operator
14705 Returns an IDENTIFIER_NODE for the operator which is a
14706 human-readable spelling of the identifier, e.g., `operator +'. */
14708 static cp_expr
14709 cp_parser_operator_function_id (cp_parser* parser)
14711 /* Look for the `operator' keyword. */
14712 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14713 return error_mark_node;
14714 /* And then the name of the operator itself. */
14715 return cp_parser_operator (parser);
14718 /* Return an identifier node for a user-defined literal operator.
14719 The suffix identifier is chained to the operator name identifier. */
14721 tree
14722 cp_literal_operator_id (const char* name)
14724 tree identifier;
14725 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14726 + strlen (name) + 10);
14727 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14728 identifier = get_identifier (buffer);
14730 return identifier;
14733 /* Parse an operator.
14735 operator:
14736 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14737 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14738 || ++ -- , ->* -> () []
14740 GNU Extensions:
14742 operator:
14743 <? >? <?= >?=
14745 Returns an IDENTIFIER_NODE for the operator which is a
14746 human-readable spelling of the identifier, e.g., `operator +'. */
14748 static cp_expr
14749 cp_parser_operator (cp_parser* parser)
14751 tree id = NULL_TREE;
14752 cp_token *token;
14753 bool utf8 = false;
14755 /* Peek at the next token. */
14756 token = cp_lexer_peek_token (parser->lexer);
14758 location_t start_loc = token->location;
14760 /* Figure out which operator we have. */
14761 enum tree_code op = ERROR_MARK;
14762 bool assop = false;
14763 bool consumed = false;
14764 switch (token->type)
14766 case CPP_KEYWORD:
14768 /* The keyword should be either `new' or `delete'. */
14769 if (token->keyword == RID_NEW)
14770 op = NEW_EXPR;
14771 else if (token->keyword == RID_DELETE)
14772 op = DELETE_EXPR;
14773 else
14774 break;
14776 /* Consume the `new' or `delete' token. */
14777 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14779 /* Peek at the next token. */
14780 token = cp_lexer_peek_token (parser->lexer);
14781 /* If it's a `[' token then this is the array variant of the
14782 operator. */
14783 if (token->type == CPP_OPEN_SQUARE)
14785 /* Consume the `[' token. */
14786 cp_lexer_consume_token (parser->lexer);
14787 /* Look for the `]' token. */
14788 if (cp_token *close_token
14789 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14790 end_loc = close_token->location;
14791 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14793 start_loc = make_location (start_loc, start_loc, end_loc);
14794 consumed = true;
14795 break;
14798 case CPP_PLUS:
14799 op = PLUS_EXPR;
14800 break;
14802 case CPP_MINUS:
14803 op = MINUS_EXPR;
14804 break;
14806 case CPP_MULT:
14807 op = MULT_EXPR;
14808 break;
14810 case CPP_DIV:
14811 op = TRUNC_DIV_EXPR;
14812 break;
14814 case CPP_MOD:
14815 op = TRUNC_MOD_EXPR;
14816 break;
14818 case CPP_XOR:
14819 op = BIT_XOR_EXPR;
14820 break;
14822 case CPP_AND:
14823 op = BIT_AND_EXPR;
14824 break;
14826 case CPP_OR:
14827 op = BIT_IOR_EXPR;
14828 break;
14830 case CPP_COMPL:
14831 op = BIT_NOT_EXPR;
14832 break;
14834 case CPP_NOT:
14835 op = TRUTH_NOT_EXPR;
14836 break;
14838 case CPP_EQ:
14839 assop = true;
14840 op = NOP_EXPR;
14841 break;
14843 case CPP_LESS:
14844 op = LT_EXPR;
14845 break;
14847 case CPP_GREATER:
14848 op = GT_EXPR;
14849 break;
14851 case CPP_PLUS_EQ:
14852 assop = true;
14853 op = PLUS_EXPR;
14854 break;
14856 case CPP_MINUS_EQ:
14857 assop = true;
14858 op = MINUS_EXPR;
14859 break;
14861 case CPP_MULT_EQ:
14862 assop = true;
14863 op = MULT_EXPR;
14864 break;
14866 case CPP_DIV_EQ:
14867 assop = true;
14868 op = TRUNC_DIV_EXPR;
14869 break;
14871 case CPP_MOD_EQ:
14872 assop = true;
14873 op = TRUNC_MOD_EXPR;
14874 break;
14876 case CPP_XOR_EQ:
14877 assop = true;
14878 op = BIT_XOR_EXPR;
14879 break;
14881 case CPP_AND_EQ:
14882 assop = true;
14883 op = BIT_AND_EXPR;
14884 break;
14886 case CPP_OR_EQ:
14887 assop = true;
14888 op = BIT_IOR_EXPR;
14889 break;
14891 case CPP_LSHIFT:
14892 op = LSHIFT_EXPR;
14893 break;
14895 case CPP_RSHIFT:
14896 op = RSHIFT_EXPR;
14897 break;
14899 case CPP_LSHIFT_EQ:
14900 assop = true;
14901 op = LSHIFT_EXPR;
14902 break;
14904 case CPP_RSHIFT_EQ:
14905 assop = true;
14906 op = RSHIFT_EXPR;
14907 break;
14909 case CPP_EQ_EQ:
14910 op = EQ_EXPR;
14911 break;
14913 case CPP_NOT_EQ:
14914 op = NE_EXPR;
14915 break;
14917 case CPP_LESS_EQ:
14918 op = LE_EXPR;
14919 break;
14921 case CPP_GREATER_EQ:
14922 op = GE_EXPR;
14923 break;
14925 case CPP_AND_AND:
14926 op = TRUTH_ANDIF_EXPR;
14927 break;
14929 case CPP_OR_OR:
14930 op = TRUTH_ORIF_EXPR;
14931 break;
14933 case CPP_PLUS_PLUS:
14934 op = POSTINCREMENT_EXPR;
14935 break;
14937 case CPP_MINUS_MINUS:
14938 op = PREDECREMENT_EXPR;
14939 break;
14941 case CPP_COMMA:
14942 op = COMPOUND_EXPR;
14943 break;
14945 case CPP_DEREF_STAR:
14946 op = MEMBER_REF;
14947 break;
14949 case CPP_DEREF:
14950 op = COMPONENT_REF;
14951 break;
14953 case CPP_OPEN_PAREN:
14955 /* Consume the `('. */
14956 matching_parens parens;
14957 parens.consume_open (parser);
14958 /* Look for the matching `)'. */
14959 parens.require_close (parser);
14960 op = CALL_EXPR;
14961 consumed = true;
14962 break;
14965 case CPP_OPEN_SQUARE:
14966 /* Consume the `['. */
14967 cp_lexer_consume_token (parser->lexer);
14968 /* Look for the matching `]'. */
14969 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14970 op = ARRAY_REF;
14971 consumed = true;
14972 break;
14974 case CPP_UTF8STRING:
14975 case CPP_UTF8STRING_USERDEF:
14976 utf8 = true;
14977 /* FALLTHRU */
14978 case CPP_STRING:
14979 case CPP_WSTRING:
14980 case CPP_STRING16:
14981 case CPP_STRING32:
14982 case CPP_STRING_USERDEF:
14983 case CPP_WSTRING_USERDEF:
14984 case CPP_STRING16_USERDEF:
14985 case CPP_STRING32_USERDEF:
14987 tree str, string_tree;
14988 int sz, len;
14990 if (cxx_dialect == cxx98)
14991 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14993 /* Consume the string. */
14994 str = cp_parser_string_literal (parser, /*translate=*/true,
14995 /*wide_ok=*/true, /*lookup_udlit=*/false);
14996 if (str == error_mark_node)
14997 return error_mark_node;
14998 else if (TREE_CODE (str) == USERDEF_LITERAL)
15000 string_tree = USERDEF_LITERAL_VALUE (str);
15001 id = USERDEF_LITERAL_SUFFIX_ID (str);
15003 else
15005 string_tree = str;
15006 /* Look for the suffix identifier. */
15007 token = cp_lexer_peek_token (parser->lexer);
15008 if (token->type == CPP_NAME)
15009 id = cp_parser_identifier (parser);
15010 else if (token->type == CPP_KEYWORD)
15012 error ("unexpected keyword;"
15013 " remove space between quotes and suffix identifier");
15014 return error_mark_node;
15016 else
15018 error ("expected suffix identifier");
15019 return error_mark_node;
15022 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15023 (TREE_TYPE (TREE_TYPE (string_tree))));
15024 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15025 if (len != 0)
15027 error ("expected empty string after %<operator%> keyword");
15028 return error_mark_node;
15030 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15031 != char_type_node)
15033 error ("invalid encoding prefix in literal operator");
15034 return error_mark_node;
15036 if (id != error_mark_node)
15038 const char *name = IDENTIFIER_POINTER (id);
15039 id = cp_literal_operator_id (name);
15041 return id;
15044 default:
15045 /* Anything else is an error. */
15046 break;
15049 /* If we have selected an identifier, we need to consume the
15050 operator token. */
15051 if (op != ERROR_MARK)
15053 id = ovl_op_identifier (assop, op);
15054 if (!consumed)
15055 cp_lexer_consume_token (parser->lexer);
15057 /* Otherwise, no valid operator name was present. */
15058 else
15060 cp_parser_error (parser, "expected operator");
15061 id = error_mark_node;
15064 return cp_expr (id, start_loc);
15067 /* Parse a template-declaration.
15069 template-declaration:
15070 export [opt] template < template-parameter-list > declaration
15072 If MEMBER_P is TRUE, this template-declaration occurs within a
15073 class-specifier.
15075 The grammar rule given by the standard isn't correct. What
15076 is really meant is:
15078 template-declaration:
15079 export [opt] template-parameter-list-seq
15080 decl-specifier-seq [opt] init-declarator [opt] ;
15081 export [opt] template-parameter-list-seq
15082 function-definition
15084 template-parameter-list-seq:
15085 template-parameter-list-seq [opt]
15086 template < template-parameter-list >
15088 Concept Extensions:
15090 template-parameter-list-seq:
15091 template < template-parameter-list > requires-clause [opt]
15093 requires-clause:
15094 requires logical-or-expression */
15096 static void
15097 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15099 /* Check for `export'. */
15100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15102 /* Consume the `export' token. */
15103 cp_lexer_consume_token (parser->lexer);
15104 /* Warn that we do not support `export'. */
15105 warning (0, "keyword %<export%> not implemented, and will be ignored");
15108 cp_parser_template_declaration_after_export (parser, member_p);
15111 /* Parse a template-parameter-list.
15113 template-parameter-list:
15114 template-parameter
15115 template-parameter-list , template-parameter
15117 Returns a TREE_LIST. Each node represents a template parameter.
15118 The nodes are connected via their TREE_CHAINs. */
15120 static tree
15121 cp_parser_template_parameter_list (cp_parser* parser)
15123 tree parameter_list = NULL_TREE;
15125 begin_template_parm_list ();
15127 /* The loop below parses the template parms. We first need to know
15128 the total number of template parms to be able to compute proper
15129 canonical types of each dependent type. So after the loop, when
15130 we know the total number of template parms,
15131 end_template_parm_list computes the proper canonical types and
15132 fixes up the dependent types accordingly. */
15133 while (true)
15135 tree parameter;
15136 bool is_non_type;
15137 bool is_parameter_pack;
15138 location_t parm_loc;
15140 /* Parse the template-parameter. */
15141 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15142 parameter = cp_parser_template_parameter (parser,
15143 &is_non_type,
15144 &is_parameter_pack);
15145 /* Add it to the list. */
15146 if (parameter != error_mark_node)
15147 parameter_list = process_template_parm (parameter_list,
15148 parm_loc,
15149 parameter,
15150 is_non_type,
15151 is_parameter_pack);
15152 else
15154 tree err_parm = build_tree_list (parameter, parameter);
15155 parameter_list = chainon (parameter_list, err_parm);
15158 /* If the next token is not a `,', we're done. */
15159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15160 break;
15161 /* Otherwise, consume the `,' token. */
15162 cp_lexer_consume_token (parser->lexer);
15165 return end_template_parm_list (parameter_list);
15168 /* Parse a introduction-list.
15170 introduction-list:
15171 introduced-parameter
15172 introduction-list , introduced-parameter
15174 introduced-parameter:
15175 ...[opt] identifier
15177 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15178 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15179 WILDCARD_DECL will also have DECL_NAME set and token location in
15180 DECL_SOURCE_LOCATION. */
15182 static tree
15183 cp_parser_introduction_list (cp_parser *parser)
15185 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15187 while (true)
15189 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15190 if (is_pack)
15191 cp_lexer_consume_token (parser->lexer);
15193 /* Build placeholder. */
15194 tree parm = build_nt (WILDCARD_DECL);
15195 DECL_SOURCE_LOCATION (parm)
15196 = cp_lexer_peek_token (parser->lexer)->location;
15197 DECL_NAME (parm) = cp_parser_identifier (parser);
15198 WILDCARD_PACK_P (parm) = is_pack;
15199 vec_safe_push (introduction_vec, parm);
15201 /* If the next token is not a `,', we're done. */
15202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15203 break;
15204 /* Otherwise, consume the `,' token. */
15205 cp_lexer_consume_token (parser->lexer);
15208 /* Convert the vec into a TREE_VEC. */
15209 tree introduction_list = make_tree_vec (introduction_vec->length ());
15210 unsigned int n;
15211 tree parm;
15212 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15213 TREE_VEC_ELT (introduction_list, n) = parm;
15215 release_tree_vector (introduction_vec);
15216 return introduction_list;
15219 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15220 is an abstract declarator. */
15222 static inline cp_declarator*
15223 get_id_declarator (cp_declarator *declarator)
15225 cp_declarator *d = declarator;
15226 while (d && d->kind != cdk_id)
15227 d = d->declarator;
15228 return d;
15231 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15232 is an abstract declarator. */
15234 static inline tree
15235 get_unqualified_id (cp_declarator *declarator)
15237 declarator = get_id_declarator (declarator);
15238 if (declarator)
15239 return declarator->u.id.unqualified_name;
15240 else
15241 return NULL_TREE;
15244 /* Returns true if DECL represents a constrained-parameter. */
15246 static inline bool
15247 is_constrained_parameter (tree decl)
15249 return (decl
15250 && TREE_CODE (decl) == TYPE_DECL
15251 && CONSTRAINED_PARM_CONCEPT (decl)
15252 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15255 /* Returns true if PARM declares a constrained-parameter. */
15257 static inline bool
15258 is_constrained_parameter (cp_parameter_declarator *parm)
15260 return is_constrained_parameter (parm->decl_specifiers.type);
15263 /* Check that the type parameter is only a declarator-id, and that its
15264 type is not cv-qualified. */
15266 bool
15267 cp_parser_check_constrained_type_parm (cp_parser *parser,
15268 cp_parameter_declarator *parm)
15270 if (!parm->declarator)
15271 return true;
15273 if (parm->declarator->kind != cdk_id)
15275 cp_parser_error (parser, "invalid constrained type parameter");
15276 return false;
15279 /* Don't allow cv-qualified type parameters. */
15280 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15281 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15283 cp_parser_error (parser, "cv-qualified type parameter");
15284 return false;
15287 return true;
15290 /* Finish parsing/processing a template type parameter and checking
15291 various restrictions. */
15293 static inline tree
15294 cp_parser_constrained_type_template_parm (cp_parser *parser,
15295 tree id,
15296 cp_parameter_declarator* parmdecl)
15298 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15299 return finish_template_type_parm (class_type_node, id);
15300 else
15301 return error_mark_node;
15304 static tree
15305 finish_constrained_template_template_parm (tree proto, tree id)
15307 /* FIXME: This should probably be copied, and we may need to adjust
15308 the template parameter depths. */
15309 tree saved_parms = current_template_parms;
15310 begin_template_parm_list ();
15311 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15312 end_template_parm_list ();
15314 tree parm = finish_template_template_parm (class_type_node, id);
15315 current_template_parms = saved_parms;
15317 return parm;
15320 /* Finish parsing/processing a template template parameter by borrowing
15321 the template parameter list from the prototype parameter. */
15323 static tree
15324 cp_parser_constrained_template_template_parm (cp_parser *parser,
15325 tree proto,
15326 tree id,
15327 cp_parameter_declarator *parmdecl)
15329 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15330 return error_mark_node;
15331 return finish_constrained_template_template_parm (proto, id);
15334 /* Create a new non-type template parameter from the given PARM
15335 declarator. */
15337 static tree
15338 constrained_non_type_template_parm (bool *is_non_type,
15339 cp_parameter_declarator *parm)
15341 *is_non_type = true;
15342 cp_declarator *decl = parm->declarator;
15343 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15344 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15345 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15348 /* Build a constrained template parameter based on the PARMDECL
15349 declarator. The type of PARMDECL is the constrained type, which
15350 refers to the prototype template parameter that ultimately
15351 specifies the type of the declared parameter. */
15353 static tree
15354 finish_constrained_parameter (cp_parser *parser,
15355 cp_parameter_declarator *parmdecl,
15356 bool *is_non_type,
15357 bool *is_parameter_pack)
15359 tree decl = parmdecl->decl_specifiers.type;
15360 tree id = get_unqualified_id (parmdecl->declarator);
15361 tree def = parmdecl->default_argument;
15362 tree proto = DECL_INITIAL (decl);
15364 /* A template parameter constrained by a variadic concept shall also
15365 be declared as a template parameter pack. */
15366 bool is_variadic = template_parameter_pack_p (proto);
15367 if (is_variadic && !*is_parameter_pack)
15368 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15370 /* Build the parameter. Return an error if the declarator was invalid. */
15371 tree parm;
15372 if (TREE_CODE (proto) == TYPE_DECL)
15373 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15374 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15375 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15376 parmdecl);
15377 else
15378 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15379 if (parm == error_mark_node)
15380 return error_mark_node;
15382 /* Finish the parameter decl and create a node attaching the
15383 default argument and constraint. */
15384 parm = build_tree_list (def, parm);
15385 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15387 return parm;
15390 /* Returns true if the parsed type actually represents the declaration
15391 of a type template-parameter. */
15393 static inline bool
15394 declares_constrained_type_template_parameter (tree type)
15396 return (is_constrained_parameter (type)
15397 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15401 /* Returns true if the parsed type actually represents the declaration of
15402 a template template-parameter. */
15404 static bool
15405 declares_constrained_template_template_parameter (tree type)
15407 return (is_constrained_parameter (type)
15408 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15411 /* Parse a default argument for a type template-parameter.
15412 Note that diagnostics are handled in cp_parser_template_parameter. */
15414 static tree
15415 cp_parser_default_type_template_argument (cp_parser *parser)
15417 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15419 /* Consume the `=' token. */
15420 cp_lexer_consume_token (parser->lexer);
15422 cp_token *token = cp_lexer_peek_token (parser->lexer);
15424 /* Parse the default-argument. */
15425 push_deferring_access_checks (dk_no_deferred);
15426 tree default_argument = cp_parser_type_id (parser);
15427 pop_deferring_access_checks ();
15429 if (flag_concepts && type_uses_auto (default_argument))
15431 error_at (token->location,
15432 "invalid use of %<auto%> in default template argument");
15433 return error_mark_node;
15436 return default_argument;
15439 /* Parse a default argument for a template template-parameter. */
15441 static tree
15442 cp_parser_default_template_template_argument (cp_parser *parser)
15444 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15446 bool is_template;
15448 /* Consume the `='. */
15449 cp_lexer_consume_token (parser->lexer);
15450 /* Parse the id-expression. */
15451 push_deferring_access_checks (dk_no_deferred);
15452 /* save token before parsing the id-expression, for error
15453 reporting */
15454 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15455 tree default_argument
15456 = cp_parser_id_expression (parser,
15457 /*template_keyword_p=*/false,
15458 /*check_dependency_p=*/true,
15459 /*template_p=*/&is_template,
15460 /*declarator_p=*/false,
15461 /*optional_p=*/false);
15462 if (TREE_CODE (default_argument) == TYPE_DECL)
15463 /* If the id-expression was a template-id that refers to
15464 a template-class, we already have the declaration here,
15465 so no further lookup is needed. */
15467 else
15468 /* Look up the name. */
15469 default_argument
15470 = cp_parser_lookup_name (parser, default_argument,
15471 none_type,
15472 /*is_template=*/is_template,
15473 /*is_namespace=*/false,
15474 /*check_dependency=*/true,
15475 /*ambiguous_decls=*/NULL,
15476 token->location);
15477 /* See if the default argument is valid. */
15478 default_argument = check_template_template_default_arg (default_argument);
15479 pop_deferring_access_checks ();
15480 return default_argument;
15483 /* Parse a template-parameter.
15485 template-parameter:
15486 type-parameter
15487 parameter-declaration
15489 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15490 the parameter. The TREE_PURPOSE is the default value, if any.
15491 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15492 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15493 set to true iff this parameter is a parameter pack. */
15495 static tree
15496 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15497 bool *is_parameter_pack)
15499 cp_token *token;
15500 cp_parameter_declarator *parameter_declarator;
15501 tree parm;
15503 /* Assume it is a type parameter or a template parameter. */
15504 *is_non_type = false;
15505 /* Assume it not a parameter pack. */
15506 *is_parameter_pack = false;
15507 /* Peek at the next token. */
15508 token = cp_lexer_peek_token (parser->lexer);
15509 /* If it is `template', we have a type-parameter. */
15510 if (token->keyword == RID_TEMPLATE)
15511 return cp_parser_type_parameter (parser, is_parameter_pack);
15512 /* If it is `class' or `typename' we do not know yet whether it is a
15513 type parameter or a non-type parameter. Consider:
15515 template <typename T, typename T::X X> ...
15519 template <class C, class D*> ...
15521 Here, the first parameter is a type parameter, and the second is
15522 a non-type parameter. We can tell by looking at the token after
15523 the identifier -- if it is a `,', `=', or `>' then we have a type
15524 parameter. */
15525 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15527 /* Peek at the token after `class' or `typename'. */
15528 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15529 /* If it's an ellipsis, we have a template type parameter
15530 pack. */
15531 if (token->type == CPP_ELLIPSIS)
15532 return cp_parser_type_parameter (parser, is_parameter_pack);
15533 /* If it's an identifier, skip it. */
15534 if (token->type == CPP_NAME)
15535 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15536 /* Now, see if the token looks like the end of a template
15537 parameter. */
15538 if (token->type == CPP_COMMA
15539 || token->type == CPP_EQ
15540 || token->type == CPP_GREATER)
15541 return cp_parser_type_parameter (parser, is_parameter_pack);
15544 /* Otherwise, it is a non-type parameter or a constrained parameter.
15546 [temp.param]
15548 When parsing a default template-argument for a non-type
15549 template-parameter, the first non-nested `>' is taken as the end
15550 of the template parameter-list rather than a greater-than
15551 operator. */
15552 parameter_declarator
15553 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15554 /*parenthesized_p=*/NULL);
15556 if (!parameter_declarator)
15557 return error_mark_node;
15559 /* If the parameter declaration is marked as a parameter pack, set
15560 *IS_PARAMETER_PACK to notify the caller. */
15561 if (parameter_declarator->template_parameter_pack_p)
15562 *is_parameter_pack = true;
15564 if (parameter_declarator->default_argument)
15566 /* Can happen in some cases of erroneous input (c++/34892). */
15567 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15568 /* Consume the `...' for better error recovery. */
15569 cp_lexer_consume_token (parser->lexer);
15572 // The parameter may have been constrained.
15573 if (is_constrained_parameter (parameter_declarator))
15574 return finish_constrained_parameter (parser,
15575 parameter_declarator,
15576 is_non_type,
15577 is_parameter_pack);
15579 // Now we're sure that the parameter is a non-type parameter.
15580 *is_non_type = true;
15582 parm = grokdeclarator (parameter_declarator->declarator,
15583 &parameter_declarator->decl_specifiers,
15584 TPARM, /*initialized=*/0,
15585 /*attrlist=*/NULL);
15586 if (parm == error_mark_node)
15587 return error_mark_node;
15589 return build_tree_list (parameter_declarator->default_argument, parm);
15592 /* Parse a type-parameter.
15594 type-parameter:
15595 class identifier [opt]
15596 class identifier [opt] = type-id
15597 typename identifier [opt]
15598 typename identifier [opt] = type-id
15599 template < template-parameter-list > class identifier [opt]
15600 template < template-parameter-list > class identifier [opt]
15601 = id-expression
15603 GNU Extension (variadic templates):
15605 type-parameter:
15606 class ... identifier [opt]
15607 typename ... identifier [opt]
15609 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15610 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15611 the declaration of the parameter.
15613 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15615 static tree
15616 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15618 cp_token *token;
15619 tree parameter;
15621 /* Look for a keyword to tell us what kind of parameter this is. */
15622 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15623 if (!token)
15624 return error_mark_node;
15626 switch (token->keyword)
15628 case RID_CLASS:
15629 case RID_TYPENAME:
15631 tree identifier;
15632 tree default_argument;
15634 /* If the next token is an ellipsis, we have a template
15635 argument pack. */
15636 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15638 /* Consume the `...' token. */
15639 cp_lexer_consume_token (parser->lexer);
15640 maybe_warn_variadic_templates ();
15642 *is_parameter_pack = true;
15645 /* If the next token is an identifier, then it names the
15646 parameter. */
15647 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15648 identifier = cp_parser_identifier (parser);
15649 else
15650 identifier = NULL_TREE;
15652 /* Create the parameter. */
15653 parameter = finish_template_type_parm (class_type_node, identifier);
15655 /* If the next token is an `=', we have a default argument. */
15656 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15658 default_argument
15659 = cp_parser_default_type_template_argument (parser);
15661 /* Template parameter packs cannot have default
15662 arguments. */
15663 if (*is_parameter_pack)
15665 if (identifier)
15666 error_at (token->location,
15667 "template parameter pack %qD cannot have a "
15668 "default argument", identifier);
15669 else
15670 error_at (token->location,
15671 "template parameter packs cannot have "
15672 "default arguments");
15673 default_argument = NULL_TREE;
15675 else if (check_for_bare_parameter_packs (default_argument))
15676 default_argument = error_mark_node;
15678 else
15679 default_argument = NULL_TREE;
15681 /* Create the combined representation of the parameter and the
15682 default argument. */
15683 parameter = build_tree_list (default_argument, parameter);
15685 break;
15687 case RID_TEMPLATE:
15689 tree identifier;
15690 tree default_argument;
15692 /* Look for the `<'. */
15693 cp_parser_require (parser, CPP_LESS, RT_LESS);
15694 /* Parse the template-parameter-list. */
15695 cp_parser_template_parameter_list (parser);
15696 /* Look for the `>'. */
15697 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15699 // If template requirements are present, parse them.
15700 if (flag_concepts)
15702 tree reqs = get_shorthand_constraints (current_template_parms);
15703 if (tree r = cp_parser_requires_clause_opt (parser))
15704 reqs = conjoin_constraints (reqs, normalize_expression (r));
15705 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15708 /* Look for the `class' or 'typename' keywords. */
15709 cp_parser_type_parameter_key (parser);
15710 /* If the next token is an ellipsis, we have a template
15711 argument pack. */
15712 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15714 /* Consume the `...' token. */
15715 cp_lexer_consume_token (parser->lexer);
15716 maybe_warn_variadic_templates ();
15718 *is_parameter_pack = true;
15720 /* If the next token is an `=', then there is a
15721 default-argument. If the next token is a `>', we are at
15722 the end of the parameter-list. If the next token is a `,',
15723 then we are at the end of this parameter. */
15724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15725 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15726 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15728 identifier = cp_parser_identifier (parser);
15729 /* Treat invalid names as if the parameter were nameless. */
15730 if (identifier == error_mark_node)
15731 identifier = NULL_TREE;
15733 else
15734 identifier = NULL_TREE;
15736 /* Create the template parameter. */
15737 parameter = finish_template_template_parm (class_type_node,
15738 identifier);
15740 /* If the next token is an `=', then there is a
15741 default-argument. */
15742 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15744 default_argument
15745 = cp_parser_default_template_template_argument (parser);
15747 /* Template parameter packs cannot have default
15748 arguments. */
15749 if (*is_parameter_pack)
15751 if (identifier)
15752 error_at (token->location,
15753 "template parameter pack %qD cannot "
15754 "have a default argument",
15755 identifier);
15756 else
15757 error_at (token->location, "template parameter packs cannot "
15758 "have default arguments");
15759 default_argument = NULL_TREE;
15762 else
15763 default_argument = NULL_TREE;
15765 /* Create the combined representation of the parameter and the
15766 default argument. */
15767 parameter = build_tree_list (default_argument, parameter);
15769 break;
15771 default:
15772 gcc_unreachable ();
15773 break;
15776 return parameter;
15779 /* Parse a template-id.
15781 template-id:
15782 template-name < template-argument-list [opt] >
15784 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15785 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15786 returned. Otherwise, if the template-name names a function, or set
15787 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15788 names a class, returns a TYPE_DECL for the specialization.
15790 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15791 uninstantiated templates. */
15793 static tree
15794 cp_parser_template_id (cp_parser *parser,
15795 bool template_keyword_p,
15796 bool check_dependency_p,
15797 enum tag_types tag_type,
15798 bool is_declaration)
15800 tree templ;
15801 tree arguments;
15802 tree template_id;
15803 cp_token_position start_of_id = 0;
15804 cp_token *next_token = NULL, *next_token_2 = NULL;
15805 bool is_identifier;
15807 /* If the next token corresponds to a template-id, there is no need
15808 to reparse it. */
15809 cp_token *token = cp_lexer_peek_token (parser->lexer);
15810 if (token->type == CPP_TEMPLATE_ID)
15812 cp_lexer_consume_token (parser->lexer);
15813 return saved_checks_value (token->u.tree_check_value);
15816 /* Avoid performing name lookup if there is no possibility of
15817 finding a template-id. */
15818 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15819 || (token->type == CPP_NAME
15820 && !cp_parser_nth_token_starts_template_argument_list_p
15821 (parser, 2)))
15823 cp_parser_error (parser, "expected template-id");
15824 return error_mark_node;
15827 /* Remember where the template-id starts. */
15828 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15829 start_of_id = cp_lexer_token_position (parser->lexer, false);
15831 push_deferring_access_checks (dk_deferred);
15833 /* Parse the template-name. */
15834 is_identifier = false;
15835 templ = cp_parser_template_name (parser, template_keyword_p,
15836 check_dependency_p,
15837 is_declaration,
15838 tag_type,
15839 &is_identifier);
15840 if (templ == error_mark_node || is_identifier)
15842 pop_deferring_access_checks ();
15843 return templ;
15846 /* Since we're going to preserve any side-effects from this parse, set up a
15847 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15848 in the template arguments. */
15849 tentative_firewall firewall (parser);
15851 /* If we find the sequence `[:' after a template-name, it's probably
15852 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15853 parse correctly the argument list. */
15854 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15855 == CPP_OPEN_SQUARE)
15856 && next_token->flags & DIGRAPH
15857 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15858 == CPP_COLON)
15859 && !(next_token_2->flags & PREV_WHITE))
15861 cp_parser_parse_tentatively (parser);
15862 /* Change `:' into `::'. */
15863 next_token_2->type = CPP_SCOPE;
15864 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15865 CPP_LESS. */
15866 cp_lexer_consume_token (parser->lexer);
15868 /* Parse the arguments. */
15869 arguments = cp_parser_enclosed_template_argument_list (parser);
15870 if (!cp_parser_parse_definitely (parser))
15872 /* If we couldn't parse an argument list, then we revert our changes
15873 and return simply an error. Maybe this is not a template-id
15874 after all. */
15875 next_token_2->type = CPP_COLON;
15876 cp_parser_error (parser, "expected %<<%>");
15877 pop_deferring_access_checks ();
15878 return error_mark_node;
15880 /* Otherwise, emit an error about the invalid digraph, but continue
15881 parsing because we got our argument list. */
15882 if (permerror (next_token->location,
15883 "%<<::%> cannot begin a template-argument list"))
15885 static bool hint = false;
15886 inform (next_token->location,
15887 "%<<:%> is an alternate spelling for %<[%>."
15888 " Insert whitespace between %<<%> and %<::%>");
15889 if (!hint && !flag_permissive)
15891 inform (next_token->location, "(if you use %<-fpermissive%> "
15892 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15893 "accept your code)");
15894 hint = true;
15898 else
15900 /* Look for the `<' that starts the template-argument-list. */
15901 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15903 pop_deferring_access_checks ();
15904 return error_mark_node;
15906 /* Parse the arguments. */
15907 arguments = cp_parser_enclosed_template_argument_list (parser);
15910 /* Set the location to be of the form:
15911 template-name < template-argument-list [opt] >
15912 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15913 with caret == start at the start of the template-name,
15914 ranging until the closing '>'. */
15915 location_t finish_loc
15916 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15917 location_t combined_loc
15918 = make_location (token->location, token->location, finish_loc);
15920 /* Check for concepts autos where they don't belong. We could
15921 identify types in some cases of idnetifier TEMPL, looking ahead
15922 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15923 types. We reject them in functions, but if what we have is an
15924 identifier, even with none_type we can't conclude it's NOT a
15925 type, we have to wait for template substitution. */
15926 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15927 template_id = error_mark_node;
15928 /* Build a representation of the specialization. */
15929 else if (identifier_p (templ))
15930 template_id = build_min_nt_loc (combined_loc,
15931 TEMPLATE_ID_EXPR,
15932 templ, arguments);
15933 else if (DECL_TYPE_TEMPLATE_P (templ)
15934 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15936 bool entering_scope;
15937 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15938 template (rather than some instantiation thereof) only if
15939 is not nested within some other construct. For example, in
15940 "template <typename T> void f(T) { A<T>::", A<T> is just an
15941 instantiation of A. */
15942 entering_scope = (template_parm_scope_p ()
15943 && cp_lexer_next_token_is (parser->lexer,
15944 CPP_SCOPE));
15945 template_id
15946 = finish_template_type (templ, arguments, entering_scope);
15948 /* A template-like identifier may be a partial concept id. */
15949 else if (flag_concepts
15950 && (template_id = (cp_parser_maybe_partial_concept_id
15951 (parser, templ, arguments))))
15952 return template_id;
15953 else if (variable_template_p (templ))
15955 template_id = lookup_template_variable (templ, arguments);
15956 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15957 SET_EXPR_LOCATION (template_id, combined_loc);
15959 else
15961 /* If it's not a class-template or a template-template, it should be
15962 a function-template. */
15963 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15964 || TREE_CODE (templ) == OVERLOAD
15965 || BASELINK_P (templ)));
15967 template_id = lookup_template_function (templ, arguments);
15968 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15969 SET_EXPR_LOCATION (template_id, combined_loc);
15972 /* If parsing tentatively, replace the sequence of tokens that makes
15973 up the template-id with a CPP_TEMPLATE_ID token. That way,
15974 should we re-parse the token stream, we will not have to repeat
15975 the effort required to do the parse, nor will we issue duplicate
15976 error messages about problems during instantiation of the
15977 template. */
15978 if (start_of_id
15979 /* Don't do this if we had a parse error in a declarator; re-parsing
15980 might succeed if a name changes meaning (60361). */
15981 && !(cp_parser_error_occurred (parser)
15982 && cp_parser_parsing_tentatively (parser)
15983 && parser->in_declarator_p))
15985 /* Reset the contents of the START_OF_ID token. */
15986 token->type = CPP_TEMPLATE_ID;
15987 token->location = combined_loc;
15989 /* We must mark the lookup as kept, so we don't throw it away on
15990 the first parse. */
15991 if (is_overloaded_fn (template_id))
15992 lookup_keep (get_fns (template_id), true);
15994 /* Retrieve any deferred checks. Do not pop this access checks yet
15995 so the memory will not be reclaimed during token replacing below. */
15996 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15997 token->u.tree_check_value->value = template_id;
15998 token->u.tree_check_value->checks = get_deferred_access_checks ();
15999 token->keyword = RID_MAX;
16001 /* Purge all subsequent tokens. */
16002 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16004 /* ??? Can we actually assume that, if template_id ==
16005 error_mark_node, we will have issued a diagnostic to the
16006 user, as opposed to simply marking the tentative parse as
16007 failed? */
16008 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16009 error_at (token->location, "parse error in template argument list");
16012 pop_to_parent_deferring_access_checks ();
16013 return template_id;
16016 /* Parse a template-name.
16018 template-name:
16019 identifier
16021 The standard should actually say:
16023 template-name:
16024 identifier
16025 operator-function-id
16027 A defect report has been filed about this issue.
16029 A conversion-function-id cannot be a template name because they cannot
16030 be part of a template-id. In fact, looking at this code:
16032 a.operator K<int>()
16034 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16035 It is impossible to call a templated conversion-function-id with an
16036 explicit argument list, since the only allowed template parameter is
16037 the type to which it is converting.
16039 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16040 `template' keyword, in a construction like:
16042 T::template f<3>()
16044 In that case `f' is taken to be a template-name, even though there
16045 is no way of knowing for sure.
16047 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16048 name refers to a set of overloaded functions, at least one of which
16049 is a template, or an IDENTIFIER_NODE with the name of the template,
16050 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16051 names are looked up inside uninstantiated templates. */
16053 static tree
16054 cp_parser_template_name (cp_parser* parser,
16055 bool template_keyword_p,
16056 bool check_dependency_p,
16057 bool is_declaration,
16058 enum tag_types tag_type,
16059 bool *is_identifier)
16061 tree identifier;
16062 tree decl;
16063 cp_token *token = cp_lexer_peek_token (parser->lexer);
16065 /* If the next token is `operator', then we have either an
16066 operator-function-id or a conversion-function-id. */
16067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16069 /* We don't know whether we're looking at an
16070 operator-function-id or a conversion-function-id. */
16071 cp_parser_parse_tentatively (parser);
16072 /* Try an operator-function-id. */
16073 identifier = cp_parser_operator_function_id (parser);
16074 /* If that didn't work, try a conversion-function-id. */
16075 if (!cp_parser_parse_definitely (parser))
16077 cp_parser_error (parser, "expected template-name");
16078 return error_mark_node;
16081 /* Look for the identifier. */
16082 else
16083 identifier = cp_parser_identifier (parser);
16085 /* If we didn't find an identifier, we don't have a template-id. */
16086 if (identifier == error_mark_node)
16087 return error_mark_node;
16089 /* If the name immediately followed the `template' keyword, then it
16090 is a template-name. However, if the next token is not `<', then
16091 we do not treat it as a template-name, since it is not being used
16092 as part of a template-id. This enables us to handle constructs
16093 like:
16095 template <typename T> struct S { S(); };
16096 template <typename T> S<T>::S();
16098 correctly. We would treat `S' as a template -- if it were `S<T>'
16099 -- but we do not if there is no `<'. */
16101 if (processing_template_decl
16102 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16104 /* In a declaration, in a dependent context, we pretend that the
16105 "template" keyword was present in order to improve error
16106 recovery. For example, given:
16108 template <typename T> void f(T::X<int>);
16110 we want to treat "X<int>" as a template-id. */
16111 if (is_declaration
16112 && !template_keyword_p
16113 && parser->scope && TYPE_P (parser->scope)
16114 && check_dependency_p
16115 && dependent_scope_p (parser->scope)
16116 /* Do not do this for dtors (or ctors), since they never
16117 need the template keyword before their name. */
16118 && !constructor_name_p (identifier, parser->scope))
16120 cp_token_position start = 0;
16122 /* Explain what went wrong. */
16123 error_at (token->location, "non-template %qD used as template",
16124 identifier);
16125 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16126 parser->scope, identifier);
16127 /* If parsing tentatively, find the location of the "<" token. */
16128 if (cp_parser_simulate_error (parser))
16129 start = cp_lexer_token_position (parser->lexer, true);
16130 /* Parse the template arguments so that we can issue error
16131 messages about them. */
16132 cp_lexer_consume_token (parser->lexer);
16133 cp_parser_enclosed_template_argument_list (parser);
16134 /* Skip tokens until we find a good place from which to
16135 continue parsing. */
16136 cp_parser_skip_to_closing_parenthesis (parser,
16137 /*recovering=*/true,
16138 /*or_comma=*/true,
16139 /*consume_paren=*/false);
16140 /* If parsing tentatively, permanently remove the
16141 template argument list. That will prevent duplicate
16142 error messages from being issued about the missing
16143 "template" keyword. */
16144 if (start)
16145 cp_lexer_purge_tokens_after (parser->lexer, start);
16146 if (is_identifier)
16147 *is_identifier = true;
16148 parser->context->object_type = NULL_TREE;
16149 return identifier;
16152 /* If the "template" keyword is present, then there is generally
16153 no point in doing name-lookup, so we just return IDENTIFIER.
16154 But, if the qualifying scope is non-dependent then we can
16155 (and must) do name-lookup normally. */
16156 if (template_keyword_p)
16158 tree scope = (parser->scope ? parser->scope
16159 : parser->context->object_type);
16160 if (scope && TYPE_P (scope)
16161 && (!CLASS_TYPE_P (scope)
16162 || (check_dependency_p && dependent_type_p (scope))))
16164 /* We're optimizing away the call to cp_parser_lookup_name, but
16165 we still need to do this. */
16166 parser->context->object_type = NULL_TREE;
16167 return identifier;
16172 /* Look up the name. */
16173 decl = cp_parser_lookup_name (parser, identifier,
16174 tag_type,
16175 /*is_template=*/true,
16176 /*is_namespace=*/false,
16177 check_dependency_p,
16178 /*ambiguous_decls=*/NULL,
16179 token->location);
16181 decl = strip_using_decl (decl);
16183 /* If DECL is a template, then the name was a template-name. */
16184 if (TREE_CODE (decl) == TEMPLATE_DECL)
16186 if (TREE_DEPRECATED (decl)
16187 && deprecated_state != DEPRECATED_SUPPRESS)
16188 warn_deprecated_use (decl, NULL_TREE);
16190 else
16192 /* The standard does not explicitly indicate whether a name that
16193 names a set of overloaded declarations, some of which are
16194 templates, is a template-name. However, such a name should
16195 be a template-name; otherwise, there is no way to form a
16196 template-id for the overloaded templates. */
16197 bool found = false;
16199 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16200 !found && iter; ++iter)
16201 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16202 found = true;
16204 if (!found)
16206 /* The name does not name a template. */
16207 cp_parser_error (parser, "expected template-name");
16208 return error_mark_node;
16212 /* If DECL is dependent, and refers to a function, then just return
16213 its name; we will look it up again during template instantiation. */
16214 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16216 tree scope = ovl_scope (decl);
16217 if (TYPE_P (scope) && dependent_type_p (scope))
16218 return identifier;
16221 return decl;
16224 /* Parse a template-argument-list.
16226 template-argument-list:
16227 template-argument ... [opt]
16228 template-argument-list , template-argument ... [opt]
16230 Returns a TREE_VEC containing the arguments. */
16232 static tree
16233 cp_parser_template_argument_list (cp_parser* parser)
16235 tree fixed_args[10];
16236 unsigned n_args = 0;
16237 unsigned alloced = 10;
16238 tree *arg_ary = fixed_args;
16239 tree vec;
16240 bool saved_in_template_argument_list_p;
16241 bool saved_ice_p;
16242 bool saved_non_ice_p;
16244 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16245 parser->in_template_argument_list_p = true;
16246 /* Even if the template-id appears in an integral
16247 constant-expression, the contents of the argument list do
16248 not. */
16249 saved_ice_p = parser->integral_constant_expression_p;
16250 parser->integral_constant_expression_p = false;
16251 saved_non_ice_p = parser->non_integral_constant_expression_p;
16252 parser->non_integral_constant_expression_p = false;
16254 /* Parse the arguments. */
16257 tree argument;
16259 if (n_args)
16260 /* Consume the comma. */
16261 cp_lexer_consume_token (parser->lexer);
16263 /* Parse the template-argument. */
16264 argument = cp_parser_template_argument (parser);
16266 /* If the next token is an ellipsis, we're expanding a template
16267 argument pack. */
16268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16270 if (argument == error_mark_node)
16272 cp_token *token = cp_lexer_peek_token (parser->lexer);
16273 error_at (token->location,
16274 "expected parameter pack before %<...%>");
16276 /* Consume the `...' token. */
16277 cp_lexer_consume_token (parser->lexer);
16279 /* Make the argument into a TYPE_PACK_EXPANSION or
16280 EXPR_PACK_EXPANSION. */
16281 argument = make_pack_expansion (argument);
16284 if (n_args == alloced)
16286 alloced *= 2;
16288 if (arg_ary == fixed_args)
16290 arg_ary = XNEWVEC (tree, alloced);
16291 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16293 else
16294 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16296 arg_ary[n_args++] = argument;
16298 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16300 vec = make_tree_vec (n_args);
16302 while (n_args--)
16303 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16305 if (arg_ary != fixed_args)
16306 free (arg_ary);
16307 parser->non_integral_constant_expression_p = saved_non_ice_p;
16308 parser->integral_constant_expression_p = saved_ice_p;
16309 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16310 if (CHECKING_P)
16311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16312 return vec;
16315 /* Parse a template-argument.
16317 template-argument:
16318 assignment-expression
16319 type-id
16320 id-expression
16322 The representation is that of an assignment-expression, type-id, or
16323 id-expression -- except that the qualified id-expression is
16324 evaluated, so that the value returned is either a DECL or an
16325 OVERLOAD.
16327 Although the standard says "assignment-expression", it forbids
16328 throw-expressions or assignments in the template argument.
16329 Therefore, we use "conditional-expression" instead. */
16331 static tree
16332 cp_parser_template_argument (cp_parser* parser)
16334 tree argument;
16335 bool template_p;
16336 bool address_p;
16337 bool maybe_type_id = false;
16338 cp_token *token = NULL, *argument_start_token = NULL;
16339 location_t loc = 0;
16340 cp_id_kind idk;
16342 /* There's really no way to know what we're looking at, so we just
16343 try each alternative in order.
16345 [temp.arg]
16347 In a template-argument, an ambiguity between a type-id and an
16348 expression is resolved to a type-id, regardless of the form of
16349 the corresponding template-parameter.
16351 Therefore, we try a type-id first. */
16352 cp_parser_parse_tentatively (parser);
16353 argument = cp_parser_template_type_arg (parser);
16354 /* If there was no error parsing the type-id but the next token is a
16355 '>>', our behavior depends on which dialect of C++ we're
16356 parsing. In C++98, we probably found a typo for '> >'. But there
16357 are type-id which are also valid expressions. For instance:
16359 struct X { int operator >> (int); };
16360 template <int V> struct Foo {};
16361 Foo<X () >> 5> r;
16363 Here 'X()' is a valid type-id of a function type, but the user just
16364 wanted to write the expression "X() >> 5". Thus, we remember that we
16365 found a valid type-id, but we still try to parse the argument as an
16366 expression to see what happens.
16368 In C++0x, the '>>' will be considered two separate '>'
16369 tokens. */
16370 if (!cp_parser_error_occurred (parser)
16371 && cxx_dialect == cxx98
16372 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16374 maybe_type_id = true;
16375 cp_parser_abort_tentative_parse (parser);
16377 else
16379 /* If the next token isn't a `,' or a `>', then this argument wasn't
16380 really finished. This means that the argument is not a valid
16381 type-id. */
16382 if (!cp_parser_next_token_ends_template_argument_p (parser))
16383 cp_parser_error (parser, "expected template-argument");
16384 /* If that worked, we're done. */
16385 if (cp_parser_parse_definitely (parser))
16386 return argument;
16388 /* We're still not sure what the argument will be. */
16389 cp_parser_parse_tentatively (parser);
16390 /* Try a template. */
16391 argument_start_token = cp_lexer_peek_token (parser->lexer);
16392 argument = cp_parser_id_expression (parser,
16393 /*template_keyword_p=*/false,
16394 /*check_dependency_p=*/true,
16395 &template_p,
16396 /*declarator_p=*/false,
16397 /*optional_p=*/false);
16398 /* If the next token isn't a `,' or a `>', then this argument wasn't
16399 really finished. */
16400 if (!cp_parser_next_token_ends_template_argument_p (parser))
16401 cp_parser_error (parser, "expected template-argument");
16402 if (!cp_parser_error_occurred (parser))
16404 /* Figure out what is being referred to. If the id-expression
16405 was for a class template specialization, then we will have a
16406 TYPE_DECL at this point. There is no need to do name lookup
16407 at this point in that case. */
16408 if (TREE_CODE (argument) != TYPE_DECL)
16409 argument = cp_parser_lookup_name (parser, argument,
16410 none_type,
16411 /*is_template=*/template_p,
16412 /*is_namespace=*/false,
16413 /*check_dependency=*/true,
16414 /*ambiguous_decls=*/NULL,
16415 argument_start_token->location);
16416 /* Handle a constrained-type-specifier for a non-type template
16417 parameter. */
16418 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16419 argument = decl;
16420 else if (TREE_CODE (argument) != TEMPLATE_DECL
16421 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16422 cp_parser_error (parser, "expected template-name");
16424 if (cp_parser_parse_definitely (parser))
16426 if (TREE_DEPRECATED (argument))
16427 warn_deprecated_use (argument, NULL_TREE);
16428 return argument;
16430 /* It must be a non-type argument. In C++17 any constant-expression is
16431 allowed. */
16432 if (cxx_dialect > cxx14)
16433 goto general_expr;
16435 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16437 -- an integral constant-expression of integral or enumeration
16438 type; or
16440 -- the name of a non-type template-parameter; or
16442 -- the name of an object or function with external linkage...
16444 -- the address of an object or function with external linkage...
16446 -- a pointer to member... */
16447 /* Look for a non-type template parameter. */
16448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16450 cp_parser_parse_tentatively (parser);
16451 argument = cp_parser_primary_expression (parser,
16452 /*address_p=*/false,
16453 /*cast_p=*/false,
16454 /*template_arg_p=*/true,
16455 &idk);
16456 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16457 || !cp_parser_next_token_ends_template_argument_p (parser))
16458 cp_parser_simulate_error (parser);
16459 if (cp_parser_parse_definitely (parser))
16460 return argument;
16463 /* If the next token is "&", the argument must be the address of an
16464 object or function with external linkage. */
16465 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16466 if (address_p)
16468 loc = cp_lexer_peek_token (parser->lexer)->location;
16469 cp_lexer_consume_token (parser->lexer);
16471 /* See if we might have an id-expression. */
16472 token = cp_lexer_peek_token (parser->lexer);
16473 if (token->type == CPP_NAME
16474 || token->keyword == RID_OPERATOR
16475 || token->type == CPP_SCOPE
16476 || token->type == CPP_TEMPLATE_ID
16477 || token->type == CPP_NESTED_NAME_SPECIFIER)
16479 cp_parser_parse_tentatively (parser);
16480 argument = cp_parser_primary_expression (parser,
16481 address_p,
16482 /*cast_p=*/false,
16483 /*template_arg_p=*/true,
16484 &idk);
16485 if (cp_parser_error_occurred (parser)
16486 || !cp_parser_next_token_ends_template_argument_p (parser))
16487 cp_parser_abort_tentative_parse (parser);
16488 else
16490 tree probe;
16492 if (INDIRECT_REF_P (argument))
16494 /* Strip the dereference temporarily. */
16495 gcc_assert (REFERENCE_REF_P (argument));
16496 argument = TREE_OPERAND (argument, 0);
16499 /* If we're in a template, we represent a qualified-id referring
16500 to a static data member as a SCOPE_REF even if the scope isn't
16501 dependent so that we can check access control later. */
16502 probe = argument;
16503 if (TREE_CODE (probe) == SCOPE_REF)
16504 probe = TREE_OPERAND (probe, 1);
16505 if (VAR_P (probe))
16507 /* A variable without external linkage might still be a
16508 valid constant-expression, so no error is issued here
16509 if the external-linkage check fails. */
16510 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16511 cp_parser_simulate_error (parser);
16513 else if (is_overloaded_fn (argument))
16514 /* All overloaded functions are allowed; if the external
16515 linkage test does not pass, an error will be issued
16516 later. */
16518 else if (address_p
16519 && (TREE_CODE (argument) == OFFSET_REF
16520 || TREE_CODE (argument) == SCOPE_REF))
16521 /* A pointer-to-member. */
16523 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16525 else
16526 cp_parser_simulate_error (parser);
16528 if (cp_parser_parse_definitely (parser))
16530 if (address_p)
16531 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16532 tf_warning_or_error);
16533 else
16534 argument = convert_from_reference (argument);
16535 return argument;
16539 /* If the argument started with "&", there are no other valid
16540 alternatives at this point. */
16541 if (address_p)
16543 cp_parser_error (parser, "invalid non-type template argument");
16544 return error_mark_node;
16547 general_expr:
16548 /* If the argument wasn't successfully parsed as a type-id followed
16549 by '>>', the argument can only be a constant expression now.
16550 Otherwise, we try parsing the constant-expression tentatively,
16551 because the argument could really be a type-id. */
16552 if (maybe_type_id)
16553 cp_parser_parse_tentatively (parser);
16555 if (cxx_dialect <= cxx14)
16556 argument = cp_parser_constant_expression (parser);
16557 else
16559 /* With C++17 generalized non-type template arguments we need to handle
16560 lvalue constant expressions, too. */
16561 argument = cp_parser_assignment_expression (parser);
16562 require_potential_constant_expression (argument);
16565 if (!maybe_type_id)
16566 return argument;
16567 if (!cp_parser_next_token_ends_template_argument_p (parser))
16568 cp_parser_error (parser, "expected template-argument");
16569 if (cp_parser_parse_definitely (parser))
16570 return argument;
16571 /* We did our best to parse the argument as a non type-id, but that
16572 was the only alternative that matched (albeit with a '>' after
16573 it). We can assume it's just a typo from the user, and a
16574 diagnostic will then be issued. */
16575 return cp_parser_template_type_arg (parser);
16578 /* Parse an explicit-instantiation.
16580 explicit-instantiation:
16581 template declaration
16583 Although the standard says `declaration', what it really means is:
16585 explicit-instantiation:
16586 template decl-specifier-seq [opt] declarator [opt] ;
16588 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16589 supposed to be allowed. A defect report has been filed about this
16590 issue.
16592 GNU Extension:
16594 explicit-instantiation:
16595 storage-class-specifier template
16596 decl-specifier-seq [opt] declarator [opt] ;
16597 function-specifier template
16598 decl-specifier-seq [opt] declarator [opt] ; */
16600 static void
16601 cp_parser_explicit_instantiation (cp_parser* parser)
16603 int declares_class_or_enum;
16604 cp_decl_specifier_seq decl_specifiers;
16605 tree extension_specifier = NULL_TREE;
16607 timevar_push (TV_TEMPLATE_INST);
16609 /* Look for an (optional) storage-class-specifier or
16610 function-specifier. */
16611 if (cp_parser_allow_gnu_extensions_p (parser))
16613 extension_specifier
16614 = cp_parser_storage_class_specifier_opt (parser);
16615 if (!extension_specifier)
16616 extension_specifier
16617 = cp_parser_function_specifier_opt (parser,
16618 /*decl_specs=*/NULL);
16621 /* Look for the `template' keyword. */
16622 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16623 /* Let the front end know that we are processing an explicit
16624 instantiation. */
16625 begin_explicit_instantiation ();
16626 /* [temp.explicit] says that we are supposed to ignore access
16627 control while processing explicit instantiation directives. */
16628 push_deferring_access_checks (dk_no_check);
16629 /* Parse a decl-specifier-seq. */
16630 cp_parser_decl_specifier_seq (parser,
16631 CP_PARSER_FLAGS_OPTIONAL,
16632 &decl_specifiers,
16633 &declares_class_or_enum);
16634 /* If there was exactly one decl-specifier, and it declared a class,
16635 and there's no declarator, then we have an explicit type
16636 instantiation. */
16637 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16639 tree type;
16641 type = check_tag_decl (&decl_specifiers,
16642 /*explicit_type_instantiation_p=*/true);
16643 /* Turn access control back on for names used during
16644 template instantiation. */
16645 pop_deferring_access_checks ();
16646 if (type)
16647 do_type_instantiation (type, extension_specifier,
16648 /*complain=*/tf_error);
16650 else
16652 cp_declarator *declarator;
16653 tree decl;
16655 /* Parse the declarator. */
16656 declarator
16657 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16658 /*ctor_dtor_or_conv_p=*/NULL,
16659 /*parenthesized_p=*/NULL,
16660 /*member_p=*/false,
16661 /*friend_p=*/false);
16662 if (declares_class_or_enum & 2)
16663 cp_parser_check_for_definition_in_return_type (declarator,
16664 decl_specifiers.type,
16665 decl_specifiers.locations[ds_type_spec]);
16666 if (declarator != cp_error_declarator)
16668 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16669 permerror (decl_specifiers.locations[ds_inline],
16670 "explicit instantiation shall not use"
16671 " %<inline%> specifier");
16672 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16673 permerror (decl_specifiers.locations[ds_constexpr],
16674 "explicit instantiation shall not use"
16675 " %<constexpr%> specifier");
16677 decl = grokdeclarator (declarator, &decl_specifiers,
16678 NORMAL, 0, &decl_specifiers.attributes);
16679 /* Turn access control back on for names used during
16680 template instantiation. */
16681 pop_deferring_access_checks ();
16682 /* Do the explicit instantiation. */
16683 do_decl_instantiation (decl, extension_specifier);
16685 else
16687 pop_deferring_access_checks ();
16688 /* Skip the body of the explicit instantiation. */
16689 cp_parser_skip_to_end_of_statement (parser);
16692 /* We're done with the instantiation. */
16693 end_explicit_instantiation ();
16695 cp_parser_consume_semicolon_at_end_of_statement (parser);
16697 timevar_pop (TV_TEMPLATE_INST);
16700 /* Parse an explicit-specialization.
16702 explicit-specialization:
16703 template < > declaration
16705 Although the standard says `declaration', what it really means is:
16707 explicit-specialization:
16708 template <> decl-specifier [opt] init-declarator [opt] ;
16709 template <> function-definition
16710 template <> explicit-specialization
16711 template <> template-declaration */
16713 static void
16714 cp_parser_explicit_specialization (cp_parser* parser)
16716 bool need_lang_pop;
16717 cp_token *token = cp_lexer_peek_token (parser->lexer);
16719 /* Look for the `template' keyword. */
16720 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16721 /* Look for the `<'. */
16722 cp_parser_require (parser, CPP_LESS, RT_LESS);
16723 /* Look for the `>'. */
16724 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16725 /* We have processed another parameter list. */
16726 ++parser->num_template_parameter_lists;
16727 /* [temp]
16729 A template ... explicit specialization ... shall not have C
16730 linkage. */
16731 if (current_lang_name == lang_name_c)
16733 error_at (token->location, "template specialization with C linkage");
16734 maybe_show_extern_c_location ();
16735 /* Give it C++ linkage to avoid confusing other parts of the
16736 front end. */
16737 push_lang_context (lang_name_cplusplus);
16738 need_lang_pop = true;
16740 else
16741 need_lang_pop = false;
16742 /* Let the front end know that we are beginning a specialization. */
16743 if (!begin_specialization ())
16745 end_specialization ();
16746 return;
16749 /* If the next keyword is `template', we need to figure out whether
16750 or not we're looking a template-declaration. */
16751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16753 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16754 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16755 cp_parser_template_declaration_after_export (parser,
16756 /*member_p=*/false);
16757 else
16758 cp_parser_explicit_specialization (parser);
16760 else
16761 /* Parse the dependent declaration. */
16762 cp_parser_single_declaration (parser,
16763 /*checks=*/NULL,
16764 /*member_p=*/false,
16765 /*explicit_specialization_p=*/true,
16766 /*friend_p=*/NULL);
16767 /* We're done with the specialization. */
16768 end_specialization ();
16769 /* For the erroneous case of a template with C linkage, we pushed an
16770 implicit C++ linkage scope; exit that scope now. */
16771 if (need_lang_pop)
16772 pop_lang_context ();
16773 /* We're done with this parameter list. */
16774 --parser->num_template_parameter_lists;
16777 /* Parse a type-specifier.
16779 type-specifier:
16780 simple-type-specifier
16781 class-specifier
16782 enum-specifier
16783 elaborated-type-specifier
16784 cv-qualifier
16786 GNU Extension:
16788 type-specifier:
16789 __complex__
16791 Returns a representation of the type-specifier. For a
16792 class-specifier, enum-specifier, or elaborated-type-specifier, a
16793 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16795 The parser flags FLAGS is used to control type-specifier parsing.
16797 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16798 in a decl-specifier-seq.
16800 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16801 class-specifier, enum-specifier, or elaborated-type-specifier, then
16802 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16803 if a type is declared; 2 if it is defined. Otherwise, it is set to
16804 zero.
16806 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16807 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16808 is set to FALSE. */
16810 static tree
16811 cp_parser_type_specifier (cp_parser* parser,
16812 cp_parser_flags flags,
16813 cp_decl_specifier_seq *decl_specs,
16814 bool is_declaration,
16815 int* declares_class_or_enum,
16816 bool* is_cv_qualifier)
16818 tree type_spec = NULL_TREE;
16819 cp_token *token;
16820 enum rid keyword;
16821 cp_decl_spec ds = ds_last;
16823 /* Assume this type-specifier does not declare a new type. */
16824 if (declares_class_or_enum)
16825 *declares_class_or_enum = 0;
16826 /* And that it does not specify a cv-qualifier. */
16827 if (is_cv_qualifier)
16828 *is_cv_qualifier = false;
16829 /* Peek at the next token. */
16830 token = cp_lexer_peek_token (parser->lexer);
16832 /* If we're looking at a keyword, we can use that to guide the
16833 production we choose. */
16834 keyword = token->keyword;
16835 switch (keyword)
16837 case RID_ENUM:
16838 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16839 goto elaborated_type_specifier;
16841 /* Look for the enum-specifier. */
16842 type_spec = cp_parser_enum_specifier (parser);
16843 /* If that worked, we're done. */
16844 if (type_spec)
16846 if (declares_class_or_enum)
16847 *declares_class_or_enum = 2;
16848 if (decl_specs)
16849 cp_parser_set_decl_spec_type (decl_specs,
16850 type_spec,
16851 token,
16852 /*type_definition_p=*/true);
16853 return type_spec;
16855 else
16856 goto elaborated_type_specifier;
16858 /* Any of these indicate either a class-specifier, or an
16859 elaborated-type-specifier. */
16860 case RID_CLASS:
16861 case RID_STRUCT:
16862 case RID_UNION:
16863 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16864 goto elaborated_type_specifier;
16866 /* Parse tentatively so that we can back up if we don't find a
16867 class-specifier. */
16868 cp_parser_parse_tentatively (parser);
16869 /* Look for the class-specifier. */
16870 type_spec = cp_parser_class_specifier (parser);
16871 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16872 /* If that worked, we're done. */
16873 if (cp_parser_parse_definitely (parser))
16875 if (declares_class_or_enum)
16876 *declares_class_or_enum = 2;
16877 if (decl_specs)
16878 cp_parser_set_decl_spec_type (decl_specs,
16879 type_spec,
16880 token,
16881 /*type_definition_p=*/true);
16882 return type_spec;
16885 /* Fall through. */
16886 elaborated_type_specifier:
16887 /* We're declaring (not defining) a class or enum. */
16888 if (declares_class_or_enum)
16889 *declares_class_or_enum = 1;
16891 /* Fall through. */
16892 case RID_TYPENAME:
16893 /* Look for an elaborated-type-specifier. */
16894 type_spec
16895 = (cp_parser_elaborated_type_specifier
16896 (parser,
16897 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16898 is_declaration));
16899 if (decl_specs)
16900 cp_parser_set_decl_spec_type (decl_specs,
16901 type_spec,
16902 token,
16903 /*type_definition_p=*/false);
16904 return type_spec;
16906 case RID_CONST:
16907 ds = ds_const;
16908 if (is_cv_qualifier)
16909 *is_cv_qualifier = true;
16910 break;
16912 case RID_VOLATILE:
16913 ds = ds_volatile;
16914 if (is_cv_qualifier)
16915 *is_cv_qualifier = true;
16916 break;
16918 case RID_RESTRICT:
16919 ds = ds_restrict;
16920 if (is_cv_qualifier)
16921 *is_cv_qualifier = true;
16922 break;
16924 case RID_COMPLEX:
16925 /* The `__complex__' keyword is a GNU extension. */
16926 ds = ds_complex;
16927 break;
16929 default:
16930 break;
16933 /* Handle simple keywords. */
16934 if (ds != ds_last)
16936 if (decl_specs)
16938 set_and_check_decl_spec_loc (decl_specs, ds, token);
16939 decl_specs->any_specifiers_p = true;
16941 return cp_lexer_consume_token (parser->lexer)->u.value;
16944 /* If we do not already have a type-specifier, assume we are looking
16945 at a simple-type-specifier. */
16946 type_spec = cp_parser_simple_type_specifier (parser,
16947 decl_specs,
16948 flags);
16950 /* If we didn't find a type-specifier, and a type-specifier was not
16951 optional in this context, issue an error message. */
16952 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16954 cp_parser_error (parser, "expected type specifier");
16955 return error_mark_node;
16958 return type_spec;
16961 /* Parse a simple-type-specifier.
16963 simple-type-specifier:
16964 :: [opt] nested-name-specifier [opt] type-name
16965 :: [opt] nested-name-specifier template template-id
16966 char
16967 wchar_t
16968 bool
16969 short
16971 long
16972 signed
16973 unsigned
16974 float
16975 double
16976 void
16978 C++11 Extension:
16980 simple-type-specifier:
16981 auto
16982 decltype ( expression )
16983 char16_t
16984 char32_t
16985 __underlying_type ( type-id )
16987 C++17 extension:
16989 nested-name-specifier(opt) template-name
16991 GNU Extension:
16993 simple-type-specifier:
16994 __int128
16995 __typeof__ unary-expression
16996 __typeof__ ( type-id )
16997 __typeof__ ( type-id ) { initializer-list , [opt] }
16999 Concepts Extension:
17001 simple-type-specifier:
17002 constrained-type-specifier
17004 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17005 appropriately updated. */
17007 static tree
17008 cp_parser_simple_type_specifier (cp_parser* parser,
17009 cp_decl_specifier_seq *decl_specs,
17010 cp_parser_flags flags)
17012 tree type = NULL_TREE;
17013 cp_token *token;
17014 int idx;
17016 /* Peek at the next token. */
17017 token = cp_lexer_peek_token (parser->lexer);
17019 /* If we're looking at a keyword, things are easy. */
17020 switch (token->keyword)
17022 case RID_CHAR:
17023 if (decl_specs)
17024 decl_specs->explicit_char_p = true;
17025 type = char_type_node;
17026 break;
17027 case RID_CHAR16:
17028 type = char16_type_node;
17029 break;
17030 case RID_CHAR32:
17031 type = char32_type_node;
17032 break;
17033 case RID_WCHAR:
17034 type = wchar_type_node;
17035 break;
17036 case RID_BOOL:
17037 type = boolean_type_node;
17038 break;
17039 case RID_SHORT:
17040 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17041 type = short_integer_type_node;
17042 break;
17043 case RID_INT:
17044 if (decl_specs)
17045 decl_specs->explicit_int_p = true;
17046 type = integer_type_node;
17047 break;
17048 case RID_INT_N_0:
17049 case RID_INT_N_1:
17050 case RID_INT_N_2:
17051 case RID_INT_N_3:
17052 idx = token->keyword - RID_INT_N_0;
17053 if (! int_n_enabled_p [idx])
17054 break;
17055 if (decl_specs)
17057 decl_specs->explicit_intN_p = true;
17058 decl_specs->int_n_idx = idx;
17060 type = int_n_trees [idx].signed_type;
17061 break;
17062 case RID_LONG:
17063 if (decl_specs)
17064 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17065 type = long_integer_type_node;
17066 break;
17067 case RID_SIGNED:
17068 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17069 type = integer_type_node;
17070 break;
17071 case RID_UNSIGNED:
17072 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17073 type = unsigned_type_node;
17074 break;
17075 case RID_FLOAT:
17076 type = float_type_node;
17077 break;
17078 case RID_DOUBLE:
17079 type = double_type_node;
17080 break;
17081 case RID_VOID:
17082 type = void_type_node;
17083 break;
17085 case RID_AUTO:
17086 maybe_warn_cpp0x (CPP0X_AUTO);
17087 if (parser->auto_is_implicit_function_template_parm_p)
17089 /* The 'auto' might be the placeholder return type for a function decl
17090 with trailing return type. */
17091 bool have_trailing_return_fn_decl = false;
17093 cp_parser_parse_tentatively (parser);
17094 cp_lexer_consume_token (parser->lexer);
17095 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17096 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17097 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17098 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17102 cp_lexer_consume_token (parser->lexer);
17103 cp_parser_skip_to_closing_parenthesis (parser,
17104 /*recovering*/false,
17105 /*or_comma*/false,
17106 /*consume_paren*/true);
17107 continue;
17110 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17112 have_trailing_return_fn_decl = true;
17113 break;
17116 cp_lexer_consume_token (parser->lexer);
17118 cp_parser_abort_tentative_parse (parser);
17120 if (have_trailing_return_fn_decl)
17122 type = make_auto ();
17123 break;
17126 if (cxx_dialect >= cxx14)
17128 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17129 type = TREE_TYPE (type);
17131 else
17132 type = error_mark_node;
17134 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17136 if (cxx_dialect < cxx14)
17137 error_at (token->location,
17138 "use of %<auto%> in lambda parameter declaration "
17139 "only available with "
17140 "-std=c++14 or -std=gnu++14");
17142 else if (cxx_dialect < cxx14)
17143 error_at (token->location,
17144 "use of %<auto%> in parameter declaration "
17145 "only available with "
17146 "-std=c++14 or -std=gnu++14");
17147 else if (!flag_concepts)
17148 pedwarn (token->location, 0,
17149 "use of %<auto%> in parameter declaration "
17150 "only available with -fconcepts");
17152 else
17153 type = make_auto ();
17154 break;
17156 case RID_DECLTYPE:
17157 /* Since DR 743, decltype can either be a simple-type-specifier by
17158 itself or begin a nested-name-specifier. Parsing it will replace
17159 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17160 handling below decide what to do. */
17161 cp_parser_decltype (parser);
17162 cp_lexer_set_token_position (parser->lexer, token);
17163 break;
17165 case RID_TYPEOF:
17166 /* Consume the `typeof' token. */
17167 cp_lexer_consume_token (parser->lexer);
17168 /* Parse the operand to `typeof'. */
17169 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17170 /* If it is not already a TYPE, take its type. */
17171 if (!TYPE_P (type))
17172 type = finish_typeof (type);
17174 if (decl_specs)
17175 cp_parser_set_decl_spec_type (decl_specs, type,
17176 token,
17177 /*type_definition_p=*/false);
17179 return type;
17181 case RID_UNDERLYING_TYPE:
17182 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17183 if (decl_specs)
17184 cp_parser_set_decl_spec_type (decl_specs, type,
17185 token,
17186 /*type_definition_p=*/false);
17188 return type;
17190 case RID_BASES:
17191 case RID_DIRECT_BASES:
17192 type = cp_parser_trait_expr (parser, token->keyword);
17193 if (decl_specs)
17194 cp_parser_set_decl_spec_type (decl_specs, type,
17195 token,
17196 /*type_definition_p=*/false);
17197 return type;
17198 default:
17199 break;
17202 /* If token is an already-parsed decltype not followed by ::,
17203 it's a simple-type-specifier. */
17204 if (token->type == CPP_DECLTYPE
17205 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17207 type = saved_checks_value (token->u.tree_check_value);
17208 if (decl_specs)
17210 cp_parser_set_decl_spec_type (decl_specs, type,
17211 token,
17212 /*type_definition_p=*/false);
17213 /* Remember that we are handling a decltype in order to
17214 implement the resolution of DR 1510 when the argument
17215 isn't instantiation dependent. */
17216 decl_specs->decltype_p = true;
17218 cp_lexer_consume_token (parser->lexer);
17219 return type;
17222 /* If the type-specifier was for a built-in type, we're done. */
17223 if (type)
17225 /* Record the type. */
17226 if (decl_specs
17227 && (token->keyword != RID_SIGNED
17228 && token->keyword != RID_UNSIGNED
17229 && token->keyword != RID_SHORT
17230 && token->keyword != RID_LONG))
17231 cp_parser_set_decl_spec_type (decl_specs,
17232 type,
17233 token,
17234 /*type_definition_p=*/false);
17235 if (decl_specs)
17236 decl_specs->any_specifiers_p = true;
17238 /* Consume the token. */
17239 cp_lexer_consume_token (parser->lexer);
17241 if (type == error_mark_node)
17242 return error_mark_node;
17244 /* There is no valid C++ program where a non-template type is
17245 followed by a "<". That usually indicates that the user thought
17246 that the type was a template. */
17247 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17248 token->location);
17250 return TYPE_NAME (type);
17253 /* The type-specifier must be a user-defined type. */
17254 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17256 bool qualified_p;
17257 bool global_p;
17259 /* Don't gobble tokens or issue error messages if this is an
17260 optional type-specifier. */
17261 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17262 cp_parser_parse_tentatively (parser);
17264 token = cp_lexer_peek_token (parser->lexer);
17266 /* Look for the optional `::' operator. */
17267 global_p
17268 = (cp_parser_global_scope_opt (parser,
17269 /*current_scope_valid_p=*/false)
17270 != NULL_TREE);
17271 /* Look for the nested-name specifier. */
17272 qualified_p
17273 = (cp_parser_nested_name_specifier_opt (parser,
17274 /*typename_keyword_p=*/false,
17275 /*check_dependency_p=*/true,
17276 /*type_p=*/false,
17277 /*is_declaration=*/false)
17278 != NULL_TREE);
17279 /* If we have seen a nested-name-specifier, and the next token
17280 is `template', then we are using the template-id production. */
17281 if (parser->scope
17282 && cp_parser_optional_template_keyword (parser))
17284 /* Look for the template-id. */
17285 type = cp_parser_template_id (parser,
17286 /*template_keyword_p=*/true,
17287 /*check_dependency_p=*/true,
17288 none_type,
17289 /*is_declaration=*/false);
17290 /* If the template-id did not name a type, we are out of
17291 luck. */
17292 if (TREE_CODE (type) != TYPE_DECL)
17294 cp_parser_error (parser, "expected template-id for type");
17295 type = NULL_TREE;
17298 /* Otherwise, look for a type-name. */
17299 else
17300 type = cp_parser_type_name (parser);
17301 /* Keep track of all name-lookups performed in class scopes. */
17302 if (type
17303 && !global_p
17304 && !qualified_p
17305 && TREE_CODE (type) == TYPE_DECL
17306 && identifier_p (DECL_NAME (type)))
17307 maybe_note_name_used_in_class (DECL_NAME (type), type);
17308 /* If it didn't work out, we don't have a TYPE. */
17309 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17310 && !cp_parser_parse_definitely (parser))
17311 type = NULL_TREE;
17312 if (!type && cxx_dialect >= cxx17)
17314 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17315 cp_parser_parse_tentatively (parser);
17317 cp_parser_global_scope_opt (parser,
17318 /*current_scope_valid_p=*/false);
17319 cp_parser_nested_name_specifier_opt (parser,
17320 /*typename_keyword_p=*/false,
17321 /*check_dependency_p=*/true,
17322 /*type_p=*/false,
17323 /*is_declaration=*/false);
17324 tree name = cp_parser_identifier (parser);
17325 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17326 && parser->scope != error_mark_node)
17328 tree tmpl = cp_parser_lookup_name (parser, name,
17329 none_type,
17330 /*is_template=*/false,
17331 /*is_namespace=*/false,
17332 /*check_dependency=*/true,
17333 /*ambiguous_decls=*/NULL,
17334 token->location);
17335 if (tmpl && tmpl != error_mark_node
17336 && (DECL_CLASS_TEMPLATE_P (tmpl)
17337 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17338 type = make_template_placeholder (tmpl);
17339 else
17341 type = error_mark_node;
17342 if (!cp_parser_simulate_error (parser))
17343 cp_parser_name_lookup_error (parser, name, tmpl,
17344 NLE_TYPE, token->location);
17347 else
17348 type = error_mark_node;
17350 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17351 && !cp_parser_parse_definitely (parser))
17352 type = NULL_TREE;
17354 if (type && decl_specs)
17355 cp_parser_set_decl_spec_type (decl_specs, type,
17356 token,
17357 /*type_definition_p=*/false);
17360 /* If we didn't get a type-name, issue an error message. */
17361 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17363 cp_parser_error (parser, "expected type-name");
17364 return error_mark_node;
17367 if (type && type != error_mark_node)
17369 /* See if TYPE is an Objective-C type, and if so, parse and
17370 accept any protocol references following it. Do this before
17371 the cp_parser_check_for_invalid_template_id() call, because
17372 Objective-C types can be followed by '<...>' which would
17373 enclose protocol names rather than template arguments, and so
17374 everything is fine. */
17375 if (c_dialect_objc () && !parser->scope
17376 && (objc_is_id (type) || objc_is_class_name (type)))
17378 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17379 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17381 /* Clobber the "unqualified" type previously entered into
17382 DECL_SPECS with the new, improved protocol-qualified version. */
17383 if (decl_specs)
17384 decl_specs->type = qual_type;
17386 return qual_type;
17389 /* There is no valid C++ program where a non-template type is
17390 followed by a "<". That usually indicates that the user
17391 thought that the type was a template. */
17392 cp_parser_check_for_invalid_template_id (parser, type,
17393 none_type,
17394 token->location);
17397 return type;
17400 /* Parse a type-name.
17402 type-name:
17403 class-name
17404 enum-name
17405 typedef-name
17406 simple-template-id [in c++0x]
17408 enum-name:
17409 identifier
17411 typedef-name:
17412 identifier
17414 Concepts:
17416 type-name:
17417 concept-name
17418 partial-concept-id
17420 concept-name:
17421 identifier
17423 Returns a TYPE_DECL for the type. */
17425 static tree
17426 cp_parser_type_name (cp_parser* parser)
17428 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17431 /* See above. */
17432 static tree
17433 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17435 tree type_decl;
17437 /* We can't know yet whether it is a class-name or not. */
17438 cp_parser_parse_tentatively (parser);
17439 /* Try a class-name. */
17440 type_decl = cp_parser_class_name (parser,
17441 typename_keyword_p,
17442 /*template_keyword_p=*/false,
17443 none_type,
17444 /*check_dependency_p=*/true,
17445 /*class_head_p=*/false,
17446 /*is_declaration=*/false);
17447 /* If it's not a class-name, keep looking. */
17448 if (!cp_parser_parse_definitely (parser))
17450 if (cxx_dialect < cxx11)
17451 /* It must be a typedef-name or an enum-name. */
17452 return cp_parser_nonclass_name (parser);
17454 cp_parser_parse_tentatively (parser);
17455 /* It is either a simple-template-id representing an
17456 instantiation of an alias template... */
17457 type_decl = cp_parser_template_id (parser,
17458 /*template_keyword_p=*/false,
17459 /*check_dependency_p=*/true,
17460 none_type,
17461 /*is_declaration=*/false);
17462 /* Note that this must be an instantiation of an alias template
17463 because [temp.names]/6 says:
17465 A template-id that names an alias template specialization
17466 is a type-name.
17468 Whereas [temp.names]/7 says:
17470 A simple-template-id that names a class template
17471 specialization is a class-name.
17473 With concepts, this could also be a partial-concept-id that
17474 declares a non-type template parameter. */
17475 if (type_decl != NULL_TREE
17476 && TREE_CODE (type_decl) == TYPE_DECL
17477 && TYPE_DECL_ALIAS_P (type_decl))
17478 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17479 else if (is_constrained_parameter (type_decl))
17480 /* Don't do anything. */ ;
17481 else
17482 cp_parser_simulate_error (parser);
17484 if (!cp_parser_parse_definitely (parser))
17485 /* ... Or a typedef-name or an enum-name. */
17486 return cp_parser_nonclass_name (parser);
17489 return type_decl;
17492 /* Check if DECL and ARGS can form a constrained-type-specifier.
17493 If ARGS is non-null, we try to form a concept check of the
17494 form DECL<?, ARGS> where ? is a wildcard that matches any
17495 kind of template argument. If ARGS is NULL, then we try to
17496 form a concept check of the form DECL<?>. */
17498 static tree
17499 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17500 tree decl, tree args)
17502 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17504 /* If we a constrained-type-specifier cannot be deduced. */
17505 if (parser->prevent_constrained_type_specifiers)
17506 return NULL_TREE;
17508 /* A constrained type specifier can only be found in an
17509 overload set or as a reference to a template declaration.
17511 FIXME: This might be masking a bug. It's possible that
17512 that the deduction below is causing template specializations
17513 to be formed with the wildcard as an argument. */
17514 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17515 return NULL_TREE;
17517 /* Try to build a call expression that evaluates the
17518 concept. This can fail if the overload set refers
17519 only to non-templates. */
17520 tree placeholder = build_nt (WILDCARD_DECL);
17521 tree check = build_concept_check (decl, placeholder, args);
17522 if (check == error_mark_node)
17523 return NULL_TREE;
17525 /* Deduce the checked constraint and the prototype parameter.
17527 FIXME: In certain cases, failure to deduce should be a
17528 diagnosable error. */
17529 tree conc;
17530 tree proto;
17531 if (!deduce_constrained_parameter (check, conc, proto))
17532 return NULL_TREE;
17534 /* In template parameter scope, this results in a constrained
17535 parameter. Return a descriptor of that parm. */
17536 if (processing_template_parmlist)
17537 return build_constrained_parameter (conc, proto, args);
17539 /* In a parameter-declaration-clause, constrained-type
17540 specifiers result in invented template parameters. */
17541 if (parser->auto_is_implicit_function_template_parm_p)
17543 tree x = build_constrained_parameter (conc, proto, args);
17544 return synthesize_implicit_template_parm (parser, x);
17546 else
17548 /* Otherwise, we're in a context where the constrained
17549 type name is deduced and the constraint applies
17550 after deduction. */
17551 return make_constrained_auto (conc, args);
17554 return NULL_TREE;
17557 /* If DECL refers to a concept, return a TYPE_DECL representing
17558 the result of using the constrained type specifier in the
17559 current context. DECL refers to a concept if
17561 - it is an overload set containing a function concept taking a single
17562 type argument, or
17564 - it is a variable concept taking a single type argument. */
17566 static tree
17567 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17569 if (flag_concepts
17570 && (TREE_CODE (decl) == OVERLOAD
17571 || BASELINK_P (decl)
17572 || variable_concept_p (decl)))
17573 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17574 else
17575 return NULL_TREE;
17578 /* Check if DECL and ARGS form a partial-concept-id. If so,
17579 assign ID to the resulting constrained placeholder.
17581 Returns true if the partial-concept-id designates a placeholder
17582 and false otherwise. Note that *id is set to NULL_TREE in
17583 this case. */
17585 static tree
17586 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17588 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17591 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17592 or a concept-name.
17594 enum-name:
17595 identifier
17597 typedef-name:
17598 identifier
17600 concept-name:
17601 identifier
17603 Returns a TYPE_DECL for the type. */
17605 static tree
17606 cp_parser_nonclass_name (cp_parser* parser)
17608 tree type_decl;
17609 tree identifier;
17611 cp_token *token = cp_lexer_peek_token (parser->lexer);
17612 identifier = cp_parser_identifier (parser);
17613 if (identifier == error_mark_node)
17614 return error_mark_node;
17616 /* Look up the type-name. */
17617 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17619 type_decl = strip_using_decl (type_decl);
17621 /* If we found an overload set, then it may refer to a concept-name. */
17622 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17623 type_decl = decl;
17625 if (TREE_CODE (type_decl) != TYPE_DECL
17626 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17628 /* See if this is an Objective-C type. */
17629 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17630 tree type = objc_get_protocol_qualified_type (identifier, protos);
17631 if (type)
17632 type_decl = TYPE_NAME (type);
17635 /* Issue an error if we did not find a type-name. */
17636 if (TREE_CODE (type_decl) != TYPE_DECL
17637 /* In Objective-C, we have the complication that class names are
17638 normally type names and start declarations (eg, the
17639 "NSObject" in "NSObject *object;"), but can be used in an
17640 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17641 is an expression. So, a classname followed by a dot is not a
17642 valid type-name. */
17643 || (objc_is_class_name (TREE_TYPE (type_decl))
17644 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17646 if (!cp_parser_simulate_error (parser))
17647 cp_parser_name_lookup_error (parser, identifier, type_decl,
17648 NLE_TYPE, token->location);
17649 return error_mark_node;
17651 /* Remember that the name was used in the definition of the
17652 current class so that we can check later to see if the
17653 meaning would have been different after the class was
17654 entirely defined. */
17655 else if (type_decl != error_mark_node
17656 && !parser->scope)
17657 maybe_note_name_used_in_class (identifier, type_decl);
17659 return type_decl;
17662 /* Parse an elaborated-type-specifier. Note that the grammar given
17663 here incorporates the resolution to DR68.
17665 elaborated-type-specifier:
17666 class-key :: [opt] nested-name-specifier [opt] identifier
17667 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17668 enum-key :: [opt] nested-name-specifier [opt] identifier
17669 typename :: [opt] nested-name-specifier identifier
17670 typename :: [opt] nested-name-specifier template [opt]
17671 template-id
17673 GNU extension:
17675 elaborated-type-specifier:
17676 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17677 class-key attributes :: [opt] nested-name-specifier [opt]
17678 template [opt] template-id
17679 enum attributes :: [opt] nested-name-specifier [opt] identifier
17681 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17682 declared `friend'. If IS_DECLARATION is TRUE, then this
17683 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17684 something is being declared.
17686 Returns the TYPE specified. */
17688 static tree
17689 cp_parser_elaborated_type_specifier (cp_parser* parser,
17690 bool is_friend,
17691 bool is_declaration)
17693 enum tag_types tag_type;
17694 tree identifier;
17695 tree type = NULL_TREE;
17696 tree attributes = NULL_TREE;
17697 tree globalscope;
17698 cp_token *token = NULL;
17700 /* See if we're looking at the `enum' keyword. */
17701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17703 /* Consume the `enum' token. */
17704 cp_lexer_consume_token (parser->lexer);
17705 /* Remember that it's an enumeration type. */
17706 tag_type = enum_type;
17707 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17708 enums) is used here. */
17709 cp_token *token = cp_lexer_peek_token (parser->lexer);
17710 if (cp_parser_is_keyword (token, RID_CLASS)
17711 || cp_parser_is_keyword (token, RID_STRUCT))
17713 gcc_rich_location richloc (token->location);
17714 richloc.add_range (input_location, false);
17715 richloc.add_fixit_remove ();
17716 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17717 "a scoped enum must not use the %qD keyword",
17718 token->u.value);
17719 /* Consume the `struct' or `class' and parse it anyway. */
17720 cp_lexer_consume_token (parser->lexer);
17722 /* Parse the attributes. */
17723 attributes = cp_parser_attributes_opt (parser);
17725 /* Or, it might be `typename'. */
17726 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17727 RID_TYPENAME))
17729 /* Consume the `typename' token. */
17730 cp_lexer_consume_token (parser->lexer);
17731 /* Remember that it's a `typename' type. */
17732 tag_type = typename_type;
17734 /* Otherwise it must be a class-key. */
17735 else
17737 tag_type = cp_parser_class_key (parser);
17738 if (tag_type == none_type)
17739 return error_mark_node;
17740 /* Parse the attributes. */
17741 attributes = cp_parser_attributes_opt (parser);
17744 /* Look for the `::' operator. */
17745 globalscope = cp_parser_global_scope_opt (parser,
17746 /*current_scope_valid_p=*/false);
17747 /* Look for the nested-name-specifier. */
17748 tree nested_name_specifier;
17749 if (tag_type == typename_type && !globalscope)
17751 nested_name_specifier
17752 = cp_parser_nested_name_specifier (parser,
17753 /*typename_keyword_p=*/true,
17754 /*check_dependency_p=*/true,
17755 /*type_p=*/true,
17756 is_declaration);
17757 if (!nested_name_specifier)
17758 return error_mark_node;
17760 else
17761 /* Even though `typename' is not present, the proposed resolution
17762 to Core Issue 180 says that in `class A<T>::B', `B' should be
17763 considered a type-name, even if `A<T>' is dependent. */
17764 nested_name_specifier
17765 = cp_parser_nested_name_specifier_opt (parser,
17766 /*typename_keyword_p=*/true,
17767 /*check_dependency_p=*/true,
17768 /*type_p=*/true,
17769 is_declaration);
17770 /* For everything but enumeration types, consider a template-id.
17771 For an enumeration type, consider only a plain identifier. */
17772 if (tag_type != enum_type)
17774 bool template_p = false;
17775 tree decl;
17777 /* Allow the `template' keyword. */
17778 template_p = cp_parser_optional_template_keyword (parser);
17779 /* If we didn't see `template', we don't know if there's a
17780 template-id or not. */
17781 if (!template_p)
17782 cp_parser_parse_tentatively (parser);
17783 /* Parse the template-id. */
17784 token = cp_lexer_peek_token (parser->lexer);
17785 decl = cp_parser_template_id (parser, template_p,
17786 /*check_dependency_p=*/true,
17787 tag_type,
17788 is_declaration);
17789 /* If we didn't find a template-id, look for an ordinary
17790 identifier. */
17791 if (!template_p && !cp_parser_parse_definitely (parser))
17793 /* We can get here when cp_parser_template_id, called by
17794 cp_parser_class_name with tag_type == none_type, succeeds
17795 and caches a BASELINK. Then, when called again here,
17796 instead of failing and returning an error_mark_node
17797 returns it (see template/typename17.C in C++11).
17798 ??? Could we diagnose this earlier? */
17799 else if (tag_type == typename_type && BASELINK_P (decl))
17801 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17802 type = error_mark_node;
17804 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17805 in effect, then we must assume that, upon instantiation, the
17806 template will correspond to a class. */
17807 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17808 && tag_type == typename_type)
17809 type = make_typename_type (parser->scope, decl,
17810 typename_type,
17811 /*complain=*/tf_error);
17812 /* If the `typename' keyword is in effect and DECL is not a type
17813 decl, then type is non existent. */
17814 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17816 else if (TREE_CODE (decl) == TYPE_DECL)
17818 type = check_elaborated_type_specifier (tag_type, decl,
17819 /*allow_template_p=*/true);
17821 /* If the next token is a semicolon, this must be a specialization,
17822 instantiation, or friend declaration. Check the scope while we
17823 still know whether or not we had a nested-name-specifier. */
17824 if (type != error_mark_node
17825 && !nested_name_specifier && !is_friend
17826 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17827 check_unqualified_spec_or_inst (type, token->location);
17829 else if (decl == error_mark_node)
17830 type = error_mark_node;
17833 if (!type)
17835 token = cp_lexer_peek_token (parser->lexer);
17836 identifier = cp_parser_identifier (parser);
17838 if (identifier == error_mark_node)
17840 parser->scope = NULL_TREE;
17841 return error_mark_node;
17844 /* For a `typename', we needn't call xref_tag. */
17845 if (tag_type == typename_type
17846 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17847 return cp_parser_make_typename_type (parser, identifier,
17848 token->location);
17850 /* Template parameter lists apply only if we are not within a
17851 function parameter list. */
17852 bool template_parm_lists_apply
17853 = parser->num_template_parameter_lists;
17854 if (template_parm_lists_apply)
17855 for (cp_binding_level *s = current_binding_level;
17856 s && s->kind != sk_template_parms;
17857 s = s->level_chain)
17858 if (s->kind == sk_function_parms)
17859 template_parm_lists_apply = false;
17861 /* Look up a qualified name in the usual way. */
17862 if (parser->scope)
17864 tree decl;
17865 tree ambiguous_decls;
17867 decl = cp_parser_lookup_name (parser, identifier,
17868 tag_type,
17869 /*is_template=*/false,
17870 /*is_namespace=*/false,
17871 /*check_dependency=*/true,
17872 &ambiguous_decls,
17873 token->location);
17875 /* If the lookup was ambiguous, an error will already have been
17876 issued. */
17877 if (ambiguous_decls)
17878 return error_mark_node;
17880 /* If we are parsing friend declaration, DECL may be a
17881 TEMPLATE_DECL tree node here. However, we need to check
17882 whether this TEMPLATE_DECL results in valid code. Consider
17883 the following example:
17885 namespace N {
17886 template <class T> class C {};
17888 class X {
17889 template <class T> friend class N::C; // #1, valid code
17891 template <class T> class Y {
17892 friend class N::C; // #2, invalid code
17895 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17896 name lookup of `N::C'. We see that friend declaration must
17897 be template for the code to be valid. Note that
17898 processing_template_decl does not work here since it is
17899 always 1 for the above two cases. */
17901 decl = (cp_parser_maybe_treat_template_as_class
17902 (decl, /*tag_name_p=*/is_friend
17903 && template_parm_lists_apply));
17905 if (TREE_CODE (decl) != TYPE_DECL)
17907 cp_parser_diagnose_invalid_type_name (parser,
17908 identifier,
17909 token->location);
17910 return error_mark_node;
17913 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17915 bool allow_template = (template_parm_lists_apply
17916 || DECL_SELF_REFERENCE_P (decl));
17917 type = check_elaborated_type_specifier (tag_type, decl,
17918 allow_template);
17920 if (type == error_mark_node)
17921 return error_mark_node;
17924 /* Forward declarations of nested types, such as
17926 class C1::C2;
17927 class C1::C2::C3;
17929 are invalid unless all components preceding the final '::'
17930 are complete. If all enclosing types are complete, these
17931 declarations become merely pointless.
17933 Invalid forward declarations of nested types are errors
17934 caught elsewhere in parsing. Those that are pointless arrive
17935 here. */
17937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17938 && !is_friend && !processing_explicit_instantiation)
17939 warning (0, "declaration %qD does not declare anything", decl);
17941 type = TREE_TYPE (decl);
17943 else
17945 /* An elaborated-type-specifier sometimes introduces a new type and
17946 sometimes names an existing type. Normally, the rule is that it
17947 introduces a new type only if there is not an existing type of
17948 the same name already in scope. For example, given:
17950 struct S {};
17951 void f() { struct S s; }
17953 the `struct S' in the body of `f' is the same `struct S' as in
17954 the global scope; the existing definition is used. However, if
17955 there were no global declaration, this would introduce a new
17956 local class named `S'.
17958 An exception to this rule applies to the following code:
17960 namespace N { struct S; }
17962 Here, the elaborated-type-specifier names a new type
17963 unconditionally; even if there is already an `S' in the
17964 containing scope this declaration names a new type.
17965 This exception only applies if the elaborated-type-specifier
17966 forms the complete declaration:
17968 [class.name]
17970 A declaration consisting solely of `class-key identifier ;' is
17971 either a redeclaration of the name in the current scope or a
17972 forward declaration of the identifier as a class name. It
17973 introduces the name into the current scope.
17975 We are in this situation precisely when the next token is a `;'.
17977 An exception to the exception is that a `friend' declaration does
17978 *not* name a new type; i.e., given:
17980 struct S { friend struct T; };
17982 `T' is not a new type in the scope of `S'.
17984 Also, `new struct S' or `sizeof (struct S)' never results in the
17985 definition of a new type; a new type can only be declared in a
17986 declaration context. */
17988 tag_scope ts;
17989 bool template_p;
17991 if (is_friend)
17992 /* Friends have special name lookup rules. */
17993 ts = ts_within_enclosing_non_class;
17994 else if (is_declaration
17995 && cp_lexer_next_token_is (parser->lexer,
17996 CPP_SEMICOLON))
17997 /* This is a `class-key identifier ;' */
17998 ts = ts_current;
17999 else
18000 ts = ts_global;
18002 template_p =
18003 (template_parm_lists_apply
18004 && (cp_parser_next_token_starts_class_definition_p (parser)
18005 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18006 /* An unqualified name was used to reference this type, so
18007 there were no qualifying templates. */
18008 if (template_parm_lists_apply
18009 && !cp_parser_check_template_parameters (parser,
18010 /*num_templates=*/0,
18011 /*template_id*/false,
18012 token->location,
18013 /*declarator=*/NULL))
18014 return error_mark_node;
18015 type = xref_tag (tag_type, identifier, ts, template_p);
18019 if (type == error_mark_node)
18020 return error_mark_node;
18022 /* Allow attributes on forward declarations of classes. */
18023 if (attributes)
18025 if (TREE_CODE (type) == TYPENAME_TYPE)
18026 warning (OPT_Wattributes,
18027 "attributes ignored on uninstantiated type");
18028 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18029 && ! processing_explicit_instantiation)
18030 warning (OPT_Wattributes,
18031 "attributes ignored on template instantiation");
18032 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18033 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18034 else
18035 warning (OPT_Wattributes,
18036 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18039 if (tag_type != enum_type)
18041 /* Indicate whether this class was declared as a `class' or as a
18042 `struct'. */
18043 if (CLASS_TYPE_P (type))
18044 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18045 cp_parser_check_class_key (tag_type, type);
18048 /* A "<" cannot follow an elaborated type specifier. If that
18049 happens, the user was probably trying to form a template-id. */
18050 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18051 token->location);
18053 return type;
18056 /* Parse an enum-specifier.
18058 enum-specifier:
18059 enum-head { enumerator-list [opt] }
18060 enum-head { enumerator-list , } [C++0x]
18062 enum-head:
18063 enum-key identifier [opt] enum-base [opt]
18064 enum-key nested-name-specifier identifier enum-base [opt]
18066 enum-key:
18067 enum
18068 enum class [C++0x]
18069 enum struct [C++0x]
18071 enum-base: [C++0x]
18072 : type-specifier-seq
18074 opaque-enum-specifier:
18075 enum-key identifier enum-base [opt] ;
18077 GNU Extensions:
18078 enum-key attributes[opt] identifier [opt] enum-base [opt]
18079 { enumerator-list [opt] }attributes[opt]
18080 enum-key attributes[opt] identifier [opt] enum-base [opt]
18081 { enumerator-list, }attributes[opt] [C++0x]
18083 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18084 if the token stream isn't an enum-specifier after all. */
18086 static tree
18087 cp_parser_enum_specifier (cp_parser* parser)
18089 tree identifier;
18090 tree type = NULL_TREE;
18091 tree prev_scope;
18092 tree nested_name_specifier = NULL_TREE;
18093 tree attributes;
18094 bool scoped_enum_p = false;
18095 bool has_underlying_type = false;
18096 bool nested_being_defined = false;
18097 bool new_value_list = false;
18098 bool is_new_type = false;
18099 bool is_unnamed = false;
18100 tree underlying_type = NULL_TREE;
18101 cp_token *type_start_token = NULL;
18102 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18104 parser->colon_corrects_to_scope_p = false;
18106 /* Parse tentatively so that we can back up if we don't find a
18107 enum-specifier. */
18108 cp_parser_parse_tentatively (parser);
18110 /* Caller guarantees that the current token is 'enum', an identifier
18111 possibly follows, and the token after that is an opening brace.
18112 If we don't have an identifier, fabricate an anonymous name for
18113 the enumeration being defined. */
18114 cp_lexer_consume_token (parser->lexer);
18116 /* Parse the "class" or "struct", which indicates a scoped
18117 enumeration type in C++0x. */
18118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18119 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18121 if (cxx_dialect < cxx11)
18122 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18124 /* Consume the `struct' or `class' token. */
18125 cp_lexer_consume_token (parser->lexer);
18127 scoped_enum_p = true;
18130 attributes = cp_parser_attributes_opt (parser);
18132 /* Clear the qualification. */
18133 parser->scope = NULL_TREE;
18134 parser->qualifying_scope = NULL_TREE;
18135 parser->object_scope = NULL_TREE;
18137 /* Figure out in what scope the declaration is being placed. */
18138 prev_scope = current_scope ();
18140 type_start_token = cp_lexer_peek_token (parser->lexer);
18142 push_deferring_access_checks (dk_no_check);
18143 nested_name_specifier
18144 = cp_parser_nested_name_specifier_opt (parser,
18145 /*typename_keyword_p=*/true,
18146 /*check_dependency_p=*/false,
18147 /*type_p=*/false,
18148 /*is_declaration=*/false);
18150 if (nested_name_specifier)
18152 tree name;
18154 identifier = cp_parser_identifier (parser);
18155 name = cp_parser_lookup_name (parser, identifier,
18156 enum_type,
18157 /*is_template=*/false,
18158 /*is_namespace=*/false,
18159 /*check_dependency=*/true,
18160 /*ambiguous_decls=*/NULL,
18161 input_location);
18162 if (name && name != error_mark_node)
18164 type = TREE_TYPE (name);
18165 if (TREE_CODE (type) == TYPENAME_TYPE)
18167 /* Are template enums allowed in ISO? */
18168 if (template_parm_scope_p ())
18169 pedwarn (type_start_token->location, OPT_Wpedantic,
18170 "%qD is an enumeration template", name);
18171 /* ignore a typename reference, for it will be solved by name
18172 in start_enum. */
18173 type = NULL_TREE;
18176 else if (nested_name_specifier == error_mark_node)
18177 /* We already issued an error. */;
18178 else
18180 error_at (type_start_token->location,
18181 "%qD does not name an enumeration in %qT",
18182 identifier, nested_name_specifier);
18183 nested_name_specifier = error_mark_node;
18186 else
18188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18189 identifier = cp_parser_identifier (parser);
18190 else
18192 identifier = make_anon_name ();
18193 is_unnamed = true;
18194 if (scoped_enum_p)
18195 error_at (type_start_token->location,
18196 "unnamed scoped enum is not allowed");
18199 pop_deferring_access_checks ();
18201 /* Check for the `:' that denotes a specified underlying type in C++0x.
18202 Note that a ':' could also indicate a bitfield width, however. */
18203 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18205 cp_decl_specifier_seq type_specifiers;
18207 /* Consume the `:'. */
18208 cp_lexer_consume_token (parser->lexer);
18210 /* Parse the type-specifier-seq. */
18211 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18212 /*is_trailing_return=*/false,
18213 &type_specifiers);
18215 /* At this point this is surely not elaborated type specifier. */
18216 if (!cp_parser_parse_definitely (parser))
18217 return NULL_TREE;
18219 if (cxx_dialect < cxx11)
18220 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18222 has_underlying_type = true;
18224 /* If that didn't work, stop. */
18225 if (type_specifiers.type != error_mark_node)
18227 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18228 /*initialized=*/0, NULL);
18229 if (underlying_type == error_mark_node
18230 || check_for_bare_parameter_packs (underlying_type))
18231 underlying_type = NULL_TREE;
18235 /* Look for the `{' but don't consume it yet. */
18236 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18238 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18240 cp_parser_error (parser, "expected %<{%>");
18241 if (has_underlying_type)
18243 type = NULL_TREE;
18244 goto out;
18247 /* An opaque-enum-specifier must have a ';' here. */
18248 if ((scoped_enum_p || underlying_type)
18249 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18251 cp_parser_error (parser, "expected %<;%> or %<{%>");
18252 if (has_underlying_type)
18254 type = NULL_TREE;
18255 goto out;
18260 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18261 return NULL_TREE;
18263 if (nested_name_specifier)
18265 if (CLASS_TYPE_P (nested_name_specifier))
18267 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18268 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18269 push_scope (nested_name_specifier);
18271 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18273 push_nested_namespace (nested_name_specifier);
18277 /* Issue an error message if type-definitions are forbidden here. */
18278 if (!cp_parser_check_type_definition (parser))
18279 type = error_mark_node;
18280 else
18281 /* Create the new type. We do this before consuming the opening
18282 brace so the enum will be recorded as being on the line of its
18283 tag (or the 'enum' keyword, if there is no tag). */
18284 type = start_enum (identifier, type, underlying_type,
18285 attributes, scoped_enum_p, &is_new_type);
18287 /* If the next token is not '{' it is an opaque-enum-specifier or an
18288 elaborated-type-specifier. */
18289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18291 timevar_push (TV_PARSE_ENUM);
18292 if (nested_name_specifier
18293 && nested_name_specifier != error_mark_node)
18295 /* The following catches invalid code such as:
18296 enum class S<int>::E { A, B, C }; */
18297 if (!processing_specialization
18298 && CLASS_TYPE_P (nested_name_specifier)
18299 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18300 error_at (type_start_token->location, "cannot add an enumerator "
18301 "list to a template instantiation");
18303 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18305 error_at (type_start_token->location,
18306 "%<%T::%E%> has not been declared",
18307 TYPE_CONTEXT (nested_name_specifier),
18308 nested_name_specifier);
18309 type = error_mark_node;
18311 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18312 && !CLASS_TYPE_P (nested_name_specifier))
18314 error_at (type_start_token->location, "nested name specifier "
18315 "%qT for enum declaration does not name a class "
18316 "or namespace", nested_name_specifier);
18317 type = error_mark_node;
18319 /* If that scope does not contain the scope in which the
18320 class was originally declared, the program is invalid. */
18321 else if (prev_scope && !is_ancestor (prev_scope,
18322 nested_name_specifier))
18324 if (at_namespace_scope_p ())
18325 error_at (type_start_token->location,
18326 "declaration of %qD in namespace %qD which does not "
18327 "enclose %qD",
18328 type, prev_scope, nested_name_specifier);
18329 else
18330 error_at (type_start_token->location,
18331 "declaration of %qD in %qD which does not "
18332 "enclose %qD",
18333 type, prev_scope, nested_name_specifier);
18334 type = error_mark_node;
18336 /* If that scope is the scope where the declaration is being placed
18337 the program is invalid. */
18338 else if (CLASS_TYPE_P (nested_name_specifier)
18339 && CLASS_TYPE_P (prev_scope)
18340 && same_type_p (nested_name_specifier, prev_scope))
18342 permerror (type_start_token->location,
18343 "extra qualification not allowed");
18344 nested_name_specifier = NULL_TREE;
18348 if (scoped_enum_p)
18349 begin_scope (sk_scoped_enum, type);
18351 /* Consume the opening brace. */
18352 matching_braces braces;
18353 braces.consume_open (parser);
18355 if (type == error_mark_node)
18356 ; /* Nothing to add */
18357 else if (OPAQUE_ENUM_P (type)
18358 || (cxx_dialect > cxx98 && processing_specialization))
18360 new_value_list = true;
18361 SET_OPAQUE_ENUM_P (type, false);
18362 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18364 else
18366 error_at (type_start_token->location,
18367 "multiple definition of %q#T", type);
18368 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18369 "previous definition here");
18370 type = error_mark_node;
18373 if (type == error_mark_node)
18374 cp_parser_skip_to_end_of_block_or_statement (parser);
18375 /* If the next token is not '}', then there are some enumerators. */
18376 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18378 if (is_unnamed && !scoped_enum_p)
18379 pedwarn (type_start_token->location, OPT_Wpedantic,
18380 "ISO C++ forbids empty unnamed enum");
18382 else
18383 cp_parser_enumerator_list (parser, type);
18385 /* Consume the final '}'. */
18386 braces.require_close (parser);
18388 if (scoped_enum_p)
18389 finish_scope ();
18390 timevar_pop (TV_PARSE_ENUM);
18392 else
18394 /* If a ';' follows, then it is an opaque-enum-specifier
18395 and additional restrictions apply. */
18396 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18398 if (is_unnamed)
18399 error_at (type_start_token->location,
18400 "opaque-enum-specifier without name");
18401 else if (nested_name_specifier)
18402 error_at (type_start_token->location,
18403 "opaque-enum-specifier must use a simple identifier");
18407 /* Look for trailing attributes to apply to this enumeration, and
18408 apply them if appropriate. */
18409 if (cp_parser_allow_gnu_extensions_p (parser))
18411 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18412 cplus_decl_attributes (&type,
18413 trailing_attr,
18414 (int) ATTR_FLAG_TYPE_IN_PLACE);
18417 /* Finish up the enumeration. */
18418 if (type != error_mark_node)
18420 if (new_value_list)
18421 finish_enum_value_list (type);
18422 if (is_new_type)
18423 finish_enum (type);
18426 if (nested_name_specifier)
18428 if (CLASS_TYPE_P (nested_name_specifier))
18430 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18431 pop_scope (nested_name_specifier);
18433 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18435 pop_nested_namespace (nested_name_specifier);
18438 out:
18439 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18440 return type;
18443 /* Parse an enumerator-list. The enumerators all have the indicated
18444 TYPE.
18446 enumerator-list:
18447 enumerator-definition
18448 enumerator-list , enumerator-definition */
18450 static void
18451 cp_parser_enumerator_list (cp_parser* parser, tree type)
18453 while (true)
18455 /* Parse an enumerator-definition. */
18456 cp_parser_enumerator_definition (parser, type);
18458 /* If the next token is not a ',', we've reached the end of
18459 the list. */
18460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18461 break;
18462 /* Otherwise, consume the `,' and keep going. */
18463 cp_lexer_consume_token (parser->lexer);
18464 /* If the next token is a `}', there is a trailing comma. */
18465 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18467 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18468 pedwarn (input_location, OPT_Wpedantic,
18469 "comma at end of enumerator list");
18470 break;
18475 /* Parse an enumerator-definition. The enumerator has the indicated
18476 TYPE.
18478 enumerator-definition:
18479 enumerator
18480 enumerator = constant-expression
18482 enumerator:
18483 identifier
18485 GNU Extensions:
18487 enumerator-definition:
18488 enumerator attributes [opt]
18489 enumerator attributes [opt] = constant-expression */
18491 static void
18492 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18494 tree identifier;
18495 tree value;
18496 location_t loc;
18498 /* Save the input location because we are interested in the location
18499 of the identifier and not the location of the explicit value. */
18500 loc = cp_lexer_peek_token (parser->lexer)->location;
18502 /* Look for the identifier. */
18503 identifier = cp_parser_identifier (parser);
18504 if (identifier == error_mark_node)
18505 return;
18507 /* Parse any specified attributes. */
18508 tree attrs = cp_parser_attributes_opt (parser);
18510 /* If the next token is an '=', then there is an explicit value. */
18511 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18513 /* Consume the `=' token. */
18514 cp_lexer_consume_token (parser->lexer);
18515 /* Parse the value. */
18516 value = cp_parser_constant_expression (parser);
18518 else
18519 value = NULL_TREE;
18521 /* If we are processing a template, make sure the initializer of the
18522 enumerator doesn't contain any bare template parameter pack. */
18523 if (check_for_bare_parameter_packs (value))
18524 value = error_mark_node;
18526 /* Create the enumerator. */
18527 build_enumerator (identifier, value, type, attrs, loc);
18530 /* Parse a namespace-name.
18532 namespace-name:
18533 original-namespace-name
18534 namespace-alias
18536 Returns the NAMESPACE_DECL for the namespace. */
18538 static tree
18539 cp_parser_namespace_name (cp_parser* parser)
18541 tree identifier;
18542 tree namespace_decl;
18544 cp_token *token = cp_lexer_peek_token (parser->lexer);
18546 /* Get the name of the namespace. */
18547 identifier = cp_parser_identifier (parser);
18548 if (identifier == error_mark_node)
18549 return error_mark_node;
18551 /* Look up the identifier in the currently active scope. Look only
18552 for namespaces, due to:
18554 [basic.lookup.udir]
18556 When looking up a namespace-name in a using-directive or alias
18557 definition, only namespace names are considered.
18559 And:
18561 [basic.lookup.qual]
18563 During the lookup of a name preceding the :: scope resolution
18564 operator, object, function, and enumerator names are ignored.
18566 (Note that cp_parser_qualifying_entity only calls this
18567 function if the token after the name is the scope resolution
18568 operator.) */
18569 namespace_decl = cp_parser_lookup_name (parser, identifier,
18570 none_type,
18571 /*is_template=*/false,
18572 /*is_namespace=*/true,
18573 /*check_dependency=*/true,
18574 /*ambiguous_decls=*/NULL,
18575 token->location);
18576 /* If it's not a namespace, issue an error. */
18577 if (namespace_decl == error_mark_node
18578 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18580 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18582 error_at (token->location, "%qD is not a namespace-name", identifier);
18583 if (namespace_decl == error_mark_node
18584 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18585 suggest_alternative_in_explicit_scope (token->location, identifier,
18586 parser->scope);
18588 cp_parser_error (parser, "expected namespace-name");
18589 namespace_decl = error_mark_node;
18592 return namespace_decl;
18595 /* Parse a namespace-definition.
18597 namespace-definition:
18598 named-namespace-definition
18599 unnamed-namespace-definition
18601 named-namespace-definition:
18602 original-namespace-definition
18603 extension-namespace-definition
18605 original-namespace-definition:
18606 namespace identifier { namespace-body }
18608 extension-namespace-definition:
18609 namespace original-namespace-name { namespace-body }
18611 unnamed-namespace-definition:
18612 namespace { namespace-body } */
18614 static void
18615 cp_parser_namespace_definition (cp_parser* parser)
18617 tree identifier;
18618 int nested_definition_count = 0;
18620 cp_ensure_no_omp_declare_simd (parser);
18621 cp_ensure_no_oacc_routine (parser);
18623 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18625 if (is_inline)
18627 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18628 cp_lexer_consume_token (parser->lexer);
18631 /* Look for the `namespace' keyword. */
18632 cp_token* token
18633 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18635 /* Parse any specified attributes before the identifier. */
18636 tree attribs = cp_parser_attributes_opt (parser);
18638 for (;;)
18640 identifier = NULL_TREE;
18642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18644 identifier = cp_parser_identifier (parser);
18646 if (cp_next_tokens_can_be_std_attribute_p (parser))
18647 pedwarn (input_location, OPT_Wpedantic,
18648 "standard attributes on namespaces must precede "
18649 "the namespace name");
18651 /* Parse any attributes specified after the identifier. */
18652 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18656 break;
18658 if (!nested_definition_count && cxx_dialect < cxx17)
18659 pedwarn (input_location, OPT_Wpedantic,
18660 "nested namespace definitions only available with "
18661 "-std=c++17 or -std=gnu++17");
18663 /* Nested namespace names can create new namespaces (unlike
18664 other qualified-ids). */
18665 if (int count = identifier ? push_namespace (identifier) : 0)
18666 nested_definition_count += count;
18667 else
18668 cp_parser_error (parser, "nested namespace name required");
18669 cp_lexer_consume_token (parser->lexer);
18672 if (nested_definition_count && !identifier)
18673 cp_parser_error (parser, "namespace name required");
18675 if (nested_definition_count && attribs)
18676 error_at (token->location,
18677 "a nested namespace definition cannot have attributes");
18678 if (nested_definition_count && is_inline)
18679 error_at (token->location,
18680 "a nested namespace definition cannot be inline");
18682 /* Start the namespace. */
18683 nested_definition_count += push_namespace (identifier, is_inline);
18685 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18687 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18689 /* Look for the `{' to validate starting the namespace. */
18690 matching_braces braces;
18691 if (braces.require_open (parser))
18693 /* Parse the body of the namespace. */
18694 cp_parser_namespace_body (parser);
18696 /* Look for the final `}'. */
18697 braces.require_close (parser);
18700 if (has_visibility)
18701 pop_visibility (1);
18703 /* Pop the nested namespace definitions. */
18704 while (nested_definition_count--)
18705 pop_namespace ();
18708 /* Parse a namespace-body.
18710 namespace-body:
18711 declaration-seq [opt] */
18713 static void
18714 cp_parser_namespace_body (cp_parser* parser)
18716 cp_parser_declaration_seq_opt (parser);
18719 /* Parse a namespace-alias-definition.
18721 namespace-alias-definition:
18722 namespace identifier = qualified-namespace-specifier ; */
18724 static void
18725 cp_parser_namespace_alias_definition (cp_parser* parser)
18727 tree identifier;
18728 tree namespace_specifier;
18730 cp_token *token = cp_lexer_peek_token (parser->lexer);
18732 /* Look for the `namespace' keyword. */
18733 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18734 /* Look for the identifier. */
18735 identifier = cp_parser_identifier (parser);
18736 if (identifier == error_mark_node)
18737 return;
18738 /* Look for the `=' token. */
18739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18740 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18742 error_at (token->location, "%<namespace%> definition is not allowed here");
18743 /* Skip the definition. */
18744 cp_lexer_consume_token (parser->lexer);
18745 if (cp_parser_skip_to_closing_brace (parser))
18746 cp_lexer_consume_token (parser->lexer);
18747 return;
18749 cp_parser_require (parser, CPP_EQ, RT_EQ);
18750 /* Look for the qualified-namespace-specifier. */
18751 namespace_specifier
18752 = cp_parser_qualified_namespace_specifier (parser);
18753 /* Look for the `;' token. */
18754 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18756 /* Register the alias in the symbol table. */
18757 do_namespace_alias (identifier, namespace_specifier);
18760 /* Parse a qualified-namespace-specifier.
18762 qualified-namespace-specifier:
18763 :: [opt] nested-name-specifier [opt] namespace-name
18765 Returns a NAMESPACE_DECL corresponding to the specified
18766 namespace. */
18768 static tree
18769 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18771 /* Look for the optional `::'. */
18772 cp_parser_global_scope_opt (parser,
18773 /*current_scope_valid_p=*/false);
18775 /* Look for the optional nested-name-specifier. */
18776 cp_parser_nested_name_specifier_opt (parser,
18777 /*typename_keyword_p=*/false,
18778 /*check_dependency_p=*/true,
18779 /*type_p=*/false,
18780 /*is_declaration=*/true);
18782 return cp_parser_namespace_name (parser);
18785 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18786 access declaration.
18788 using-declaration:
18789 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18790 using :: unqualified-id ;
18792 access-declaration:
18793 qualified-id ;
18797 static bool
18798 cp_parser_using_declaration (cp_parser* parser,
18799 bool access_declaration_p)
18801 cp_token *token;
18802 bool typename_p = false;
18803 bool global_scope_p;
18804 tree decl;
18805 tree identifier;
18806 tree qscope;
18807 int oldcount = errorcount;
18808 cp_token *diag_token = NULL;
18810 if (access_declaration_p)
18812 diag_token = cp_lexer_peek_token (parser->lexer);
18813 cp_parser_parse_tentatively (parser);
18815 else
18817 /* Look for the `using' keyword. */
18818 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18820 again:
18821 /* Peek at the next token. */
18822 token = cp_lexer_peek_token (parser->lexer);
18823 /* See if it's `typename'. */
18824 if (token->keyword == RID_TYPENAME)
18826 /* Remember that we've seen it. */
18827 typename_p = true;
18828 /* Consume the `typename' token. */
18829 cp_lexer_consume_token (parser->lexer);
18833 /* Look for the optional global scope qualification. */
18834 global_scope_p
18835 = (cp_parser_global_scope_opt (parser,
18836 /*current_scope_valid_p=*/false)
18837 != NULL_TREE);
18839 /* If we saw `typename', or didn't see `::', then there must be a
18840 nested-name-specifier present. */
18841 if (typename_p || !global_scope_p)
18843 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18844 /*check_dependency_p=*/true,
18845 /*type_p=*/false,
18846 /*is_declaration=*/true);
18847 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18849 cp_parser_skip_to_end_of_block_or_statement (parser);
18850 return false;
18853 /* Otherwise, we could be in either of the two productions. In that
18854 case, treat the nested-name-specifier as optional. */
18855 else
18856 qscope = cp_parser_nested_name_specifier_opt (parser,
18857 /*typename_keyword_p=*/false,
18858 /*check_dependency_p=*/true,
18859 /*type_p=*/false,
18860 /*is_declaration=*/true);
18861 if (!qscope)
18862 qscope = global_namespace;
18863 else if (UNSCOPED_ENUM_P (qscope))
18864 qscope = CP_TYPE_CONTEXT (qscope);
18866 if (access_declaration_p && cp_parser_error_occurred (parser))
18867 /* Something has already gone wrong; there's no need to parse
18868 further. Since an error has occurred, the return value of
18869 cp_parser_parse_definitely will be false, as required. */
18870 return cp_parser_parse_definitely (parser);
18872 token = cp_lexer_peek_token (parser->lexer);
18873 /* Parse the unqualified-id. */
18874 identifier = cp_parser_unqualified_id (parser,
18875 /*template_keyword_p=*/false,
18876 /*check_dependency_p=*/true,
18877 /*declarator_p=*/true,
18878 /*optional_p=*/false);
18880 if (access_declaration_p)
18882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18883 cp_parser_simulate_error (parser);
18884 if (!cp_parser_parse_definitely (parser))
18885 return false;
18887 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18889 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18890 if (cxx_dialect < cxx17
18891 && !in_system_header_at (ell->location))
18892 pedwarn (ell->location, 0,
18893 "pack expansion in using-declaration only available "
18894 "with -std=c++17 or -std=gnu++17");
18895 qscope = make_pack_expansion (qscope);
18898 /* The function we call to handle a using-declaration is different
18899 depending on what scope we are in. */
18900 if (qscope == error_mark_node || identifier == error_mark_node)
18902 else if (!identifier_p (identifier)
18903 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18904 /* [namespace.udecl]
18906 A using declaration shall not name a template-id. */
18907 error_at (token->location,
18908 "a template-id may not appear in a using-declaration");
18909 else
18911 if (at_class_scope_p ())
18913 /* Create the USING_DECL. */
18914 decl = do_class_using_decl (qscope, identifier);
18916 if (decl && typename_p)
18917 USING_DECL_TYPENAME_P (decl) = 1;
18919 if (check_for_bare_parameter_packs (decl))
18921 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18922 return false;
18924 else
18925 /* Add it to the list of members in this class. */
18926 finish_member_declaration (decl);
18928 else
18930 decl = cp_parser_lookup_name_simple (parser,
18931 identifier,
18932 token->location);
18933 if (decl == error_mark_node)
18934 cp_parser_name_lookup_error (parser, identifier,
18935 decl, NLE_NULL,
18936 token->location);
18937 else if (check_for_bare_parameter_packs (decl))
18939 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18940 return false;
18942 else if (!at_namespace_scope_p ())
18943 finish_local_using_decl (decl, qscope, identifier);
18944 else
18945 finish_namespace_using_decl (decl, qscope, identifier);
18949 if (!access_declaration_p
18950 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18952 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18953 if (cxx_dialect < cxx17)
18954 pedwarn (comma->location, 0,
18955 "comma-separated list in using-declaration only available "
18956 "with -std=c++17 or -std=gnu++17");
18957 goto again;
18960 /* Look for the final `;'. */
18961 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18963 if (access_declaration_p && errorcount == oldcount)
18964 warning_at (diag_token->location, OPT_Wdeprecated,
18965 "access declarations are deprecated "
18966 "in favour of using-declarations; "
18967 "suggestion: add the %<using%> keyword");
18969 return true;
18972 /* Parse an alias-declaration.
18974 alias-declaration:
18975 using identifier attribute-specifier-seq [opt] = type-id */
18977 static tree
18978 cp_parser_alias_declaration (cp_parser* parser)
18980 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18981 location_t id_location;
18982 cp_declarator *declarator;
18983 cp_decl_specifier_seq decl_specs;
18984 bool member_p;
18985 const char *saved_message = NULL;
18987 /* Look for the `using' keyword. */
18988 cp_token *using_token
18989 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18990 if (using_token == NULL)
18991 return error_mark_node;
18993 id_location = cp_lexer_peek_token (parser->lexer)->location;
18994 id = cp_parser_identifier (parser);
18995 if (id == error_mark_node)
18996 return error_mark_node;
18998 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18999 attributes = cp_parser_attributes_opt (parser);
19000 if (attributes == error_mark_node)
19001 return error_mark_node;
19003 cp_parser_require (parser, CPP_EQ, RT_EQ);
19005 if (cp_parser_error_occurred (parser))
19006 return error_mark_node;
19008 cp_parser_commit_to_tentative_parse (parser);
19010 /* Now we are going to parse the type-id of the declaration. */
19013 [dcl.type]/3 says:
19015 "A type-specifier-seq shall not define a class or enumeration
19016 unless it appears in the type-id of an alias-declaration (7.1.3) that
19017 is not the declaration of a template-declaration."
19019 In other words, if we currently are in an alias template, the
19020 type-id should not define a type.
19022 So let's set parser->type_definition_forbidden_message in that
19023 case; cp_parser_check_type_definition (called by
19024 cp_parser_class_specifier) will then emit an error if a type is
19025 defined in the type-id. */
19026 if (parser->num_template_parameter_lists)
19028 saved_message = parser->type_definition_forbidden_message;
19029 parser->type_definition_forbidden_message =
19030 G_("types may not be defined in alias template declarations");
19033 type = cp_parser_type_id (parser);
19035 /* Restore the error message if need be. */
19036 if (parser->num_template_parameter_lists)
19037 parser->type_definition_forbidden_message = saved_message;
19039 if (type == error_mark_node
19040 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19042 cp_parser_skip_to_end_of_block_or_statement (parser);
19043 return error_mark_node;
19046 /* A typedef-name can also be introduced by an alias-declaration. The
19047 identifier following the using keyword becomes a typedef-name. It has
19048 the same semantics as if it were introduced by the typedef
19049 specifier. In particular, it does not define a new type and it shall
19050 not appear in the type-id. */
19052 clear_decl_specs (&decl_specs);
19053 decl_specs.type = type;
19054 if (attributes != NULL_TREE)
19056 decl_specs.attributes = attributes;
19057 set_and_check_decl_spec_loc (&decl_specs,
19058 ds_attribute,
19059 attrs_token);
19061 set_and_check_decl_spec_loc (&decl_specs,
19062 ds_typedef,
19063 using_token);
19064 set_and_check_decl_spec_loc (&decl_specs,
19065 ds_alias,
19066 using_token);
19068 if (parser->num_template_parameter_lists
19069 && !cp_parser_check_template_parameters (parser,
19070 /*num_templates=*/0,
19071 /*template_id*/false,
19072 id_location,
19073 /*declarator=*/NULL))
19074 return error_mark_node;
19076 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19077 declarator->id_loc = id_location;
19079 member_p = at_class_scope_p ();
19080 if (member_p)
19081 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19082 NULL_TREE, attributes);
19083 else
19084 decl = start_decl (declarator, &decl_specs, 0,
19085 attributes, NULL_TREE, &pushed_scope);
19086 if (decl == error_mark_node)
19087 return decl;
19089 // Attach constraints to the alias declaration.
19090 if (flag_concepts && current_template_parms)
19092 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19093 tree constr = build_constraints (reqs, NULL_TREE);
19094 set_constraints (decl, constr);
19097 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19099 if (pushed_scope)
19100 pop_scope (pushed_scope);
19102 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19103 added into the symbol table; otherwise, return the TYPE_DECL. */
19104 if (DECL_LANG_SPECIFIC (decl)
19105 && DECL_TEMPLATE_INFO (decl)
19106 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19108 decl = DECL_TI_TEMPLATE (decl);
19109 if (member_p)
19110 check_member_template (decl);
19113 return decl;
19116 /* Parse a using-directive.
19118 using-directive:
19119 using namespace :: [opt] nested-name-specifier [opt]
19120 namespace-name ; */
19122 static void
19123 cp_parser_using_directive (cp_parser* parser)
19125 tree namespace_decl;
19126 tree attribs;
19128 /* Look for the `using' keyword. */
19129 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19130 /* And the `namespace' keyword. */
19131 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19132 /* Look for the optional `::' operator. */
19133 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19134 /* And the optional nested-name-specifier. */
19135 cp_parser_nested_name_specifier_opt (parser,
19136 /*typename_keyword_p=*/false,
19137 /*check_dependency_p=*/true,
19138 /*type_p=*/false,
19139 /*is_declaration=*/true);
19140 /* Get the namespace being used. */
19141 namespace_decl = cp_parser_namespace_name (parser);
19142 /* And any specified attributes. */
19143 attribs = cp_parser_attributes_opt (parser);
19145 /* Update the symbol table. */
19146 if (namespace_bindings_p ())
19147 finish_namespace_using_directive (namespace_decl, attribs);
19148 else
19149 finish_local_using_directive (namespace_decl, attribs);
19151 /* Look for the final `;'. */
19152 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19155 /* Parse an asm-definition.
19157 asm-definition:
19158 asm ( string-literal ) ;
19160 GNU Extension:
19162 asm-definition:
19163 asm volatile [opt] ( string-literal ) ;
19164 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19165 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19166 : asm-operand-list [opt] ) ;
19167 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19168 : asm-operand-list [opt]
19169 : asm-clobber-list [opt] ) ;
19170 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19171 : asm-clobber-list [opt]
19172 : asm-goto-list ) ; */
19174 static void
19175 cp_parser_asm_definition (cp_parser* parser)
19177 tree string;
19178 tree outputs = NULL_TREE;
19179 tree inputs = NULL_TREE;
19180 tree clobbers = NULL_TREE;
19181 tree labels = NULL_TREE;
19182 tree asm_stmt;
19183 bool volatile_p = false;
19184 bool extended_p = false;
19185 bool invalid_inputs_p = false;
19186 bool invalid_outputs_p = false;
19187 bool goto_p = false;
19188 required_token missing = RT_NONE;
19190 /* Look for the `asm' keyword. */
19191 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19193 if (parser->in_function_body
19194 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19196 error ("%<asm%> in %<constexpr%> function");
19197 cp_function_chain->invalid_constexpr = true;
19200 /* See if the next token is `volatile'. */
19201 if (cp_parser_allow_gnu_extensions_p (parser)
19202 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19204 /* Remember that we saw the `volatile' keyword. */
19205 volatile_p = true;
19206 /* Consume the token. */
19207 cp_lexer_consume_token (parser->lexer);
19209 if (cp_parser_allow_gnu_extensions_p (parser)
19210 && parser->in_function_body
19211 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19213 /* Remember that we saw the `goto' keyword. */
19214 goto_p = true;
19215 /* Consume the token. */
19216 cp_lexer_consume_token (parser->lexer);
19218 /* Look for the opening `('. */
19219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19220 return;
19221 /* Look for the string. */
19222 string = cp_parser_string_literal (parser, false, false);
19223 if (string == error_mark_node)
19225 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19226 /*consume_paren=*/true);
19227 return;
19230 /* If we're allowing GNU extensions, check for the extended assembly
19231 syntax. Unfortunately, the `:' tokens need not be separated by
19232 a space in C, and so, for compatibility, we tolerate that here
19233 too. Doing that means that we have to treat the `::' operator as
19234 two `:' tokens. */
19235 if (cp_parser_allow_gnu_extensions_p (parser)
19236 && parser->in_function_body
19237 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19238 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19240 bool inputs_p = false;
19241 bool clobbers_p = false;
19242 bool labels_p = false;
19244 /* The extended syntax was used. */
19245 extended_p = true;
19247 /* Look for outputs. */
19248 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19250 /* Consume the `:'. */
19251 cp_lexer_consume_token (parser->lexer);
19252 /* Parse the output-operands. */
19253 if (cp_lexer_next_token_is_not (parser->lexer,
19254 CPP_COLON)
19255 && cp_lexer_next_token_is_not (parser->lexer,
19256 CPP_SCOPE)
19257 && cp_lexer_next_token_is_not (parser->lexer,
19258 CPP_CLOSE_PAREN)
19259 && !goto_p)
19261 outputs = cp_parser_asm_operand_list (parser);
19262 if (outputs == error_mark_node)
19263 invalid_outputs_p = true;
19266 /* If the next token is `::', there are no outputs, and the
19267 next token is the beginning of the inputs. */
19268 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19269 /* The inputs are coming next. */
19270 inputs_p = true;
19272 /* Look for inputs. */
19273 if (inputs_p
19274 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19276 /* Consume the `:' or `::'. */
19277 cp_lexer_consume_token (parser->lexer);
19278 /* Parse the output-operands. */
19279 if (cp_lexer_next_token_is_not (parser->lexer,
19280 CPP_COLON)
19281 && cp_lexer_next_token_is_not (parser->lexer,
19282 CPP_SCOPE)
19283 && cp_lexer_next_token_is_not (parser->lexer,
19284 CPP_CLOSE_PAREN))
19286 inputs = cp_parser_asm_operand_list (parser);
19287 if (inputs == error_mark_node)
19288 invalid_inputs_p = true;
19291 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19292 /* The clobbers are coming next. */
19293 clobbers_p = true;
19295 /* Look for clobbers. */
19296 if (clobbers_p
19297 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19299 clobbers_p = true;
19300 /* Consume the `:' or `::'. */
19301 cp_lexer_consume_token (parser->lexer);
19302 /* Parse the clobbers. */
19303 if (cp_lexer_next_token_is_not (parser->lexer,
19304 CPP_COLON)
19305 && cp_lexer_next_token_is_not (parser->lexer,
19306 CPP_CLOSE_PAREN))
19307 clobbers = cp_parser_asm_clobber_list (parser);
19309 else if (goto_p
19310 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19311 /* The labels are coming next. */
19312 labels_p = true;
19314 /* Look for labels. */
19315 if (labels_p
19316 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19318 labels_p = true;
19319 /* Consume the `:' or `::'. */
19320 cp_lexer_consume_token (parser->lexer);
19321 /* Parse the labels. */
19322 labels = cp_parser_asm_label_list (parser);
19325 if (goto_p && !labels_p)
19326 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19328 else if (goto_p)
19329 missing = RT_COLON_SCOPE;
19331 /* Look for the closing `)'. */
19332 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19333 missing ? missing : RT_CLOSE_PAREN))
19334 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19335 /*consume_paren=*/true);
19336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19338 if (!invalid_inputs_p && !invalid_outputs_p)
19340 /* Create the ASM_EXPR. */
19341 if (parser->in_function_body)
19343 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19344 inputs, clobbers, labels);
19345 /* If the extended syntax was not used, mark the ASM_EXPR. */
19346 if (!extended_p)
19348 tree temp = asm_stmt;
19349 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19350 temp = TREE_OPERAND (temp, 0);
19352 ASM_INPUT_P (temp) = 1;
19355 else
19356 symtab->finalize_toplevel_asm (string);
19360 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19361 type that comes from the decl-specifier-seq. */
19363 static tree
19364 strip_declarator_types (tree type, cp_declarator *declarator)
19366 for (cp_declarator *d = declarator; d;)
19367 switch (d->kind)
19369 case cdk_id:
19370 case cdk_decomp:
19371 case cdk_error:
19372 d = NULL;
19373 break;
19375 default:
19376 if (TYPE_PTRMEMFUNC_P (type))
19377 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19378 type = TREE_TYPE (type);
19379 d = d->declarator;
19380 break;
19383 return type;
19386 /* Declarators [gram.dcl.decl] */
19388 /* Parse an init-declarator.
19390 init-declarator:
19391 declarator initializer [opt]
19393 GNU Extension:
19395 init-declarator:
19396 declarator asm-specification [opt] attributes [opt] initializer [opt]
19398 function-definition:
19399 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19400 function-body
19401 decl-specifier-seq [opt] declarator function-try-block
19403 GNU Extension:
19405 function-definition:
19406 __extension__ function-definition
19408 TM Extension:
19410 function-definition:
19411 decl-specifier-seq [opt] declarator function-transaction-block
19413 The DECL_SPECIFIERS apply to this declarator. Returns a
19414 representation of the entity declared. If MEMBER_P is TRUE, then
19415 this declarator appears in a class scope. The new DECL created by
19416 this declarator is returned.
19418 The CHECKS are access checks that should be performed once we know
19419 what entity is being declared (and, therefore, what classes have
19420 befriended it).
19422 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19423 for a function-definition here as well. If the declarator is a
19424 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19425 be TRUE upon return. By that point, the function-definition will
19426 have been completely parsed.
19428 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19429 is FALSE.
19431 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19432 parsed declaration if it is an uninitialized single declarator not followed
19433 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19434 if present, will not be consumed. If returned, this declarator will be
19435 created with SD_INITIALIZED but will not call cp_finish_decl.
19437 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19438 and there is an initializer, the pointed location_t is set to the
19439 location of the '=' or `(', or '{' in C++11 token introducing the
19440 initializer. */
19442 static tree
19443 cp_parser_init_declarator (cp_parser* parser,
19444 cp_decl_specifier_seq *decl_specifiers,
19445 vec<deferred_access_check, va_gc> *checks,
19446 bool function_definition_allowed_p,
19447 bool member_p,
19448 int declares_class_or_enum,
19449 bool* function_definition_p,
19450 tree* maybe_range_for_decl,
19451 location_t* init_loc,
19452 tree* auto_result)
19454 cp_token *token = NULL, *asm_spec_start_token = NULL,
19455 *attributes_start_token = NULL;
19456 cp_declarator *declarator;
19457 tree prefix_attributes;
19458 tree attributes = NULL;
19459 tree asm_specification;
19460 tree initializer;
19461 tree decl = NULL_TREE;
19462 tree scope;
19463 int is_initialized;
19464 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19465 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19466 "(...)". */
19467 enum cpp_ttype initialization_kind;
19468 bool is_direct_init = false;
19469 bool is_non_constant_init;
19470 int ctor_dtor_or_conv_p;
19471 bool friend_p = cp_parser_friend_p (decl_specifiers);
19472 tree pushed_scope = NULL_TREE;
19473 bool range_for_decl_p = false;
19474 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19475 location_t tmp_init_loc = UNKNOWN_LOCATION;
19477 /* Gather the attributes that were provided with the
19478 decl-specifiers. */
19479 prefix_attributes = decl_specifiers->attributes;
19481 /* Assume that this is not the declarator for a function
19482 definition. */
19483 if (function_definition_p)
19484 *function_definition_p = false;
19486 /* Default arguments are only permitted for function parameters. */
19487 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19488 parser->default_arg_ok_p = false;
19490 /* Defer access checks while parsing the declarator; we cannot know
19491 what names are accessible until we know what is being
19492 declared. */
19493 resume_deferring_access_checks ();
19495 token = cp_lexer_peek_token (parser->lexer);
19497 /* Parse the declarator. */
19498 declarator
19499 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19500 &ctor_dtor_or_conv_p,
19501 /*parenthesized_p=*/NULL,
19502 member_p, friend_p);
19503 /* Gather up the deferred checks. */
19504 stop_deferring_access_checks ();
19506 parser->default_arg_ok_p = saved_default_arg_ok_p;
19508 /* If the DECLARATOR was erroneous, there's no need to go
19509 further. */
19510 if (declarator == cp_error_declarator)
19511 return error_mark_node;
19513 /* Check that the number of template-parameter-lists is OK. */
19514 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19515 token->location))
19516 return error_mark_node;
19518 if (declares_class_or_enum & 2)
19519 cp_parser_check_for_definition_in_return_type (declarator,
19520 decl_specifiers->type,
19521 decl_specifiers->locations[ds_type_spec]);
19523 /* Figure out what scope the entity declared by the DECLARATOR is
19524 located in. `grokdeclarator' sometimes changes the scope, so
19525 we compute it now. */
19526 scope = get_scope_of_declarator (declarator);
19528 /* Perform any lookups in the declared type which were thought to be
19529 dependent, but are not in the scope of the declarator. */
19530 decl_specifiers->type
19531 = maybe_update_decl_type (decl_specifiers->type, scope);
19533 /* If we're allowing GNU extensions, look for an
19534 asm-specification. */
19535 if (cp_parser_allow_gnu_extensions_p (parser))
19537 /* Look for an asm-specification. */
19538 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19539 asm_specification = cp_parser_asm_specification_opt (parser);
19541 else
19542 asm_specification = NULL_TREE;
19544 /* Look for attributes. */
19545 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19546 attributes = cp_parser_attributes_opt (parser);
19548 /* Peek at the next token. */
19549 token = cp_lexer_peek_token (parser->lexer);
19551 bool bogus_implicit_tmpl = false;
19553 if (function_declarator_p (declarator))
19555 /* Handle C++17 deduction guides. */
19556 if (!decl_specifiers->type
19557 && ctor_dtor_or_conv_p <= 0
19558 && cxx_dialect >= cxx17)
19560 cp_declarator *id = get_id_declarator (declarator);
19561 tree name = id->u.id.unqualified_name;
19562 parser->scope = id->u.id.qualifying_scope;
19563 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19564 if (tmpl
19565 && (DECL_CLASS_TEMPLATE_P (tmpl)
19566 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19568 id->u.id.unqualified_name = dguide_name (tmpl);
19569 id->u.id.sfk = sfk_deduction_guide;
19570 ctor_dtor_or_conv_p = 1;
19574 /* Check to see if the token indicates the start of a
19575 function-definition. */
19576 if (cp_parser_token_starts_function_definition_p (token))
19578 if (!function_definition_allowed_p)
19580 /* If a function-definition should not appear here, issue an
19581 error message. */
19582 cp_parser_error (parser,
19583 "a function-definition is not allowed here");
19584 return error_mark_node;
19587 location_t func_brace_location
19588 = cp_lexer_peek_token (parser->lexer)->location;
19590 /* Neither attributes nor an asm-specification are allowed
19591 on a function-definition. */
19592 if (asm_specification)
19593 error_at (asm_spec_start_token->location,
19594 "an asm-specification is not allowed "
19595 "on a function-definition");
19596 if (attributes)
19597 error_at (attributes_start_token->location,
19598 "attributes are not allowed "
19599 "on a function-definition");
19600 /* This is a function-definition. */
19601 *function_definition_p = true;
19603 /* Parse the function definition. */
19604 if (member_p)
19605 decl = cp_parser_save_member_function_body (parser,
19606 decl_specifiers,
19607 declarator,
19608 prefix_attributes);
19609 else
19610 decl =
19611 (cp_parser_function_definition_from_specifiers_and_declarator
19612 (parser, decl_specifiers, prefix_attributes, declarator));
19614 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19616 /* This is where the prologue starts... */
19617 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19618 = func_brace_location;
19621 return decl;
19624 else if (parser->fully_implicit_function_template_p)
19626 /* A non-template declaration involving a function parameter list
19627 containing an implicit template parameter will be made into a
19628 template. If the resulting declaration is not going to be an
19629 actual function then finish the template scope here to prevent it.
19630 An error message will be issued once we have a decl to talk about.
19632 FIXME probably we should do type deduction rather than create an
19633 implicit template, but the standard currently doesn't allow it. */
19634 bogus_implicit_tmpl = true;
19635 finish_fully_implicit_template (parser, NULL_TREE);
19638 /* [dcl.dcl]
19640 Only in function declarations for constructors, destructors, type
19641 conversions, and deduction guides can the decl-specifier-seq be omitted.
19643 We explicitly postpone this check past the point where we handle
19644 function-definitions because we tolerate function-definitions
19645 that are missing their return types in some modes. */
19646 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19648 cp_parser_error (parser,
19649 "expected constructor, destructor, or type conversion");
19650 return error_mark_node;
19653 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19654 if (token->type == CPP_EQ
19655 || token->type == CPP_OPEN_PAREN
19656 || token->type == CPP_OPEN_BRACE)
19658 is_initialized = SD_INITIALIZED;
19659 initialization_kind = token->type;
19660 if (maybe_range_for_decl)
19661 *maybe_range_for_decl = error_mark_node;
19662 tmp_init_loc = token->location;
19663 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19664 *init_loc = tmp_init_loc;
19666 if (token->type == CPP_EQ
19667 && function_declarator_p (declarator))
19669 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19670 if (t2->keyword == RID_DEFAULT)
19671 is_initialized = SD_DEFAULTED;
19672 else if (t2->keyword == RID_DELETE)
19673 is_initialized = SD_DELETED;
19676 else
19678 /* If the init-declarator isn't initialized and isn't followed by a
19679 `,' or `;', it's not a valid init-declarator. */
19680 if (token->type != CPP_COMMA
19681 && token->type != CPP_SEMICOLON)
19683 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19684 range_for_decl_p = true;
19685 else
19687 if (!maybe_range_for_decl)
19688 cp_parser_error (parser, "expected initializer");
19689 return error_mark_node;
19692 is_initialized = SD_UNINITIALIZED;
19693 initialization_kind = CPP_EOF;
19696 /* Because start_decl has side-effects, we should only call it if we
19697 know we're going ahead. By this point, we know that we cannot
19698 possibly be looking at any other construct. */
19699 cp_parser_commit_to_tentative_parse (parser);
19701 /* Enter the newly declared entry in the symbol table. If we're
19702 processing a declaration in a class-specifier, we wait until
19703 after processing the initializer. */
19704 if (!member_p)
19706 if (parser->in_unbraced_linkage_specification_p)
19707 decl_specifiers->storage_class = sc_extern;
19708 decl = start_decl (declarator, decl_specifiers,
19709 range_for_decl_p? SD_INITIALIZED : is_initialized,
19710 attributes, prefix_attributes, &pushed_scope);
19711 cp_finalize_omp_declare_simd (parser, decl);
19712 cp_finalize_oacc_routine (parser, decl, false);
19713 /* Adjust location of decl if declarator->id_loc is more appropriate:
19714 set, and decl wasn't merged with another decl, in which case its
19715 location would be different from input_location, and more accurate. */
19716 if (DECL_P (decl)
19717 && declarator->id_loc != UNKNOWN_LOCATION
19718 && DECL_SOURCE_LOCATION (decl) == input_location)
19719 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19721 else if (scope)
19722 /* Enter the SCOPE. That way unqualified names appearing in the
19723 initializer will be looked up in SCOPE. */
19724 pushed_scope = push_scope (scope);
19726 /* Perform deferred access control checks, now that we know in which
19727 SCOPE the declared entity resides. */
19728 if (!member_p && decl)
19730 tree saved_current_function_decl = NULL_TREE;
19732 /* If the entity being declared is a function, pretend that we
19733 are in its scope. If it is a `friend', it may have access to
19734 things that would not otherwise be accessible. */
19735 if (TREE_CODE (decl) == FUNCTION_DECL)
19737 saved_current_function_decl = current_function_decl;
19738 current_function_decl = decl;
19741 /* Perform access checks for template parameters. */
19742 cp_parser_perform_template_parameter_access_checks (checks);
19744 /* Perform the access control checks for the declarator and the
19745 decl-specifiers. */
19746 perform_deferred_access_checks (tf_warning_or_error);
19748 /* Restore the saved value. */
19749 if (TREE_CODE (decl) == FUNCTION_DECL)
19750 current_function_decl = saved_current_function_decl;
19753 /* Parse the initializer. */
19754 initializer = NULL_TREE;
19755 is_direct_init = false;
19756 is_non_constant_init = true;
19757 if (is_initialized)
19759 if (function_declarator_p (declarator))
19761 if (initialization_kind == CPP_EQ)
19762 initializer = cp_parser_pure_specifier (parser);
19763 else
19765 /* If the declaration was erroneous, we don't really
19766 know what the user intended, so just silently
19767 consume the initializer. */
19768 if (decl != error_mark_node)
19769 error_at (tmp_init_loc, "initializer provided for function");
19770 cp_parser_skip_to_closing_parenthesis (parser,
19771 /*recovering=*/true,
19772 /*or_comma=*/false,
19773 /*consume_paren=*/true);
19776 else
19778 /* We want to record the extra mangling scope for in-class
19779 initializers of class members and initializers of static data
19780 member templates. The former involves deferring
19781 parsing of the initializer until end of class as with default
19782 arguments. So right here we only handle the latter. */
19783 if (!member_p && processing_template_decl && decl != error_mark_node)
19784 start_lambda_scope (decl);
19785 initializer = cp_parser_initializer (parser,
19786 &is_direct_init,
19787 &is_non_constant_init);
19788 if (!member_p && processing_template_decl && decl != error_mark_node)
19789 finish_lambda_scope ();
19790 if (initializer == error_mark_node)
19791 cp_parser_skip_to_end_of_statement (parser);
19795 /* The old parser allows attributes to appear after a parenthesized
19796 initializer. Mark Mitchell proposed removing this functionality
19797 on the GCC mailing lists on 2002-08-13. This parser accepts the
19798 attributes -- but ignores them. Made a permerror in GCC 8. */
19799 if (cp_parser_allow_gnu_extensions_p (parser)
19800 && initialization_kind == CPP_OPEN_PAREN
19801 && cp_parser_attributes_opt (parser)
19802 && permerror (input_location,
19803 "attributes after parenthesized initializer ignored"))
19805 static bool hint;
19806 if (flag_permissive && !hint)
19808 hint = true;
19809 inform (input_location,
19810 "this flexibility is deprecated and will be removed");
19814 /* And now complain about a non-function implicit template. */
19815 if (bogus_implicit_tmpl && decl != error_mark_node)
19816 error_at (DECL_SOURCE_LOCATION (decl),
19817 "non-function %qD declared as implicit template", decl);
19819 /* For an in-class declaration, use `grokfield' to create the
19820 declaration. */
19821 if (member_p)
19823 if (pushed_scope)
19825 pop_scope (pushed_scope);
19826 pushed_scope = NULL_TREE;
19828 decl = grokfield (declarator, decl_specifiers,
19829 initializer, !is_non_constant_init,
19830 /*asmspec=*/NULL_TREE,
19831 attr_chainon (attributes, prefix_attributes));
19832 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19833 cp_parser_save_default_args (parser, decl);
19834 cp_finalize_omp_declare_simd (parser, decl);
19835 cp_finalize_oacc_routine (parser, decl, false);
19838 /* Finish processing the declaration. But, skip member
19839 declarations. */
19840 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19842 cp_finish_decl (decl,
19843 initializer, !is_non_constant_init,
19844 asm_specification,
19845 /* If the initializer is in parentheses, then this is
19846 a direct-initialization, which means that an
19847 `explicit' constructor is OK. Otherwise, an
19848 `explicit' constructor cannot be used. */
19849 ((is_direct_init || !is_initialized)
19850 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19852 else if ((cxx_dialect != cxx98) && friend_p
19853 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19854 /* Core issue #226 (C++0x only): A default template-argument
19855 shall not be specified in a friend class template
19856 declaration. */
19857 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19858 /*is_partial=*/false, /*is_friend_decl=*/1);
19860 if (!friend_p && pushed_scope)
19861 pop_scope (pushed_scope);
19863 if (function_declarator_p (declarator)
19864 && parser->fully_implicit_function_template_p)
19866 if (member_p)
19867 decl = finish_fully_implicit_template (parser, decl);
19868 else
19869 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19872 if (auto_result && is_initialized && decl_specifiers->type
19873 && type_uses_auto (decl_specifiers->type))
19874 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19876 return decl;
19879 /* Parse a declarator.
19881 declarator:
19882 direct-declarator
19883 ptr-operator declarator
19885 abstract-declarator:
19886 ptr-operator abstract-declarator [opt]
19887 direct-abstract-declarator
19889 GNU Extensions:
19891 declarator:
19892 attributes [opt] direct-declarator
19893 attributes [opt] ptr-operator declarator
19895 abstract-declarator:
19896 attributes [opt] ptr-operator abstract-declarator [opt]
19897 attributes [opt] direct-abstract-declarator
19899 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19900 detect constructors, destructors, deduction guides, or conversion operators.
19901 It is set to -1 if the declarator is a name, and +1 if it is a
19902 function. Otherwise it is set to zero. Usually you just want to
19903 test for >0, but internally the negative value is used.
19905 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19906 a decl-specifier-seq unless it declares a constructor, destructor,
19907 or conversion. It might seem that we could check this condition in
19908 semantic analysis, rather than parsing, but that makes it difficult
19909 to handle something like `f()'. We want to notice that there are
19910 no decl-specifiers, and therefore realize that this is an
19911 expression, not a declaration.)
19913 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19914 the declarator is a direct-declarator of the form "(...)".
19916 MEMBER_P is true iff this declarator is a member-declarator.
19918 FRIEND_P is true iff this declarator is a friend. */
19920 static cp_declarator *
19921 cp_parser_declarator (cp_parser* parser,
19922 cp_parser_declarator_kind dcl_kind,
19923 int* ctor_dtor_or_conv_p,
19924 bool* parenthesized_p,
19925 bool member_p, bool friend_p)
19927 cp_declarator *declarator;
19928 enum tree_code code;
19929 cp_cv_quals cv_quals;
19930 tree class_type;
19931 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19933 /* Assume this is not a constructor, destructor, or type-conversion
19934 operator. */
19935 if (ctor_dtor_or_conv_p)
19936 *ctor_dtor_or_conv_p = 0;
19938 if (cp_parser_allow_gnu_extensions_p (parser))
19939 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19941 /* Check for the ptr-operator production. */
19942 cp_parser_parse_tentatively (parser);
19943 /* Parse the ptr-operator. */
19944 code = cp_parser_ptr_operator (parser,
19945 &class_type,
19946 &cv_quals,
19947 &std_attributes);
19949 /* If that worked, then we have a ptr-operator. */
19950 if (cp_parser_parse_definitely (parser))
19952 /* If a ptr-operator was found, then this declarator was not
19953 parenthesized. */
19954 if (parenthesized_p)
19955 *parenthesized_p = true;
19956 /* The dependent declarator is optional if we are parsing an
19957 abstract-declarator. */
19958 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19959 cp_parser_parse_tentatively (parser);
19961 /* Parse the dependent declarator. */
19962 declarator = cp_parser_declarator (parser, dcl_kind,
19963 /*ctor_dtor_or_conv_p=*/NULL,
19964 /*parenthesized_p=*/NULL,
19965 /*member_p=*/false,
19966 friend_p);
19968 /* If we are parsing an abstract-declarator, we must handle the
19969 case where the dependent declarator is absent. */
19970 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19971 && !cp_parser_parse_definitely (parser))
19972 declarator = NULL;
19974 declarator = cp_parser_make_indirect_declarator
19975 (code, class_type, cv_quals, declarator, std_attributes);
19977 /* Everything else is a direct-declarator. */
19978 else
19980 if (parenthesized_p)
19981 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19982 CPP_OPEN_PAREN);
19983 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19984 ctor_dtor_or_conv_p,
19985 member_p, friend_p);
19988 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19989 declarator->attributes = gnu_attributes;
19990 return declarator;
19993 /* Parse a direct-declarator or direct-abstract-declarator.
19995 direct-declarator:
19996 declarator-id
19997 direct-declarator ( parameter-declaration-clause )
19998 cv-qualifier-seq [opt]
19999 ref-qualifier [opt]
20000 exception-specification [opt]
20001 direct-declarator [ constant-expression [opt] ]
20002 ( declarator )
20004 direct-abstract-declarator:
20005 direct-abstract-declarator [opt]
20006 ( parameter-declaration-clause )
20007 cv-qualifier-seq [opt]
20008 ref-qualifier [opt]
20009 exception-specification [opt]
20010 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20011 ( abstract-declarator )
20013 Returns a representation of the declarator. DCL_KIND is
20014 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20015 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20016 we are parsing a direct-declarator. It is
20017 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20018 of ambiguity we prefer an abstract declarator, as per
20019 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20020 as for cp_parser_declarator. */
20022 static cp_declarator *
20023 cp_parser_direct_declarator (cp_parser* parser,
20024 cp_parser_declarator_kind dcl_kind,
20025 int* ctor_dtor_or_conv_p,
20026 bool member_p, bool friend_p)
20028 cp_token *token;
20029 cp_declarator *declarator = NULL;
20030 tree scope = NULL_TREE;
20031 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20032 bool saved_in_declarator_p = parser->in_declarator_p;
20033 bool first = true;
20034 tree pushed_scope = NULL_TREE;
20035 cp_token *open_paren = NULL, *close_paren = NULL;
20037 while (true)
20039 /* Peek at the next token. */
20040 token = cp_lexer_peek_token (parser->lexer);
20041 if (token->type == CPP_OPEN_PAREN)
20043 /* This is either a parameter-declaration-clause, or a
20044 parenthesized declarator. When we know we are parsing a
20045 named declarator, it must be a parenthesized declarator
20046 if FIRST is true. For instance, `(int)' is a
20047 parameter-declaration-clause, with an omitted
20048 direct-abstract-declarator. But `((*))', is a
20049 parenthesized abstract declarator. Finally, when T is a
20050 template parameter `(T)' is a
20051 parameter-declaration-clause, and not a parenthesized
20052 named declarator.
20054 We first try and parse a parameter-declaration-clause,
20055 and then try a nested declarator (if FIRST is true).
20057 It is not an error for it not to be a
20058 parameter-declaration-clause, even when FIRST is
20059 false. Consider,
20061 int i (int);
20062 int i (3);
20064 The first is the declaration of a function while the
20065 second is the definition of a variable, including its
20066 initializer.
20068 Having seen only the parenthesis, we cannot know which of
20069 these two alternatives should be selected. Even more
20070 complex are examples like:
20072 int i (int (a));
20073 int i (int (3));
20075 The former is a function-declaration; the latter is a
20076 variable initialization.
20078 Thus again, we try a parameter-declaration-clause, and if
20079 that fails, we back out and return. */
20081 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20083 tree params;
20084 bool is_declarator = false;
20086 open_paren = NULL;
20088 /* In a member-declarator, the only valid interpretation
20089 of a parenthesis is the start of a
20090 parameter-declaration-clause. (It is invalid to
20091 initialize a static data member with a parenthesized
20092 initializer; only the "=" form of initialization is
20093 permitted.) */
20094 if (!member_p)
20095 cp_parser_parse_tentatively (parser);
20097 /* Consume the `('. */
20098 matching_parens parens;
20099 parens.consume_open (parser);
20100 if (first)
20102 /* If this is going to be an abstract declarator, we're
20103 in a declarator and we can't have default args. */
20104 parser->default_arg_ok_p = false;
20105 parser->in_declarator_p = true;
20108 begin_scope (sk_function_parms, NULL_TREE);
20110 /* Parse the parameter-declaration-clause. */
20111 params = cp_parser_parameter_declaration_clause (parser);
20113 /* Consume the `)'. */
20114 parens.require_close (parser);
20116 /* If all went well, parse the cv-qualifier-seq,
20117 ref-qualifier and the exception-specification. */
20118 if (member_p || cp_parser_parse_definitely (parser))
20120 cp_cv_quals cv_quals;
20121 cp_virt_specifiers virt_specifiers;
20122 cp_ref_qualifier ref_qual;
20123 tree exception_specification;
20124 tree late_return;
20125 tree attrs;
20126 bool memfn = (member_p || (pushed_scope
20127 && CLASS_TYPE_P (pushed_scope)));
20129 is_declarator = true;
20131 if (ctor_dtor_or_conv_p)
20132 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20133 first = false;
20135 /* Parse the cv-qualifier-seq. */
20136 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20137 /* Parse the ref-qualifier. */
20138 ref_qual = cp_parser_ref_qualifier_opt (parser);
20139 /* Parse the tx-qualifier. */
20140 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20141 /* And the exception-specification. */
20142 exception_specification
20143 = cp_parser_exception_specification_opt (parser);
20145 attrs = cp_parser_std_attribute_spec_seq (parser);
20147 /* In here, we handle cases where attribute is used after
20148 the function declaration. For example:
20149 void func (int x) __attribute__((vector(..))); */
20150 tree gnu_attrs = NULL_TREE;
20151 tree requires_clause = NULL_TREE;
20152 late_return = (cp_parser_late_return_type_opt
20153 (parser, declarator, requires_clause,
20154 memfn ? cv_quals : -1));
20156 /* Parse the virt-specifier-seq. */
20157 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20159 /* Create the function-declarator. */
20160 declarator = make_call_declarator (declarator,
20161 params,
20162 cv_quals,
20163 virt_specifiers,
20164 ref_qual,
20165 tx_qual,
20166 exception_specification,
20167 late_return,
20168 requires_clause);
20169 declarator->std_attributes = attrs;
20170 declarator->attributes = gnu_attrs;
20171 /* Any subsequent parameter lists are to do with
20172 return type, so are not those of the declared
20173 function. */
20174 parser->default_arg_ok_p = false;
20177 /* Remove the function parms from scope. */
20178 pop_bindings_and_leave_scope ();
20180 if (is_declarator)
20181 /* Repeat the main loop. */
20182 continue;
20185 /* If this is the first, we can try a parenthesized
20186 declarator. */
20187 if (first)
20189 bool saved_in_type_id_in_expr_p;
20191 parser->default_arg_ok_p = saved_default_arg_ok_p;
20192 parser->in_declarator_p = saved_in_declarator_p;
20194 open_paren = token;
20195 /* Consume the `('. */
20196 matching_parens parens;
20197 parens.consume_open (parser);
20198 /* Parse the nested declarator. */
20199 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20200 parser->in_type_id_in_expr_p = true;
20201 declarator
20202 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20203 /*parenthesized_p=*/NULL,
20204 member_p, friend_p);
20205 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20206 first = false;
20207 /* Expect a `)'. */
20208 close_paren = cp_lexer_peek_token (parser->lexer);
20209 if (!parens.require_close (parser))
20210 declarator = cp_error_declarator;
20211 if (declarator == cp_error_declarator)
20212 break;
20214 goto handle_declarator;
20216 /* Otherwise, we must be done. */
20217 else
20218 break;
20220 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20221 && token->type == CPP_OPEN_SQUARE
20222 && !cp_next_tokens_can_be_attribute_p (parser))
20224 /* Parse an array-declarator. */
20225 tree bounds, attrs;
20227 if (ctor_dtor_or_conv_p)
20228 *ctor_dtor_or_conv_p = 0;
20230 open_paren = NULL;
20231 first = false;
20232 parser->default_arg_ok_p = false;
20233 parser->in_declarator_p = true;
20234 /* Consume the `['. */
20235 cp_lexer_consume_token (parser->lexer);
20236 /* Peek at the next token. */
20237 token = cp_lexer_peek_token (parser->lexer);
20238 /* If the next token is `]', then there is no
20239 constant-expression. */
20240 if (token->type != CPP_CLOSE_SQUARE)
20242 bool non_constant_p;
20243 bounds
20244 = cp_parser_constant_expression (parser,
20245 /*allow_non_constant=*/true,
20246 &non_constant_p);
20247 if (!non_constant_p)
20248 /* OK */;
20249 else if (error_operand_p (bounds))
20250 /* Already gave an error. */;
20251 else if (!parser->in_function_body
20252 || current_binding_level->kind == sk_function_parms)
20254 /* Normally, the array bound must be an integral constant
20255 expression. However, as an extension, we allow VLAs
20256 in function scopes as long as they aren't part of a
20257 parameter declaration. */
20258 cp_parser_error (parser,
20259 "array bound is not an integer constant");
20260 bounds = error_mark_node;
20262 else if (processing_template_decl
20263 && !type_dependent_expression_p (bounds))
20265 /* Remember this wasn't a constant-expression. */
20266 bounds = build_nop (TREE_TYPE (bounds), bounds);
20267 TREE_SIDE_EFFECTS (bounds) = 1;
20270 else
20271 bounds = NULL_TREE;
20272 /* Look for the closing `]'. */
20273 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20275 declarator = cp_error_declarator;
20276 break;
20279 attrs = cp_parser_std_attribute_spec_seq (parser);
20280 declarator = make_array_declarator (declarator, bounds);
20281 declarator->std_attributes = attrs;
20283 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20286 tree qualifying_scope;
20287 tree unqualified_name;
20288 tree attrs;
20289 special_function_kind sfk;
20290 bool abstract_ok;
20291 bool pack_expansion_p = false;
20292 cp_token *declarator_id_start_token;
20294 /* Parse a declarator-id */
20295 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20296 if (abstract_ok)
20298 cp_parser_parse_tentatively (parser);
20300 /* If we see an ellipsis, we should be looking at a
20301 parameter pack. */
20302 if (token->type == CPP_ELLIPSIS)
20304 /* Consume the `...' */
20305 cp_lexer_consume_token (parser->lexer);
20307 pack_expansion_p = true;
20311 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20312 unqualified_name
20313 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20314 qualifying_scope = parser->scope;
20315 if (abstract_ok)
20317 bool okay = false;
20319 if (!unqualified_name && pack_expansion_p)
20321 /* Check whether an error occurred. */
20322 okay = !cp_parser_error_occurred (parser);
20324 /* We already consumed the ellipsis to mark a
20325 parameter pack, but we have no way to report it,
20326 so abort the tentative parse. We will be exiting
20327 immediately anyway. */
20328 cp_parser_abort_tentative_parse (parser);
20330 else
20331 okay = cp_parser_parse_definitely (parser);
20333 if (!okay)
20334 unqualified_name = error_mark_node;
20335 else if (unqualified_name
20336 && (qualifying_scope
20337 || (!identifier_p (unqualified_name))))
20339 cp_parser_error (parser, "expected unqualified-id");
20340 unqualified_name = error_mark_node;
20344 if (!unqualified_name)
20345 return NULL;
20346 if (unqualified_name == error_mark_node)
20348 declarator = cp_error_declarator;
20349 pack_expansion_p = false;
20350 declarator->parameter_pack_p = false;
20351 break;
20354 attrs = cp_parser_std_attribute_spec_seq (parser);
20356 if (qualifying_scope && at_namespace_scope_p ()
20357 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20359 /* In the declaration of a member of a template class
20360 outside of the class itself, the SCOPE will sometimes
20361 be a TYPENAME_TYPE. For example, given:
20363 template <typename T>
20364 int S<T>::R::i = 3;
20366 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20367 this context, we must resolve S<T>::R to an ordinary
20368 type, rather than a typename type.
20370 The reason we normally avoid resolving TYPENAME_TYPEs
20371 is that a specialization of `S' might render
20372 `S<T>::R' not a type. However, if `S' is
20373 specialized, then this `i' will not be used, so there
20374 is no harm in resolving the types here. */
20375 tree type;
20377 /* Resolve the TYPENAME_TYPE. */
20378 type = resolve_typename_type (qualifying_scope,
20379 /*only_current_p=*/false);
20380 /* If that failed, the declarator is invalid. */
20381 if (TREE_CODE (type) == TYPENAME_TYPE)
20383 if (typedef_variant_p (type))
20384 error_at (declarator_id_start_token->location,
20385 "cannot define member of dependent typedef "
20386 "%qT", type);
20387 else
20388 error_at (declarator_id_start_token->location,
20389 "%<%T::%E%> is not a type",
20390 TYPE_CONTEXT (qualifying_scope),
20391 TYPE_IDENTIFIER (qualifying_scope));
20393 qualifying_scope = type;
20396 sfk = sfk_none;
20398 if (unqualified_name)
20400 tree class_type;
20402 if (qualifying_scope
20403 && CLASS_TYPE_P (qualifying_scope))
20404 class_type = qualifying_scope;
20405 else
20406 class_type = current_class_type;
20408 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20410 tree name_type = TREE_TYPE (unqualified_name);
20412 if (!class_type || !same_type_p (name_type, class_type))
20414 /* We do not attempt to print the declarator
20415 here because we do not have enough
20416 information about its original syntactic
20417 form. */
20418 cp_parser_error (parser, "invalid declarator");
20419 declarator = cp_error_declarator;
20420 break;
20422 else if (qualifying_scope
20423 && CLASSTYPE_USE_TEMPLATE (name_type))
20425 error_at (declarator_id_start_token->location,
20426 "invalid use of constructor as a template");
20427 inform (declarator_id_start_token->location,
20428 "use %<%T::%D%> instead of %<%T::%D%> to "
20429 "name the constructor in a qualified name",
20430 class_type,
20431 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20432 class_type, name_type);
20433 declarator = cp_error_declarator;
20434 break;
20436 unqualified_name = constructor_name (class_type);
20439 if (class_type)
20441 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20442 sfk = sfk_destructor;
20443 else if (identifier_p (unqualified_name)
20444 && IDENTIFIER_CONV_OP_P (unqualified_name))
20445 sfk = sfk_conversion;
20446 else if (/* There's no way to declare a constructor
20447 for an unnamed type, even if the type
20448 got a name for linkage purposes. */
20449 !TYPE_WAS_UNNAMED (class_type)
20450 /* Handle correctly (c++/19200):
20452 struct S {
20453 struct T{};
20454 friend void S(T);
20457 and also:
20459 namespace N {
20460 void S();
20463 struct S {
20464 friend void N::S();
20465 }; */
20466 && (!friend_p || class_type == qualifying_scope)
20467 && constructor_name_p (unqualified_name,
20468 class_type))
20469 sfk = sfk_constructor;
20470 else if (is_overloaded_fn (unqualified_name)
20471 && DECL_CONSTRUCTOR_P (get_first_fn
20472 (unqualified_name)))
20473 sfk = sfk_constructor;
20475 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20476 *ctor_dtor_or_conv_p = -1;
20479 declarator = make_id_declarator (qualifying_scope,
20480 unqualified_name,
20481 sfk);
20482 declarator->std_attributes = attrs;
20483 declarator->id_loc = token->location;
20484 declarator->parameter_pack_p = pack_expansion_p;
20486 if (pack_expansion_p)
20487 maybe_warn_variadic_templates ();
20490 handle_declarator:;
20491 scope = get_scope_of_declarator (declarator);
20492 if (scope)
20494 /* Any names that appear after the declarator-id for a
20495 member are looked up in the containing scope. */
20496 if (at_function_scope_p ())
20498 /* But declarations with qualified-ids can't appear in a
20499 function. */
20500 cp_parser_error (parser, "qualified-id in declaration");
20501 declarator = cp_error_declarator;
20502 break;
20504 pushed_scope = push_scope (scope);
20506 parser->in_declarator_p = true;
20507 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20508 || (declarator && declarator->kind == cdk_id))
20509 /* Default args are only allowed on function
20510 declarations. */
20511 parser->default_arg_ok_p = saved_default_arg_ok_p;
20512 else
20513 parser->default_arg_ok_p = false;
20515 first = false;
20517 /* We're done. */
20518 else
20519 break;
20522 /* For an abstract declarator, we might wind up with nothing at this
20523 point. That's an error; the declarator is not optional. */
20524 if (!declarator)
20525 cp_parser_error (parser, "expected declarator");
20526 else if (open_paren)
20528 /* Record overly parenthesized declarator so we can give a
20529 diagnostic about confusing decl/expr disambiguation. */
20530 if (declarator->kind == cdk_array)
20532 /* If the open and close parens are on different lines, this
20533 is probably a formatting thing, so ignore. */
20534 expanded_location open = expand_location (open_paren->location);
20535 expanded_location close = expand_location (close_paren->location);
20536 if (open.line != close.line || open.file != close.file)
20537 open_paren = NULL;
20539 if (open_paren)
20540 declarator->parenthesized = open_paren->location;
20543 /* If we entered a scope, we must exit it now. */
20544 if (pushed_scope)
20545 pop_scope (pushed_scope);
20547 parser->default_arg_ok_p = saved_default_arg_ok_p;
20548 parser->in_declarator_p = saved_in_declarator_p;
20550 return declarator;
20553 /* Parse a ptr-operator.
20555 ptr-operator:
20556 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20557 * cv-qualifier-seq [opt]
20559 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20560 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20562 GNU Extension:
20564 ptr-operator:
20565 & cv-qualifier-seq [opt]
20567 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20568 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20569 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20570 filled in with the TYPE containing the member. *CV_QUALS is
20571 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20572 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20573 Note that the tree codes returned by this function have nothing
20574 to do with the types of trees that will be eventually be created
20575 to represent the pointer or reference type being parsed. They are
20576 just constants with suggestive names. */
20577 static enum tree_code
20578 cp_parser_ptr_operator (cp_parser* parser,
20579 tree* type,
20580 cp_cv_quals *cv_quals,
20581 tree *attributes)
20583 enum tree_code code = ERROR_MARK;
20584 cp_token *token;
20585 tree attrs = NULL_TREE;
20587 /* Assume that it's not a pointer-to-member. */
20588 *type = NULL_TREE;
20589 /* And that there are no cv-qualifiers. */
20590 *cv_quals = TYPE_UNQUALIFIED;
20592 /* Peek at the next token. */
20593 token = cp_lexer_peek_token (parser->lexer);
20595 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20596 if (token->type == CPP_MULT)
20597 code = INDIRECT_REF;
20598 else if (token->type == CPP_AND)
20599 code = ADDR_EXPR;
20600 else if ((cxx_dialect != cxx98) &&
20601 token->type == CPP_AND_AND) /* C++0x only */
20602 code = NON_LVALUE_EXPR;
20604 if (code != ERROR_MARK)
20606 /* Consume the `*', `&' or `&&'. */
20607 cp_lexer_consume_token (parser->lexer);
20609 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20610 `&', if we are allowing GNU extensions. (The only qualifier
20611 that can legally appear after `&' is `restrict', but that is
20612 enforced during semantic analysis. */
20613 if (code == INDIRECT_REF
20614 || cp_parser_allow_gnu_extensions_p (parser))
20615 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20617 attrs = cp_parser_std_attribute_spec_seq (parser);
20618 if (attributes != NULL)
20619 *attributes = attrs;
20621 else
20623 /* Try the pointer-to-member case. */
20624 cp_parser_parse_tentatively (parser);
20625 /* Look for the optional `::' operator. */
20626 cp_parser_global_scope_opt (parser,
20627 /*current_scope_valid_p=*/false);
20628 /* Look for the nested-name specifier. */
20629 token = cp_lexer_peek_token (parser->lexer);
20630 cp_parser_nested_name_specifier (parser,
20631 /*typename_keyword_p=*/false,
20632 /*check_dependency_p=*/true,
20633 /*type_p=*/false,
20634 /*is_declaration=*/false);
20635 /* If we found it, and the next token is a `*', then we are
20636 indeed looking at a pointer-to-member operator. */
20637 if (!cp_parser_error_occurred (parser)
20638 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20640 /* Indicate that the `*' operator was used. */
20641 code = INDIRECT_REF;
20643 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20644 error_at (token->location, "%qD is a namespace", parser->scope);
20645 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20646 error_at (token->location, "cannot form pointer to member of "
20647 "non-class %q#T", parser->scope);
20648 else
20650 /* The type of which the member is a member is given by the
20651 current SCOPE. */
20652 *type = parser->scope;
20653 /* The next name will not be qualified. */
20654 parser->scope = NULL_TREE;
20655 parser->qualifying_scope = NULL_TREE;
20656 parser->object_scope = NULL_TREE;
20657 /* Look for optional c++11 attributes. */
20658 attrs = cp_parser_std_attribute_spec_seq (parser);
20659 if (attributes != NULL)
20660 *attributes = attrs;
20661 /* Look for the optional cv-qualifier-seq. */
20662 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20665 /* If that didn't work we don't have a ptr-operator. */
20666 if (!cp_parser_parse_definitely (parser))
20667 cp_parser_error (parser, "expected ptr-operator");
20670 return code;
20673 /* Parse an (optional) cv-qualifier-seq.
20675 cv-qualifier-seq:
20676 cv-qualifier cv-qualifier-seq [opt]
20678 cv-qualifier:
20679 const
20680 volatile
20682 GNU Extension:
20684 cv-qualifier:
20685 __restrict__
20687 Returns a bitmask representing the cv-qualifiers. */
20689 static cp_cv_quals
20690 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20692 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20694 while (true)
20696 cp_token *token;
20697 cp_cv_quals cv_qualifier;
20699 /* Peek at the next token. */
20700 token = cp_lexer_peek_token (parser->lexer);
20701 /* See if it's a cv-qualifier. */
20702 switch (token->keyword)
20704 case RID_CONST:
20705 cv_qualifier = TYPE_QUAL_CONST;
20706 break;
20708 case RID_VOLATILE:
20709 cv_qualifier = TYPE_QUAL_VOLATILE;
20710 break;
20712 case RID_RESTRICT:
20713 cv_qualifier = TYPE_QUAL_RESTRICT;
20714 break;
20716 default:
20717 cv_qualifier = TYPE_UNQUALIFIED;
20718 break;
20721 if (!cv_qualifier)
20722 break;
20724 if (cv_quals & cv_qualifier)
20726 gcc_rich_location richloc (token->location);
20727 richloc.add_fixit_remove ();
20728 error_at (&richloc, "duplicate cv-qualifier");
20729 cp_lexer_purge_token (parser->lexer);
20731 else
20733 cp_lexer_consume_token (parser->lexer);
20734 cv_quals |= cv_qualifier;
20738 return cv_quals;
20741 /* Parse an (optional) ref-qualifier
20743 ref-qualifier:
20747 Returns cp_ref_qualifier representing ref-qualifier. */
20749 static cp_ref_qualifier
20750 cp_parser_ref_qualifier_opt (cp_parser* parser)
20752 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20754 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20755 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20756 return ref_qual;
20758 while (true)
20760 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20761 cp_token *token = cp_lexer_peek_token (parser->lexer);
20763 switch (token->type)
20765 case CPP_AND:
20766 curr_ref_qual = REF_QUAL_LVALUE;
20767 break;
20769 case CPP_AND_AND:
20770 curr_ref_qual = REF_QUAL_RVALUE;
20771 break;
20773 default:
20774 curr_ref_qual = REF_QUAL_NONE;
20775 break;
20778 if (!curr_ref_qual)
20779 break;
20780 else if (ref_qual)
20782 error_at (token->location, "multiple ref-qualifiers");
20783 cp_lexer_purge_token (parser->lexer);
20785 else
20787 ref_qual = curr_ref_qual;
20788 cp_lexer_consume_token (parser->lexer);
20792 return ref_qual;
20795 /* Parse an optional tx-qualifier.
20797 tx-qualifier:
20798 transaction_safe
20799 transaction_safe_dynamic */
20801 static tree
20802 cp_parser_tx_qualifier_opt (cp_parser *parser)
20804 cp_token *token = cp_lexer_peek_token (parser->lexer);
20805 if (token->type == CPP_NAME)
20807 tree name = token->u.value;
20808 const char *p = IDENTIFIER_POINTER (name);
20809 const int len = strlen ("transaction_safe");
20810 if (!strncmp (p, "transaction_safe", len))
20812 p += len;
20813 if (*p == '\0'
20814 || !strcmp (p, "_dynamic"))
20816 cp_lexer_consume_token (parser->lexer);
20817 if (!flag_tm)
20819 error ("%qE requires %<-fgnu-tm%>", name);
20820 return NULL_TREE;
20822 else
20823 return name;
20827 return NULL_TREE;
20830 /* Parse an (optional) virt-specifier-seq.
20832 virt-specifier-seq:
20833 virt-specifier virt-specifier-seq [opt]
20835 virt-specifier:
20836 override
20837 final
20839 Returns a bitmask representing the virt-specifiers. */
20841 static cp_virt_specifiers
20842 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20844 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20846 while (true)
20848 cp_token *token;
20849 cp_virt_specifiers virt_specifier;
20851 /* Peek at the next token. */
20852 token = cp_lexer_peek_token (parser->lexer);
20853 /* See if it's a virt-specifier-qualifier. */
20854 if (token->type != CPP_NAME)
20855 break;
20856 if (id_equal (token->u.value, "override"))
20858 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20859 virt_specifier = VIRT_SPEC_OVERRIDE;
20861 else if (id_equal (token->u.value, "final"))
20863 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20864 virt_specifier = VIRT_SPEC_FINAL;
20866 else if (id_equal (token->u.value, "__final"))
20868 virt_specifier = VIRT_SPEC_FINAL;
20870 else
20871 break;
20873 if (virt_specifiers & virt_specifier)
20875 gcc_rich_location richloc (token->location);
20876 richloc.add_fixit_remove ();
20877 error_at (&richloc, "duplicate virt-specifier");
20878 cp_lexer_purge_token (parser->lexer);
20880 else
20882 cp_lexer_consume_token (parser->lexer);
20883 virt_specifiers |= virt_specifier;
20886 return virt_specifiers;
20889 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20890 is in scope even though it isn't real. */
20892 void
20893 inject_this_parameter (tree ctype, cp_cv_quals quals)
20895 tree this_parm;
20897 if (current_class_ptr)
20899 /* We don't clear this between NSDMIs. Is it already what we want? */
20900 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20901 if (DECL_P (current_class_ptr)
20902 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20903 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20904 && cp_type_quals (type) == quals)
20905 return;
20908 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20909 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20910 current_class_ptr = NULL_TREE;
20911 current_class_ref
20912 = cp_build_fold_indirect_ref (this_parm);
20913 current_class_ptr = this_parm;
20916 /* Return true iff our current scope is a non-static data member
20917 initializer. */
20919 bool
20920 parsing_nsdmi (void)
20922 /* We recognize NSDMI context by the context-less 'this' pointer set up
20923 by the function above. */
20924 if (current_class_ptr
20925 && TREE_CODE (current_class_ptr) == PARM_DECL
20926 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20927 return true;
20928 return false;
20931 /* Parse a late-specified return type, if any. This is not a separate
20932 non-terminal, but part of a function declarator, which looks like
20934 -> trailing-type-specifier-seq abstract-declarator(opt)
20936 Returns the type indicated by the type-id.
20938 In addition to this, parse any queued up #pragma omp declare simd
20939 clauses, and #pragma acc routine clauses.
20941 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20942 function. */
20944 static tree
20945 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20946 tree& requires_clause, cp_cv_quals quals)
20948 cp_token *token;
20949 tree type = NULL_TREE;
20950 bool declare_simd_p = (parser->omp_declare_simd
20951 && declarator
20952 && declarator->kind == cdk_id);
20954 bool oacc_routine_p = (parser->oacc_routine
20955 && declarator
20956 && declarator->kind == cdk_id);
20958 /* Peek at the next token. */
20959 token = cp_lexer_peek_token (parser->lexer);
20960 /* A late-specified return type is indicated by an initial '->'. */
20961 if (token->type != CPP_DEREF
20962 && token->keyword != RID_REQUIRES
20963 && !(token->type == CPP_NAME
20964 && token->u.value == ridpointers[RID_REQUIRES])
20965 && !(declare_simd_p || oacc_routine_p))
20966 return NULL_TREE;
20968 tree save_ccp = current_class_ptr;
20969 tree save_ccr = current_class_ref;
20970 if (quals >= 0)
20972 /* DR 1207: 'this' is in scope in the trailing return type. */
20973 inject_this_parameter (current_class_type, quals);
20976 if (token->type == CPP_DEREF)
20978 /* Consume the ->. */
20979 cp_lexer_consume_token (parser->lexer);
20981 type = cp_parser_trailing_type_id (parser);
20984 /* Function declarations may be followed by a trailing
20985 requires-clause. */
20986 requires_clause = cp_parser_requires_clause_opt (parser);
20988 if (declare_simd_p)
20989 declarator->attributes
20990 = cp_parser_late_parsing_omp_declare_simd (parser,
20991 declarator->attributes);
20992 if (oacc_routine_p)
20993 declarator->attributes
20994 = cp_parser_late_parsing_oacc_routine (parser,
20995 declarator->attributes);
20997 if (quals >= 0)
20999 current_class_ptr = save_ccp;
21000 current_class_ref = save_ccr;
21003 return type;
21006 /* Parse a declarator-id.
21008 declarator-id:
21009 id-expression
21010 :: [opt] nested-name-specifier [opt] type-name
21012 In the `id-expression' case, the value returned is as for
21013 cp_parser_id_expression if the id-expression was an unqualified-id.
21014 If the id-expression was a qualified-id, then a SCOPE_REF is
21015 returned. The first operand is the scope (either a NAMESPACE_DECL
21016 or TREE_TYPE), but the second is still just a representation of an
21017 unqualified-id. */
21019 static tree
21020 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21022 tree id;
21023 /* The expression must be an id-expression. Assume that qualified
21024 names are the names of types so that:
21026 template <class T>
21027 int S<T>::R::i = 3;
21029 will work; we must treat `S<T>::R' as the name of a type.
21030 Similarly, assume that qualified names are templates, where
21031 required, so that:
21033 template <class T>
21034 int S<T>::R<T>::i = 3;
21036 will work, too. */
21037 id = cp_parser_id_expression (parser,
21038 /*template_keyword_p=*/false,
21039 /*check_dependency_p=*/false,
21040 /*template_p=*/NULL,
21041 /*declarator_p=*/true,
21042 optional_p);
21043 if (id && BASELINK_P (id))
21044 id = BASELINK_FUNCTIONS (id);
21045 return id;
21048 /* Parse a type-id.
21050 type-id:
21051 type-specifier-seq abstract-declarator [opt]
21053 Returns the TYPE specified. */
21055 static tree
21056 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21057 bool is_trailing_return)
21059 cp_decl_specifier_seq type_specifier_seq;
21060 cp_declarator *abstract_declarator;
21062 /* Parse the type-specifier-seq. */
21063 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21064 is_trailing_return,
21065 &type_specifier_seq);
21066 if (is_template_arg && type_specifier_seq.type
21067 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21068 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21069 /* A bare template name as a template argument is a template template
21070 argument, not a placeholder, so fail parsing it as a type argument. */
21072 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21073 cp_parser_simulate_error (parser);
21074 return error_mark_node;
21076 if (type_specifier_seq.type == error_mark_node)
21077 return error_mark_node;
21079 /* There might or might not be an abstract declarator. */
21080 cp_parser_parse_tentatively (parser);
21081 /* Look for the declarator. */
21082 abstract_declarator
21083 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21084 /*parenthesized_p=*/NULL,
21085 /*member_p=*/false,
21086 /*friend_p=*/false);
21087 /* Check to see if there really was a declarator. */
21088 if (!cp_parser_parse_definitely (parser))
21089 abstract_declarator = NULL;
21091 if (type_specifier_seq.type
21092 /* The concepts TS allows 'auto' as a type-id. */
21093 && (!flag_concepts || parser->in_type_id_in_expr_p)
21094 /* None of the valid uses of 'auto' in C++14 involve the type-id
21095 nonterminal, but it is valid in a trailing-return-type. */
21096 && !(cxx_dialect >= cxx14 && is_trailing_return))
21097 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21099 /* A type-id with type 'auto' is only ok if the abstract declarator
21100 is a function declarator with a late-specified return type.
21102 A type-id with 'auto' is also valid in a trailing-return-type
21103 in a compound-requirement. */
21104 if (abstract_declarator
21105 && abstract_declarator->kind == cdk_function
21106 && abstract_declarator->u.function.late_return_type)
21107 /* OK */;
21108 else if (parser->in_result_type_constraint_p)
21109 /* OK */;
21110 else
21112 location_t loc = type_specifier_seq.locations[ds_type_spec];
21113 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21115 error_at (loc, "missing template arguments after %qT",
21116 auto_node);
21117 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21118 tmpl);
21120 else
21121 error_at (loc, "invalid use of %qT", auto_node);
21122 return error_mark_node;
21126 return groktypename (&type_specifier_seq, abstract_declarator,
21127 is_template_arg);
21130 static tree
21131 cp_parser_type_id (cp_parser *parser)
21133 return cp_parser_type_id_1 (parser, false, false);
21136 static tree
21137 cp_parser_template_type_arg (cp_parser *parser)
21139 tree r;
21140 const char *saved_message = parser->type_definition_forbidden_message;
21141 parser->type_definition_forbidden_message
21142 = G_("types may not be defined in template arguments");
21143 r = cp_parser_type_id_1 (parser, true, false);
21144 parser->type_definition_forbidden_message = saved_message;
21145 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21147 error ("invalid use of %<auto%> in template argument");
21148 r = error_mark_node;
21150 return r;
21153 static tree
21154 cp_parser_trailing_type_id (cp_parser *parser)
21156 return cp_parser_type_id_1 (parser, false, true);
21159 /* Parse a type-specifier-seq.
21161 type-specifier-seq:
21162 type-specifier type-specifier-seq [opt]
21164 GNU extension:
21166 type-specifier-seq:
21167 attributes type-specifier-seq [opt]
21169 If IS_DECLARATION is true, we are at the start of a "condition" or
21170 exception-declaration, so we might be followed by a declarator-id.
21172 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21173 i.e. we've just seen "->".
21175 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21177 static void
21178 cp_parser_type_specifier_seq (cp_parser* parser,
21179 bool is_declaration,
21180 bool is_trailing_return,
21181 cp_decl_specifier_seq *type_specifier_seq)
21183 bool seen_type_specifier = false;
21184 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21185 cp_token *start_token = NULL;
21187 /* Clear the TYPE_SPECIFIER_SEQ. */
21188 clear_decl_specs (type_specifier_seq);
21190 /* In the context of a trailing return type, enum E { } is an
21191 elaborated-type-specifier followed by a function-body, not an
21192 enum-specifier. */
21193 if (is_trailing_return)
21194 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21196 /* Parse the type-specifiers and attributes. */
21197 while (true)
21199 tree type_specifier;
21200 bool is_cv_qualifier;
21202 /* Check for attributes first. */
21203 if (cp_next_tokens_can_be_attribute_p (parser))
21205 type_specifier_seq->attributes
21206 = attr_chainon (type_specifier_seq->attributes,
21207 cp_parser_attributes_opt (parser));
21208 continue;
21211 /* record the token of the beginning of the type specifier seq,
21212 for error reporting purposes*/
21213 if (!start_token)
21214 start_token = cp_lexer_peek_token (parser->lexer);
21216 /* Look for the type-specifier. */
21217 type_specifier = cp_parser_type_specifier (parser,
21218 flags,
21219 type_specifier_seq,
21220 /*is_declaration=*/false,
21221 NULL,
21222 &is_cv_qualifier);
21223 if (!type_specifier)
21225 /* If the first type-specifier could not be found, this is not a
21226 type-specifier-seq at all. */
21227 if (!seen_type_specifier)
21229 /* Set in_declarator_p to avoid skipping to the semicolon. */
21230 int in_decl = parser->in_declarator_p;
21231 parser->in_declarator_p = true;
21233 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21234 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21235 cp_parser_error (parser, "expected type-specifier");
21237 parser->in_declarator_p = in_decl;
21239 type_specifier_seq->type = error_mark_node;
21240 return;
21242 /* If subsequent type-specifiers could not be found, the
21243 type-specifier-seq is complete. */
21244 break;
21247 seen_type_specifier = true;
21248 /* The standard says that a condition can be:
21250 type-specifier-seq declarator = assignment-expression
21252 However, given:
21254 struct S {};
21255 if (int S = ...)
21257 we should treat the "S" as a declarator, not as a
21258 type-specifier. The standard doesn't say that explicitly for
21259 type-specifier-seq, but it does say that for
21260 decl-specifier-seq in an ordinary declaration. Perhaps it
21261 would be clearer just to allow a decl-specifier-seq here, and
21262 then add a semantic restriction that if any decl-specifiers
21263 that are not type-specifiers appear, the program is invalid. */
21264 if (is_declaration && !is_cv_qualifier)
21265 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21269 /* Return whether the function currently being declared has an associated
21270 template parameter list. */
21272 static bool
21273 function_being_declared_is_template_p (cp_parser* parser)
21275 if (!current_template_parms || processing_template_parmlist)
21276 return false;
21278 if (parser->implicit_template_scope)
21279 return true;
21281 if (at_class_scope_p ()
21282 && TYPE_BEING_DEFINED (current_class_type))
21283 return parser->num_template_parameter_lists != 0;
21285 return ((int) parser->num_template_parameter_lists > template_class_depth
21286 (current_class_type));
21289 /* Parse a parameter-declaration-clause.
21291 parameter-declaration-clause:
21292 parameter-declaration-list [opt] ... [opt]
21293 parameter-declaration-list , ...
21295 Returns a representation for the parameter declarations. A return
21296 value of NULL indicates a parameter-declaration-clause consisting
21297 only of an ellipsis. */
21299 static tree
21300 cp_parser_parameter_declaration_clause (cp_parser* parser)
21302 tree parameters;
21303 cp_token *token;
21304 bool ellipsis_p;
21306 temp_override<bool> cleanup
21307 (parser->auto_is_implicit_function_template_parm_p);
21309 if (!processing_specialization
21310 && !processing_template_parmlist
21311 && !processing_explicit_instantiation
21312 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21313 actual function or a random abstract declarator. */
21314 && parser->default_arg_ok_p)
21315 if (!current_function_decl
21316 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21317 parser->auto_is_implicit_function_template_parm_p = true;
21319 /* Peek at the next token. */
21320 token = cp_lexer_peek_token (parser->lexer);
21321 /* Check for trivial parameter-declaration-clauses. */
21322 if (token->type == CPP_ELLIPSIS)
21324 /* Consume the `...' token. */
21325 cp_lexer_consume_token (parser->lexer);
21326 return NULL_TREE;
21328 else if (token->type == CPP_CLOSE_PAREN)
21329 /* There are no parameters. */
21331 #ifndef NO_IMPLICIT_EXTERN_C
21332 if (in_system_header_at (input_location)
21333 && current_class_type == NULL
21334 && current_lang_name == lang_name_c)
21335 return NULL_TREE;
21336 else
21337 #endif
21338 return void_list_node;
21340 /* Check for `(void)', too, which is a special case. */
21341 else if (token->keyword == RID_VOID
21342 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21343 == CPP_CLOSE_PAREN))
21345 /* Consume the `void' token. */
21346 cp_lexer_consume_token (parser->lexer);
21347 /* There are no parameters. */
21348 return void_list_node;
21351 /* Parse the parameter-declaration-list. */
21352 parameters = cp_parser_parameter_declaration_list (parser);
21353 /* If a parse error occurred while parsing the
21354 parameter-declaration-list, then the entire
21355 parameter-declaration-clause is erroneous. */
21356 if (parameters == error_mark_node)
21357 return NULL_TREE;
21359 /* Peek at the next token. */
21360 token = cp_lexer_peek_token (parser->lexer);
21361 /* If it's a `,', the clause should terminate with an ellipsis. */
21362 if (token->type == CPP_COMMA)
21364 /* Consume the `,'. */
21365 cp_lexer_consume_token (parser->lexer);
21366 /* Expect an ellipsis. */
21367 ellipsis_p
21368 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21370 /* It might also be `...' if the optional trailing `,' was
21371 omitted. */
21372 else if (token->type == CPP_ELLIPSIS)
21374 /* Consume the `...' token. */
21375 cp_lexer_consume_token (parser->lexer);
21376 /* And remember that we saw it. */
21377 ellipsis_p = true;
21379 else
21380 ellipsis_p = false;
21382 /* Finish the parameter list. */
21383 if (!ellipsis_p)
21384 parameters = chainon (parameters, void_list_node);
21386 return parameters;
21389 /* Parse a parameter-declaration-list.
21391 parameter-declaration-list:
21392 parameter-declaration
21393 parameter-declaration-list , parameter-declaration
21395 Returns a representation of the parameter-declaration-list, as for
21396 cp_parser_parameter_declaration_clause. However, the
21397 `void_list_node' is never appended to the list. */
21399 static tree
21400 cp_parser_parameter_declaration_list (cp_parser* parser)
21402 tree parameters = NULL_TREE;
21403 tree *tail = &parameters;
21404 bool saved_in_unbraced_linkage_specification_p;
21405 int index = 0;
21407 /* The special considerations that apply to a function within an
21408 unbraced linkage specifications do not apply to the parameters
21409 to the function. */
21410 saved_in_unbraced_linkage_specification_p
21411 = parser->in_unbraced_linkage_specification_p;
21412 parser->in_unbraced_linkage_specification_p = false;
21414 /* Look for more parameters. */
21415 while (true)
21417 cp_parameter_declarator *parameter;
21418 tree decl = error_mark_node;
21419 bool parenthesized_p = false;
21421 /* Parse the parameter. */
21422 parameter
21423 = cp_parser_parameter_declaration (parser,
21424 /*template_parm_p=*/false,
21425 &parenthesized_p);
21427 /* We don't know yet if the enclosing context is deprecated, so wait
21428 and warn in grokparms if appropriate. */
21429 deprecated_state = DEPRECATED_SUPPRESS;
21431 if (parameter)
21433 decl = grokdeclarator (parameter->declarator,
21434 &parameter->decl_specifiers,
21435 PARM,
21436 parameter->default_argument != NULL_TREE,
21437 &parameter->decl_specifiers.attributes);
21438 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21439 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21442 deprecated_state = DEPRECATED_NORMAL;
21444 /* If a parse error occurred parsing the parameter declaration,
21445 then the entire parameter-declaration-list is erroneous. */
21446 if (decl == error_mark_node)
21448 parameters = error_mark_node;
21449 break;
21452 if (parameter->decl_specifiers.attributes)
21453 cplus_decl_attributes (&decl,
21454 parameter->decl_specifiers.attributes,
21456 if (DECL_NAME (decl))
21457 decl = pushdecl (decl);
21459 if (decl != error_mark_node)
21461 retrofit_lang_decl (decl);
21462 DECL_PARM_INDEX (decl) = ++index;
21463 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21466 /* Add the new parameter to the list. */
21467 *tail = build_tree_list (parameter->default_argument, decl);
21468 tail = &TREE_CHAIN (*tail);
21470 /* Peek at the next token. */
21471 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21472 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21473 /* These are for Objective-C++ */
21474 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21475 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21476 /* The parameter-declaration-list is complete. */
21477 break;
21478 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21480 cp_token *token;
21482 /* Peek at the next token. */
21483 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21484 /* If it's an ellipsis, then the list is complete. */
21485 if (token->type == CPP_ELLIPSIS)
21486 break;
21487 /* Otherwise, there must be more parameters. Consume the
21488 `,'. */
21489 cp_lexer_consume_token (parser->lexer);
21490 /* When parsing something like:
21492 int i(float f, double d)
21494 we can tell after seeing the declaration for "f" that we
21495 are not looking at an initialization of a variable "i",
21496 but rather at the declaration of a function "i".
21498 Due to the fact that the parsing of template arguments
21499 (as specified to a template-id) requires backtracking we
21500 cannot use this technique when inside a template argument
21501 list. */
21502 if (!parser->in_template_argument_list_p
21503 && !parser->in_type_id_in_expr_p
21504 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21505 /* However, a parameter-declaration of the form
21506 "float(f)" (which is a valid declaration of a
21507 parameter "f") can also be interpreted as an
21508 expression (the conversion of "f" to "float"). */
21509 && !parenthesized_p)
21510 cp_parser_commit_to_tentative_parse (parser);
21512 else
21514 cp_parser_error (parser, "expected %<,%> or %<...%>");
21515 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21516 cp_parser_skip_to_closing_parenthesis (parser,
21517 /*recovering=*/true,
21518 /*or_comma=*/false,
21519 /*consume_paren=*/false);
21520 break;
21524 parser->in_unbraced_linkage_specification_p
21525 = saved_in_unbraced_linkage_specification_p;
21527 /* Reset implicit_template_scope if we are about to leave the function
21528 parameter list that introduced it. Note that for out-of-line member
21529 definitions, there will be one or more class scopes before we get to
21530 the template parameter scope. */
21532 if (cp_binding_level *its = parser->implicit_template_scope)
21533 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21535 while (maybe_its->kind == sk_class)
21536 maybe_its = maybe_its->level_chain;
21537 if (maybe_its == its)
21539 parser->implicit_template_parms = 0;
21540 parser->implicit_template_scope = 0;
21544 return parameters;
21547 /* Parse a parameter declaration.
21549 parameter-declaration:
21550 decl-specifier-seq ... [opt] declarator
21551 decl-specifier-seq declarator = assignment-expression
21552 decl-specifier-seq ... [opt] abstract-declarator [opt]
21553 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21555 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21556 declares a template parameter. (In that case, a non-nested `>'
21557 token encountered during the parsing of the assignment-expression
21558 is not interpreted as a greater-than operator.)
21560 Returns a representation of the parameter, or NULL if an error
21561 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21562 true iff the declarator is of the form "(p)". */
21564 static cp_parameter_declarator *
21565 cp_parser_parameter_declaration (cp_parser *parser,
21566 bool template_parm_p,
21567 bool *parenthesized_p)
21569 int declares_class_or_enum;
21570 cp_decl_specifier_seq decl_specifiers;
21571 cp_declarator *declarator;
21572 tree default_argument;
21573 cp_token *token = NULL, *declarator_token_start = NULL;
21574 const char *saved_message;
21575 bool template_parameter_pack_p = false;
21577 /* In a template parameter, `>' is not an operator.
21579 [temp.param]
21581 When parsing a default template-argument for a non-type
21582 template-parameter, the first non-nested `>' is taken as the end
21583 of the template parameter-list rather than a greater-than
21584 operator. */
21586 /* Type definitions may not appear in parameter types. */
21587 saved_message = parser->type_definition_forbidden_message;
21588 parser->type_definition_forbidden_message
21589 = G_("types may not be defined in parameter types");
21591 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21592 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21593 (current_template_parms)) : 0);
21595 /* Parse the declaration-specifiers. */
21596 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21597 cp_parser_decl_specifier_seq (parser,
21598 CP_PARSER_FLAGS_NONE,
21599 &decl_specifiers,
21600 &declares_class_or_enum);
21602 /* Complain about missing 'typename' or other invalid type names. */
21603 if (!decl_specifiers.any_type_specifiers_p
21604 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21605 decl_specifiers.type = error_mark_node;
21607 /* If an error occurred, there's no reason to attempt to parse the
21608 rest of the declaration. */
21609 if (cp_parser_error_occurred (parser))
21611 parser->type_definition_forbidden_message = saved_message;
21612 return NULL;
21615 /* Peek at the next token. */
21616 token = cp_lexer_peek_token (parser->lexer);
21618 /* If the next token is a `)', `,', `=', `>', or `...', then there
21619 is no declarator. However, when variadic templates are enabled,
21620 there may be a declarator following `...'. */
21621 if (token->type == CPP_CLOSE_PAREN
21622 || token->type == CPP_COMMA
21623 || token->type == CPP_EQ
21624 || token->type == CPP_GREATER)
21626 declarator = NULL;
21627 if (parenthesized_p)
21628 *parenthesized_p = false;
21630 /* Otherwise, there should be a declarator. */
21631 else
21633 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21634 parser->default_arg_ok_p = false;
21636 /* After seeing a decl-specifier-seq, if the next token is not a
21637 "(", there is no possibility that the code is a valid
21638 expression. Therefore, if parsing tentatively, we commit at
21639 this point. */
21640 if (!parser->in_template_argument_list_p
21641 /* In an expression context, having seen:
21643 (int((char ...
21645 we cannot be sure whether we are looking at a
21646 function-type (taking a "char" as a parameter) or a cast
21647 of some object of type "char" to "int". */
21648 && !parser->in_type_id_in_expr_p
21649 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21650 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21651 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21652 cp_parser_commit_to_tentative_parse (parser);
21653 /* Parse the declarator. */
21654 declarator_token_start = token;
21655 declarator = cp_parser_declarator (parser,
21656 CP_PARSER_DECLARATOR_EITHER,
21657 /*ctor_dtor_or_conv_p=*/NULL,
21658 parenthesized_p,
21659 /*member_p=*/false,
21660 /*friend_p=*/false);
21661 parser->default_arg_ok_p = saved_default_arg_ok_p;
21662 /* After the declarator, allow more attributes. */
21663 decl_specifiers.attributes
21664 = attr_chainon (decl_specifiers.attributes,
21665 cp_parser_attributes_opt (parser));
21667 /* If the declarator is a template parameter pack, remember that and
21668 clear the flag in the declarator itself so we don't get errors
21669 from grokdeclarator. */
21670 if (template_parm_p && declarator && declarator->parameter_pack_p)
21672 declarator->parameter_pack_p = false;
21673 template_parameter_pack_p = true;
21677 /* If the next token is an ellipsis, and we have not seen a declarator
21678 name, and if either the type of the declarator contains parameter
21679 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21680 for, eg, abbreviated integral type names), then we actually have a
21681 parameter pack expansion expression. Otherwise, leave the ellipsis
21682 for a C-style variadic function. */
21683 token = cp_lexer_peek_token (parser->lexer);
21685 /* If a function parameter pack was specified and an implicit template
21686 parameter was introduced during cp_parser_parameter_declaration,
21687 change any implicit parameters introduced into packs. */
21688 if (parser->implicit_template_parms
21689 && (token->type == CPP_ELLIPSIS
21690 || (declarator && declarator->parameter_pack_p)))
21692 int latest_template_parm_idx = TREE_VEC_LENGTH
21693 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21695 if (latest_template_parm_idx != template_parm_idx)
21696 decl_specifiers.type = convert_generic_types_to_packs
21697 (decl_specifiers.type,
21698 template_parm_idx, latest_template_parm_idx);
21701 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21703 tree type = decl_specifiers.type;
21705 if (type && DECL_P (type))
21706 type = TREE_TYPE (type);
21708 if (((type
21709 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21710 && (template_parm_p || uses_parameter_packs (type)))
21711 || (!type && template_parm_p))
21712 && declarator_can_be_parameter_pack (declarator))
21714 /* Consume the `...'. */
21715 cp_lexer_consume_token (parser->lexer);
21716 maybe_warn_variadic_templates ();
21718 /* Build a pack expansion type */
21719 if (template_parm_p)
21720 template_parameter_pack_p = true;
21721 else if (declarator)
21722 declarator->parameter_pack_p = true;
21723 else
21724 decl_specifiers.type = make_pack_expansion (type);
21728 /* The restriction on defining new types applies only to the type
21729 of the parameter, not to the default argument. */
21730 parser->type_definition_forbidden_message = saved_message;
21732 /* If the next token is `=', then process a default argument. */
21733 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21735 tree type = decl_specifiers.type;
21736 token = cp_lexer_peek_token (parser->lexer);
21737 /* If we are defining a class, then the tokens that make up the
21738 default argument must be saved and processed later. */
21739 if (!template_parm_p && at_class_scope_p ()
21740 && TYPE_BEING_DEFINED (current_class_type)
21741 && !LAMBDA_TYPE_P (current_class_type))
21742 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21744 // A constrained-type-specifier may declare a type template-parameter.
21745 else if (declares_constrained_type_template_parameter (type))
21746 default_argument
21747 = cp_parser_default_type_template_argument (parser);
21749 // A constrained-type-specifier may declare a template-template-parameter.
21750 else if (declares_constrained_template_template_parameter (type))
21751 default_argument
21752 = cp_parser_default_template_template_argument (parser);
21754 /* Outside of a class definition, we can just parse the
21755 assignment-expression. */
21756 else
21757 default_argument
21758 = cp_parser_default_argument (parser, template_parm_p);
21760 if (!parser->default_arg_ok_p)
21762 permerror (token->location,
21763 "default arguments are only "
21764 "permitted for function parameters");
21766 else if ((declarator && declarator->parameter_pack_p)
21767 || template_parameter_pack_p
21768 || (decl_specifiers.type
21769 && PACK_EXPANSION_P (decl_specifiers.type)))
21771 /* Find the name of the parameter pack. */
21772 cp_declarator *id_declarator = declarator;
21773 while (id_declarator && id_declarator->kind != cdk_id)
21774 id_declarator = id_declarator->declarator;
21776 if (id_declarator && id_declarator->kind == cdk_id)
21777 error_at (declarator_token_start->location,
21778 template_parm_p
21779 ? G_("template parameter pack %qD "
21780 "cannot have a default argument")
21781 : G_("parameter pack %qD cannot have "
21782 "a default argument"),
21783 id_declarator->u.id.unqualified_name);
21784 else
21785 error_at (declarator_token_start->location,
21786 template_parm_p
21787 ? G_("template parameter pack cannot have "
21788 "a default argument")
21789 : G_("parameter pack cannot have a "
21790 "default argument"));
21792 default_argument = NULL_TREE;
21795 else
21796 default_argument = NULL_TREE;
21798 /* Generate a location for the parameter, ranging from the start of the
21799 initial token to the end of the final token (using input_location for
21800 the latter, set up by cp_lexer_set_source_position_from_token when
21801 consuming tokens).
21803 If we have a identifier, then use it for the caret location, e.g.
21805 extern int callee (int one, int (*two)(int, int), float three);
21806 ~~~~~~^~~~~~~~~~~~~~
21808 otherwise, reuse the start location for the caret location e.g.:
21810 extern int callee (int one, int (*)(int, int), float three);
21811 ^~~~~~~~~~~~~~~~~
21814 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21815 ? declarator->id_loc
21816 : decl_spec_token_start->location);
21817 location_t param_loc = make_location (caret_loc,
21818 decl_spec_token_start->location,
21819 input_location);
21821 return make_parameter_declarator (&decl_specifiers,
21822 declarator,
21823 default_argument,
21824 param_loc,
21825 template_parameter_pack_p);
21828 /* Parse a default argument and return it.
21830 TEMPLATE_PARM_P is true if this is a default argument for a
21831 non-type template parameter. */
21832 static tree
21833 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21835 tree default_argument = NULL_TREE;
21836 bool saved_greater_than_is_operator_p;
21837 bool saved_local_variables_forbidden_p;
21838 bool non_constant_p, is_direct_init;
21840 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21841 set correctly. */
21842 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21843 parser->greater_than_is_operator_p = !template_parm_p;
21844 /* Local variable names (and the `this' keyword) may not
21845 appear in a default argument. */
21846 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21847 parser->local_variables_forbidden_p = true;
21848 /* Parse the assignment-expression. */
21849 if (template_parm_p)
21850 push_deferring_access_checks (dk_no_deferred);
21851 tree saved_class_ptr = NULL_TREE;
21852 tree saved_class_ref = NULL_TREE;
21853 /* The "this" pointer is not valid in a default argument. */
21854 if (cfun)
21856 saved_class_ptr = current_class_ptr;
21857 cp_function_chain->x_current_class_ptr = NULL_TREE;
21858 saved_class_ref = current_class_ref;
21859 cp_function_chain->x_current_class_ref = NULL_TREE;
21861 default_argument
21862 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21863 /* Restore the "this" pointer. */
21864 if (cfun)
21866 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21867 cp_function_chain->x_current_class_ref = saved_class_ref;
21869 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21871 if (template_parm_p)
21872 pop_deferring_access_checks ();
21873 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21874 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21876 return default_argument;
21879 /* Parse a function-body.
21881 function-body:
21882 compound_statement */
21884 static void
21885 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21887 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21888 ? BCS_TRY_BLOCK : BCS_NORMAL),
21889 true);
21892 /* Parse a ctor-initializer-opt followed by a function-body. Return
21893 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21894 is true we are parsing a function-try-block. */
21896 static void
21897 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21898 bool in_function_try_block)
21900 tree body, list;
21901 const bool check_body_p =
21902 DECL_CONSTRUCTOR_P (current_function_decl)
21903 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21904 tree last = NULL;
21906 /* Begin the function body. */
21907 body = begin_function_body ();
21908 /* Parse the optional ctor-initializer. */
21909 cp_parser_ctor_initializer_opt (parser);
21911 /* If we're parsing a constexpr constructor definition, we need
21912 to check that the constructor body is indeed empty. However,
21913 before we get to cp_parser_function_body lot of junk has been
21914 generated, so we can't just check that we have an empty block.
21915 Rather we take a snapshot of the outermost block, and check whether
21916 cp_parser_function_body changed its state. */
21917 if (check_body_p)
21919 list = cur_stmt_list;
21920 if (STATEMENT_LIST_TAIL (list))
21921 last = STATEMENT_LIST_TAIL (list)->stmt;
21923 /* Parse the function-body. */
21924 cp_parser_function_body (parser, in_function_try_block);
21925 if (check_body_p)
21926 check_constexpr_ctor_body (last, list, /*complain=*/true);
21927 /* Finish the function body. */
21928 finish_function_body (body);
21931 /* Parse an initializer.
21933 initializer:
21934 = initializer-clause
21935 ( expression-list )
21937 Returns an expression representing the initializer. If no
21938 initializer is present, NULL_TREE is returned.
21940 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21941 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21942 set to TRUE if there is no initializer present. If there is an
21943 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21944 is set to true; otherwise it is set to false. */
21946 static tree
21947 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21948 bool* non_constant_p, bool subexpression_p)
21950 cp_token *token;
21951 tree init;
21953 /* Peek at the next token. */
21954 token = cp_lexer_peek_token (parser->lexer);
21956 /* Let our caller know whether or not this initializer was
21957 parenthesized. */
21958 *is_direct_init = (token->type != CPP_EQ);
21959 /* Assume that the initializer is constant. */
21960 *non_constant_p = false;
21962 if (token->type == CPP_EQ)
21964 /* Consume the `='. */
21965 cp_lexer_consume_token (parser->lexer);
21966 /* Parse the initializer-clause. */
21967 init = cp_parser_initializer_clause (parser, non_constant_p);
21969 else if (token->type == CPP_OPEN_PAREN)
21971 vec<tree, va_gc> *vec;
21972 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21973 /*cast_p=*/false,
21974 /*allow_expansion_p=*/true,
21975 non_constant_p);
21976 if (vec == NULL)
21977 return error_mark_node;
21978 init = build_tree_list_vec (vec);
21979 release_tree_vector (vec);
21981 else if (token->type == CPP_OPEN_BRACE)
21983 cp_lexer_set_source_position (parser->lexer);
21984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21985 init = cp_parser_braced_list (parser, non_constant_p);
21986 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21988 else
21990 /* Anything else is an error. */
21991 cp_parser_error (parser, "expected initializer");
21992 init = error_mark_node;
21995 if (!subexpression_p && check_for_bare_parameter_packs (init))
21996 init = error_mark_node;
21998 return init;
22001 /* Parse an initializer-clause.
22003 initializer-clause:
22004 assignment-expression
22005 braced-init-list
22007 Returns an expression representing the initializer.
22009 If the `assignment-expression' production is used the value
22010 returned is simply a representation for the expression.
22012 Otherwise, calls cp_parser_braced_list. */
22014 static cp_expr
22015 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22017 cp_expr initializer;
22019 /* Assume the expression is constant. */
22020 *non_constant_p = false;
22022 /* If it is not a `{', then we are looking at an
22023 assignment-expression. */
22024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22026 initializer
22027 = cp_parser_constant_expression (parser,
22028 /*allow_non_constant_p=*/true,
22029 non_constant_p);
22031 else
22032 initializer = cp_parser_braced_list (parser, non_constant_p);
22034 return initializer;
22037 /* Parse a brace-enclosed initializer list.
22039 braced-init-list:
22040 { initializer-list , [opt] }
22041 { designated-initializer-list , [opt] }
22044 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22045 the elements of the initializer-list (or NULL, if the last
22046 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22047 NULL_TREE. There is no way to detect whether or not the optional
22048 trailing `,' was provided. NON_CONSTANT_P is as for
22049 cp_parser_initializer. */
22051 static cp_expr
22052 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22054 tree initializer;
22055 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22057 /* Consume the `{' token. */
22058 matching_braces braces;
22059 braces.require_open (parser);
22060 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22061 initializer = make_node (CONSTRUCTOR);
22062 /* If it's not a `}', then there is a non-trivial initializer. */
22063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22065 /* Parse the initializer list. */
22066 CONSTRUCTOR_ELTS (initializer)
22067 = cp_parser_initializer_list (parser, non_constant_p);
22068 /* A trailing `,' token is allowed. */
22069 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22070 cp_lexer_consume_token (parser->lexer);
22072 else
22073 *non_constant_p = false;
22074 /* Now, there should be a trailing `}'. */
22075 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22076 braces.require_close (parser);
22077 TREE_TYPE (initializer) = init_list_type_node;
22079 cp_expr result (initializer);
22080 /* Build a location of the form:
22081 { ... }
22082 ^~~~~~~
22083 with caret==start at the open brace, finish at the close brace. */
22084 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22085 result.set_location (combined_loc);
22086 return result;
22089 /* Consume tokens up to, and including, the next non-nested closing `]'.
22090 Returns true iff we found a closing `]'. */
22092 static bool
22093 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22095 unsigned square_depth = 0;
22097 while (true)
22099 cp_token * token = cp_lexer_peek_token (parser->lexer);
22101 switch (token->type)
22103 case CPP_EOF:
22104 case CPP_PRAGMA_EOL:
22105 /* If we've run out of tokens, then there is no closing `]'. */
22106 return false;
22108 case CPP_OPEN_SQUARE:
22109 ++square_depth;
22110 break;
22112 case CPP_CLOSE_SQUARE:
22113 if (!square_depth--)
22115 cp_lexer_consume_token (parser->lexer);
22116 return true;
22118 break;
22120 default:
22121 break;
22124 /* Consume the token. */
22125 cp_lexer_consume_token (parser->lexer);
22129 /* Return true if we are looking at an array-designator, false otherwise. */
22131 static bool
22132 cp_parser_array_designator_p (cp_parser *parser)
22134 /* Consume the `['. */
22135 cp_lexer_consume_token (parser->lexer);
22137 cp_lexer_save_tokens (parser->lexer);
22139 /* Skip tokens until the next token is a closing square bracket.
22140 If we find the closing `]', and the next token is a `=', then
22141 we are looking at an array designator. */
22142 bool array_designator_p
22143 = (cp_parser_skip_to_closing_square_bracket (parser)
22144 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22146 /* Roll back the tokens we skipped. */
22147 cp_lexer_rollback_tokens (parser->lexer);
22149 return array_designator_p;
22152 /* Parse an initializer-list.
22154 initializer-list:
22155 initializer-clause ... [opt]
22156 initializer-list , initializer-clause ... [opt]
22158 C++2A Extension:
22160 designated-initializer-list:
22161 designated-initializer-clause
22162 designated-initializer-list , designated-initializer-clause
22164 designated-initializer-clause:
22165 designator brace-or-equal-initializer
22167 designator:
22168 . identifier
22170 GNU Extension:
22172 initializer-list:
22173 designation initializer-clause ...[opt]
22174 initializer-list , designation initializer-clause ...[opt]
22176 designation:
22177 . identifier =
22178 identifier :
22179 [ constant-expression ] =
22181 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22182 for the initializer. If the INDEX of the elt is non-NULL, it is the
22183 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22184 as for cp_parser_initializer. */
22186 static vec<constructor_elt, va_gc> *
22187 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22189 vec<constructor_elt, va_gc> *v = NULL;
22190 bool first_p = true;
22191 tree first_designator = NULL_TREE;
22193 /* Assume all of the expressions are constant. */
22194 *non_constant_p = false;
22196 /* Parse the rest of the list. */
22197 while (true)
22199 cp_token *token;
22200 tree designator;
22201 tree initializer;
22202 bool clause_non_constant_p;
22203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22205 /* Handle the C++2A syntax, '. id ='. */
22206 if ((cxx_dialect >= cxx2a
22207 || cp_parser_allow_gnu_extensions_p (parser))
22208 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22209 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22210 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22211 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22212 == CPP_OPEN_BRACE)))
22214 if (cxx_dialect < cxx2a)
22215 pedwarn (loc, OPT_Wpedantic,
22216 "C++ designated initializers only available with "
22217 "-std=c++2a or -std=gnu++2a");
22218 /* Consume the `.'. */
22219 cp_lexer_consume_token (parser->lexer);
22220 /* Consume the identifier. */
22221 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22222 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22223 /* Consume the `='. */
22224 cp_lexer_consume_token (parser->lexer);
22226 /* Also, if the next token is an identifier and the following one is a
22227 colon, we are looking at the GNU designated-initializer
22228 syntax. */
22229 else if (cp_parser_allow_gnu_extensions_p (parser)
22230 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22231 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22232 == CPP_COLON))
22234 /* Warn the user that they are using an extension. */
22235 pedwarn (loc, OPT_Wpedantic,
22236 "ISO C++ does not allow GNU designated initializers");
22237 /* Consume the identifier. */
22238 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22239 /* Consume the `:'. */
22240 cp_lexer_consume_token (parser->lexer);
22242 /* Also handle C99 array designators, '[ const ] ='. */
22243 else if (cp_parser_allow_gnu_extensions_p (parser)
22244 && !c_dialect_objc ()
22245 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22247 /* In C++11, [ could start a lambda-introducer. */
22248 bool non_const = false;
22250 cp_parser_parse_tentatively (parser);
22252 if (!cp_parser_array_designator_p (parser))
22254 cp_parser_simulate_error (parser);
22255 designator = NULL_TREE;
22257 else
22259 designator = cp_parser_constant_expression (parser, true,
22260 &non_const);
22261 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22262 cp_parser_require (parser, CPP_EQ, RT_EQ);
22265 if (!cp_parser_parse_definitely (parser))
22266 designator = NULL_TREE;
22267 else if (non_const
22268 && (!require_potential_rvalue_constant_expression
22269 (designator)))
22270 designator = NULL_TREE;
22271 if (designator)
22272 /* Warn the user that they are using an extension. */
22273 pedwarn (loc, OPT_Wpedantic,
22274 "ISO C++ does not allow C99 designated initializers");
22276 else
22277 designator = NULL_TREE;
22279 if (first_p)
22281 first_designator = designator;
22282 first_p = false;
22284 else if (cxx_dialect >= cxx2a
22285 && first_designator != error_mark_node
22286 && (!first_designator != !designator))
22288 error_at (loc, "either all initializer clauses should be designated "
22289 "or none of them should be");
22290 first_designator = error_mark_node;
22292 else if (cxx_dialect < cxx2a && !first_designator)
22293 first_designator = designator;
22295 /* Parse the initializer. */
22296 initializer = cp_parser_initializer_clause (parser,
22297 &clause_non_constant_p);
22298 /* If any clause is non-constant, so is the entire initializer. */
22299 if (clause_non_constant_p)
22300 *non_constant_p = true;
22302 /* If we have an ellipsis, this is an initializer pack
22303 expansion. */
22304 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22306 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22308 /* Consume the `...'. */
22309 cp_lexer_consume_token (parser->lexer);
22311 if (designator && cxx_dialect >= cxx2a)
22312 error_at (loc,
22313 "%<...%> not allowed in designated initializer list");
22315 /* Turn the initializer into an initializer expansion. */
22316 initializer = make_pack_expansion (initializer);
22319 /* Add it to the vector. */
22320 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22322 /* If the next token is not a comma, we have reached the end of
22323 the list. */
22324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22325 break;
22327 /* Peek at the next token. */
22328 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22329 /* If the next token is a `}', then we're still done. An
22330 initializer-clause can have a trailing `,' after the
22331 initializer-list and before the closing `}'. */
22332 if (token->type == CPP_CLOSE_BRACE)
22333 break;
22335 /* Consume the `,' token. */
22336 cp_lexer_consume_token (parser->lexer);
22339 /* The same identifier shall not appear in multiple designators
22340 of a designated-initializer-list. */
22341 if (first_designator)
22343 unsigned int i;
22344 tree designator, val;
22345 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22346 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22348 if (IDENTIFIER_MARKED (designator))
22350 error_at (EXPR_LOC_OR_LOC (val, input_location),
22351 "%<.%s%> designator used multiple times in "
22352 "the same initializer list",
22353 IDENTIFIER_POINTER (designator));
22354 (*v)[i].index = NULL_TREE;
22356 else
22357 IDENTIFIER_MARKED (designator) = 1;
22359 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22360 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22361 IDENTIFIER_MARKED (designator) = 0;
22364 return v;
22367 /* Classes [gram.class] */
22369 /* Parse a class-name.
22371 class-name:
22372 identifier
22373 template-id
22375 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22376 to indicate that names looked up in dependent types should be
22377 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22378 keyword has been used to indicate that the name that appears next
22379 is a template. TAG_TYPE indicates the explicit tag given before
22380 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22381 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22382 is the class being defined in a class-head. If ENUM_OK is TRUE,
22383 enum-names are also accepted.
22385 Returns the TYPE_DECL representing the class. */
22387 static tree
22388 cp_parser_class_name (cp_parser *parser,
22389 bool typename_keyword_p,
22390 bool template_keyword_p,
22391 enum tag_types tag_type,
22392 bool check_dependency_p,
22393 bool class_head_p,
22394 bool is_declaration,
22395 bool enum_ok)
22397 tree decl;
22398 tree scope;
22399 bool typename_p;
22400 cp_token *token;
22401 tree identifier = NULL_TREE;
22403 /* All class-names start with an identifier. */
22404 token = cp_lexer_peek_token (parser->lexer);
22405 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22407 cp_parser_error (parser, "expected class-name");
22408 return error_mark_node;
22411 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22412 to a template-id, so we save it here. */
22413 scope = parser->scope;
22414 if (scope == error_mark_node)
22415 return error_mark_node;
22417 /* Any name names a type if we're following the `typename' keyword
22418 in a qualified name where the enclosing scope is type-dependent. */
22419 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22420 && dependent_type_p (scope));
22421 /* Handle the common case (an identifier, but not a template-id)
22422 efficiently. */
22423 if (token->type == CPP_NAME
22424 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22426 cp_token *identifier_token;
22427 bool ambiguous_p;
22429 /* Look for the identifier. */
22430 identifier_token = cp_lexer_peek_token (parser->lexer);
22431 ambiguous_p = identifier_token->error_reported;
22432 identifier = cp_parser_identifier (parser);
22433 /* If the next token isn't an identifier, we are certainly not
22434 looking at a class-name. */
22435 if (identifier == error_mark_node)
22436 decl = error_mark_node;
22437 /* If we know this is a type-name, there's no need to look it
22438 up. */
22439 else if (typename_p)
22440 decl = identifier;
22441 else
22443 tree ambiguous_decls;
22444 /* If we already know that this lookup is ambiguous, then
22445 we've already issued an error message; there's no reason
22446 to check again. */
22447 if (ambiguous_p)
22449 cp_parser_simulate_error (parser);
22450 return error_mark_node;
22452 /* If the next token is a `::', then the name must be a type
22453 name.
22455 [basic.lookup.qual]
22457 During the lookup for a name preceding the :: scope
22458 resolution operator, object, function, and enumerator
22459 names are ignored. */
22460 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22461 tag_type = scope_type;
22462 /* Look up the name. */
22463 decl = cp_parser_lookup_name (parser, identifier,
22464 tag_type,
22465 /*is_template=*/false,
22466 /*is_namespace=*/false,
22467 check_dependency_p,
22468 &ambiguous_decls,
22469 identifier_token->location);
22470 if (ambiguous_decls)
22472 if (cp_parser_parsing_tentatively (parser))
22473 cp_parser_simulate_error (parser);
22474 return error_mark_node;
22478 else
22480 /* Try a template-id. */
22481 decl = cp_parser_template_id (parser, template_keyword_p,
22482 check_dependency_p,
22483 tag_type,
22484 is_declaration);
22485 if (decl == error_mark_node)
22486 return error_mark_node;
22489 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22491 /* If this is a typename, create a TYPENAME_TYPE. */
22492 if (typename_p && decl != error_mark_node)
22494 decl = make_typename_type (scope, decl, typename_type,
22495 /*complain=*/tf_error);
22496 if (decl != error_mark_node)
22497 decl = TYPE_NAME (decl);
22500 decl = strip_using_decl (decl);
22502 /* Check to see that it is really the name of a class. */
22503 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22504 && identifier_p (TREE_OPERAND (decl, 0))
22505 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22506 /* Situations like this:
22508 template <typename T> struct A {
22509 typename T::template X<int>::I i;
22512 are problematic. Is `T::template X<int>' a class-name? The
22513 standard does not seem to be definitive, but there is no other
22514 valid interpretation of the following `::'. Therefore, those
22515 names are considered class-names. */
22517 decl = make_typename_type (scope, decl, tag_type, tf_error);
22518 if (decl != error_mark_node)
22519 decl = TYPE_NAME (decl);
22521 else if (TREE_CODE (decl) != TYPE_DECL
22522 || TREE_TYPE (decl) == error_mark_node
22523 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22524 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22525 /* In Objective-C 2.0, a classname followed by '.' starts a
22526 dot-syntax expression, and it's not a type-name. */
22527 || (c_dialect_objc ()
22528 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22529 && objc_is_class_name (decl)))
22530 decl = error_mark_node;
22532 if (decl == error_mark_node)
22533 cp_parser_error (parser, "expected class-name");
22534 else if (identifier && !parser->scope)
22535 maybe_note_name_used_in_class (identifier, decl);
22537 return decl;
22540 /* Parse a class-specifier.
22542 class-specifier:
22543 class-head { member-specification [opt] }
22545 Returns the TREE_TYPE representing the class. */
22547 static tree
22548 cp_parser_class_specifier_1 (cp_parser* parser)
22550 tree type;
22551 tree attributes = NULL_TREE;
22552 bool nested_name_specifier_p;
22553 unsigned saved_num_template_parameter_lists;
22554 bool saved_in_function_body;
22555 unsigned char in_statement;
22556 bool in_switch_statement_p;
22557 bool saved_in_unbraced_linkage_specification_p;
22558 tree old_scope = NULL_TREE;
22559 tree scope = NULL_TREE;
22560 cp_token *closing_brace;
22562 push_deferring_access_checks (dk_no_deferred);
22564 /* Parse the class-head. */
22565 type = cp_parser_class_head (parser,
22566 &nested_name_specifier_p);
22567 /* If the class-head was a semantic disaster, skip the entire body
22568 of the class. */
22569 if (!type)
22571 cp_parser_skip_to_end_of_block_or_statement (parser);
22572 pop_deferring_access_checks ();
22573 return error_mark_node;
22576 /* Look for the `{'. */
22577 matching_braces braces;
22578 if (!braces.require_open (parser))
22580 pop_deferring_access_checks ();
22581 return error_mark_node;
22584 cp_ensure_no_omp_declare_simd (parser);
22585 cp_ensure_no_oacc_routine (parser);
22587 /* Issue an error message if type-definitions are forbidden here. */
22588 cp_parser_check_type_definition (parser);
22589 /* Remember that we are defining one more class. */
22590 ++parser->num_classes_being_defined;
22591 /* Inside the class, surrounding template-parameter-lists do not
22592 apply. */
22593 saved_num_template_parameter_lists
22594 = parser->num_template_parameter_lists;
22595 parser->num_template_parameter_lists = 0;
22596 /* We are not in a function body. */
22597 saved_in_function_body = parser->in_function_body;
22598 parser->in_function_body = false;
22599 /* Or in a loop. */
22600 in_statement = parser->in_statement;
22601 parser->in_statement = 0;
22602 /* Or in a switch. */
22603 in_switch_statement_p = parser->in_switch_statement_p;
22604 parser->in_switch_statement_p = false;
22605 /* We are not immediately inside an extern "lang" block. */
22606 saved_in_unbraced_linkage_specification_p
22607 = parser->in_unbraced_linkage_specification_p;
22608 parser->in_unbraced_linkage_specification_p = false;
22610 // Associate constraints with the type.
22611 if (flag_concepts)
22612 type = associate_classtype_constraints (type);
22614 /* Start the class. */
22615 if (nested_name_specifier_p)
22617 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22618 old_scope = push_inner_scope (scope);
22620 type = begin_class_definition (type);
22622 if (type == error_mark_node)
22623 /* If the type is erroneous, skip the entire body of the class. */
22624 cp_parser_skip_to_closing_brace (parser);
22625 else
22626 /* Parse the member-specification. */
22627 cp_parser_member_specification_opt (parser);
22629 /* Look for the trailing `}'. */
22630 closing_brace = braces.require_close (parser);
22631 /* Look for trailing attributes to apply to this class. */
22632 if (cp_parser_allow_gnu_extensions_p (parser))
22633 attributes = cp_parser_gnu_attributes_opt (parser);
22634 if (type != error_mark_node)
22635 type = finish_struct (type, attributes);
22636 if (nested_name_specifier_p)
22637 pop_inner_scope (old_scope, scope);
22639 /* We've finished a type definition. Check for the common syntax
22640 error of forgetting a semicolon after the definition. We need to
22641 be careful, as we can't just check for not-a-semicolon and be done
22642 with it; the user might have typed:
22644 class X { } c = ...;
22645 class X { } *p = ...;
22647 and so forth. Instead, enumerate all the possible tokens that
22648 might follow this production; if we don't see one of them, then
22649 complain and silently insert the semicolon. */
22651 cp_token *token = cp_lexer_peek_token (parser->lexer);
22652 bool want_semicolon = true;
22654 if (cp_next_tokens_can_be_std_attribute_p (parser))
22655 /* Don't try to parse c++11 attributes here. As per the
22656 grammar, that should be a task for
22657 cp_parser_decl_specifier_seq. */
22658 want_semicolon = false;
22660 switch (token->type)
22662 case CPP_NAME:
22663 case CPP_SEMICOLON:
22664 case CPP_MULT:
22665 case CPP_AND:
22666 case CPP_OPEN_PAREN:
22667 case CPP_CLOSE_PAREN:
22668 case CPP_COMMA:
22669 want_semicolon = false;
22670 break;
22672 /* While it's legal for type qualifiers and storage class
22673 specifiers to follow type definitions in the grammar, only
22674 compiler testsuites contain code like that. Assume that if
22675 we see such code, then what we're really seeing is a case
22676 like:
22678 class X { }
22679 const <type> var = ...;
22683 class Y { }
22684 static <type> func (...) ...
22686 i.e. the qualifier or specifier applies to the next
22687 declaration. To do so, however, we need to look ahead one
22688 more token to see if *that* token is a type specifier.
22690 This code could be improved to handle:
22692 class Z { }
22693 static const <type> var = ...; */
22694 case CPP_KEYWORD:
22695 if (keyword_is_decl_specifier (token->keyword))
22697 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22699 /* Handling user-defined types here would be nice, but very
22700 tricky. */
22701 want_semicolon
22702 = (lookahead->type == CPP_KEYWORD
22703 && keyword_begins_type_specifier (lookahead->keyword));
22705 break;
22706 default:
22707 break;
22710 /* If we don't have a type, then something is very wrong and we
22711 shouldn't try to do anything clever. Likewise for not seeing the
22712 closing brace. */
22713 if (closing_brace && TYPE_P (type) && want_semicolon)
22715 /* Locate the closing brace. */
22716 cp_token_position prev
22717 = cp_lexer_previous_token_position (parser->lexer);
22718 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22719 location_t loc = prev_token->location;
22721 /* We want to suggest insertion of a ';' immediately *after* the
22722 closing brace, so, if we can, offset the location by 1 column. */
22723 location_t next_loc = loc;
22724 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22725 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22727 rich_location richloc (line_table, next_loc);
22729 /* If we successfully offset the location, suggest the fix-it. */
22730 if (next_loc != loc)
22731 richloc.add_fixit_insert_before (next_loc, ";");
22733 if (CLASSTYPE_DECLARED_CLASS (type))
22734 error_at (&richloc,
22735 "expected %<;%> after class definition");
22736 else if (TREE_CODE (type) == RECORD_TYPE)
22737 error_at (&richloc,
22738 "expected %<;%> after struct definition");
22739 else if (TREE_CODE (type) == UNION_TYPE)
22740 error_at (&richloc,
22741 "expected %<;%> after union definition");
22742 else
22743 gcc_unreachable ();
22745 /* Unget one token and smash it to look as though we encountered
22746 a semicolon in the input stream. */
22747 cp_lexer_set_token_position (parser->lexer, prev);
22748 token = cp_lexer_peek_token (parser->lexer);
22749 token->type = CPP_SEMICOLON;
22750 token->keyword = RID_MAX;
22754 /* If this class is not itself within the scope of another class,
22755 then we need to parse the bodies of all of the queued function
22756 definitions. Note that the queued functions defined in a class
22757 are not always processed immediately following the
22758 class-specifier for that class. Consider:
22760 struct A {
22761 struct B { void f() { sizeof (A); } };
22764 If `f' were processed before the processing of `A' were
22765 completed, there would be no way to compute the size of `A'.
22766 Note that the nesting we are interested in here is lexical --
22767 not the semantic nesting given by TYPE_CONTEXT. In particular,
22768 for:
22770 struct A { struct B; };
22771 struct A::B { void f() { } };
22773 there is no need to delay the parsing of `A::B::f'. */
22774 if (--parser->num_classes_being_defined == 0)
22776 tree decl;
22777 tree class_type = NULL_TREE;
22778 tree pushed_scope = NULL_TREE;
22779 unsigned ix;
22780 cp_default_arg_entry *e;
22781 tree save_ccp, save_ccr;
22783 if (any_erroneous_template_args_p (type))
22785 /* Skip default arguments, NSDMIs, etc, in order to improve
22786 error recovery (c++/71169, c++/71832). */
22787 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22788 vec_safe_truncate (unparsed_nsdmis, 0);
22789 vec_safe_truncate (unparsed_classes, 0);
22790 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22793 /* In a first pass, parse default arguments to the functions.
22794 Then, in a second pass, parse the bodies of the functions.
22795 This two-phased approach handles cases like:
22797 struct S {
22798 void f() { g(); }
22799 void g(int i = 3);
22803 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22805 decl = e->decl;
22806 /* If there are default arguments that have not yet been processed,
22807 take care of them now. */
22808 if (class_type != e->class_type)
22810 if (pushed_scope)
22811 pop_scope (pushed_scope);
22812 class_type = e->class_type;
22813 pushed_scope = push_scope (class_type);
22815 /* Make sure that any template parameters are in scope. */
22816 maybe_begin_member_template_processing (decl);
22817 /* Parse the default argument expressions. */
22818 cp_parser_late_parsing_default_args (parser, decl);
22819 /* Remove any template parameters from the symbol table. */
22820 maybe_end_member_template_processing ();
22822 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22823 /* Now parse any NSDMIs. */
22824 save_ccp = current_class_ptr;
22825 save_ccr = current_class_ref;
22826 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22828 if (class_type != DECL_CONTEXT (decl))
22830 if (pushed_scope)
22831 pop_scope (pushed_scope);
22832 class_type = DECL_CONTEXT (decl);
22833 pushed_scope = push_scope (class_type);
22835 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22836 cp_parser_late_parsing_nsdmi (parser, decl);
22838 vec_safe_truncate (unparsed_nsdmis, 0);
22839 current_class_ptr = save_ccp;
22840 current_class_ref = save_ccr;
22841 if (pushed_scope)
22842 pop_scope (pushed_scope);
22844 /* Now do some post-NSDMI bookkeeping. */
22845 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22846 after_nsdmi_defaulted_late_checks (class_type);
22847 vec_safe_truncate (unparsed_classes, 0);
22848 after_nsdmi_defaulted_late_checks (type);
22850 /* Now parse the body of the functions. */
22851 if (flag_openmp)
22853 /* OpenMP UDRs need to be parsed before all other functions. */
22854 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22855 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22856 cp_parser_late_parsing_for_member (parser, decl);
22857 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22858 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22859 cp_parser_late_parsing_for_member (parser, decl);
22861 else
22862 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22863 cp_parser_late_parsing_for_member (parser, decl);
22864 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22866 else
22867 vec_safe_push (unparsed_classes, type);
22869 /* Put back any saved access checks. */
22870 pop_deferring_access_checks ();
22872 /* Restore saved state. */
22873 parser->in_switch_statement_p = in_switch_statement_p;
22874 parser->in_statement = in_statement;
22875 parser->in_function_body = saved_in_function_body;
22876 parser->num_template_parameter_lists
22877 = saved_num_template_parameter_lists;
22878 parser->in_unbraced_linkage_specification_p
22879 = saved_in_unbraced_linkage_specification_p;
22881 return type;
22884 static tree
22885 cp_parser_class_specifier (cp_parser* parser)
22887 tree ret;
22888 timevar_push (TV_PARSE_STRUCT);
22889 ret = cp_parser_class_specifier_1 (parser);
22890 timevar_pop (TV_PARSE_STRUCT);
22891 return ret;
22894 /* Parse a class-head.
22896 class-head:
22897 class-key identifier [opt] base-clause [opt]
22898 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22899 class-key nested-name-specifier [opt] template-id
22900 base-clause [opt]
22902 class-virt-specifier:
22903 final
22905 GNU Extensions:
22906 class-key attributes identifier [opt] base-clause [opt]
22907 class-key attributes nested-name-specifier identifier base-clause [opt]
22908 class-key attributes nested-name-specifier [opt] template-id
22909 base-clause [opt]
22911 Upon return BASES is initialized to the list of base classes (or
22912 NULL, if there are none) in the same form returned by
22913 cp_parser_base_clause.
22915 Returns the TYPE of the indicated class. Sets
22916 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22917 involving a nested-name-specifier was used, and FALSE otherwise.
22919 Returns error_mark_node if this is not a class-head.
22921 Returns NULL_TREE if the class-head is syntactically valid, but
22922 semantically invalid in a way that means we should skip the entire
22923 body of the class. */
22925 static tree
22926 cp_parser_class_head (cp_parser* parser,
22927 bool* nested_name_specifier_p)
22929 tree nested_name_specifier;
22930 enum tag_types class_key;
22931 tree id = NULL_TREE;
22932 tree type = NULL_TREE;
22933 tree attributes;
22934 tree bases;
22935 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22936 bool template_id_p = false;
22937 bool qualified_p = false;
22938 bool invalid_nested_name_p = false;
22939 bool invalid_explicit_specialization_p = false;
22940 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22941 tree pushed_scope = NULL_TREE;
22942 unsigned num_templates;
22943 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22944 /* Assume no nested-name-specifier will be present. */
22945 *nested_name_specifier_p = false;
22946 /* Assume no template parameter lists will be used in defining the
22947 type. */
22948 num_templates = 0;
22949 parser->colon_corrects_to_scope_p = false;
22951 /* Look for the class-key. */
22952 class_key = cp_parser_class_key (parser);
22953 if (class_key == none_type)
22954 return error_mark_node;
22956 location_t class_head_start_location = input_location;
22958 /* Parse the attributes. */
22959 attributes = cp_parser_attributes_opt (parser);
22961 /* If the next token is `::', that is invalid -- but sometimes
22962 people do try to write:
22964 struct ::S {};
22966 Handle this gracefully by accepting the extra qualifier, and then
22967 issuing an error about it later if this really is a
22968 class-head. If it turns out just to be an elaborated type
22969 specifier, remain silent. */
22970 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22971 qualified_p = true;
22973 push_deferring_access_checks (dk_no_check);
22975 /* Determine the name of the class. Begin by looking for an
22976 optional nested-name-specifier. */
22977 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22978 nested_name_specifier
22979 = cp_parser_nested_name_specifier_opt (parser,
22980 /*typename_keyword_p=*/false,
22981 /*check_dependency_p=*/false,
22982 /*type_p=*/true,
22983 /*is_declaration=*/false);
22984 /* If there was a nested-name-specifier, then there *must* be an
22985 identifier. */
22987 cp_token *bad_template_keyword = NULL;
22989 if (nested_name_specifier)
22991 type_start_token = cp_lexer_peek_token (parser->lexer);
22992 /* Although the grammar says `identifier', it really means
22993 `class-name' or `template-name'. You are only allowed to
22994 define a class that has already been declared with this
22995 syntax.
22997 The proposed resolution for Core Issue 180 says that wherever
22998 you see `class T::X' you should treat `X' as a type-name.
23000 It is OK to define an inaccessible class; for example:
23002 class A { class B; };
23003 class A::B {};
23005 We do not know if we will see a class-name, or a
23006 template-name. We look for a class-name first, in case the
23007 class-name is a template-id; if we looked for the
23008 template-name first we would stop after the template-name. */
23009 cp_parser_parse_tentatively (parser);
23010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23011 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23012 type = cp_parser_class_name (parser,
23013 /*typename_keyword_p=*/false,
23014 /*template_keyword_p=*/false,
23015 class_type,
23016 /*check_dependency_p=*/false,
23017 /*class_head_p=*/true,
23018 /*is_declaration=*/false);
23019 /* If that didn't work, ignore the nested-name-specifier. */
23020 if (!cp_parser_parse_definitely (parser))
23022 invalid_nested_name_p = true;
23023 type_start_token = cp_lexer_peek_token (parser->lexer);
23024 id = cp_parser_identifier (parser);
23025 if (id == error_mark_node)
23026 id = NULL_TREE;
23028 /* If we could not find a corresponding TYPE, treat this
23029 declaration like an unqualified declaration. */
23030 if (type == error_mark_node)
23031 nested_name_specifier = NULL_TREE;
23032 /* Otherwise, count the number of templates used in TYPE and its
23033 containing scopes. */
23034 else
23035 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23037 /* Otherwise, the identifier is optional. */
23038 else
23040 /* We don't know whether what comes next is a template-id,
23041 an identifier, or nothing at all. */
23042 cp_parser_parse_tentatively (parser);
23043 /* Check for a template-id. */
23044 type_start_token = cp_lexer_peek_token (parser->lexer);
23045 id = cp_parser_template_id (parser,
23046 /*template_keyword_p=*/false,
23047 /*check_dependency_p=*/true,
23048 class_key,
23049 /*is_declaration=*/true);
23050 /* If that didn't work, it could still be an identifier. */
23051 if (!cp_parser_parse_definitely (parser))
23053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23055 type_start_token = cp_lexer_peek_token (parser->lexer);
23056 id = cp_parser_identifier (parser);
23058 else
23059 id = NULL_TREE;
23061 else
23063 template_id_p = true;
23064 ++num_templates;
23068 pop_deferring_access_checks ();
23070 if (id)
23072 cp_parser_check_for_invalid_template_id (parser, id,
23073 class_key,
23074 type_start_token->location);
23076 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23078 /* If it's not a `:' or a `{' then we can't really be looking at a
23079 class-head, since a class-head only appears as part of a
23080 class-specifier. We have to detect this situation before calling
23081 xref_tag, since that has irreversible side-effects. */
23082 if (!cp_parser_next_token_starts_class_definition_p (parser))
23084 cp_parser_error (parser, "expected %<{%> or %<:%>");
23085 type = error_mark_node;
23086 goto out;
23089 /* At this point, we're going ahead with the class-specifier, even
23090 if some other problem occurs. */
23091 cp_parser_commit_to_tentative_parse (parser);
23092 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23094 cp_parser_error (parser,
23095 "cannot specify %<override%> for a class");
23096 type = error_mark_node;
23097 goto out;
23099 /* Issue the error about the overly-qualified name now. */
23100 if (qualified_p)
23102 cp_parser_error (parser,
23103 "global qualification of class name is invalid");
23104 type = error_mark_node;
23105 goto out;
23107 else if (invalid_nested_name_p)
23109 cp_parser_error (parser,
23110 "qualified name does not name a class");
23111 type = error_mark_node;
23112 goto out;
23114 else if (nested_name_specifier)
23116 tree scope;
23118 if (bad_template_keyword)
23119 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23120 keyword template shall not appear at the top level. */
23121 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23122 "keyword %<template%> not allowed in class-head-name");
23124 /* Reject typedef-names in class heads. */
23125 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23127 error_at (type_start_token->location,
23128 "invalid class name in declaration of %qD",
23129 type);
23130 type = NULL_TREE;
23131 goto done;
23134 /* Figure out in what scope the declaration is being placed. */
23135 scope = current_scope ();
23136 /* If that scope does not contain the scope in which the
23137 class was originally declared, the program is invalid. */
23138 if (scope && !is_ancestor (scope, nested_name_specifier))
23140 if (at_namespace_scope_p ())
23141 error_at (type_start_token->location,
23142 "declaration of %qD in namespace %qD which does not "
23143 "enclose %qD",
23144 type, scope, nested_name_specifier);
23145 else
23146 error_at (type_start_token->location,
23147 "declaration of %qD in %qD which does not enclose %qD",
23148 type, scope, nested_name_specifier);
23149 type = NULL_TREE;
23150 goto done;
23152 /* [dcl.meaning]
23154 A declarator-id shall not be qualified except for the
23155 definition of a ... nested class outside of its class
23156 ... [or] the definition or explicit instantiation of a
23157 class member of a namespace outside of its namespace. */
23158 if (scope == nested_name_specifier)
23160 permerror (nested_name_specifier_token_start->location,
23161 "extra qualification not allowed");
23162 nested_name_specifier = NULL_TREE;
23163 num_templates = 0;
23166 /* An explicit-specialization must be preceded by "template <>". If
23167 it is not, try to recover gracefully. */
23168 if (at_namespace_scope_p ()
23169 && parser->num_template_parameter_lists == 0
23170 && !processing_template_parmlist
23171 && template_id_p)
23173 /* Build a location of this form:
23174 struct typename <ARGS>
23175 ^~~~~~~~~~~~~~~~~~~~~~
23176 with caret==start at the start token, and
23177 finishing at the end of the type. */
23178 location_t reported_loc
23179 = make_location (class_head_start_location,
23180 class_head_start_location,
23181 get_finish (type_start_token->location));
23182 rich_location richloc (line_table, reported_loc);
23183 richloc.add_fixit_insert_before (class_head_start_location,
23184 "template <> ");
23185 error_at (&richloc,
23186 "an explicit specialization must be preceded by"
23187 " %<template <>%>");
23188 invalid_explicit_specialization_p = true;
23189 /* Take the same action that would have been taken by
23190 cp_parser_explicit_specialization. */
23191 ++parser->num_template_parameter_lists;
23192 begin_specialization ();
23194 /* There must be no "return" statements between this point and the
23195 end of this function; set "type "to the correct return value and
23196 use "goto done;" to return. */
23197 /* Make sure that the right number of template parameters were
23198 present. */
23199 if (!cp_parser_check_template_parameters (parser, num_templates,
23200 template_id_p,
23201 type_start_token->location,
23202 /*declarator=*/NULL))
23204 /* If something went wrong, there is no point in even trying to
23205 process the class-definition. */
23206 type = NULL_TREE;
23207 goto done;
23210 /* Look up the type. */
23211 if (template_id_p)
23213 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23214 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23215 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23217 error_at (type_start_token->location,
23218 "function template %qD redeclared as a class template", id);
23219 type = error_mark_node;
23221 else
23223 type = TREE_TYPE (id);
23224 type = maybe_process_partial_specialization (type);
23226 /* Check the scope while we still know whether or not we had a
23227 nested-name-specifier. */
23228 if (type != error_mark_node)
23229 check_unqualified_spec_or_inst (type, type_start_token->location);
23231 if (nested_name_specifier)
23232 pushed_scope = push_scope (nested_name_specifier);
23234 else if (nested_name_specifier)
23236 tree class_type;
23238 /* Given:
23240 template <typename T> struct S { struct T };
23241 template <typename T> struct S<T>::T { };
23243 we will get a TYPENAME_TYPE when processing the definition of
23244 `S::T'. We need to resolve it to the actual type before we
23245 try to define it. */
23246 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23248 class_type = resolve_typename_type (TREE_TYPE (type),
23249 /*only_current_p=*/false);
23250 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23251 type = TYPE_NAME (class_type);
23252 else
23254 cp_parser_error (parser, "could not resolve typename type");
23255 type = error_mark_node;
23259 if (maybe_process_partial_specialization (TREE_TYPE (type))
23260 == error_mark_node)
23262 type = NULL_TREE;
23263 goto done;
23266 class_type = current_class_type;
23267 /* Enter the scope indicated by the nested-name-specifier. */
23268 pushed_scope = push_scope (nested_name_specifier);
23269 /* Get the canonical version of this type. */
23270 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23271 /* Call push_template_decl if it seems like we should be defining a
23272 template either from the template headers or the type we're
23273 defining, so that we diagnose both extra and missing headers. */
23274 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23275 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23278 type = push_template_decl (type);
23279 if (type == error_mark_node)
23281 type = NULL_TREE;
23282 goto done;
23286 type = TREE_TYPE (type);
23287 *nested_name_specifier_p = true;
23289 else /* The name is not a nested name. */
23291 /* If the class was unnamed, create a dummy name. */
23292 if (!id)
23293 id = make_anon_name ();
23294 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23295 ? ts_within_enclosing_non_class
23296 : ts_current);
23297 type = xref_tag (class_key, id, tag_scope,
23298 parser->num_template_parameter_lists);
23301 /* Indicate whether this class was declared as a `class' or as a
23302 `struct'. */
23303 if (TREE_CODE (type) == RECORD_TYPE)
23304 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23305 cp_parser_check_class_key (class_key, type);
23307 /* If this type was already complete, and we see another definition,
23308 that's an error. */
23309 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23311 error_at (type_start_token->location, "redefinition of %q#T",
23312 type);
23313 inform (location_of (type), "previous definition of %q#T",
23314 type);
23315 type = NULL_TREE;
23316 goto done;
23318 else if (type == error_mark_node)
23319 type = NULL_TREE;
23321 if (type)
23323 /* Apply attributes now, before any use of the class as a template
23324 argument in its base list. */
23325 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23326 fixup_attribute_variants (type);
23329 /* We will have entered the scope containing the class; the names of
23330 base classes should be looked up in that context. For example:
23332 struct A { struct B {}; struct C; };
23333 struct A::C : B {};
23335 is valid. */
23337 /* Get the list of base-classes, if there is one. */
23338 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23340 /* PR59482: enter the class scope so that base-specifiers are looked
23341 up correctly. */
23342 if (type)
23343 pushclass (type);
23344 bases = cp_parser_base_clause (parser);
23345 /* PR59482: get out of the previously pushed class scope so that the
23346 subsequent pops pop the right thing. */
23347 if (type)
23348 popclass ();
23350 else
23351 bases = NULL_TREE;
23353 /* If we're really defining a class, process the base classes.
23354 If they're invalid, fail. */
23355 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23356 xref_basetypes (type, bases);
23358 done:
23359 /* Leave the scope given by the nested-name-specifier. We will
23360 enter the class scope itself while processing the members. */
23361 if (pushed_scope)
23362 pop_scope (pushed_scope);
23364 if (invalid_explicit_specialization_p)
23366 end_specialization ();
23367 --parser->num_template_parameter_lists;
23370 if (type)
23371 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23372 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23373 CLASSTYPE_FINAL (type) = 1;
23374 out:
23375 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23376 return type;
23379 /* Parse a class-key.
23381 class-key:
23382 class
23383 struct
23384 union
23386 Returns the kind of class-key specified, or none_type to indicate
23387 error. */
23389 static enum tag_types
23390 cp_parser_class_key (cp_parser* parser)
23392 cp_token *token;
23393 enum tag_types tag_type;
23395 /* Look for the class-key. */
23396 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23397 if (!token)
23398 return none_type;
23400 /* Check to see if the TOKEN is a class-key. */
23401 tag_type = cp_parser_token_is_class_key (token);
23402 if (!tag_type)
23403 cp_parser_error (parser, "expected class-key");
23404 return tag_type;
23407 /* Parse a type-parameter-key.
23409 type-parameter-key:
23410 class
23411 typename
23414 static void
23415 cp_parser_type_parameter_key (cp_parser* parser)
23417 /* Look for the type-parameter-key. */
23418 enum tag_types tag_type = none_type;
23419 cp_token *token = cp_lexer_peek_token (parser->lexer);
23420 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23422 cp_lexer_consume_token (parser->lexer);
23423 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23424 /* typename is not allowed in a template template parameter
23425 by the standard until C++17. */
23426 pedwarn (token->location, OPT_Wpedantic,
23427 "ISO C++ forbids typename key in template template parameter;"
23428 " use -std=c++17 or -std=gnu++17");
23430 else
23431 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23433 return;
23436 /* Parse an (optional) member-specification.
23438 member-specification:
23439 member-declaration member-specification [opt]
23440 access-specifier : member-specification [opt] */
23442 static void
23443 cp_parser_member_specification_opt (cp_parser* parser)
23445 while (true)
23447 cp_token *token;
23448 enum rid keyword;
23450 /* Peek at the next token. */
23451 token = cp_lexer_peek_token (parser->lexer);
23452 /* If it's a `}', or EOF then we've seen all the members. */
23453 if (token->type == CPP_CLOSE_BRACE
23454 || token->type == CPP_EOF
23455 || token->type == CPP_PRAGMA_EOL)
23456 break;
23458 /* See if this token is a keyword. */
23459 keyword = token->keyword;
23460 switch (keyword)
23462 case RID_PUBLIC:
23463 case RID_PROTECTED:
23464 case RID_PRIVATE:
23465 /* Consume the access-specifier. */
23466 cp_lexer_consume_token (parser->lexer);
23467 /* Remember which access-specifier is active. */
23468 current_access_specifier = token->u.value;
23469 /* Look for the `:'. */
23470 cp_parser_require (parser, CPP_COLON, RT_COLON);
23471 break;
23473 default:
23474 /* Accept #pragmas at class scope. */
23475 if (token->type == CPP_PRAGMA)
23477 cp_parser_pragma (parser, pragma_member, NULL);
23478 break;
23481 /* Otherwise, the next construction must be a
23482 member-declaration. */
23483 cp_parser_member_declaration (parser);
23488 /* Parse a member-declaration.
23490 member-declaration:
23491 decl-specifier-seq [opt] member-declarator-list [opt] ;
23492 function-definition ; [opt]
23493 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23494 using-declaration
23495 template-declaration
23496 alias-declaration
23498 member-declarator-list:
23499 member-declarator
23500 member-declarator-list , member-declarator
23502 member-declarator:
23503 declarator pure-specifier [opt]
23504 declarator constant-initializer [opt]
23505 identifier [opt] : constant-expression
23507 GNU Extensions:
23509 member-declaration:
23510 __extension__ member-declaration
23512 member-declarator:
23513 declarator attributes [opt] pure-specifier [opt]
23514 declarator attributes [opt] constant-initializer [opt]
23515 identifier [opt] attributes [opt] : constant-expression
23517 C++0x Extensions:
23519 member-declaration:
23520 static_assert-declaration */
23522 static void
23523 cp_parser_member_declaration (cp_parser* parser)
23525 cp_decl_specifier_seq decl_specifiers;
23526 tree prefix_attributes;
23527 tree decl;
23528 int declares_class_or_enum;
23529 bool friend_p;
23530 cp_token *token = NULL;
23531 cp_token *decl_spec_token_start = NULL;
23532 cp_token *initializer_token_start = NULL;
23533 int saved_pedantic;
23534 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23536 /* Check for the `__extension__' keyword. */
23537 if (cp_parser_extension_opt (parser, &saved_pedantic))
23539 /* Recurse. */
23540 cp_parser_member_declaration (parser);
23541 /* Restore the old value of the PEDANTIC flag. */
23542 pedantic = saved_pedantic;
23544 return;
23547 /* Check for a template-declaration. */
23548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23550 /* An explicit specialization here is an error condition, and we
23551 expect the specialization handler to detect and report this. */
23552 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23553 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23554 cp_parser_explicit_specialization (parser);
23555 else
23556 cp_parser_template_declaration (parser, /*member_p=*/true);
23558 return;
23560 /* Check for a template introduction. */
23561 else if (cp_parser_template_declaration_after_export (parser, true))
23562 return;
23564 /* Check for a using-declaration. */
23565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23567 if (cxx_dialect < cxx11)
23569 /* Parse the using-declaration. */
23570 cp_parser_using_declaration (parser,
23571 /*access_declaration_p=*/false);
23572 return;
23574 else
23576 tree decl;
23577 bool alias_decl_expected;
23578 cp_parser_parse_tentatively (parser);
23579 decl = cp_parser_alias_declaration (parser);
23580 /* Note that if we actually see the '=' token after the
23581 identifier, cp_parser_alias_declaration commits the
23582 tentative parse. In that case, we really expect an
23583 alias-declaration. Otherwise, we expect a using
23584 declaration. */
23585 alias_decl_expected =
23586 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23587 cp_parser_parse_definitely (parser);
23589 if (alias_decl_expected)
23590 finish_member_declaration (decl);
23591 else
23592 cp_parser_using_declaration (parser,
23593 /*access_declaration_p=*/false);
23594 return;
23598 /* Check for @defs. */
23599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23601 tree ivar, member;
23602 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23603 ivar = ivar_chains;
23604 while (ivar)
23606 member = ivar;
23607 ivar = TREE_CHAIN (member);
23608 TREE_CHAIN (member) = NULL_TREE;
23609 finish_member_declaration (member);
23611 return;
23614 /* If the next token is `static_assert' we have a static assertion. */
23615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23617 cp_parser_static_assert (parser, /*member_p=*/true);
23618 return;
23621 parser->colon_corrects_to_scope_p = false;
23623 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23624 goto out;
23626 /* Parse the decl-specifier-seq. */
23627 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23628 cp_parser_decl_specifier_seq (parser,
23629 CP_PARSER_FLAGS_OPTIONAL,
23630 &decl_specifiers,
23631 &declares_class_or_enum);
23632 /* Check for an invalid type-name. */
23633 if (!decl_specifiers.any_type_specifiers_p
23634 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23635 goto out;
23636 /* If there is no declarator, then the decl-specifier-seq should
23637 specify a type. */
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23640 /* If there was no decl-specifier-seq, and the next token is a
23641 `;', then we have something like:
23643 struct S { ; };
23645 [class.mem]
23647 Each member-declaration shall declare at least one member
23648 name of the class. */
23649 if (!decl_specifiers.any_specifiers_p)
23651 cp_token *token = cp_lexer_peek_token (parser->lexer);
23652 if (!in_system_header_at (token->location))
23654 gcc_rich_location richloc (token->location);
23655 richloc.add_fixit_remove ();
23656 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23659 else
23661 tree type;
23663 /* See if this declaration is a friend. */
23664 friend_p = cp_parser_friend_p (&decl_specifiers);
23665 /* If there were decl-specifiers, check to see if there was
23666 a class-declaration. */
23667 type = check_tag_decl (&decl_specifiers,
23668 /*explicit_type_instantiation_p=*/false);
23669 /* Nested classes have already been added to the class, but
23670 a `friend' needs to be explicitly registered. */
23671 if (friend_p)
23673 /* If the `friend' keyword was present, the friend must
23674 be introduced with a class-key. */
23675 if (!declares_class_or_enum && cxx_dialect < cxx11)
23676 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23677 "in C++03 a class-key must be used "
23678 "when declaring a friend");
23679 /* In this case:
23681 template <typename T> struct A {
23682 friend struct A<T>::B;
23685 A<T>::B will be represented by a TYPENAME_TYPE, and
23686 therefore not recognized by check_tag_decl. */
23687 if (!type)
23689 type = decl_specifiers.type;
23690 if (type && TREE_CODE (type) == TYPE_DECL)
23691 type = TREE_TYPE (type);
23693 if (!type || !TYPE_P (type))
23694 error_at (decl_spec_token_start->location,
23695 "friend declaration does not name a class or "
23696 "function");
23697 else
23698 make_friend_class (current_class_type, type,
23699 /*complain=*/true);
23701 /* If there is no TYPE, an error message will already have
23702 been issued. */
23703 else if (!type || type == error_mark_node)
23705 /* An anonymous aggregate has to be handled specially; such
23706 a declaration really declares a data member (with a
23707 particular type), as opposed to a nested class. */
23708 else if (ANON_AGGR_TYPE_P (type))
23710 /* C++11 9.5/6. */
23711 if (decl_specifiers.storage_class != sc_none)
23712 error_at (decl_spec_token_start->location,
23713 "a storage class on an anonymous aggregate "
23714 "in class scope is not allowed");
23716 /* Remove constructors and such from TYPE, now that we
23717 know it is an anonymous aggregate. */
23718 fixup_anonymous_aggr (type);
23719 /* And make the corresponding data member. */
23720 decl = build_decl (decl_spec_token_start->location,
23721 FIELD_DECL, NULL_TREE, type);
23722 /* Add it to the class. */
23723 finish_member_declaration (decl);
23725 else
23726 cp_parser_check_access_in_redeclaration
23727 (TYPE_NAME (type),
23728 decl_spec_token_start->location);
23731 else
23733 bool assume_semicolon = false;
23735 /* Clear attributes from the decl_specifiers but keep them
23736 around as prefix attributes that apply them to the entity
23737 being declared. */
23738 prefix_attributes = decl_specifiers.attributes;
23739 decl_specifiers.attributes = NULL_TREE;
23741 /* See if these declarations will be friends. */
23742 friend_p = cp_parser_friend_p (&decl_specifiers);
23744 /* Keep going until we hit the `;' at the end of the
23745 declaration. */
23746 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23748 tree attributes = NULL_TREE;
23749 tree first_attribute;
23750 tree initializer;
23751 bool named_bitfld = false;
23753 /* Peek at the next token. */
23754 token = cp_lexer_peek_token (parser->lexer);
23756 /* The following code wants to know early if it is a bit-field
23757 or some other declaration. Attributes can appear before
23758 the `:' token. Skip over them without consuming any tokens
23759 to peek if they are followed by `:'. */
23760 if (cp_next_tokens_can_be_attribute_p (parser)
23761 || (token->type == CPP_NAME
23762 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23763 && (named_bitfld = true)))
23765 size_t n
23766 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23767 token = cp_lexer_peek_nth_token (parser->lexer, n);
23770 /* Check for a bitfield declaration. */
23771 if (token->type == CPP_COLON
23772 || (token->type == CPP_NAME
23773 && token == cp_lexer_peek_token (parser->lexer)
23774 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23775 && (named_bitfld = true)))
23777 tree identifier;
23778 tree width;
23779 tree late_attributes = NULL_TREE;
23781 if (named_bitfld)
23782 identifier = cp_parser_identifier (parser);
23783 else
23784 identifier = NULL_TREE;
23786 /* Look for attributes that apply to the bitfield. */
23787 attributes = cp_parser_attributes_opt (parser);
23789 /* Consume the `:' token. */
23790 cp_lexer_consume_token (parser->lexer);
23792 /* Get the width of the bitfield. */
23793 width = cp_parser_constant_expression (parser, false, NULL,
23794 cxx_dialect >= cxx11);
23796 /* In C++2A and as extension for C++11 and above we allow
23797 default member initializers for bit-fields. */
23798 initializer = NULL_TREE;
23799 if (cxx_dialect >= cxx11
23800 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23801 || cp_lexer_next_token_is (parser->lexer,
23802 CPP_OPEN_BRACE)))
23804 location_t loc
23805 = cp_lexer_peek_token (parser->lexer)->location;
23806 if (cxx_dialect < cxx2a
23807 && !in_system_header_at (loc)
23808 && identifier != NULL_TREE)
23809 pedwarn (loc, 0,
23810 "default member initializers for bit-fields "
23811 "only available with -std=c++2a or "
23812 "-std=gnu++2a");
23814 initializer = cp_parser_save_nsdmi (parser);
23815 if (identifier == NULL_TREE)
23817 error_at (loc, "default member initializer for "
23818 "unnamed bit-field");
23819 initializer = NULL_TREE;
23822 else
23824 /* Look for attributes that apply to the bitfield after
23825 the `:' token and width. This is where GCC used to
23826 parse attributes in the past, pedwarn if there is
23827 a std attribute. */
23828 if (cp_next_tokens_can_be_std_attribute_p (parser))
23829 pedwarn (input_location, OPT_Wpedantic,
23830 "ISO C++ allows bit-field attributes only "
23831 "before the %<:%> token");
23833 late_attributes = cp_parser_attributes_opt (parser);
23836 attributes = attr_chainon (attributes, late_attributes);
23838 /* Remember which attributes are prefix attributes and
23839 which are not. */
23840 first_attribute = attributes;
23841 /* Combine the attributes. */
23842 attributes = attr_chainon (prefix_attributes, attributes);
23844 /* Create the bitfield declaration. */
23845 decl = grokbitfield (identifier
23846 ? make_id_declarator (NULL_TREE,
23847 identifier,
23848 sfk_none)
23849 : NULL,
23850 &decl_specifiers,
23851 width, initializer,
23852 attributes);
23854 else
23856 cp_declarator *declarator;
23857 tree asm_specification;
23858 int ctor_dtor_or_conv_p;
23860 /* Parse the declarator. */
23861 declarator
23862 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23863 &ctor_dtor_or_conv_p,
23864 /*parenthesized_p=*/NULL,
23865 /*member_p=*/true,
23866 friend_p);
23868 /* If something went wrong parsing the declarator, make sure
23869 that we at least consume some tokens. */
23870 if (declarator == cp_error_declarator)
23872 /* Skip to the end of the statement. */
23873 cp_parser_skip_to_end_of_statement (parser);
23874 /* If the next token is not a semicolon, that is
23875 probably because we just skipped over the body of
23876 a function. So, we consume a semicolon if
23877 present, but do not issue an error message if it
23878 is not present. */
23879 if (cp_lexer_next_token_is (parser->lexer,
23880 CPP_SEMICOLON))
23881 cp_lexer_consume_token (parser->lexer);
23882 goto out;
23885 if (declares_class_or_enum & 2)
23886 cp_parser_check_for_definition_in_return_type
23887 (declarator, decl_specifiers.type,
23888 decl_specifiers.locations[ds_type_spec]);
23890 /* Look for an asm-specification. */
23891 asm_specification = cp_parser_asm_specification_opt (parser);
23892 /* Look for attributes that apply to the declaration. */
23893 attributes = cp_parser_attributes_opt (parser);
23894 /* Remember which attributes are prefix attributes and
23895 which are not. */
23896 first_attribute = attributes;
23897 /* Combine the attributes. */
23898 attributes = attr_chainon (prefix_attributes, attributes);
23900 /* If it's an `=', then we have a constant-initializer or a
23901 pure-specifier. It is not correct to parse the
23902 initializer before registering the member declaration
23903 since the member declaration should be in scope while
23904 its initializer is processed. However, the rest of the
23905 front end does not yet provide an interface that allows
23906 us to handle this correctly. */
23907 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23909 /* In [class.mem]:
23911 A pure-specifier shall be used only in the declaration of
23912 a virtual function.
23914 A member-declarator can contain a constant-initializer
23915 only if it declares a static member of integral or
23916 enumeration type.
23918 Therefore, if the DECLARATOR is for a function, we look
23919 for a pure-specifier; otherwise, we look for a
23920 constant-initializer. When we call `grokfield', it will
23921 perform more stringent semantics checks. */
23922 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23923 if (function_declarator_p (declarator)
23924 || (decl_specifiers.type
23925 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23926 && declarator->kind == cdk_id
23927 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23928 == FUNCTION_TYPE)))
23929 initializer = cp_parser_pure_specifier (parser);
23930 else if (decl_specifiers.storage_class != sc_static)
23931 initializer = cp_parser_save_nsdmi (parser);
23932 else if (cxx_dialect >= cxx11)
23934 bool nonconst;
23935 /* Don't require a constant rvalue in C++11, since we
23936 might want a reference constant. We'll enforce
23937 constancy later. */
23938 cp_lexer_consume_token (parser->lexer);
23939 /* Parse the initializer. */
23940 initializer = cp_parser_initializer_clause (parser,
23941 &nonconst);
23943 else
23944 /* Parse the initializer. */
23945 initializer = cp_parser_constant_initializer (parser);
23947 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23948 && !function_declarator_p (declarator))
23950 bool x;
23951 if (decl_specifiers.storage_class != sc_static)
23952 initializer = cp_parser_save_nsdmi (parser);
23953 else
23954 initializer = cp_parser_initializer (parser, &x, &x);
23956 /* Otherwise, there is no initializer. */
23957 else
23958 initializer = NULL_TREE;
23960 /* See if we are probably looking at a function
23961 definition. We are certainly not looking at a
23962 member-declarator. Calling `grokfield' has
23963 side-effects, so we must not do it unless we are sure
23964 that we are looking at a member-declarator. */
23965 if (cp_parser_token_starts_function_definition_p
23966 (cp_lexer_peek_token (parser->lexer)))
23968 /* The grammar does not allow a pure-specifier to be
23969 used when a member function is defined. (It is
23970 possible that this fact is an oversight in the
23971 standard, since a pure function may be defined
23972 outside of the class-specifier. */
23973 if (initializer && initializer_token_start)
23974 error_at (initializer_token_start->location,
23975 "pure-specifier on function-definition");
23976 decl = cp_parser_save_member_function_body (parser,
23977 &decl_specifiers,
23978 declarator,
23979 attributes);
23980 if (parser->fully_implicit_function_template_p)
23981 decl = finish_fully_implicit_template (parser, decl);
23982 /* If the member was not a friend, declare it here. */
23983 if (!friend_p)
23984 finish_member_declaration (decl);
23985 /* Peek at the next token. */
23986 token = cp_lexer_peek_token (parser->lexer);
23987 /* If the next token is a semicolon, consume it. */
23988 if (token->type == CPP_SEMICOLON)
23990 location_t semicolon_loc
23991 = cp_lexer_consume_token (parser->lexer)->location;
23992 gcc_rich_location richloc (semicolon_loc);
23993 richloc.add_fixit_remove ();
23994 warning_at (&richloc, OPT_Wextra_semi,
23995 "extra %<;%> after in-class "
23996 "function definition");
23998 goto out;
24000 else
24001 if (declarator->kind == cdk_function)
24002 declarator->id_loc = token->location;
24003 /* Create the declaration. */
24004 decl = grokfield (declarator, &decl_specifiers,
24005 initializer, /*init_const_expr_p=*/true,
24006 asm_specification, attributes);
24007 if (parser->fully_implicit_function_template_p)
24009 if (friend_p)
24010 finish_fully_implicit_template (parser, 0);
24011 else
24012 decl = finish_fully_implicit_template (parser, decl);
24016 cp_finalize_omp_declare_simd (parser, decl);
24017 cp_finalize_oacc_routine (parser, decl, false);
24019 /* Reset PREFIX_ATTRIBUTES. */
24020 if (attributes != error_mark_node)
24022 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24023 attributes = TREE_CHAIN (attributes);
24024 if (attributes)
24025 TREE_CHAIN (attributes) = NULL_TREE;
24028 /* If there is any qualification still in effect, clear it
24029 now; we will be starting fresh with the next declarator. */
24030 parser->scope = NULL_TREE;
24031 parser->qualifying_scope = NULL_TREE;
24032 parser->object_scope = NULL_TREE;
24033 /* If it's a `,', then there are more declarators. */
24034 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24036 cp_lexer_consume_token (parser->lexer);
24037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24039 cp_token *token = cp_lexer_previous_token (parser->lexer);
24040 gcc_rich_location richloc (token->location);
24041 richloc.add_fixit_remove ();
24042 error_at (&richloc, "stray %<,%> at end of "
24043 "member declaration");
24046 /* If the next token isn't a `;', then we have a parse error. */
24047 else if (cp_lexer_next_token_is_not (parser->lexer,
24048 CPP_SEMICOLON))
24050 /* The next token might be a ways away from where the
24051 actual semicolon is missing. Find the previous token
24052 and use that for our error position. */
24053 cp_token *token = cp_lexer_previous_token (parser->lexer);
24054 gcc_rich_location richloc (token->location);
24055 richloc.add_fixit_insert_after (";");
24056 error_at (&richloc, "expected %<;%> at end of "
24057 "member declaration");
24059 /* Assume that the user meant to provide a semicolon. If
24060 we were to cp_parser_skip_to_end_of_statement, we might
24061 skip to a semicolon inside a member function definition
24062 and issue nonsensical error messages. */
24063 assume_semicolon = true;
24066 if (decl)
24068 /* Add DECL to the list of members. */
24069 if (!friend_p
24070 /* Explicitly include, eg, NSDMIs, for better error
24071 recovery (c++/58650). */
24072 || !DECL_DECLARES_FUNCTION_P (decl))
24073 finish_member_declaration (decl);
24075 if (TREE_CODE (decl) == FUNCTION_DECL)
24076 cp_parser_save_default_args (parser, decl);
24077 else if (TREE_CODE (decl) == FIELD_DECL
24078 && DECL_INITIAL (decl))
24079 /* Add DECL to the queue of NSDMI to be parsed later. */
24080 vec_safe_push (unparsed_nsdmis, decl);
24083 if (assume_semicolon)
24084 goto out;
24088 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24089 out:
24090 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24093 /* Parse a pure-specifier.
24095 pure-specifier:
24098 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24099 Otherwise, ERROR_MARK_NODE is returned. */
24101 static tree
24102 cp_parser_pure_specifier (cp_parser* parser)
24104 cp_token *token;
24106 /* Look for the `=' token. */
24107 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24108 return error_mark_node;
24109 /* Look for the `0' token. */
24110 token = cp_lexer_peek_token (parser->lexer);
24112 if (token->type == CPP_EOF
24113 || token->type == CPP_PRAGMA_EOL)
24114 return error_mark_node;
24116 cp_lexer_consume_token (parser->lexer);
24118 /* Accept = default or = delete in c++0x mode. */
24119 if (token->keyword == RID_DEFAULT
24120 || token->keyword == RID_DELETE)
24122 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24123 return token->u.value;
24126 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24127 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24129 cp_parser_error (parser,
24130 "invalid pure specifier (only %<= 0%> is allowed)");
24131 cp_parser_skip_to_end_of_statement (parser);
24132 return error_mark_node;
24134 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24136 error_at (token->location, "templates may not be %<virtual%>");
24137 return error_mark_node;
24140 return integer_zero_node;
24143 /* Parse a constant-initializer.
24145 constant-initializer:
24146 = constant-expression
24148 Returns a representation of the constant-expression. */
24150 static tree
24151 cp_parser_constant_initializer (cp_parser* parser)
24153 /* Look for the `=' token. */
24154 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24155 return error_mark_node;
24157 /* It is invalid to write:
24159 struct S { static const int i = { 7 }; };
24162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24164 cp_parser_error (parser,
24165 "a brace-enclosed initializer is not allowed here");
24166 /* Consume the opening brace. */
24167 matching_braces braces;
24168 braces.consume_open (parser);
24169 /* Skip the initializer. */
24170 cp_parser_skip_to_closing_brace (parser);
24171 /* Look for the trailing `}'. */
24172 braces.require_close (parser);
24174 return error_mark_node;
24177 return cp_parser_constant_expression (parser);
24180 /* Derived classes [gram.class.derived] */
24182 /* Parse a base-clause.
24184 base-clause:
24185 : base-specifier-list
24187 base-specifier-list:
24188 base-specifier ... [opt]
24189 base-specifier-list , base-specifier ... [opt]
24191 Returns a TREE_LIST representing the base-classes, in the order in
24192 which they were declared. The representation of each node is as
24193 described by cp_parser_base_specifier.
24195 In the case that no bases are specified, this function will return
24196 NULL_TREE, not ERROR_MARK_NODE. */
24198 static tree
24199 cp_parser_base_clause (cp_parser* parser)
24201 tree bases = NULL_TREE;
24203 /* Look for the `:' that begins the list. */
24204 cp_parser_require (parser, CPP_COLON, RT_COLON);
24206 /* Scan the base-specifier-list. */
24207 while (true)
24209 cp_token *token;
24210 tree base;
24211 bool pack_expansion_p = false;
24213 /* Look for the base-specifier. */
24214 base = cp_parser_base_specifier (parser);
24215 /* Look for the (optional) ellipsis. */
24216 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24218 /* Consume the `...'. */
24219 cp_lexer_consume_token (parser->lexer);
24221 pack_expansion_p = true;
24224 /* Add BASE to the front of the list. */
24225 if (base && base != error_mark_node)
24227 if (pack_expansion_p)
24228 /* Make this a pack expansion type. */
24229 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24231 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24233 TREE_CHAIN (base) = bases;
24234 bases = base;
24237 /* Peek at the next token. */
24238 token = cp_lexer_peek_token (parser->lexer);
24239 /* If it's not a comma, then the list is complete. */
24240 if (token->type != CPP_COMMA)
24241 break;
24242 /* Consume the `,'. */
24243 cp_lexer_consume_token (parser->lexer);
24246 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24247 base class had a qualified name. However, the next name that
24248 appears is certainly not qualified. */
24249 parser->scope = NULL_TREE;
24250 parser->qualifying_scope = NULL_TREE;
24251 parser->object_scope = NULL_TREE;
24253 return nreverse (bases);
24256 /* Parse a base-specifier.
24258 base-specifier:
24259 :: [opt] nested-name-specifier [opt] class-name
24260 virtual access-specifier [opt] :: [opt] nested-name-specifier
24261 [opt] class-name
24262 access-specifier virtual [opt] :: [opt] nested-name-specifier
24263 [opt] class-name
24265 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24266 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24267 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24268 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24270 static tree
24271 cp_parser_base_specifier (cp_parser* parser)
24273 cp_token *token;
24274 bool done = false;
24275 bool virtual_p = false;
24276 bool duplicate_virtual_error_issued_p = false;
24277 bool duplicate_access_error_issued_p = false;
24278 bool class_scope_p, template_p;
24279 tree access = access_default_node;
24280 tree type;
24282 /* Process the optional `virtual' and `access-specifier'. */
24283 while (!done)
24285 /* Peek at the next token. */
24286 token = cp_lexer_peek_token (parser->lexer);
24287 /* Process `virtual'. */
24288 switch (token->keyword)
24290 case RID_VIRTUAL:
24291 /* If `virtual' appears more than once, issue an error. */
24292 if (virtual_p && !duplicate_virtual_error_issued_p)
24294 cp_parser_error (parser,
24295 "%<virtual%> specified more than once in base-specifier");
24296 duplicate_virtual_error_issued_p = true;
24299 virtual_p = true;
24301 /* Consume the `virtual' token. */
24302 cp_lexer_consume_token (parser->lexer);
24304 break;
24306 case RID_PUBLIC:
24307 case RID_PROTECTED:
24308 case RID_PRIVATE:
24309 /* If more than one access specifier appears, issue an
24310 error. */
24311 if (access != access_default_node
24312 && !duplicate_access_error_issued_p)
24314 cp_parser_error (parser,
24315 "more than one access specifier in base-specifier");
24316 duplicate_access_error_issued_p = true;
24319 access = ridpointers[(int) token->keyword];
24321 /* Consume the access-specifier. */
24322 cp_lexer_consume_token (parser->lexer);
24324 break;
24326 default:
24327 done = true;
24328 break;
24331 /* It is not uncommon to see programs mechanically, erroneously, use
24332 the 'typename' keyword to denote (dependent) qualified types
24333 as base classes. */
24334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24336 token = cp_lexer_peek_token (parser->lexer);
24337 if (!processing_template_decl)
24338 error_at (token->location,
24339 "keyword %<typename%> not allowed outside of templates");
24340 else
24341 error_at (token->location,
24342 "keyword %<typename%> not allowed in this context "
24343 "(the base class is implicitly a type)");
24344 cp_lexer_consume_token (parser->lexer);
24347 /* Look for the optional `::' operator. */
24348 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24349 /* Look for the nested-name-specifier. The simplest way to
24350 implement:
24352 [temp.res]
24354 The keyword `typename' is not permitted in a base-specifier or
24355 mem-initializer; in these contexts a qualified name that
24356 depends on a template-parameter is implicitly assumed to be a
24357 type name.
24359 is to pretend that we have seen the `typename' keyword at this
24360 point. */
24361 cp_parser_nested_name_specifier_opt (parser,
24362 /*typename_keyword_p=*/true,
24363 /*check_dependency_p=*/true,
24364 /*type_p=*/true,
24365 /*is_declaration=*/true);
24366 /* If the base class is given by a qualified name, assume that names
24367 we see are type names or templates, as appropriate. */
24368 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24369 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24371 if (!parser->scope
24372 && cp_lexer_next_token_is_decltype (parser->lexer))
24373 /* DR 950 allows decltype as a base-specifier. */
24374 type = cp_parser_decltype (parser);
24375 else
24377 /* Otherwise, look for the class-name. */
24378 type = cp_parser_class_name (parser,
24379 class_scope_p,
24380 template_p,
24381 typename_type,
24382 /*check_dependency_p=*/true,
24383 /*class_head_p=*/false,
24384 /*is_declaration=*/true);
24385 type = TREE_TYPE (type);
24388 if (type == error_mark_node)
24389 return error_mark_node;
24391 return finish_base_specifier (type, access, virtual_p);
24394 /* Exception handling [gram.exception] */
24396 /* Parse an (optional) noexcept-specification.
24398 noexcept-specification:
24399 noexcept ( constant-expression ) [opt]
24401 If no noexcept-specification is present, returns NULL_TREE.
24402 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24403 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24404 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24405 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24406 in which case a boolean condition is returned instead. */
24408 static tree
24409 cp_parser_noexcept_specification_opt (cp_parser* parser,
24410 bool require_constexpr,
24411 bool* consumed_expr,
24412 bool return_cond)
24414 cp_token *token;
24415 const char *saved_message;
24417 /* Peek at the next token. */
24418 token = cp_lexer_peek_token (parser->lexer);
24420 /* Is it a noexcept-specification? */
24421 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24423 tree expr;
24424 cp_lexer_consume_token (parser->lexer);
24426 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24428 matching_parens parens;
24429 parens.consume_open (parser);
24431 if (require_constexpr)
24433 /* Types may not be defined in an exception-specification. */
24434 saved_message = parser->type_definition_forbidden_message;
24435 parser->type_definition_forbidden_message
24436 = G_("types may not be defined in an exception-specification");
24438 expr = cp_parser_constant_expression (parser);
24440 /* Restore the saved message. */
24441 parser->type_definition_forbidden_message = saved_message;
24443 else
24445 expr = cp_parser_expression (parser);
24446 *consumed_expr = true;
24449 parens.require_close (parser);
24451 else
24453 expr = boolean_true_node;
24454 if (!require_constexpr)
24455 *consumed_expr = false;
24458 /* We cannot build a noexcept-spec right away because this will check
24459 that expr is a constexpr. */
24460 if (!return_cond)
24461 return build_noexcept_spec (expr, tf_warning_or_error);
24462 else
24463 return expr;
24465 else
24466 return NULL_TREE;
24469 /* Parse an (optional) exception-specification.
24471 exception-specification:
24472 throw ( type-id-list [opt] )
24474 Returns a TREE_LIST representing the exception-specification. The
24475 TREE_VALUE of each node is a type. */
24477 static tree
24478 cp_parser_exception_specification_opt (cp_parser* parser)
24480 cp_token *token;
24481 tree type_id_list;
24482 const char *saved_message;
24484 /* Peek at the next token. */
24485 token = cp_lexer_peek_token (parser->lexer);
24487 /* Is it a noexcept-specification? */
24488 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24489 false);
24490 if (type_id_list != NULL_TREE)
24491 return type_id_list;
24493 /* If it's not `throw', then there's no exception-specification. */
24494 if (!cp_parser_is_keyword (token, RID_THROW))
24495 return NULL_TREE;
24497 location_t loc = token->location;
24499 /* Consume the `throw'. */
24500 cp_lexer_consume_token (parser->lexer);
24502 /* Look for the `('. */
24503 matching_parens parens;
24504 parens.require_open (parser);
24506 /* Peek at the next token. */
24507 token = cp_lexer_peek_token (parser->lexer);
24508 /* If it's not a `)', then there is a type-id-list. */
24509 if (token->type != CPP_CLOSE_PAREN)
24511 /* Types may not be defined in an exception-specification. */
24512 saved_message = parser->type_definition_forbidden_message;
24513 parser->type_definition_forbidden_message
24514 = G_("types may not be defined in an exception-specification");
24515 /* Parse the type-id-list. */
24516 type_id_list = cp_parser_type_id_list (parser);
24517 /* Restore the saved message. */
24518 parser->type_definition_forbidden_message = saved_message;
24520 if (cxx_dialect >= cxx17)
24522 error_at (loc, "ISO C++17 does not allow dynamic exception "
24523 "specifications");
24524 type_id_list = NULL_TREE;
24526 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24527 warning_at (loc, OPT_Wdeprecated,
24528 "dynamic exception specifications are deprecated in "
24529 "C++11");
24531 /* In C++17, throw() is equivalent to noexcept (true). throw()
24532 is deprecated in C++11 and above as well, but is still widely used,
24533 so don't warn about it yet. */
24534 else if (cxx_dialect >= cxx17)
24535 type_id_list = noexcept_true_spec;
24536 else
24537 type_id_list = empty_except_spec;
24539 /* Look for the `)'. */
24540 parens.require_close (parser);
24542 return type_id_list;
24545 /* Parse an (optional) type-id-list.
24547 type-id-list:
24548 type-id ... [opt]
24549 type-id-list , type-id ... [opt]
24551 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24552 in the order that the types were presented. */
24554 static tree
24555 cp_parser_type_id_list (cp_parser* parser)
24557 tree types = NULL_TREE;
24559 while (true)
24561 cp_token *token;
24562 tree type;
24564 token = cp_lexer_peek_token (parser->lexer);
24566 /* Get the next type-id. */
24567 type = cp_parser_type_id (parser);
24568 /* Check for invalid 'auto'. */
24569 if (flag_concepts && type_uses_auto (type))
24571 error_at (token->location,
24572 "invalid use of %<auto%> in exception-specification");
24573 type = error_mark_node;
24575 /* Parse the optional ellipsis. */
24576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24578 /* Consume the `...'. */
24579 cp_lexer_consume_token (parser->lexer);
24581 /* Turn the type into a pack expansion expression. */
24582 type = make_pack_expansion (type);
24584 /* Add it to the list. */
24585 types = add_exception_specifier (types, type, /*complain=*/1);
24586 /* Peek at the next token. */
24587 token = cp_lexer_peek_token (parser->lexer);
24588 /* If it is not a `,', we are done. */
24589 if (token->type != CPP_COMMA)
24590 break;
24591 /* Consume the `,'. */
24592 cp_lexer_consume_token (parser->lexer);
24595 return nreverse (types);
24598 /* Parse a try-block.
24600 try-block:
24601 try compound-statement handler-seq */
24603 static tree
24604 cp_parser_try_block (cp_parser* parser)
24606 tree try_block;
24608 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24609 if (parser->in_function_body
24610 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24611 error ("%<try%> in %<constexpr%> function");
24613 try_block = begin_try_block ();
24614 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24615 finish_try_block (try_block);
24616 cp_parser_handler_seq (parser);
24617 finish_handler_sequence (try_block);
24619 return try_block;
24622 /* Parse a function-try-block.
24624 function-try-block:
24625 try ctor-initializer [opt] function-body handler-seq */
24627 static void
24628 cp_parser_function_try_block (cp_parser* parser)
24630 tree compound_stmt;
24631 tree try_block;
24633 /* Look for the `try' keyword. */
24634 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24635 return;
24636 /* Let the rest of the front end know where we are. */
24637 try_block = begin_function_try_block (&compound_stmt);
24638 /* Parse the function-body. */
24639 cp_parser_ctor_initializer_opt_and_function_body
24640 (parser, /*in_function_try_block=*/true);
24641 /* We're done with the `try' part. */
24642 finish_function_try_block (try_block);
24643 /* Parse the handlers. */
24644 cp_parser_handler_seq (parser);
24645 /* We're done with the handlers. */
24646 finish_function_handler_sequence (try_block, compound_stmt);
24649 /* Parse a handler-seq.
24651 handler-seq:
24652 handler handler-seq [opt] */
24654 static void
24655 cp_parser_handler_seq (cp_parser* parser)
24657 while (true)
24659 cp_token *token;
24661 /* Parse the handler. */
24662 cp_parser_handler (parser);
24663 /* Peek at the next token. */
24664 token = cp_lexer_peek_token (parser->lexer);
24665 /* If it's not `catch' then there are no more handlers. */
24666 if (!cp_parser_is_keyword (token, RID_CATCH))
24667 break;
24671 /* Parse a handler.
24673 handler:
24674 catch ( exception-declaration ) compound-statement */
24676 static void
24677 cp_parser_handler (cp_parser* parser)
24679 tree handler;
24680 tree declaration;
24682 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24683 handler = begin_handler ();
24684 matching_parens parens;
24685 parens.require_open (parser);
24686 declaration = cp_parser_exception_declaration (parser);
24687 finish_handler_parms (declaration, handler);
24688 parens.require_close (parser);
24689 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24690 finish_handler (handler);
24693 /* Parse an exception-declaration.
24695 exception-declaration:
24696 type-specifier-seq declarator
24697 type-specifier-seq abstract-declarator
24698 type-specifier-seq
24701 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24702 ellipsis variant is used. */
24704 static tree
24705 cp_parser_exception_declaration (cp_parser* parser)
24707 cp_decl_specifier_seq type_specifiers;
24708 cp_declarator *declarator;
24709 const char *saved_message;
24711 /* If it's an ellipsis, it's easy to handle. */
24712 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24714 /* Consume the `...' token. */
24715 cp_lexer_consume_token (parser->lexer);
24716 return NULL_TREE;
24719 /* Types may not be defined in exception-declarations. */
24720 saved_message = parser->type_definition_forbidden_message;
24721 parser->type_definition_forbidden_message
24722 = G_("types may not be defined in exception-declarations");
24724 /* Parse the type-specifier-seq. */
24725 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24726 /*is_trailing_return=*/false,
24727 &type_specifiers);
24728 /* If it's a `)', then there is no declarator. */
24729 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24730 declarator = NULL;
24731 else
24732 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24733 /*ctor_dtor_or_conv_p=*/NULL,
24734 /*parenthesized_p=*/NULL,
24735 /*member_p=*/false,
24736 /*friend_p=*/false);
24738 /* Restore the saved message. */
24739 parser->type_definition_forbidden_message = saved_message;
24741 if (!type_specifiers.any_specifiers_p)
24742 return error_mark_node;
24744 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24747 /* Parse a throw-expression.
24749 throw-expression:
24750 throw assignment-expression [opt]
24752 Returns a THROW_EXPR representing the throw-expression. */
24754 static tree
24755 cp_parser_throw_expression (cp_parser* parser)
24757 tree expression;
24758 cp_token* token;
24760 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24761 token = cp_lexer_peek_token (parser->lexer);
24762 /* Figure out whether or not there is an assignment-expression
24763 following the "throw" keyword. */
24764 if (token->type == CPP_COMMA
24765 || token->type == CPP_SEMICOLON
24766 || token->type == CPP_CLOSE_PAREN
24767 || token->type == CPP_CLOSE_SQUARE
24768 || token->type == CPP_CLOSE_BRACE
24769 || token->type == CPP_COLON)
24770 expression = NULL_TREE;
24771 else
24772 expression = cp_parser_assignment_expression (parser);
24774 return build_throw (expression);
24777 /* GNU Extensions */
24779 /* Parse an (optional) asm-specification.
24781 asm-specification:
24782 asm ( string-literal )
24784 If the asm-specification is present, returns a STRING_CST
24785 corresponding to the string-literal. Otherwise, returns
24786 NULL_TREE. */
24788 static tree
24789 cp_parser_asm_specification_opt (cp_parser* parser)
24791 cp_token *token;
24792 tree asm_specification;
24794 /* Peek at the next token. */
24795 token = cp_lexer_peek_token (parser->lexer);
24796 /* If the next token isn't the `asm' keyword, then there's no
24797 asm-specification. */
24798 if (!cp_parser_is_keyword (token, RID_ASM))
24799 return NULL_TREE;
24801 /* Consume the `asm' token. */
24802 cp_lexer_consume_token (parser->lexer);
24803 /* Look for the `('. */
24804 matching_parens parens;
24805 parens.require_open (parser);
24807 /* Look for the string-literal. */
24808 asm_specification = cp_parser_string_literal (parser, false, false);
24810 /* Look for the `)'. */
24811 parens.require_close (parser);
24813 return asm_specification;
24816 /* Parse an asm-operand-list.
24818 asm-operand-list:
24819 asm-operand
24820 asm-operand-list , asm-operand
24822 asm-operand:
24823 string-literal ( expression )
24824 [ string-literal ] string-literal ( expression )
24826 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24827 each node is the expression. The TREE_PURPOSE is itself a
24828 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24829 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24830 is a STRING_CST for the string literal before the parenthesis. Returns
24831 ERROR_MARK_NODE if any of the operands are invalid. */
24833 static tree
24834 cp_parser_asm_operand_list (cp_parser* parser)
24836 tree asm_operands = NULL_TREE;
24837 bool invalid_operands = false;
24839 while (true)
24841 tree string_literal;
24842 tree expression;
24843 tree name;
24845 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24847 /* Consume the `[' token. */
24848 cp_lexer_consume_token (parser->lexer);
24849 /* Read the operand name. */
24850 name = cp_parser_identifier (parser);
24851 if (name != error_mark_node)
24852 name = build_string (IDENTIFIER_LENGTH (name),
24853 IDENTIFIER_POINTER (name));
24854 /* Look for the closing `]'. */
24855 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24857 else
24858 name = NULL_TREE;
24859 /* Look for the string-literal. */
24860 string_literal = cp_parser_string_literal (parser, false, false);
24862 /* Look for the `('. */
24863 matching_parens parens;
24864 parens.require_open (parser);
24865 /* Parse the expression. */
24866 expression = cp_parser_expression (parser);
24867 /* Look for the `)'. */
24868 parens.require_close (parser);
24870 if (name == error_mark_node
24871 || string_literal == error_mark_node
24872 || expression == error_mark_node)
24873 invalid_operands = true;
24875 /* Add this operand to the list. */
24876 asm_operands = tree_cons (build_tree_list (name, string_literal),
24877 expression,
24878 asm_operands);
24879 /* If the next token is not a `,', there are no more
24880 operands. */
24881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24882 break;
24883 /* Consume the `,'. */
24884 cp_lexer_consume_token (parser->lexer);
24887 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24890 /* Parse an asm-clobber-list.
24892 asm-clobber-list:
24893 string-literal
24894 asm-clobber-list , string-literal
24896 Returns a TREE_LIST, indicating the clobbers in the order that they
24897 appeared. The TREE_VALUE of each node is a STRING_CST. */
24899 static tree
24900 cp_parser_asm_clobber_list (cp_parser* parser)
24902 tree clobbers = NULL_TREE;
24904 while (true)
24906 tree string_literal;
24908 /* Look for the string literal. */
24909 string_literal = cp_parser_string_literal (parser, false, false);
24910 /* Add it to the list. */
24911 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24912 /* If the next token is not a `,', then the list is
24913 complete. */
24914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24915 break;
24916 /* Consume the `,' token. */
24917 cp_lexer_consume_token (parser->lexer);
24920 return clobbers;
24923 /* Parse an asm-label-list.
24925 asm-label-list:
24926 identifier
24927 asm-label-list , identifier
24929 Returns a TREE_LIST, indicating the labels in the order that they
24930 appeared. The TREE_VALUE of each node is a label. */
24932 static tree
24933 cp_parser_asm_label_list (cp_parser* parser)
24935 tree labels = NULL_TREE;
24937 while (true)
24939 tree identifier, label, name;
24941 /* Look for the identifier. */
24942 identifier = cp_parser_identifier (parser);
24943 if (!error_operand_p (identifier))
24945 label = lookup_label (identifier);
24946 if (TREE_CODE (label) == LABEL_DECL)
24948 TREE_USED (label) = 1;
24949 check_goto (label);
24950 name = build_string (IDENTIFIER_LENGTH (identifier),
24951 IDENTIFIER_POINTER (identifier));
24952 labels = tree_cons (name, label, labels);
24955 /* If the next token is not a `,', then the list is
24956 complete. */
24957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24958 break;
24959 /* Consume the `,' token. */
24960 cp_lexer_consume_token (parser->lexer);
24963 return nreverse (labels);
24966 /* Return TRUE iff the next tokens in the stream are possibly the
24967 beginning of a GNU extension attribute. */
24969 static bool
24970 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24972 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24975 /* Return TRUE iff the next tokens in the stream are possibly the
24976 beginning of a standard C++-11 attribute specifier. */
24978 static bool
24979 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24981 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24984 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24985 beginning of a standard C++-11 attribute specifier. */
24987 static bool
24988 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24990 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24992 return (cxx_dialect >= cxx11
24993 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24994 || (token->type == CPP_OPEN_SQUARE
24995 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24996 && token->type == CPP_OPEN_SQUARE)));
24999 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25000 beginning of a GNU extension attribute. */
25002 static bool
25003 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25005 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25007 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25010 /* Return true iff the next tokens can be the beginning of either a
25011 GNU attribute list, or a standard C++11 attribute sequence. */
25013 static bool
25014 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25016 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25017 || cp_next_tokens_can_be_std_attribute_p (parser));
25020 /* Return true iff the next Nth tokens can be the beginning of either
25021 a GNU attribute list, or a standard C++11 attribute sequence. */
25023 static bool
25024 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25026 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25027 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25030 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25031 of GNU attributes, or return NULL. */
25033 static tree
25034 cp_parser_attributes_opt (cp_parser *parser)
25036 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25037 return cp_parser_gnu_attributes_opt (parser);
25038 return cp_parser_std_attribute_spec_seq (parser);
25041 /* Parse an (optional) series of attributes.
25043 attributes:
25044 attributes attribute
25046 attribute:
25047 __attribute__ (( attribute-list [opt] ))
25049 The return value is as for cp_parser_gnu_attribute_list. */
25051 static tree
25052 cp_parser_gnu_attributes_opt (cp_parser* parser)
25054 tree attributes = NULL_TREE;
25056 temp_override<bool> cleanup
25057 (parser->auto_is_implicit_function_template_parm_p, false);
25059 while (true)
25061 cp_token *token;
25062 tree attribute_list;
25063 bool ok = true;
25065 /* Peek at the next token. */
25066 token = cp_lexer_peek_token (parser->lexer);
25067 /* If it's not `__attribute__', then we're done. */
25068 if (token->keyword != RID_ATTRIBUTE)
25069 break;
25071 /* Consume the `__attribute__' keyword. */
25072 cp_lexer_consume_token (parser->lexer);
25073 /* Look for the two `(' tokens. */
25074 matching_parens outer_parens;
25075 outer_parens.require_open (parser);
25076 matching_parens inner_parens;
25077 inner_parens.require_open (parser);
25079 /* Peek at the next token. */
25080 token = cp_lexer_peek_token (parser->lexer);
25081 if (token->type != CPP_CLOSE_PAREN)
25082 /* Parse the attribute-list. */
25083 attribute_list = cp_parser_gnu_attribute_list (parser);
25084 else
25085 /* If the next token is a `)', then there is no attribute
25086 list. */
25087 attribute_list = NULL;
25089 /* Look for the two `)' tokens. */
25090 if (!inner_parens.require_close (parser))
25091 ok = false;
25092 if (!outer_parens.require_close (parser))
25093 ok = false;
25094 if (!ok)
25095 cp_parser_skip_to_end_of_statement (parser);
25097 /* Add these new attributes to the list. */
25098 attributes = attr_chainon (attributes, attribute_list);
25101 return attributes;
25104 /* Parse a GNU attribute-list.
25106 attribute-list:
25107 attribute
25108 attribute-list , attribute
25110 attribute:
25111 identifier
25112 identifier ( identifier )
25113 identifier ( identifier , expression-list )
25114 identifier ( expression-list )
25116 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25117 to an attribute. The TREE_PURPOSE of each node is the identifier
25118 indicating which attribute is in use. The TREE_VALUE represents
25119 the arguments, if any. */
25121 static tree
25122 cp_parser_gnu_attribute_list (cp_parser* parser)
25124 tree attribute_list = NULL_TREE;
25125 bool save_translate_strings_p = parser->translate_strings_p;
25127 parser->translate_strings_p = false;
25128 while (true)
25130 cp_token *token;
25131 tree identifier;
25132 tree attribute;
25134 /* Look for the identifier. We also allow keywords here; for
25135 example `__attribute__ ((const))' is legal. */
25136 token = cp_lexer_peek_token (parser->lexer);
25137 if (token->type == CPP_NAME
25138 || token->type == CPP_KEYWORD)
25140 tree arguments = NULL_TREE;
25142 /* Consume the token, but save it since we need it for the
25143 SIMD enabled function parsing. */
25144 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25146 /* Save away the identifier that indicates which attribute
25147 this is. */
25148 identifier = (token->type == CPP_KEYWORD)
25149 /* For keywords, use the canonical spelling, not the
25150 parsed identifier. */
25151 ? ridpointers[(int) token->keyword]
25152 : id_token->u.value;
25154 identifier = canonicalize_attr_name (identifier);
25155 attribute = build_tree_list (identifier, NULL_TREE);
25157 /* Peek at the next token. */
25158 token = cp_lexer_peek_token (parser->lexer);
25159 /* If it's an `(', then parse the attribute arguments. */
25160 if (token->type == CPP_OPEN_PAREN)
25162 vec<tree, va_gc> *vec;
25163 int attr_flag = (attribute_takes_identifier_p (identifier)
25164 ? id_attr : normal_attr);
25165 vec = cp_parser_parenthesized_expression_list
25166 (parser, attr_flag, /*cast_p=*/false,
25167 /*allow_expansion_p=*/false,
25168 /*non_constant_p=*/NULL);
25169 if (vec == NULL)
25170 arguments = error_mark_node;
25171 else
25173 arguments = build_tree_list_vec (vec);
25174 release_tree_vector (vec);
25176 /* Save the arguments away. */
25177 TREE_VALUE (attribute) = arguments;
25180 if (arguments != error_mark_node)
25182 /* Add this attribute to the list. */
25183 TREE_CHAIN (attribute) = attribute_list;
25184 attribute_list = attribute;
25187 token = cp_lexer_peek_token (parser->lexer);
25189 /* Now, look for more attributes. If the next token isn't a
25190 `,', we're done. */
25191 if (token->type != CPP_COMMA)
25192 break;
25194 /* Consume the comma and keep going. */
25195 cp_lexer_consume_token (parser->lexer);
25197 parser->translate_strings_p = save_translate_strings_p;
25199 /* We built up the list in reverse order. */
25200 return nreverse (attribute_list);
25203 /* Parse a standard C++11 attribute.
25205 The returned representation is a TREE_LIST which TREE_PURPOSE is
25206 the scoped name of the attribute, and the TREE_VALUE is its
25207 arguments list.
25209 Note that the scoped name of the attribute is itself a TREE_LIST
25210 which TREE_PURPOSE is the namespace of the attribute, and
25211 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25212 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25213 and which TREE_PURPOSE is directly the attribute name.
25215 Clients of the attribute code should use get_attribute_namespace
25216 and get_attribute_name to get the actual namespace and name of
25217 attributes, regardless of their being GNU or C++11 attributes.
25219 attribute:
25220 attribute-token attribute-argument-clause [opt]
25222 attribute-token:
25223 identifier
25224 attribute-scoped-token
25226 attribute-scoped-token:
25227 attribute-namespace :: identifier
25229 attribute-namespace:
25230 identifier
25232 attribute-argument-clause:
25233 ( balanced-token-seq )
25235 balanced-token-seq:
25236 balanced-token [opt]
25237 balanced-token-seq balanced-token
25239 balanced-token:
25240 ( balanced-token-seq )
25241 [ balanced-token-seq ]
25242 { balanced-token-seq }. */
25244 static tree
25245 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25247 tree attribute, attr_id = NULL_TREE, arguments;
25248 cp_token *token;
25250 temp_override<bool> cleanup
25251 (parser->auto_is_implicit_function_template_parm_p, false);
25253 /* First, parse name of the attribute, a.k.a attribute-token. */
25255 token = cp_lexer_peek_token (parser->lexer);
25256 if (token->type == CPP_NAME)
25257 attr_id = token->u.value;
25258 else if (token->type == CPP_KEYWORD)
25259 attr_id = ridpointers[(int) token->keyword];
25260 else if (token->flags & NAMED_OP)
25261 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25263 if (attr_id == NULL_TREE)
25264 return NULL_TREE;
25266 cp_lexer_consume_token (parser->lexer);
25268 token = cp_lexer_peek_token (parser->lexer);
25269 if (token->type == CPP_SCOPE)
25271 /* We are seeing a scoped attribute token. */
25273 cp_lexer_consume_token (parser->lexer);
25274 if (attr_ns)
25275 error_at (token->location, "attribute using prefix used together "
25276 "with scoped attribute token");
25277 attr_ns = attr_id;
25279 token = cp_lexer_consume_token (parser->lexer);
25280 if (token->type == CPP_NAME)
25281 attr_id = token->u.value;
25282 else if (token->type == CPP_KEYWORD)
25283 attr_id = ridpointers[(int) token->keyword];
25284 else if (token->flags & NAMED_OP)
25285 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25286 else
25288 error_at (token->location,
25289 "expected an identifier for the attribute name");
25290 return error_mark_node;
25293 attr_id = canonicalize_attr_name (attr_id);
25294 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25295 NULL_TREE);
25296 token = cp_lexer_peek_token (parser->lexer);
25298 else if (attr_ns)
25299 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25300 NULL_TREE);
25301 else
25303 attr_id = canonicalize_attr_name (attr_id);
25304 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25305 NULL_TREE);
25306 /* C++11 noreturn attribute is equivalent to GNU's. */
25307 if (is_attribute_p ("noreturn", attr_id))
25308 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25309 /* C++14 deprecated attribute is equivalent to GNU's. */
25310 else if (is_attribute_p ("deprecated", attr_id))
25311 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25312 /* C++17 fallthrough attribute is equivalent to GNU's. */
25313 else if (is_attribute_p ("fallthrough", attr_id))
25314 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25315 /* Transactional Memory TS optimize_for_synchronized attribute is
25316 equivalent to GNU transaction_callable. */
25317 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25318 TREE_PURPOSE (attribute)
25319 = get_identifier ("transaction_callable");
25320 /* Transactional Memory attributes are GNU attributes. */
25321 else if (tm_attr_to_mask (attr_id))
25322 TREE_PURPOSE (attribute) = attr_id;
25325 /* Now parse the optional argument clause of the attribute. */
25327 if (token->type != CPP_OPEN_PAREN)
25328 return attribute;
25331 vec<tree, va_gc> *vec;
25332 int attr_flag = normal_attr;
25334 if (attr_ns == get_identifier ("gnu")
25335 && attribute_takes_identifier_p (attr_id))
25336 /* A GNU attribute that takes an identifier in parameter. */
25337 attr_flag = id_attr;
25339 vec = cp_parser_parenthesized_expression_list
25340 (parser, attr_flag, /*cast_p=*/false,
25341 /*allow_expansion_p=*/true,
25342 /*non_constant_p=*/NULL);
25343 if (vec == NULL)
25344 arguments = error_mark_node;
25345 else
25347 arguments = build_tree_list_vec (vec);
25348 release_tree_vector (vec);
25351 if (arguments == error_mark_node)
25352 attribute = error_mark_node;
25353 else
25354 TREE_VALUE (attribute) = arguments;
25357 return attribute;
25360 /* Check that the attribute ATTRIBUTE appears at most once in the
25361 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25362 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25363 isn't implemented yet in GCC. */
25365 static void
25366 cp_parser_check_std_attribute (tree attributes, tree attribute)
25368 if (attributes)
25370 tree name = get_attribute_name (attribute);
25371 if (is_attribute_p ("noreturn", name)
25372 && lookup_attribute ("noreturn", attributes))
25373 error ("attribute %<noreturn%> can appear at most once "
25374 "in an attribute-list");
25375 else if (is_attribute_p ("deprecated", name)
25376 && lookup_attribute ("deprecated", attributes))
25377 error ("attribute %<deprecated%> can appear at most once "
25378 "in an attribute-list");
25382 /* Parse a list of standard C++-11 attributes.
25384 attribute-list:
25385 attribute [opt]
25386 attribute-list , attribute[opt]
25387 attribute ...
25388 attribute-list , attribute ...
25391 static tree
25392 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25394 tree attributes = NULL_TREE, attribute = NULL_TREE;
25395 cp_token *token = NULL;
25397 while (true)
25399 attribute = cp_parser_std_attribute (parser, attr_ns);
25400 if (attribute == error_mark_node)
25401 break;
25402 if (attribute != NULL_TREE)
25404 cp_parser_check_std_attribute (attributes, attribute);
25405 TREE_CHAIN (attribute) = attributes;
25406 attributes = attribute;
25408 token = cp_lexer_peek_token (parser->lexer);
25409 if (token->type == CPP_ELLIPSIS)
25411 cp_lexer_consume_token (parser->lexer);
25412 if (attribute == NULL_TREE)
25413 error_at (token->location,
25414 "expected attribute before %<...%>");
25415 else
25417 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25418 if (pack == error_mark_node)
25419 return error_mark_node;
25420 TREE_VALUE (attribute) = pack;
25422 token = cp_lexer_peek_token (parser->lexer);
25424 if (token->type != CPP_COMMA)
25425 break;
25426 cp_lexer_consume_token (parser->lexer);
25428 attributes = nreverse (attributes);
25429 return attributes;
25432 /* Parse a standard C++-11 attribute specifier.
25434 attribute-specifier:
25435 [ [ attribute-using-prefix [opt] attribute-list ] ]
25436 alignment-specifier
25438 attribute-using-prefix:
25439 using attribute-namespace :
25441 alignment-specifier:
25442 alignas ( type-id ... [opt] )
25443 alignas ( alignment-expression ... [opt] ). */
25445 static tree
25446 cp_parser_std_attribute_spec (cp_parser *parser)
25448 tree attributes = NULL_TREE;
25449 cp_token *token = cp_lexer_peek_token (parser->lexer);
25451 if (token->type == CPP_OPEN_SQUARE
25452 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25454 tree attr_ns = NULL_TREE;
25456 cp_lexer_consume_token (parser->lexer);
25457 cp_lexer_consume_token (parser->lexer);
25459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25461 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25462 if (token->type == CPP_NAME)
25463 attr_ns = token->u.value;
25464 else if (token->type == CPP_KEYWORD)
25465 attr_ns = ridpointers[(int) token->keyword];
25466 else if (token->flags & NAMED_OP)
25467 attr_ns = get_identifier (cpp_type2name (token->type,
25468 token->flags));
25469 if (attr_ns
25470 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25472 if (cxx_dialect < cxx17
25473 && !in_system_header_at (input_location))
25474 pedwarn (input_location, 0,
25475 "attribute using prefix only available "
25476 "with -std=c++17 or -std=gnu++17");
25478 cp_lexer_consume_token (parser->lexer);
25479 cp_lexer_consume_token (parser->lexer);
25480 cp_lexer_consume_token (parser->lexer);
25482 else
25483 attr_ns = NULL_TREE;
25486 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25488 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25489 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25490 cp_parser_skip_to_end_of_statement (parser);
25491 else
25492 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25493 when we are sure that we have actually parsed them. */
25494 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25496 else
25498 tree alignas_expr;
25500 /* Look for an alignment-specifier. */
25502 token = cp_lexer_peek_token (parser->lexer);
25504 if (token->type != CPP_KEYWORD
25505 || token->keyword != RID_ALIGNAS)
25506 return NULL_TREE;
25508 cp_lexer_consume_token (parser->lexer);
25509 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25511 matching_parens parens;
25512 if (!parens.require_open (parser))
25513 return error_mark_node;
25515 cp_parser_parse_tentatively (parser);
25516 alignas_expr = cp_parser_type_id (parser);
25518 if (!cp_parser_parse_definitely (parser))
25520 alignas_expr = cp_parser_assignment_expression (parser);
25521 if (alignas_expr == error_mark_node)
25522 cp_parser_skip_to_end_of_statement (parser);
25523 if (alignas_expr == NULL_TREE
25524 || alignas_expr == error_mark_node)
25525 return alignas_expr;
25528 alignas_expr = cxx_alignas_expr (alignas_expr);
25529 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25531 /* Handle alignas (pack...). */
25532 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25534 cp_lexer_consume_token (parser->lexer);
25535 alignas_expr = make_pack_expansion (alignas_expr);
25538 /* Something went wrong, so don't build the attribute. */
25539 if (alignas_expr == error_mark_node)
25540 return error_mark_node;
25542 if (!parens.require_close (parser))
25543 return error_mark_node;
25545 /* Build the C++-11 representation of an 'aligned'
25546 attribute. */
25547 attributes =
25548 build_tree_list (build_tree_list (get_identifier ("gnu"),
25549 get_identifier ("aligned")),
25550 alignas_expr);
25553 return attributes;
25556 /* Parse a standard C++-11 attribute-specifier-seq.
25558 attribute-specifier-seq:
25559 attribute-specifier-seq [opt] attribute-specifier
25562 static tree
25563 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25565 tree attr_specs = NULL_TREE;
25566 tree attr_last = NULL_TREE;
25568 while (true)
25570 tree attr_spec = cp_parser_std_attribute_spec (parser);
25571 if (attr_spec == NULL_TREE)
25572 break;
25573 if (attr_spec == error_mark_node)
25574 return error_mark_node;
25576 if (attr_last)
25577 TREE_CHAIN (attr_last) = attr_spec;
25578 else
25579 attr_specs = attr_last = attr_spec;
25580 attr_last = tree_last (attr_last);
25583 return attr_specs;
25586 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25587 return index of the first token after balanced-token, or N on failure. */
25589 static size_t
25590 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25592 size_t orig_n = n;
25593 int nparens = 0, nbraces = 0, nsquares = 0;
25595 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25597 case CPP_EOF:
25598 case CPP_PRAGMA_EOL:
25599 /* Ran out of tokens. */
25600 return orig_n;
25601 case CPP_OPEN_PAREN:
25602 ++nparens;
25603 break;
25604 case CPP_OPEN_BRACE:
25605 ++nbraces;
25606 break;
25607 case CPP_OPEN_SQUARE:
25608 ++nsquares;
25609 break;
25610 case CPP_CLOSE_PAREN:
25611 --nparens;
25612 break;
25613 case CPP_CLOSE_BRACE:
25614 --nbraces;
25615 break;
25616 case CPP_CLOSE_SQUARE:
25617 --nsquares;
25618 break;
25619 default:
25620 break;
25622 while (nparens || nbraces || nsquares);
25623 return n;
25626 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25627 return index of the first token after the GNU attribute tokens, or N on
25628 failure. */
25630 static size_t
25631 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25633 while (true)
25635 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25636 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25637 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25638 break;
25640 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25641 if (n2 == n + 2)
25642 break;
25643 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25644 break;
25645 n = n2 + 1;
25647 return n;
25650 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25651 next token), return index of the first token after the standard C++11
25652 attribute tokens, or N on failure. */
25654 static size_t
25655 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25657 while (true)
25659 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25660 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25662 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25663 if (n2 == n + 1)
25664 break;
25665 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25666 break;
25667 n = n2 + 1;
25669 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25670 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25672 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25673 if (n2 == n + 1)
25674 break;
25675 n = n2;
25677 else
25678 break;
25680 return n;
25683 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25684 as the next token), return index of the first token after the attribute
25685 tokens, or N on failure. */
25687 static size_t
25688 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25690 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25691 return cp_parser_skip_gnu_attributes_opt (parser, n);
25692 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25695 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25696 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25697 current value of the PEDANTIC flag, regardless of whether or not
25698 the `__extension__' keyword is present. The caller is responsible
25699 for restoring the value of the PEDANTIC flag. */
25701 static bool
25702 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25704 /* Save the old value of the PEDANTIC flag. */
25705 *saved_pedantic = pedantic;
25707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25709 /* Consume the `__extension__' token. */
25710 cp_lexer_consume_token (parser->lexer);
25711 /* We're not being pedantic while the `__extension__' keyword is
25712 in effect. */
25713 pedantic = 0;
25715 return true;
25718 return false;
25721 /* Parse a label declaration.
25723 label-declaration:
25724 __label__ label-declarator-seq ;
25726 label-declarator-seq:
25727 identifier , label-declarator-seq
25728 identifier */
25730 static void
25731 cp_parser_label_declaration (cp_parser* parser)
25733 /* Look for the `__label__' keyword. */
25734 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25736 while (true)
25738 tree identifier;
25740 /* Look for an identifier. */
25741 identifier = cp_parser_identifier (parser);
25742 /* If we failed, stop. */
25743 if (identifier == error_mark_node)
25744 break;
25745 /* Declare it as a label. */
25746 finish_label_decl (identifier);
25747 /* If the next token is a `;', stop. */
25748 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25749 break;
25750 /* Look for the `,' separating the label declarations. */
25751 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25754 /* Look for the final `;'. */
25755 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25758 // -------------------------------------------------------------------------- //
25759 // Requires Clause
25761 // Parse a requires clause.
25763 // requires-clause:
25764 // 'requires' logical-or-expression
25766 // The required logical-or-expression must be a constant expression. Note
25767 // that we don't check that the expression is constepxr here. We defer until
25768 // we analyze constraints and then, we only check atomic constraints.
25769 static tree
25770 cp_parser_requires_clause (cp_parser *parser)
25772 // Parse the requires clause so that it is not automatically folded.
25773 ++processing_template_decl;
25774 tree expr = cp_parser_binary_expression (parser, false, false,
25775 PREC_NOT_OPERATOR, NULL);
25776 if (check_for_bare_parameter_packs (expr))
25777 expr = error_mark_node;
25778 --processing_template_decl;
25779 return expr;
25782 // Optionally parse a requires clause:
25783 static tree
25784 cp_parser_requires_clause_opt (cp_parser *parser)
25786 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25787 if (tok->keyword != RID_REQUIRES)
25789 if (!flag_concepts && tok->type == CPP_NAME
25790 && tok->u.value == ridpointers[RID_REQUIRES])
25792 error_at (cp_lexer_peek_token (parser->lexer)->location,
25793 "%<requires%> only available with -fconcepts");
25794 /* Parse and discard the requires-clause. */
25795 cp_lexer_consume_token (parser->lexer);
25796 cp_parser_requires_clause (parser);
25798 return NULL_TREE;
25800 cp_lexer_consume_token (parser->lexer);
25801 return cp_parser_requires_clause (parser);
25805 /*---------------------------------------------------------------------------
25806 Requires expressions
25807 ---------------------------------------------------------------------------*/
25809 /* Parse a requires expression
25811 requirement-expression:
25812 'requires' requirement-parameter-list [opt] requirement-body */
25813 static tree
25814 cp_parser_requires_expression (cp_parser *parser)
25816 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25817 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25819 /* A requires-expression shall appear only within a concept
25820 definition or a requires-clause.
25822 TODO: Implement this diagnostic correctly. */
25823 if (!processing_template_decl)
25825 error_at (loc, "a requires expression cannot appear outside a template");
25826 cp_parser_skip_to_end_of_statement (parser);
25827 return error_mark_node;
25830 tree parms, reqs;
25832 /* Local parameters are delared as variables within the scope
25833 of the expression. They are not visible past the end of
25834 the expression. Expressions within the requires-expression
25835 are unevaluated. */
25836 struct scope_sentinel
25838 scope_sentinel ()
25840 ++cp_unevaluated_operand;
25841 begin_scope (sk_block, NULL_TREE);
25844 ~scope_sentinel ()
25846 pop_bindings_and_leave_scope ();
25847 --cp_unevaluated_operand;
25849 } s;
25851 /* Parse the optional parameter list. */
25852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25854 parms = cp_parser_requirement_parameter_list (parser);
25855 if (parms == error_mark_node)
25856 return error_mark_node;
25858 else
25859 parms = NULL_TREE;
25861 /* Parse the requirement body. */
25862 reqs = cp_parser_requirement_body (parser);
25863 if (reqs == error_mark_node)
25864 return error_mark_node;
25867 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25868 the parm chain. */
25869 grokparms (parms, &parms);
25870 return finish_requires_expr (parms, reqs);
25873 /* Parse a parameterized requirement.
25875 requirement-parameter-list:
25876 '(' parameter-declaration-clause ')' */
25877 static tree
25878 cp_parser_requirement_parameter_list (cp_parser *parser)
25880 matching_parens parens;
25881 if (!parens.require_open (parser))
25882 return error_mark_node;
25884 tree parms = cp_parser_parameter_declaration_clause (parser);
25886 if (!parens.require_close (parser))
25887 return error_mark_node;
25889 return parms;
25892 /* Parse the body of a requirement.
25894 requirement-body:
25895 '{' requirement-list '}' */
25896 static tree
25897 cp_parser_requirement_body (cp_parser *parser)
25899 matching_braces braces;
25900 if (!braces.require_open (parser))
25901 return error_mark_node;
25903 tree reqs = cp_parser_requirement_list (parser);
25905 if (!braces.require_close (parser))
25906 return error_mark_node;
25908 return reqs;
25911 /* Parse a list of requirements.
25913 requirement-list:
25914 requirement
25915 requirement-list ';' requirement[opt] */
25916 static tree
25917 cp_parser_requirement_list (cp_parser *parser)
25919 tree result = NULL_TREE;
25920 while (true)
25922 tree req = cp_parser_requirement (parser);
25923 if (req == error_mark_node)
25924 return error_mark_node;
25926 result = tree_cons (NULL_TREE, req, result);
25928 /* If we see a semi-colon, consume it. */
25929 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25930 cp_lexer_consume_token (parser->lexer);
25932 /* Stop processing at the end of the list. */
25933 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25934 break;
25937 /* Reverse the order of requirements so they are analyzed in
25938 declaration order. */
25939 return nreverse (result);
25942 /* Parse a syntactic requirement or type requirement.
25944 requirement:
25945 simple-requirement
25946 compound-requirement
25947 type-requirement
25948 nested-requirement */
25949 static tree
25950 cp_parser_requirement (cp_parser *parser)
25952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25953 return cp_parser_compound_requirement (parser);
25954 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25955 return cp_parser_type_requirement (parser);
25956 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25957 return cp_parser_nested_requirement (parser);
25958 else
25959 return cp_parser_simple_requirement (parser);
25962 /* Parse a simple requirement.
25964 simple-requirement:
25965 expression ';' */
25966 static tree
25967 cp_parser_simple_requirement (cp_parser *parser)
25969 tree expr = cp_parser_expression (parser, NULL, false, false);
25970 if (!expr || expr == error_mark_node)
25971 return error_mark_node;
25973 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25974 return error_mark_node;
25976 return finish_simple_requirement (expr);
25979 /* Parse a type requirement
25981 type-requirement
25982 nested-name-specifier [opt] required-type-name ';'
25984 required-type-name:
25985 type-name
25986 'template' [opt] simple-template-id */
25987 static tree
25988 cp_parser_type_requirement (cp_parser *parser)
25990 cp_lexer_consume_token (parser->lexer);
25992 // Save the scope before parsing name specifiers.
25993 tree saved_scope = parser->scope;
25994 tree saved_object_scope = parser->object_scope;
25995 tree saved_qualifying_scope = parser->qualifying_scope;
25996 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25997 cp_parser_nested_name_specifier_opt (parser,
25998 /*typename_keyword_p=*/true,
25999 /*check_dependency_p=*/false,
26000 /*type_p=*/true,
26001 /*is_declaration=*/false);
26003 tree type;
26004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26006 cp_lexer_consume_token (parser->lexer);
26007 type = cp_parser_template_id (parser,
26008 /*template_keyword_p=*/true,
26009 /*check_dependency=*/false,
26010 /*tag_type=*/none_type,
26011 /*is_declaration=*/false);
26012 type = make_typename_type (parser->scope, type, typename_type,
26013 /*complain=*/tf_error);
26015 else
26016 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26018 if (TREE_CODE (type) == TYPE_DECL)
26019 type = TREE_TYPE (type);
26021 parser->scope = saved_scope;
26022 parser->object_scope = saved_object_scope;
26023 parser->qualifying_scope = saved_qualifying_scope;
26025 if (type == error_mark_node)
26026 cp_parser_skip_to_end_of_statement (parser);
26028 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26029 return error_mark_node;
26030 if (type == error_mark_node)
26031 return error_mark_node;
26033 return finish_type_requirement (type);
26036 /* Parse a compound requirement
26038 compound-requirement:
26039 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26040 static tree
26041 cp_parser_compound_requirement (cp_parser *parser)
26043 /* Parse an expression enclosed in '{ }'s. */
26044 matching_braces braces;
26045 if (!braces.require_open (parser))
26046 return error_mark_node;
26048 tree expr = cp_parser_expression (parser, NULL, false, false);
26049 if (!expr || expr == error_mark_node)
26050 return error_mark_node;
26052 if (!braces.require_close (parser))
26053 return error_mark_node;
26055 /* Parse the optional noexcept. */
26056 bool noexcept_p = false;
26057 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26059 cp_lexer_consume_token (parser->lexer);
26060 noexcept_p = true;
26063 /* Parse the optional trailing return type. */
26064 tree type = NULL_TREE;
26065 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26067 cp_lexer_consume_token (parser->lexer);
26068 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26069 parser->in_result_type_constraint_p = true;
26070 type = cp_parser_trailing_type_id (parser);
26071 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26072 if (type == error_mark_node)
26073 return error_mark_node;
26076 return finish_compound_requirement (expr, type, noexcept_p);
26079 /* Parse a nested requirement. This is the same as a requires clause.
26081 nested-requirement:
26082 requires-clause */
26083 static tree
26084 cp_parser_nested_requirement (cp_parser *parser)
26086 cp_lexer_consume_token (parser->lexer);
26087 tree req = cp_parser_requires_clause (parser);
26088 if (req == error_mark_node)
26089 return error_mark_node;
26090 return finish_nested_requirement (req);
26093 /* Support Functions */
26095 /* Return the appropriate prefer_type argument for lookup_name_real based on
26096 tag_type and template_mem_access. */
26098 static inline int
26099 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26101 /* DR 141: When looking in the current enclosing context for a template-name
26102 after -> or ., only consider class templates. */
26103 if (template_mem_access)
26104 return 2;
26105 switch (tag_type)
26107 case none_type: return 0; // No preference.
26108 case scope_type: return 1; // Type or namespace.
26109 default: return 2; // Type only.
26113 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26114 NAME should have one of the representations used for an
26115 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26116 is returned. If PARSER->SCOPE is a dependent type, then a
26117 SCOPE_REF is returned.
26119 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26120 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26121 was formed. Abstractly, such entities should not be passed to this
26122 function, because they do not need to be looked up, but it is
26123 simpler to check for this special case here, rather than at the
26124 call-sites.
26126 In cases not explicitly covered above, this function returns a
26127 DECL, OVERLOAD, or baselink representing the result of the lookup.
26128 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26129 is returned.
26131 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26132 (e.g., "struct") that was used. In that case bindings that do not
26133 refer to types are ignored.
26135 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26136 ignored.
26138 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26139 are ignored.
26141 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26142 types.
26144 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26145 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26146 NULL_TREE otherwise. */
26148 static cp_expr
26149 cp_parser_lookup_name (cp_parser *parser, tree name,
26150 enum tag_types tag_type,
26151 bool is_template,
26152 bool is_namespace,
26153 bool check_dependency,
26154 tree *ambiguous_decls,
26155 location_t name_location)
26157 tree decl;
26158 tree object_type = parser->context->object_type;
26160 /* Assume that the lookup will be unambiguous. */
26161 if (ambiguous_decls)
26162 *ambiguous_decls = NULL_TREE;
26164 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26165 no longer valid. Note that if we are parsing tentatively, and
26166 the parse fails, OBJECT_TYPE will be automatically restored. */
26167 parser->context->object_type = NULL_TREE;
26169 if (name == error_mark_node)
26170 return error_mark_node;
26172 /* A template-id has already been resolved; there is no lookup to
26173 do. */
26174 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26175 return name;
26176 if (BASELINK_P (name))
26178 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26179 == TEMPLATE_ID_EXPR);
26180 return name;
26183 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26184 it should already have been checked to make sure that the name
26185 used matches the type being destroyed. */
26186 if (TREE_CODE (name) == BIT_NOT_EXPR)
26188 tree type;
26190 /* Figure out to which type this destructor applies. */
26191 if (parser->scope)
26192 type = parser->scope;
26193 else if (object_type)
26194 type = object_type;
26195 else
26196 type = current_class_type;
26197 /* If that's not a class type, there is no destructor. */
26198 if (!type || !CLASS_TYPE_P (type))
26199 return error_mark_node;
26201 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26202 lazily_declare_fn (sfk_destructor, type);
26204 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26205 return dtor;
26207 return error_mark_node;
26210 /* By this point, the NAME should be an ordinary identifier. If
26211 the id-expression was a qualified name, the qualifying scope is
26212 stored in PARSER->SCOPE at this point. */
26213 gcc_assert (identifier_p (name));
26215 /* Perform the lookup. */
26216 if (parser->scope)
26218 bool dependent_p;
26220 if (parser->scope == error_mark_node)
26221 return error_mark_node;
26223 /* If the SCOPE is dependent, the lookup must be deferred until
26224 the template is instantiated -- unless we are explicitly
26225 looking up names in uninstantiated templates. Even then, we
26226 cannot look up the name if the scope is not a class type; it
26227 might, for example, be a template type parameter. */
26228 dependent_p = (TYPE_P (parser->scope)
26229 && dependent_scope_p (parser->scope));
26230 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26231 && dependent_p)
26232 /* Defer lookup. */
26233 decl = error_mark_node;
26234 else
26236 tree pushed_scope = NULL_TREE;
26238 /* If PARSER->SCOPE is a dependent type, then it must be a
26239 class type, and we must not be checking dependencies;
26240 otherwise, we would have processed this lookup above. So
26241 that PARSER->SCOPE is not considered a dependent base by
26242 lookup_member, we must enter the scope here. */
26243 if (dependent_p)
26244 pushed_scope = push_scope (parser->scope);
26246 /* If the PARSER->SCOPE is a template specialization, it
26247 may be instantiated during name lookup. In that case,
26248 errors may be issued. Even if we rollback the current
26249 tentative parse, those errors are valid. */
26250 decl = lookup_qualified_name (parser->scope, name,
26251 prefer_type_arg (tag_type),
26252 /*complain=*/true);
26254 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26255 lookup result and the nested-name-specifier nominates a class C:
26256 * if the name specified after the nested-name-specifier, when
26257 looked up in C, is the injected-class-name of C (Clause 9), or
26258 * if the name specified after the nested-name-specifier is the
26259 same as the identifier or the simple-template-id's template-
26260 name in the last component of the nested-name-specifier,
26261 the name is instead considered to name the constructor of
26262 class C. [ Note: for example, the constructor is not an
26263 acceptable lookup result in an elaborated-type-specifier so
26264 the constructor would not be used in place of the
26265 injected-class-name. --end note ] Such a constructor name
26266 shall be used only in the declarator-id of a declaration that
26267 names a constructor or in a using-declaration. */
26268 if (tag_type == none_type
26269 && DECL_SELF_REFERENCE_P (decl)
26270 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26271 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26272 prefer_type_arg (tag_type),
26273 /*complain=*/true);
26275 /* If we have a single function from a using decl, pull it out. */
26276 if (TREE_CODE (decl) == OVERLOAD
26277 && !really_overloaded_fn (decl))
26278 decl = OVL_FUNCTION (decl);
26280 if (pushed_scope)
26281 pop_scope (pushed_scope);
26284 /* If the scope is a dependent type and either we deferred lookup or
26285 we did lookup but didn't find the name, rememeber the name. */
26286 if (decl == error_mark_node && TYPE_P (parser->scope)
26287 && dependent_type_p (parser->scope))
26289 if (tag_type)
26291 tree type;
26293 /* The resolution to Core Issue 180 says that `struct
26294 A::B' should be considered a type-name, even if `A'
26295 is dependent. */
26296 type = make_typename_type (parser->scope, name, tag_type,
26297 /*complain=*/tf_error);
26298 if (type != error_mark_node)
26299 decl = TYPE_NAME (type);
26301 else if (is_template
26302 && (cp_parser_next_token_ends_template_argument_p (parser)
26303 || cp_lexer_next_token_is (parser->lexer,
26304 CPP_CLOSE_PAREN)))
26305 decl = make_unbound_class_template (parser->scope,
26306 name, NULL_TREE,
26307 /*complain=*/tf_error);
26308 else
26309 decl = build_qualified_name (/*type=*/NULL_TREE,
26310 parser->scope, name,
26311 is_template);
26313 parser->qualifying_scope = parser->scope;
26314 parser->object_scope = NULL_TREE;
26316 else if (object_type)
26318 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26319 OBJECT_TYPE is not a class. */
26320 if (CLASS_TYPE_P (object_type))
26321 /* If the OBJECT_TYPE is a template specialization, it may
26322 be instantiated during name lookup. In that case, errors
26323 may be issued. Even if we rollback the current tentative
26324 parse, those errors are valid. */
26325 decl = lookup_member (object_type,
26326 name,
26327 /*protect=*/0,
26328 prefer_type_arg (tag_type),
26329 tf_warning_or_error);
26330 else
26331 decl = NULL_TREE;
26333 if (!decl)
26334 /* Look it up in the enclosing context. DR 141: When looking for a
26335 template-name after -> or ., only consider class templates. */
26336 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26337 /*nonclass=*/0,
26338 /*block_p=*/true, is_namespace, 0);
26339 if (object_type == unknown_type_node)
26340 /* The object is type-dependent, so we can't look anything up; we used
26341 this to get the DR 141 behavior. */
26342 object_type = NULL_TREE;
26343 parser->object_scope = object_type;
26344 parser->qualifying_scope = NULL_TREE;
26346 else
26348 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26349 /*nonclass=*/0,
26350 /*block_p=*/true, is_namespace, 0);
26351 parser->qualifying_scope = NULL_TREE;
26352 parser->object_scope = NULL_TREE;
26355 /* If the lookup failed, let our caller know. */
26356 if (!decl || decl == error_mark_node)
26357 return error_mark_node;
26359 /* Pull out the template from an injected-class-name (or multiple). */
26360 if (is_template)
26361 decl = maybe_get_template_decl_from_type_decl (decl);
26363 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26364 if (TREE_CODE (decl) == TREE_LIST)
26366 if (ambiguous_decls)
26367 *ambiguous_decls = decl;
26368 /* The error message we have to print is too complicated for
26369 cp_parser_error, so we incorporate its actions directly. */
26370 if (!cp_parser_simulate_error (parser))
26372 error_at (name_location, "reference to %qD is ambiguous",
26373 name);
26374 print_candidates (decl);
26376 return error_mark_node;
26379 gcc_assert (DECL_P (decl)
26380 || TREE_CODE (decl) == OVERLOAD
26381 || TREE_CODE (decl) == SCOPE_REF
26382 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26383 || BASELINK_P (decl));
26385 /* If we have resolved the name of a member declaration, check to
26386 see if the declaration is accessible. When the name resolves to
26387 set of overloaded functions, accessibility is checked when
26388 overload resolution is done.
26390 During an explicit instantiation, access is not checked at all,
26391 as per [temp.explicit]. */
26392 if (DECL_P (decl))
26393 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26395 maybe_record_typedef_use (decl);
26397 return cp_expr (decl, name_location);
26400 /* Like cp_parser_lookup_name, but for use in the typical case where
26401 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26402 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26404 static tree
26405 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26407 return cp_parser_lookup_name (parser, name,
26408 none_type,
26409 /*is_template=*/false,
26410 /*is_namespace=*/false,
26411 /*check_dependency=*/true,
26412 /*ambiguous_decls=*/NULL,
26413 location);
26416 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26417 the current context, return the TYPE_DECL. If TAG_NAME_P is
26418 true, the DECL indicates the class being defined in a class-head,
26419 or declared in an elaborated-type-specifier.
26421 Otherwise, return DECL. */
26423 static tree
26424 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26426 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26427 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26429 struct A {
26430 template <typename T> struct B;
26433 template <typename T> struct A::B {};
26435 Similarly, in an elaborated-type-specifier:
26437 namespace N { struct X{}; }
26439 struct A {
26440 template <typename T> friend struct N::X;
26443 However, if the DECL refers to a class type, and we are in
26444 the scope of the class, then the name lookup automatically
26445 finds the TYPE_DECL created by build_self_reference rather
26446 than a TEMPLATE_DECL. For example, in:
26448 template <class T> struct S {
26449 S s;
26452 there is no need to handle such case. */
26454 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26455 return DECL_TEMPLATE_RESULT (decl);
26457 return decl;
26460 /* If too many, or too few, template-parameter lists apply to the
26461 declarator, issue an error message. Returns TRUE if all went well,
26462 and FALSE otherwise. */
26464 static bool
26465 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26466 cp_declarator *declarator,
26467 location_t declarator_location)
26469 switch (declarator->kind)
26471 case cdk_id:
26473 unsigned num_templates = 0;
26474 tree scope = declarator->u.id.qualifying_scope;
26475 bool template_id_p = false;
26477 if (scope)
26478 num_templates = num_template_headers_for_class (scope);
26479 else if (TREE_CODE (declarator->u.id.unqualified_name)
26480 == TEMPLATE_ID_EXPR)
26482 /* If the DECLARATOR has the form `X<y>' then it uses one
26483 additional level of template parameters. */
26484 ++num_templates;
26485 template_id_p = true;
26488 return cp_parser_check_template_parameters
26489 (parser, num_templates, template_id_p, declarator_location,
26490 declarator);
26493 case cdk_function:
26494 case cdk_array:
26495 case cdk_pointer:
26496 case cdk_reference:
26497 case cdk_ptrmem:
26498 return (cp_parser_check_declarator_template_parameters
26499 (parser, declarator->declarator, declarator_location));
26501 case cdk_decomp:
26502 case cdk_error:
26503 return true;
26505 default:
26506 gcc_unreachable ();
26508 return false;
26511 /* NUM_TEMPLATES were used in the current declaration. If that is
26512 invalid, return FALSE and issue an error messages. Otherwise,
26513 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26514 declarator and we can print more accurate diagnostics. */
26516 static bool
26517 cp_parser_check_template_parameters (cp_parser* parser,
26518 unsigned num_templates,
26519 bool template_id_p,
26520 location_t location,
26521 cp_declarator *declarator)
26523 /* If there are the same number of template classes and parameter
26524 lists, that's OK. */
26525 if (parser->num_template_parameter_lists == num_templates)
26526 return true;
26527 /* If there are more, but only one more, and the name ends in an identifier,
26528 then we are declaring a primary template. That's OK too. */
26529 if (!template_id_p
26530 && parser->num_template_parameter_lists == num_templates + 1)
26531 return true;
26532 /* If there are more template classes than parameter lists, we have
26533 something like:
26535 template <class T> void S<T>::R<T>::f (); */
26536 if (parser->num_template_parameter_lists < num_templates)
26538 if (declarator && !current_function_decl)
26539 error_at (location, "specializing member %<%T::%E%> "
26540 "requires %<template<>%> syntax",
26541 declarator->u.id.qualifying_scope,
26542 declarator->u.id.unqualified_name);
26543 else if (declarator)
26544 error_at (location, "invalid declaration of %<%T::%E%>",
26545 declarator->u.id.qualifying_scope,
26546 declarator->u.id.unqualified_name);
26547 else
26548 error_at (location, "too few template-parameter-lists");
26549 return false;
26551 /* Otherwise, there are too many template parameter lists. We have
26552 something like:
26554 template <class T> template <class U> void S::f(); */
26555 error_at (location, "too many template-parameter-lists");
26556 return false;
26559 /* Parse an optional `::' token indicating that the following name is
26560 from the global namespace. If so, PARSER->SCOPE is set to the
26561 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26562 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26563 Returns the new value of PARSER->SCOPE, if the `::' token is
26564 present, and NULL_TREE otherwise. */
26566 static tree
26567 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26569 cp_token *token;
26571 /* Peek at the next token. */
26572 token = cp_lexer_peek_token (parser->lexer);
26573 /* If we're looking at a `::' token then we're starting from the
26574 global namespace, not our current location. */
26575 if (token->type == CPP_SCOPE)
26577 /* Consume the `::' token. */
26578 cp_lexer_consume_token (parser->lexer);
26579 /* Set the SCOPE so that we know where to start the lookup. */
26580 parser->scope = global_namespace;
26581 parser->qualifying_scope = global_namespace;
26582 parser->object_scope = NULL_TREE;
26584 return parser->scope;
26586 else if (!current_scope_valid_p)
26588 parser->scope = NULL_TREE;
26589 parser->qualifying_scope = NULL_TREE;
26590 parser->object_scope = NULL_TREE;
26593 return NULL_TREE;
26596 /* Returns TRUE if the upcoming token sequence is the start of a
26597 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26598 declarator is preceded by the `friend' specifier. */
26600 static bool
26601 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26603 bool constructor_p;
26604 bool outside_class_specifier_p;
26605 tree nested_name_specifier;
26606 cp_token *next_token;
26608 /* The common case is that this is not a constructor declarator, so
26609 try to avoid doing lots of work if at all possible. It's not
26610 valid declare a constructor at function scope. */
26611 if (parser->in_function_body)
26612 return false;
26613 /* And only certain tokens can begin a constructor declarator. */
26614 next_token = cp_lexer_peek_token (parser->lexer);
26615 if (next_token->type != CPP_NAME
26616 && next_token->type != CPP_SCOPE
26617 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26618 && next_token->type != CPP_TEMPLATE_ID)
26619 return false;
26621 /* Parse tentatively; we are going to roll back all of the tokens
26622 consumed here. */
26623 cp_parser_parse_tentatively (parser);
26624 /* Assume that we are looking at a constructor declarator. */
26625 constructor_p = true;
26627 /* Look for the optional `::' operator. */
26628 cp_parser_global_scope_opt (parser,
26629 /*current_scope_valid_p=*/false);
26630 /* Look for the nested-name-specifier. */
26631 nested_name_specifier
26632 = (cp_parser_nested_name_specifier_opt (parser,
26633 /*typename_keyword_p=*/false,
26634 /*check_dependency_p=*/false,
26635 /*type_p=*/false,
26636 /*is_declaration=*/false));
26638 outside_class_specifier_p = (!at_class_scope_p ()
26639 || !TYPE_BEING_DEFINED (current_class_type)
26640 || friend_p);
26642 /* Outside of a class-specifier, there must be a
26643 nested-name-specifier. Except in C++17 mode, where we
26644 might be declaring a guiding declaration. */
26645 if (!nested_name_specifier && outside_class_specifier_p
26646 && cxx_dialect < cxx17)
26647 constructor_p = false;
26648 else if (nested_name_specifier == error_mark_node)
26649 constructor_p = false;
26651 /* If we have a class scope, this is easy; DR 147 says that S::S always
26652 names the constructor, and no other qualified name could. */
26653 if (constructor_p && nested_name_specifier
26654 && CLASS_TYPE_P (nested_name_specifier))
26656 tree id = cp_parser_unqualified_id (parser,
26657 /*template_keyword_p=*/false,
26658 /*check_dependency_p=*/false,
26659 /*declarator_p=*/true,
26660 /*optional_p=*/false);
26661 if (is_overloaded_fn (id))
26662 id = DECL_NAME (get_first_fn (id));
26663 if (!constructor_name_p (id, nested_name_specifier))
26664 constructor_p = false;
26666 /* If we still think that this might be a constructor-declarator,
26667 look for a class-name. */
26668 else if (constructor_p)
26670 /* If we have:
26672 template <typename T> struct S {
26673 S();
26676 we must recognize that the nested `S' names a class. */
26677 if (cxx_dialect >= cxx17)
26678 cp_parser_parse_tentatively (parser);
26680 tree type_decl;
26681 type_decl = cp_parser_class_name (parser,
26682 /*typename_keyword_p=*/false,
26683 /*template_keyword_p=*/false,
26684 none_type,
26685 /*check_dependency_p=*/false,
26686 /*class_head_p=*/false,
26687 /*is_declaration=*/false);
26689 if (cxx_dialect >= cxx17
26690 && !cp_parser_parse_definitely (parser))
26692 type_decl = NULL_TREE;
26693 tree tmpl = cp_parser_template_name (parser,
26694 /*template_keyword*/false,
26695 /*check_dependency_p*/false,
26696 /*is_declaration*/false,
26697 none_type,
26698 /*is_identifier*/NULL);
26699 if (DECL_CLASS_TEMPLATE_P (tmpl)
26700 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26701 /* It's a deduction guide, return true. */;
26702 else
26703 cp_parser_simulate_error (parser);
26706 /* If there was no class-name, then this is not a constructor.
26707 Otherwise, if we are in a class-specifier and we aren't
26708 handling a friend declaration, check that its type matches
26709 current_class_type (c++/38313). Note: error_mark_node
26710 is left alone for error recovery purposes. */
26711 constructor_p = (!cp_parser_error_occurred (parser)
26712 && (outside_class_specifier_p
26713 || type_decl == NULL_TREE
26714 || type_decl == error_mark_node
26715 || same_type_p (current_class_type,
26716 TREE_TYPE (type_decl))));
26718 /* If we're still considering a constructor, we have to see a `(',
26719 to begin the parameter-declaration-clause, followed by either a
26720 `)', an `...', or a decl-specifier. We need to check for a
26721 type-specifier to avoid being fooled into thinking that:
26723 S (f) (int);
26725 is a constructor. (It is actually a function named `f' that
26726 takes one parameter (of type `int') and returns a value of type
26727 `S'. */
26728 if (constructor_p
26729 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26730 constructor_p = false;
26732 if (constructor_p
26733 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26734 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26735 /* A parameter declaration begins with a decl-specifier,
26736 which is either the "attribute" keyword, a storage class
26737 specifier, or (usually) a type-specifier. */
26738 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26740 tree type;
26741 tree pushed_scope = NULL_TREE;
26742 unsigned saved_num_template_parameter_lists;
26744 /* Names appearing in the type-specifier should be looked up
26745 in the scope of the class. */
26746 if (current_class_type)
26747 type = NULL_TREE;
26748 else if (type_decl)
26750 type = TREE_TYPE (type_decl);
26751 if (TREE_CODE (type) == TYPENAME_TYPE)
26753 type = resolve_typename_type (type,
26754 /*only_current_p=*/false);
26755 if (TREE_CODE (type) == TYPENAME_TYPE)
26757 cp_parser_abort_tentative_parse (parser);
26758 return false;
26761 pushed_scope = push_scope (type);
26764 /* Inside the constructor parameter list, surrounding
26765 template-parameter-lists do not apply. */
26766 saved_num_template_parameter_lists
26767 = parser->num_template_parameter_lists;
26768 parser->num_template_parameter_lists = 0;
26770 /* Look for the type-specifier. */
26771 cp_parser_type_specifier (parser,
26772 CP_PARSER_FLAGS_NONE,
26773 /*decl_specs=*/NULL,
26774 /*is_declarator=*/true,
26775 /*declares_class_or_enum=*/NULL,
26776 /*is_cv_qualifier=*/NULL);
26778 parser->num_template_parameter_lists
26779 = saved_num_template_parameter_lists;
26781 /* Leave the scope of the class. */
26782 if (pushed_scope)
26783 pop_scope (pushed_scope);
26785 constructor_p = !cp_parser_error_occurred (parser);
26789 /* We did not really want to consume any tokens. */
26790 cp_parser_abort_tentative_parse (parser);
26792 return constructor_p;
26795 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26796 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26797 they must be performed once we are in the scope of the function.
26799 Returns the function defined. */
26801 static tree
26802 cp_parser_function_definition_from_specifiers_and_declarator
26803 (cp_parser* parser,
26804 cp_decl_specifier_seq *decl_specifiers,
26805 tree attributes,
26806 const cp_declarator *declarator)
26808 tree fn;
26809 bool success_p;
26811 /* Begin the function-definition. */
26812 success_p = start_function (decl_specifiers, declarator, attributes);
26814 /* The things we're about to see are not directly qualified by any
26815 template headers we've seen thus far. */
26816 reset_specialization ();
26818 /* If there were names looked up in the decl-specifier-seq that we
26819 did not check, check them now. We must wait until we are in the
26820 scope of the function to perform the checks, since the function
26821 might be a friend. */
26822 perform_deferred_access_checks (tf_warning_or_error);
26824 if (success_p)
26826 cp_finalize_omp_declare_simd (parser, current_function_decl);
26827 parser->omp_declare_simd = NULL;
26828 cp_finalize_oacc_routine (parser, current_function_decl, true);
26829 parser->oacc_routine = NULL;
26832 if (!success_p)
26834 /* Skip the entire function. */
26835 cp_parser_skip_to_end_of_block_or_statement (parser);
26836 fn = error_mark_node;
26838 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26840 /* Seen already, skip it. An error message has already been output. */
26841 cp_parser_skip_to_end_of_block_or_statement (parser);
26842 fn = current_function_decl;
26843 current_function_decl = NULL_TREE;
26844 /* If this is a function from a class, pop the nested class. */
26845 if (current_class_name)
26846 pop_nested_class ();
26848 else
26850 timevar_id_t tv;
26851 if (DECL_DECLARED_INLINE_P (current_function_decl))
26852 tv = TV_PARSE_INLINE;
26853 else
26854 tv = TV_PARSE_FUNC;
26855 timevar_push (tv);
26856 fn = cp_parser_function_definition_after_declarator (parser,
26857 /*inline_p=*/false);
26858 timevar_pop (tv);
26861 return fn;
26864 /* Parse the part of a function-definition that follows the
26865 declarator. INLINE_P is TRUE iff this function is an inline
26866 function defined within a class-specifier.
26868 Returns the function defined. */
26870 static tree
26871 cp_parser_function_definition_after_declarator (cp_parser* parser,
26872 bool inline_p)
26874 tree fn;
26875 bool saved_in_unbraced_linkage_specification_p;
26876 bool saved_in_function_body;
26877 unsigned saved_num_template_parameter_lists;
26878 cp_token *token;
26879 bool fully_implicit_function_template_p
26880 = parser->fully_implicit_function_template_p;
26881 parser->fully_implicit_function_template_p = false;
26882 tree implicit_template_parms
26883 = parser->implicit_template_parms;
26884 parser->implicit_template_parms = 0;
26885 cp_binding_level* implicit_template_scope
26886 = parser->implicit_template_scope;
26887 parser->implicit_template_scope = 0;
26889 saved_in_function_body = parser->in_function_body;
26890 parser->in_function_body = true;
26891 /* If the next token is `return', then the code may be trying to
26892 make use of the "named return value" extension that G++ used to
26893 support. */
26894 token = cp_lexer_peek_token (parser->lexer);
26895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26897 /* Consume the `return' keyword. */
26898 cp_lexer_consume_token (parser->lexer);
26899 /* Look for the identifier that indicates what value is to be
26900 returned. */
26901 cp_parser_identifier (parser);
26902 /* Issue an error message. */
26903 error_at (token->location,
26904 "named return values are no longer supported");
26905 /* Skip tokens until we reach the start of the function body. */
26906 while (true)
26908 cp_token *token = cp_lexer_peek_token (parser->lexer);
26909 if (token->type == CPP_OPEN_BRACE
26910 || token->type == CPP_EOF
26911 || token->type == CPP_PRAGMA_EOL)
26912 break;
26913 cp_lexer_consume_token (parser->lexer);
26916 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26917 anything declared inside `f'. */
26918 saved_in_unbraced_linkage_specification_p
26919 = parser->in_unbraced_linkage_specification_p;
26920 parser->in_unbraced_linkage_specification_p = false;
26921 /* Inside the function, surrounding template-parameter-lists do not
26922 apply. */
26923 saved_num_template_parameter_lists
26924 = parser->num_template_parameter_lists;
26925 parser->num_template_parameter_lists = 0;
26927 /* If the next token is `try', `__transaction_atomic', or
26928 `__transaction_relaxed`, then we are looking at either function-try-block
26929 or function-transaction-block. Note that all of these include the
26930 function-body. */
26931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26932 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26933 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26934 RID_TRANSACTION_RELAXED))
26935 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26936 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26937 cp_parser_function_try_block (parser);
26938 else
26939 cp_parser_ctor_initializer_opt_and_function_body
26940 (parser, /*in_function_try_block=*/false);
26942 /* Finish the function. */
26943 fn = finish_function (inline_p);
26944 /* Generate code for it, if necessary. */
26945 expand_or_defer_fn (fn);
26946 /* Restore the saved values. */
26947 parser->in_unbraced_linkage_specification_p
26948 = saved_in_unbraced_linkage_specification_p;
26949 parser->num_template_parameter_lists
26950 = saved_num_template_parameter_lists;
26951 parser->in_function_body = saved_in_function_body;
26953 parser->fully_implicit_function_template_p
26954 = fully_implicit_function_template_p;
26955 parser->implicit_template_parms
26956 = implicit_template_parms;
26957 parser->implicit_template_scope
26958 = implicit_template_scope;
26960 if (parser->fully_implicit_function_template_p)
26961 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26963 return fn;
26966 /* Parse a template-declaration body (following argument list). */
26968 static void
26969 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26970 tree parameter_list,
26971 bool member_p)
26973 tree decl = NULL_TREE;
26974 bool friend_p = false;
26976 /* We just processed one more parameter list. */
26977 ++parser->num_template_parameter_lists;
26979 /* Get the deferred access checks from the parameter list. These
26980 will be checked once we know what is being declared, as for a
26981 member template the checks must be performed in the scope of the
26982 class containing the member. */
26983 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26985 /* Tentatively parse for a new template parameter list, which can either be
26986 the template keyword or a template introduction. */
26987 if (cp_parser_template_declaration_after_export (parser, member_p))
26988 /* OK */;
26989 else if (cxx_dialect >= cxx11
26990 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26991 decl = cp_parser_alias_declaration (parser);
26992 else
26994 /* There are no access checks when parsing a template, as we do not
26995 know if a specialization will be a friend. */
26996 push_deferring_access_checks (dk_no_check);
26997 cp_token *token = cp_lexer_peek_token (parser->lexer);
26998 decl = cp_parser_single_declaration (parser,
26999 checks,
27000 member_p,
27001 /*explicit_specialization_p=*/false,
27002 &friend_p);
27003 pop_deferring_access_checks ();
27005 /* If this is a member template declaration, let the front
27006 end know. */
27007 if (member_p && !friend_p && decl)
27009 if (TREE_CODE (decl) == TYPE_DECL)
27010 cp_parser_check_access_in_redeclaration (decl, token->location);
27012 decl = finish_member_template_decl (decl);
27014 else if (friend_p && decl
27015 && DECL_DECLARES_TYPE_P (decl))
27016 make_friend_class (current_class_type, TREE_TYPE (decl),
27017 /*complain=*/true);
27019 /* We are done with the current parameter list. */
27020 --parser->num_template_parameter_lists;
27022 pop_deferring_access_checks ();
27024 /* Finish up. */
27025 finish_template_decl (parameter_list);
27027 /* Check the template arguments for a literal operator template. */
27028 if (decl
27029 && DECL_DECLARES_FUNCTION_P (decl)
27030 && UDLIT_OPER_P (DECL_NAME (decl)))
27032 bool ok = true;
27033 if (parameter_list == NULL_TREE)
27034 ok = false;
27035 else
27037 int num_parms = TREE_VEC_LENGTH (parameter_list);
27038 if (num_parms == 1)
27040 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27041 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27042 if (TREE_TYPE (parm) != char_type_node
27043 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27044 ok = false;
27046 else if (num_parms == 2 && cxx_dialect >= cxx14)
27048 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27049 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27050 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27051 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27052 if (parm == error_mark_node
27053 || TREE_TYPE (parm) != TREE_TYPE (type)
27054 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27055 ok = false;
27057 else
27058 ok = false;
27060 if (!ok)
27062 if (cxx_dialect >= cxx14)
27063 error ("literal operator template %qD has invalid parameter list."
27064 " Expected non-type template argument pack <char...>"
27065 " or <typename CharT, CharT...>",
27066 decl);
27067 else
27068 error ("literal operator template %qD has invalid parameter list."
27069 " Expected non-type template argument pack <char...>",
27070 decl);
27074 /* Register member declarations. */
27075 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27076 finish_member_declaration (decl);
27077 /* If DECL is a function template, we must return to parse it later.
27078 (Even though there is no definition, there might be default
27079 arguments that need handling.) */
27080 if (member_p && decl
27081 && DECL_DECLARES_FUNCTION_P (decl))
27082 vec_safe_push (unparsed_funs_with_definitions, decl);
27085 /* Parse a template introduction header for a template-declaration. Returns
27086 false if tentative parse fails. */
27088 static bool
27089 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27091 cp_parser_parse_tentatively (parser);
27093 tree saved_scope = parser->scope;
27094 tree saved_object_scope = parser->object_scope;
27095 tree saved_qualifying_scope = parser->qualifying_scope;
27097 /* Look for the optional `::' operator. */
27098 cp_parser_global_scope_opt (parser,
27099 /*current_scope_valid_p=*/false);
27100 /* Look for the nested-name-specifier. */
27101 cp_parser_nested_name_specifier_opt (parser,
27102 /*typename_keyword_p=*/false,
27103 /*check_dependency_p=*/true,
27104 /*type_p=*/false,
27105 /*is_declaration=*/false);
27107 cp_token *token = cp_lexer_peek_token (parser->lexer);
27108 tree concept_name = cp_parser_identifier (parser);
27110 /* Look up the concept for which we will be matching
27111 template parameters. */
27112 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27113 token->location);
27114 parser->scope = saved_scope;
27115 parser->object_scope = saved_object_scope;
27116 parser->qualifying_scope = saved_qualifying_scope;
27118 if (concept_name == error_mark_node)
27119 cp_parser_simulate_error (parser);
27121 /* Look for opening brace for introduction. */
27122 matching_braces braces;
27123 braces.require_open (parser);
27125 if (!cp_parser_parse_definitely (parser))
27126 return false;
27128 push_deferring_access_checks (dk_deferred);
27130 /* Build vector of placeholder parameters and grab
27131 matching identifiers. */
27132 tree introduction_list = cp_parser_introduction_list (parser);
27134 /* The introduction-list shall not be empty. */
27135 int nargs = TREE_VEC_LENGTH (introduction_list);
27136 if (nargs == 0)
27138 error ("empty introduction-list");
27139 return true;
27142 /* Look for closing brace for introduction. */
27143 if (!braces.require_close (parser))
27144 return true;
27146 if (tmpl_decl == error_mark_node)
27148 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27149 token->location);
27150 return true;
27153 /* Build and associate the constraint. */
27154 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27155 if (parms && parms != error_mark_node)
27157 cp_parser_template_declaration_after_parameters (parser, parms,
27158 member_p);
27159 return true;
27162 error_at (token->location, "no matching concept for template-introduction");
27163 return true;
27166 /* Parse a normal template-declaration following the template keyword. */
27168 static void
27169 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27171 tree parameter_list;
27172 bool need_lang_pop;
27173 location_t location = input_location;
27175 /* Look for the `<' token. */
27176 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27177 return;
27178 if (at_class_scope_p () && current_function_decl)
27180 /* 14.5.2.2 [temp.mem]
27182 A local class shall not have member templates. */
27183 error_at (location,
27184 "invalid declaration of member template in local class");
27185 cp_parser_skip_to_end_of_block_or_statement (parser);
27186 return;
27188 /* [temp]
27190 A template ... shall not have C linkage. */
27191 if (current_lang_name == lang_name_c)
27193 error_at (location, "template with C linkage");
27194 maybe_show_extern_c_location ();
27195 /* Give it C++ linkage to avoid confusing other parts of the
27196 front end. */
27197 push_lang_context (lang_name_cplusplus);
27198 need_lang_pop = true;
27200 else
27201 need_lang_pop = false;
27203 /* We cannot perform access checks on the template parameter
27204 declarations until we know what is being declared, just as we
27205 cannot check the decl-specifier list. */
27206 push_deferring_access_checks (dk_deferred);
27208 /* If the next token is `>', then we have an invalid
27209 specialization. Rather than complain about an invalid template
27210 parameter, issue an error message here. */
27211 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27213 cp_parser_error (parser, "invalid explicit specialization");
27214 begin_specialization ();
27215 parameter_list = NULL_TREE;
27217 else
27219 /* Parse the template parameters. */
27220 parameter_list = cp_parser_template_parameter_list (parser);
27223 /* Look for the `>'. */
27224 cp_parser_skip_to_end_of_template_parameter_list (parser);
27226 /* Manage template requirements */
27227 if (flag_concepts)
27229 tree reqs = get_shorthand_constraints (current_template_parms);
27230 if (tree r = cp_parser_requires_clause_opt (parser))
27231 reqs = conjoin_constraints (reqs, normalize_expression (r));
27232 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27235 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27236 member_p);
27238 /* For the erroneous case of a template with C linkage, we pushed an
27239 implicit C++ linkage scope; exit that scope now. */
27240 if (need_lang_pop)
27241 pop_lang_context ();
27244 /* Parse a template-declaration, assuming that the `export' (and
27245 `extern') keywords, if present, has already been scanned. MEMBER_P
27246 is as for cp_parser_template_declaration. */
27248 static bool
27249 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27253 cp_lexer_consume_token (parser->lexer);
27254 cp_parser_explicit_template_declaration (parser, member_p);
27255 return true;
27257 else if (flag_concepts)
27258 return cp_parser_template_introduction (parser, member_p);
27260 return false;
27263 /* Perform the deferred access checks from a template-parameter-list.
27264 CHECKS is a TREE_LIST of access checks, as returned by
27265 get_deferred_access_checks. */
27267 static void
27268 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27270 ++processing_template_parmlist;
27271 perform_access_checks (checks, tf_warning_or_error);
27272 --processing_template_parmlist;
27275 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27276 `function-definition' sequence that follows a template header.
27277 If MEMBER_P is true, this declaration appears in a class scope.
27279 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27280 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27282 static tree
27283 cp_parser_single_declaration (cp_parser* parser,
27284 vec<deferred_access_check, va_gc> *checks,
27285 bool member_p,
27286 bool explicit_specialization_p,
27287 bool* friend_p)
27289 int declares_class_or_enum;
27290 tree decl = NULL_TREE;
27291 cp_decl_specifier_seq decl_specifiers;
27292 bool function_definition_p = false;
27293 cp_token *decl_spec_token_start;
27295 /* This function is only used when processing a template
27296 declaration. */
27297 gcc_assert (innermost_scope_kind () == sk_template_parms
27298 || innermost_scope_kind () == sk_template_spec);
27300 /* Defer access checks until we know what is being declared. */
27301 push_deferring_access_checks (dk_deferred);
27303 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27304 alternative. */
27305 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27306 cp_parser_decl_specifier_seq (parser,
27307 CP_PARSER_FLAGS_OPTIONAL,
27308 &decl_specifiers,
27309 &declares_class_or_enum);
27310 if (friend_p)
27311 *friend_p = cp_parser_friend_p (&decl_specifiers);
27313 /* There are no template typedefs. */
27314 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27316 error_at (decl_spec_token_start->location,
27317 "template declaration of %<typedef%>");
27318 decl = error_mark_node;
27321 /* Gather up the access checks that occurred the
27322 decl-specifier-seq. */
27323 stop_deferring_access_checks ();
27325 /* Check for the declaration of a template class. */
27326 if (declares_class_or_enum)
27328 if (cp_parser_declares_only_class_p (parser)
27329 || (declares_class_or_enum & 2))
27331 // If this is a declaration, but not a definition, associate
27332 // any constraints with the type declaration. Constraints
27333 // are associated with definitions in cp_parser_class_specifier.
27334 if (declares_class_or_enum == 1)
27335 associate_classtype_constraints (decl_specifiers.type);
27337 decl = shadow_tag (&decl_specifiers);
27339 /* In this case:
27341 struct C {
27342 friend template <typename T> struct A<T>::B;
27345 A<T>::B will be represented by a TYPENAME_TYPE, and
27346 therefore not recognized by shadow_tag. */
27347 if (friend_p && *friend_p
27348 && !decl
27349 && decl_specifiers.type
27350 && TYPE_P (decl_specifiers.type))
27351 decl = decl_specifiers.type;
27353 if (decl && decl != error_mark_node)
27354 decl = TYPE_NAME (decl);
27355 else
27356 decl = error_mark_node;
27358 /* Perform access checks for template parameters. */
27359 cp_parser_perform_template_parameter_access_checks (checks);
27361 /* Give a helpful diagnostic for
27362 template <class T> struct A { } a;
27363 if we aren't already recovering from an error. */
27364 if (!cp_parser_declares_only_class_p (parser)
27365 && !seen_error ())
27367 error_at (cp_lexer_peek_token (parser->lexer)->location,
27368 "a class template declaration must not declare "
27369 "anything else");
27370 cp_parser_skip_to_end_of_block_or_statement (parser);
27371 goto out;
27376 /* Complain about missing 'typename' or other invalid type names. */
27377 if (!decl_specifiers.any_type_specifiers_p
27378 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27380 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27381 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27382 the rest of this declaration. */
27383 decl = error_mark_node;
27384 goto out;
27387 /* If it's not a template class, try for a template function. If
27388 the next token is a `;', then this declaration does not declare
27389 anything. But, if there were errors in the decl-specifiers, then
27390 the error might well have come from an attempted class-specifier.
27391 In that case, there's no need to warn about a missing declarator. */
27392 if (!decl
27393 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27394 || decl_specifiers.type != error_mark_node))
27396 decl = cp_parser_init_declarator (parser,
27397 &decl_specifiers,
27398 checks,
27399 /*function_definition_allowed_p=*/true,
27400 member_p,
27401 declares_class_or_enum,
27402 &function_definition_p,
27403 NULL, NULL, NULL);
27405 /* 7.1.1-1 [dcl.stc]
27407 A storage-class-specifier shall not be specified in an explicit
27408 specialization... */
27409 if (decl
27410 && explicit_specialization_p
27411 && decl_specifiers.storage_class != sc_none)
27413 error_at (decl_spec_token_start->location,
27414 "explicit template specialization cannot have a storage class");
27415 decl = error_mark_node;
27418 if (decl && VAR_P (decl))
27419 check_template_variable (decl);
27422 /* Look for a trailing `;' after the declaration. */
27423 if (!function_definition_p
27424 && (decl == error_mark_node
27425 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27426 cp_parser_skip_to_end_of_block_or_statement (parser);
27428 out:
27429 pop_deferring_access_checks ();
27431 /* Clear any current qualification; whatever comes next is the start
27432 of something new. */
27433 parser->scope = NULL_TREE;
27434 parser->qualifying_scope = NULL_TREE;
27435 parser->object_scope = NULL_TREE;
27437 return decl;
27440 /* Parse a cast-expression that is not the operand of a unary "&". */
27442 static cp_expr
27443 cp_parser_simple_cast_expression (cp_parser *parser)
27445 return cp_parser_cast_expression (parser, /*address_p=*/false,
27446 /*cast_p=*/false, /*decltype*/false, NULL);
27449 /* Parse a functional cast to TYPE. Returns an expression
27450 representing the cast. */
27452 static cp_expr
27453 cp_parser_functional_cast (cp_parser* parser, tree type)
27455 vec<tree, va_gc> *vec;
27456 tree expression_list;
27457 cp_expr cast;
27458 bool nonconst_p;
27460 location_t start_loc = input_location;
27462 if (!type)
27463 type = error_mark_node;
27465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27467 cp_lexer_set_source_position (parser->lexer);
27468 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27469 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27470 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27471 if (TREE_CODE (type) == TYPE_DECL)
27472 type = TREE_TYPE (type);
27474 cast = finish_compound_literal (type, expression_list,
27475 tf_warning_or_error, fcl_functional);
27476 /* Create a location of the form:
27477 type_name{i, f}
27478 ^~~~~~~~~~~~~~~
27479 with caret == start at the start of the type name,
27480 finishing at the closing brace. */
27481 location_t finish_loc
27482 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27483 location_t combined_loc = make_location (start_loc, start_loc,
27484 finish_loc);
27485 cast.set_location (combined_loc);
27486 return cast;
27490 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27491 /*cast_p=*/true,
27492 /*allow_expansion_p=*/true,
27493 /*non_constant_p=*/NULL);
27494 if (vec == NULL)
27495 expression_list = error_mark_node;
27496 else
27498 expression_list = build_tree_list_vec (vec);
27499 release_tree_vector (vec);
27502 cast = build_functional_cast (type, expression_list,
27503 tf_warning_or_error);
27504 /* [expr.const]/1: In an integral constant expression "only type
27505 conversions to integral or enumeration type can be used". */
27506 if (TREE_CODE (type) == TYPE_DECL)
27507 type = TREE_TYPE (type);
27508 if (cast != error_mark_node
27509 && !cast_valid_in_integral_constant_expression_p (type)
27510 && cp_parser_non_integral_constant_expression (parser,
27511 NIC_CONSTRUCTOR))
27512 return error_mark_node;
27514 /* Create a location of the form:
27515 float(i)
27516 ^~~~~~~~
27517 with caret == start at the start of the type name,
27518 finishing at the closing paren. */
27519 location_t finish_loc
27520 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27521 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27522 cast.set_location (combined_loc);
27523 return cast;
27526 /* Save the tokens that make up the body of a member function defined
27527 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27528 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27529 specifiers applied to the declaration. Returns the FUNCTION_DECL
27530 for the member function. */
27532 static tree
27533 cp_parser_save_member_function_body (cp_parser* parser,
27534 cp_decl_specifier_seq *decl_specifiers,
27535 cp_declarator *declarator,
27536 tree attributes)
27538 cp_token *first;
27539 cp_token *last;
27540 tree fn;
27541 bool function_try_block = false;
27543 /* Create the FUNCTION_DECL. */
27544 fn = grokmethod (decl_specifiers, declarator, attributes);
27545 cp_finalize_omp_declare_simd (parser, fn);
27546 cp_finalize_oacc_routine (parser, fn, true);
27547 /* If something went badly wrong, bail out now. */
27548 if (fn == error_mark_node)
27550 /* If there's a function-body, skip it. */
27551 if (cp_parser_token_starts_function_definition_p
27552 (cp_lexer_peek_token (parser->lexer)))
27553 cp_parser_skip_to_end_of_block_or_statement (parser);
27554 return error_mark_node;
27557 /* Remember it, if there default args to post process. */
27558 cp_parser_save_default_args (parser, fn);
27560 /* Save away the tokens that make up the body of the
27561 function. */
27562 first = parser->lexer->next_token;
27564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27565 cp_lexer_consume_token (parser->lexer);
27566 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27567 RID_TRANSACTION_ATOMIC))
27569 cp_lexer_consume_token (parser->lexer);
27570 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27572 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27573 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27574 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27575 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27576 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27578 cp_lexer_consume_token (parser->lexer);
27579 cp_lexer_consume_token (parser->lexer);
27580 cp_lexer_consume_token (parser->lexer);
27581 cp_lexer_consume_token (parser->lexer);
27582 cp_lexer_consume_token (parser->lexer);
27584 else
27585 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27586 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27588 cp_lexer_consume_token (parser->lexer);
27589 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27590 break;
27594 /* Handle function try blocks. */
27595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27597 cp_lexer_consume_token (parser->lexer);
27598 function_try_block = true;
27600 /* We can have braced-init-list mem-initializers before the fn body. */
27601 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27603 cp_lexer_consume_token (parser->lexer);
27604 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27606 /* cache_group will stop after an un-nested { } pair, too. */
27607 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27608 break;
27610 /* variadic mem-inits have ... after the ')'. */
27611 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27612 cp_lexer_consume_token (parser->lexer);
27615 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27616 /* Handle function try blocks. */
27617 if (function_try_block)
27618 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27619 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27620 last = parser->lexer->next_token;
27622 /* Save away the inline definition; we will process it when the
27623 class is complete. */
27624 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27625 DECL_PENDING_INLINE_P (fn) = 1;
27627 /* We need to know that this was defined in the class, so that
27628 friend templates are handled correctly. */
27629 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27631 /* Add FN to the queue of functions to be parsed later. */
27632 vec_safe_push (unparsed_funs_with_definitions, fn);
27634 return fn;
27637 /* Save the tokens that make up the in-class initializer for a non-static
27638 data member. Returns a DEFAULT_ARG. */
27640 static tree
27641 cp_parser_save_nsdmi (cp_parser* parser)
27643 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27646 /* Parse a template-argument-list, as well as the trailing ">" (but
27647 not the opening "<"). See cp_parser_template_argument_list for the
27648 return value. */
27650 static tree
27651 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27653 tree arguments;
27654 tree saved_scope;
27655 tree saved_qualifying_scope;
27656 tree saved_object_scope;
27657 bool saved_greater_than_is_operator_p;
27658 int saved_unevaluated_operand;
27659 int saved_inhibit_evaluation_warnings;
27661 /* [temp.names]
27663 When parsing a template-id, the first non-nested `>' is taken as
27664 the end of the template-argument-list rather than a greater-than
27665 operator. */
27666 saved_greater_than_is_operator_p
27667 = parser->greater_than_is_operator_p;
27668 parser->greater_than_is_operator_p = false;
27669 /* Parsing the argument list may modify SCOPE, so we save it
27670 here. */
27671 saved_scope = parser->scope;
27672 saved_qualifying_scope = parser->qualifying_scope;
27673 saved_object_scope = parser->object_scope;
27674 /* We need to evaluate the template arguments, even though this
27675 template-id may be nested within a "sizeof". */
27676 saved_unevaluated_operand = cp_unevaluated_operand;
27677 cp_unevaluated_operand = 0;
27678 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27679 c_inhibit_evaluation_warnings = 0;
27680 /* Parse the template-argument-list itself. */
27681 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27682 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27683 arguments = NULL_TREE;
27684 else
27685 arguments = cp_parser_template_argument_list (parser);
27686 /* Look for the `>' that ends the template-argument-list. If we find
27687 a '>>' instead, it's probably just a typo. */
27688 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27690 if (cxx_dialect != cxx98)
27692 /* In C++0x, a `>>' in a template argument list or cast
27693 expression is considered to be two separate `>'
27694 tokens. So, change the current token to a `>', but don't
27695 consume it: it will be consumed later when the outer
27696 template argument list (or cast expression) is parsed.
27697 Note that this replacement of `>' for `>>' is necessary
27698 even if we are parsing tentatively: in the tentative
27699 case, after calling
27700 cp_parser_enclosed_template_argument_list we will always
27701 throw away all of the template arguments and the first
27702 closing `>', either because the template argument list
27703 was erroneous or because we are replacing those tokens
27704 with a CPP_TEMPLATE_ID token. The second `>' (which will
27705 not have been thrown away) is needed either to close an
27706 outer template argument list or to complete a new-style
27707 cast. */
27708 cp_token *token = cp_lexer_peek_token (parser->lexer);
27709 token->type = CPP_GREATER;
27711 else if (!saved_greater_than_is_operator_p)
27713 /* If we're in a nested template argument list, the '>>' has
27714 to be a typo for '> >'. We emit the error message, but we
27715 continue parsing and we push a '>' as next token, so that
27716 the argument list will be parsed correctly. Note that the
27717 global source location is still on the token before the
27718 '>>', so we need to say explicitly where we want it. */
27719 cp_token *token = cp_lexer_peek_token (parser->lexer);
27720 gcc_rich_location richloc (token->location);
27721 richloc.add_fixit_replace ("> >");
27722 error_at (&richloc, "%<>>%> should be %<> >%> "
27723 "within a nested template argument list");
27725 token->type = CPP_GREATER;
27727 else
27729 /* If this is not a nested template argument list, the '>>'
27730 is a typo for '>'. Emit an error message and continue.
27731 Same deal about the token location, but here we can get it
27732 right by consuming the '>>' before issuing the diagnostic. */
27733 cp_token *token = cp_lexer_consume_token (parser->lexer);
27734 error_at (token->location,
27735 "spurious %<>>%>, use %<>%> to terminate "
27736 "a template argument list");
27739 else
27740 cp_parser_skip_to_end_of_template_parameter_list (parser);
27741 /* The `>' token might be a greater-than operator again now. */
27742 parser->greater_than_is_operator_p
27743 = saved_greater_than_is_operator_p;
27744 /* Restore the SAVED_SCOPE. */
27745 parser->scope = saved_scope;
27746 parser->qualifying_scope = saved_qualifying_scope;
27747 parser->object_scope = saved_object_scope;
27748 cp_unevaluated_operand = saved_unevaluated_operand;
27749 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27751 return arguments;
27754 /* MEMBER_FUNCTION is a member function, or a friend. If default
27755 arguments, or the body of the function have not yet been parsed,
27756 parse them now. */
27758 static void
27759 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27761 timevar_push (TV_PARSE_INMETH);
27762 /* If this member is a template, get the underlying
27763 FUNCTION_DECL. */
27764 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27765 member_function = DECL_TEMPLATE_RESULT (member_function);
27767 /* There should not be any class definitions in progress at this
27768 point; the bodies of members are only parsed outside of all class
27769 definitions. */
27770 gcc_assert (parser->num_classes_being_defined == 0);
27771 /* While we're parsing the member functions we might encounter more
27772 classes. We want to handle them right away, but we don't want
27773 them getting mixed up with functions that are currently in the
27774 queue. */
27775 push_unparsed_function_queues (parser);
27777 /* Make sure that any template parameters are in scope. */
27778 maybe_begin_member_template_processing (member_function);
27780 /* If the body of the function has not yet been parsed, parse it
27781 now. */
27782 if (DECL_PENDING_INLINE_P (member_function))
27784 tree function_scope;
27785 cp_token_cache *tokens;
27787 /* The function is no longer pending; we are processing it. */
27788 tokens = DECL_PENDING_INLINE_INFO (member_function);
27789 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27790 DECL_PENDING_INLINE_P (member_function) = 0;
27792 /* If this is a local class, enter the scope of the containing
27793 function. */
27794 function_scope = current_function_decl;
27795 if (function_scope)
27796 push_function_context ();
27798 /* Push the body of the function onto the lexer stack. */
27799 cp_parser_push_lexer_for_tokens (parser, tokens);
27801 /* Let the front end know that we going to be defining this
27802 function. */
27803 start_preparsed_function (member_function, NULL_TREE,
27804 SF_PRE_PARSED | SF_INCLASS_INLINE);
27806 /* Don't do access checking if it is a templated function. */
27807 if (processing_template_decl)
27808 push_deferring_access_checks (dk_no_check);
27810 /* #pragma omp declare reduction needs special parsing. */
27811 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27813 parser->lexer->in_pragma = true;
27814 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27815 finish_function (/*inline_p=*/true);
27816 cp_check_omp_declare_reduction (member_function);
27818 else
27819 /* Now, parse the body of the function. */
27820 cp_parser_function_definition_after_declarator (parser,
27821 /*inline_p=*/true);
27823 if (processing_template_decl)
27824 pop_deferring_access_checks ();
27826 /* Leave the scope of the containing function. */
27827 if (function_scope)
27828 pop_function_context ();
27829 cp_parser_pop_lexer (parser);
27832 /* Remove any template parameters from the symbol table. */
27833 maybe_end_member_template_processing ();
27835 /* Restore the queue. */
27836 pop_unparsed_function_queues (parser);
27837 timevar_pop (TV_PARSE_INMETH);
27840 /* If DECL contains any default args, remember it on the unparsed
27841 functions queue. */
27843 static void
27844 cp_parser_save_default_args (cp_parser* parser, tree decl)
27846 tree probe;
27848 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27849 probe;
27850 probe = TREE_CHAIN (probe))
27851 if (TREE_PURPOSE (probe))
27853 cp_default_arg_entry entry = {current_class_type, decl};
27854 vec_safe_push (unparsed_funs_with_default_args, entry);
27855 break;
27859 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27860 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27861 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27862 from the parameter-type-list. */
27864 static tree
27865 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27866 tree default_arg, tree parmtype)
27868 cp_token_cache *tokens;
27869 tree parsed_arg;
27870 bool dummy;
27872 if (default_arg == error_mark_node)
27873 return error_mark_node;
27875 /* Push the saved tokens for the default argument onto the parser's
27876 lexer stack. */
27877 tokens = DEFARG_TOKENS (default_arg);
27878 cp_parser_push_lexer_for_tokens (parser, tokens);
27880 start_lambda_scope (decl);
27882 /* Parse the default argument. */
27883 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27884 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27885 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27887 finish_lambda_scope ();
27889 if (parsed_arg == error_mark_node)
27890 cp_parser_skip_to_end_of_statement (parser);
27892 if (!processing_template_decl)
27894 /* In a non-template class, check conversions now. In a template,
27895 we'll wait and instantiate these as needed. */
27896 if (TREE_CODE (decl) == PARM_DECL)
27897 parsed_arg = check_default_argument (parmtype, parsed_arg,
27898 tf_warning_or_error);
27899 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27900 parsed_arg = error_mark_node;
27901 else
27902 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27905 /* If the token stream has not been completely used up, then
27906 there was extra junk after the end of the default
27907 argument. */
27908 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27910 if (TREE_CODE (decl) == PARM_DECL)
27911 cp_parser_error (parser, "expected %<,%>");
27912 else
27913 cp_parser_error (parser, "expected %<;%>");
27916 /* Revert to the main lexer. */
27917 cp_parser_pop_lexer (parser);
27919 return parsed_arg;
27922 /* FIELD is a non-static data member with an initializer which we saved for
27923 later; parse it now. */
27925 static void
27926 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27928 tree def;
27930 maybe_begin_member_template_processing (field);
27932 push_unparsed_function_queues (parser);
27933 def = cp_parser_late_parse_one_default_arg (parser, field,
27934 DECL_INITIAL (field),
27935 NULL_TREE);
27936 pop_unparsed_function_queues (parser);
27938 maybe_end_member_template_processing ();
27940 DECL_INITIAL (field) = def;
27943 /* FN is a FUNCTION_DECL which may contains a parameter with an
27944 unparsed DEFAULT_ARG. Parse the default args now. This function
27945 assumes that the current scope is the scope in which the default
27946 argument should be processed. */
27948 static void
27949 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27951 bool saved_local_variables_forbidden_p;
27952 tree parm, parmdecl;
27954 /* While we're parsing the default args, we might (due to the
27955 statement expression extension) encounter more classes. We want
27956 to handle them right away, but we don't want them getting mixed
27957 up with default args that are currently in the queue. */
27958 push_unparsed_function_queues (parser);
27960 /* Local variable names (and the `this' keyword) may not appear
27961 in a default argument. */
27962 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27963 parser->local_variables_forbidden_p = true;
27965 push_defarg_context (fn);
27967 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27968 parmdecl = DECL_ARGUMENTS (fn);
27969 parm && parm != void_list_node;
27970 parm = TREE_CHAIN (parm),
27971 parmdecl = DECL_CHAIN (parmdecl))
27973 tree default_arg = TREE_PURPOSE (parm);
27974 tree parsed_arg;
27975 vec<tree, va_gc> *insts;
27976 tree copy;
27977 unsigned ix;
27979 if (!default_arg)
27980 continue;
27982 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27983 /* This can happen for a friend declaration for a function
27984 already declared with default arguments. */
27985 continue;
27987 parsed_arg
27988 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27989 default_arg,
27990 TREE_VALUE (parm));
27991 TREE_PURPOSE (parm) = parsed_arg;
27993 /* Update any instantiations we've already created. */
27994 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27995 vec_safe_iterate (insts, ix, &copy); ix++)
27996 TREE_PURPOSE (copy) = parsed_arg;
27999 pop_defarg_context ();
28001 /* Make sure no default arg is missing. */
28002 check_default_args (fn);
28004 /* Restore the state of local_variables_forbidden_p. */
28005 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28007 /* Restore the queue. */
28008 pop_unparsed_function_queues (parser);
28011 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28013 sizeof ... ( identifier )
28015 where the 'sizeof' token has already been consumed. */
28017 static tree
28018 cp_parser_sizeof_pack (cp_parser *parser)
28020 /* Consume the `...'. */
28021 cp_lexer_consume_token (parser->lexer);
28022 maybe_warn_variadic_templates ();
28024 matching_parens parens;
28025 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28026 if (paren)
28027 parens.consume_open (parser);
28028 else
28029 permerror (cp_lexer_peek_token (parser->lexer)->location,
28030 "%<sizeof...%> argument must be surrounded by parentheses");
28032 cp_token *token = cp_lexer_peek_token (parser->lexer);
28033 tree name = cp_parser_identifier (parser);
28034 if (name == error_mark_node)
28035 return error_mark_node;
28036 /* The name is not qualified. */
28037 parser->scope = NULL_TREE;
28038 parser->qualifying_scope = NULL_TREE;
28039 parser->object_scope = NULL_TREE;
28040 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28041 if (expr == error_mark_node)
28042 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28043 token->location);
28044 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28045 expr = TREE_TYPE (expr);
28046 else if (TREE_CODE (expr) == CONST_DECL)
28047 expr = DECL_INITIAL (expr);
28048 expr = make_pack_expansion (expr);
28049 PACK_EXPANSION_SIZEOF_P (expr) = true;
28051 if (paren)
28052 parens.require_close (parser);
28054 return expr;
28057 /* Parse the operand of `sizeof' (or a similar operator). Returns
28058 either a TYPE or an expression, depending on the form of the
28059 input. The KEYWORD indicates which kind of expression we have
28060 encountered. */
28062 static tree
28063 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28065 tree expr = NULL_TREE;
28066 const char *saved_message;
28067 char *tmp;
28068 bool saved_integral_constant_expression_p;
28069 bool saved_non_integral_constant_expression_p;
28071 /* If it's a `...', then we are computing the length of a parameter
28072 pack. */
28073 if (keyword == RID_SIZEOF
28074 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28075 return cp_parser_sizeof_pack (parser);
28077 /* Types cannot be defined in a `sizeof' expression. Save away the
28078 old message. */
28079 saved_message = parser->type_definition_forbidden_message;
28080 /* And create the new one. */
28081 tmp = concat ("types may not be defined in %<",
28082 IDENTIFIER_POINTER (ridpointers[keyword]),
28083 "%> expressions", NULL);
28084 parser->type_definition_forbidden_message = tmp;
28086 /* The restrictions on constant-expressions do not apply inside
28087 sizeof expressions. */
28088 saved_integral_constant_expression_p
28089 = parser->integral_constant_expression_p;
28090 saved_non_integral_constant_expression_p
28091 = parser->non_integral_constant_expression_p;
28092 parser->integral_constant_expression_p = false;
28094 /* Do not actually evaluate the expression. */
28095 ++cp_unevaluated_operand;
28096 ++c_inhibit_evaluation_warnings;
28097 /* If it's a `(', then we might be looking at the type-id
28098 construction. */
28099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28101 tree type = NULL_TREE;
28103 /* We can't be sure yet whether we're looking at a type-id or an
28104 expression. */
28105 cp_parser_parse_tentatively (parser);
28107 matching_parens parens;
28108 parens.consume_open (parser);
28110 /* Note: as a GNU Extension, compound literals are considered
28111 postfix-expressions as they are in C99, so they are valid
28112 arguments to sizeof. See comment in cp_parser_cast_expression
28113 for details. */
28114 if (cp_parser_compound_literal_p (parser))
28115 cp_parser_simulate_error (parser);
28116 else
28118 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28119 parser->in_type_id_in_expr_p = true;
28120 /* Look for the type-id. */
28121 type = cp_parser_type_id (parser);
28122 /* Look for the closing `)'. */
28123 parens.require_close (parser);
28124 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28127 /* If all went well, then we're done. */
28128 if (cp_parser_parse_definitely (parser))
28130 cp_decl_specifier_seq decl_specs;
28132 /* Build a trivial decl-specifier-seq. */
28133 clear_decl_specs (&decl_specs);
28134 decl_specs.type = type;
28136 /* Call grokdeclarator to figure out what type this is. */
28137 expr = grokdeclarator (NULL,
28138 &decl_specs,
28139 TYPENAME,
28140 /*initialized=*/0,
28141 /*attrlist=*/NULL);
28145 /* If the type-id production did not work out, then we must be
28146 looking at the unary-expression production. */
28147 if (!expr)
28148 expr = cp_parser_unary_expression (parser);
28150 /* Go back to evaluating expressions. */
28151 --cp_unevaluated_operand;
28152 --c_inhibit_evaluation_warnings;
28154 /* Free the message we created. */
28155 free (tmp);
28156 /* And restore the old one. */
28157 parser->type_definition_forbidden_message = saved_message;
28158 parser->integral_constant_expression_p
28159 = saved_integral_constant_expression_p;
28160 parser->non_integral_constant_expression_p
28161 = saved_non_integral_constant_expression_p;
28163 return expr;
28166 /* If the current declaration has no declarator, return true. */
28168 static bool
28169 cp_parser_declares_only_class_p (cp_parser *parser)
28171 /* If the next token is a `;' or a `,' then there is no
28172 declarator. */
28173 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28174 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28177 /* Update the DECL_SPECS to reflect the storage class indicated by
28178 KEYWORD. */
28180 static void
28181 cp_parser_set_storage_class (cp_parser *parser,
28182 cp_decl_specifier_seq *decl_specs,
28183 enum rid keyword,
28184 cp_token *token)
28186 cp_storage_class storage_class;
28188 if (parser->in_unbraced_linkage_specification_p)
28190 error_at (token->location, "invalid use of %qD in linkage specification",
28191 ridpointers[keyword]);
28192 return;
28194 else if (decl_specs->storage_class != sc_none)
28196 decl_specs->conflicting_specifiers_p = true;
28197 return;
28200 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28201 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28202 && decl_specs->gnu_thread_keyword_p)
28204 pedwarn (decl_specs->locations[ds_thread], 0,
28205 "%<__thread%> before %qD", ridpointers[keyword]);
28208 switch (keyword)
28210 case RID_AUTO:
28211 storage_class = sc_auto;
28212 break;
28213 case RID_REGISTER:
28214 storage_class = sc_register;
28215 break;
28216 case RID_STATIC:
28217 storage_class = sc_static;
28218 break;
28219 case RID_EXTERN:
28220 storage_class = sc_extern;
28221 break;
28222 case RID_MUTABLE:
28223 storage_class = sc_mutable;
28224 break;
28225 default:
28226 gcc_unreachable ();
28228 decl_specs->storage_class = storage_class;
28229 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28231 /* A storage class specifier cannot be applied alongside a typedef
28232 specifier. If there is a typedef specifier present then set
28233 conflicting_specifiers_p which will trigger an error later
28234 on in grokdeclarator. */
28235 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28236 decl_specs->conflicting_specifiers_p = true;
28239 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28240 is true, the type is a class or enum definition. */
28242 static void
28243 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28244 tree type_spec,
28245 cp_token *token,
28246 bool type_definition_p)
28248 decl_specs->any_specifiers_p = true;
28250 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28251 (with, for example, in "typedef int wchar_t;") we remember that
28252 this is what happened. In system headers, we ignore these
28253 declarations so that G++ can work with system headers that are not
28254 C++-safe. */
28255 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28256 && !type_definition_p
28257 && (type_spec == boolean_type_node
28258 || type_spec == char16_type_node
28259 || type_spec == char32_type_node
28260 || type_spec == wchar_type_node)
28261 && (decl_specs->type
28262 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28263 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28264 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28265 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28267 decl_specs->redefined_builtin_type = type_spec;
28268 set_and_check_decl_spec_loc (decl_specs,
28269 ds_redefined_builtin_type_spec,
28270 token);
28271 if (!decl_specs->type)
28273 decl_specs->type = type_spec;
28274 decl_specs->type_definition_p = false;
28275 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28278 else if (decl_specs->type)
28279 decl_specs->multiple_types_p = true;
28280 else
28282 decl_specs->type = type_spec;
28283 decl_specs->type_definition_p = type_definition_p;
28284 decl_specs->redefined_builtin_type = NULL_TREE;
28285 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28289 /* True iff TOKEN is the GNU keyword __thread. */
28291 static bool
28292 token_is__thread (cp_token *token)
28294 gcc_assert (token->keyword == RID_THREAD);
28295 return id_equal (token->u.value, "__thread");
28298 /* Set the location for a declarator specifier and check if it is
28299 duplicated.
28301 DECL_SPECS is the sequence of declarator specifiers onto which to
28302 set the location.
28304 DS is the single declarator specifier to set which location is to
28305 be set onto the existing sequence of declarators.
28307 LOCATION is the location for the declarator specifier to
28308 consider. */
28310 static void
28311 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28312 cp_decl_spec ds, cp_token *token)
28314 gcc_assert (ds < ds_last);
28316 if (decl_specs == NULL)
28317 return;
28319 source_location location = token->location;
28321 if (decl_specs->locations[ds] == 0)
28323 decl_specs->locations[ds] = location;
28324 if (ds == ds_thread)
28325 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28327 else
28329 if (ds == ds_long)
28331 if (decl_specs->locations[ds_long_long] != 0)
28332 error_at (location,
28333 "%<long long long%> is too long for GCC");
28334 else
28336 decl_specs->locations[ds_long_long] = location;
28337 pedwarn_cxx98 (location,
28338 OPT_Wlong_long,
28339 "ISO C++ 1998 does not support %<long long%>");
28342 else if (ds == ds_thread)
28344 bool gnu = token_is__thread (token);
28345 if (gnu != decl_specs->gnu_thread_keyword_p)
28346 error_at (location,
28347 "both %<__thread%> and %<thread_local%> specified");
28348 else
28350 gcc_rich_location richloc (location);
28351 richloc.add_fixit_remove ();
28352 error_at (&richloc, "duplicate %qD", token->u.value);
28355 else
28357 static const char *const decl_spec_names[] = {
28358 "signed",
28359 "unsigned",
28360 "short",
28361 "long",
28362 "const",
28363 "volatile",
28364 "restrict",
28365 "inline",
28366 "virtual",
28367 "explicit",
28368 "friend",
28369 "typedef",
28370 "using",
28371 "constexpr",
28372 "__complex"
28374 gcc_rich_location richloc (location);
28375 richloc.add_fixit_remove ();
28376 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28381 /* Return true iff the declarator specifier DS is present in the
28382 sequence of declarator specifiers DECL_SPECS. */
28384 bool
28385 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28386 cp_decl_spec ds)
28388 gcc_assert (ds < ds_last);
28390 if (decl_specs == NULL)
28391 return false;
28393 return decl_specs->locations[ds] != 0;
28396 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28397 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28399 static bool
28400 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28402 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28405 /* Issue an error message indicating that TOKEN_DESC was expected.
28406 If KEYWORD is true, it indicated this function is called by
28407 cp_parser_require_keword and the required token can only be
28408 a indicated keyword.
28410 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28411 within any error as the location of an "opening" token matching
28412 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28413 RT_CLOSE_PAREN). */
28415 static void
28416 cp_parser_required_error (cp_parser *parser,
28417 required_token token_desc,
28418 bool keyword,
28419 location_t matching_location)
28421 if (cp_parser_simulate_error (parser))
28422 return;
28424 const char *gmsgid = NULL;
28425 switch (token_desc)
28427 case RT_NEW:
28428 gmsgid = G_("expected %<new%>");
28429 break;
28430 case RT_DELETE:
28431 gmsgid = G_("expected %<delete%>");
28432 break;
28433 case RT_RETURN:
28434 gmsgid = G_("expected %<return%>");
28435 break;
28436 case RT_WHILE:
28437 gmsgid = G_("expected %<while%>");
28438 break;
28439 case RT_EXTERN:
28440 gmsgid = G_("expected %<extern%>");
28441 break;
28442 case RT_STATIC_ASSERT:
28443 gmsgid = G_("expected %<static_assert%>");
28444 break;
28445 case RT_DECLTYPE:
28446 gmsgid = G_("expected %<decltype%>");
28447 break;
28448 case RT_OPERATOR:
28449 gmsgid = G_("expected %<operator%>");
28450 break;
28451 case RT_CLASS:
28452 gmsgid = G_("expected %<class%>");
28453 break;
28454 case RT_TEMPLATE:
28455 gmsgid = G_("expected %<template%>");
28456 break;
28457 case RT_NAMESPACE:
28458 gmsgid = G_("expected %<namespace%>");
28459 break;
28460 case RT_USING:
28461 gmsgid = G_("expected %<using%>");
28462 break;
28463 case RT_ASM:
28464 gmsgid = G_("expected %<asm%>");
28465 break;
28466 case RT_TRY:
28467 gmsgid = G_("expected %<try%>");
28468 break;
28469 case RT_CATCH:
28470 gmsgid = G_("expected %<catch%>");
28471 break;
28472 case RT_THROW:
28473 gmsgid = G_("expected %<throw%>");
28474 break;
28475 case RT_LABEL:
28476 gmsgid = G_("expected %<__label__%>");
28477 break;
28478 case RT_AT_TRY:
28479 gmsgid = G_("expected %<@try%>");
28480 break;
28481 case RT_AT_SYNCHRONIZED:
28482 gmsgid = G_("expected %<@synchronized%>");
28483 break;
28484 case RT_AT_THROW:
28485 gmsgid = G_("expected %<@throw%>");
28486 break;
28487 case RT_TRANSACTION_ATOMIC:
28488 gmsgid = G_("expected %<__transaction_atomic%>");
28489 break;
28490 case RT_TRANSACTION_RELAXED:
28491 gmsgid = G_("expected %<__transaction_relaxed%>");
28492 break;
28493 default:
28494 break;
28497 if (!gmsgid && !keyword)
28499 switch (token_desc)
28501 case RT_SEMICOLON:
28502 gmsgid = G_("expected %<;%>");
28503 break;
28504 case RT_OPEN_PAREN:
28505 gmsgid = G_("expected %<(%>");
28506 break;
28507 case RT_CLOSE_BRACE:
28508 gmsgid = G_("expected %<}%>");
28509 break;
28510 case RT_OPEN_BRACE:
28511 gmsgid = G_("expected %<{%>");
28512 break;
28513 case RT_CLOSE_SQUARE:
28514 gmsgid = G_("expected %<]%>");
28515 break;
28516 case RT_OPEN_SQUARE:
28517 gmsgid = G_("expected %<[%>");
28518 break;
28519 case RT_COMMA:
28520 gmsgid = G_("expected %<,%>");
28521 break;
28522 case RT_SCOPE:
28523 gmsgid = G_("expected %<::%>");
28524 break;
28525 case RT_LESS:
28526 gmsgid = G_("expected %<<%>");
28527 break;
28528 case RT_GREATER:
28529 gmsgid = G_("expected %<>%>");
28530 break;
28531 case RT_EQ:
28532 gmsgid = G_("expected %<=%>");
28533 break;
28534 case RT_ELLIPSIS:
28535 gmsgid = G_("expected %<...%>");
28536 break;
28537 case RT_MULT:
28538 gmsgid = G_("expected %<*%>");
28539 break;
28540 case RT_COMPL:
28541 gmsgid = G_("expected %<~%>");
28542 break;
28543 case RT_COLON:
28544 gmsgid = G_("expected %<:%>");
28545 break;
28546 case RT_COLON_SCOPE:
28547 gmsgid = G_("expected %<:%> or %<::%>");
28548 break;
28549 case RT_CLOSE_PAREN:
28550 gmsgid = G_("expected %<)%>");
28551 break;
28552 case RT_COMMA_CLOSE_PAREN:
28553 gmsgid = G_("expected %<,%> or %<)%>");
28554 break;
28555 case RT_PRAGMA_EOL:
28556 gmsgid = G_("expected end of line");
28557 break;
28558 case RT_NAME:
28559 gmsgid = G_("expected identifier");
28560 break;
28561 case RT_SELECT:
28562 gmsgid = G_("expected selection-statement");
28563 break;
28564 case RT_ITERATION:
28565 gmsgid = G_("expected iteration-statement");
28566 break;
28567 case RT_JUMP:
28568 gmsgid = G_("expected jump-statement");
28569 break;
28570 case RT_CLASS_KEY:
28571 gmsgid = G_("expected class-key");
28572 break;
28573 case RT_CLASS_TYPENAME_TEMPLATE:
28574 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28575 break;
28576 default:
28577 gcc_unreachable ();
28581 if (gmsgid)
28582 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28586 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28587 issue an error message indicating that TOKEN_DESC was expected.
28589 Returns the token consumed, if the token had the appropriate type.
28590 Otherwise, returns NULL.
28592 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28593 within any error as the location of an "opening" token matching
28594 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28595 RT_CLOSE_PAREN). */
28597 static cp_token *
28598 cp_parser_require (cp_parser* parser,
28599 enum cpp_ttype type,
28600 required_token token_desc,
28601 location_t matching_location)
28603 if (cp_lexer_next_token_is (parser->lexer, type))
28604 return cp_lexer_consume_token (parser->lexer);
28605 else
28607 /* Output the MESSAGE -- unless we're parsing tentatively. */
28608 if (!cp_parser_simulate_error (parser))
28609 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28610 matching_location);
28611 return NULL;
28615 /* An error message is produced if the next token is not '>'.
28616 All further tokens are skipped until the desired token is
28617 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28619 static void
28620 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28622 /* Current level of '< ... >'. */
28623 unsigned level = 0;
28624 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28625 unsigned nesting_depth = 0;
28627 /* Are we ready, yet? If not, issue error message. */
28628 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28629 return;
28631 /* Skip tokens until the desired token is found. */
28632 while (true)
28634 /* Peek at the next token. */
28635 switch (cp_lexer_peek_token (parser->lexer)->type)
28637 case CPP_LESS:
28638 if (!nesting_depth)
28639 ++level;
28640 break;
28642 case CPP_RSHIFT:
28643 if (cxx_dialect == cxx98)
28644 /* C++0x views the `>>' operator as two `>' tokens, but
28645 C++98 does not. */
28646 break;
28647 else if (!nesting_depth && level-- == 0)
28649 /* We've hit a `>>' where the first `>' closes the
28650 template argument list, and the second `>' is
28651 spurious. Just consume the `>>' and stop; we've
28652 already produced at least one error. */
28653 cp_lexer_consume_token (parser->lexer);
28654 return;
28656 /* Fall through for C++0x, so we handle the second `>' in
28657 the `>>'. */
28658 gcc_fallthrough ();
28660 case CPP_GREATER:
28661 if (!nesting_depth && level-- == 0)
28663 /* We've reached the token we want, consume it and stop. */
28664 cp_lexer_consume_token (parser->lexer);
28665 return;
28667 break;
28669 case CPP_OPEN_PAREN:
28670 case CPP_OPEN_SQUARE:
28671 ++nesting_depth;
28672 break;
28674 case CPP_CLOSE_PAREN:
28675 case CPP_CLOSE_SQUARE:
28676 if (nesting_depth-- == 0)
28677 return;
28678 break;
28680 case CPP_EOF:
28681 case CPP_PRAGMA_EOL:
28682 case CPP_SEMICOLON:
28683 case CPP_OPEN_BRACE:
28684 case CPP_CLOSE_BRACE:
28685 /* The '>' was probably forgotten, don't look further. */
28686 return;
28688 default:
28689 break;
28692 /* Consume this token. */
28693 cp_lexer_consume_token (parser->lexer);
28697 /* If the next token is the indicated keyword, consume it. Otherwise,
28698 issue an error message indicating that TOKEN_DESC was expected.
28700 Returns the token consumed, if the token had the appropriate type.
28701 Otherwise, returns NULL. */
28703 static cp_token *
28704 cp_parser_require_keyword (cp_parser* parser,
28705 enum rid keyword,
28706 required_token token_desc)
28708 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28710 if (token && token->keyword != keyword)
28712 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28713 UNKNOWN_LOCATION);
28714 return NULL;
28717 return token;
28720 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28721 function-definition. */
28723 static bool
28724 cp_parser_token_starts_function_definition_p (cp_token* token)
28726 return (/* An ordinary function-body begins with an `{'. */
28727 token->type == CPP_OPEN_BRACE
28728 /* A ctor-initializer begins with a `:'. */
28729 || token->type == CPP_COLON
28730 /* A function-try-block begins with `try'. */
28731 || token->keyword == RID_TRY
28732 /* A function-transaction-block begins with `__transaction_atomic'
28733 or `__transaction_relaxed'. */
28734 || token->keyword == RID_TRANSACTION_ATOMIC
28735 || token->keyword == RID_TRANSACTION_RELAXED
28736 /* The named return value extension begins with `return'. */
28737 || token->keyword == RID_RETURN);
28740 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28741 definition. */
28743 static bool
28744 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28746 cp_token *token;
28748 token = cp_lexer_peek_token (parser->lexer);
28749 return (token->type == CPP_OPEN_BRACE
28750 || (token->type == CPP_COLON
28751 && !parser->colon_doesnt_start_class_def_p));
28754 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28755 C++0x) ending a template-argument. */
28757 static bool
28758 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28760 cp_token *token;
28762 token = cp_lexer_peek_token (parser->lexer);
28763 return (token->type == CPP_COMMA
28764 || token->type == CPP_GREATER
28765 || token->type == CPP_ELLIPSIS
28766 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28769 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28770 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28772 static bool
28773 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28774 size_t n)
28776 cp_token *token;
28778 token = cp_lexer_peek_nth_token (parser->lexer, n);
28779 if (token->type == CPP_LESS)
28780 return true;
28781 /* Check for the sequence `<::' in the original code. It would be lexed as
28782 `[:', where `[' is a digraph, and there is no whitespace before
28783 `:'. */
28784 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28786 cp_token *token2;
28787 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28788 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28789 return true;
28791 return false;
28794 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28795 or none_type otherwise. */
28797 static enum tag_types
28798 cp_parser_token_is_class_key (cp_token* token)
28800 switch (token->keyword)
28802 case RID_CLASS:
28803 return class_type;
28804 case RID_STRUCT:
28805 return record_type;
28806 case RID_UNION:
28807 return union_type;
28809 default:
28810 return none_type;
28814 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28815 or none_type otherwise or if the token is null. */
28817 static enum tag_types
28818 cp_parser_token_is_type_parameter_key (cp_token* token)
28820 if (!token)
28821 return none_type;
28823 switch (token->keyword)
28825 case RID_CLASS:
28826 return class_type;
28827 case RID_TYPENAME:
28828 return typename_type;
28830 default:
28831 return none_type;
28835 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28837 static void
28838 cp_parser_check_class_key (enum tag_types class_key, tree type)
28840 if (type == error_mark_node)
28841 return;
28842 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28844 if (permerror (input_location, "%qs tag used in naming %q#T",
28845 class_key == union_type ? "union"
28846 : class_key == record_type ? "struct" : "class",
28847 type))
28848 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28849 "%q#T was previously declared here", type);
28853 /* Issue an error message if DECL is redeclared with different
28854 access than its original declaration [class.access.spec/3].
28855 This applies to nested classes, nested class templates and
28856 enumerations [class.mem/1]. */
28858 static void
28859 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28861 if (!decl
28862 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28863 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28864 return;
28866 if ((TREE_PRIVATE (decl)
28867 != (current_access_specifier == access_private_node))
28868 || (TREE_PROTECTED (decl)
28869 != (current_access_specifier == access_protected_node)))
28870 error_at (location, "%qD redeclared with different access", decl);
28873 /* Look for the `template' keyword, as a syntactic disambiguator.
28874 Return TRUE iff it is present, in which case it will be
28875 consumed. */
28877 static bool
28878 cp_parser_optional_template_keyword (cp_parser *parser)
28880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28882 /* In C++98 the `template' keyword can only be used within templates;
28883 outside templates the parser can always figure out what is a
28884 template and what is not. In C++11, per the resolution of DR 468,
28885 `template' is allowed in cases where it is not strictly necessary. */
28886 if (!processing_template_decl
28887 && pedantic && cxx_dialect == cxx98)
28889 cp_token *token = cp_lexer_peek_token (parser->lexer);
28890 pedwarn (token->location, OPT_Wpedantic,
28891 "in C++98 %<template%> (as a disambiguator) is only "
28892 "allowed within templates");
28893 /* If this part of the token stream is rescanned, the same
28894 error message would be generated. So, we purge the token
28895 from the stream. */
28896 cp_lexer_purge_token (parser->lexer);
28897 return false;
28899 else
28901 /* Consume the `template' keyword. */
28902 cp_lexer_consume_token (parser->lexer);
28903 return true;
28906 return false;
28909 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28910 set PARSER->SCOPE, and perform other related actions. */
28912 static void
28913 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28915 struct tree_check *check_value;
28917 /* Get the stored value. */
28918 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28919 /* Set the scope from the stored value. */
28920 parser->scope = saved_checks_value (check_value);
28921 parser->qualifying_scope = check_value->qualifying_scope;
28922 parser->object_scope = NULL_TREE;
28925 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28926 encounter the end of a block before what we were looking for. */
28928 static bool
28929 cp_parser_cache_group (cp_parser *parser,
28930 enum cpp_ttype end,
28931 unsigned depth)
28933 while (true)
28935 cp_token *token = cp_lexer_peek_token (parser->lexer);
28937 /* Abort a parenthesized expression if we encounter a semicolon. */
28938 if ((end == CPP_CLOSE_PAREN || depth == 0)
28939 && token->type == CPP_SEMICOLON)
28940 return true;
28941 /* If we've reached the end of the file, stop. */
28942 if (token->type == CPP_EOF
28943 || (end != CPP_PRAGMA_EOL
28944 && token->type == CPP_PRAGMA_EOL))
28945 return true;
28946 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28947 /* We've hit the end of an enclosing block, so there's been some
28948 kind of syntax error. */
28949 return true;
28951 /* Consume the token. */
28952 cp_lexer_consume_token (parser->lexer);
28953 /* See if it starts a new group. */
28954 if (token->type == CPP_OPEN_BRACE)
28956 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28957 /* In theory this should probably check end == '}', but
28958 cp_parser_save_member_function_body needs it to exit
28959 after either '}' or ')' when called with ')'. */
28960 if (depth == 0)
28961 return false;
28963 else if (token->type == CPP_OPEN_PAREN)
28965 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28966 if (depth == 0 && end == CPP_CLOSE_PAREN)
28967 return false;
28969 else if (token->type == CPP_PRAGMA)
28970 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28971 else if (token->type == end)
28972 return false;
28976 /* Like above, for caching a default argument or NSDMI. Both of these are
28977 terminated by a non-nested comma, but it can be unclear whether or not a
28978 comma is nested in a template argument list unless we do more parsing.
28979 In order to handle this ambiguity, when we encounter a ',' after a '<'
28980 we try to parse what follows as a parameter-declaration-list (in the
28981 case of a default argument) or a member-declarator (in the case of an
28982 NSDMI). If that succeeds, then we stop caching. */
28984 static tree
28985 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28987 unsigned depth = 0;
28988 int maybe_template_id = 0;
28989 cp_token *first_token;
28990 cp_token *token;
28991 tree default_argument;
28993 /* Add tokens until we have processed the entire default
28994 argument. We add the range [first_token, token). */
28995 first_token = cp_lexer_peek_token (parser->lexer);
28996 if (first_token->type == CPP_OPEN_BRACE)
28998 /* For list-initialization, this is straightforward. */
28999 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29000 token = cp_lexer_peek_token (parser->lexer);
29002 else while (true)
29004 bool done = false;
29006 /* Peek at the next token. */
29007 token = cp_lexer_peek_token (parser->lexer);
29008 /* What we do depends on what token we have. */
29009 switch (token->type)
29011 /* In valid code, a default argument must be
29012 immediately followed by a `,' `)', or `...'. */
29013 case CPP_COMMA:
29014 if (depth == 0 && maybe_template_id)
29016 /* If we've seen a '<', we might be in a
29017 template-argument-list. Until Core issue 325 is
29018 resolved, we don't know how this situation ought
29019 to be handled, so try to DTRT. We check whether
29020 what comes after the comma is a valid parameter
29021 declaration list. If it is, then the comma ends
29022 the default argument; otherwise the default
29023 argument continues. */
29024 bool error = false;
29025 cp_token *peek;
29027 /* Set ITALP so cp_parser_parameter_declaration_list
29028 doesn't decide to commit to this parse. */
29029 bool saved_italp = parser->in_template_argument_list_p;
29030 parser->in_template_argument_list_p = true;
29032 cp_parser_parse_tentatively (parser);
29034 if (nsdmi)
29036 /* Parse declarators until we reach a non-comma or
29037 somthing that cannot be an initializer.
29038 Just checking whether we're looking at a single
29039 declarator is insufficient. Consider:
29040 int var = tuple<T,U>::x;
29041 The template parameter 'U' looks exactly like a
29042 declarator. */
29045 int ctor_dtor_or_conv_p;
29046 cp_lexer_consume_token (parser->lexer);
29047 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29048 &ctor_dtor_or_conv_p,
29049 /*parenthesized_p=*/NULL,
29050 /*member_p=*/true,
29051 /*friend_p=*/false);
29052 peek = cp_lexer_peek_token (parser->lexer);
29053 if (cp_parser_error_occurred (parser))
29054 break;
29056 while (peek->type == CPP_COMMA);
29057 /* If we met an '=' or ';' then the original comma
29058 was the end of the NSDMI. Otherwise assume
29059 we're still in the NSDMI. */
29060 error = (peek->type != CPP_EQ
29061 && peek->type != CPP_SEMICOLON);
29063 else
29065 cp_lexer_consume_token (parser->lexer);
29066 begin_scope (sk_function_parms, NULL_TREE);
29067 if (cp_parser_parameter_declaration_list (parser)
29068 == error_mark_node)
29069 error = true;
29070 pop_bindings_and_leave_scope ();
29072 if (!cp_parser_error_occurred (parser) && !error)
29073 done = true;
29074 cp_parser_abort_tentative_parse (parser);
29076 parser->in_template_argument_list_p = saved_italp;
29077 break;
29079 /* FALLTHRU */
29080 case CPP_CLOSE_PAREN:
29081 case CPP_ELLIPSIS:
29082 /* If we run into a non-nested `;', `}', or `]',
29083 then the code is invalid -- but the default
29084 argument is certainly over. */
29085 case CPP_SEMICOLON:
29086 case CPP_CLOSE_BRACE:
29087 case CPP_CLOSE_SQUARE:
29088 if (depth == 0
29089 /* Handle correctly int n = sizeof ... ( p ); */
29090 && token->type != CPP_ELLIPSIS)
29091 done = true;
29092 /* Update DEPTH, if necessary. */
29093 else if (token->type == CPP_CLOSE_PAREN
29094 || token->type == CPP_CLOSE_BRACE
29095 || token->type == CPP_CLOSE_SQUARE)
29096 --depth;
29097 break;
29099 case CPP_OPEN_PAREN:
29100 case CPP_OPEN_SQUARE:
29101 case CPP_OPEN_BRACE:
29102 ++depth;
29103 break;
29105 case CPP_LESS:
29106 if (depth == 0)
29107 /* This might be the comparison operator, or it might
29108 start a template argument list. */
29109 ++maybe_template_id;
29110 break;
29112 case CPP_RSHIFT:
29113 if (cxx_dialect == cxx98)
29114 break;
29115 /* Fall through for C++0x, which treats the `>>'
29116 operator like two `>' tokens in certain
29117 cases. */
29118 gcc_fallthrough ();
29120 case CPP_GREATER:
29121 if (depth == 0)
29123 /* This might be an operator, or it might close a
29124 template argument list. But if a previous '<'
29125 started a template argument list, this will have
29126 closed it, so we can't be in one anymore. */
29127 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29128 if (maybe_template_id < 0)
29129 maybe_template_id = 0;
29131 break;
29133 /* If we run out of tokens, issue an error message. */
29134 case CPP_EOF:
29135 case CPP_PRAGMA_EOL:
29136 error_at (token->location, "file ends in default argument");
29137 return error_mark_node;
29139 case CPP_NAME:
29140 case CPP_SCOPE:
29141 /* In these cases, we should look for template-ids.
29142 For example, if the default argument is
29143 `X<int, double>()', we need to do name lookup to
29144 figure out whether or not `X' is a template; if
29145 so, the `,' does not end the default argument.
29147 That is not yet done. */
29148 break;
29150 default:
29151 break;
29154 /* If we've reached the end, stop. */
29155 if (done)
29156 break;
29158 /* Add the token to the token block. */
29159 token = cp_lexer_consume_token (parser->lexer);
29162 /* Create a DEFAULT_ARG to represent the unparsed default
29163 argument. */
29164 default_argument = make_node (DEFAULT_ARG);
29165 DEFARG_TOKENS (default_argument)
29166 = cp_token_cache_new (first_token, token);
29167 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29169 return default_argument;
29172 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29174 location_t
29175 defarg_location (tree default_argument)
29177 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29178 location_t start = tokens->first->location;
29179 location_t end = tokens->last->location;
29180 return make_location (start, start, end);
29183 /* Begin parsing tentatively. We always save tokens while parsing
29184 tentatively so that if the tentative parsing fails we can restore the
29185 tokens. */
29187 static void
29188 cp_parser_parse_tentatively (cp_parser* parser)
29190 /* Enter a new parsing context. */
29191 parser->context = cp_parser_context_new (parser->context);
29192 /* Begin saving tokens. */
29193 cp_lexer_save_tokens (parser->lexer);
29194 /* In order to avoid repetitive access control error messages,
29195 access checks are queued up until we are no longer parsing
29196 tentatively. */
29197 push_deferring_access_checks (dk_deferred);
29200 /* Commit to the currently active tentative parse. */
29202 static void
29203 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29205 cp_parser_context *context;
29206 cp_lexer *lexer;
29208 /* Mark all of the levels as committed. */
29209 lexer = parser->lexer;
29210 for (context = parser->context; context->next; context = context->next)
29212 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29213 break;
29214 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29215 while (!cp_lexer_saving_tokens (lexer))
29216 lexer = lexer->next;
29217 cp_lexer_commit_tokens (lexer);
29221 /* Commit to the topmost currently active tentative parse.
29223 Note that this function shouldn't be called when there are
29224 irreversible side-effects while in a tentative state. For
29225 example, we shouldn't create a permanent entry in the symbol
29226 table, or issue an error message that might not apply if the
29227 tentative parse is aborted. */
29229 static void
29230 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29232 cp_parser_context *context = parser->context;
29233 cp_lexer *lexer = parser->lexer;
29235 if (context)
29237 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29238 return;
29239 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29241 while (!cp_lexer_saving_tokens (lexer))
29242 lexer = lexer->next;
29243 cp_lexer_commit_tokens (lexer);
29247 /* Abort the currently active tentative parse. All consumed tokens
29248 will be rolled back, and no diagnostics will be issued. */
29250 static void
29251 cp_parser_abort_tentative_parse (cp_parser* parser)
29253 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29254 || errorcount > 0);
29255 cp_parser_simulate_error (parser);
29256 /* Now, pretend that we want to see if the construct was
29257 successfully parsed. */
29258 cp_parser_parse_definitely (parser);
29261 /* Stop parsing tentatively. If a parse error has occurred, restore the
29262 token stream. Otherwise, commit to the tokens we have consumed.
29263 Returns true if no error occurred; false otherwise. */
29265 static bool
29266 cp_parser_parse_definitely (cp_parser* parser)
29268 bool error_occurred;
29269 cp_parser_context *context;
29271 /* Remember whether or not an error occurred, since we are about to
29272 destroy that information. */
29273 error_occurred = cp_parser_error_occurred (parser);
29274 /* Remove the topmost context from the stack. */
29275 context = parser->context;
29276 parser->context = context->next;
29277 /* If no parse errors occurred, commit to the tentative parse. */
29278 if (!error_occurred)
29280 /* Commit to the tokens read tentatively, unless that was
29281 already done. */
29282 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29283 cp_lexer_commit_tokens (parser->lexer);
29285 pop_to_parent_deferring_access_checks ();
29287 /* Otherwise, if errors occurred, roll back our state so that things
29288 are just as they were before we began the tentative parse. */
29289 else
29291 cp_lexer_rollback_tokens (parser->lexer);
29292 pop_deferring_access_checks ();
29294 /* Add the context to the front of the free list. */
29295 context->next = cp_parser_context_free_list;
29296 cp_parser_context_free_list = context;
29298 return !error_occurred;
29301 /* Returns true if we are parsing tentatively and are not committed to
29302 this tentative parse. */
29304 static bool
29305 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29307 return (cp_parser_parsing_tentatively (parser)
29308 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29311 /* Returns nonzero iff an error has occurred during the most recent
29312 tentative parse. */
29314 static bool
29315 cp_parser_error_occurred (cp_parser* parser)
29317 return (cp_parser_parsing_tentatively (parser)
29318 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29321 /* Returns nonzero if GNU extensions are allowed. */
29323 static bool
29324 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29326 return parser->allow_gnu_extensions_p;
29329 /* Objective-C++ Productions */
29332 /* Parse an Objective-C expression, which feeds into a primary-expression
29333 above.
29335 objc-expression:
29336 objc-message-expression
29337 objc-string-literal
29338 objc-encode-expression
29339 objc-protocol-expression
29340 objc-selector-expression
29342 Returns a tree representation of the expression. */
29344 static cp_expr
29345 cp_parser_objc_expression (cp_parser* parser)
29347 /* Try to figure out what kind of declaration is present. */
29348 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29350 switch (kwd->type)
29352 case CPP_OPEN_SQUARE:
29353 return cp_parser_objc_message_expression (parser);
29355 case CPP_OBJC_STRING:
29356 kwd = cp_lexer_consume_token (parser->lexer);
29357 return objc_build_string_object (kwd->u.value);
29359 case CPP_KEYWORD:
29360 switch (kwd->keyword)
29362 case RID_AT_ENCODE:
29363 return cp_parser_objc_encode_expression (parser);
29365 case RID_AT_PROTOCOL:
29366 return cp_parser_objc_protocol_expression (parser);
29368 case RID_AT_SELECTOR:
29369 return cp_parser_objc_selector_expression (parser);
29371 default:
29372 break;
29374 /* FALLTHRU */
29375 default:
29376 error_at (kwd->location,
29377 "misplaced %<@%D%> Objective-C++ construct",
29378 kwd->u.value);
29379 cp_parser_skip_to_end_of_block_or_statement (parser);
29382 return error_mark_node;
29385 /* Parse an Objective-C message expression.
29387 objc-message-expression:
29388 [ objc-message-receiver objc-message-args ]
29390 Returns a representation of an Objective-C message. */
29392 static tree
29393 cp_parser_objc_message_expression (cp_parser* parser)
29395 tree receiver, messageargs;
29397 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29398 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29399 receiver = cp_parser_objc_message_receiver (parser);
29400 messageargs = cp_parser_objc_message_args (parser);
29401 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29404 tree result = objc_build_message_expr (receiver, messageargs);
29406 /* Construct a location e.g.
29407 [self func1:5]
29408 ^~~~~~~~~~~~~~
29409 ranging from the '[' to the ']', with the caret at the start. */
29410 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29411 protected_set_expr_location (result, combined_loc);
29413 return result;
29416 /* Parse an objc-message-receiver.
29418 objc-message-receiver:
29419 expression
29420 simple-type-specifier
29422 Returns a representation of the type or expression. */
29424 static tree
29425 cp_parser_objc_message_receiver (cp_parser* parser)
29427 tree rcv;
29429 /* An Objective-C message receiver may be either (1) a type
29430 or (2) an expression. */
29431 cp_parser_parse_tentatively (parser);
29432 rcv = cp_parser_expression (parser);
29434 /* If that worked out, fine. */
29435 if (cp_parser_parse_definitely (parser))
29436 return rcv;
29438 cp_parser_parse_tentatively (parser);
29439 rcv = cp_parser_simple_type_specifier (parser,
29440 /*decl_specs=*/NULL,
29441 CP_PARSER_FLAGS_NONE);
29443 if (cp_parser_parse_definitely (parser))
29444 return objc_get_class_reference (rcv);
29446 cp_parser_error (parser, "objective-c++ message receiver expected");
29447 return error_mark_node;
29450 /* Parse the arguments and selectors comprising an Objective-C message.
29452 objc-message-args:
29453 objc-selector
29454 objc-selector-args
29455 objc-selector-args , objc-comma-args
29457 objc-selector-args:
29458 objc-selector [opt] : assignment-expression
29459 objc-selector-args objc-selector [opt] : assignment-expression
29461 objc-comma-args:
29462 assignment-expression
29463 objc-comma-args , assignment-expression
29465 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29466 selector arguments and TREE_VALUE containing a list of comma
29467 arguments. */
29469 static tree
29470 cp_parser_objc_message_args (cp_parser* parser)
29472 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29473 bool maybe_unary_selector_p = true;
29474 cp_token *token = cp_lexer_peek_token (parser->lexer);
29476 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29478 tree selector = NULL_TREE, arg;
29480 if (token->type != CPP_COLON)
29481 selector = cp_parser_objc_selector (parser);
29483 /* Detect if we have a unary selector. */
29484 if (maybe_unary_selector_p
29485 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29486 return build_tree_list (selector, NULL_TREE);
29488 maybe_unary_selector_p = false;
29489 cp_parser_require (parser, CPP_COLON, RT_COLON);
29490 arg = cp_parser_assignment_expression (parser);
29492 sel_args
29493 = chainon (sel_args,
29494 build_tree_list (selector, arg));
29496 token = cp_lexer_peek_token (parser->lexer);
29499 /* Handle non-selector arguments, if any. */
29500 while (token->type == CPP_COMMA)
29502 tree arg;
29504 cp_lexer_consume_token (parser->lexer);
29505 arg = cp_parser_assignment_expression (parser);
29507 addl_args
29508 = chainon (addl_args,
29509 build_tree_list (NULL_TREE, arg));
29511 token = cp_lexer_peek_token (parser->lexer);
29514 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29516 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29517 return build_tree_list (error_mark_node, error_mark_node);
29520 return build_tree_list (sel_args, addl_args);
29523 /* Parse an Objective-C encode expression.
29525 objc-encode-expression:
29526 @encode objc-typename
29528 Returns an encoded representation of the type argument. */
29530 static cp_expr
29531 cp_parser_objc_encode_expression (cp_parser* parser)
29533 tree type;
29534 cp_token *token;
29535 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29537 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29538 matching_parens parens;
29539 parens.require_open (parser);
29540 token = cp_lexer_peek_token (parser->lexer);
29541 type = complete_type (cp_parser_type_id (parser));
29542 parens.require_close (parser);
29544 if (!type)
29546 error_at (token->location,
29547 "%<@encode%> must specify a type as an argument");
29548 return error_mark_node;
29551 /* This happens if we find @encode(T) (where T is a template
29552 typename or something dependent on a template typename) when
29553 parsing a template. In that case, we can't compile it
29554 immediately, but we rather create an AT_ENCODE_EXPR which will
29555 need to be instantiated when the template is used.
29557 if (dependent_type_p (type))
29559 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29560 TREE_READONLY (value) = 1;
29561 return value;
29565 /* Build a location of the form:
29566 @encode(int)
29567 ^~~~~~~~~~~~
29568 with caret==start at the @ token, finishing at the close paren. */
29569 location_t combined_loc
29570 = make_location (start_loc, start_loc,
29571 cp_lexer_previous_token (parser->lexer)->location);
29573 return cp_expr (objc_build_encode_expr (type), combined_loc);
29576 /* Parse an Objective-C @defs expression. */
29578 static tree
29579 cp_parser_objc_defs_expression (cp_parser *parser)
29581 tree name;
29583 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29584 matching_parens parens;
29585 parens.require_open (parser);
29586 name = cp_parser_identifier (parser);
29587 parens.require_close (parser);
29589 return objc_get_class_ivars (name);
29592 /* Parse an Objective-C protocol expression.
29594 objc-protocol-expression:
29595 @protocol ( identifier )
29597 Returns a representation of the protocol expression. */
29599 static tree
29600 cp_parser_objc_protocol_expression (cp_parser* parser)
29602 tree proto;
29603 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29605 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29606 matching_parens parens;
29607 parens.require_open (parser);
29608 proto = cp_parser_identifier (parser);
29609 parens.require_close (parser);
29611 /* Build a location of the form:
29612 @protocol(prot)
29613 ^~~~~~~~~~~~~~~
29614 with caret==start at the @ token, finishing at the close paren. */
29615 location_t combined_loc
29616 = make_location (start_loc, start_loc,
29617 cp_lexer_previous_token (parser->lexer)->location);
29618 tree result = objc_build_protocol_expr (proto);
29619 protected_set_expr_location (result, combined_loc);
29620 return result;
29623 /* Parse an Objective-C selector expression.
29625 objc-selector-expression:
29626 @selector ( objc-method-signature )
29628 objc-method-signature:
29629 objc-selector
29630 objc-selector-seq
29632 objc-selector-seq:
29633 objc-selector :
29634 objc-selector-seq objc-selector :
29636 Returns a representation of the method selector. */
29638 static tree
29639 cp_parser_objc_selector_expression (cp_parser* parser)
29641 tree sel_seq = NULL_TREE;
29642 bool maybe_unary_selector_p = true;
29643 cp_token *token;
29644 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29646 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29647 matching_parens parens;
29648 parens.require_open (parser);
29649 token = cp_lexer_peek_token (parser->lexer);
29651 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29652 || token->type == CPP_SCOPE)
29654 tree selector = NULL_TREE;
29656 if (token->type != CPP_COLON
29657 || token->type == CPP_SCOPE)
29658 selector = cp_parser_objc_selector (parser);
29660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29661 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29663 /* Detect if we have a unary selector. */
29664 if (maybe_unary_selector_p)
29666 sel_seq = selector;
29667 goto finish_selector;
29669 else
29671 cp_parser_error (parser, "expected %<:%>");
29674 maybe_unary_selector_p = false;
29675 token = cp_lexer_consume_token (parser->lexer);
29677 if (token->type == CPP_SCOPE)
29679 sel_seq
29680 = chainon (sel_seq,
29681 build_tree_list (selector, NULL_TREE));
29682 sel_seq
29683 = chainon (sel_seq,
29684 build_tree_list (NULL_TREE, NULL_TREE));
29686 else
29687 sel_seq
29688 = chainon (sel_seq,
29689 build_tree_list (selector, NULL_TREE));
29691 token = cp_lexer_peek_token (parser->lexer);
29694 finish_selector:
29695 parens.require_close (parser);
29698 /* Build a location of the form:
29699 @selector(func)
29700 ^~~~~~~~~~~~~~~
29701 with caret==start at the @ token, finishing at the close paren. */
29702 location_t combined_loc
29703 = make_location (loc, loc,
29704 cp_lexer_previous_token (parser->lexer)->location);
29705 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29706 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29707 protected_set_expr_location (result, combined_loc);
29708 return result;
29711 /* Parse a list of identifiers.
29713 objc-identifier-list:
29714 identifier
29715 objc-identifier-list , identifier
29717 Returns a TREE_LIST of identifier nodes. */
29719 static tree
29720 cp_parser_objc_identifier_list (cp_parser* parser)
29722 tree identifier;
29723 tree list;
29724 cp_token *sep;
29726 identifier = cp_parser_identifier (parser);
29727 if (identifier == error_mark_node)
29728 return error_mark_node;
29730 list = build_tree_list (NULL_TREE, identifier);
29731 sep = cp_lexer_peek_token (parser->lexer);
29733 while (sep->type == CPP_COMMA)
29735 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29736 identifier = cp_parser_identifier (parser);
29737 if (identifier == error_mark_node)
29738 return list;
29740 list = chainon (list, build_tree_list (NULL_TREE,
29741 identifier));
29742 sep = cp_lexer_peek_token (parser->lexer);
29745 return list;
29748 /* Parse an Objective-C alias declaration.
29750 objc-alias-declaration:
29751 @compatibility_alias identifier identifier ;
29753 This function registers the alias mapping with the Objective-C front end.
29754 It returns nothing. */
29756 static void
29757 cp_parser_objc_alias_declaration (cp_parser* parser)
29759 tree alias, orig;
29761 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29762 alias = cp_parser_identifier (parser);
29763 orig = cp_parser_identifier (parser);
29764 objc_declare_alias (alias, orig);
29765 cp_parser_consume_semicolon_at_end_of_statement (parser);
29768 /* Parse an Objective-C class forward-declaration.
29770 objc-class-declaration:
29771 @class objc-identifier-list ;
29773 The function registers the forward declarations with the Objective-C
29774 front end. It returns nothing. */
29776 static void
29777 cp_parser_objc_class_declaration (cp_parser* parser)
29779 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29780 while (true)
29782 tree id;
29784 id = cp_parser_identifier (parser);
29785 if (id == error_mark_node)
29786 break;
29788 objc_declare_class (id);
29790 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29791 cp_lexer_consume_token (parser->lexer);
29792 else
29793 break;
29795 cp_parser_consume_semicolon_at_end_of_statement (parser);
29798 /* Parse a list of Objective-C protocol references.
29800 objc-protocol-refs-opt:
29801 objc-protocol-refs [opt]
29803 objc-protocol-refs:
29804 < objc-identifier-list >
29806 Returns a TREE_LIST of identifiers, if any. */
29808 static tree
29809 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29811 tree protorefs = NULL_TREE;
29813 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29815 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29816 protorefs = cp_parser_objc_identifier_list (parser);
29817 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29820 return protorefs;
29823 /* Parse a Objective-C visibility specification. */
29825 static void
29826 cp_parser_objc_visibility_spec (cp_parser* parser)
29828 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29830 switch (vis->keyword)
29832 case RID_AT_PRIVATE:
29833 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29834 break;
29835 case RID_AT_PROTECTED:
29836 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29837 break;
29838 case RID_AT_PUBLIC:
29839 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29840 break;
29841 case RID_AT_PACKAGE:
29842 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29843 break;
29844 default:
29845 return;
29848 /* Eat '@private'/'@protected'/'@public'. */
29849 cp_lexer_consume_token (parser->lexer);
29852 /* Parse an Objective-C method type. Return 'true' if it is a class
29853 (+) method, and 'false' if it is an instance (-) method. */
29855 static inline bool
29856 cp_parser_objc_method_type (cp_parser* parser)
29858 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29859 return true;
29860 else
29861 return false;
29864 /* Parse an Objective-C protocol qualifier. */
29866 static tree
29867 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29869 tree quals = NULL_TREE, node;
29870 cp_token *token = cp_lexer_peek_token (parser->lexer);
29872 node = token->u.value;
29874 while (node && identifier_p (node)
29875 && (node == ridpointers [(int) RID_IN]
29876 || node == ridpointers [(int) RID_OUT]
29877 || node == ridpointers [(int) RID_INOUT]
29878 || node == ridpointers [(int) RID_BYCOPY]
29879 || node == ridpointers [(int) RID_BYREF]
29880 || node == ridpointers [(int) RID_ONEWAY]))
29882 quals = tree_cons (NULL_TREE, node, quals);
29883 cp_lexer_consume_token (parser->lexer);
29884 token = cp_lexer_peek_token (parser->lexer);
29885 node = token->u.value;
29888 return quals;
29891 /* Parse an Objective-C typename. */
29893 static tree
29894 cp_parser_objc_typename (cp_parser* parser)
29896 tree type_name = NULL_TREE;
29898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29900 tree proto_quals, cp_type = NULL_TREE;
29902 matching_parens parens;
29903 parens.consume_open (parser); /* Eat '('. */
29904 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29906 /* An ObjC type name may consist of just protocol qualifiers, in which
29907 case the type shall default to 'id'. */
29908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29910 cp_type = cp_parser_type_id (parser);
29912 /* If the type could not be parsed, an error has already
29913 been produced. For error recovery, behave as if it had
29914 not been specified, which will use the default type
29915 'id'. */
29916 if (cp_type == error_mark_node)
29918 cp_type = NULL_TREE;
29919 /* We need to skip to the closing parenthesis as
29920 cp_parser_type_id() does not seem to do it for
29921 us. */
29922 cp_parser_skip_to_closing_parenthesis (parser,
29923 /*recovering=*/true,
29924 /*or_comma=*/false,
29925 /*consume_paren=*/false);
29929 parens.require_close (parser);
29930 type_name = build_tree_list (proto_quals, cp_type);
29933 return type_name;
29936 /* Check to see if TYPE refers to an Objective-C selector name. */
29938 static bool
29939 cp_parser_objc_selector_p (enum cpp_ttype type)
29941 return (type == CPP_NAME || type == CPP_KEYWORD
29942 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29943 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29944 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29945 || type == CPP_XOR || type == CPP_XOR_EQ);
29948 /* Parse an Objective-C selector. */
29950 static tree
29951 cp_parser_objc_selector (cp_parser* parser)
29953 cp_token *token = cp_lexer_consume_token (parser->lexer);
29955 if (!cp_parser_objc_selector_p (token->type))
29957 error_at (token->location, "invalid Objective-C++ selector name");
29958 return error_mark_node;
29961 /* C++ operator names are allowed to appear in ObjC selectors. */
29962 switch (token->type)
29964 case CPP_AND_AND: return get_identifier ("and");
29965 case CPP_AND_EQ: return get_identifier ("and_eq");
29966 case CPP_AND: return get_identifier ("bitand");
29967 case CPP_OR: return get_identifier ("bitor");
29968 case CPP_COMPL: return get_identifier ("compl");
29969 case CPP_NOT: return get_identifier ("not");
29970 case CPP_NOT_EQ: return get_identifier ("not_eq");
29971 case CPP_OR_OR: return get_identifier ("or");
29972 case CPP_OR_EQ: return get_identifier ("or_eq");
29973 case CPP_XOR: return get_identifier ("xor");
29974 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29975 default: return token->u.value;
29979 /* Parse an Objective-C params list. */
29981 static tree
29982 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29984 tree params = NULL_TREE;
29985 bool maybe_unary_selector_p = true;
29986 cp_token *token = cp_lexer_peek_token (parser->lexer);
29988 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29990 tree selector = NULL_TREE, type_name, identifier;
29991 tree parm_attr = NULL_TREE;
29993 if (token->keyword == RID_ATTRIBUTE)
29994 break;
29996 if (token->type != CPP_COLON)
29997 selector = cp_parser_objc_selector (parser);
29999 /* Detect if we have a unary selector. */
30000 if (maybe_unary_selector_p
30001 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30003 params = selector; /* Might be followed by attributes. */
30004 break;
30007 maybe_unary_selector_p = false;
30008 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30010 /* Something went quite wrong. There should be a colon
30011 here, but there is not. Stop parsing parameters. */
30012 break;
30014 type_name = cp_parser_objc_typename (parser);
30015 /* New ObjC allows attributes on parameters too. */
30016 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30017 parm_attr = cp_parser_attributes_opt (parser);
30018 identifier = cp_parser_identifier (parser);
30020 params
30021 = chainon (params,
30022 objc_build_keyword_decl (selector,
30023 type_name,
30024 identifier,
30025 parm_attr));
30027 token = cp_lexer_peek_token (parser->lexer);
30030 if (params == NULL_TREE)
30032 cp_parser_error (parser, "objective-c++ method declaration is expected");
30033 return error_mark_node;
30036 /* We allow tail attributes for the method. */
30037 if (token->keyword == RID_ATTRIBUTE)
30039 *attributes = cp_parser_attributes_opt (parser);
30040 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30041 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30042 return params;
30043 cp_parser_error (parser,
30044 "method attributes must be specified at the end");
30045 return error_mark_node;
30048 if (params == NULL_TREE)
30050 cp_parser_error (parser, "objective-c++ method declaration is expected");
30051 return error_mark_node;
30053 return params;
30056 /* Parse the non-keyword Objective-C params. */
30058 static tree
30059 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30060 tree* attributes)
30062 tree params = make_node (TREE_LIST);
30063 cp_token *token = cp_lexer_peek_token (parser->lexer);
30064 *ellipsisp = false; /* Initially, assume no ellipsis. */
30066 while (token->type == CPP_COMMA)
30068 cp_parameter_declarator *parmdecl;
30069 tree parm;
30071 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30072 token = cp_lexer_peek_token (parser->lexer);
30074 if (token->type == CPP_ELLIPSIS)
30076 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30077 *ellipsisp = true;
30078 token = cp_lexer_peek_token (parser->lexer);
30079 break;
30082 /* TODO: parse attributes for tail parameters. */
30083 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30084 parm = grokdeclarator (parmdecl->declarator,
30085 &parmdecl->decl_specifiers,
30086 PARM, /*initialized=*/0,
30087 /*attrlist=*/NULL);
30089 chainon (params, build_tree_list (NULL_TREE, parm));
30090 token = cp_lexer_peek_token (parser->lexer);
30093 /* We allow tail attributes for the method. */
30094 if (token->keyword == RID_ATTRIBUTE)
30096 if (*attributes == NULL_TREE)
30098 *attributes = cp_parser_attributes_opt (parser);
30099 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30100 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30101 return params;
30103 else
30104 /* We have an error, but parse the attributes, so that we can
30105 carry on. */
30106 *attributes = cp_parser_attributes_opt (parser);
30108 cp_parser_error (parser,
30109 "method attributes must be specified at the end");
30110 return error_mark_node;
30113 return params;
30116 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30118 static void
30119 cp_parser_objc_interstitial_code (cp_parser* parser)
30121 cp_token *token = cp_lexer_peek_token (parser->lexer);
30123 /* If the next token is `extern' and the following token is a string
30124 literal, then we have a linkage specification. */
30125 if (token->keyword == RID_EXTERN
30126 && cp_parser_is_pure_string_literal
30127 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30128 cp_parser_linkage_specification (parser);
30129 /* Handle #pragma, if any. */
30130 else if (token->type == CPP_PRAGMA)
30131 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30132 /* Allow stray semicolons. */
30133 else if (token->type == CPP_SEMICOLON)
30134 cp_lexer_consume_token (parser->lexer);
30135 /* Mark methods as optional or required, when building protocols. */
30136 else if (token->keyword == RID_AT_OPTIONAL)
30138 cp_lexer_consume_token (parser->lexer);
30139 objc_set_method_opt (true);
30141 else if (token->keyword == RID_AT_REQUIRED)
30143 cp_lexer_consume_token (parser->lexer);
30144 objc_set_method_opt (false);
30146 else if (token->keyword == RID_NAMESPACE)
30147 cp_parser_namespace_definition (parser);
30148 /* Other stray characters must generate errors. */
30149 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30151 cp_lexer_consume_token (parser->lexer);
30152 error ("stray %qs between Objective-C++ methods",
30153 token->type == CPP_OPEN_BRACE ? "{" : "}");
30155 /* Finally, try to parse a block-declaration, or a function-definition. */
30156 else
30157 cp_parser_block_declaration (parser, /*statement_p=*/false);
30160 /* Parse a method signature. */
30162 static tree
30163 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30165 tree rettype, kwdparms, optparms;
30166 bool ellipsis = false;
30167 bool is_class_method;
30169 is_class_method = cp_parser_objc_method_type (parser);
30170 rettype = cp_parser_objc_typename (parser);
30171 *attributes = NULL_TREE;
30172 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30173 if (kwdparms == error_mark_node)
30174 return error_mark_node;
30175 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30176 if (optparms == error_mark_node)
30177 return error_mark_node;
30179 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30182 static bool
30183 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30185 tree tattr;
30186 cp_lexer_save_tokens (parser->lexer);
30187 tattr = cp_parser_attributes_opt (parser);
30188 gcc_assert (tattr) ;
30190 /* If the attributes are followed by a method introducer, this is not allowed.
30191 Dump the attributes and flag the situation. */
30192 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30193 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30194 return true;
30196 /* Otherwise, the attributes introduce some interstitial code, possibly so
30197 rewind to allow that check. */
30198 cp_lexer_rollback_tokens (parser->lexer);
30199 return false;
30202 /* Parse an Objective-C method prototype list. */
30204 static void
30205 cp_parser_objc_method_prototype_list (cp_parser* parser)
30207 cp_token *token = cp_lexer_peek_token (parser->lexer);
30209 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30211 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30213 tree attributes, sig;
30214 bool is_class_method;
30215 if (token->type == CPP_PLUS)
30216 is_class_method = true;
30217 else
30218 is_class_method = false;
30219 sig = cp_parser_objc_method_signature (parser, &attributes);
30220 if (sig == error_mark_node)
30222 cp_parser_skip_to_end_of_block_or_statement (parser);
30223 token = cp_lexer_peek_token (parser->lexer);
30224 continue;
30226 objc_add_method_declaration (is_class_method, sig, attributes);
30227 cp_parser_consume_semicolon_at_end_of_statement (parser);
30229 else if (token->keyword == RID_AT_PROPERTY)
30230 cp_parser_objc_at_property_declaration (parser);
30231 else if (token->keyword == RID_ATTRIBUTE
30232 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30233 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30234 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_interface ();
30251 /* Parse an Objective-C method definition list. */
30253 static void
30254 cp_parser_objc_method_definition_list (cp_parser* parser)
30256 cp_token *token = cp_lexer_peek_token (parser->lexer);
30258 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30260 tree meth;
30262 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30264 cp_token *ptk;
30265 tree sig, attribute;
30266 bool is_class_method;
30267 if (token->type == CPP_PLUS)
30268 is_class_method = true;
30269 else
30270 is_class_method = false;
30271 push_deferring_access_checks (dk_deferred);
30272 sig = cp_parser_objc_method_signature (parser, &attribute);
30273 if (sig == error_mark_node)
30275 cp_parser_skip_to_end_of_block_or_statement (parser);
30276 token = cp_lexer_peek_token (parser->lexer);
30277 continue;
30279 objc_start_method_definition (is_class_method, sig, attribute,
30280 NULL_TREE);
30282 /* For historical reasons, we accept an optional semicolon. */
30283 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30284 cp_lexer_consume_token (parser->lexer);
30286 ptk = cp_lexer_peek_token (parser->lexer);
30287 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30288 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30290 perform_deferred_access_checks (tf_warning_or_error);
30291 stop_deferring_access_checks ();
30292 meth = cp_parser_function_definition_after_declarator (parser,
30293 false);
30294 pop_deferring_access_checks ();
30295 objc_finish_method_definition (meth);
30298 /* The following case will be removed once @synthesize is
30299 completely implemented. */
30300 else if (token->keyword == RID_AT_PROPERTY)
30301 cp_parser_objc_at_property_declaration (parser);
30302 else if (token->keyword == RID_AT_SYNTHESIZE)
30303 cp_parser_objc_at_synthesize_declaration (parser);
30304 else if (token->keyword == RID_AT_DYNAMIC)
30305 cp_parser_objc_at_dynamic_declaration (parser);
30306 else if (token->keyword == RID_ATTRIBUTE
30307 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30308 warning_at (token->location, OPT_Wattributes,
30309 "prefix attributes are ignored for methods");
30310 else
30311 /* Allow for interspersed non-ObjC++ code. */
30312 cp_parser_objc_interstitial_code (parser);
30314 token = cp_lexer_peek_token (parser->lexer);
30317 if (token->type != CPP_EOF)
30318 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30319 else
30320 cp_parser_error (parser, "expected %<@end%>");
30322 objc_finish_implementation ();
30325 /* Parse Objective-C ivars. */
30327 static void
30328 cp_parser_objc_class_ivars (cp_parser* parser)
30330 cp_token *token = cp_lexer_peek_token (parser->lexer);
30332 if (token->type != CPP_OPEN_BRACE)
30333 return; /* No ivars specified. */
30335 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30336 token = cp_lexer_peek_token (parser->lexer);
30338 while (token->type != CPP_CLOSE_BRACE
30339 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30341 cp_decl_specifier_seq declspecs;
30342 int decl_class_or_enum_p;
30343 tree prefix_attributes;
30345 cp_parser_objc_visibility_spec (parser);
30347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30348 break;
30350 cp_parser_decl_specifier_seq (parser,
30351 CP_PARSER_FLAGS_OPTIONAL,
30352 &declspecs,
30353 &decl_class_or_enum_p);
30355 /* auto, register, static, extern, mutable. */
30356 if (declspecs.storage_class != sc_none)
30358 cp_parser_error (parser, "invalid type for instance variable");
30359 declspecs.storage_class = sc_none;
30362 /* thread_local. */
30363 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30365 cp_parser_error (parser, "invalid type for instance variable");
30366 declspecs.locations[ds_thread] = 0;
30369 /* typedef. */
30370 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30372 cp_parser_error (parser, "invalid type for instance variable");
30373 declspecs.locations[ds_typedef] = 0;
30376 prefix_attributes = declspecs.attributes;
30377 declspecs.attributes = NULL_TREE;
30379 /* Keep going until we hit the `;' at the end of the
30380 declaration. */
30381 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30383 tree width = NULL_TREE, attributes, first_attribute, decl;
30384 cp_declarator *declarator = NULL;
30385 int ctor_dtor_or_conv_p;
30387 /* Check for a (possibly unnamed) bitfield declaration. */
30388 token = cp_lexer_peek_token (parser->lexer);
30389 if (token->type == CPP_COLON)
30390 goto eat_colon;
30392 if (token->type == CPP_NAME
30393 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30394 == CPP_COLON))
30396 /* Get the name of the bitfield. */
30397 declarator = make_id_declarator (NULL_TREE,
30398 cp_parser_identifier (parser),
30399 sfk_none);
30401 eat_colon:
30402 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30403 /* Get the width of the bitfield. */
30404 width
30405 = cp_parser_constant_expression (parser);
30407 else
30409 /* Parse the declarator. */
30410 declarator
30411 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30412 &ctor_dtor_or_conv_p,
30413 /*parenthesized_p=*/NULL,
30414 /*member_p=*/false,
30415 /*friend_p=*/false);
30418 /* Look for attributes that apply to the ivar. */
30419 attributes = cp_parser_attributes_opt (parser);
30420 /* Remember which attributes are prefix attributes and
30421 which are not. */
30422 first_attribute = attributes;
30423 /* Combine the attributes. */
30424 attributes = attr_chainon (prefix_attributes, attributes);
30426 if (width)
30427 /* Create the bitfield declaration. */
30428 decl = grokbitfield (declarator, &declspecs,
30429 width, NULL_TREE, attributes);
30430 else
30431 decl = grokfield (declarator, &declspecs,
30432 NULL_TREE, /*init_const_expr_p=*/false,
30433 NULL_TREE, attributes);
30435 /* Add the instance variable. */
30436 if (decl != error_mark_node && decl != NULL_TREE)
30437 objc_add_instance_variable (decl);
30439 /* Reset PREFIX_ATTRIBUTES. */
30440 if (attributes != error_mark_node)
30442 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30443 attributes = TREE_CHAIN (attributes);
30444 if (attributes)
30445 TREE_CHAIN (attributes) = NULL_TREE;
30448 token = cp_lexer_peek_token (parser->lexer);
30450 if (token->type == CPP_COMMA)
30452 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30453 continue;
30455 break;
30458 cp_parser_consume_semicolon_at_end_of_statement (parser);
30459 token = cp_lexer_peek_token (parser->lexer);
30462 if (token->keyword == RID_AT_END)
30463 cp_parser_error (parser, "expected %<}%>");
30465 /* Do not consume the RID_AT_END, so it will be read again as terminating
30466 the @interface of @implementation. */
30467 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30468 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30470 /* For historical reasons, we accept an optional semicolon. */
30471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30472 cp_lexer_consume_token (parser->lexer);
30475 /* Parse an Objective-C protocol declaration. */
30477 static void
30478 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30480 tree proto, protorefs;
30481 cp_token *tok;
30483 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30486 tok = cp_lexer_peek_token (parser->lexer);
30487 error_at (tok->location, "identifier expected after %<@protocol%>");
30488 cp_parser_consume_semicolon_at_end_of_statement (parser);
30489 return;
30492 /* See if we have a forward declaration or a definition. */
30493 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30495 /* Try a forward declaration first. */
30496 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30498 while (true)
30500 tree id;
30502 id = cp_parser_identifier (parser);
30503 if (id == error_mark_node)
30504 break;
30506 objc_declare_protocol (id, attributes);
30508 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30509 cp_lexer_consume_token (parser->lexer);
30510 else
30511 break;
30513 cp_parser_consume_semicolon_at_end_of_statement (parser);
30516 /* Ok, we got a full-fledged definition (or at least should). */
30517 else
30519 proto = cp_parser_identifier (parser);
30520 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30521 objc_start_protocol (proto, protorefs, attributes);
30522 cp_parser_objc_method_prototype_list (parser);
30526 /* Parse an Objective-C superclass or category. */
30528 static void
30529 cp_parser_objc_superclass_or_category (cp_parser *parser,
30530 bool iface_p,
30531 tree *super,
30532 tree *categ, bool *is_class_extension)
30534 cp_token *next = cp_lexer_peek_token (parser->lexer);
30536 *super = *categ = NULL_TREE;
30537 *is_class_extension = false;
30538 if (next->type == CPP_COLON)
30540 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30541 *super = cp_parser_identifier (parser);
30543 else if (next->type == CPP_OPEN_PAREN)
30545 matching_parens parens;
30546 parens.consume_open (parser); /* Eat '('. */
30548 /* If there is no category name, and this is an @interface, we
30549 have a class extension. */
30550 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30552 *categ = NULL_TREE;
30553 *is_class_extension = true;
30555 else
30556 *categ = cp_parser_identifier (parser);
30558 parens.require_close (parser);
30562 /* Parse an Objective-C class interface. */
30564 static void
30565 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30567 tree name, super, categ, protos;
30568 bool is_class_extension;
30570 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30571 name = cp_parser_identifier (parser);
30572 if (name == error_mark_node)
30574 /* It's hard to recover because even if valid @interface stuff
30575 is to follow, we can't compile it (or validate it) if we
30576 don't even know which class it refers to. Let's assume this
30577 was a stray '@interface' token in the stream and skip it.
30579 return;
30581 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30582 &is_class_extension);
30583 protos = cp_parser_objc_protocol_refs_opt (parser);
30585 /* We have either a class or a category on our hands. */
30586 if (categ || is_class_extension)
30587 objc_start_category_interface (name, categ, protos, attributes);
30588 else
30590 objc_start_class_interface (name, super, protos, attributes);
30591 /* Handle instance variable declarations, if any. */
30592 cp_parser_objc_class_ivars (parser);
30593 objc_continue_interface ();
30596 cp_parser_objc_method_prototype_list (parser);
30599 /* Parse an Objective-C class implementation. */
30601 static void
30602 cp_parser_objc_class_implementation (cp_parser* parser)
30604 tree name, super, categ;
30605 bool is_class_extension;
30607 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30608 name = cp_parser_identifier (parser);
30609 if (name == error_mark_node)
30611 /* It's hard to recover because even if valid @implementation
30612 stuff is to follow, we can't compile it (or validate it) if
30613 we don't even know which class it refers to. Let's assume
30614 this was a stray '@implementation' token in the stream and
30615 skip it.
30617 return;
30619 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30620 &is_class_extension);
30622 /* We have either a class or a category on our hands. */
30623 if (categ)
30624 objc_start_category_implementation (name, categ);
30625 else
30627 objc_start_class_implementation (name, super);
30628 /* Handle instance variable declarations, if any. */
30629 cp_parser_objc_class_ivars (parser);
30630 objc_continue_implementation ();
30633 cp_parser_objc_method_definition_list (parser);
30636 /* Consume the @end token and finish off the implementation. */
30638 static void
30639 cp_parser_objc_end_implementation (cp_parser* parser)
30641 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30642 objc_finish_implementation ();
30645 /* Parse an Objective-C declaration. */
30647 static void
30648 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30650 /* Try to figure out what kind of declaration is present. */
30651 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30653 if (attributes)
30654 switch (kwd->keyword)
30656 case RID_AT_ALIAS:
30657 case RID_AT_CLASS:
30658 case RID_AT_END:
30659 error_at (kwd->location, "attributes may not be specified before"
30660 " the %<@%D%> Objective-C++ keyword",
30661 kwd->u.value);
30662 attributes = NULL;
30663 break;
30664 case RID_AT_IMPLEMENTATION:
30665 warning_at (kwd->location, OPT_Wattributes,
30666 "prefix attributes are ignored before %<@%D%>",
30667 kwd->u.value);
30668 attributes = NULL;
30669 default:
30670 break;
30673 switch (kwd->keyword)
30675 case RID_AT_ALIAS:
30676 cp_parser_objc_alias_declaration (parser);
30677 break;
30678 case RID_AT_CLASS:
30679 cp_parser_objc_class_declaration (parser);
30680 break;
30681 case RID_AT_PROTOCOL:
30682 cp_parser_objc_protocol_declaration (parser, attributes);
30683 break;
30684 case RID_AT_INTERFACE:
30685 cp_parser_objc_class_interface (parser, attributes);
30686 break;
30687 case RID_AT_IMPLEMENTATION:
30688 cp_parser_objc_class_implementation (parser);
30689 break;
30690 case RID_AT_END:
30691 cp_parser_objc_end_implementation (parser);
30692 break;
30693 default:
30694 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30695 kwd->u.value);
30696 cp_parser_skip_to_end_of_block_or_statement (parser);
30700 /* Parse an Objective-C try-catch-finally statement.
30702 objc-try-catch-finally-stmt:
30703 @try compound-statement objc-catch-clause-seq [opt]
30704 objc-finally-clause [opt]
30706 objc-catch-clause-seq:
30707 objc-catch-clause objc-catch-clause-seq [opt]
30709 objc-catch-clause:
30710 @catch ( objc-exception-declaration ) compound-statement
30712 objc-finally-clause:
30713 @finally compound-statement
30715 objc-exception-declaration:
30716 parameter-declaration
30717 '...'
30719 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30721 Returns NULL_TREE.
30723 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30724 for C. Keep them in sync. */
30726 static tree
30727 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30729 location_t location;
30730 tree stmt;
30732 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30733 location = cp_lexer_peek_token (parser->lexer)->location;
30734 objc_maybe_warn_exceptions (location);
30735 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30736 node, lest it get absorbed into the surrounding block. */
30737 stmt = push_stmt_list ();
30738 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30739 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30741 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30743 cp_parameter_declarator *parm;
30744 tree parameter_declaration = error_mark_node;
30745 bool seen_open_paren = false;
30746 matching_parens parens;
30748 cp_lexer_consume_token (parser->lexer);
30749 if (parens.require_open (parser))
30750 seen_open_paren = true;
30751 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30753 /* We have "@catch (...)" (where the '...' are literally
30754 what is in the code). Skip the '...'.
30755 parameter_declaration is set to NULL_TREE, and
30756 objc_being_catch_clauses() knows that that means
30757 '...'. */
30758 cp_lexer_consume_token (parser->lexer);
30759 parameter_declaration = NULL_TREE;
30761 else
30763 /* We have "@catch (NSException *exception)" or something
30764 like that. Parse the parameter declaration. */
30765 parm = cp_parser_parameter_declaration (parser, false, NULL);
30766 if (parm == NULL)
30767 parameter_declaration = error_mark_node;
30768 else
30769 parameter_declaration = grokdeclarator (parm->declarator,
30770 &parm->decl_specifiers,
30771 PARM, /*initialized=*/0,
30772 /*attrlist=*/NULL);
30774 if (seen_open_paren)
30775 parens.require_close (parser);
30776 else
30778 /* If there was no open parenthesis, we are recovering from
30779 an error, and we are trying to figure out what mistake
30780 the user has made. */
30782 /* If there is an immediate closing parenthesis, the user
30783 probably forgot the opening one (ie, they typed "@catch
30784 NSException *e)". Parse the closing parenthesis and keep
30785 going. */
30786 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30787 cp_lexer_consume_token (parser->lexer);
30789 /* If these is no immediate closing parenthesis, the user
30790 probably doesn't know that parenthesis are required at
30791 all (ie, they typed "@catch NSException *e"). So, just
30792 forget about the closing parenthesis and keep going. */
30794 objc_begin_catch_clause (parameter_declaration);
30795 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30796 objc_finish_catch_clause ();
30798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30800 cp_lexer_consume_token (parser->lexer);
30801 location = cp_lexer_peek_token (parser->lexer)->location;
30802 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30803 node, lest it get absorbed into the surrounding block. */
30804 stmt = push_stmt_list ();
30805 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30806 objc_build_finally_clause (location, pop_stmt_list (stmt));
30809 return objc_finish_try_stmt ();
30812 /* Parse an Objective-C synchronized statement.
30814 objc-synchronized-stmt:
30815 @synchronized ( expression ) compound-statement
30817 Returns NULL_TREE. */
30819 static tree
30820 cp_parser_objc_synchronized_statement (cp_parser *parser)
30822 location_t location;
30823 tree lock, stmt;
30825 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30827 location = cp_lexer_peek_token (parser->lexer)->location;
30828 objc_maybe_warn_exceptions (location);
30829 matching_parens parens;
30830 parens.require_open (parser);
30831 lock = cp_parser_expression (parser);
30832 parens.require_close (parser);
30834 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30835 node, lest it get absorbed into the surrounding block. */
30836 stmt = push_stmt_list ();
30837 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30839 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30842 /* Parse an Objective-C throw statement.
30844 objc-throw-stmt:
30845 @throw assignment-expression [opt] ;
30847 Returns a constructed '@throw' statement. */
30849 static tree
30850 cp_parser_objc_throw_statement (cp_parser *parser)
30852 tree expr = NULL_TREE;
30853 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30855 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30858 expr = cp_parser_expression (parser);
30860 cp_parser_consume_semicolon_at_end_of_statement (parser);
30862 return objc_build_throw_stmt (loc, expr);
30865 /* Parse an Objective-C statement. */
30867 static tree
30868 cp_parser_objc_statement (cp_parser * parser)
30870 /* Try to figure out what kind of declaration is present. */
30871 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30873 switch (kwd->keyword)
30875 case RID_AT_TRY:
30876 return cp_parser_objc_try_catch_finally_statement (parser);
30877 case RID_AT_SYNCHRONIZED:
30878 return cp_parser_objc_synchronized_statement (parser);
30879 case RID_AT_THROW:
30880 return cp_parser_objc_throw_statement (parser);
30881 default:
30882 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30883 kwd->u.value);
30884 cp_parser_skip_to_end_of_block_or_statement (parser);
30887 return error_mark_node;
30890 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30891 look ahead to see if an objc keyword follows the attributes. This
30892 is to detect the use of prefix attributes on ObjC @interface and
30893 @protocol. */
30895 static bool
30896 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30898 cp_lexer_save_tokens (parser->lexer);
30899 *attrib = cp_parser_attributes_opt (parser);
30900 gcc_assert (*attrib);
30901 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30903 cp_lexer_commit_tokens (parser->lexer);
30904 return true;
30906 cp_lexer_rollback_tokens (parser->lexer);
30907 return false;
30910 /* This routine is a minimal replacement for
30911 c_parser_struct_declaration () used when parsing the list of
30912 types/names or ObjC++ properties. For example, when parsing the
30913 code
30915 @property (readonly) int a, b, c;
30917 this function is responsible for parsing "int a, int b, int c" and
30918 returning the declarations as CHAIN of DECLs.
30920 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30921 similar parsing. */
30922 static tree
30923 cp_parser_objc_struct_declaration (cp_parser *parser)
30925 tree decls = NULL_TREE;
30926 cp_decl_specifier_seq declspecs;
30927 int decl_class_or_enum_p;
30928 tree prefix_attributes;
30930 cp_parser_decl_specifier_seq (parser,
30931 CP_PARSER_FLAGS_NONE,
30932 &declspecs,
30933 &decl_class_or_enum_p);
30935 if (declspecs.type == error_mark_node)
30936 return error_mark_node;
30938 /* auto, register, static, extern, mutable. */
30939 if (declspecs.storage_class != sc_none)
30941 cp_parser_error (parser, "invalid type for property");
30942 declspecs.storage_class = sc_none;
30945 /* thread_local. */
30946 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30948 cp_parser_error (parser, "invalid type for property");
30949 declspecs.locations[ds_thread] = 0;
30952 /* typedef. */
30953 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30955 cp_parser_error (parser, "invalid type for property");
30956 declspecs.locations[ds_typedef] = 0;
30959 prefix_attributes = declspecs.attributes;
30960 declspecs.attributes = NULL_TREE;
30962 /* Keep going until we hit the `;' at the end of the declaration. */
30963 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30965 tree attributes, first_attribute, decl;
30966 cp_declarator *declarator;
30967 cp_token *token;
30969 /* Parse the declarator. */
30970 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30971 NULL, NULL, false, false);
30973 /* Look for attributes that apply to the ivar. */
30974 attributes = cp_parser_attributes_opt (parser);
30975 /* Remember which attributes are prefix attributes and
30976 which are not. */
30977 first_attribute = attributes;
30978 /* Combine the attributes. */
30979 attributes = attr_chainon (prefix_attributes, attributes);
30981 decl = grokfield (declarator, &declspecs,
30982 NULL_TREE, /*init_const_expr_p=*/false,
30983 NULL_TREE, attributes);
30985 if (decl == error_mark_node || decl == NULL_TREE)
30986 return error_mark_node;
30988 /* Reset PREFIX_ATTRIBUTES. */
30989 if (attributes != error_mark_node)
30991 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30992 attributes = TREE_CHAIN (attributes);
30993 if (attributes)
30994 TREE_CHAIN (attributes) = NULL_TREE;
30997 DECL_CHAIN (decl) = decls;
30998 decls = decl;
31000 token = cp_lexer_peek_token (parser->lexer);
31001 if (token->type == CPP_COMMA)
31003 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31004 continue;
31006 else
31007 break;
31009 return decls;
31012 /* Parse an Objective-C @property declaration. The syntax is:
31014 objc-property-declaration:
31015 '@property' objc-property-attributes[opt] struct-declaration ;
31017 objc-property-attributes:
31018 '(' objc-property-attribute-list ')'
31020 objc-property-attribute-list:
31021 objc-property-attribute
31022 objc-property-attribute-list, objc-property-attribute
31024 objc-property-attribute
31025 'getter' = identifier
31026 'setter' = identifier
31027 'readonly'
31028 'readwrite'
31029 'assign'
31030 'retain'
31031 'copy'
31032 'nonatomic'
31034 For example:
31035 @property NSString *name;
31036 @property (readonly) id object;
31037 @property (retain, nonatomic, getter=getTheName) id name;
31038 @property int a, b, c;
31040 PS: This function is identical to
31041 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31042 static void
31043 cp_parser_objc_at_property_declaration (cp_parser *parser)
31045 /* The following variables hold the attributes of the properties as
31046 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31047 seen. When we see an attribute, we set them to 'true' (if they
31048 are boolean properties) or to the identifier (if they have an
31049 argument, ie, for getter and setter). Note that here we only
31050 parse the list of attributes, check the syntax and accumulate the
31051 attributes that we find. objc_add_property_declaration() will
31052 then process the information. */
31053 bool property_assign = false;
31054 bool property_copy = false;
31055 tree property_getter_ident = NULL_TREE;
31056 bool property_nonatomic = false;
31057 bool property_readonly = false;
31058 bool property_readwrite = false;
31059 bool property_retain = false;
31060 tree property_setter_ident = NULL_TREE;
31062 /* 'properties' is the list of properties that we read. Usually a
31063 single one, but maybe more (eg, in "@property int a, b, c;" there
31064 are three). */
31065 tree properties;
31066 location_t loc;
31068 loc = cp_lexer_peek_token (parser->lexer)->location;
31070 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31072 /* Parse the optional attribute list... */
31073 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31075 /* Eat the '('. */
31076 matching_parens parens;
31077 parens.consume_open (parser);
31079 while (true)
31081 bool syntax_error = false;
31082 cp_token *token = cp_lexer_peek_token (parser->lexer);
31083 enum rid keyword;
31085 if (token->type != CPP_NAME)
31087 cp_parser_error (parser, "expected identifier");
31088 break;
31090 keyword = C_RID_CODE (token->u.value);
31091 cp_lexer_consume_token (parser->lexer);
31092 switch (keyword)
31094 case RID_ASSIGN: property_assign = true; break;
31095 case RID_COPY: property_copy = true; break;
31096 case RID_NONATOMIC: property_nonatomic = true; break;
31097 case RID_READONLY: property_readonly = true; break;
31098 case RID_READWRITE: property_readwrite = true; break;
31099 case RID_RETAIN: property_retain = true; break;
31101 case RID_GETTER:
31102 case RID_SETTER:
31103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31105 if (keyword == RID_GETTER)
31106 cp_parser_error (parser,
31107 "missing %<=%> (after %<getter%> attribute)");
31108 else
31109 cp_parser_error (parser,
31110 "missing %<=%> (after %<setter%> attribute)");
31111 syntax_error = true;
31112 break;
31114 cp_lexer_consume_token (parser->lexer); /* eat the = */
31115 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31117 cp_parser_error (parser, "expected identifier");
31118 syntax_error = true;
31119 break;
31121 if (keyword == RID_SETTER)
31123 if (property_setter_ident != NULL_TREE)
31125 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31126 cp_lexer_consume_token (parser->lexer);
31128 else
31129 property_setter_ident = cp_parser_objc_selector (parser);
31130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31131 cp_parser_error (parser, "setter name must terminate with %<:%>");
31132 else
31133 cp_lexer_consume_token (parser->lexer);
31135 else
31137 if (property_getter_ident != NULL_TREE)
31139 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31140 cp_lexer_consume_token (parser->lexer);
31142 else
31143 property_getter_ident = cp_parser_objc_selector (parser);
31145 break;
31146 default:
31147 cp_parser_error (parser, "unknown property attribute");
31148 syntax_error = true;
31149 break;
31152 if (syntax_error)
31153 break;
31155 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31156 cp_lexer_consume_token (parser->lexer);
31157 else
31158 break;
31161 /* FIXME: "@property (setter, assign);" will generate a spurious
31162 "error: expected ‘)’ before ‘,’ token". This is because
31163 cp_parser_require, unlike the C counterpart, will produce an
31164 error even if we are in error recovery. */
31165 if (!parens.require_close (parser))
31167 cp_parser_skip_to_closing_parenthesis (parser,
31168 /*recovering=*/true,
31169 /*or_comma=*/false,
31170 /*consume_paren=*/true);
31174 /* ... and the property declaration(s). */
31175 properties = cp_parser_objc_struct_declaration (parser);
31177 if (properties == error_mark_node)
31179 cp_parser_skip_to_end_of_statement (parser);
31180 /* If the next token is now a `;', consume it. */
31181 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31182 cp_lexer_consume_token (parser->lexer);
31183 return;
31186 if (properties == NULL_TREE)
31187 cp_parser_error (parser, "expected identifier");
31188 else
31190 /* Comma-separated properties are chained together in
31191 reverse order; add them one by one. */
31192 properties = nreverse (properties);
31194 for (; properties; properties = TREE_CHAIN (properties))
31195 objc_add_property_declaration (loc, copy_node (properties),
31196 property_readonly, property_readwrite,
31197 property_assign, property_retain,
31198 property_copy, property_nonatomic,
31199 property_getter_ident, property_setter_ident);
31202 cp_parser_consume_semicolon_at_end_of_statement (parser);
31205 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31207 objc-synthesize-declaration:
31208 @synthesize objc-synthesize-identifier-list ;
31210 objc-synthesize-identifier-list:
31211 objc-synthesize-identifier
31212 objc-synthesize-identifier-list, objc-synthesize-identifier
31214 objc-synthesize-identifier
31215 identifier
31216 identifier = identifier
31218 For example:
31219 @synthesize MyProperty;
31220 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31222 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31223 for C. Keep them in sync.
31225 static void
31226 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31228 tree list = NULL_TREE;
31229 location_t loc;
31230 loc = cp_lexer_peek_token (parser->lexer)->location;
31232 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31233 while (true)
31235 tree property, ivar;
31236 property = cp_parser_identifier (parser);
31237 if (property == error_mark_node)
31239 cp_parser_consume_semicolon_at_end_of_statement (parser);
31240 return;
31242 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31244 cp_lexer_consume_token (parser->lexer);
31245 ivar = cp_parser_identifier (parser);
31246 if (ivar == error_mark_node)
31248 cp_parser_consume_semicolon_at_end_of_statement (parser);
31249 return;
31252 else
31253 ivar = NULL_TREE;
31254 list = chainon (list, build_tree_list (ivar, property));
31255 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31256 cp_lexer_consume_token (parser->lexer);
31257 else
31258 break;
31260 cp_parser_consume_semicolon_at_end_of_statement (parser);
31261 objc_add_synthesize_declaration (loc, list);
31264 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31266 objc-dynamic-declaration:
31267 @dynamic identifier-list ;
31269 For example:
31270 @dynamic MyProperty;
31271 @dynamic MyProperty, AnotherProperty;
31273 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31274 for C. Keep them in sync.
31276 static void
31277 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31279 tree list = NULL_TREE;
31280 location_t loc;
31281 loc = cp_lexer_peek_token (parser->lexer)->location;
31283 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31284 while (true)
31286 tree property;
31287 property = cp_parser_identifier (parser);
31288 if (property == error_mark_node)
31290 cp_parser_consume_semicolon_at_end_of_statement (parser);
31291 return;
31293 list = chainon (list, build_tree_list (NULL, property));
31294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31295 cp_lexer_consume_token (parser->lexer);
31296 else
31297 break;
31299 cp_parser_consume_semicolon_at_end_of_statement (parser);
31300 objc_add_dynamic_declaration (loc, list);
31304 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31306 /* Returns name of the next clause.
31307 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31308 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31309 returned and the token is consumed. */
31311 static pragma_omp_clause
31312 cp_parser_omp_clause_name (cp_parser *parser)
31314 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31317 result = PRAGMA_OACC_CLAUSE_AUTO;
31318 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31319 result = PRAGMA_OMP_CLAUSE_IF;
31320 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31321 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31322 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31323 result = PRAGMA_OACC_CLAUSE_DELETE;
31324 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31325 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31326 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31327 result = PRAGMA_OMP_CLAUSE_FOR;
31328 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31330 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31331 const char *p = IDENTIFIER_POINTER (id);
31333 switch (p[0])
31335 case 'a':
31336 if (!strcmp ("aligned", p))
31337 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31338 else if (!strcmp ("async", p))
31339 result = PRAGMA_OACC_CLAUSE_ASYNC;
31340 break;
31341 case 'c':
31342 if (!strcmp ("collapse", p))
31343 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31344 else if (!strcmp ("copy", p))
31345 result = PRAGMA_OACC_CLAUSE_COPY;
31346 else if (!strcmp ("copyin", p))
31347 result = PRAGMA_OMP_CLAUSE_COPYIN;
31348 else if (!strcmp ("copyout", p))
31349 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31350 else if (!strcmp ("copyprivate", p))
31351 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31352 else if (!strcmp ("create", p))
31353 result = PRAGMA_OACC_CLAUSE_CREATE;
31354 break;
31355 case 'd':
31356 if (!strcmp ("defaultmap", p))
31357 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31358 else if (!strcmp ("depend", p))
31359 result = PRAGMA_OMP_CLAUSE_DEPEND;
31360 else if (!strcmp ("device", p))
31361 result = PRAGMA_OMP_CLAUSE_DEVICE;
31362 else if (!strcmp ("deviceptr", p))
31363 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31364 else if (!strcmp ("device_resident", p))
31365 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31366 else if (!strcmp ("dist_schedule", p))
31367 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31368 break;
31369 case 'f':
31370 if (!strcmp ("final", p))
31371 result = PRAGMA_OMP_CLAUSE_FINAL;
31372 else if (!strcmp ("firstprivate", p))
31373 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31374 else if (!strcmp ("from", p))
31375 result = PRAGMA_OMP_CLAUSE_FROM;
31376 break;
31377 case 'g':
31378 if (!strcmp ("gang", p))
31379 result = PRAGMA_OACC_CLAUSE_GANG;
31380 else if (!strcmp ("grainsize", p))
31381 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31382 break;
31383 case 'h':
31384 if (!strcmp ("hint", p))
31385 result = PRAGMA_OMP_CLAUSE_HINT;
31386 else if (!strcmp ("host", p))
31387 result = PRAGMA_OACC_CLAUSE_HOST;
31388 break;
31389 case 'i':
31390 if (!strcmp ("inbranch", p))
31391 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31392 else if (!strcmp ("independent", p))
31393 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31394 else if (!strcmp ("is_device_ptr", p))
31395 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31396 break;
31397 case 'l':
31398 if (!strcmp ("lastprivate", p))
31399 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31400 else if (!strcmp ("linear", p))
31401 result = PRAGMA_OMP_CLAUSE_LINEAR;
31402 else if (!strcmp ("link", p))
31403 result = PRAGMA_OMP_CLAUSE_LINK;
31404 break;
31405 case 'm':
31406 if (!strcmp ("map", p))
31407 result = PRAGMA_OMP_CLAUSE_MAP;
31408 else if (!strcmp ("mergeable", p))
31409 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31410 break;
31411 case 'n':
31412 if (!strcmp ("nogroup", p))
31413 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31414 else if (!strcmp ("notinbranch", p))
31415 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31416 else if (!strcmp ("nowait", p))
31417 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31418 else if (!strcmp ("num_gangs", p))
31419 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31420 else if (!strcmp ("num_tasks", p))
31421 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31422 else if (!strcmp ("num_teams", p))
31423 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31424 else if (!strcmp ("num_threads", p))
31425 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31426 else if (!strcmp ("num_workers", p))
31427 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31428 break;
31429 case 'o':
31430 if (!strcmp ("ordered", p))
31431 result = PRAGMA_OMP_CLAUSE_ORDERED;
31432 break;
31433 case 'p':
31434 if (!strcmp ("parallel", p))
31435 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31436 else if (!strcmp ("present", p))
31437 result = PRAGMA_OACC_CLAUSE_PRESENT;
31438 else if (!strcmp ("present_or_copy", p)
31439 || !strcmp ("pcopy", p))
31440 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31441 else if (!strcmp ("present_or_copyin", p)
31442 || !strcmp ("pcopyin", p))
31443 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31444 else if (!strcmp ("present_or_copyout", p)
31445 || !strcmp ("pcopyout", p))
31446 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31447 else if (!strcmp ("present_or_create", p)
31448 || !strcmp ("pcreate", p))
31449 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31450 else if (!strcmp ("priority", p))
31451 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31452 else if (!strcmp ("proc_bind", p))
31453 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31454 break;
31455 case 'r':
31456 if (!strcmp ("reduction", p))
31457 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31458 break;
31459 case 's':
31460 if (!strcmp ("safelen", p))
31461 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31462 else if (!strcmp ("schedule", p))
31463 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31464 else if (!strcmp ("sections", p))
31465 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31466 else if (!strcmp ("self", p))
31467 result = PRAGMA_OACC_CLAUSE_SELF;
31468 else if (!strcmp ("seq", p))
31469 result = PRAGMA_OACC_CLAUSE_SEQ;
31470 else if (!strcmp ("shared", p))
31471 result = PRAGMA_OMP_CLAUSE_SHARED;
31472 else if (!strcmp ("simd", p))
31473 result = PRAGMA_OMP_CLAUSE_SIMD;
31474 else if (!strcmp ("simdlen", p))
31475 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31476 break;
31477 case 't':
31478 if (!strcmp ("taskgroup", p))
31479 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31480 else if (!strcmp ("thread_limit", p))
31481 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31482 else if (!strcmp ("threads", p))
31483 result = PRAGMA_OMP_CLAUSE_THREADS;
31484 else if (!strcmp ("tile", p))
31485 result = PRAGMA_OACC_CLAUSE_TILE;
31486 else if (!strcmp ("to", p))
31487 result = PRAGMA_OMP_CLAUSE_TO;
31488 break;
31489 case 'u':
31490 if (!strcmp ("uniform", p))
31491 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31492 else if (!strcmp ("untied", p))
31493 result = PRAGMA_OMP_CLAUSE_UNTIED;
31494 else if (!strcmp ("use_device", p))
31495 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31496 else if (!strcmp ("use_device_ptr", p))
31497 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31498 break;
31499 case 'v':
31500 if (!strcmp ("vector", p))
31501 result = PRAGMA_OACC_CLAUSE_VECTOR;
31502 else if (!strcmp ("vector_length", p))
31503 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31504 break;
31505 case 'w':
31506 if (!strcmp ("wait", p))
31507 result = PRAGMA_OACC_CLAUSE_WAIT;
31508 else if (!strcmp ("worker", p))
31509 result = PRAGMA_OACC_CLAUSE_WORKER;
31510 break;
31514 if (result != PRAGMA_OMP_CLAUSE_NONE)
31515 cp_lexer_consume_token (parser->lexer);
31517 return result;
31520 /* Validate that a clause of the given type does not already exist. */
31522 static void
31523 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31524 const char *name, location_t location)
31526 tree c;
31528 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31529 if (OMP_CLAUSE_CODE (c) == code)
31531 error_at (location, "too many %qs clauses", name);
31532 break;
31536 /* OpenMP 2.5:
31537 variable-list:
31538 identifier
31539 variable-list , identifier
31541 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31542 colon). An opening parenthesis will have been consumed by the caller.
31544 If KIND is nonzero, create the appropriate node and install the decl
31545 in OMP_CLAUSE_DECL and add the node to the head of the list.
31547 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31548 return the list created.
31550 COLON can be NULL if only closing parenthesis should end the list,
31551 or pointer to bool which will receive false if the list is terminated
31552 by closing parenthesis or true if the list is terminated by colon. */
31554 static tree
31555 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31556 tree list, bool *colon)
31558 cp_token *token;
31559 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31560 if (colon)
31562 parser->colon_corrects_to_scope_p = false;
31563 *colon = false;
31565 while (1)
31567 tree name, decl;
31569 token = cp_lexer_peek_token (parser->lexer);
31570 if (kind != 0
31571 && current_class_ptr
31572 && cp_parser_is_keyword (token, RID_THIS))
31574 decl = finish_this_expr ();
31575 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31576 || CONVERT_EXPR_P (decl))
31577 decl = TREE_OPERAND (decl, 0);
31578 cp_lexer_consume_token (parser->lexer);
31580 else
31582 name = cp_parser_id_expression (parser, /*template_p=*/false,
31583 /*check_dependency_p=*/true,
31584 /*template_p=*/NULL,
31585 /*declarator_p=*/false,
31586 /*optional_p=*/false);
31587 if (name == error_mark_node)
31588 goto skip_comma;
31590 if (identifier_p (name))
31591 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31592 else
31593 decl = name;
31594 if (decl == error_mark_node)
31595 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31596 token->location);
31598 if (decl == error_mark_node)
31600 else if (kind != 0)
31602 switch (kind)
31604 case OMP_CLAUSE__CACHE_:
31605 /* The OpenACC cache directive explicitly only allows "array
31606 elements or subarrays". */
31607 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31609 error_at (token->location, "expected %<[%>");
31610 decl = error_mark_node;
31611 break;
31613 /* FALLTHROUGH. */
31614 case OMP_CLAUSE_MAP:
31615 case OMP_CLAUSE_FROM:
31616 case OMP_CLAUSE_TO:
31617 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31619 location_t loc
31620 = cp_lexer_peek_token (parser->lexer)->location;
31621 cp_id_kind idk = CP_ID_KIND_NONE;
31622 cp_lexer_consume_token (parser->lexer);
31623 decl = convert_from_reference (decl);
31624 decl
31625 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31626 decl, false,
31627 &idk, loc);
31629 /* FALLTHROUGH. */
31630 case OMP_CLAUSE_DEPEND:
31631 case OMP_CLAUSE_REDUCTION:
31632 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31634 tree low_bound = NULL_TREE, length = NULL_TREE;
31636 parser->colon_corrects_to_scope_p = false;
31637 cp_lexer_consume_token (parser->lexer);
31638 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31639 low_bound = cp_parser_expression (parser);
31640 if (!colon)
31641 parser->colon_corrects_to_scope_p
31642 = saved_colon_corrects_to_scope_p;
31643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31644 length = integer_one_node;
31645 else
31647 /* Look for `:'. */
31648 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31649 goto skip_comma;
31650 if (!cp_lexer_next_token_is (parser->lexer,
31651 CPP_CLOSE_SQUARE))
31652 length = cp_parser_expression (parser);
31654 /* Look for the closing `]'. */
31655 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31656 RT_CLOSE_SQUARE))
31657 goto skip_comma;
31659 decl = tree_cons (low_bound, length, decl);
31661 break;
31662 default:
31663 break;
31666 tree u = build_omp_clause (token->location, kind);
31667 OMP_CLAUSE_DECL (u) = decl;
31668 OMP_CLAUSE_CHAIN (u) = list;
31669 list = u;
31671 else
31672 list = tree_cons (decl, NULL_TREE, list);
31674 get_comma:
31675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31676 break;
31677 cp_lexer_consume_token (parser->lexer);
31680 if (colon)
31681 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31683 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31685 *colon = true;
31686 cp_parser_require (parser, CPP_COLON, RT_COLON);
31687 return list;
31690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31692 int ending;
31694 /* Try to resync to an unnested comma. Copied from
31695 cp_parser_parenthesized_expression_list. */
31696 skip_comma:
31697 if (colon)
31698 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31699 ending = cp_parser_skip_to_closing_parenthesis (parser,
31700 /*recovering=*/true,
31701 /*or_comma=*/true,
31702 /*consume_paren=*/true);
31703 if (ending < 0)
31704 goto get_comma;
31707 return list;
31710 /* Similarly, but expect leading and trailing parenthesis. This is a very
31711 common case for omp clauses. */
31713 static tree
31714 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31716 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31717 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31718 return list;
31721 /* OpenACC 2.0:
31722 copy ( variable-list )
31723 copyin ( variable-list )
31724 copyout ( variable-list )
31725 create ( variable-list )
31726 delete ( variable-list )
31727 present ( variable-list )
31728 present_or_copy ( variable-list )
31729 pcopy ( variable-list )
31730 present_or_copyin ( variable-list )
31731 pcopyin ( variable-list )
31732 present_or_copyout ( variable-list )
31733 pcopyout ( variable-list )
31734 present_or_create ( variable-list )
31735 pcreate ( variable-list ) */
31737 static tree
31738 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31739 tree list)
31741 enum gomp_map_kind kind;
31742 switch (c_kind)
31744 case PRAGMA_OACC_CLAUSE_COPY:
31745 kind = GOMP_MAP_FORCE_TOFROM;
31746 break;
31747 case PRAGMA_OACC_CLAUSE_COPYIN:
31748 kind = GOMP_MAP_FORCE_TO;
31749 break;
31750 case PRAGMA_OACC_CLAUSE_COPYOUT:
31751 kind = GOMP_MAP_FORCE_FROM;
31752 break;
31753 case PRAGMA_OACC_CLAUSE_CREATE:
31754 kind = GOMP_MAP_FORCE_ALLOC;
31755 break;
31756 case PRAGMA_OACC_CLAUSE_DELETE:
31757 kind = GOMP_MAP_DELETE;
31758 break;
31759 case PRAGMA_OACC_CLAUSE_DEVICE:
31760 kind = GOMP_MAP_FORCE_TO;
31761 break;
31762 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31763 kind = GOMP_MAP_DEVICE_RESIDENT;
31764 break;
31765 case PRAGMA_OACC_CLAUSE_HOST:
31766 case PRAGMA_OACC_CLAUSE_SELF:
31767 kind = GOMP_MAP_FORCE_FROM;
31768 break;
31769 case PRAGMA_OACC_CLAUSE_LINK:
31770 kind = GOMP_MAP_LINK;
31771 break;
31772 case PRAGMA_OACC_CLAUSE_PRESENT:
31773 kind = GOMP_MAP_FORCE_PRESENT;
31774 break;
31775 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31776 kind = GOMP_MAP_TOFROM;
31777 break;
31778 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31779 kind = GOMP_MAP_TO;
31780 break;
31781 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31782 kind = GOMP_MAP_FROM;
31783 break;
31784 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31785 kind = GOMP_MAP_ALLOC;
31786 break;
31787 default:
31788 gcc_unreachable ();
31790 tree nl, c;
31791 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31793 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31794 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31796 return nl;
31799 /* OpenACC 2.0:
31800 deviceptr ( variable-list ) */
31802 static tree
31803 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31805 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31806 tree vars, t;
31808 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31809 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31810 variable-list must only allow for pointer variables. */
31811 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31812 for (t = vars; t; t = TREE_CHAIN (t))
31814 tree v = TREE_PURPOSE (t);
31815 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31816 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31817 OMP_CLAUSE_DECL (u) = v;
31818 OMP_CLAUSE_CHAIN (u) = list;
31819 list = u;
31822 return list;
31825 /* OpenACC 2.0:
31826 auto
31827 independent
31828 nohost
31829 seq */
31831 static tree
31832 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31833 enum omp_clause_code code,
31834 tree list, location_t location)
31836 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31837 tree c = build_omp_clause (location, code);
31838 OMP_CLAUSE_CHAIN (c) = list;
31839 return c;
31842 /* OpenACC:
31843 num_gangs ( expression )
31844 num_workers ( expression )
31845 vector_length ( expression ) */
31847 static tree
31848 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31849 const char *str, tree list)
31851 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31853 matching_parens parens;
31854 if (!parens.require_open (parser))
31855 return list;
31857 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31859 if (t == error_mark_node
31860 || !parens.require_close (parser))
31862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31863 /*or_comma=*/false,
31864 /*consume_paren=*/true);
31865 return list;
31868 check_no_duplicate_clause (list, code, str, loc);
31870 tree c = build_omp_clause (loc, code);
31871 OMP_CLAUSE_OPERAND (c, 0) = t;
31872 OMP_CLAUSE_CHAIN (c) = list;
31873 return c;
31876 /* OpenACC:
31878 gang [( gang-arg-list )]
31879 worker [( [num:] int-expr )]
31880 vector [( [length:] int-expr )]
31882 where gang-arg is one of:
31884 [num:] int-expr
31885 static: size-expr
31887 and size-expr may be:
31890 int-expr
31893 static tree
31894 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31895 const char *str, tree list)
31897 const char *id = "num";
31898 cp_lexer *lexer = parser->lexer;
31899 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31900 location_t loc = cp_lexer_peek_token (lexer)->location;
31902 if (kind == OMP_CLAUSE_VECTOR)
31903 id = "length";
31905 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31907 matching_parens parens;
31908 parens.consume_open (parser);
31912 cp_token *next = cp_lexer_peek_token (lexer);
31913 int idx = 0;
31915 /* Gang static argument. */
31916 if (kind == OMP_CLAUSE_GANG
31917 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31919 cp_lexer_consume_token (lexer);
31921 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31922 goto cleanup_error;
31924 idx = 1;
31925 if (ops[idx] != NULL)
31927 cp_parser_error (parser, "too many %<static%> arguments");
31928 goto cleanup_error;
31931 /* Check for the '*' argument. */
31932 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31933 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31934 || cp_lexer_nth_token_is (parser->lexer, 2,
31935 CPP_CLOSE_PAREN)))
31937 cp_lexer_consume_token (lexer);
31938 ops[idx] = integer_minus_one_node;
31940 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31942 cp_lexer_consume_token (lexer);
31943 continue;
31945 else break;
31948 /* Worker num: argument and vector length: arguments. */
31949 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31950 && id_equal (next->u.value, id)
31951 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31953 cp_lexer_consume_token (lexer); /* id */
31954 cp_lexer_consume_token (lexer); /* ':' */
31957 /* Now collect the actual argument. */
31958 if (ops[idx] != NULL_TREE)
31960 cp_parser_error (parser, "unexpected argument");
31961 goto cleanup_error;
31964 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31965 false);
31966 if (expr == error_mark_node)
31967 goto cleanup_error;
31969 mark_exp_read (expr);
31970 ops[idx] = expr;
31972 if (kind == OMP_CLAUSE_GANG
31973 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31975 cp_lexer_consume_token (lexer);
31976 continue;
31978 break;
31980 while (1);
31982 if (!parens.require_close (parser))
31983 goto cleanup_error;
31986 check_no_duplicate_clause (list, kind, str, loc);
31988 c = build_omp_clause (loc, kind);
31990 if (ops[1])
31991 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31993 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31994 OMP_CLAUSE_CHAIN (c) = list;
31996 return c;
31998 cleanup_error:
31999 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32000 return list;
32003 /* OpenACC 2.0:
32004 tile ( size-expr-list ) */
32006 static tree
32007 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32009 tree c, expr = error_mark_node;
32010 tree tile = NULL_TREE;
32012 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32013 so, but the spec authors never considered such a case and have
32014 differing opinions on what it might mean, including 'not
32015 allowed'.) */
32016 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32017 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32018 clause_loc);
32020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32021 return list;
32025 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32026 return list;
32028 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32029 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32030 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32032 cp_lexer_consume_token (parser->lexer);
32033 expr = integer_zero_node;
32035 else
32036 expr = cp_parser_constant_expression (parser);
32038 tile = tree_cons (NULL_TREE, expr, tile);
32040 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32042 /* Consume the trailing ')'. */
32043 cp_lexer_consume_token (parser->lexer);
32045 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32046 tile = nreverse (tile);
32047 OMP_CLAUSE_TILE_LIST (c) = tile;
32048 OMP_CLAUSE_CHAIN (c) = list;
32049 return c;
32052 /* OpenACC 2.0
32053 Parse wait clause or directive parameters. */
32055 static tree
32056 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32058 vec<tree, va_gc> *args;
32059 tree t, args_tree;
32061 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32062 /*cast_p=*/false,
32063 /*allow_expansion_p=*/true,
32064 /*non_constant_p=*/NULL);
32066 if (args == NULL || args->length () == 0)
32068 cp_parser_error (parser, "expected integer expression before ')'");
32069 if (args != NULL)
32070 release_tree_vector (args);
32071 return list;
32074 args_tree = build_tree_list_vec (args);
32076 release_tree_vector (args);
32078 for (t = args_tree; t; t = TREE_CHAIN (t))
32080 tree targ = TREE_VALUE (t);
32082 if (targ != error_mark_node)
32084 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32085 error ("%<wait%> expression must be integral");
32086 else
32088 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32090 targ = mark_rvalue_use (targ);
32091 OMP_CLAUSE_DECL (c) = targ;
32092 OMP_CLAUSE_CHAIN (c) = list;
32093 list = c;
32098 return list;
32101 /* OpenACC:
32102 wait ( int-expr-list ) */
32104 static tree
32105 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32107 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32109 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32110 return list;
32112 list = cp_parser_oacc_wait_list (parser, location, list);
32114 return list;
32117 /* OpenMP 3.0:
32118 collapse ( constant-expression ) */
32120 static tree
32121 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32123 tree c, num;
32124 location_t loc;
32125 HOST_WIDE_INT n;
32127 loc = cp_lexer_peek_token (parser->lexer)->location;
32128 matching_parens parens;
32129 if (!parens.require_open (parser))
32130 return list;
32132 num = cp_parser_constant_expression (parser);
32134 if (!parens.require_close (parser))
32135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32136 /*or_comma=*/false,
32137 /*consume_paren=*/true);
32139 if (num == error_mark_node)
32140 return list;
32141 num = fold_non_dependent_expr (num);
32142 if (!tree_fits_shwi_p (num)
32143 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32144 || (n = tree_to_shwi (num)) <= 0
32145 || (int) n != n)
32147 error_at (loc, "collapse argument needs positive constant integer expression");
32148 return list;
32151 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32152 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32153 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32154 OMP_CLAUSE_CHAIN (c) = list;
32155 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32157 return c;
32160 /* OpenMP 2.5:
32161 default ( none | shared )
32163 OpenACC:
32164 default ( none | present ) */
32166 static tree
32167 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32168 location_t location, bool is_oacc)
32170 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32171 tree c;
32173 matching_parens parens;
32174 if (!parens.require_open (parser))
32175 return list;
32176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32179 const char *p = IDENTIFIER_POINTER (id);
32181 switch (p[0])
32183 case 'n':
32184 if (strcmp ("none", p) != 0)
32185 goto invalid_kind;
32186 kind = OMP_CLAUSE_DEFAULT_NONE;
32187 break;
32189 case 'p':
32190 if (strcmp ("present", p) != 0 || !is_oacc)
32191 goto invalid_kind;
32192 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32193 break;
32195 case 's':
32196 if (strcmp ("shared", p) != 0 || is_oacc)
32197 goto invalid_kind;
32198 kind = OMP_CLAUSE_DEFAULT_SHARED;
32199 break;
32201 default:
32202 goto invalid_kind;
32205 cp_lexer_consume_token (parser->lexer);
32207 else
32209 invalid_kind:
32210 if (is_oacc)
32211 cp_parser_error (parser, "expected %<none%> or %<present%>");
32212 else
32213 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32216 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32217 || !parens.require_close (parser))
32218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32219 /*or_comma=*/false,
32220 /*consume_paren=*/true);
32222 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32223 return list;
32225 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32226 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32227 OMP_CLAUSE_CHAIN (c) = list;
32228 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32230 return c;
32233 /* OpenMP 3.1:
32234 final ( expression ) */
32236 static tree
32237 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32239 tree t, c;
32241 matching_parens parens;
32242 if (!parens.require_open (parser))
32243 return list;
32245 t = cp_parser_condition (parser);
32247 if (t == error_mark_node
32248 || !parens.require_close (parser))
32249 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32250 /*or_comma=*/false,
32251 /*consume_paren=*/true);
32253 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32255 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32256 OMP_CLAUSE_FINAL_EXPR (c) = t;
32257 OMP_CLAUSE_CHAIN (c) = list;
32259 return c;
32262 /* OpenMP 2.5:
32263 if ( expression )
32265 OpenMP 4.5:
32266 if ( directive-name-modifier : expression )
32268 directive-name-modifier:
32269 parallel | task | taskloop | target data | target | target update
32270 | target enter data | target exit data */
32272 static tree
32273 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32274 bool is_omp)
32276 tree t, c;
32277 enum tree_code if_modifier = ERROR_MARK;
32279 matching_parens parens;
32280 if (!parens.require_open (parser))
32281 return list;
32283 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32286 const char *p = IDENTIFIER_POINTER (id);
32287 int n = 2;
32289 if (strcmp ("parallel", p) == 0)
32290 if_modifier = OMP_PARALLEL;
32291 else if (strcmp ("task", p) == 0)
32292 if_modifier = OMP_TASK;
32293 else if (strcmp ("taskloop", p) == 0)
32294 if_modifier = OMP_TASKLOOP;
32295 else if (strcmp ("target", p) == 0)
32297 if_modifier = OMP_TARGET;
32298 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32300 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32301 p = IDENTIFIER_POINTER (id);
32302 if (strcmp ("data", p) == 0)
32303 if_modifier = OMP_TARGET_DATA;
32304 else if (strcmp ("update", p) == 0)
32305 if_modifier = OMP_TARGET_UPDATE;
32306 else if (strcmp ("enter", p) == 0)
32307 if_modifier = OMP_TARGET_ENTER_DATA;
32308 else if (strcmp ("exit", p) == 0)
32309 if_modifier = OMP_TARGET_EXIT_DATA;
32310 if (if_modifier != OMP_TARGET)
32311 n = 3;
32312 else
32314 location_t loc
32315 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32316 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32317 "or %<exit%>");
32318 if_modifier = ERROR_MARK;
32320 if (if_modifier == OMP_TARGET_ENTER_DATA
32321 || if_modifier == OMP_TARGET_EXIT_DATA)
32323 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32325 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32326 p = IDENTIFIER_POINTER (id);
32327 if (strcmp ("data", p) == 0)
32328 n = 4;
32330 if (n != 4)
32332 location_t loc
32333 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32334 error_at (loc, "expected %<data%>");
32335 if_modifier = ERROR_MARK;
32340 if (if_modifier != ERROR_MARK)
32342 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32344 while (n-- > 0)
32345 cp_lexer_consume_token (parser->lexer);
32347 else
32349 if (n > 2)
32351 location_t loc
32352 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32353 error_at (loc, "expected %<:%>");
32355 if_modifier = ERROR_MARK;
32360 t = cp_parser_condition (parser);
32362 if (t == error_mark_node
32363 || !parens.require_close (parser))
32364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32369 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32371 if (if_modifier != ERROR_MARK
32372 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32374 const char *p = NULL;
32375 switch (if_modifier)
32377 case OMP_PARALLEL: p = "parallel"; break;
32378 case OMP_TASK: p = "task"; break;
32379 case OMP_TASKLOOP: p = "taskloop"; break;
32380 case OMP_TARGET_DATA: p = "target data"; break;
32381 case OMP_TARGET: p = "target"; break;
32382 case OMP_TARGET_UPDATE: p = "target update"; break;
32383 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32384 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32385 default: gcc_unreachable ();
32387 error_at (location, "too many %<if%> clauses with %qs modifier",
32389 return list;
32391 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32393 if (!is_omp)
32394 error_at (location, "too many %<if%> clauses");
32395 else
32396 error_at (location, "too many %<if%> clauses without modifier");
32397 return list;
32399 else if (if_modifier == ERROR_MARK
32400 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32402 error_at (location, "if any %<if%> clause has modifier, then all "
32403 "%<if%> clauses have to use modifier");
32404 return list;
32408 c = build_omp_clause (location, OMP_CLAUSE_IF);
32409 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32410 OMP_CLAUSE_IF_EXPR (c) = t;
32411 OMP_CLAUSE_CHAIN (c) = list;
32413 return c;
32416 /* OpenMP 3.1:
32417 mergeable */
32419 static tree
32420 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32421 tree list, location_t location)
32423 tree c;
32425 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32426 location);
32428 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32429 OMP_CLAUSE_CHAIN (c) = list;
32430 return c;
32433 /* OpenMP 2.5:
32434 nowait */
32436 static tree
32437 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32438 tree list, location_t location)
32440 tree c;
32442 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32444 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32445 OMP_CLAUSE_CHAIN (c) = list;
32446 return c;
32449 /* OpenMP 2.5:
32450 num_threads ( expression ) */
32452 static tree
32453 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32454 location_t location)
32456 tree t, c;
32458 matching_parens parens;
32459 if (!parens.require_open (parser))
32460 return list;
32462 t = cp_parser_expression (parser);
32464 if (t == error_mark_node
32465 || !parens.require_close (parser))
32466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32467 /*or_comma=*/false,
32468 /*consume_paren=*/true);
32470 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32471 "num_threads", location);
32473 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32474 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32475 OMP_CLAUSE_CHAIN (c) = list;
32477 return c;
32480 /* OpenMP 4.5:
32481 num_tasks ( expression ) */
32483 static tree
32484 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32485 location_t location)
32487 tree t, c;
32489 matching_parens parens;
32490 if (!parens.require_open (parser))
32491 return list;
32493 t = cp_parser_expression (parser);
32495 if (t == error_mark_node
32496 || !parens.require_close (parser))
32497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32498 /*or_comma=*/false,
32499 /*consume_paren=*/true);
32501 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32502 "num_tasks", location);
32504 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32505 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32506 OMP_CLAUSE_CHAIN (c) = list;
32508 return c;
32511 /* OpenMP 4.5:
32512 grainsize ( expression ) */
32514 static tree
32515 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32516 location_t location)
32518 tree t, c;
32520 matching_parens parens;
32521 if (!parens.require_open (parser))
32522 return list;
32524 t = cp_parser_expression (parser);
32526 if (t == error_mark_node
32527 || !parens.require_close (parser))
32528 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32529 /*or_comma=*/false,
32530 /*consume_paren=*/true);
32532 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32533 "grainsize", location);
32535 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32536 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32537 OMP_CLAUSE_CHAIN (c) = list;
32539 return c;
32542 /* OpenMP 4.5:
32543 priority ( expression ) */
32545 static tree
32546 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32547 location_t location)
32549 tree t, c;
32551 matching_parens parens;
32552 if (!parens.require_open (parser))
32553 return list;
32555 t = cp_parser_expression (parser);
32557 if (t == error_mark_node
32558 || !parens.require_close (parser))
32559 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32560 /*or_comma=*/false,
32561 /*consume_paren=*/true);
32563 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32564 "priority", location);
32566 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32567 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32568 OMP_CLAUSE_CHAIN (c) = list;
32570 return c;
32573 /* OpenMP 4.5:
32574 hint ( expression ) */
32576 static tree
32577 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32578 location_t location)
32580 tree t, c;
32582 matching_parens parens;
32583 if (!parens.require_open (parser))
32584 return list;
32586 t = cp_parser_expression (parser);
32588 if (t == error_mark_node
32589 || !parens.require_close (parser))
32590 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32591 /*or_comma=*/false,
32592 /*consume_paren=*/true);
32594 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32596 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32597 OMP_CLAUSE_HINT_EXPR (c) = t;
32598 OMP_CLAUSE_CHAIN (c) = list;
32600 return c;
32603 /* OpenMP 4.5:
32604 defaultmap ( tofrom : scalar ) */
32606 static tree
32607 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32608 location_t location)
32610 tree c, id;
32611 const char *p;
32613 matching_parens parens;
32614 if (!parens.require_open (parser))
32615 return list;
32617 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32619 cp_parser_error (parser, "expected %<tofrom%>");
32620 goto out_err;
32622 id = cp_lexer_peek_token (parser->lexer)->u.value;
32623 p = IDENTIFIER_POINTER (id);
32624 if (strcmp (p, "tofrom") != 0)
32626 cp_parser_error (parser, "expected %<tofrom%>");
32627 goto out_err;
32629 cp_lexer_consume_token (parser->lexer);
32630 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32631 goto out_err;
32633 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32635 cp_parser_error (parser, "expected %<scalar%>");
32636 goto out_err;
32638 id = cp_lexer_peek_token (parser->lexer)->u.value;
32639 p = IDENTIFIER_POINTER (id);
32640 if (strcmp (p, "scalar") != 0)
32642 cp_parser_error (parser, "expected %<scalar%>");
32643 goto out_err;
32645 cp_lexer_consume_token (parser->lexer);
32646 if (!parens.require_close (parser))
32647 goto out_err;
32649 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32650 location);
32652 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32653 OMP_CLAUSE_CHAIN (c) = list;
32654 return c;
32656 out_err:
32657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32658 /*or_comma=*/false,
32659 /*consume_paren=*/true);
32660 return list;
32663 /* OpenMP 2.5:
32664 ordered
32666 OpenMP 4.5:
32667 ordered ( constant-expression ) */
32669 static tree
32670 cp_parser_omp_clause_ordered (cp_parser *parser,
32671 tree list, location_t location)
32673 tree c, num = NULL_TREE;
32674 HOST_WIDE_INT n;
32676 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32677 "ordered", location);
32679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32681 matching_parens parens;
32682 parens.consume_open (parser);
32684 num = cp_parser_constant_expression (parser);
32686 if (!parens.require_close (parser))
32687 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32688 /*or_comma=*/false,
32689 /*consume_paren=*/true);
32691 if (num == error_mark_node)
32692 return list;
32693 num = fold_non_dependent_expr (num);
32694 if (!tree_fits_shwi_p (num)
32695 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32696 || (n = tree_to_shwi (num)) <= 0
32697 || (int) n != n)
32699 error_at (location,
32700 "ordered argument needs positive constant integer "
32701 "expression");
32702 return list;
32706 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32707 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32708 OMP_CLAUSE_CHAIN (c) = list;
32709 return c;
32712 /* OpenMP 2.5:
32713 reduction ( reduction-operator : variable-list )
32715 reduction-operator:
32716 One of: + * - & ^ | && ||
32718 OpenMP 3.1:
32720 reduction-operator:
32721 One of: + * - & ^ | && || min max
32723 OpenMP 4.0:
32725 reduction-operator:
32726 One of: + * - & ^ | && ||
32727 id-expression */
32729 static tree
32730 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32732 enum tree_code code = ERROR_MARK;
32733 tree nlist, c, id = NULL_TREE;
32735 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32736 return list;
32738 switch (cp_lexer_peek_token (parser->lexer)->type)
32740 case CPP_PLUS: code = PLUS_EXPR; break;
32741 case CPP_MULT: code = MULT_EXPR; break;
32742 case CPP_MINUS: code = MINUS_EXPR; break;
32743 case CPP_AND: code = BIT_AND_EXPR; break;
32744 case CPP_XOR: code = BIT_XOR_EXPR; break;
32745 case CPP_OR: code = BIT_IOR_EXPR; break;
32746 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32747 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32748 default: break;
32751 if (code != ERROR_MARK)
32752 cp_lexer_consume_token (parser->lexer);
32753 else
32755 bool saved_colon_corrects_to_scope_p;
32756 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32757 parser->colon_corrects_to_scope_p = false;
32758 id = cp_parser_id_expression (parser, /*template_p=*/false,
32759 /*check_dependency_p=*/true,
32760 /*template_p=*/NULL,
32761 /*declarator_p=*/false,
32762 /*optional_p=*/false);
32763 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32764 if (identifier_p (id))
32766 const char *p = IDENTIFIER_POINTER (id);
32768 if (strcmp (p, "min") == 0)
32769 code = MIN_EXPR;
32770 else if (strcmp (p, "max") == 0)
32771 code = MAX_EXPR;
32772 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32773 code = PLUS_EXPR;
32774 else if (id == ovl_op_identifier (false, MULT_EXPR))
32775 code = MULT_EXPR;
32776 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32777 code = MINUS_EXPR;
32778 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32779 code = BIT_AND_EXPR;
32780 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32781 code = BIT_IOR_EXPR;
32782 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32783 code = BIT_XOR_EXPR;
32784 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32785 code = TRUTH_ANDIF_EXPR;
32786 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32787 code = TRUTH_ORIF_EXPR;
32788 id = omp_reduction_id (code, id, NULL_TREE);
32789 tree scope = parser->scope;
32790 if (scope)
32791 id = build_qualified_name (NULL_TREE, scope, id, false);
32792 parser->scope = NULL_TREE;
32793 parser->qualifying_scope = NULL_TREE;
32794 parser->object_scope = NULL_TREE;
32796 else
32798 error ("invalid reduction-identifier");
32799 resync_fail:
32800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32801 /*or_comma=*/false,
32802 /*consume_paren=*/true);
32803 return list;
32807 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32808 goto resync_fail;
32810 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32811 NULL);
32812 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32814 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32815 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32818 return nlist;
32821 /* OpenMP 2.5:
32822 schedule ( schedule-kind )
32823 schedule ( schedule-kind , expression )
32825 schedule-kind:
32826 static | dynamic | guided | runtime | auto
32828 OpenMP 4.5:
32829 schedule ( schedule-modifier : schedule-kind )
32830 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32832 schedule-modifier:
32833 simd
32834 monotonic
32835 nonmonotonic */
32837 static tree
32838 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32840 tree c, t;
32841 int modifiers = 0, nmodifiers = 0;
32843 matching_parens parens;
32844 if (!parens.require_open (parser))
32845 return list;
32847 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32849 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32851 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32852 const char *p = IDENTIFIER_POINTER (id);
32853 if (strcmp ("simd", p) == 0)
32854 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32855 else if (strcmp ("monotonic", p) == 0)
32856 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32857 else if (strcmp ("nonmonotonic", p) == 0)
32858 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32859 else
32860 break;
32861 cp_lexer_consume_token (parser->lexer);
32862 if (nmodifiers++ == 0
32863 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32864 cp_lexer_consume_token (parser->lexer);
32865 else
32867 cp_parser_require (parser, CPP_COLON, RT_COLON);
32868 break;
32872 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32875 const char *p = IDENTIFIER_POINTER (id);
32877 switch (p[0])
32879 case 'd':
32880 if (strcmp ("dynamic", p) != 0)
32881 goto invalid_kind;
32882 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32883 break;
32885 case 'g':
32886 if (strcmp ("guided", p) != 0)
32887 goto invalid_kind;
32888 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32889 break;
32891 case 'r':
32892 if (strcmp ("runtime", p) != 0)
32893 goto invalid_kind;
32894 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32895 break;
32897 default:
32898 goto invalid_kind;
32901 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32902 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32903 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32904 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32905 else
32906 goto invalid_kind;
32907 cp_lexer_consume_token (parser->lexer);
32909 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32910 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32911 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32912 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32914 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32915 "specified");
32916 modifiers = 0;
32919 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32921 cp_token *token;
32922 cp_lexer_consume_token (parser->lexer);
32924 token = cp_lexer_peek_token (parser->lexer);
32925 t = cp_parser_assignment_expression (parser);
32927 if (t == error_mark_node)
32928 goto resync_fail;
32929 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32930 error_at (token->location, "schedule %<runtime%> does not take "
32931 "a %<chunk_size%> parameter");
32932 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32933 error_at (token->location, "schedule %<auto%> does not take "
32934 "a %<chunk_size%> parameter");
32935 else
32936 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32938 if (!parens.require_close (parser))
32939 goto resync_fail;
32941 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32942 goto resync_fail;
32944 OMP_CLAUSE_SCHEDULE_KIND (c)
32945 = (enum omp_clause_schedule_kind)
32946 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32948 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32949 OMP_CLAUSE_CHAIN (c) = list;
32950 return c;
32952 invalid_kind:
32953 cp_parser_error (parser, "invalid schedule kind");
32954 resync_fail:
32955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32956 /*or_comma=*/false,
32957 /*consume_paren=*/true);
32958 return list;
32961 /* OpenMP 3.0:
32962 untied */
32964 static tree
32965 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32966 tree list, location_t location)
32968 tree c;
32970 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32972 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32973 OMP_CLAUSE_CHAIN (c) = list;
32974 return c;
32977 /* OpenMP 4.0:
32978 inbranch
32979 notinbranch */
32981 static tree
32982 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32983 tree list, location_t location)
32985 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32986 tree c = build_omp_clause (location, code);
32987 OMP_CLAUSE_CHAIN (c) = list;
32988 return c;
32991 /* OpenMP 4.0:
32992 parallel
32994 sections
32995 taskgroup */
32997 static tree
32998 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32999 enum omp_clause_code code,
33000 tree list, location_t location)
33002 tree c = build_omp_clause (location, code);
33003 OMP_CLAUSE_CHAIN (c) = list;
33004 return c;
33007 /* OpenMP 4.5:
33008 nogroup */
33010 static tree
33011 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33012 tree list, location_t location)
33014 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33015 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33016 OMP_CLAUSE_CHAIN (c) = list;
33017 return c;
33020 /* OpenMP 4.5:
33021 simd
33022 threads */
33024 static tree
33025 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33026 enum omp_clause_code code,
33027 tree list, location_t location)
33029 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33030 tree c = build_omp_clause (location, code);
33031 OMP_CLAUSE_CHAIN (c) = list;
33032 return c;
33035 /* OpenMP 4.0:
33036 num_teams ( expression ) */
33038 static tree
33039 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33040 location_t location)
33042 tree t, c;
33044 matching_parens parens;
33045 if (!parens.require_open (parser))
33046 return list;
33048 t = cp_parser_expression (parser);
33050 if (t == error_mark_node
33051 || !parens.require_close (parser))
33052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33053 /*or_comma=*/false,
33054 /*consume_paren=*/true);
33056 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33057 "num_teams", location);
33059 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33060 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33061 OMP_CLAUSE_CHAIN (c) = list;
33063 return c;
33066 /* OpenMP 4.0:
33067 thread_limit ( expression ) */
33069 static tree
33070 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33071 location_t location)
33073 tree t, c;
33075 matching_parens parens;
33076 if (!parens.require_open (parser))
33077 return list;
33079 t = cp_parser_expression (parser);
33081 if (t == error_mark_node
33082 || !parens.require_close (parser))
33083 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33084 /*or_comma=*/false,
33085 /*consume_paren=*/true);
33087 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33088 "thread_limit", location);
33090 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33091 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33092 OMP_CLAUSE_CHAIN (c) = list;
33094 return c;
33097 /* OpenMP 4.0:
33098 aligned ( variable-list )
33099 aligned ( variable-list : constant-expression ) */
33101 static tree
33102 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33104 tree nlist, c, alignment = NULL_TREE;
33105 bool colon;
33107 matching_parens parens;
33108 if (!parens.require_open (parser))
33109 return list;
33111 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33112 &colon);
33114 if (colon)
33116 alignment = cp_parser_constant_expression (parser);
33118 if (!parens.require_close (parser))
33119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33123 if (alignment == error_mark_node)
33124 alignment = NULL_TREE;
33127 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33128 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33130 return nlist;
33133 /* OpenMP 4.0:
33134 linear ( variable-list )
33135 linear ( variable-list : expression )
33137 OpenMP 4.5:
33138 linear ( modifier ( variable-list ) )
33139 linear ( modifier ( variable-list ) : expression ) */
33141 static tree
33142 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33143 bool declare_simd)
33145 tree nlist, c, step = integer_one_node;
33146 bool colon;
33147 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33149 matching_parens parens;
33150 if (!parens.require_open (parser))
33151 return list;
33153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33155 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33156 const char *p = IDENTIFIER_POINTER (id);
33158 if (strcmp ("ref", p) == 0)
33159 kind = OMP_CLAUSE_LINEAR_REF;
33160 else if (strcmp ("val", p) == 0)
33161 kind = OMP_CLAUSE_LINEAR_VAL;
33162 else if (strcmp ("uval", p) == 0)
33163 kind = OMP_CLAUSE_LINEAR_UVAL;
33164 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33165 cp_lexer_consume_token (parser->lexer);
33166 else
33167 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33170 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33171 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33172 &colon);
33173 else
33175 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33176 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33177 if (colon)
33178 cp_parser_require (parser, CPP_COLON, RT_COLON);
33179 else if (!parens.require_close (parser))
33180 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33181 /*or_comma=*/false,
33182 /*consume_paren=*/true);
33185 if (colon)
33187 step = NULL_TREE;
33188 if (declare_simd
33189 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33190 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33192 cp_token *token = cp_lexer_peek_token (parser->lexer);
33193 cp_parser_parse_tentatively (parser);
33194 step = cp_parser_id_expression (parser, /*template_p=*/false,
33195 /*check_dependency_p=*/true,
33196 /*template_p=*/NULL,
33197 /*declarator_p=*/false,
33198 /*optional_p=*/false);
33199 if (step != error_mark_node)
33200 step = cp_parser_lookup_name_simple (parser, step, token->location);
33201 if (step == error_mark_node)
33203 step = NULL_TREE;
33204 cp_parser_abort_tentative_parse (parser);
33206 else if (!cp_parser_parse_definitely (parser))
33207 step = NULL_TREE;
33209 if (!step)
33210 step = cp_parser_expression (parser);
33212 if (!parens.require_close (parser))
33213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33214 /*or_comma=*/false,
33215 /*consume_paren=*/true);
33217 if (step == error_mark_node)
33218 return list;
33221 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33223 OMP_CLAUSE_LINEAR_STEP (c) = step;
33224 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33227 return nlist;
33230 /* OpenMP 4.0:
33231 safelen ( constant-expression ) */
33233 static tree
33234 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33235 location_t location)
33237 tree t, c;
33239 matching_parens parens;
33240 if (!parens.require_open (parser))
33241 return list;
33243 t = cp_parser_constant_expression (parser);
33245 if (t == error_mark_node
33246 || !parens.require_close (parser))
33247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33248 /*or_comma=*/false,
33249 /*consume_paren=*/true);
33251 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33253 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33254 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33255 OMP_CLAUSE_CHAIN (c) = list;
33257 return c;
33260 /* OpenMP 4.0:
33261 simdlen ( constant-expression ) */
33263 static tree
33264 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33265 location_t location)
33267 tree t, c;
33269 matching_parens parens;
33270 if (!parens.require_open (parser))
33271 return list;
33273 t = cp_parser_constant_expression (parser);
33275 if (t == error_mark_node
33276 || !parens.require_close (parser))
33277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33278 /*or_comma=*/false,
33279 /*consume_paren=*/true);
33281 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33283 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33284 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33285 OMP_CLAUSE_CHAIN (c) = list;
33287 return c;
33290 /* OpenMP 4.5:
33291 vec:
33292 identifier [+/- integer]
33293 vec , identifier [+/- integer]
33296 static tree
33297 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33298 tree list)
33300 tree vec = NULL;
33302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33304 cp_parser_error (parser, "expected identifier");
33305 return list;
33308 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33310 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33311 tree t, identifier = cp_parser_identifier (parser);
33312 tree addend = NULL;
33314 if (identifier == error_mark_node)
33315 t = error_mark_node;
33316 else
33318 t = cp_parser_lookup_name_simple
33319 (parser, identifier,
33320 cp_lexer_peek_token (parser->lexer)->location);
33321 if (t == error_mark_node)
33322 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33323 id_loc);
33326 bool neg = false;
33327 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33328 neg = true;
33329 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33331 addend = integer_zero_node;
33332 goto add_to_vector;
33334 cp_lexer_consume_token (parser->lexer);
33336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33338 cp_parser_error (parser, "expected integer");
33339 return list;
33342 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33343 if (TREE_CODE (addend) != INTEGER_CST)
33345 cp_parser_error (parser, "expected integer");
33346 return list;
33348 cp_lexer_consume_token (parser->lexer);
33350 add_to_vector:
33351 if (t != error_mark_node)
33353 vec = tree_cons (addend, t, vec);
33354 if (neg)
33355 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33359 break;
33361 cp_lexer_consume_token (parser->lexer);
33364 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33366 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33367 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33368 OMP_CLAUSE_DECL (u) = nreverse (vec);
33369 OMP_CLAUSE_CHAIN (u) = list;
33370 return u;
33372 return list;
33375 /* OpenMP 4.0:
33376 depend ( depend-kind : variable-list )
33378 depend-kind:
33379 in | out | inout
33381 OpenMP 4.5:
33382 depend ( source )
33384 depend ( sink : vec ) */
33386 static tree
33387 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33389 tree nlist, c;
33390 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33392 matching_parens parens;
33393 if (!parens.require_open (parser))
33394 return list;
33396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33398 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33399 const char *p = IDENTIFIER_POINTER (id);
33401 if (strcmp ("in", p) == 0)
33402 kind = OMP_CLAUSE_DEPEND_IN;
33403 else if (strcmp ("inout", p) == 0)
33404 kind = OMP_CLAUSE_DEPEND_INOUT;
33405 else if (strcmp ("out", p) == 0)
33406 kind = OMP_CLAUSE_DEPEND_OUT;
33407 else if (strcmp ("source", p) == 0)
33408 kind = OMP_CLAUSE_DEPEND_SOURCE;
33409 else if (strcmp ("sink", p) == 0)
33410 kind = OMP_CLAUSE_DEPEND_SINK;
33411 else
33412 goto invalid_kind;
33414 else
33415 goto invalid_kind;
33417 cp_lexer_consume_token (parser->lexer);
33419 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33421 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33422 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33423 OMP_CLAUSE_DECL (c) = NULL_TREE;
33424 OMP_CLAUSE_CHAIN (c) = list;
33425 if (!parens.require_close (parser))
33426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33427 /*or_comma=*/false,
33428 /*consume_paren=*/true);
33429 return c;
33432 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33433 goto resync_fail;
33435 if (kind == OMP_CLAUSE_DEPEND_SINK)
33436 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33437 else
33439 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33440 list, NULL);
33442 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33443 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33445 return nlist;
33447 invalid_kind:
33448 cp_parser_error (parser, "invalid depend kind");
33449 resync_fail:
33450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33451 /*or_comma=*/false,
33452 /*consume_paren=*/true);
33453 return list;
33456 /* OpenMP 4.0:
33457 map ( map-kind : variable-list )
33458 map ( variable-list )
33460 map-kind:
33461 alloc | to | from | tofrom
33463 OpenMP 4.5:
33464 map-kind:
33465 alloc | to | from | tofrom | release | delete
33467 map ( always [,] map-kind: variable-list ) */
33469 static tree
33470 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33472 tree nlist, c;
33473 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33474 bool always = false;
33476 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33477 return list;
33479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33482 const char *p = IDENTIFIER_POINTER (id);
33484 if (strcmp ("always", p) == 0)
33486 int nth = 2;
33487 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33488 nth++;
33489 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33490 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33491 == RID_DELETE))
33492 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33493 == CPP_COLON))
33495 always = true;
33496 cp_lexer_consume_token (parser->lexer);
33497 if (nth == 3)
33498 cp_lexer_consume_token (parser->lexer);
33503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33504 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33507 const char *p = IDENTIFIER_POINTER (id);
33509 if (strcmp ("alloc", p) == 0)
33510 kind = GOMP_MAP_ALLOC;
33511 else if (strcmp ("to", p) == 0)
33512 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33513 else if (strcmp ("from", p) == 0)
33514 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33515 else if (strcmp ("tofrom", p) == 0)
33516 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33517 else if (strcmp ("release", p) == 0)
33518 kind = GOMP_MAP_RELEASE;
33519 else
33521 cp_parser_error (parser, "invalid map kind");
33522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33523 /*or_comma=*/false,
33524 /*consume_paren=*/true);
33525 return list;
33527 cp_lexer_consume_token (parser->lexer);
33528 cp_lexer_consume_token (parser->lexer);
33530 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33533 kind = GOMP_MAP_DELETE;
33534 cp_lexer_consume_token (parser->lexer);
33535 cp_lexer_consume_token (parser->lexer);
33538 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33539 NULL);
33541 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33542 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33544 return nlist;
33547 /* OpenMP 4.0:
33548 device ( expression ) */
33550 static tree
33551 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33552 location_t location)
33554 tree t, c;
33556 matching_parens parens;
33557 if (!parens.require_open (parser))
33558 return list;
33560 t = cp_parser_expression (parser);
33562 if (t == error_mark_node
33563 || !parens.require_close (parser))
33564 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33565 /*or_comma=*/false,
33566 /*consume_paren=*/true);
33568 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33569 "device", location);
33571 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33572 OMP_CLAUSE_DEVICE_ID (c) = t;
33573 OMP_CLAUSE_CHAIN (c) = list;
33575 return c;
33578 /* OpenMP 4.0:
33579 dist_schedule ( static )
33580 dist_schedule ( static , expression ) */
33582 static tree
33583 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33584 location_t location)
33586 tree c, t;
33588 matching_parens parens;
33589 if (!parens.require_open (parser))
33590 return list;
33592 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33594 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33595 goto invalid_kind;
33596 cp_lexer_consume_token (parser->lexer);
33598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33600 cp_lexer_consume_token (parser->lexer);
33602 t = cp_parser_assignment_expression (parser);
33604 if (t == error_mark_node)
33605 goto resync_fail;
33606 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33608 if (!parens.require_close (parser))
33609 goto resync_fail;
33611 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33612 goto resync_fail;
33614 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33615 location);
33616 OMP_CLAUSE_CHAIN (c) = list;
33617 return c;
33619 invalid_kind:
33620 cp_parser_error (parser, "invalid dist_schedule kind");
33621 resync_fail:
33622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33623 /*or_comma=*/false,
33624 /*consume_paren=*/true);
33625 return list;
33628 /* OpenMP 4.0:
33629 proc_bind ( proc-bind-kind )
33631 proc-bind-kind:
33632 master | close | spread */
33634 static tree
33635 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33636 location_t location)
33638 tree c;
33639 enum omp_clause_proc_bind_kind kind;
33641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33642 return list;
33644 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33646 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33647 const char *p = IDENTIFIER_POINTER (id);
33649 if (strcmp ("master", p) == 0)
33650 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33651 else if (strcmp ("close", p) == 0)
33652 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33653 else if (strcmp ("spread", p) == 0)
33654 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33655 else
33656 goto invalid_kind;
33658 else
33659 goto invalid_kind;
33661 cp_lexer_consume_token (parser->lexer);
33662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33663 goto resync_fail;
33665 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33666 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33667 location);
33668 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33669 OMP_CLAUSE_CHAIN (c) = list;
33670 return c;
33672 invalid_kind:
33673 cp_parser_error (parser, "invalid depend kind");
33674 resync_fail:
33675 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33676 /*or_comma=*/false,
33677 /*consume_paren=*/true);
33678 return list;
33681 /* OpenACC:
33682 async [( int-expr )] */
33684 static tree
33685 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33687 tree c, t;
33688 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33690 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33692 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33694 matching_parens parens;
33695 parens.consume_open (parser);
33697 t = cp_parser_expression (parser);
33698 if (t == error_mark_node
33699 || !parens.require_close (parser))
33700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33701 /*or_comma=*/false,
33702 /*consume_paren=*/true);
33705 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33707 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33708 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33709 OMP_CLAUSE_CHAIN (c) = list;
33710 list = c;
33712 return list;
33715 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33716 is a bitmask in MASK. Return the list of clauses found. */
33718 static tree
33719 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33720 const char *where, cp_token *pragma_tok,
33721 bool finish_p = true)
33723 tree clauses = NULL;
33724 bool first = true;
33726 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33728 location_t here;
33729 pragma_omp_clause c_kind;
33730 omp_clause_code code;
33731 const char *c_name;
33732 tree prev = clauses;
33734 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33735 cp_lexer_consume_token (parser->lexer);
33737 here = cp_lexer_peek_token (parser->lexer)->location;
33738 c_kind = cp_parser_omp_clause_name (parser);
33740 switch (c_kind)
33742 case PRAGMA_OACC_CLAUSE_ASYNC:
33743 clauses = cp_parser_oacc_clause_async (parser, clauses);
33744 c_name = "async";
33745 break;
33746 case PRAGMA_OACC_CLAUSE_AUTO:
33747 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33748 clauses, here);
33749 c_name = "auto";
33750 break;
33751 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33752 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33753 c_name = "collapse";
33754 break;
33755 case PRAGMA_OACC_CLAUSE_COPY:
33756 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33757 c_name = "copy";
33758 break;
33759 case PRAGMA_OACC_CLAUSE_COPYIN:
33760 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33761 c_name = "copyin";
33762 break;
33763 case PRAGMA_OACC_CLAUSE_COPYOUT:
33764 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33765 c_name = "copyout";
33766 break;
33767 case PRAGMA_OACC_CLAUSE_CREATE:
33768 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33769 c_name = "create";
33770 break;
33771 case PRAGMA_OACC_CLAUSE_DELETE:
33772 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33773 c_name = "delete";
33774 break;
33775 case PRAGMA_OMP_CLAUSE_DEFAULT:
33776 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33777 c_name = "default";
33778 break;
33779 case PRAGMA_OACC_CLAUSE_DEVICE:
33780 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33781 c_name = "device";
33782 break;
33783 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33784 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33785 c_name = "deviceptr";
33786 break;
33787 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33788 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33789 c_name = "device_resident";
33790 break;
33791 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33793 clauses);
33794 c_name = "firstprivate";
33795 break;
33796 case PRAGMA_OACC_CLAUSE_GANG:
33797 c_name = "gang";
33798 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33799 c_name, clauses);
33800 break;
33801 case PRAGMA_OACC_CLAUSE_HOST:
33802 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33803 c_name = "host";
33804 break;
33805 case PRAGMA_OACC_CLAUSE_IF:
33806 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33807 c_name = "if";
33808 break;
33809 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33810 clauses = cp_parser_oacc_simple_clause (parser,
33811 OMP_CLAUSE_INDEPENDENT,
33812 clauses, here);
33813 c_name = "independent";
33814 break;
33815 case PRAGMA_OACC_CLAUSE_LINK:
33816 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33817 c_name = "link";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33820 code = OMP_CLAUSE_NUM_GANGS;
33821 c_name = "num_gangs";
33822 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33823 clauses);
33824 break;
33825 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33826 c_name = "num_workers";
33827 code = OMP_CLAUSE_NUM_WORKERS;
33828 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33829 clauses);
33830 break;
33831 case PRAGMA_OACC_CLAUSE_PRESENT:
33832 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33833 c_name = "present";
33834 break;
33835 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33836 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33837 c_name = "present_or_copy";
33838 break;
33839 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33840 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33841 c_name = "present_or_copyin";
33842 break;
33843 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33844 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33845 c_name = "present_or_copyout";
33846 break;
33847 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33848 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33849 c_name = "present_or_create";
33850 break;
33851 case PRAGMA_OACC_CLAUSE_PRIVATE:
33852 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33853 clauses);
33854 c_name = "private";
33855 break;
33856 case PRAGMA_OACC_CLAUSE_REDUCTION:
33857 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33858 c_name = "reduction";
33859 break;
33860 case PRAGMA_OACC_CLAUSE_SELF:
33861 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33862 c_name = "self";
33863 break;
33864 case PRAGMA_OACC_CLAUSE_SEQ:
33865 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33866 clauses, here);
33867 c_name = "seq";
33868 break;
33869 case PRAGMA_OACC_CLAUSE_TILE:
33870 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33871 c_name = "tile";
33872 break;
33873 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33874 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33875 clauses);
33876 c_name = "use_device";
33877 break;
33878 case PRAGMA_OACC_CLAUSE_VECTOR:
33879 c_name = "vector";
33880 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33881 c_name, clauses);
33882 break;
33883 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33884 c_name = "vector_length";
33885 code = OMP_CLAUSE_VECTOR_LENGTH;
33886 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33887 clauses);
33888 break;
33889 case PRAGMA_OACC_CLAUSE_WAIT:
33890 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33891 c_name = "wait";
33892 break;
33893 case PRAGMA_OACC_CLAUSE_WORKER:
33894 c_name = "worker";
33895 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33896 c_name, clauses);
33897 break;
33898 default:
33899 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33900 goto saw_error;
33903 first = false;
33905 if (((mask >> c_kind) & 1) == 0)
33907 /* Remove the invalid clause(s) from the list to avoid
33908 confusing the rest of the compiler. */
33909 clauses = prev;
33910 error_at (here, "%qs is not valid for %qs", c_name, where);
33914 saw_error:
33915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33917 if (finish_p)
33918 return finish_omp_clauses (clauses, C_ORT_ACC);
33920 return clauses;
33923 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33924 is a bitmask in MASK. Return the list of clauses found; the result
33925 of clause default goes in *pdefault. */
33927 static tree
33928 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33929 const char *where, cp_token *pragma_tok,
33930 bool finish_p = true)
33932 tree clauses = NULL;
33933 bool first = true;
33934 cp_token *token = NULL;
33936 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33938 pragma_omp_clause c_kind;
33939 const char *c_name;
33940 tree prev = clauses;
33942 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33943 cp_lexer_consume_token (parser->lexer);
33945 token = cp_lexer_peek_token (parser->lexer);
33946 c_kind = cp_parser_omp_clause_name (parser);
33948 switch (c_kind)
33950 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33951 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33952 token->location);
33953 c_name = "collapse";
33954 break;
33955 case PRAGMA_OMP_CLAUSE_COPYIN:
33956 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33957 c_name = "copyin";
33958 break;
33959 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33960 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33961 clauses);
33962 c_name = "copyprivate";
33963 break;
33964 case PRAGMA_OMP_CLAUSE_DEFAULT:
33965 clauses = cp_parser_omp_clause_default (parser, clauses,
33966 token->location, false);
33967 c_name = "default";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_FINAL:
33970 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33971 c_name = "final";
33972 break;
33973 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33974 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33975 clauses);
33976 c_name = "firstprivate";
33977 break;
33978 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33979 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33980 token->location);
33981 c_name = "grainsize";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_HINT:
33984 clauses = cp_parser_omp_clause_hint (parser, clauses,
33985 token->location);
33986 c_name = "hint";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33989 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33990 token->location);
33991 c_name = "defaultmap";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33994 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33995 clauses);
33996 c_name = "use_device_ptr";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33999 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34000 clauses);
34001 c_name = "is_device_ptr";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_IF:
34004 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34005 true);
34006 c_name = "if";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34009 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34010 clauses);
34011 c_name = "lastprivate";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34014 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34015 token->location);
34016 c_name = "mergeable";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_NOWAIT:
34019 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34020 c_name = "nowait";
34021 break;
34022 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34023 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34024 token->location);
34025 c_name = "num_tasks";
34026 break;
34027 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34028 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34029 token->location);
34030 c_name = "num_threads";
34031 break;
34032 case PRAGMA_OMP_CLAUSE_ORDERED:
34033 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34034 token->location);
34035 c_name = "ordered";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_PRIORITY:
34038 clauses = cp_parser_omp_clause_priority (parser, clauses,
34039 token->location);
34040 c_name = "priority";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_PRIVATE:
34043 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34044 clauses);
34045 c_name = "private";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_REDUCTION:
34048 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34049 c_name = "reduction";
34050 break;
34051 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34052 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34053 token->location);
34054 c_name = "schedule";
34055 break;
34056 case PRAGMA_OMP_CLAUSE_SHARED:
34057 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34058 clauses);
34059 c_name = "shared";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_UNTIED:
34062 clauses = cp_parser_omp_clause_untied (parser, clauses,
34063 token->location);
34064 c_name = "untied";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_INBRANCH:
34067 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34068 clauses, token->location);
34069 c_name = "inbranch";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34072 clauses = cp_parser_omp_clause_branch (parser,
34073 OMP_CLAUSE_NOTINBRANCH,
34074 clauses, token->location);
34075 c_name = "notinbranch";
34076 break;
34077 case PRAGMA_OMP_CLAUSE_PARALLEL:
34078 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34079 clauses, token->location);
34080 c_name = "parallel";
34081 if (!first)
34083 clause_not_first:
34084 error_at (token->location, "%qs must be the first clause of %qs",
34085 c_name, where);
34086 clauses = prev;
34088 break;
34089 case PRAGMA_OMP_CLAUSE_FOR:
34090 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34091 clauses, token->location);
34092 c_name = "for";
34093 if (!first)
34094 goto clause_not_first;
34095 break;
34096 case PRAGMA_OMP_CLAUSE_SECTIONS:
34097 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34098 clauses, token->location);
34099 c_name = "sections";
34100 if (!first)
34101 goto clause_not_first;
34102 break;
34103 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34104 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34105 clauses, token->location);
34106 c_name = "taskgroup";
34107 if (!first)
34108 goto clause_not_first;
34109 break;
34110 case PRAGMA_OMP_CLAUSE_LINK:
34111 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34112 c_name = "to";
34113 break;
34114 case PRAGMA_OMP_CLAUSE_TO:
34115 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34116 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34117 clauses);
34118 else
34119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34120 c_name = "to";
34121 break;
34122 case PRAGMA_OMP_CLAUSE_FROM:
34123 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34124 c_name = "from";
34125 break;
34126 case PRAGMA_OMP_CLAUSE_UNIFORM:
34127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34128 clauses);
34129 c_name = "uniform";
34130 break;
34131 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34132 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34133 token->location);
34134 c_name = "num_teams";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34137 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34138 token->location);
34139 c_name = "thread_limit";
34140 break;
34141 case PRAGMA_OMP_CLAUSE_ALIGNED:
34142 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34143 c_name = "aligned";
34144 break;
34145 case PRAGMA_OMP_CLAUSE_LINEAR:
34147 bool declare_simd = false;
34148 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34149 declare_simd = true;
34150 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34152 c_name = "linear";
34153 break;
34154 case PRAGMA_OMP_CLAUSE_DEPEND:
34155 clauses = cp_parser_omp_clause_depend (parser, clauses,
34156 token->location);
34157 c_name = "depend";
34158 break;
34159 case PRAGMA_OMP_CLAUSE_MAP:
34160 clauses = cp_parser_omp_clause_map (parser, clauses);
34161 c_name = "map";
34162 break;
34163 case PRAGMA_OMP_CLAUSE_DEVICE:
34164 clauses = cp_parser_omp_clause_device (parser, clauses,
34165 token->location);
34166 c_name = "device";
34167 break;
34168 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34169 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34170 token->location);
34171 c_name = "dist_schedule";
34172 break;
34173 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34174 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34175 token->location);
34176 c_name = "proc_bind";
34177 break;
34178 case PRAGMA_OMP_CLAUSE_SAFELEN:
34179 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34180 token->location);
34181 c_name = "safelen";
34182 break;
34183 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34184 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34185 token->location);
34186 c_name = "simdlen";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_NOGROUP:
34189 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34190 token->location);
34191 c_name = "nogroup";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_THREADS:
34194 clauses
34195 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34196 clauses, token->location);
34197 c_name = "threads";
34198 break;
34199 case PRAGMA_OMP_CLAUSE_SIMD:
34200 clauses
34201 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34202 clauses, token->location);
34203 c_name = "simd";
34204 break;
34205 default:
34206 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34207 goto saw_error;
34210 first = false;
34212 if (((mask >> c_kind) & 1) == 0)
34214 /* Remove the invalid clause(s) from the list to avoid
34215 confusing the rest of the compiler. */
34216 clauses = prev;
34217 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34220 saw_error:
34221 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34222 if (finish_p)
34224 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34225 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34226 else
34227 return finish_omp_clauses (clauses, C_ORT_OMP);
34229 return clauses;
34232 /* OpenMP 2.5:
34233 structured-block:
34234 statement
34236 In practice, we're also interested in adding the statement to an
34237 outer node. So it is convenient if we work around the fact that
34238 cp_parser_statement calls add_stmt. */
34240 static unsigned
34241 cp_parser_begin_omp_structured_block (cp_parser *parser)
34243 unsigned save = parser->in_statement;
34245 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34246 This preserves the "not within loop or switch" style error messages
34247 for nonsense cases like
34248 void foo() {
34249 #pragma omp single
34250 break;
34253 if (parser->in_statement)
34254 parser->in_statement = IN_OMP_BLOCK;
34256 return save;
34259 static void
34260 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34262 parser->in_statement = save;
34265 static tree
34266 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34268 tree stmt = begin_omp_structured_block ();
34269 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34271 cp_parser_statement (parser, NULL_TREE, false, if_p);
34273 cp_parser_end_omp_structured_block (parser, save);
34274 return finish_omp_structured_block (stmt);
34277 /* OpenMP 2.5:
34278 # pragma omp atomic new-line
34279 expression-stmt
34281 expression-stmt:
34282 x binop= expr | x++ | ++x | x-- | --x
34283 binop:
34284 +, *, -, /, &, ^, |, <<, >>
34286 where x is an lvalue expression with scalar type.
34288 OpenMP 3.1:
34289 # pragma omp atomic new-line
34290 update-stmt
34292 # pragma omp atomic read new-line
34293 read-stmt
34295 # pragma omp atomic write new-line
34296 write-stmt
34298 # pragma omp atomic update new-line
34299 update-stmt
34301 # pragma omp atomic capture new-line
34302 capture-stmt
34304 # pragma omp atomic capture new-line
34305 capture-block
34307 read-stmt:
34308 v = x
34309 write-stmt:
34310 x = expr
34311 update-stmt:
34312 expression-stmt | x = x binop expr
34313 capture-stmt:
34314 v = expression-stmt
34315 capture-block:
34316 { v = x; update-stmt; } | { update-stmt; v = x; }
34318 OpenMP 4.0:
34319 update-stmt:
34320 expression-stmt | x = x binop expr | x = expr binop x
34321 capture-stmt:
34322 v = update-stmt
34323 capture-block:
34324 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34326 where x and v are lvalue expressions with scalar type. */
34328 static void
34329 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34331 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34332 tree rhs1 = NULL_TREE, orig_lhs;
34333 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34334 bool structured_block = false;
34335 bool seq_cst = false;
34337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34339 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34340 const char *p = IDENTIFIER_POINTER (id);
34342 if (!strcmp (p, "seq_cst"))
34344 seq_cst = true;
34345 cp_lexer_consume_token (parser->lexer);
34346 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34347 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34348 cp_lexer_consume_token (parser->lexer);
34351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34354 const char *p = IDENTIFIER_POINTER (id);
34356 if (!strcmp (p, "read"))
34357 code = OMP_ATOMIC_READ;
34358 else if (!strcmp (p, "write"))
34359 code = NOP_EXPR;
34360 else if (!strcmp (p, "update"))
34361 code = OMP_ATOMIC;
34362 else if (!strcmp (p, "capture"))
34363 code = OMP_ATOMIC_CAPTURE_NEW;
34364 else
34365 p = NULL;
34366 if (p)
34367 cp_lexer_consume_token (parser->lexer);
34369 if (!seq_cst)
34371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34372 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34373 cp_lexer_consume_token (parser->lexer);
34375 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34377 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34378 const char *p = IDENTIFIER_POINTER (id);
34380 if (!strcmp (p, "seq_cst"))
34382 seq_cst = true;
34383 cp_lexer_consume_token (parser->lexer);
34387 cp_parser_require_pragma_eol (parser, pragma_tok);
34389 switch (code)
34391 case OMP_ATOMIC_READ:
34392 case NOP_EXPR: /* atomic write */
34393 v = cp_parser_unary_expression (parser);
34394 if (v == error_mark_node)
34395 goto saw_error;
34396 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34397 goto saw_error;
34398 if (code == NOP_EXPR)
34399 lhs = cp_parser_expression (parser);
34400 else
34401 lhs = cp_parser_unary_expression (parser);
34402 if (lhs == error_mark_node)
34403 goto saw_error;
34404 if (code == NOP_EXPR)
34406 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34407 opcode. */
34408 code = OMP_ATOMIC;
34409 rhs = lhs;
34410 lhs = v;
34411 v = NULL_TREE;
34413 goto done;
34414 case OMP_ATOMIC_CAPTURE_NEW:
34415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34417 cp_lexer_consume_token (parser->lexer);
34418 structured_block = true;
34420 else
34422 v = cp_parser_unary_expression (parser);
34423 if (v == error_mark_node)
34424 goto saw_error;
34425 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34426 goto saw_error;
34428 default:
34429 break;
34432 restart:
34433 lhs = cp_parser_unary_expression (parser);
34434 orig_lhs = lhs;
34435 switch (TREE_CODE (lhs))
34437 case ERROR_MARK:
34438 goto saw_error;
34440 case POSTINCREMENT_EXPR:
34441 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34442 code = OMP_ATOMIC_CAPTURE_OLD;
34443 /* FALLTHROUGH */
34444 case PREINCREMENT_EXPR:
34445 lhs = TREE_OPERAND (lhs, 0);
34446 opcode = PLUS_EXPR;
34447 rhs = integer_one_node;
34448 break;
34450 case POSTDECREMENT_EXPR:
34451 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34452 code = OMP_ATOMIC_CAPTURE_OLD;
34453 /* FALLTHROUGH */
34454 case PREDECREMENT_EXPR:
34455 lhs = TREE_OPERAND (lhs, 0);
34456 opcode = MINUS_EXPR;
34457 rhs = integer_one_node;
34458 break;
34460 case COMPOUND_EXPR:
34461 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34462 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34463 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34464 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34465 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34466 (TREE_OPERAND (lhs, 1), 0), 0)))
34467 == BOOLEAN_TYPE)
34468 /* Undo effects of boolean_increment for post {in,de}crement. */
34469 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34470 /* FALLTHRU */
34471 case MODIFY_EXPR:
34472 if (TREE_CODE (lhs) == MODIFY_EXPR
34473 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34475 /* Undo effects of boolean_increment. */
34476 if (integer_onep (TREE_OPERAND (lhs, 1)))
34478 /* This is pre or post increment. */
34479 rhs = TREE_OPERAND (lhs, 1);
34480 lhs = TREE_OPERAND (lhs, 0);
34481 opcode = NOP_EXPR;
34482 if (code == OMP_ATOMIC_CAPTURE_NEW
34483 && !structured_block
34484 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34485 code = OMP_ATOMIC_CAPTURE_OLD;
34486 break;
34489 /* FALLTHRU */
34490 default:
34491 switch (cp_lexer_peek_token (parser->lexer)->type)
34493 case CPP_MULT_EQ:
34494 opcode = MULT_EXPR;
34495 break;
34496 case CPP_DIV_EQ:
34497 opcode = TRUNC_DIV_EXPR;
34498 break;
34499 case CPP_PLUS_EQ:
34500 opcode = PLUS_EXPR;
34501 break;
34502 case CPP_MINUS_EQ:
34503 opcode = MINUS_EXPR;
34504 break;
34505 case CPP_LSHIFT_EQ:
34506 opcode = LSHIFT_EXPR;
34507 break;
34508 case CPP_RSHIFT_EQ:
34509 opcode = RSHIFT_EXPR;
34510 break;
34511 case CPP_AND_EQ:
34512 opcode = BIT_AND_EXPR;
34513 break;
34514 case CPP_OR_EQ:
34515 opcode = BIT_IOR_EXPR;
34516 break;
34517 case CPP_XOR_EQ:
34518 opcode = BIT_XOR_EXPR;
34519 break;
34520 case CPP_EQ:
34521 enum cp_parser_prec oprec;
34522 cp_token *token;
34523 cp_lexer_consume_token (parser->lexer);
34524 cp_parser_parse_tentatively (parser);
34525 rhs1 = cp_parser_simple_cast_expression (parser);
34526 if (rhs1 == error_mark_node)
34528 cp_parser_abort_tentative_parse (parser);
34529 cp_parser_simple_cast_expression (parser);
34530 goto saw_error;
34532 token = cp_lexer_peek_token (parser->lexer);
34533 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34535 cp_parser_abort_tentative_parse (parser);
34536 cp_parser_parse_tentatively (parser);
34537 rhs = cp_parser_binary_expression (parser, false, true,
34538 PREC_NOT_OPERATOR, NULL);
34539 if (rhs == error_mark_node)
34541 cp_parser_abort_tentative_parse (parser);
34542 cp_parser_binary_expression (parser, false, true,
34543 PREC_NOT_OPERATOR, NULL);
34544 goto saw_error;
34546 switch (TREE_CODE (rhs))
34548 case MULT_EXPR:
34549 case TRUNC_DIV_EXPR:
34550 case RDIV_EXPR:
34551 case PLUS_EXPR:
34552 case MINUS_EXPR:
34553 case LSHIFT_EXPR:
34554 case RSHIFT_EXPR:
34555 case BIT_AND_EXPR:
34556 case BIT_IOR_EXPR:
34557 case BIT_XOR_EXPR:
34558 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34560 if (cp_parser_parse_definitely (parser))
34562 opcode = TREE_CODE (rhs);
34563 rhs1 = TREE_OPERAND (rhs, 0);
34564 rhs = TREE_OPERAND (rhs, 1);
34565 goto stmt_done;
34567 else
34568 goto saw_error;
34570 break;
34571 default:
34572 break;
34574 cp_parser_abort_tentative_parse (parser);
34575 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34577 rhs = cp_parser_expression (parser);
34578 if (rhs == error_mark_node)
34579 goto saw_error;
34580 opcode = NOP_EXPR;
34581 rhs1 = NULL_TREE;
34582 goto stmt_done;
34584 cp_parser_error (parser,
34585 "invalid form of %<#pragma omp atomic%>");
34586 goto saw_error;
34588 if (!cp_parser_parse_definitely (parser))
34589 goto saw_error;
34590 switch (token->type)
34592 case CPP_SEMICOLON:
34593 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34595 code = OMP_ATOMIC_CAPTURE_OLD;
34596 v = lhs;
34597 lhs = NULL_TREE;
34598 lhs1 = rhs1;
34599 rhs1 = NULL_TREE;
34600 cp_lexer_consume_token (parser->lexer);
34601 goto restart;
34603 else if (structured_block)
34605 opcode = NOP_EXPR;
34606 rhs = rhs1;
34607 rhs1 = NULL_TREE;
34608 goto stmt_done;
34610 cp_parser_error (parser,
34611 "invalid form of %<#pragma omp atomic%>");
34612 goto saw_error;
34613 case CPP_MULT:
34614 opcode = MULT_EXPR;
34615 break;
34616 case CPP_DIV:
34617 opcode = TRUNC_DIV_EXPR;
34618 break;
34619 case CPP_PLUS:
34620 opcode = PLUS_EXPR;
34621 break;
34622 case CPP_MINUS:
34623 opcode = MINUS_EXPR;
34624 break;
34625 case CPP_LSHIFT:
34626 opcode = LSHIFT_EXPR;
34627 break;
34628 case CPP_RSHIFT:
34629 opcode = RSHIFT_EXPR;
34630 break;
34631 case CPP_AND:
34632 opcode = BIT_AND_EXPR;
34633 break;
34634 case CPP_OR:
34635 opcode = BIT_IOR_EXPR;
34636 break;
34637 case CPP_XOR:
34638 opcode = BIT_XOR_EXPR;
34639 break;
34640 default:
34641 cp_parser_error (parser,
34642 "invalid operator for %<#pragma omp atomic%>");
34643 goto saw_error;
34645 oprec = TOKEN_PRECEDENCE (token);
34646 gcc_assert (oprec != PREC_NOT_OPERATOR);
34647 if (commutative_tree_code (opcode))
34648 oprec = (enum cp_parser_prec) (oprec - 1);
34649 cp_lexer_consume_token (parser->lexer);
34650 rhs = cp_parser_binary_expression (parser, false, false,
34651 oprec, NULL);
34652 if (rhs == error_mark_node)
34653 goto saw_error;
34654 goto stmt_done;
34655 /* FALLTHROUGH */
34656 default:
34657 cp_parser_error (parser,
34658 "invalid operator for %<#pragma omp atomic%>");
34659 goto saw_error;
34661 cp_lexer_consume_token (parser->lexer);
34663 rhs = cp_parser_expression (parser);
34664 if (rhs == error_mark_node)
34665 goto saw_error;
34666 break;
34668 stmt_done:
34669 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34671 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34672 goto saw_error;
34673 v = cp_parser_unary_expression (parser);
34674 if (v == error_mark_node)
34675 goto saw_error;
34676 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34677 goto saw_error;
34678 lhs1 = cp_parser_unary_expression (parser);
34679 if (lhs1 == error_mark_node)
34680 goto saw_error;
34682 if (structured_block)
34684 cp_parser_consume_semicolon_at_end_of_statement (parser);
34685 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34687 done:
34688 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34689 if (!structured_block)
34690 cp_parser_consume_semicolon_at_end_of_statement (parser);
34691 return;
34693 saw_error:
34694 cp_parser_skip_to_end_of_block_or_statement (parser);
34695 if (structured_block)
34697 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34698 cp_lexer_consume_token (parser->lexer);
34699 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34701 cp_parser_skip_to_end_of_block_or_statement (parser);
34702 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34703 cp_lexer_consume_token (parser->lexer);
34709 /* OpenMP 2.5:
34710 # pragma omp barrier new-line */
34712 static void
34713 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34715 cp_parser_require_pragma_eol (parser, pragma_tok);
34716 finish_omp_barrier ();
34719 /* OpenMP 2.5:
34720 # pragma omp critical [(name)] new-line
34721 structured-block
34723 OpenMP 4.5:
34724 # pragma omp critical [(name) [hint(expression)]] new-line
34725 structured-block */
34727 #define OMP_CRITICAL_CLAUSE_MASK \
34728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34730 static tree
34731 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34733 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34737 matching_parens parens;
34738 parens.consume_open (parser);
34740 name = cp_parser_identifier (parser);
34742 if (name == error_mark_node
34743 || !parens.require_close (parser))
34744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34745 /*or_comma=*/false,
34746 /*consume_paren=*/true);
34747 if (name == error_mark_node)
34748 name = NULL;
34750 clauses = cp_parser_omp_all_clauses (parser,
34751 OMP_CRITICAL_CLAUSE_MASK,
34752 "#pragma omp critical", pragma_tok);
34754 else
34755 cp_parser_require_pragma_eol (parser, pragma_tok);
34757 stmt = cp_parser_omp_structured_block (parser, if_p);
34758 return c_finish_omp_critical (input_location, stmt, name, clauses);
34761 /* OpenMP 2.5:
34762 # pragma omp flush flush-vars[opt] new-line
34764 flush-vars:
34765 ( variable-list ) */
34767 static void
34768 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34771 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34772 cp_parser_require_pragma_eol (parser, pragma_tok);
34774 finish_omp_flush ();
34777 /* Helper function, to parse omp for increment expression. */
34779 static tree
34780 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34782 tree cond = cp_parser_binary_expression (parser, false, true,
34783 PREC_NOT_OPERATOR, NULL);
34784 if (cond == error_mark_node
34785 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34787 cp_parser_skip_to_end_of_statement (parser);
34788 return error_mark_node;
34791 switch (TREE_CODE (cond))
34793 case GT_EXPR:
34794 case GE_EXPR:
34795 case LT_EXPR:
34796 case LE_EXPR:
34797 break;
34798 case NE_EXPR:
34799 /* Fall through: OpenMP disallows NE_EXPR. */
34800 gcc_fallthrough ();
34801 default:
34802 return error_mark_node;
34805 /* If decl is an iterator, preserve LHS and RHS of the relational
34806 expr until finish_omp_for. */
34807 if (decl
34808 && (type_dependent_expression_p (decl)
34809 || CLASS_TYPE_P (TREE_TYPE (decl))))
34810 return cond;
34812 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34813 TREE_CODE (cond),
34814 TREE_OPERAND (cond, 0), ERROR_MARK,
34815 TREE_OPERAND (cond, 1), ERROR_MARK,
34816 /*overload=*/NULL, tf_warning_or_error);
34819 /* Helper function, to parse omp for increment expression. */
34821 static tree
34822 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34824 cp_token *token = cp_lexer_peek_token (parser->lexer);
34825 enum tree_code op;
34826 tree lhs, rhs;
34827 cp_id_kind idk;
34828 bool decl_first;
34830 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34832 op = (token->type == CPP_PLUS_PLUS
34833 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34834 cp_lexer_consume_token (parser->lexer);
34835 lhs = cp_parser_simple_cast_expression (parser);
34836 if (lhs != decl
34837 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34838 return error_mark_node;
34839 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34842 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34843 if (lhs != decl
34844 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34845 return error_mark_node;
34847 token = cp_lexer_peek_token (parser->lexer);
34848 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34850 op = (token->type == CPP_PLUS_PLUS
34851 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34852 cp_lexer_consume_token (parser->lexer);
34853 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34856 op = cp_parser_assignment_operator_opt (parser);
34857 if (op == ERROR_MARK)
34858 return error_mark_node;
34860 if (op != NOP_EXPR)
34862 rhs = cp_parser_assignment_expression (parser);
34863 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34864 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34867 lhs = cp_parser_binary_expression (parser, false, false,
34868 PREC_ADDITIVE_EXPRESSION, NULL);
34869 token = cp_lexer_peek_token (parser->lexer);
34870 decl_first = (lhs == decl
34871 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34872 if (decl_first)
34873 lhs = NULL_TREE;
34874 if (token->type != CPP_PLUS
34875 && token->type != CPP_MINUS)
34876 return error_mark_node;
34880 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34881 cp_lexer_consume_token (parser->lexer);
34882 rhs = cp_parser_binary_expression (parser, false, false,
34883 PREC_ADDITIVE_EXPRESSION, NULL);
34884 token = cp_lexer_peek_token (parser->lexer);
34885 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34887 if (lhs == NULL_TREE)
34889 if (op == PLUS_EXPR)
34890 lhs = rhs;
34891 else
34892 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34893 tf_warning_or_error);
34895 else
34896 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34897 ERROR_MARK, NULL, tf_warning_or_error);
34900 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34902 if (!decl_first)
34904 if ((rhs != decl
34905 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34906 || op == MINUS_EXPR)
34907 return error_mark_node;
34908 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34910 else
34911 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34913 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34916 /* Parse the initialization statement of an OpenMP for loop.
34918 Return true if the resulting construct should have an
34919 OMP_CLAUSE_PRIVATE added to it. */
34921 static tree
34922 cp_parser_omp_for_loop_init (cp_parser *parser,
34923 tree &this_pre_body,
34924 vec<tree, va_gc> *for_block,
34925 tree &init,
34926 tree &orig_init,
34927 tree &decl,
34928 tree &real_decl)
34930 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34931 return NULL_TREE;
34933 tree add_private_clause = NULL_TREE;
34935 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34937 init-expr:
34938 var = lb
34939 integer-type var = lb
34940 random-access-iterator-type var = lb
34941 pointer-type var = lb
34943 cp_decl_specifier_seq type_specifiers;
34945 /* First, try to parse as an initialized declaration. See
34946 cp_parser_condition, from whence the bulk of this is copied. */
34948 cp_parser_parse_tentatively (parser);
34949 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34950 /*is_trailing_return=*/false,
34951 &type_specifiers);
34952 if (cp_parser_parse_definitely (parser))
34954 /* If parsing a type specifier seq succeeded, then this
34955 MUST be a initialized declaration. */
34956 tree asm_specification, attributes;
34957 cp_declarator *declarator;
34959 declarator = cp_parser_declarator (parser,
34960 CP_PARSER_DECLARATOR_NAMED,
34961 /*ctor_dtor_or_conv_p=*/NULL,
34962 /*parenthesized_p=*/NULL,
34963 /*member_p=*/false,
34964 /*friend_p=*/false);
34965 attributes = cp_parser_attributes_opt (parser);
34966 asm_specification = cp_parser_asm_specification_opt (parser);
34968 if (declarator == cp_error_declarator)
34969 cp_parser_skip_to_end_of_statement (parser);
34971 else
34973 tree pushed_scope, auto_node;
34975 decl = start_decl (declarator, &type_specifiers,
34976 SD_INITIALIZED, attributes,
34977 /*prefix_attributes=*/NULL_TREE,
34978 &pushed_scope);
34980 auto_node = type_uses_auto (TREE_TYPE (decl));
34981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34983 if (cp_lexer_next_token_is (parser->lexer,
34984 CPP_OPEN_PAREN))
34985 error ("parenthesized initialization is not allowed in "
34986 "OpenMP %<for%> loop");
34987 else
34988 /* Trigger an error. */
34989 cp_parser_require (parser, CPP_EQ, RT_EQ);
34991 init = error_mark_node;
34992 cp_parser_skip_to_end_of_statement (parser);
34994 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34995 || type_dependent_expression_p (decl)
34996 || auto_node)
34998 bool is_direct_init, is_non_constant_init;
35000 init = cp_parser_initializer (parser,
35001 &is_direct_init,
35002 &is_non_constant_init);
35004 if (auto_node)
35006 TREE_TYPE (decl)
35007 = do_auto_deduction (TREE_TYPE (decl), init,
35008 auto_node);
35010 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35011 && !type_dependent_expression_p (decl))
35012 goto non_class;
35015 cp_finish_decl (decl, init, !is_non_constant_init,
35016 asm_specification,
35017 LOOKUP_ONLYCONVERTING);
35018 orig_init = init;
35019 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35021 vec_safe_push (for_block, this_pre_body);
35022 init = NULL_TREE;
35024 else
35026 init = pop_stmt_list (this_pre_body);
35027 if (init && TREE_CODE (init) == STATEMENT_LIST)
35029 tree_stmt_iterator i = tsi_start (init);
35030 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35031 while (!tsi_end_p (i))
35033 tree t = tsi_stmt (i);
35034 if (TREE_CODE (t) == DECL_EXPR
35035 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35037 tsi_delink (&i);
35038 vec_safe_push (for_block, t);
35039 continue;
35041 break;
35043 if (tsi_one_before_end_p (i))
35045 tree t = tsi_stmt (i);
35046 tsi_delink (&i);
35047 free_stmt_list (init);
35048 init = t;
35052 this_pre_body = NULL_TREE;
35054 else
35056 /* Consume '='. */
35057 cp_lexer_consume_token (parser->lexer);
35058 init = cp_parser_assignment_expression (parser);
35060 non_class:
35061 if (TYPE_REF_P (TREE_TYPE (decl)))
35062 init = error_mark_node;
35063 else
35064 cp_finish_decl (decl, NULL_TREE,
35065 /*init_const_expr_p=*/false,
35066 asm_specification,
35067 LOOKUP_ONLYCONVERTING);
35070 if (pushed_scope)
35071 pop_scope (pushed_scope);
35074 else
35076 cp_id_kind idk;
35077 /* If parsing a type specifier sequence failed, then
35078 this MUST be a simple expression. */
35079 cp_parser_parse_tentatively (parser);
35080 decl = cp_parser_primary_expression (parser, false, false,
35081 false, &idk);
35082 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35083 if (!cp_parser_error_occurred (parser)
35084 && decl
35085 && (TREE_CODE (decl) == COMPONENT_REF
35086 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35088 cp_parser_abort_tentative_parse (parser);
35089 cp_parser_parse_tentatively (parser);
35090 cp_token *token = cp_lexer_peek_token (parser->lexer);
35091 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35092 /*check_dependency_p=*/true,
35093 /*template_p=*/NULL,
35094 /*declarator_p=*/false,
35095 /*optional_p=*/false);
35096 if (name != error_mark_node
35097 && last_tok == cp_lexer_peek_token (parser->lexer))
35099 decl = cp_parser_lookup_name_simple (parser, name,
35100 token->location);
35101 if (TREE_CODE (decl) == FIELD_DECL)
35102 add_private_clause = omp_privatize_field (decl, false);
35104 cp_parser_abort_tentative_parse (parser);
35105 cp_parser_parse_tentatively (parser);
35106 decl = cp_parser_primary_expression (parser, false, false,
35107 false, &idk);
35109 if (!cp_parser_error_occurred (parser)
35110 && decl
35111 && DECL_P (decl)
35112 && CLASS_TYPE_P (TREE_TYPE (decl)))
35114 tree rhs;
35116 cp_parser_parse_definitely (parser);
35117 cp_parser_require (parser, CPP_EQ, RT_EQ);
35118 rhs = cp_parser_assignment_expression (parser);
35119 orig_init = rhs;
35120 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35121 decl, NOP_EXPR,
35122 rhs,
35123 tf_warning_or_error));
35124 if (!add_private_clause)
35125 add_private_clause = decl;
35127 else
35129 decl = NULL;
35130 cp_parser_abort_tentative_parse (parser);
35131 init = cp_parser_expression (parser);
35132 if (init)
35134 if (TREE_CODE (init) == MODIFY_EXPR
35135 || TREE_CODE (init) == MODOP_EXPR)
35136 real_decl = TREE_OPERAND (init, 0);
35140 return add_private_clause;
35143 /* Parse the restricted form of the for statement allowed by OpenMP. */
35145 static tree
35146 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35147 tree *cclauses, bool *if_p)
35149 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35150 tree real_decl, initv, condv, incrv, declv;
35151 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35152 location_t loc_first;
35153 bool collapse_err = false;
35154 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35155 vec<tree, va_gc> *for_block = make_tree_vector ();
35156 auto_vec<tree, 4> orig_inits;
35157 bool tiling = false;
35159 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35160 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35161 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35162 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35164 tiling = true;
35165 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35167 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35168 && OMP_CLAUSE_ORDERED_EXPR (cl))
35170 ordered_cl = cl;
35171 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35174 if (ordered && ordered < collapse)
35176 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35177 "%<ordered%> clause parameter is less than %<collapse%>");
35178 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35179 = build_int_cst (NULL_TREE, collapse);
35180 ordered = collapse;
35182 if (ordered)
35184 for (tree *pc = &clauses; *pc; )
35185 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35187 error_at (OMP_CLAUSE_LOCATION (*pc),
35188 "%<linear%> clause may not be specified together "
35189 "with %<ordered%> clause with a parameter");
35190 *pc = OMP_CLAUSE_CHAIN (*pc);
35192 else
35193 pc = &OMP_CLAUSE_CHAIN (*pc);
35196 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35197 count = ordered ? ordered : collapse;
35199 declv = make_tree_vec (count);
35200 initv = make_tree_vec (count);
35201 condv = make_tree_vec (count);
35202 incrv = make_tree_vec (count);
35204 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35206 for (i = 0; i < count; i++)
35208 int bracecount = 0;
35209 tree add_private_clause = NULL_TREE;
35210 location_t loc;
35212 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35214 if (!collapse_err)
35215 cp_parser_error (parser, "for statement expected");
35216 return NULL;
35218 loc = cp_lexer_consume_token (parser->lexer)->location;
35220 matching_parens parens;
35221 if (!parens.require_open (parser))
35222 return NULL;
35224 init = orig_init = decl = real_decl = NULL;
35225 this_pre_body = push_stmt_list ();
35227 add_private_clause
35228 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35229 init, orig_init, decl, real_decl);
35231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35232 if (this_pre_body)
35234 this_pre_body = pop_stmt_list (this_pre_body);
35235 if (pre_body)
35237 tree t = pre_body;
35238 pre_body = push_stmt_list ();
35239 add_stmt (t);
35240 add_stmt (this_pre_body);
35241 pre_body = pop_stmt_list (pre_body);
35243 else
35244 pre_body = this_pre_body;
35247 if (decl)
35248 real_decl = decl;
35249 if (cclauses != NULL
35250 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35251 && real_decl != NULL_TREE)
35253 tree *c;
35254 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35255 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35256 && OMP_CLAUSE_DECL (*c) == real_decl)
35258 error_at (loc, "iteration variable %qD"
35259 " should not be firstprivate", real_decl);
35260 *c = OMP_CLAUSE_CHAIN (*c);
35262 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35263 && OMP_CLAUSE_DECL (*c) == real_decl)
35265 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35266 tree l = *c;
35267 *c = OMP_CLAUSE_CHAIN (*c);
35268 if (code == OMP_SIMD)
35270 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35271 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35273 else
35275 OMP_CLAUSE_CHAIN (l) = clauses;
35276 clauses = l;
35278 add_private_clause = NULL_TREE;
35280 else
35282 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35283 && OMP_CLAUSE_DECL (*c) == real_decl)
35284 add_private_clause = NULL_TREE;
35285 c = &OMP_CLAUSE_CHAIN (*c);
35289 if (add_private_clause)
35291 tree c;
35292 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35294 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35295 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35296 && OMP_CLAUSE_DECL (c) == decl)
35297 break;
35298 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35299 && OMP_CLAUSE_DECL (c) == decl)
35300 error_at (loc, "iteration variable %qD "
35301 "should not be firstprivate",
35302 decl);
35303 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35304 && OMP_CLAUSE_DECL (c) == decl)
35305 error_at (loc, "iteration variable %qD should not be reduction",
35306 decl);
35308 if (c == NULL)
35310 if (code != OMP_SIMD)
35311 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35312 else if (collapse == 1)
35313 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35314 else
35315 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35316 OMP_CLAUSE_DECL (c) = add_private_clause;
35317 c = finish_omp_clauses (c, C_ORT_OMP);
35318 if (c)
35320 OMP_CLAUSE_CHAIN (c) = clauses;
35321 clauses = c;
35322 /* For linear, signal that we need to fill up
35323 the so far unknown linear step. */
35324 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35325 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35330 cond = NULL;
35331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35332 cond = cp_parser_omp_for_cond (parser, decl);
35333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35335 incr = NULL;
35336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35338 /* If decl is an iterator, preserve the operator on decl
35339 until finish_omp_for. */
35340 if (real_decl
35341 && ((processing_template_decl
35342 && (TREE_TYPE (real_decl) == NULL_TREE
35343 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35344 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35345 incr = cp_parser_omp_for_incr (parser, real_decl);
35346 else
35347 incr = cp_parser_expression (parser);
35348 if (!EXPR_HAS_LOCATION (incr))
35349 protected_set_expr_location (incr, input_location);
35352 if (!parens.require_close (parser))
35353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35354 /*or_comma=*/false,
35355 /*consume_paren=*/true);
35357 TREE_VEC_ELT (declv, i) = decl;
35358 TREE_VEC_ELT (initv, i) = init;
35359 TREE_VEC_ELT (condv, i) = cond;
35360 TREE_VEC_ELT (incrv, i) = incr;
35361 if (orig_init)
35363 orig_inits.safe_grow_cleared (i + 1);
35364 orig_inits[i] = orig_init;
35367 if (i == count - 1)
35368 break;
35370 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35371 in between the collapsed for loops to be still considered perfectly
35372 nested. Hopefully the final version clarifies this.
35373 For now handle (multiple) {'s and empty statements. */
35374 cp_parser_parse_tentatively (parser);
35375 for (;;)
35377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35378 break;
35379 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35381 cp_lexer_consume_token (parser->lexer);
35382 bracecount++;
35384 else if (bracecount
35385 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35386 cp_lexer_consume_token (parser->lexer);
35387 else
35389 loc = cp_lexer_peek_token (parser->lexer)->location;
35390 error_at (loc, "not enough for loops to collapse");
35391 collapse_err = true;
35392 cp_parser_abort_tentative_parse (parser);
35393 declv = NULL_TREE;
35394 break;
35398 if (declv)
35400 cp_parser_parse_definitely (parser);
35401 nbraces += bracecount;
35405 if (nbraces)
35406 if_p = NULL;
35408 /* Note that we saved the original contents of this flag when we entered
35409 the structured block, and so we don't need to re-save it here. */
35410 parser->in_statement = IN_OMP_FOR;
35412 /* Note that the grammar doesn't call for a structured block here,
35413 though the loop as a whole is a structured block. */
35414 body = push_stmt_list ();
35415 cp_parser_statement (parser, NULL_TREE, false, if_p);
35416 body = pop_stmt_list (body);
35418 if (declv == NULL_TREE)
35419 ret = NULL_TREE;
35420 else
35421 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35422 body, pre_body, &orig_inits, clauses);
35424 while (nbraces)
35426 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35428 cp_lexer_consume_token (parser->lexer);
35429 nbraces--;
35431 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35432 cp_lexer_consume_token (parser->lexer);
35433 else
35435 if (!collapse_err)
35437 error_at (cp_lexer_peek_token (parser->lexer)->location,
35438 "collapsed loops not perfectly nested");
35440 collapse_err = true;
35441 cp_parser_statement_seq_opt (parser, NULL);
35442 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35443 break;
35447 while (!for_block->is_empty ())
35449 tree t = for_block->pop ();
35450 if (TREE_CODE (t) == STATEMENT_LIST)
35451 add_stmt (pop_stmt_list (t));
35452 else
35453 add_stmt (t);
35455 release_tree_vector (for_block);
35457 return ret;
35460 /* Helper function for OpenMP parsing, split clauses and call
35461 finish_omp_clauses on each of the set of clauses afterwards. */
35463 static void
35464 cp_omp_split_clauses (location_t loc, enum tree_code code,
35465 omp_clause_mask mask, tree clauses, tree *cclauses)
35467 int i;
35468 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35469 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35470 if (cclauses[i])
35471 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35474 /* OpenMP 4.0:
35475 #pragma omp simd simd-clause[optseq] new-line
35476 for-loop */
35478 #define OMP_SIMD_CLAUSE_MASK \
35479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35488 static tree
35489 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35490 char *p_name, omp_clause_mask mask, tree *cclauses,
35491 bool *if_p)
35493 tree clauses, sb, ret;
35494 unsigned int save;
35495 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35497 strcat (p_name, " simd");
35498 mask |= OMP_SIMD_CLAUSE_MASK;
35500 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35501 cclauses == NULL);
35502 if (cclauses)
35504 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35505 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35506 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35507 OMP_CLAUSE_ORDERED);
35508 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35510 error_at (OMP_CLAUSE_LOCATION (c),
35511 "%<ordered%> clause with parameter may not be specified "
35512 "on %qs construct", p_name);
35513 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35517 sb = begin_omp_structured_block ();
35518 save = cp_parser_begin_omp_structured_block (parser);
35520 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35522 cp_parser_end_omp_structured_block (parser, save);
35523 add_stmt (finish_omp_structured_block (sb));
35525 return ret;
35528 /* OpenMP 2.5:
35529 #pragma omp for for-clause[optseq] new-line
35530 for-loop
35532 OpenMP 4.0:
35533 #pragma omp for simd for-simd-clause[optseq] new-line
35534 for-loop */
35536 #define OMP_FOR_CLAUSE_MASK \
35537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35547 static tree
35548 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35549 char *p_name, omp_clause_mask mask, tree *cclauses,
35550 bool *if_p)
35552 tree clauses, sb, ret;
35553 unsigned int save;
35554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35556 strcat (p_name, " for");
35557 mask |= OMP_FOR_CLAUSE_MASK;
35558 /* parallel for{, simd} disallows nowait clause, but for
35559 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35560 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35561 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35562 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35563 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35564 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35569 const char *p = IDENTIFIER_POINTER (id);
35571 if (strcmp (p, "simd") == 0)
35573 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35574 if (cclauses == NULL)
35575 cclauses = cclauses_buf;
35577 cp_lexer_consume_token (parser->lexer);
35578 if (!flag_openmp) /* flag_openmp_simd */
35579 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35580 cclauses, if_p);
35581 sb = begin_omp_structured_block ();
35582 save = cp_parser_begin_omp_structured_block (parser);
35583 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35584 cclauses, if_p);
35585 cp_parser_end_omp_structured_block (parser, save);
35586 tree body = finish_omp_structured_block (sb);
35587 if (ret == NULL)
35588 return ret;
35589 ret = make_node (OMP_FOR);
35590 TREE_TYPE (ret) = void_type_node;
35591 OMP_FOR_BODY (ret) = body;
35592 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35593 SET_EXPR_LOCATION (ret, loc);
35594 add_stmt (ret);
35595 return ret;
35598 if (!flag_openmp) /* flag_openmp_simd */
35600 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35601 return NULL_TREE;
35604 /* Composite distribute parallel for disallows linear clause. */
35605 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35606 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35608 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35609 cclauses == NULL);
35610 if (cclauses)
35612 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35613 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35616 sb = begin_omp_structured_block ();
35617 save = cp_parser_begin_omp_structured_block (parser);
35619 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35621 cp_parser_end_omp_structured_block (parser, save);
35622 add_stmt (finish_omp_structured_block (sb));
35624 return ret;
35627 /* OpenMP 2.5:
35628 # pragma omp master new-line
35629 structured-block */
35631 static tree
35632 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35634 cp_parser_require_pragma_eol (parser, pragma_tok);
35635 return c_finish_omp_master (input_location,
35636 cp_parser_omp_structured_block (parser, if_p));
35639 /* OpenMP 2.5:
35640 # pragma omp ordered new-line
35641 structured-block
35643 OpenMP 4.5:
35644 # pragma omp ordered ordered-clauses new-line
35645 structured-block */
35647 #define OMP_ORDERED_CLAUSE_MASK \
35648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35651 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35652 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35654 static bool
35655 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35656 enum pragma_context context, bool *if_p)
35658 location_t loc = pragma_tok->location;
35660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35663 const char *p = IDENTIFIER_POINTER (id);
35665 if (strcmp (p, "depend") == 0)
35667 if (!flag_openmp) /* flag_openmp_simd */
35669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35670 return false;
35672 if (context == pragma_stmt)
35674 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35675 "%<depend%> clause may only be used in compound "
35676 "statements");
35677 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35678 return false;
35680 tree clauses
35681 = cp_parser_omp_all_clauses (parser,
35682 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35683 "#pragma omp ordered", pragma_tok);
35684 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35685 return false;
35689 tree clauses
35690 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35691 "#pragma omp ordered", pragma_tok);
35693 if (!flag_openmp /* flag_openmp_simd */
35694 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35695 return false;
35697 c_finish_omp_ordered (loc, clauses,
35698 cp_parser_omp_structured_block (parser, if_p));
35699 return true;
35702 /* OpenMP 2.5:
35704 section-scope:
35705 { section-sequence }
35707 section-sequence:
35708 section-directive[opt] structured-block
35709 section-sequence section-directive structured-block */
35711 static tree
35712 cp_parser_omp_sections_scope (cp_parser *parser)
35714 tree stmt, substmt;
35715 bool error_suppress = false;
35716 cp_token *tok;
35718 matching_braces braces;
35719 if (!braces.require_open (parser))
35720 return NULL_TREE;
35722 stmt = push_stmt_list ();
35724 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35725 != PRAGMA_OMP_SECTION)
35727 substmt = cp_parser_omp_structured_block (parser, NULL);
35728 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35729 add_stmt (substmt);
35732 while (1)
35734 tok = cp_lexer_peek_token (parser->lexer);
35735 if (tok->type == CPP_CLOSE_BRACE)
35736 break;
35737 if (tok->type == CPP_EOF)
35738 break;
35740 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35742 cp_lexer_consume_token (parser->lexer);
35743 cp_parser_require_pragma_eol (parser, tok);
35744 error_suppress = false;
35746 else if (!error_suppress)
35748 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35749 error_suppress = true;
35752 substmt = cp_parser_omp_structured_block (parser, NULL);
35753 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35754 add_stmt (substmt);
35756 braces.require_close (parser);
35758 substmt = pop_stmt_list (stmt);
35760 stmt = make_node (OMP_SECTIONS);
35761 TREE_TYPE (stmt) = void_type_node;
35762 OMP_SECTIONS_BODY (stmt) = substmt;
35764 add_stmt (stmt);
35765 return stmt;
35768 /* OpenMP 2.5:
35769 # pragma omp sections sections-clause[optseq] newline
35770 sections-scope */
35772 #define OMP_SECTIONS_CLAUSE_MASK \
35773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35779 static tree
35780 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35781 char *p_name, omp_clause_mask mask, tree *cclauses)
35783 tree clauses, ret;
35784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35786 strcat (p_name, " sections");
35787 mask |= OMP_SECTIONS_CLAUSE_MASK;
35788 if (cclauses)
35789 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35791 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35792 cclauses == NULL);
35793 if (cclauses)
35795 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35796 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35799 ret = cp_parser_omp_sections_scope (parser);
35800 if (ret)
35801 OMP_SECTIONS_CLAUSES (ret) = clauses;
35803 return ret;
35806 /* OpenMP 2.5:
35807 # pragma omp parallel parallel-clause[optseq] new-line
35808 structured-block
35809 # pragma omp parallel for parallel-for-clause[optseq] new-line
35810 structured-block
35811 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35812 structured-block
35814 OpenMP 4.0:
35815 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35816 structured-block */
35818 #define OMP_PARALLEL_CLAUSE_MASK \
35819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35829 static tree
35830 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35831 char *p_name, omp_clause_mask mask, tree *cclauses,
35832 bool *if_p)
35834 tree stmt, clauses, block;
35835 unsigned int save;
35836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35838 strcat (p_name, " parallel");
35839 mask |= OMP_PARALLEL_CLAUSE_MASK;
35840 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35841 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35842 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35843 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35847 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35848 if (cclauses == NULL)
35849 cclauses = cclauses_buf;
35851 cp_lexer_consume_token (parser->lexer);
35852 if (!flag_openmp) /* flag_openmp_simd */
35853 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35854 if_p);
35855 block = begin_omp_parallel ();
35856 save = cp_parser_begin_omp_structured_block (parser);
35857 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35858 if_p);
35859 cp_parser_end_omp_structured_block (parser, save);
35860 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35861 block);
35862 if (ret == NULL_TREE)
35863 return ret;
35864 OMP_PARALLEL_COMBINED (stmt) = 1;
35865 return stmt;
35867 /* When combined with distribute, parallel has to be followed by for.
35868 #pragma omp target parallel is allowed though. */
35869 else if (cclauses
35870 && (mask & (OMP_CLAUSE_MASK_1
35871 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35873 error_at (loc, "expected %<for%> after %qs", p_name);
35874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35875 return NULL_TREE;
35877 else if (!flag_openmp) /* flag_openmp_simd */
35879 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35880 return NULL_TREE;
35882 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35885 const char *p = IDENTIFIER_POINTER (id);
35886 if (strcmp (p, "sections") == 0)
35888 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35889 cclauses = cclauses_buf;
35891 cp_lexer_consume_token (parser->lexer);
35892 block = begin_omp_parallel ();
35893 save = cp_parser_begin_omp_structured_block (parser);
35894 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35895 cp_parser_end_omp_structured_block (parser, save);
35896 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35897 block);
35898 OMP_PARALLEL_COMBINED (stmt) = 1;
35899 return stmt;
35903 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35904 cclauses == NULL);
35905 if (cclauses)
35907 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35908 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35911 block = begin_omp_parallel ();
35912 save = cp_parser_begin_omp_structured_block (parser);
35913 cp_parser_statement (parser, NULL_TREE, false, if_p);
35914 cp_parser_end_omp_structured_block (parser, save);
35915 stmt = finish_omp_parallel (clauses, block);
35916 return stmt;
35919 /* OpenMP 2.5:
35920 # pragma omp single single-clause[optseq] new-line
35921 structured-block */
35923 #define OMP_SINGLE_CLAUSE_MASK \
35924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35929 static tree
35930 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35932 tree stmt = make_node (OMP_SINGLE);
35933 TREE_TYPE (stmt) = void_type_node;
35935 OMP_SINGLE_CLAUSES (stmt)
35936 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35937 "#pragma omp single", pragma_tok);
35938 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35940 return add_stmt (stmt);
35943 /* OpenMP 3.0:
35944 # pragma omp task task-clause[optseq] new-line
35945 structured-block */
35947 #define OMP_TASK_CLAUSE_MASK \
35948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35959 static tree
35960 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35962 tree clauses, block;
35963 unsigned int save;
35965 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35966 "#pragma omp task", pragma_tok);
35967 block = begin_omp_task ();
35968 save = cp_parser_begin_omp_structured_block (parser);
35969 cp_parser_statement (parser, NULL_TREE, false, if_p);
35970 cp_parser_end_omp_structured_block (parser, save);
35971 return finish_omp_task (clauses, block);
35974 /* OpenMP 3.0:
35975 # pragma omp taskwait new-line */
35977 static void
35978 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35980 cp_parser_require_pragma_eol (parser, pragma_tok);
35981 finish_omp_taskwait ();
35984 /* OpenMP 3.1:
35985 # pragma omp taskyield new-line */
35987 static void
35988 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35990 cp_parser_require_pragma_eol (parser, pragma_tok);
35991 finish_omp_taskyield ();
35994 /* OpenMP 4.0:
35995 # pragma omp taskgroup new-line
35996 structured-block */
35998 static tree
35999 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36001 cp_parser_require_pragma_eol (parser, pragma_tok);
36002 return c_finish_omp_taskgroup (input_location,
36003 cp_parser_omp_structured_block (parser,
36004 if_p));
36008 /* OpenMP 2.5:
36009 # pragma omp threadprivate (variable-list) */
36011 static void
36012 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36014 tree vars;
36016 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36017 cp_parser_require_pragma_eol (parser, pragma_tok);
36019 finish_omp_threadprivate (vars);
36022 /* OpenMP 4.0:
36023 # pragma omp cancel cancel-clause[optseq] new-line */
36025 #define OMP_CANCEL_CLAUSE_MASK \
36026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36032 static void
36033 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36035 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36036 "#pragma omp cancel", pragma_tok);
36037 finish_omp_cancel (clauses);
36040 /* OpenMP 4.0:
36041 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36043 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36049 static void
36050 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36051 enum pragma_context context)
36053 tree clauses;
36054 bool point_seen = false;
36056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36058 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36059 const char *p = IDENTIFIER_POINTER (id);
36061 if (strcmp (p, "point") == 0)
36063 cp_lexer_consume_token (parser->lexer);
36064 point_seen = true;
36067 if (!point_seen)
36069 cp_parser_error (parser, "expected %<point%>");
36070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36071 return;
36074 if (context != pragma_compound)
36076 if (context == pragma_stmt)
36077 error_at (pragma_tok->location,
36078 "%<#pragma %s%> may only be used in compound statements",
36079 "omp cancellation point");
36080 else
36081 cp_parser_error (parser, "expected declaration specifiers");
36082 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36083 return;
36086 clauses = cp_parser_omp_all_clauses (parser,
36087 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36088 "#pragma omp cancellation point",
36089 pragma_tok);
36090 finish_omp_cancellation_point (clauses);
36093 /* OpenMP 4.0:
36094 #pragma omp distribute distribute-clause[optseq] new-line
36095 for-loop */
36097 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36104 static tree
36105 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36106 char *p_name, omp_clause_mask mask, tree *cclauses,
36107 bool *if_p)
36109 tree clauses, sb, ret;
36110 unsigned int save;
36111 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36113 strcat (p_name, " distribute");
36114 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36116 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36118 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36119 const char *p = IDENTIFIER_POINTER (id);
36120 bool simd = false;
36121 bool parallel = false;
36123 if (strcmp (p, "simd") == 0)
36124 simd = true;
36125 else
36126 parallel = strcmp (p, "parallel") == 0;
36127 if (parallel || simd)
36129 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36130 if (cclauses == NULL)
36131 cclauses = cclauses_buf;
36132 cp_lexer_consume_token (parser->lexer);
36133 if (!flag_openmp) /* flag_openmp_simd */
36135 if (simd)
36136 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36137 cclauses, if_p);
36138 else
36139 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36140 cclauses, if_p);
36142 sb = begin_omp_structured_block ();
36143 save = cp_parser_begin_omp_structured_block (parser);
36144 if (simd)
36145 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36146 cclauses, if_p);
36147 else
36148 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36149 cclauses, if_p);
36150 cp_parser_end_omp_structured_block (parser, save);
36151 tree body = finish_omp_structured_block (sb);
36152 if (ret == NULL)
36153 return ret;
36154 ret = make_node (OMP_DISTRIBUTE);
36155 TREE_TYPE (ret) = void_type_node;
36156 OMP_FOR_BODY (ret) = body;
36157 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36158 SET_EXPR_LOCATION (ret, loc);
36159 add_stmt (ret);
36160 return ret;
36163 if (!flag_openmp) /* flag_openmp_simd */
36165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36166 return NULL_TREE;
36169 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36170 cclauses == NULL);
36171 if (cclauses)
36173 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36174 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36177 sb = begin_omp_structured_block ();
36178 save = cp_parser_begin_omp_structured_block (parser);
36180 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36182 cp_parser_end_omp_structured_block (parser, save);
36183 add_stmt (finish_omp_structured_block (sb));
36185 return ret;
36188 /* OpenMP 4.0:
36189 # pragma omp teams teams-clause[optseq] new-line
36190 structured-block */
36192 #define OMP_TEAMS_CLAUSE_MASK \
36193 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36201 static tree
36202 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36203 char *p_name, omp_clause_mask mask, tree *cclauses,
36204 bool *if_p)
36206 tree clauses, sb, ret;
36207 unsigned int save;
36208 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36210 strcat (p_name, " teams");
36211 mask |= OMP_TEAMS_CLAUSE_MASK;
36213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36215 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36216 const char *p = IDENTIFIER_POINTER (id);
36217 if (strcmp (p, "distribute") == 0)
36219 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36220 if (cclauses == NULL)
36221 cclauses = cclauses_buf;
36223 cp_lexer_consume_token (parser->lexer);
36224 if (!flag_openmp) /* flag_openmp_simd */
36225 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36226 cclauses, if_p);
36227 sb = begin_omp_structured_block ();
36228 save = cp_parser_begin_omp_structured_block (parser);
36229 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36230 cclauses, if_p);
36231 cp_parser_end_omp_structured_block (parser, save);
36232 tree body = finish_omp_structured_block (sb);
36233 if (ret == NULL)
36234 return ret;
36235 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36236 ret = make_node (OMP_TEAMS);
36237 TREE_TYPE (ret) = void_type_node;
36238 OMP_TEAMS_CLAUSES (ret) = clauses;
36239 OMP_TEAMS_BODY (ret) = body;
36240 OMP_TEAMS_COMBINED (ret) = 1;
36241 SET_EXPR_LOCATION (ret, loc);
36242 return add_stmt (ret);
36245 if (!flag_openmp) /* flag_openmp_simd */
36247 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36248 return NULL_TREE;
36251 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36252 cclauses == NULL);
36253 if (cclauses)
36255 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36256 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36259 tree stmt = make_node (OMP_TEAMS);
36260 TREE_TYPE (stmt) = void_type_node;
36261 OMP_TEAMS_CLAUSES (stmt) = clauses;
36262 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36263 SET_EXPR_LOCATION (stmt, loc);
36265 return add_stmt (stmt);
36268 /* OpenMP 4.0:
36269 # pragma omp target data target-data-clause[optseq] new-line
36270 structured-block */
36272 #define OMP_TARGET_DATA_CLAUSE_MASK \
36273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36278 static tree
36279 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36281 tree clauses
36282 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36283 "#pragma omp target data", pragma_tok);
36284 int map_seen = 0;
36285 for (tree *pc = &clauses; *pc;)
36287 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36288 switch (OMP_CLAUSE_MAP_KIND (*pc))
36290 case GOMP_MAP_TO:
36291 case GOMP_MAP_ALWAYS_TO:
36292 case GOMP_MAP_FROM:
36293 case GOMP_MAP_ALWAYS_FROM:
36294 case GOMP_MAP_TOFROM:
36295 case GOMP_MAP_ALWAYS_TOFROM:
36296 case GOMP_MAP_ALLOC:
36297 map_seen = 3;
36298 break;
36299 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36300 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36301 case GOMP_MAP_ALWAYS_POINTER:
36302 break;
36303 default:
36304 map_seen |= 1;
36305 error_at (OMP_CLAUSE_LOCATION (*pc),
36306 "%<#pragma omp target data%> with map-type other "
36307 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36308 "on %<map%> clause");
36309 *pc = OMP_CLAUSE_CHAIN (*pc);
36310 continue;
36312 pc = &OMP_CLAUSE_CHAIN (*pc);
36315 if (map_seen != 3)
36317 if (map_seen == 0)
36318 error_at (pragma_tok->location,
36319 "%<#pragma omp target data%> must contain at least "
36320 "one %<map%> clause");
36321 return NULL_TREE;
36324 tree stmt = make_node (OMP_TARGET_DATA);
36325 TREE_TYPE (stmt) = void_type_node;
36326 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36328 keep_next_level (true);
36329 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36331 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36332 return add_stmt (stmt);
36335 /* OpenMP 4.5:
36336 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36337 structured-block */
36339 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36340 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36346 static tree
36347 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36348 enum pragma_context context)
36350 bool data_seen = false;
36351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36354 const char *p = IDENTIFIER_POINTER (id);
36356 if (strcmp (p, "data") == 0)
36358 cp_lexer_consume_token (parser->lexer);
36359 data_seen = true;
36362 if (!data_seen)
36364 cp_parser_error (parser, "expected %<data%>");
36365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36366 return NULL_TREE;
36369 if (context == pragma_stmt)
36371 error_at (pragma_tok->location,
36372 "%<#pragma %s%> may only be used in compound statements",
36373 "omp target enter data");
36374 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36375 return NULL_TREE;
36378 tree clauses
36379 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36380 "#pragma omp target enter data", pragma_tok);
36381 int map_seen = 0;
36382 for (tree *pc = &clauses; *pc;)
36384 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36385 switch (OMP_CLAUSE_MAP_KIND (*pc))
36387 case GOMP_MAP_TO:
36388 case GOMP_MAP_ALWAYS_TO:
36389 case GOMP_MAP_ALLOC:
36390 map_seen = 3;
36391 break;
36392 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36393 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36394 case GOMP_MAP_ALWAYS_POINTER:
36395 break;
36396 default:
36397 map_seen |= 1;
36398 error_at (OMP_CLAUSE_LOCATION (*pc),
36399 "%<#pragma omp target enter data%> with map-type other "
36400 "than %<to%> or %<alloc%> on %<map%> clause");
36401 *pc = OMP_CLAUSE_CHAIN (*pc);
36402 continue;
36404 pc = &OMP_CLAUSE_CHAIN (*pc);
36407 if (map_seen != 3)
36409 if (map_seen == 0)
36410 error_at (pragma_tok->location,
36411 "%<#pragma omp target enter data%> must contain at least "
36412 "one %<map%> clause");
36413 return NULL_TREE;
36416 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36417 TREE_TYPE (stmt) = void_type_node;
36418 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36419 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36420 return add_stmt (stmt);
36423 /* OpenMP 4.5:
36424 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36425 structured-block */
36427 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36434 static tree
36435 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36436 enum pragma_context context)
36438 bool data_seen = false;
36439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36442 const char *p = IDENTIFIER_POINTER (id);
36444 if (strcmp (p, "data") == 0)
36446 cp_lexer_consume_token (parser->lexer);
36447 data_seen = true;
36450 if (!data_seen)
36452 cp_parser_error (parser, "expected %<data%>");
36453 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36454 return NULL_TREE;
36457 if (context == pragma_stmt)
36459 error_at (pragma_tok->location,
36460 "%<#pragma %s%> may only be used in compound statements",
36461 "omp target exit data");
36462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36463 return NULL_TREE;
36466 tree clauses
36467 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36468 "#pragma omp target exit data", pragma_tok);
36469 int map_seen = 0;
36470 for (tree *pc = &clauses; *pc;)
36472 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36473 switch (OMP_CLAUSE_MAP_KIND (*pc))
36475 case GOMP_MAP_FROM:
36476 case GOMP_MAP_ALWAYS_FROM:
36477 case GOMP_MAP_RELEASE:
36478 case GOMP_MAP_DELETE:
36479 map_seen = 3;
36480 break;
36481 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36482 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36483 case GOMP_MAP_ALWAYS_POINTER:
36484 break;
36485 default:
36486 map_seen |= 1;
36487 error_at (OMP_CLAUSE_LOCATION (*pc),
36488 "%<#pragma omp target exit data%> with map-type other "
36489 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36490 " clause");
36491 *pc = OMP_CLAUSE_CHAIN (*pc);
36492 continue;
36494 pc = &OMP_CLAUSE_CHAIN (*pc);
36497 if (map_seen != 3)
36499 if (map_seen == 0)
36500 error_at (pragma_tok->location,
36501 "%<#pragma omp target exit data%> must contain at least "
36502 "one %<map%> clause");
36503 return NULL_TREE;
36506 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36507 TREE_TYPE (stmt) = void_type_node;
36508 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36509 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36510 return add_stmt (stmt);
36513 /* OpenMP 4.0:
36514 # pragma omp target update target-update-clause[optseq] new-line */
36516 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36524 static bool
36525 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36526 enum pragma_context context)
36528 if (context == pragma_stmt)
36530 error_at (pragma_tok->location,
36531 "%<#pragma %s%> may only be used in compound statements",
36532 "omp target update");
36533 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36534 return false;
36537 tree clauses
36538 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36539 "#pragma omp target update", pragma_tok);
36540 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36541 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36543 error_at (pragma_tok->location,
36544 "%<#pragma omp target update%> must contain at least one "
36545 "%<from%> or %<to%> clauses");
36546 return false;
36549 tree stmt = make_node (OMP_TARGET_UPDATE);
36550 TREE_TYPE (stmt) = void_type_node;
36551 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36552 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36553 add_stmt (stmt);
36554 return false;
36557 /* OpenMP 4.0:
36558 # pragma omp target target-clause[optseq] new-line
36559 structured-block */
36561 #define OMP_TARGET_CLAUSE_MASK \
36562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36572 static bool
36573 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36574 enum pragma_context context, bool *if_p)
36576 tree *pc = NULL, stmt;
36578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36580 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36581 const char *p = IDENTIFIER_POINTER (id);
36582 enum tree_code ccode = ERROR_MARK;
36584 if (strcmp (p, "teams") == 0)
36585 ccode = OMP_TEAMS;
36586 else if (strcmp (p, "parallel") == 0)
36587 ccode = OMP_PARALLEL;
36588 else if (strcmp (p, "simd") == 0)
36589 ccode = OMP_SIMD;
36590 if (ccode != ERROR_MARK)
36592 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36593 char p_name[sizeof ("#pragma omp target teams distribute "
36594 "parallel for simd")];
36596 cp_lexer_consume_token (parser->lexer);
36597 strcpy (p_name, "#pragma omp target");
36598 if (!flag_openmp) /* flag_openmp_simd */
36600 tree stmt;
36601 switch (ccode)
36603 case OMP_TEAMS:
36604 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36605 OMP_TARGET_CLAUSE_MASK,
36606 cclauses, if_p);
36607 break;
36608 case OMP_PARALLEL:
36609 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36610 OMP_TARGET_CLAUSE_MASK,
36611 cclauses, if_p);
36612 break;
36613 case OMP_SIMD:
36614 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36615 OMP_TARGET_CLAUSE_MASK,
36616 cclauses, if_p);
36617 break;
36618 default:
36619 gcc_unreachable ();
36621 return stmt != NULL_TREE;
36623 keep_next_level (true);
36624 tree sb = begin_omp_structured_block (), ret;
36625 unsigned save = cp_parser_begin_omp_structured_block (parser);
36626 switch (ccode)
36628 case OMP_TEAMS:
36629 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36630 OMP_TARGET_CLAUSE_MASK, cclauses,
36631 if_p);
36632 break;
36633 case OMP_PARALLEL:
36634 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36635 OMP_TARGET_CLAUSE_MASK, cclauses,
36636 if_p);
36637 break;
36638 case OMP_SIMD:
36639 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36640 OMP_TARGET_CLAUSE_MASK, cclauses,
36641 if_p);
36642 break;
36643 default:
36644 gcc_unreachable ();
36646 cp_parser_end_omp_structured_block (parser, save);
36647 tree body = finish_omp_structured_block (sb);
36648 if (ret == NULL_TREE)
36649 return false;
36650 if (ccode == OMP_TEAMS && !processing_template_decl)
36652 /* For combined target teams, ensure the num_teams and
36653 thread_limit clause expressions are evaluated on the host,
36654 before entering the target construct. */
36655 tree c;
36656 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36657 c; c = OMP_CLAUSE_CHAIN (c))
36658 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36659 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36660 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36662 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36663 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36664 if (expr == error_mark_node)
36665 continue;
36666 tree tmp = TARGET_EXPR_SLOT (expr);
36667 add_stmt (expr);
36668 OMP_CLAUSE_OPERAND (c, 0) = expr;
36669 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36670 OMP_CLAUSE_FIRSTPRIVATE);
36671 OMP_CLAUSE_DECL (tc) = tmp;
36672 OMP_CLAUSE_CHAIN (tc)
36673 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36674 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36677 tree stmt = make_node (OMP_TARGET);
36678 TREE_TYPE (stmt) = void_type_node;
36679 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36680 OMP_TARGET_BODY (stmt) = body;
36681 OMP_TARGET_COMBINED (stmt) = 1;
36682 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36683 add_stmt (stmt);
36684 pc = &OMP_TARGET_CLAUSES (stmt);
36685 goto check_clauses;
36687 else if (!flag_openmp) /* flag_openmp_simd */
36689 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36690 return false;
36692 else if (strcmp (p, "data") == 0)
36694 cp_lexer_consume_token (parser->lexer);
36695 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36696 return true;
36698 else if (strcmp (p, "enter") == 0)
36700 cp_lexer_consume_token (parser->lexer);
36701 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36702 return false;
36704 else if (strcmp (p, "exit") == 0)
36706 cp_lexer_consume_token (parser->lexer);
36707 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36708 return false;
36710 else if (strcmp (p, "update") == 0)
36712 cp_lexer_consume_token (parser->lexer);
36713 return cp_parser_omp_target_update (parser, pragma_tok, context);
36716 if (!flag_openmp) /* flag_openmp_simd */
36718 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36719 return false;
36722 stmt = make_node (OMP_TARGET);
36723 TREE_TYPE (stmt) = void_type_node;
36725 OMP_TARGET_CLAUSES (stmt)
36726 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36727 "#pragma omp target", pragma_tok);
36728 pc = &OMP_TARGET_CLAUSES (stmt);
36729 keep_next_level (true);
36730 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36732 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36733 add_stmt (stmt);
36735 check_clauses:
36736 while (*pc)
36738 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36739 switch (OMP_CLAUSE_MAP_KIND (*pc))
36741 case GOMP_MAP_TO:
36742 case GOMP_MAP_ALWAYS_TO:
36743 case GOMP_MAP_FROM:
36744 case GOMP_MAP_ALWAYS_FROM:
36745 case GOMP_MAP_TOFROM:
36746 case GOMP_MAP_ALWAYS_TOFROM:
36747 case GOMP_MAP_ALLOC:
36748 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36749 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36750 case GOMP_MAP_ALWAYS_POINTER:
36751 break;
36752 default:
36753 error_at (OMP_CLAUSE_LOCATION (*pc),
36754 "%<#pragma omp target%> with map-type other "
36755 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36756 "on %<map%> clause");
36757 *pc = OMP_CLAUSE_CHAIN (*pc);
36758 continue;
36760 pc = &OMP_CLAUSE_CHAIN (*pc);
36762 return true;
36765 /* OpenACC 2.0:
36766 # pragma acc cache (variable-list) new-line
36769 static tree
36770 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36772 tree stmt, clauses;
36774 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36775 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36777 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36779 stmt = make_node (OACC_CACHE);
36780 TREE_TYPE (stmt) = void_type_node;
36781 OACC_CACHE_CLAUSES (stmt) = clauses;
36782 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36783 add_stmt (stmt);
36785 return stmt;
36788 /* OpenACC 2.0:
36789 # pragma acc data oacc-data-clause[optseq] new-line
36790 structured-block */
36792 #define OACC_DATA_CLAUSE_MASK \
36793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36805 static tree
36806 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36808 tree stmt, clauses, block;
36809 unsigned int save;
36811 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36812 "#pragma acc data", pragma_tok);
36814 block = begin_omp_parallel ();
36815 save = cp_parser_begin_omp_structured_block (parser);
36816 cp_parser_statement (parser, NULL_TREE, false, if_p);
36817 cp_parser_end_omp_structured_block (parser, save);
36818 stmt = finish_oacc_data (clauses, block);
36819 return stmt;
36822 /* OpenACC 2.0:
36823 # pragma acc host_data <clauses> new-line
36824 structured-block */
36826 #define OACC_HOST_DATA_CLAUSE_MASK \
36827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36829 static tree
36830 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36832 tree stmt, clauses, block;
36833 unsigned int save;
36835 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36836 "#pragma acc host_data", pragma_tok);
36838 block = begin_omp_parallel ();
36839 save = cp_parser_begin_omp_structured_block (parser);
36840 cp_parser_statement (parser, NULL_TREE, false, if_p);
36841 cp_parser_end_omp_structured_block (parser, save);
36842 stmt = finish_oacc_host_data (clauses, block);
36843 return stmt;
36846 /* OpenACC 2.0:
36847 # pragma acc declare oacc-data-clause[optseq] new-line
36850 #define OACC_DECLARE_CLAUSE_MASK \
36851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36864 static tree
36865 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36867 tree clauses, stmt;
36868 bool error = false;
36870 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36871 "#pragma acc declare", pragma_tok, true);
36874 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36876 error_at (pragma_tok->location,
36877 "no valid clauses specified in %<#pragma acc declare%>");
36878 return NULL_TREE;
36881 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36883 location_t loc = OMP_CLAUSE_LOCATION (t);
36884 tree decl = OMP_CLAUSE_DECL (t);
36885 if (!DECL_P (decl))
36887 error_at (loc, "array section in %<#pragma acc declare%>");
36888 error = true;
36889 continue;
36891 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36892 switch (OMP_CLAUSE_MAP_KIND (t))
36894 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36895 case GOMP_MAP_FORCE_ALLOC:
36896 case GOMP_MAP_FORCE_TO:
36897 case GOMP_MAP_FORCE_DEVICEPTR:
36898 case GOMP_MAP_DEVICE_RESIDENT:
36899 break;
36901 case GOMP_MAP_LINK:
36902 if (!global_bindings_p ()
36903 && (TREE_STATIC (decl)
36904 || !DECL_EXTERNAL (decl)))
36906 error_at (loc,
36907 "%qD must be a global variable in "
36908 "%<#pragma acc declare link%>",
36909 decl);
36910 error = true;
36911 continue;
36913 break;
36915 default:
36916 if (global_bindings_p ())
36918 error_at (loc, "invalid OpenACC clause at file scope");
36919 error = true;
36920 continue;
36922 if (DECL_EXTERNAL (decl))
36924 error_at (loc,
36925 "invalid use of %<extern%> variable %qD "
36926 "in %<#pragma acc declare%>", decl);
36927 error = true;
36928 continue;
36930 else if (TREE_PUBLIC (decl))
36932 error_at (loc,
36933 "invalid use of %<global%> variable %qD "
36934 "in %<#pragma acc declare%>", decl);
36935 error = true;
36936 continue;
36938 break;
36941 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36942 || lookup_attribute ("omp declare target link",
36943 DECL_ATTRIBUTES (decl)))
36945 error_at (loc, "variable %qD used more than once with "
36946 "%<#pragma acc declare%>", decl);
36947 error = true;
36948 continue;
36951 if (!error)
36953 tree id;
36955 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36956 id = get_identifier ("omp declare target link");
36957 else
36958 id = get_identifier ("omp declare target");
36960 DECL_ATTRIBUTES (decl)
36961 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36962 if (global_bindings_p ())
36964 symtab_node *node = symtab_node::get (decl);
36965 if (node != NULL)
36967 node->offloadable = 1;
36968 if (ENABLE_OFFLOADING)
36970 g->have_offload = true;
36971 if (is_a <varpool_node *> (node))
36972 vec_safe_push (offload_vars, decl);
36979 if (error || global_bindings_p ())
36980 return NULL_TREE;
36982 stmt = make_node (OACC_DECLARE);
36983 TREE_TYPE (stmt) = void_type_node;
36984 OACC_DECLARE_CLAUSES (stmt) = clauses;
36985 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36987 add_stmt (stmt);
36989 return NULL_TREE;
36992 /* OpenACC 2.0:
36993 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36997 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36999 LOC is the location of the #pragma token.
37002 #define OACC_ENTER_DATA_CLAUSE_MASK \
37003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37011 #define OACC_EXIT_DATA_CLAUSE_MASK \
37012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37018 static tree
37019 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37020 bool enter)
37022 location_t loc = pragma_tok->location;
37023 tree stmt, clauses;
37024 const char *p = "";
37026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37027 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37029 if (strcmp (p, "data") != 0)
37031 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37032 enter ? "enter" : "exit");
37033 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37034 return NULL_TREE;
37037 cp_lexer_consume_token (parser->lexer);
37039 if (enter)
37040 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37041 "#pragma acc enter data", pragma_tok);
37042 else
37043 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37044 "#pragma acc exit data", pragma_tok);
37046 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37048 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37049 enter ? "enter" : "exit");
37050 return NULL_TREE;
37053 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37054 TREE_TYPE (stmt) = void_type_node;
37055 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37056 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37057 add_stmt (stmt);
37058 return stmt;
37061 /* OpenACC 2.0:
37062 # pragma acc loop oacc-loop-clause[optseq] new-line
37063 structured-block */
37065 #define OACC_LOOP_CLAUSE_MASK \
37066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37077 static tree
37078 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37079 omp_clause_mask mask, tree *cclauses, bool *if_p)
37081 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37083 strcat (p_name, " loop");
37084 mask |= OACC_LOOP_CLAUSE_MASK;
37086 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37087 cclauses == NULL);
37088 if (cclauses)
37090 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37091 if (*cclauses)
37092 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37093 if (clauses)
37094 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37097 tree block = begin_omp_structured_block ();
37098 int save = cp_parser_begin_omp_structured_block (parser);
37099 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37100 cp_parser_end_omp_structured_block (parser, save);
37101 add_stmt (finish_omp_structured_block (block));
37103 return stmt;
37106 /* OpenACC 2.0:
37107 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37108 structured-block
37112 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37113 structured-block
37116 #define OACC_KERNELS_CLAUSE_MASK \
37117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37135 #define OACC_PARALLEL_CLAUSE_MASK \
37136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37157 static tree
37158 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37159 char *p_name, bool *if_p)
37161 omp_clause_mask mask;
37162 enum tree_code code;
37163 switch (cp_parser_pragma_kind (pragma_tok))
37165 case PRAGMA_OACC_KERNELS:
37166 strcat (p_name, " kernels");
37167 mask = OACC_KERNELS_CLAUSE_MASK;
37168 code = OACC_KERNELS;
37169 break;
37170 case PRAGMA_OACC_PARALLEL:
37171 strcat (p_name, " parallel");
37172 mask = OACC_PARALLEL_CLAUSE_MASK;
37173 code = OACC_PARALLEL;
37174 break;
37175 default:
37176 gcc_unreachable ();
37179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37181 const char *p
37182 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37183 if (strcmp (p, "loop") == 0)
37185 cp_lexer_consume_token (parser->lexer);
37186 tree block = begin_omp_parallel ();
37187 tree clauses;
37188 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37189 if_p);
37190 return finish_omp_construct (code, block, clauses);
37194 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37196 tree block = begin_omp_parallel ();
37197 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37198 cp_parser_statement (parser, NULL_TREE, false, if_p);
37199 cp_parser_end_omp_structured_block (parser, save);
37200 return finish_omp_construct (code, block, clauses);
37203 /* OpenACC 2.0:
37204 # pragma acc update oacc-update-clause[optseq] new-line
37207 #define OACC_UPDATE_CLAUSE_MASK \
37208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37215 static tree
37216 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37218 tree stmt, clauses;
37220 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37221 "#pragma acc update", pragma_tok);
37223 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37225 error_at (pragma_tok->location,
37226 "%<#pragma acc update%> must contain at least one "
37227 "%<device%> or %<host%> or %<self%> clause");
37228 return NULL_TREE;
37231 stmt = make_node (OACC_UPDATE);
37232 TREE_TYPE (stmt) = void_type_node;
37233 OACC_UPDATE_CLAUSES (stmt) = clauses;
37234 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37235 add_stmt (stmt);
37236 return stmt;
37239 /* OpenACC 2.0:
37240 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37242 LOC is the location of the #pragma token.
37245 #define OACC_WAIT_CLAUSE_MASK \
37246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37248 static tree
37249 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37251 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37252 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37254 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37255 list = cp_parser_oacc_wait_list (parser, loc, list);
37257 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37258 "#pragma acc wait", pragma_tok);
37260 stmt = c_finish_oacc_wait (loc, list, clauses);
37261 stmt = finish_expr_stmt (stmt);
37263 return stmt;
37266 /* OpenMP 4.0:
37267 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37269 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37277 static void
37278 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37279 enum pragma_context context)
37281 bool first_p = parser->omp_declare_simd == NULL;
37282 cp_omp_declare_simd_data data;
37283 if (first_p)
37285 data.error_seen = false;
37286 data.fndecl_seen = false;
37287 data.tokens = vNULL;
37288 data.clauses = NULL_TREE;
37289 /* It is safe to take the address of a local variable; it will only be
37290 used while this scope is live. */
37291 parser->omp_declare_simd = &data;
37294 /* Store away all pragma tokens. */
37295 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37296 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37297 cp_lexer_consume_token (parser->lexer);
37298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37299 parser->omp_declare_simd->error_seen = true;
37300 cp_parser_require_pragma_eol (parser, pragma_tok);
37301 struct cp_token_cache *cp
37302 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37303 parser->omp_declare_simd->tokens.safe_push (cp);
37305 if (first_p)
37307 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37308 cp_parser_pragma (parser, context, NULL);
37309 switch (context)
37311 case pragma_external:
37312 cp_parser_declaration (parser);
37313 break;
37314 case pragma_member:
37315 cp_parser_member_declaration (parser);
37316 break;
37317 case pragma_objc_icode:
37318 cp_parser_block_declaration (parser, /*statement_p=*/false);
37319 break;
37320 default:
37321 cp_parser_declaration_statement (parser);
37322 break;
37324 if (parser->omp_declare_simd
37325 && !parser->omp_declare_simd->error_seen
37326 && !parser->omp_declare_simd->fndecl_seen)
37327 error_at (pragma_tok->location,
37328 "%<#pragma omp declare simd%> not immediately followed by "
37329 "function declaration or definition");
37330 data.tokens.release ();
37331 parser->omp_declare_simd = NULL;
37335 /* Finalize #pragma omp declare simd clauses after direct declarator has
37336 been parsed, and put that into "omp declare simd" attribute. */
37338 static tree
37339 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37341 struct cp_token_cache *ce;
37342 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37343 int i;
37345 if (!data->error_seen && data->fndecl_seen)
37347 error ("%<#pragma omp declare simd%> not immediately followed by "
37348 "a single function declaration or definition");
37349 data->error_seen = true;
37351 if (data->error_seen)
37352 return attrs;
37354 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37356 tree c, cl;
37358 cp_parser_push_lexer_for_tokens (parser, ce);
37359 parser->lexer->in_pragma = true;
37360 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37361 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37362 cp_lexer_consume_token (parser->lexer);
37363 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37364 "#pragma omp declare simd", pragma_tok);
37365 cp_parser_pop_lexer (parser);
37366 if (cl)
37367 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37368 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37369 TREE_CHAIN (c) = attrs;
37370 if (processing_template_decl)
37371 ATTR_IS_DEPENDENT (c) = 1;
37372 attrs = c;
37375 data->fndecl_seen = true;
37376 return attrs;
37380 /* OpenMP 4.0:
37381 # pragma omp declare target new-line
37382 declarations and definitions
37383 # pragma omp end declare target new-line
37385 OpenMP 4.5:
37386 # pragma omp declare target ( extended-list ) new-line
37388 # pragma omp declare target declare-target-clauses[seq] new-line */
37390 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37394 static void
37395 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37397 tree clauses = NULL_TREE;
37398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37399 clauses
37400 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37401 "#pragma omp declare target", pragma_tok);
37402 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37405 clauses);
37406 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37407 cp_parser_require_pragma_eol (parser, pragma_tok);
37409 else
37411 cp_parser_require_pragma_eol (parser, pragma_tok);
37412 scope_chain->omp_declare_target_attribute++;
37413 return;
37415 if (scope_chain->omp_declare_target_attribute)
37416 error_at (pragma_tok->location,
37417 "%<#pragma omp declare target%> with clauses in between "
37418 "%<#pragma omp declare target%> without clauses and "
37419 "%<#pragma omp end declare target%>");
37420 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37422 tree t = OMP_CLAUSE_DECL (c), id;
37423 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37424 tree at2 = lookup_attribute ("omp declare target link",
37425 DECL_ATTRIBUTES (t));
37426 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37428 id = get_identifier ("omp declare target link");
37429 std::swap (at1, at2);
37431 else
37432 id = get_identifier ("omp declare target");
37433 if (at2)
37435 error_at (OMP_CLAUSE_LOCATION (c),
37436 "%qD specified both in declare target %<link%> and %<to%>"
37437 " clauses", t);
37438 continue;
37440 if (!at1)
37442 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37443 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37444 continue;
37446 symtab_node *node = symtab_node::get (t);
37447 if (node != NULL)
37449 node->offloadable = 1;
37450 if (ENABLE_OFFLOADING)
37452 g->have_offload = true;
37453 if (is_a <varpool_node *> (node))
37454 vec_safe_push (offload_vars, t);
37461 static void
37462 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37464 const char *p = "";
37465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37467 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37468 p = IDENTIFIER_POINTER (id);
37470 if (strcmp (p, "declare") == 0)
37472 cp_lexer_consume_token (parser->lexer);
37473 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);
37479 if (strcmp (p, "target") == 0)
37480 cp_lexer_consume_token (parser->lexer);
37481 else
37483 cp_parser_error (parser, "expected %<target%>");
37484 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37485 return;
37488 else
37490 cp_parser_error (parser, "expected %<declare%>");
37491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37492 return;
37494 cp_parser_require_pragma_eol (parser, pragma_tok);
37495 if (!scope_chain->omp_declare_target_attribute)
37496 error_at (pragma_tok->location,
37497 "%<#pragma omp end declare target%> without corresponding "
37498 "%<#pragma omp declare target%>");
37499 else
37500 scope_chain->omp_declare_target_attribute--;
37503 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37504 expression and optional initializer clause of
37505 #pragma omp declare reduction. We store the expression(s) as
37506 either 3, 6 or 7 special statements inside of the artificial function's
37507 body. The first two statements are DECL_EXPRs for the artificial
37508 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37509 expression that uses those variables.
37510 If there was any INITIALIZER clause, this is followed by further statements,
37511 the fourth and fifth statements are DECL_EXPRs for the artificial
37512 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37513 constructor variant (first token after open paren is not omp_priv),
37514 then the sixth statement is a statement with the function call expression
37515 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37516 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37517 to initialize the OMP_PRIV artificial variable and there is seventh
37518 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37520 static bool
37521 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37523 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37524 gcc_assert (TYPE_REF_P (type));
37525 type = TREE_TYPE (type);
37526 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37527 DECL_ARTIFICIAL (omp_out) = 1;
37528 pushdecl (omp_out);
37529 add_decl_expr (omp_out);
37530 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37531 DECL_ARTIFICIAL (omp_in) = 1;
37532 pushdecl (omp_in);
37533 add_decl_expr (omp_in);
37534 tree combiner;
37535 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37537 keep_next_level (true);
37538 tree block = begin_omp_structured_block ();
37539 combiner = cp_parser_expression (parser);
37540 finish_expr_stmt (combiner);
37541 block = finish_omp_structured_block (block);
37542 add_stmt (block);
37544 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37545 return false;
37547 const char *p = "";
37548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37551 p = IDENTIFIER_POINTER (id);
37554 if (strcmp (p, "initializer") == 0)
37556 cp_lexer_consume_token (parser->lexer);
37557 matching_parens parens;
37558 if (!parens.require_open (parser))
37559 return false;
37561 p = "";
37562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37565 p = IDENTIFIER_POINTER (id);
37568 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37569 DECL_ARTIFICIAL (omp_priv) = 1;
37570 pushdecl (omp_priv);
37571 add_decl_expr (omp_priv);
37572 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37573 DECL_ARTIFICIAL (omp_orig) = 1;
37574 pushdecl (omp_orig);
37575 add_decl_expr (omp_orig);
37577 keep_next_level (true);
37578 block = begin_omp_structured_block ();
37580 bool ctor = false;
37581 if (strcmp (p, "omp_priv") == 0)
37583 bool is_direct_init, is_non_constant_init;
37584 ctor = true;
37585 cp_lexer_consume_token (parser->lexer);
37586 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37588 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37589 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37590 == CPP_CLOSE_PAREN
37591 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37592 == CPP_CLOSE_PAREN))
37594 finish_omp_structured_block (block);
37595 error ("invalid initializer clause");
37596 return false;
37598 initializer = cp_parser_initializer (parser, &is_direct_init,
37599 &is_non_constant_init);
37600 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37601 NULL_TREE, LOOKUP_ONLYCONVERTING);
37603 else
37605 cp_parser_parse_tentatively (parser);
37606 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37607 /*check_dependency_p=*/true,
37608 /*template_p=*/NULL,
37609 /*declarator_p=*/false,
37610 /*optional_p=*/false);
37611 vec<tree, va_gc> *args;
37612 if (fn_name == error_mark_node
37613 || cp_parser_error_occurred (parser)
37614 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37615 || ((args = cp_parser_parenthesized_expression_list
37616 (parser, non_attr, /*cast_p=*/false,
37617 /*allow_expansion_p=*/true,
37618 /*non_constant_p=*/NULL)),
37619 cp_parser_error_occurred (parser)))
37621 finish_omp_structured_block (block);
37622 cp_parser_abort_tentative_parse (parser);
37623 cp_parser_error (parser, "expected id-expression (arguments)");
37624 return false;
37626 unsigned int i;
37627 tree arg;
37628 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37629 if (arg == omp_priv
37630 || (TREE_CODE (arg) == ADDR_EXPR
37631 && TREE_OPERAND (arg, 0) == omp_priv))
37632 break;
37633 cp_parser_abort_tentative_parse (parser);
37634 if (arg == NULL_TREE)
37635 error ("one of the initializer call arguments should be %<omp_priv%>"
37636 " or %<&omp_priv%>");
37637 initializer = cp_parser_postfix_expression (parser, false, false, false,
37638 false, NULL);
37639 finish_expr_stmt (initializer);
37642 block = finish_omp_structured_block (block);
37643 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37644 add_stmt (block);
37646 if (ctor)
37647 add_decl_expr (omp_orig);
37649 if (!parens.require_close (parser))
37650 return false;
37653 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37654 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37655 UNKNOWN_LOCATION);
37657 return true;
37660 /* OpenMP 4.0
37661 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37662 initializer-clause[opt] new-line
37664 initializer-clause:
37665 initializer (omp_priv initializer)
37666 initializer (function-name (argument-list)) */
37668 static void
37669 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37670 enum pragma_context)
37672 auto_vec<tree> types;
37673 enum tree_code reduc_code = ERROR_MARK;
37674 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37675 unsigned int i;
37676 cp_token *first_token;
37677 cp_token_cache *cp;
37678 int errs;
37679 void *p;
37681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37682 p = obstack_alloc (&declarator_obstack, 0);
37684 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37685 goto fail;
37687 switch (cp_lexer_peek_token (parser->lexer)->type)
37689 case CPP_PLUS:
37690 reduc_code = PLUS_EXPR;
37691 break;
37692 case CPP_MULT:
37693 reduc_code = MULT_EXPR;
37694 break;
37695 case CPP_MINUS:
37696 reduc_code = MINUS_EXPR;
37697 break;
37698 case CPP_AND:
37699 reduc_code = BIT_AND_EXPR;
37700 break;
37701 case CPP_XOR:
37702 reduc_code = BIT_XOR_EXPR;
37703 break;
37704 case CPP_OR:
37705 reduc_code = BIT_IOR_EXPR;
37706 break;
37707 case CPP_AND_AND:
37708 reduc_code = TRUTH_ANDIF_EXPR;
37709 break;
37710 case CPP_OR_OR:
37711 reduc_code = TRUTH_ORIF_EXPR;
37712 break;
37713 case CPP_NAME:
37714 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37715 break;
37716 default:
37717 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37718 "%<|%>, %<&&%>, %<||%> or identifier");
37719 goto fail;
37722 if (reduc_code != ERROR_MARK)
37723 cp_lexer_consume_token (parser->lexer);
37725 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37726 if (reduc_id == error_mark_node)
37727 goto fail;
37729 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37730 goto fail;
37732 /* Types may not be defined in declare reduction type list. */
37733 const char *saved_message;
37734 saved_message = parser->type_definition_forbidden_message;
37735 parser->type_definition_forbidden_message
37736 = G_("types may not be defined in declare reduction type list");
37737 bool saved_colon_corrects_to_scope_p;
37738 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37739 parser->colon_corrects_to_scope_p = false;
37740 bool saved_colon_doesnt_start_class_def_p;
37741 saved_colon_doesnt_start_class_def_p
37742 = parser->colon_doesnt_start_class_def_p;
37743 parser->colon_doesnt_start_class_def_p = true;
37745 while (true)
37747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37748 type = cp_parser_type_id (parser);
37749 if (type == error_mark_node)
37751 else if (ARITHMETIC_TYPE_P (type)
37752 && (orig_reduc_id == NULL_TREE
37753 || (TREE_CODE (type) != COMPLEX_TYPE
37754 && (id_equal (orig_reduc_id, "min")
37755 || id_equal (orig_reduc_id, "max")))))
37756 error_at (loc, "predeclared arithmetic type %qT in "
37757 "%<#pragma omp declare reduction%>", type);
37758 else if (TREE_CODE (type) == FUNCTION_TYPE
37759 || TREE_CODE (type) == METHOD_TYPE
37760 || TREE_CODE (type) == ARRAY_TYPE)
37761 error_at (loc, "function or array type %qT in "
37762 "%<#pragma omp declare reduction%>", type);
37763 else if (TYPE_REF_P (type))
37764 error_at (loc, "reference type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37767 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37768 "%<#pragma omp declare reduction%>", type);
37769 else
37770 types.safe_push (type);
37772 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37773 cp_lexer_consume_token (parser->lexer);
37774 else
37775 break;
37778 /* Restore the saved message. */
37779 parser->type_definition_forbidden_message = saved_message;
37780 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37781 parser->colon_doesnt_start_class_def_p
37782 = saved_colon_doesnt_start_class_def_p;
37784 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37785 || types.is_empty ())
37787 fail:
37788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37789 goto done;
37792 first_token = cp_lexer_peek_token (parser->lexer);
37793 cp = NULL;
37794 errs = errorcount;
37795 FOR_EACH_VEC_ELT (types, i, type)
37797 tree fntype
37798 = build_function_type_list (void_type_node,
37799 cp_build_reference_type (type, false),
37800 NULL_TREE);
37801 tree this_reduc_id = reduc_id;
37802 if (!dependent_type_p (type))
37803 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37804 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37805 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37806 DECL_ARTIFICIAL (fndecl) = 1;
37807 DECL_EXTERNAL (fndecl) = 1;
37808 DECL_DECLARED_INLINE_P (fndecl) = 1;
37809 DECL_IGNORED_P (fndecl) = 1;
37810 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37811 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37812 DECL_ATTRIBUTES (fndecl)
37813 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37814 DECL_ATTRIBUTES (fndecl));
37815 if (processing_template_decl)
37816 fndecl = push_template_decl (fndecl);
37817 bool block_scope = false;
37818 tree block = NULL_TREE;
37819 if (current_function_decl)
37821 block_scope = true;
37822 DECL_CONTEXT (fndecl) = global_namespace;
37823 if (!processing_template_decl)
37824 pushdecl (fndecl);
37826 else if (current_class_type)
37828 if (cp == NULL)
37830 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37831 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37832 cp_lexer_consume_token (parser->lexer);
37833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37834 goto fail;
37835 cp = cp_token_cache_new (first_token,
37836 cp_lexer_peek_nth_token (parser->lexer,
37837 2));
37839 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37840 finish_member_declaration (fndecl);
37841 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37842 DECL_PENDING_INLINE_P (fndecl) = 1;
37843 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37844 continue;
37846 else
37848 DECL_CONTEXT (fndecl) = current_namespace;
37849 pushdecl (fndecl);
37851 if (!block_scope)
37852 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37853 else
37854 block = begin_omp_structured_block ();
37855 if (cp)
37857 cp_parser_push_lexer_for_tokens (parser, cp);
37858 parser->lexer->in_pragma = true;
37860 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37862 if (!block_scope)
37863 finish_function (/*inline_p=*/false);
37864 else
37865 DECL_CONTEXT (fndecl) = current_function_decl;
37866 if (cp)
37867 cp_parser_pop_lexer (parser);
37868 goto fail;
37870 if (cp)
37871 cp_parser_pop_lexer (parser);
37872 if (!block_scope)
37873 finish_function (/*inline_p=*/false);
37874 else
37876 DECL_CONTEXT (fndecl) = current_function_decl;
37877 block = finish_omp_structured_block (block);
37878 if (TREE_CODE (block) == BIND_EXPR)
37879 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37880 else if (TREE_CODE (block) == STATEMENT_LIST)
37881 DECL_SAVED_TREE (fndecl) = block;
37882 if (processing_template_decl)
37883 add_decl_expr (fndecl);
37885 cp_check_omp_declare_reduction (fndecl);
37886 if (cp == NULL && types.length () > 1)
37887 cp = cp_token_cache_new (first_token,
37888 cp_lexer_peek_nth_token (parser->lexer, 2));
37889 if (errs != errorcount)
37890 break;
37893 cp_parser_require_pragma_eol (parser, pragma_tok);
37895 done:
37896 /* Free any declarators allocated. */
37897 obstack_free (&declarator_obstack, p);
37900 /* OpenMP 4.0
37901 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37902 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37903 initializer-clause[opt] new-line
37904 #pragma omp declare target new-line */
37906 static bool
37907 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37908 enum pragma_context context)
37910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37913 const char *p = IDENTIFIER_POINTER (id);
37915 if (strcmp (p, "simd") == 0)
37917 cp_lexer_consume_token (parser->lexer);
37918 cp_parser_omp_declare_simd (parser, pragma_tok,
37919 context);
37920 return true;
37922 cp_ensure_no_omp_declare_simd (parser);
37923 if (strcmp (p, "reduction") == 0)
37925 cp_lexer_consume_token (parser->lexer);
37926 cp_parser_omp_declare_reduction (parser, pragma_tok,
37927 context);
37928 return false;
37930 if (!flag_openmp) /* flag_openmp_simd */
37932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37933 return false;
37935 if (strcmp (p, "target") == 0)
37937 cp_lexer_consume_token (parser->lexer);
37938 cp_parser_omp_declare_target (parser, pragma_tok);
37939 return false;
37942 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37943 "or %<target%>");
37944 cp_parser_require_pragma_eol (parser, pragma_tok);
37945 return false;
37948 /* OpenMP 4.5:
37949 #pragma omp taskloop taskloop-clause[optseq] new-line
37950 for-loop
37952 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37953 for-loop */
37955 #define OMP_TASKLOOP_CLAUSE_MASK \
37956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37971 static tree
37972 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37973 char *p_name, omp_clause_mask mask, tree *cclauses,
37974 bool *if_p)
37976 tree clauses, sb, ret;
37977 unsigned int save;
37978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37980 strcat (p_name, " taskloop");
37981 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37983 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37985 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37986 const char *p = IDENTIFIER_POINTER (id);
37988 if (strcmp (p, "simd") == 0)
37990 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37991 if (cclauses == NULL)
37992 cclauses = cclauses_buf;
37994 cp_lexer_consume_token (parser->lexer);
37995 if (!flag_openmp) /* flag_openmp_simd */
37996 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37997 cclauses, if_p);
37998 sb = begin_omp_structured_block ();
37999 save = cp_parser_begin_omp_structured_block (parser);
38000 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38001 cclauses, if_p);
38002 cp_parser_end_omp_structured_block (parser, save);
38003 tree body = finish_omp_structured_block (sb);
38004 if (ret == NULL)
38005 return ret;
38006 ret = make_node (OMP_TASKLOOP);
38007 TREE_TYPE (ret) = void_type_node;
38008 OMP_FOR_BODY (ret) = body;
38009 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38010 SET_EXPR_LOCATION (ret, loc);
38011 add_stmt (ret);
38012 return ret;
38015 if (!flag_openmp) /* flag_openmp_simd */
38017 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38018 return NULL_TREE;
38021 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38022 cclauses == NULL);
38023 if (cclauses)
38025 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38026 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38029 sb = begin_omp_structured_block ();
38030 save = cp_parser_begin_omp_structured_block (parser);
38032 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38033 if_p);
38035 cp_parser_end_omp_structured_block (parser, save);
38036 add_stmt (finish_omp_structured_block (sb));
38038 return ret;
38042 /* OpenACC 2.0:
38043 # pragma acc routine oacc-routine-clause[optseq] new-line
38044 function-definition
38046 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38049 #define OACC_ROUTINE_CLAUSE_MASK \
38050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38056 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38057 component, which must resolve to a declared namespace-scope
38058 function. The clauses are either processed directly (for a named
38059 function), or defered until the immediatley following declaration
38060 is parsed. */
38062 static void
38063 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38064 enum pragma_context context)
38066 gcc_checking_assert (context == pragma_external);
38067 /* The checking for "another pragma following this one" in the "no optional
38068 '( name )'" case makes sure that we dont re-enter. */
38069 gcc_checking_assert (parser->oacc_routine == NULL);
38071 cp_oacc_routine_data data;
38072 data.error_seen = false;
38073 data.fndecl_seen = false;
38074 data.tokens = vNULL;
38075 data.clauses = NULL_TREE;
38076 data.loc = pragma_tok->location;
38077 /* It is safe to take the address of a local variable; it will only be
38078 used while this scope is live. */
38079 parser->oacc_routine = &data;
38081 /* Look for optional '( name )'. */
38082 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38084 matching_parens parens;
38085 parens.consume_open (parser); /* '(' */
38087 /* We parse the name as an id-expression. If it resolves to
38088 anything other than a non-overloaded function at namespace
38089 scope, it's an error. */
38090 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38091 tree name = cp_parser_id_expression (parser,
38092 /*template_keyword_p=*/false,
38093 /*check_dependency_p=*/false,
38094 /*template_p=*/NULL,
38095 /*declarator_p=*/false,
38096 /*optional_p=*/false);
38097 tree decl = (identifier_p (name)
38098 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38099 : name);
38100 if (name != error_mark_node && decl == error_mark_node)
38101 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38103 if (decl == error_mark_node
38104 || !parens.require_close (parser))
38106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38107 parser->oacc_routine = NULL;
38108 return;
38111 data.clauses
38112 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38113 "#pragma acc routine",
38114 cp_lexer_peek_token (parser->lexer));
38116 if (decl && is_overloaded_fn (decl)
38117 && (TREE_CODE (decl) != FUNCTION_DECL
38118 || DECL_FUNCTION_TEMPLATE_P (decl)))
38120 error_at (name_loc,
38121 "%<#pragma acc routine%> names a set of overloads");
38122 parser->oacc_routine = NULL;
38123 return;
38126 /* Perhaps we should use the same rule as declarations in different
38127 namespaces? */
38128 if (!DECL_NAMESPACE_SCOPE_P (decl))
38130 error_at (name_loc,
38131 "%qD does not refer to a namespace scope function", decl);
38132 parser->oacc_routine = NULL;
38133 return;
38136 if (TREE_CODE (decl) != FUNCTION_DECL)
38138 error_at (name_loc, "%qD does not refer to a function", decl);
38139 parser->oacc_routine = NULL;
38140 return;
38143 cp_finalize_oacc_routine (parser, decl, false);
38144 parser->oacc_routine = NULL;
38146 else /* No optional '( name )'. */
38148 /* Store away all pragma tokens. */
38149 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38150 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38151 cp_lexer_consume_token (parser->lexer);
38152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38153 parser->oacc_routine->error_seen = true;
38154 cp_parser_require_pragma_eol (parser, pragma_tok);
38155 struct cp_token_cache *cp
38156 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38157 parser->oacc_routine->tokens.safe_push (cp);
38159 /* Emit a helpful diagnostic if there's another pragma following this
38160 one. */
38161 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38163 cp_ensure_no_oacc_routine (parser);
38164 data.tokens.release ();
38165 /* ..., and then just keep going. */
38166 return;
38169 /* We only have to consider the pragma_external case here. */
38170 cp_parser_declaration (parser);
38171 if (parser->oacc_routine
38172 && !parser->oacc_routine->fndecl_seen)
38173 cp_ensure_no_oacc_routine (parser);
38174 else
38175 parser->oacc_routine = NULL;
38176 data.tokens.release ();
38180 /* Finalize #pragma acc routine clauses after direct declarator has
38181 been parsed. */
38183 static tree
38184 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38186 struct cp_token_cache *ce;
38187 cp_oacc_routine_data *data = parser->oacc_routine;
38189 if (!data->error_seen && data->fndecl_seen)
38191 error_at (data->loc,
38192 "%<#pragma acc routine%> not immediately followed by "
38193 "a single function declaration or definition");
38194 data->error_seen = true;
38196 if (data->error_seen)
38197 return attrs;
38199 gcc_checking_assert (data->tokens.length () == 1);
38200 ce = data->tokens[0];
38202 cp_parser_push_lexer_for_tokens (parser, ce);
38203 parser->lexer->in_pragma = true;
38204 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38206 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38207 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38208 parser->oacc_routine->clauses
38209 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38210 "#pragma acc routine", pragma_tok);
38211 cp_parser_pop_lexer (parser);
38212 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38213 fndecl_seen. */
38215 return attrs;
38218 /* Apply any saved OpenACC routine clauses to a just-parsed
38219 declaration. */
38221 static void
38222 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38224 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38226 /* Keep going if we're in error reporting mode. */
38227 if (parser->oacc_routine->error_seen
38228 || fndecl == error_mark_node)
38229 return;
38231 if (parser->oacc_routine->fndecl_seen)
38233 error_at (parser->oacc_routine->loc,
38234 "%<#pragma acc routine%> not immediately followed by"
38235 " a single function declaration or definition");
38236 parser->oacc_routine = NULL;
38237 return;
38239 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38241 cp_ensure_no_oacc_routine (parser);
38242 return;
38245 if (oacc_get_fn_attrib (fndecl))
38247 error_at (parser->oacc_routine->loc,
38248 "%<#pragma acc routine%> already applied to %qD", fndecl);
38249 parser->oacc_routine = NULL;
38250 return;
38253 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38255 error_at (parser->oacc_routine->loc,
38256 TREE_USED (fndecl)
38257 ? G_("%<#pragma acc routine%> must be applied before use")
38258 : G_("%<#pragma acc routine%> must be applied before "
38259 "definition"));
38260 parser->oacc_routine = NULL;
38261 return;
38264 /* Process the routine's dimension clauses. */
38265 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38266 oacc_replace_fn_attrib (fndecl, dims);
38268 /* Add an "omp declare target" attribute. */
38269 DECL_ATTRIBUTES (fndecl)
38270 = tree_cons (get_identifier ("omp declare target"),
38271 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38273 /* Don't unset parser->oacc_routine here: we may still need it to
38274 diagnose wrong usage. But, remember that we've used this "#pragma acc
38275 routine". */
38276 parser->oacc_routine->fndecl_seen = true;
38280 /* Main entry point to OpenMP statement pragmas. */
38282 static void
38283 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38285 tree stmt;
38286 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38287 omp_clause_mask mask (0);
38289 switch (cp_parser_pragma_kind (pragma_tok))
38291 case PRAGMA_OACC_ATOMIC:
38292 cp_parser_omp_atomic (parser, pragma_tok);
38293 return;
38294 case PRAGMA_OACC_CACHE:
38295 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38296 break;
38297 case PRAGMA_OACC_DATA:
38298 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38299 break;
38300 case PRAGMA_OACC_ENTER_DATA:
38301 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38302 break;
38303 case PRAGMA_OACC_EXIT_DATA:
38304 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38305 break;
38306 case PRAGMA_OACC_HOST_DATA:
38307 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38308 break;
38309 case PRAGMA_OACC_KERNELS:
38310 case PRAGMA_OACC_PARALLEL:
38311 strcpy (p_name, "#pragma acc");
38312 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38313 if_p);
38314 break;
38315 case PRAGMA_OACC_LOOP:
38316 strcpy (p_name, "#pragma acc");
38317 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38318 if_p);
38319 break;
38320 case PRAGMA_OACC_UPDATE:
38321 stmt = cp_parser_oacc_update (parser, pragma_tok);
38322 break;
38323 case PRAGMA_OACC_WAIT:
38324 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38325 break;
38326 case PRAGMA_OMP_ATOMIC:
38327 cp_parser_omp_atomic (parser, pragma_tok);
38328 return;
38329 case PRAGMA_OMP_CRITICAL:
38330 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38331 break;
38332 case PRAGMA_OMP_DISTRIBUTE:
38333 strcpy (p_name, "#pragma omp");
38334 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38335 if_p);
38336 break;
38337 case PRAGMA_OMP_FOR:
38338 strcpy (p_name, "#pragma omp");
38339 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38340 if_p);
38341 break;
38342 case PRAGMA_OMP_MASTER:
38343 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38344 break;
38345 case PRAGMA_OMP_PARALLEL:
38346 strcpy (p_name, "#pragma omp");
38347 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38348 if_p);
38349 break;
38350 case PRAGMA_OMP_SECTIONS:
38351 strcpy (p_name, "#pragma omp");
38352 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38353 break;
38354 case PRAGMA_OMP_SIMD:
38355 strcpy (p_name, "#pragma omp");
38356 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38357 if_p);
38358 break;
38359 case PRAGMA_OMP_SINGLE:
38360 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38361 break;
38362 case PRAGMA_OMP_TASK:
38363 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38364 break;
38365 case PRAGMA_OMP_TASKGROUP:
38366 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38367 break;
38368 case PRAGMA_OMP_TASKLOOP:
38369 strcpy (p_name, "#pragma omp");
38370 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38371 if_p);
38372 break;
38373 case PRAGMA_OMP_TEAMS:
38374 strcpy (p_name, "#pragma omp");
38375 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38376 if_p);
38377 break;
38378 default:
38379 gcc_unreachable ();
38382 protected_set_expr_location (stmt, pragma_tok->location);
38385 /* Transactional Memory parsing routines. */
38387 /* Parse a transaction attribute.
38389 txn-attribute:
38390 attribute
38391 [ [ identifier ] ]
38393 We use this instead of cp_parser_attributes_opt for transactions to avoid
38394 the pedwarn in C++98 mode. */
38396 static tree
38397 cp_parser_txn_attribute_opt (cp_parser *parser)
38399 cp_token *token;
38400 tree attr_name, attr = NULL;
38402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38403 return cp_parser_attributes_opt (parser);
38405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38406 return NULL_TREE;
38407 cp_lexer_consume_token (parser->lexer);
38408 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38409 goto error1;
38411 token = cp_lexer_peek_token (parser->lexer);
38412 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38414 token = cp_lexer_consume_token (parser->lexer);
38416 attr_name = (token->type == CPP_KEYWORD
38417 /* For keywords, use the canonical spelling,
38418 not the parsed identifier. */
38419 ? ridpointers[(int) token->keyword]
38420 : token->u.value);
38421 attr = build_tree_list (attr_name, NULL_TREE);
38423 else
38424 cp_parser_error (parser, "expected identifier");
38426 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38427 error1:
38428 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38429 return attr;
38432 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38434 transaction-statement:
38435 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38436 compound-statement
38437 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38440 static tree
38441 cp_parser_transaction (cp_parser *parser, cp_token *token)
38443 unsigned char old_in = parser->in_transaction;
38444 unsigned char this_in = 1, new_in;
38445 enum rid keyword = token->keyword;
38446 tree stmt, attrs, noex;
38448 cp_lexer_consume_token (parser->lexer);
38450 if (keyword == RID_TRANSACTION_RELAXED
38451 || keyword == RID_SYNCHRONIZED)
38452 this_in |= TM_STMT_ATTR_RELAXED;
38453 else
38455 attrs = cp_parser_txn_attribute_opt (parser);
38456 if (attrs)
38457 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38460 /* Parse a noexcept specification. */
38461 if (keyword == RID_ATOMIC_NOEXCEPT)
38462 noex = boolean_true_node;
38463 else if (keyword == RID_ATOMIC_CANCEL)
38465 /* cancel-and-throw is unimplemented. */
38466 sorry ("atomic_cancel");
38467 noex = NULL_TREE;
38469 else
38470 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38472 /* Keep track if we're in the lexical scope of an outer transaction. */
38473 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38475 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38477 parser->in_transaction = new_in;
38478 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38479 parser->in_transaction = old_in;
38481 finish_transaction_stmt (stmt, NULL, this_in, noex);
38483 return stmt;
38486 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38488 transaction-expression:
38489 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38490 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38493 static tree
38494 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38496 unsigned char old_in = parser->in_transaction;
38497 unsigned char this_in = 1;
38498 cp_token *token;
38499 tree expr, noex;
38500 bool noex_expr;
38501 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38503 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38504 || keyword == RID_TRANSACTION_RELAXED);
38506 if (!flag_tm)
38507 error_at (loc,
38508 keyword == RID_TRANSACTION_RELAXED
38509 ? G_("%<__transaction_relaxed%> without transactional memory "
38510 "support enabled")
38511 : G_("%<__transaction_atomic%> without transactional memory "
38512 "support enabled"));
38514 token = cp_parser_require_keyword (parser, keyword,
38515 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38516 : RT_TRANSACTION_RELAXED));
38517 gcc_assert (token != NULL);
38519 if (keyword == RID_TRANSACTION_RELAXED)
38520 this_in |= TM_STMT_ATTR_RELAXED;
38522 /* Set this early. This might mean that we allow transaction_cancel in
38523 an expression that we find out later actually has to be a constexpr.
38524 However, we expect that cxx_constant_value will be able to deal with
38525 this; also, if the noexcept has no constexpr, then what we parse next
38526 really is a transaction's body. */
38527 parser->in_transaction = this_in;
38529 /* Parse a noexcept specification. */
38530 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38531 true);
38533 if (!noex || !noex_expr
38534 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38536 matching_parens parens;
38537 parens.require_open (parser);
38539 expr = cp_parser_expression (parser);
38540 expr = finish_parenthesized_expr (expr);
38542 parens.require_close (parser);
38544 else
38546 /* The only expression that is available got parsed for the noexcept
38547 already. noexcept is true then. */
38548 expr = noex;
38549 noex = boolean_true_node;
38552 expr = build_transaction_expr (token->location, expr, this_in, noex);
38553 parser->in_transaction = old_in;
38555 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38556 return error_mark_node;
38558 return (flag_tm ? expr : error_mark_node);
38561 /* Parse a function-transaction-block.
38563 function-transaction-block:
38564 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38565 function-body
38566 __transaction_atomic txn-attribute[opt] function-try-block
38567 __transaction_relaxed ctor-initializer[opt] function-body
38568 __transaction_relaxed function-try-block
38571 static void
38572 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38574 unsigned char old_in = parser->in_transaction;
38575 unsigned char new_in = 1;
38576 tree compound_stmt, stmt, attrs;
38577 cp_token *token;
38579 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38580 || keyword == RID_TRANSACTION_RELAXED);
38581 token = cp_parser_require_keyword (parser, keyword,
38582 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38583 : RT_TRANSACTION_RELAXED));
38584 gcc_assert (token != NULL);
38586 if (keyword == RID_TRANSACTION_RELAXED)
38587 new_in |= TM_STMT_ATTR_RELAXED;
38588 else
38590 attrs = cp_parser_txn_attribute_opt (parser);
38591 if (attrs)
38592 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38595 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38597 parser->in_transaction = new_in;
38599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38600 cp_parser_function_try_block (parser);
38601 else
38602 cp_parser_ctor_initializer_opt_and_function_body
38603 (parser, /*in_function_try_block=*/false);
38605 parser->in_transaction = old_in;
38607 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38610 /* Parse a __transaction_cancel statement.
38612 cancel-statement:
38613 __transaction_cancel txn-attribute[opt] ;
38614 __transaction_cancel txn-attribute[opt] throw-expression ;
38616 ??? Cancel and throw is not yet implemented. */
38618 static tree
38619 cp_parser_transaction_cancel (cp_parser *parser)
38621 cp_token *token;
38622 bool is_outer = false;
38623 tree stmt, attrs;
38625 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38626 RT_TRANSACTION_CANCEL);
38627 gcc_assert (token != NULL);
38629 attrs = cp_parser_txn_attribute_opt (parser);
38630 if (attrs)
38631 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38633 /* ??? Parse cancel-and-throw here. */
38635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38637 if (!flag_tm)
38639 error_at (token->location, "%<__transaction_cancel%> without "
38640 "transactional memory support enabled");
38641 return error_mark_node;
38643 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38645 error_at (token->location, "%<__transaction_cancel%> within a "
38646 "%<__transaction_relaxed%>");
38647 return error_mark_node;
38649 else if (is_outer)
38651 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38652 && !is_tm_may_cancel_outer (current_function_decl))
38654 error_at (token->location, "outer %<__transaction_cancel%> not "
38655 "within outer %<__transaction_atomic%>");
38656 error_at (token->location,
38657 " or a %<transaction_may_cancel_outer%> function");
38658 return error_mark_node;
38661 else if (parser->in_transaction == 0)
38663 error_at (token->location, "%<__transaction_cancel%> not within "
38664 "%<__transaction_atomic%>");
38665 return error_mark_node;
38668 stmt = build_tm_abort_call (token->location, is_outer);
38669 add_stmt (stmt);
38671 return stmt;
38674 /* The parser. */
38676 static GTY (()) cp_parser *the_parser;
38679 /* Special handling for the first token or line in the file. The first
38680 thing in the file might be #pragma GCC pch_preprocess, which loads a
38681 PCH file, which is a GC collection point. So we need to handle this
38682 first pragma without benefit of an existing lexer structure.
38684 Always returns one token to the caller in *FIRST_TOKEN. This is
38685 either the true first token of the file, or the first token after
38686 the initial pragma. */
38688 static void
38689 cp_parser_initial_pragma (cp_token *first_token)
38691 tree name = NULL;
38693 cp_lexer_get_preprocessor_token (NULL, first_token);
38694 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38695 return;
38697 cp_lexer_get_preprocessor_token (NULL, first_token);
38698 if (first_token->type == CPP_STRING)
38700 name = first_token->u.value;
38702 cp_lexer_get_preprocessor_token (NULL, first_token);
38703 if (first_token->type != CPP_PRAGMA_EOL)
38704 error_at (first_token->location,
38705 "junk at end of %<#pragma GCC pch_preprocess%>");
38707 else
38708 error_at (first_token->location, "expected string literal");
38710 /* Skip to the end of the pragma. */
38711 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38712 cp_lexer_get_preprocessor_token (NULL, first_token);
38714 /* Now actually load the PCH file. */
38715 if (name)
38716 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38718 /* Read one more token to return to our caller. We have to do this
38719 after reading the PCH file in, since its pointers have to be
38720 live. */
38721 cp_lexer_get_preprocessor_token (NULL, first_token);
38724 /* Parse a pragma GCC ivdep. */
38726 static bool
38727 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38729 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38730 return true;
38733 /* Parse a pragma GCC unroll. */
38735 static unsigned short
38736 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38738 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38739 tree expr = cp_parser_constant_expression (parser);
38740 unsigned short unroll;
38741 expr = maybe_constant_value (expr);
38742 HOST_WIDE_INT lunroll = 0;
38743 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38744 || TREE_CODE (expr) != INTEGER_CST
38745 || (lunroll = tree_to_shwi (expr)) < 0
38746 || lunroll >= USHRT_MAX)
38748 error_at (location, "%<#pragma GCC unroll%> requires an"
38749 " assignment-expression that evaluates to a non-negative"
38750 " integral constant less than %u", USHRT_MAX);
38751 unroll = 0;
38753 else
38755 unroll = (unsigned short)lunroll;
38756 if (unroll == 0)
38757 unroll = 1;
38759 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38760 return unroll;
38763 /* Normal parsing of a pragma token. Here we can (and must) use the
38764 regular lexer. */
38766 static bool
38767 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38769 cp_token *pragma_tok;
38770 unsigned int id;
38771 tree stmt;
38772 bool ret;
38774 pragma_tok = cp_lexer_consume_token (parser->lexer);
38775 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38776 parser->lexer->in_pragma = true;
38778 id = cp_parser_pragma_kind (pragma_tok);
38779 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38780 cp_ensure_no_omp_declare_simd (parser);
38781 switch (id)
38783 case PRAGMA_GCC_PCH_PREPROCESS:
38784 error_at (pragma_tok->location,
38785 "%<#pragma GCC pch_preprocess%> must be first");
38786 break;
38788 case PRAGMA_OMP_BARRIER:
38789 switch (context)
38791 case pragma_compound:
38792 cp_parser_omp_barrier (parser, pragma_tok);
38793 return false;
38794 case pragma_stmt:
38795 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38796 "used in compound statements", "omp barrier");
38797 break;
38798 default:
38799 goto bad_stmt;
38801 break;
38803 case PRAGMA_OMP_FLUSH:
38804 switch (context)
38806 case pragma_compound:
38807 cp_parser_omp_flush (parser, pragma_tok);
38808 return false;
38809 case pragma_stmt:
38810 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38811 "used in compound statements", "omp flush");
38812 break;
38813 default:
38814 goto bad_stmt;
38816 break;
38818 case PRAGMA_OMP_TASKWAIT:
38819 switch (context)
38821 case pragma_compound:
38822 cp_parser_omp_taskwait (parser, pragma_tok);
38823 return false;
38824 case pragma_stmt:
38825 error_at (pragma_tok->location,
38826 "%<#pragma %s%> may only be used in compound statements",
38827 "omp taskwait");
38828 break;
38829 default:
38830 goto bad_stmt;
38832 break;
38834 case PRAGMA_OMP_TASKYIELD:
38835 switch (context)
38837 case pragma_compound:
38838 cp_parser_omp_taskyield (parser, pragma_tok);
38839 return false;
38840 case pragma_stmt:
38841 error_at (pragma_tok->location,
38842 "%<#pragma %s%> may only be used in compound statements",
38843 "omp taskyield");
38844 break;
38845 default:
38846 goto bad_stmt;
38848 break;
38850 case PRAGMA_OMP_CANCEL:
38851 switch (context)
38853 case pragma_compound:
38854 cp_parser_omp_cancel (parser, pragma_tok);
38855 return false;
38856 case pragma_stmt:
38857 error_at (pragma_tok->location,
38858 "%<#pragma %s%> may only be used in compound statements",
38859 "omp cancel");
38860 break;
38861 default:
38862 goto bad_stmt;
38864 break;
38866 case PRAGMA_OMP_CANCELLATION_POINT:
38867 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38868 return false;
38870 case PRAGMA_OMP_THREADPRIVATE:
38871 cp_parser_omp_threadprivate (parser, pragma_tok);
38872 return false;
38874 case PRAGMA_OMP_DECLARE:
38875 return cp_parser_omp_declare (parser, pragma_tok, context);
38877 case PRAGMA_OACC_DECLARE:
38878 cp_parser_oacc_declare (parser, pragma_tok);
38879 return false;
38881 case PRAGMA_OACC_ENTER_DATA:
38882 if (context == pragma_stmt)
38884 error_at (pragma_tok->location,
38885 "%<#pragma %s%> may only be used in compound statements",
38886 "acc enter data");
38887 break;
38889 else if (context != pragma_compound)
38890 goto bad_stmt;
38891 cp_parser_omp_construct (parser, pragma_tok, if_p);
38892 return true;
38894 case PRAGMA_OACC_EXIT_DATA:
38895 if (context == pragma_stmt)
38897 error_at (pragma_tok->location,
38898 "%<#pragma %s%> may only be used in compound statements",
38899 "acc exit data");
38900 break;
38902 else if (context != pragma_compound)
38903 goto bad_stmt;
38904 cp_parser_omp_construct (parser, pragma_tok, if_p);
38905 return true;
38907 case PRAGMA_OACC_ROUTINE:
38908 if (context != pragma_external)
38910 error_at (pragma_tok->location,
38911 "%<#pragma acc routine%> must be at file scope");
38912 break;
38914 cp_parser_oacc_routine (parser, pragma_tok, context);
38915 return false;
38917 case PRAGMA_OACC_UPDATE:
38918 if (context == pragma_stmt)
38920 error_at (pragma_tok->location,
38921 "%<#pragma %s%> may only be used in compound statements",
38922 "acc update");
38923 break;
38925 else if (context != pragma_compound)
38926 goto bad_stmt;
38927 cp_parser_omp_construct (parser, pragma_tok, if_p);
38928 return true;
38930 case PRAGMA_OACC_WAIT:
38931 if (context == pragma_stmt)
38933 error_at (pragma_tok->location,
38934 "%<#pragma %s%> may only be used in compound statements",
38935 "acc wait");
38936 break;
38938 else if (context != pragma_compound)
38939 goto bad_stmt;
38940 cp_parser_omp_construct (parser, pragma_tok, if_p);
38941 return true;
38943 case PRAGMA_OACC_ATOMIC:
38944 case PRAGMA_OACC_CACHE:
38945 case PRAGMA_OACC_DATA:
38946 case PRAGMA_OACC_HOST_DATA:
38947 case PRAGMA_OACC_KERNELS:
38948 case PRAGMA_OACC_PARALLEL:
38949 case PRAGMA_OACC_LOOP:
38950 case PRAGMA_OMP_ATOMIC:
38951 case PRAGMA_OMP_CRITICAL:
38952 case PRAGMA_OMP_DISTRIBUTE:
38953 case PRAGMA_OMP_FOR:
38954 case PRAGMA_OMP_MASTER:
38955 case PRAGMA_OMP_PARALLEL:
38956 case PRAGMA_OMP_SECTIONS:
38957 case PRAGMA_OMP_SIMD:
38958 case PRAGMA_OMP_SINGLE:
38959 case PRAGMA_OMP_TASK:
38960 case PRAGMA_OMP_TASKGROUP:
38961 case PRAGMA_OMP_TASKLOOP:
38962 case PRAGMA_OMP_TEAMS:
38963 if (context != pragma_stmt && context != pragma_compound)
38964 goto bad_stmt;
38965 stmt = push_omp_privatization_clauses (false);
38966 cp_parser_omp_construct (parser, pragma_tok, if_p);
38967 pop_omp_privatization_clauses (stmt);
38968 return true;
38970 case PRAGMA_OMP_ORDERED:
38971 if (context != pragma_stmt && context != pragma_compound)
38972 goto bad_stmt;
38973 stmt = push_omp_privatization_clauses (false);
38974 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38975 pop_omp_privatization_clauses (stmt);
38976 return ret;
38978 case PRAGMA_OMP_TARGET:
38979 if (context != pragma_stmt && context != pragma_compound)
38980 goto bad_stmt;
38981 stmt = push_omp_privatization_clauses (false);
38982 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38983 pop_omp_privatization_clauses (stmt);
38984 return ret;
38986 case PRAGMA_OMP_END_DECLARE_TARGET:
38987 cp_parser_omp_end_declare_target (parser, pragma_tok);
38988 return false;
38990 case PRAGMA_OMP_SECTION:
38991 error_at (pragma_tok->location,
38992 "%<#pragma omp section%> may only be used in "
38993 "%<#pragma omp sections%> construct");
38994 break;
38996 case PRAGMA_IVDEP:
38998 if (context == pragma_external)
39000 error_at (pragma_tok->location,
39001 "%<#pragma GCC ivdep%> must be inside a function");
39002 break;
39004 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39005 unsigned short unroll;
39006 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39007 if (tok->type == CPP_PRAGMA
39008 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39010 tok = cp_lexer_consume_token (parser->lexer);
39011 unroll = cp_parser_pragma_unroll (parser, tok);
39012 tok = cp_lexer_peek_token (the_parser->lexer);
39014 else
39015 unroll = 0;
39016 if (tok->type != CPP_KEYWORD
39017 || (tok->keyword != RID_FOR
39018 && tok->keyword != RID_WHILE
39019 && tok->keyword != RID_DO))
39021 cp_parser_error (parser, "for, while or do statement expected");
39022 return false;
39024 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39025 return true;
39028 case PRAGMA_UNROLL:
39030 if (context == pragma_external)
39032 error_at (pragma_tok->location,
39033 "%<#pragma GCC unroll%> must be inside a function");
39034 break;
39036 const unsigned short unroll
39037 = cp_parser_pragma_unroll (parser, pragma_tok);
39038 bool ivdep;
39039 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39040 if (tok->type == CPP_PRAGMA
39041 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39043 tok = cp_lexer_consume_token (parser->lexer);
39044 ivdep = cp_parser_pragma_ivdep (parser, tok);
39045 tok = cp_lexer_peek_token (the_parser->lexer);
39047 else
39048 ivdep = false;
39049 if (tok->type != CPP_KEYWORD
39050 || (tok->keyword != RID_FOR
39051 && tok->keyword != RID_WHILE
39052 && tok->keyword != RID_DO))
39054 cp_parser_error (parser, "for, while or do statement expected");
39055 return false;
39057 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39058 return true;
39061 default:
39062 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39063 c_invoke_pragma_handler (id);
39064 break;
39066 bad_stmt:
39067 cp_parser_error (parser, "expected declaration specifiers");
39068 break;
39071 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39072 return false;
39075 /* The interface the pragma parsers have to the lexer. */
39077 enum cpp_ttype
39078 pragma_lex (tree *value, location_t *loc)
39080 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39081 enum cpp_ttype ret = tok->type;
39083 *value = tok->u.value;
39084 if (loc)
39085 *loc = tok->location;
39087 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39088 ret = CPP_EOF;
39089 else if (ret == CPP_STRING)
39090 *value = cp_parser_string_literal (the_parser, false, false);
39091 else
39093 if (ret == CPP_KEYWORD)
39094 ret = CPP_NAME;
39095 cp_lexer_consume_token (the_parser->lexer);
39098 return ret;
39102 /* External interface. */
39104 /* Parse one entire translation unit. */
39106 void
39107 c_parse_file (void)
39109 static bool already_called = false;
39111 if (already_called)
39112 fatal_error (input_location,
39113 "inter-module optimizations not implemented for C++");
39114 already_called = true;
39116 the_parser = cp_parser_new ();
39117 push_deferring_access_checks (flag_access_control
39118 ? dk_no_deferred : dk_no_check);
39119 cp_parser_translation_unit (the_parser);
39120 the_parser = NULL;
39123 /* Create an identifier for a generic parameter type (a synthesized
39124 template parameter implied by `auto' or a concept identifier). */
39126 static GTY(()) int generic_parm_count;
39127 static tree
39128 make_generic_type_name ()
39130 char buf[32];
39131 sprintf (buf, "auto:%d", ++generic_parm_count);
39132 return get_identifier (buf);
39135 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39136 (creating a new template parameter list if necessary). Returns the newly
39137 created template type parm. */
39139 static tree
39140 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39142 gcc_assert (current_binding_level->kind == sk_function_parms);
39144 /* Before committing to modifying any scope, if we're in an
39145 implicit template scope, and we're trying to synthesize a
39146 constrained parameter, try to find a previous parameter with
39147 the same name. This is the same-type rule for abbreviated
39148 function templates.
39150 NOTE: We can generate implicit parameters when tentatively
39151 parsing a nested name specifier, only to reject that parse
39152 later. However, matching the same template-id as part of a
39153 direct-declarator should generate an identical template
39154 parameter, so this rule will merge them. */
39155 if (parser->implicit_template_scope && constr)
39157 tree t = parser->implicit_template_parms;
39158 while (t)
39160 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39162 tree d = TREE_VALUE (t);
39163 if (TREE_CODE (d) == PARM_DECL)
39164 /* Return the TEMPLATE_PARM_INDEX. */
39165 d = DECL_INITIAL (d);
39166 return d;
39168 t = TREE_CHAIN (t);
39172 /* We are either continuing a function template that already contains implicit
39173 template parameters, creating a new fully-implicit function template, or
39174 extending an existing explicit function template with implicit template
39175 parameters. */
39177 cp_binding_level *const entry_scope = current_binding_level;
39179 bool become_template = false;
39180 cp_binding_level *parent_scope = 0;
39182 if (parser->implicit_template_scope)
39184 gcc_assert (parser->implicit_template_parms);
39186 current_binding_level = parser->implicit_template_scope;
39188 else
39190 /* Roll back to the existing template parameter scope (in the case of
39191 extending an explicit function template) or introduce a new template
39192 parameter scope ahead of the function parameter scope (or class scope
39193 in the case of out-of-line member definitions). The function scope is
39194 added back after template parameter synthesis below. */
39196 cp_binding_level *scope = entry_scope;
39198 while (scope->kind == sk_function_parms)
39200 parent_scope = scope;
39201 scope = scope->level_chain;
39203 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39205 /* If not defining a class, then any class scope is a scope level in
39206 an out-of-line member definition. In this case simply wind back
39207 beyond the first such scope to inject the template parameter list.
39208 Otherwise wind back to the class being defined. The latter can
39209 occur in class member friend declarations such as:
39211 class A {
39212 void foo (auto);
39214 class B {
39215 friend void A::foo (auto);
39218 The template parameter list synthesized for the friend declaration
39219 must be injected in the scope of 'B'. This can also occur in
39220 erroneous cases such as:
39222 struct A {
39223 struct B {
39224 void foo (auto);
39226 void B::foo (auto) {}
39229 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39230 but, nevertheless, the template parameter list synthesized for the
39231 declarator should be injected into the scope of 'A' as if the
39232 ill-formed template was specified explicitly. */
39234 while (scope->kind == sk_class && !scope->defining_class_p)
39236 parent_scope = scope;
39237 scope = scope->level_chain;
39241 current_binding_level = scope;
39243 if (scope->kind != sk_template_parms
39244 || !function_being_declared_is_template_p (parser))
39246 /* Introduce a new template parameter list for implicit template
39247 parameters. */
39249 become_template = true;
39251 parser->implicit_template_scope
39252 = begin_scope (sk_template_parms, NULL);
39254 ++processing_template_decl;
39256 parser->fully_implicit_function_template_p = true;
39257 ++parser->num_template_parameter_lists;
39259 else
39261 /* Synthesize implicit template parameters at the end of the explicit
39262 template parameter list. */
39264 gcc_assert (current_template_parms);
39266 parser->implicit_template_scope = scope;
39268 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39269 parser->implicit_template_parms
39270 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39274 /* Synthesize a new template parameter and track the current template
39275 parameter chain with implicit_template_parms. */
39277 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39278 tree synth_id = make_generic_type_name ();
39279 tree synth_tmpl_parm;
39280 bool non_type = false;
39282 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39283 synth_tmpl_parm
39284 = finish_template_type_parm (class_type_node, synth_id);
39285 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39286 synth_tmpl_parm
39287 = finish_constrained_template_template_parm (proto, synth_id);
39288 else
39290 synth_tmpl_parm = copy_decl (proto);
39291 DECL_NAME (synth_tmpl_parm) = synth_id;
39292 non_type = true;
39295 // Attach the constraint to the parm before processing.
39296 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39297 TREE_TYPE (node) = constr;
39298 tree new_parm
39299 = process_template_parm (parser->implicit_template_parms,
39300 input_location,
39301 node,
39302 /*non_type=*/non_type,
39303 /*param_pack=*/false);
39305 // Chain the new parameter to the list of implicit parameters.
39306 if (parser->implicit_template_parms)
39307 parser->implicit_template_parms
39308 = TREE_CHAIN (parser->implicit_template_parms);
39309 else
39310 parser->implicit_template_parms = new_parm;
39312 tree new_decl = get_local_decls ();
39313 if (non_type)
39314 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39315 new_decl = DECL_INITIAL (new_decl);
39317 /* If creating a fully implicit function template, start the new implicit
39318 template parameter list with this synthesized type, otherwise grow the
39319 current template parameter list. */
39321 if (become_template)
39323 parent_scope->level_chain = current_binding_level;
39325 tree new_parms = make_tree_vec (1);
39326 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39327 current_template_parms = tree_cons (size_int (processing_template_decl),
39328 new_parms, current_template_parms);
39330 else
39332 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39333 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39334 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39335 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39338 // If the new parameter was constrained, we need to add that to the
39339 // constraints in the template parameter list.
39340 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39342 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39343 reqs = conjoin_constraints (reqs, req);
39344 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39347 current_binding_level = entry_scope;
39349 return new_decl;
39352 /* Finish the declaration of a fully implicit function template. Such a
39353 template has no explicit template parameter list so has not been through the
39354 normal template head and tail processing. synthesize_implicit_template_parm
39355 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39356 provided if the declaration is a class member such that its template
39357 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39358 form is returned. Otherwise NULL_TREE is returned. */
39360 static tree
39361 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39363 gcc_assert (parser->fully_implicit_function_template_p);
39365 if (member_decl_opt && member_decl_opt != error_mark_node
39366 && DECL_VIRTUAL_P (member_decl_opt))
39368 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39369 "implicit templates may not be %<virtual%>");
39370 DECL_VIRTUAL_P (member_decl_opt) = false;
39373 if (member_decl_opt)
39374 member_decl_opt = finish_member_template_decl (member_decl_opt);
39375 end_template_decl ();
39377 parser->fully_implicit_function_template_p = false;
39378 parser->implicit_template_parms = 0;
39379 parser->implicit_template_scope = 0;
39380 --parser->num_template_parameter_lists;
39382 return member_decl_opt;
39385 /* Like finish_fully_implicit_template, but to be used in error
39386 recovery, rearranging scopes so that we restore the state we had
39387 before synthesize_implicit_template_parm inserted the implement
39388 template parms scope. */
39390 static void
39391 abort_fully_implicit_template (cp_parser *parser)
39393 cp_binding_level *return_to_scope = current_binding_level;
39395 if (parser->implicit_template_scope
39396 && return_to_scope != parser->implicit_template_scope)
39398 cp_binding_level *child = return_to_scope;
39399 for (cp_binding_level *scope = child->level_chain;
39400 scope != parser->implicit_template_scope;
39401 scope = child->level_chain)
39402 child = scope;
39403 child->level_chain = parser->implicit_template_scope->level_chain;
39404 parser->implicit_template_scope->level_chain = return_to_scope;
39405 current_binding_level = parser->implicit_template_scope;
39407 else
39408 return_to_scope = return_to_scope->level_chain;
39410 finish_fully_implicit_template (parser, NULL);
39412 gcc_assert (current_binding_level == return_to_scope);
39415 /* Helper function for diagnostics that have complained about things
39416 being used with 'extern "C"' linkage.
39418 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39420 void
39421 maybe_show_extern_c_location (void)
39423 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39424 inform (the_parser->innermost_linkage_specification_location,
39425 "%<extern \"C\"%> linkage started here");
39428 #include "gt-cp-parser.h"